Subversion Repositories Kolibri OS

Rev

Rev 1514 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.
  4. ;; PROGRAMMING:
  5. ;; Ivan Poddubny
  6. ;; Marat Zakiyanov (Mario79)
  7. ;; VaStaNi
  8. ;; Trans
  9. ;; Mihail Semenyako (mike.dld)
  10. ;; Sergey Kuzmin (Wildwest)
  11. ;; Andrey Halyavin (halyavin)
  12. ;; Mihail Lisovin (Mihasik)
  13. ;; Andrey Ignatiev (andrew_programmer)
  14. ;; NoName
  15. ;; Evgeny Grechnikov (Diamond)
  16. ;; Iliya Mihailov (Ghost)
  17. ;; Sergey Semyonov (Serge)
  18. ;; Johnny_B
  19. ;; SPraid (simba)
  20. ;; Hidnplayr
  21. ;; Alexey Teplov (<Lrz>)
  22. ;;
  23. ;; Data in this file was originally part of MenuetOS project which is
  24. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  25. ;; part of KolibriOS project under the terms of GNU GPL.
  26. ;;
  27. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  28. ;; PROGRAMMING:
  29. ;;
  30. ;; Ville Mikael Turjanmaa, villemt@itu.jyu.fi
  31. ;; - main os coding/design
  32. ;; Jan-Michael Brummer, BUZZ2@gmx.de
  33. ;; Felix Kaiser, info@felix-kaiser.de
  34. ;; Paolo Minazzi, paolo.minazzi@inwind.it
  35. ;; quickcode@mail.ru
  36. ;; Alexey, kgaz@crosswinds.net
  37. ;; Juan M. Caravaca, bitrider@wanadoo.es
  38. ;; kristol@nic.fi
  39. ;; Mike Hibbett, mikeh@oceanfree.net
  40. ;; Lasse Kuusijarvi, kuusijar@lut.fi
  41. ;; Jarek Pelczar, jarekp3@wp.pl
  42. ;;
  43. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  44. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  45. ;; consequences of using it or for whether it serves any particular purpose or
  46. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  47. ;; License (the "GPL") for full details.
  48. ;
  49. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  50. ;; but only under the conditions described in the GPL. A copy of this license
  51. ;; is supposed to have been given to you along with KolibriOS so you can know
  52. ;; your rights and responsibilities. It should be in a file named COPYING.
  53. ;; Among other things, the copyright notice and this notice must be preserved
  54. ;; on all copies.
  55. ;;
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57.  
  58. include 'macros.inc'
  59. include 'struct.inc'
  60.  
  61. $Revision: 2305 $
  62.  
  63.  
  64. USE_COM_IRQ     equ 1      ; make irq 3 and irq 4 available for PCI devices
  65.  
  66. ; Enabling the next line will enable serial output console
  67. debug_com_base  equ 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3, 0x2e8 is com4, no irq's are used
  68.  
  69. include "proc32.inc"
  70. include "kglobals.inc"
  71. lang fix en
  72.  
  73. include "const.inc"
  74. max_processes    equ   255
  75. tss_step         equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
  76.  
  77.  
  78. os_stack       equ  (os_data_l-gdts)    ; GDTs
  79. os_code        equ  (os_code_l-gdts)
  80. graph_data     equ  (3+graph_data_l-gdts)
  81. tss0           equ  (tss0_l-gdts)
  82. app_code       equ  (3+app_code_l-gdts)
  83. app_data       equ  (3+app_data_l-gdts)
  84. app_tls        equ  (3+tls_data_l-gdts)
  85. pci_code_sel   equ  (pci_code_32-gdts)
  86. pci_data_sel   equ  (pci_data_32-gdts)
  87.  
  88.  
  89. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  90. ;;
  91. ;;   Included files:
  92. ;;
  93. ;;   Kernel16.inc
  94. ;;    - Booteng.inc   English text for bootup
  95. ;;    - Bootcode.inc  Hardware setup
  96. ;;    - Pci16.inc     PCI functions
  97. ;;
  98. ;;   Kernel32.inc
  99. ;;    - Sys32.inc     Process management
  100. ;;    - Shutdown.inc  Shutdown and restart
  101. ;;    - Fat32.inc     Read / write hd
  102. ;;    - Vesa12.inc    Vesa 1.2 driver
  103. ;;    - Vesa20.inc    Vesa 2.0 driver
  104. ;;    - Vga.inc       VGA driver
  105. ;;    - Stack.inc     Network interface
  106. ;;    - Mouse.inc     Mouse pointer
  107. ;;    - Scincode.inc  Window skinning
  108. ;;    - Pci32.inc     PCI functions
  109. ;;
  110. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  111.  
  112.  
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  114. ;;                                                                      ;;
  115. ;;                  16 BIT ENTRY FROM BOOTSECTOR                        ;;
  116. ;;                                                                      ;;
  117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  118.  
  119. use16
  120.                   org   0x0
  121.                   jmp   start_of_code
  122.  
  123. version db    'Kolibri OS  version 0.7.7.0+     ',13,10,13,10,0
  124.  
  125. include "boot/bootstr.inc"     ; language-independent boot messages
  126. include "boot/preboot.inc"
  127.  
  128. if lang eq en
  129. include "boot/booteng.inc"     ; english system boot messages
  130. else if lang eq ru
  131. include "boot/bootru.inc"      ; russian system boot messages
  132. include "boot/ru.inc"          ; Russian font
  133. else if lang eq et
  134. include "boot/bootet.inc"      ; estonian system boot messages
  135. include "boot/et.inc"          ; Estonian font
  136. else
  137. include "boot/bootge.inc"      ; german system boot messages
  138. end if
  139.  
  140. include "boot/bootcode.inc"    ; 16 bit system boot code
  141. include "bus/pci/pci16.inc"
  142. include "detect/biosdisk.inc"
  143.  
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145. ;;                                                                      ;;
  146. ;;                  SWITCH TO 32 BIT PROTECTED MODE                     ;;
  147. ;;                                                                      ;;
  148. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  149.  
  150.  
  151. ; CR0 Flags - Protected mode and Paging
  152.  
  153.         mov ecx, CR0_PE
  154.  
  155. ; Enabling 32 bit protected mode
  156.  
  157.         sidt    [cs:old_ints_h]
  158.  
  159.         cli                             ; disable all irqs
  160.         cld
  161.         mov     al,255                  ; mask all irqs
  162.         out     0xa1,al
  163.         out     0x21,al
  164.    l.5: in      al, 0x64                ; Enable A20
  165.         test    al, 2
  166.         jnz     l.5
  167.         mov     al, 0xD1
  168.         out     0x64, al
  169.    l.6: in      al, 0x64
  170.         test    al, 2
  171.         jnz     l.6
  172.         mov     al, 0xDF
  173.         out     0x60, al
  174.    l.7: in      al, 0x64
  175.         test    al, 2
  176.         jnz     l.7
  177.         mov     al, 0xFF
  178.         out     0x64, al
  179.  
  180.         lgdt    [cs:tmp_gdt]            ; Load GDT
  181.         mov     eax, cr0                ; protected mode
  182.         or      eax, ecx
  183.         and     eax, 10011111b *65536*256 + 0xffffff ; caching enabled
  184.         mov     cr0, eax
  185.         jmp     pword os_code:B32       ; jmp to enable 32 bit mode
  186.  
  187. align 8
  188. tmp_gdt:
  189.  
  190.         dw     23
  191.         dd     tmp_gdt+0x10000
  192.         dw     0
  193.  
  194.         dw     0xffff
  195.         dw     0x0000
  196.         db     0x00
  197.         dw     11011111b *256 +10011010b
  198.         db     0x00
  199.  
  200.         dw     0xffff
  201.         dw     0x0000
  202.         db     0x00
  203.         dw     11011111b *256 +10010010b
  204.         db     0x00
  205.  
  206. include "data16.inc"
  207.  
  208. use32
  209. org $+0x10000
  210.  
  211. align 4
  212. B32:
  213.            mov   ax,os_stack       ; Selector for os
  214.            mov   ds,ax
  215.            mov   es,ax
  216.            mov   fs,ax
  217.            mov   gs,ax
  218.            mov   ss,ax
  219.            mov   esp,0x3ec00       ; Set stack
  220.  
  221. ; CLEAR 0x280000 - HEAP_BASE
  222.  
  223.            xor   eax,eax
  224.            mov   edi,0x280000
  225.            mov   ecx,(HEAP_BASE-OS_BASE-0x280000) / 4
  226.            cld
  227.            rep   stosd
  228.  
  229.            mov   edi,0x40000
  230.            mov   ecx,(0x90000-0x40000)/4
  231.            rep   stosd
  232.  
  233. ; CLEAR KERNEL UNDEFINED GLOBALS
  234.            mov   edi, endofcode-OS_BASE
  235.            mov   ecx, (uglobals_size/4)+4
  236.            rep   stosd
  237.  
  238. ; SAVE & CLEAR 0-0xffff
  239.  
  240.            xor   esi, esi
  241.            mov   edi,0x2F0000
  242.            mov   ecx,0x10000 / 4
  243.            rep   movsd
  244.            mov   edi,0x1000
  245.            mov   ecx,0xf000 / 4
  246.            rep   stosd
  247.  
  248.            call test_cpu
  249.            bts [cpu_caps-OS_BASE], CAPS_TSC     ;force use rdtsc
  250.  
  251.            call init_BIOS32
  252. ; MEMORY MODEL
  253.            call mem_test
  254.            call init_mem
  255.            call init_page_map
  256.  
  257. ; ENABLE PAGING
  258.  
  259.            mov eax, sys_pgdir-OS_BASE
  260.            mov cr3, eax
  261.  
  262.            mov eax,cr0
  263.            or eax,CR0_PG+CR0_WP
  264.            mov cr0,eax
  265.  
  266.            lgdt [gdts]
  267.            jmp pword os_code:high_code
  268.  
  269. align 4
  270. bios32_entry    dd ?
  271. tmp_page_tabs   dd ?
  272.  
  273. use16
  274. org $-0x10000
  275. include "boot/shutdown.inc" ; shutdown or restart
  276. org $+0x10000
  277. use32
  278.  
  279. __DEBUG__ fix 1
  280. __DEBUG_LEVEL__ fix 1
  281. include 'init.inc'
  282.  
  283. org OS_BASE+$
  284.  
  285. align 4
  286. high_code:
  287.            mov ax, os_stack
  288.            mov bx, app_data
  289.            mov cx, app_tls
  290.            mov ss, ax
  291.            add esp, OS_BASE
  292.  
  293.            mov ds, bx
  294.            mov es, bx
  295.            mov fs, cx
  296.            mov gs, bx
  297.  
  298.            bt [cpu_caps], CAPS_PGE
  299.            jnc @F
  300.  
  301.            or dword [sys_pgdir+(OS_BASE shr 20)], PG_GLOBAL
  302.  
  303.            mov ebx, cr4
  304.            or ebx, CR4_PGE
  305.            mov cr4, ebx
  306. @@:
  307.            xor eax, eax
  308.            mov dword [sys_pgdir], eax
  309.            mov dword [sys_pgdir+4], eax
  310.  
  311.            mov eax, cr3
  312.            mov cr3, eax           ; flush TLB
  313.  
  314. ; SAVE REAL MODE VARIABLES
  315.         mov     ax, [BOOT_VAR + 0x9031]
  316.         mov     [IDEContrRegsBaseAddr], ax
  317. ; --------------- APM ---------------------
  318.  
  319. ; init selectors
  320.     mov ebx,[BOOT_VAR+0x9040]                   ; offset of APM entry point
  321.     movzx eax,word [BOOT_VAR+0x9050]    ; real-mode segment base address of
  322.                                                                                 ; protected-mode 32-bit code segment
  323.     movzx ecx,word [BOOT_VAR+0x9052]    ; real-mode segment base address of
  324.                                                                                 ; protected-mode 16-bit code segment
  325.     movzx edx,word [BOOT_VAR+0x9054]    ; real-mode segment base address of
  326.                                                                                 ; protected-mode 16-bit data segment
  327.  
  328.     shl    eax, 4
  329.     mov    [dword apm_code_32 + 2], ax
  330.     shr    eax, 16
  331.     mov    [dword apm_code_32 + 4], al
  332.  
  333.     shl    ecx, 4
  334.     mov    [dword apm_code_16 + 2], cx
  335.     shr    ecx, 16
  336.     mov    [dword apm_code_16 + 4], cl
  337.  
  338.     shl    edx, 4
  339.     mov    [dword apm_data_16 + 2], dx
  340.     shr    edx, 16
  341.     mov    [dword apm_data_16 + 4], dl
  342.  
  343.     mov    dword[apm_entry], ebx
  344.     mov    word [apm_entry + 4], apm_code_32 - gdts
  345.  
  346.     mov    eax, [BOOT_VAR + 0x9044]    ; version & flags
  347.     mov    [apm_vf], eax
  348. ; -----------------------------------------
  349. ;        movzx eax,byte [BOOT_VAR+0x9010]       ; mouse port
  350. ;        mov   [0xF604],byte 1  ;al
  351.                 mov     al, [BOOT_VAR+0x901F]           ; DMA access
  352.         mov     [allow_dma_access], al
  353.         movzx eax, byte [BOOT_VAR+0x9000]        ; bpp
  354.         mov [ScreenBPP],al
  355.  
  356.         mov [_display.bpp], eax
  357.         mov [_display.vrefresh], 60
  358.         mov [_display.disable_mouse],  __sys_disable_mouse
  359.  
  360.         movzx eax,word [BOOT_VAR+0x900A]  ; X max
  361.         mov [_display.width], eax
  362.         dec   eax
  363.         mov   [Screen_Max_X],eax
  364.         mov   [screen_workarea.right],eax
  365.         movzx eax,word [BOOT_VAR+0x900C]  ; Y max
  366.         mov [_display.height], eax
  367.         dec   eax
  368.         mov   [Screen_Max_Y],eax
  369.         mov   [screen_workarea.bottom],eax
  370.         movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  371.         mov   [SCR_MODE],eax
  372.         mov   eax,[BOOT_VAR+0x9014]       ; Vesa 1.2 bnk sw add
  373.         mov   [BANK_SWITCH],eax
  374.         mov   [BytesPerScanLine],word 640*4         ; Bytes PerScanLine
  375.         cmp   [SCR_MODE],word 0x13          ; 320x200
  376.         je    @f
  377.         cmp   [SCR_MODE],word 0x12          ; VGA 640x480
  378.         je    @f
  379.         movzx eax, word[BOOT_VAR+0x9001]        ; for other modes
  380.         mov   [BytesPerScanLine],ax
  381.         mov [_display.pitch], eax
  382. @@:
  383.         mov eax, [_display.width]
  384.         mul [_display.height]
  385.         mov [_WinMapSize], eax
  386.  
  387.         mov     esi, BOOT_VAR+0x9080
  388.         movzx   ecx, byte [esi-1]
  389.         mov     [NumBiosDisks], ecx
  390.         mov     edi, BiosDisksData
  391.         rep     movsd
  392.  
  393. ; GRAPHICS ADDRESSES
  394.  
  395.         and     byte [BOOT_VAR+0x901e],0x0
  396.         mov     eax,[BOOT_VAR+0x9018]
  397.         mov     [LFBAddress],eax
  398.  
  399.         cmp     [SCR_MODE],word 0100000000000000b
  400.         jge     setvesa20
  401.         cmp     [SCR_MODE],word 0x13
  402.         je      v20ga32
  403.         mov     [PUTPIXEL],dword Vesa12_putpixel24  ; Vesa 1.2
  404.         mov     [GETPIXEL],dword Vesa12_getpixel24
  405.         cmp     [ScreenBPP],byte 24
  406.         jz      ga24
  407.         mov     [PUTPIXEL],dword Vesa12_putpixel32
  408.         mov     [GETPIXEL],dword Vesa12_getpixel32
  409.       ga24:
  410.         jmp     v20ga24
  411.       setvesa20:
  412.         mov     [PUTPIXEL],dword Vesa20_putpixel24  ; Vesa 2.0
  413.         mov     [GETPIXEL],dword Vesa20_getpixel24
  414.         cmp     [ScreenBPP],byte 24
  415.         jz      v20ga24
  416.       v20ga32:
  417.         mov     [PUTPIXEL],dword Vesa20_putpixel32
  418.         mov     [GETPIXEL],dword Vesa20_getpixel32
  419.       v20ga24:
  420.         cmp     [SCR_MODE],word 0x12                ; 16 C VGA 640x480
  421.         jne     no_mode_0x12
  422.         mov     [PUTPIXEL],dword VGA_putpixel
  423.         mov     [GETPIXEL],dword Vesa20_getpixel32
  424.       no_mode_0x12:
  425.  
  426. ; -------- Fast System Call init ----------
  427. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  428.            bt [cpu_caps], CAPS_SEP
  429.            jnc .SEnP   ; SysEnter not Present
  430.            xor edx, edx
  431.            mov ecx, MSR_SYSENTER_CS
  432.            mov eax, os_code
  433.            wrmsr
  434.            mov ecx, MSR_SYSENTER_ESP
  435. ;           mov eax, sysenter_stack ; Check it
  436.            xor     eax, eax
  437.            wrmsr
  438.            mov ecx, MSR_SYSENTER_EIP
  439.            mov eax, sysenter_entry
  440.            wrmsr
  441. .SEnP:
  442. ; AMD SYSCALL/SYSRET
  443.            cmp byte[cpu_vendor], 'A'
  444.            jne .noSYSCALL
  445.            mov eax, 0x80000001
  446.            cpuid
  447.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  448.            jz .noSYSCALL
  449.            mov ecx, MSR_AMD_EFER
  450.            rdmsr
  451.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  452.            wrmsr
  453.  
  454.         ; !!!! It`s dirty hack, fix it !!!
  455.         ; Bits of EDX :
  456.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  457.         ;  and the contents of this field, plus 8, are copied into the SS register.
  458.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  459.         ;  and the contents of this field, plus 8, are copied into the SS register.
  460.  
  461.         ; mov   edx, (os_code + 16) * 65536 + os_code
  462.            mov edx, 0x1B0008
  463.  
  464.            mov eax, syscall_entry
  465.            mov ecx, MSR_AMD_STAR
  466.            wrmsr
  467. .noSYSCALL:
  468. ; -----------------------------------------
  469.         stdcall alloc_page
  470.         stdcall map_page, tss-0xF80, eax, PG_SW
  471.         stdcall alloc_page
  472.         inc     eax
  473.         mov     [SLOT_BASE+256+APPDATA.io_map], eax
  474.         stdcall map_page, tss+0x80, eax, PG_SW
  475.         stdcall alloc_page
  476.         inc     eax
  477.         mov     dword [SLOT_BASE+256+APPDATA.io_map+4], eax
  478.         stdcall map_page, tss+0x1080, eax, PG_SW
  479.  
  480. ; LOAD IDT
  481.  
  482.            call build_interrupt_table ;lidt is executed
  483.           ;lidt [idtreg]
  484.  
  485.            call init_kernel_heap
  486.            stdcall kernel_alloc, RING0_STACK_SIZE+512
  487.            mov [os_stack_seg], eax
  488.  
  489.            lea esp, [eax+RING0_STACK_SIZE]
  490.  
  491.            mov [tss._ss0], os_stack
  492.            mov [tss._esp0], esp
  493.            mov [tss._esp], esp
  494.            mov [tss._cs],os_code
  495.            mov [tss._ss],os_stack
  496.            mov [tss._ds],app_data
  497.            mov [tss._es],app_data
  498.            mov [tss._fs],app_data
  499.            mov [tss._gs],app_data
  500.            mov [tss._io],128
  501. ;Add IO access table - bit array of permitted ports
  502.            mov edi, tss._io_map_0
  503.            xor eax, eax
  504.            not eax
  505.            mov ecx, 8192/4
  506.            rep stosd                 ; access to 4096*8=65536 ports
  507.  
  508.            mov  ax,tss0
  509.            ltr  ax
  510.  
  511.            mov [LFBSize], 0x800000
  512.            call init_LFB
  513.            call init_fpu
  514.            call init_malloc
  515.  
  516.            stdcall alloc_kernel_space, 0x51000
  517.            mov [default_io_map], eax
  518.  
  519.            add eax, 0x2000
  520.            mov [ipc_tmp], eax
  521.            mov ebx, 0x1000
  522.  
  523.            add eax, 0x40000
  524.            mov [proc_mem_map], eax
  525.  
  526.            add eax, 0x8000
  527.            mov [proc_mem_pdir], eax
  528.  
  529.            add eax, ebx
  530.            mov [proc_mem_tab], eax
  531.  
  532.            add eax, ebx
  533.            mov [tmp_task_pdir], eax
  534.  
  535.            add eax, ebx
  536.            mov [tmp_task_ptab], eax
  537.  
  538.            add eax, ebx
  539.            mov [ipc_pdir], eax
  540.  
  541.            add eax, ebx
  542.            mov [ipc_ptab], eax
  543.  
  544.            stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
  545.                                  (unpack.lc+unpack.lp)))*4
  546.  
  547.            mov [unpack.p], eax
  548.  
  549.            call init_events
  550.            mov eax, srv.fd-SRV_FD_OFFSET
  551.            mov [srv.fd], eax
  552.            mov [srv.bk], eax
  553.  
  554.            mov edi, irq_tab
  555.            xor eax, eax
  556.            mov ecx, 16
  557.            rep stosd
  558.  
  559. ;Set base of graphic segment to linear address of LFB
  560.         mov     eax,[LFBAddress]          ; set for gs
  561.         mov     [graph_data_l+2],ax
  562.         shr     eax,16
  563.         mov     [graph_data_l+4],al
  564.         mov     [graph_data_l+7],ah
  565.  
  566.         stdcall kernel_alloc, [_WinMapSize]
  567.         mov [_WinMapAddress], eax
  568.  
  569.         xor  eax,eax
  570.         inc  eax
  571.         mov [CURRENT_TASK],eax          ;dword 1
  572.         mov [TASK_COUNT],eax            ;dword 1
  573.         mov [TASK_BASE],dword TASK_DATA
  574.         mov [current_slot], SLOT_BASE+256
  575.  
  576. ; set background
  577.  
  578.         mov   [BgrDrawMode],eax
  579.         mov   [BgrDataWidth],eax
  580.         mov   [BgrDataHeight],eax
  581.         mov    [mem_BACKGROUND], 4
  582.         mov [img_background], static_background_data
  583.  
  584.         mov     [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE
  585.  
  586. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  587.  
  588.         call  rerouteirqs
  589.  
  590. ; Initialize system V86 machine
  591.         call    init_sys_v86
  592.  
  593. ; TIMER SET TO 1/100 S
  594.  
  595.         mov   al,0x34              ; set to 100Hz
  596.         out   0x43,al
  597.         mov   al,0x9b              ; lsb    1193180 / 1193
  598.         out   0x40,al
  599.         mov   al,0x2e              ; msb
  600.         out   0x40,al
  601.  
  602. ; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
  603. ; they are used: when partitions are scanned, hd_read relies on timer
  604. ; Also enable IRQ2, because in some configurations
  605. ; IRQs from slave controller are not delivered until IRQ2 on master is enabled
  606.         mov     al, 0xFA
  607.         out     0x21, al
  608.         mov     al, 0x3F
  609.         out     0xA1, al
  610.  
  611. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  612. include 'detect/disks.inc'
  613. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  614.  
  615.   call Parser_params
  616.  
  617. ; READ RAMDISK IMAGE FROM HD
  618.  
  619. ;!!!!!!!!!!!!!!!!!!!!!!!
  620. include 'boot/rdload.inc'
  621. ;!!!!!!!!!!!!!!!!!!!!!!!
  622. ;    mov    [dma_hdd],1
  623. ; CALCULATE FAT CHAIN FOR RAMDISK
  624.  
  625.         call  calculatefatchain
  626.  
  627. ; LOAD VMODE DRIVER
  628.  
  629. ;!!!!!!!!!!!!!!!!!!!!!!!
  630. include 'vmodeld.inc'
  631. ;!!!!!!!!!!!!!!!!!!!!!!!
  632.  
  633. if 0
  634.   mov ax,[OS_BASE+0x10000+bx_from_load]
  635.   cmp ax,'r1'           ; if using not ram disk, then load librares and parameters {SPraid.simba}
  636.   je  no_lib_load
  637. ; LOADING LIBRARES
  638.    stdcall dll.Load,@IMPORT                 ; loading librares for kernel (.obj files)
  639.    call load_file_parse_table               ; prepare file parse table
  640.    call set_kernel_conf                     ; configure devices and gui
  641. no_lib_load:
  642. end if
  643.  
  644. ; LOAD FONTS I and II
  645.  
  646.         stdcall read_file, char, FONT_I, 0, 2304
  647.         stdcall read_file, char2, FONT_II, 0, 2560
  648.  
  649.         mov   esi,boot_fonts
  650.         call  boot_log
  651.  
  652. ; PRINT AMOUNT OF MEMORY
  653.         mov     esi, boot_memdetect
  654.         call    boot_log
  655.  
  656.         movzx   ecx, word [boot_y]
  657.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  658.         sub     ecx, 10
  659.         mov     edx, 0xFFFFFF
  660.         mov     ebx, [MEM_AMOUNT]
  661.         shr     ebx, 20
  662.         xor     edi,edi
  663.         mov     eax, 0x00040000
  664.                 inc             edi
  665.         call    display_number_force
  666.  
  667. ; BUILD SCHEDULER
  668.  
  669.         call   build_scheduler ; sys32.inc
  670.  
  671.         mov    esi,boot_devices
  672.         call   boot_log
  673.  
  674.         mov  [pci_access_enabled],1
  675.  
  676.  
  677. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  678.  
  679.         mov   esi,boot_windefs
  680.         call  boot_log
  681.         call  set_window_defaults
  682.  
  683. ; SET BACKGROUND DEFAULTS
  684.  
  685.         mov   esi,boot_bgr
  686.         call  boot_log
  687.         call  init_background
  688.         call  calculatebackground
  689.  
  690. ; RESERVE SYSTEM IRQ'S JA PORT'S
  691.  
  692.         mov   esi,boot_resirqports
  693.         call  boot_log
  694.         call  reserve_irqs_ports
  695.  
  696. ; SET PORTS FOR IRQ HANDLERS
  697.  
  698.         mov  esi,boot_setrports
  699.         call boot_log
  700.         ;call setirqreadports
  701.  
  702. ; SET UP OS TASK
  703.  
  704.         mov  esi,boot_setostask
  705.         call boot_log
  706.  
  707.         xor  eax, eax
  708.         mov  dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
  709.         mov  dword [SLOT_BASE+APPDATA.exc_handler], eax
  710.         mov  dword [SLOT_BASE+APPDATA.except_mask], eax
  711.  
  712.         ; name for OS/IDLE process
  713.  
  714.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  715.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  716.         mov edi, [os_stack_seg]
  717.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  718.         add edi, 0x2000-512
  719.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  720.         mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi ; just for case
  721.         ; [SLOT_BASE+256+APPDATA.io_map] was set earlier
  722.  
  723.         mov esi, fpu_data
  724.         mov ecx, 512/4
  725.         cld
  726.         rep movsd
  727.  
  728.         mov dword [SLOT_BASE+256+APPDATA.exc_handler], eax
  729.         mov dword [SLOT_BASE+256+APPDATA.except_mask], eax
  730.  
  731.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  732.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  733.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  734.  
  735.         mov  dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
  736.         mov dword [SLOT_BASE+256+APPDATA.tls_base], eax
  737.  
  738.         ; task list
  739.         mov  dword [TASK_DATA+TASKDATA.mem_start],eax   ; process base address
  740.         inc  eax
  741.         mov  dword [CURRENT_TASK],eax
  742.         mov  dword [TASK_COUNT],eax
  743.         mov  [current_slot], SLOT_BASE+256
  744.         mov  [TASK_BASE],dword TASK_DATA
  745.         mov  byte[TASK_DATA+TASKDATA.wnd_number],al     ; on screen number
  746.         mov  dword [TASK_DATA+TASKDATA.pid], eax        ; process id number
  747.  
  748.         call init_display
  749.         mov eax, [def_cursor]
  750.         mov [SLOT_BASE+APPDATA.cursor],eax
  751.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  752.  
  753.   ; READ TSC / SECOND
  754.  
  755.         mov   esi,boot_tsc
  756.         call  boot_log
  757.         cli
  758.         rdtsc ;call  _rdtsc
  759.         mov   ecx,eax
  760.         mov   esi,250               ; wait 1/4 a second
  761.         call  delay_ms
  762.         rdtsc ;call  _rdtsc
  763.         sti
  764.         sub   eax,ecx
  765.         shl   eax,2
  766.         mov   [CPU_FREQ],eax          ; save tsc / sec
  767. ;       mov ebx, 1000000
  768. ;       div ebx
  769. ; ¢®®¡é¥-â® ¯à®¨§¢®¤¨â¥«ì­®áâì ¢ ¤ ­­®¬ ª®­ªà¥â­®¬ ¬¥áâ¥
  770. ; ᮢ¥à襭­® ­¥ªà¨â¨ç­ , ­® çâ®¡ë § âª­ãâì «î¡¨â¥«¥©
  771. ; ®¯â¨¬¨§¨àãîé¨å ª®¬¯¨«ïâ®à®¢ Ÿ‚“...
  772.         mov     edx, 2251799814
  773.         mul     edx
  774.         shr     edx, 19
  775.         mov [stall_mcs], edx
  776. ; PRINT CPU FREQUENCY
  777.         mov     esi, boot_cpufreq
  778.         call    boot_log
  779.  
  780.         mov     ebx, edx
  781.         movzx   ecx, word [boot_y]
  782.         add     ecx, (10+17*6) shl 16 - 10 ; 'CPU frequency is '
  783.         mov     edx, 0xFFFFFF
  784.         xor     edi,edi
  785.         mov     eax, 0x00040000
  786.                 inc             edi
  787.         call    display_number_force
  788.  
  789. ; SET VARIABLES
  790.  
  791.         call  set_variables
  792.  
  793. ; SET MOUSE
  794.  
  795.         ;call   detect_devices
  796.         stdcall load_driver, szPS2MDriver
  797. ;        stdcall load_driver, szCOM_MDriver
  798.  
  799.         mov   esi,boot_setmouse
  800.         call  boot_log
  801.         call  setmouse
  802.  
  803.  
  804. ; STACK AND FDC
  805.  
  806.         call  stack_init
  807.         call  fdc_init
  808.  
  809. ; PALETTE FOR 320x200 and 640x480 16 col
  810.  
  811.         cmp   [SCR_MODE],word 0x12
  812.         jne   no_pal_vga
  813.         mov   esi,boot_pal_vga
  814.         call  boot_log
  815.         call  paletteVGA
  816.       no_pal_vga:
  817.  
  818.         cmp   [SCR_MODE],word 0x13
  819.         jne   no_pal_ega
  820.         mov   esi,boot_pal_ega
  821.         call  boot_log
  822.         call  palette320x200
  823.       no_pal_ega:
  824.  
  825. ; LOAD DEFAULT SKIN
  826.  
  827.         call    load_default_skin
  828.  
  829. ;protect io permission map
  830.  
  831.            mov esi, [default_io_map]
  832.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map], PG_MAP
  833.            add esi, 0x1000
  834.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  835.  
  836.            stdcall map_page,tss._io_map_0,\
  837.                    [SLOT_BASE+256+APPDATA.io_map], PG_MAP
  838.            stdcall map_page,tss._io_map_1,\
  839.                    [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  840.  
  841. ; LOAD FIRST APPLICATION
  842.         cli
  843.  
  844.         cmp   byte [BOOT_VAR+0x9030],1
  845.         jne   no_load_vrr_m
  846.  
  847.         mov     ebp, vrr_m
  848.         call    fs_execute_from_sysdir
  849.  
  850. ;        cmp   eax,2                  ; if vrr_m app found (PID=2)
  851.         sub   eax,2
  852.         jz    first_app_found
  853.  
  854. no_load_vrr_m:
  855.  
  856.         mov     ebp, firstapp
  857.         call    fs_execute_from_sysdir
  858.  
  859. ;        cmp   eax,2                  ; continue if a process has been loaded
  860.         sub   eax,2
  861.         jz    first_app_found
  862.  
  863.         mov     esi, boot_failed
  864.         call    boot_log
  865.  
  866.         mov   eax, 0xDEADBEEF        ; otherwise halt
  867.         hlt
  868.  
  869. first_app_found:
  870.  
  871.         cli
  872.  
  873.         ;mov   [TASK_COUNT],dword 2
  874.         push  1
  875.         pop   dword [CURRENT_TASK]      ; set OS task fisrt
  876.  
  877. ; SET KEYBOARD PARAMETERS
  878.         mov   al, 0xf6         ; reset keyboard, scan enabled
  879.         call  kb_write
  880.  
  881.         ; wait until 8042 is ready
  882.         xor ecx,ecx
  883.       @@:
  884.         in     al,64h
  885.         and    al,00000010b
  886.         loopnz @b
  887.  
  888.        ; mov   al, 0xED       ; svetodiody - only for testing!
  889.        ; call  kb_write
  890.        ; call  kb_read
  891.        ; mov   al, 111b
  892.        ; call  kb_write
  893.        ; call  kb_read
  894.  
  895.         mov   al, 0xF3       ; set repeat rate & delay
  896.         call  kb_write
  897. ;        call  kb_read
  898.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  899.         call  kb_write
  900. ;        call  kb_read
  901.      ;// mike.dld [
  902.         call  set_lights
  903.      ;// mike.dld ]
  904.  
  905.  
  906. ; Setup serial output console (if enabled)
  907.  
  908. if defined debug_com_base
  909.  
  910.         ; enable Divisor latch
  911.  
  912.         mov     dx, debug_com_base+3
  913.         mov     al, 1 shl 7
  914.         out     dx, al
  915.  
  916.         ; Set speed to 115200 baud (max speed)
  917.  
  918.         mov     dx, debug_com_base
  919.         mov     al, 0x01
  920.         out     dx, al
  921.  
  922.         mov     dx, debug_com_base+1
  923.         mov     al, 0x00
  924.         out     dx, al
  925.  
  926.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  927.  
  928.         mov     dx, debug_com_base+3
  929.         mov     al, 3
  930.         out     dx, al
  931.  
  932.         ; disable interrupts
  933.  
  934.         mov     dx, debug_com_base+1
  935.         mov     al, 0
  936.         out     dx, al
  937.  
  938.         ; clear +  enable fifo (64 bits)
  939.  
  940.         mov     dx, debug_com_base+2
  941.         mov     al, 0x7 + 1 shl 5
  942.         out     dx, al
  943.  
  944.  
  945. end if
  946.  
  947. ; START MULTITASKING
  948.  
  949. if preboot_blogesc
  950.         mov     esi, boot_tasking
  951.         call    boot_log
  952. .bll1:  in      al, 0x60        ; wait for ESC key press
  953.         cmp     al, 129
  954.         jne     .bll1
  955. end if
  956.  
  957. ;       mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  958.  
  959. ; UNMASK ALL IRQ'S
  960.  
  961.         mov   esi,boot_allirqs
  962.         call  boot_log
  963.  
  964.         cli                          ;guarantee forbidance of interrupts.
  965.         mov   al,0                   ; unmask all irq's
  966.         out   0xA1,al
  967.         out   0x21,al
  968.  
  969.         mov   ecx,32
  970.  
  971.      ready_for_irqs:
  972.  
  973.         mov   al,0x20                ; ready for irqs
  974.         out   0x20,al
  975.         out   0xa0,al
  976.  
  977.         loop  ready_for_irqs         ; flush the queue
  978.  
  979.         stdcall attach_int_handler, dword 1, irq1, dword 0
  980.  
  981. ;        mov    [dma_hdd],1
  982.         cmp     [IDEContrRegsBaseAddr], 0
  983.         setnz   [dma_hdd]
  984.         mov [timer_ticks_enable],1              ; for cd driver
  985.  
  986.         sti
  987.         call change_task
  988.  
  989.         jmp osloop
  990.  
  991. ;        jmp   $                      ; wait here for timer to take control
  992.  
  993.         ; Fly :)
  994.  
  995. include 'unpacker.inc'
  996. include 'fdo.inc'
  997.  
  998. align 4
  999. boot_log:
  1000.          pushad
  1001.  
  1002.         mov   ebx,10*65536
  1003.         mov   bx,word [boot_y]
  1004.         add   [boot_y],dword 10
  1005.         mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  1006.                 xor       edi,edi
  1007.         mov   edx,esi
  1008.                 inc       edi
  1009.         call  dtext
  1010.  
  1011.         mov   [novesachecksum],1000
  1012.         call  checkVga_N13
  1013.  
  1014.         popad
  1015.  
  1016.         ret
  1017.  
  1018. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1019. ;                                                                    ;
  1020. ;                    MAIN OS LOOP START                              ;
  1021. ;                                                                    ;
  1022. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1023. align 32
  1024. osloop:
  1025.         call   [draw_pointer]
  1026.         call   check_buttons
  1027.         call   checkwindows
  1028. ;       call   check_window_move_request
  1029.         call   checkmisc
  1030.         call   checkVga_N13
  1031.         call   stack_handler
  1032.         call   checkidle
  1033.         call   check_fdd_motor_status
  1034.         call   check_ATAPI_device_event
  1035.         jmp    osloop
  1036. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1037. ;                                                                    ;
  1038. ;                      MAIN OS LOOP END                              ;
  1039. ;                                                                    ;
  1040. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1041. align 4
  1042. checkidle:
  1043.         pushad
  1044.         call    change_task
  1045.         jmp     idle_loop_entry
  1046.   idle_loop:
  1047.         cmp     eax,[idlemem]     ; eax == [timer_ticks]
  1048.         jne     idle_exit
  1049.         rdtsc   ;call _rdtsc
  1050.         mov     ecx,eax
  1051.         hlt
  1052.         rdtsc   ;call _rdtsc
  1053.         sub     eax,ecx
  1054.         add     [idleuse],eax
  1055.   idle_loop_entry:
  1056.         mov     eax,[timer_ticks] ; eax =  [timer_ticks]
  1057.         cmp     [check_idle_semaphore],0
  1058.         je      idle_loop
  1059.         dec     [check_idle_semaphore]
  1060.   idle_exit:
  1061.         mov     [idlemem],eax     ; eax == [timer_ticks]
  1062.         popad
  1063.         ret
  1064.  
  1065. uglobal
  1066.   idlemem               dd   0x0
  1067.   idleuse               dd   0x0
  1068.   idleusesec            dd   0x0
  1069.   check_idle_semaphore  dd   0x0
  1070. endg
  1071.  
  1072.  
  1073.  
  1074. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1075. ;                                                                      ;
  1076. ;                   INCLUDED SYSTEM FILES                              ;
  1077. ;                                                                      ;
  1078. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1079.  
  1080.  
  1081. include "kernel32.inc"
  1082.  
  1083.  
  1084. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1085. ;                                                                      ;
  1086. ;                       KERNEL FUNCTIONS                               ;
  1087. ;                                                                      ;
  1088. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1089.  
  1090. reserve_irqs_ports:
  1091.  
  1092.         push eax
  1093.         xor     eax,eax
  1094.                 inc     eax
  1095.         mov  byte [irq_owner+4*0],al            ;1    ; timer
  1096.         ;mov  [irq_owner+4*1], 1    ; keyboard
  1097.         mov  byte [irq_owner+4*6],al            ;1    ; floppy diskette
  1098.         mov  byte [irq_owner+4*13],al   ;1   ; math co-pros
  1099.         mov  byte [irq_owner+4*14],al   ;1   ; ide I
  1100.         mov  byte [irq_owner+4*15],al   ;1   ; ide II
  1101.         pop  eax
  1102.  
  1103. ; RESERVE PORTS
  1104.         push  4
  1105.         pop   dword [RESERVED_PORTS]    ;,edi
  1106.  
  1107.         push  1
  1108.         pop   dword [RESERVED_PORTS+16+0]       ;,dword 1
  1109.         and   dword [RESERVED_PORTS+16+4],0     ;,dword 0x0
  1110.         mov   dword [RESERVED_PORTS+16+8],0x2d  ;,dword 0x2d
  1111.  
  1112.         push  1
  1113.         pop   dword [RESERVED_PORTS+32+0]       ;,dword 1
  1114.         push  0x30
  1115.         pop   dword [RESERVED_PORTS+32+4]       ;,dword 0x30
  1116.         push  0x4d
  1117.         pop   dword [RESERVED_PORTS+32+8]       ;,dword 0x4d
  1118.  
  1119.         push  1
  1120.         pop   dword [RESERVED_PORTS+48+0]       ;,dword 1
  1121.         push  0x50
  1122.         pop   dword [RESERVED_PORTS+48+4]       ;,dword 0x50
  1123.         mov   dword [RESERVED_PORTS+48+8],0xdf  ;,dword 0xdf
  1124.  
  1125.         push  1
  1126.         pop   dword [RESERVED_PORTS+64+0]       ;,dword 1
  1127.  
  1128.         mov   dword [RESERVED_PORTS+64+4],0xe5  ;,dword 0xe5
  1129.         mov   dword [RESERVED_PORTS+64+8],0xff  ;,dword 0xff
  1130.  
  1131.         ret
  1132.  
  1133. setirqreadports:
  1134.  
  1135.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1136.             and   dword [irq12read+4],0                   ; end of port list
  1137. ;        mov   [irq12read+4],dword 0                  ; end of port list
  1138.         ;mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1139.         ;mov   [irq04read+4],dword 0                  ; end of port list
  1140.         ;mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1141.         ;mov   [irq03read+4],dword 0                  ; end of port list
  1142.  
  1143.         ret
  1144.  
  1145. iglobal
  1146.   process_number dd 0x1
  1147. endg
  1148.  
  1149. set_variables:
  1150.  
  1151.         mov   ecx,0x100                       ; flush port 0x60
  1152. .fl60:  in    al,0x60
  1153.         loop  .fl60
  1154.         push  eax
  1155.  
  1156.         mov   ax,[BOOT_VAR+0x900c]
  1157.         shr   ax,1
  1158.         shl   eax,16
  1159.         mov   ax,[BOOT_VAR+0x900A]
  1160.         shr   ax,1
  1161.         mov   [MOUSE_X],eax
  1162.  
  1163.         xor   eax,eax
  1164.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  1165.  
  1166.         mov   byte [MOUSE_BUFF_COUNT],al                 ; mouse buffer
  1167.         mov   byte [KEY_COUNT],al                 ; keyboard buffer
  1168.         mov   byte [BTN_COUNT],al                 ; button buffer
  1169. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1170.  
  1171.      ;!! IP 04.02.2005:
  1172.         mov   byte [DONT_SWITCH],al ; change task if possible
  1173.         pop   eax
  1174.         ret
  1175.  
  1176. align 4
  1177. ;input  eax=43,bl-byte of output, ecx - number of port
  1178. sys_outport:
  1179.  
  1180.     mov   edi,ecx          ; separate flag for read / write
  1181.     and   ecx,65535
  1182.  
  1183.     mov   eax,[RESERVED_PORTS]
  1184.     test  eax,eax
  1185.     jnz   .sopl8
  1186.     inc   eax
  1187.         mov   [esp+32],eax
  1188.     ret
  1189.  
  1190.   .sopl8:
  1191.     mov   edx,[TASK_BASE]
  1192.     mov   edx,[edx+0x4]
  1193.     ;and   ecx,65535
  1194.     ;cld - set on interrupt 0x40
  1195.   .sopl1:
  1196.  
  1197.     mov   esi,eax
  1198.     shl   esi,4
  1199.     add   esi,RESERVED_PORTS
  1200.     cmp   edx,[esi+0]
  1201.     jne   .sopl2
  1202.     cmp   ecx,[esi+4]
  1203.     jb    .sopl2
  1204.     cmp   ecx,[esi+8]
  1205.     jg    .sopl2
  1206. .sopl3:
  1207.  
  1208.     test  edi,0x80000000 ; read ?
  1209.     jnz   .sopl4
  1210.  
  1211.         mov       eax,ebx
  1212.     mov   dx,cx          ; write
  1213.     out   dx,al
  1214.     and   [esp+32],dword 0
  1215.     ret
  1216.  
  1217.         .sopl2:
  1218.  
  1219.     dec   eax
  1220.     jnz   .sopl1
  1221.         inc       eax
  1222.     mov   [esp+32],eax
  1223.     ret
  1224.  
  1225.  
  1226.   .sopl4:
  1227.  
  1228.     mov   dx,cx          ; read
  1229.     in    al,dx
  1230.     and   eax,0xff
  1231.     and   [esp+32],dword 0
  1232.     mov   [esp+20],eax
  1233.     ret
  1234.  
  1235. display_number:
  1236. ;It is not optimization
  1237.         mov     eax, ebx
  1238.         mov     ebx, ecx
  1239.         mov     ecx, edx
  1240.         mov     edx, esi
  1241.         mov     esi, edi
  1242. ; eax = print type, al=0 -> ebx is number
  1243. ;                   al=1 -> ebx is pointer
  1244. ;                   ah=0 -> display decimal
  1245. ;                   ah=1 -> display hexadecimal
  1246. ;                   ah=2 -> display binary
  1247. ;                   eax bits 16-21 = number of digits to display (0-32)
  1248. ;                   eax bits 22-31 = reserved
  1249. ;
  1250. ; ebx = number or pointer
  1251. ; ecx = x shl 16 + y
  1252. ; edx = color
  1253.     xor     edi, edi
  1254. display_number_force:
  1255.     push  eax
  1256.     and   eax,0x3fffffff
  1257.     cmp   eax,0xffff            ; length > 0 ?
  1258.     pop   eax
  1259.     jge   cont_displ
  1260.     ret
  1261.    cont_displ:
  1262.      push  eax
  1263.      and   eax,0x3fffffff
  1264.      cmp   eax,61*0x10000        ; length <= 60 ?
  1265.      pop   eax
  1266.      jb    cont_displ2
  1267.      ret
  1268.    cont_displ2:
  1269.  
  1270.      pushad
  1271.  
  1272.      cmp   al,1                  ; ecx is a pointer ?
  1273.      jne   displnl1
  1274.      mov   ebp,ebx
  1275.      add   ebp,4
  1276.      mov   ebp,[ebp+std_application_base_address]
  1277.      mov   ebx,[ebx+std_application_base_address]
  1278.  displnl1:
  1279.      sub   esp,64
  1280.  
  1281.     test   ah,ah                  ; DECIMAL
  1282.     jnz   no_display_desnum
  1283.     shr   eax,16
  1284.     and   eax,0xC03f
  1285. ;     and   eax,0x3f
  1286.     push  eax
  1287.     and   eax,0x3f
  1288.     mov   edi,esp
  1289.     add   edi,4+64-1
  1290.     mov   ecx,eax
  1291.     mov   eax,ebx
  1292.     mov   ebx,10
  1293.  d_desnum:
  1294.      xor   edx,edx
  1295.      call  division_64_bits
  1296.      div   ebx
  1297.      add   dl,48
  1298.      mov   [edi],dl
  1299.      dec   edi
  1300.      loop  d_desnum
  1301.      pop   eax
  1302.      call  normalize_number
  1303.      call  draw_num_text
  1304.      add   esp,64
  1305.      popad
  1306.      ret
  1307.    no_display_desnum:
  1308.  
  1309.      cmp   ah,0x01               ; HEXADECIMAL
  1310.      jne   no_display_hexnum
  1311.      shr   eax,16
  1312.      and   eax,0xC03f
  1313. ;     and   eax,0x3f
  1314.      push  eax
  1315.      and   eax,0x3f
  1316.      mov   edi,esp
  1317.      add   edi,4+64-1
  1318.      mov   ecx,eax
  1319.      mov   eax,ebx
  1320.      mov   ebx,16
  1321.    d_hexnum:
  1322.      xor   edx,edx
  1323.      call  division_64_bits
  1324.      div   ebx
  1325.    hexletters = __fdo_hexdigits
  1326.      add   edx,hexletters
  1327.      mov   dl,[edx]
  1328.      mov   [edi],dl
  1329.      dec   edi
  1330.      loop  d_hexnum
  1331.      pop   eax
  1332.      call  normalize_number
  1333.      call  draw_num_text
  1334.      add   esp,64
  1335.      popad
  1336.      ret
  1337.    no_display_hexnum:
  1338.  
  1339.      cmp   ah,0x02               ; BINARY
  1340.      jne   no_display_binnum
  1341.      shr   eax,16
  1342.      and   eax,0xC03f
  1343. ;     and   eax,0x3f
  1344.      push  eax
  1345.      and   eax,0x3f
  1346.      mov   edi,esp
  1347.      add   edi,4+64-1
  1348.      mov   ecx,eax
  1349.      mov   eax,ebx
  1350.      mov   ebx,2
  1351.    d_binnum:
  1352.      xor   edx,edx
  1353.      call  division_64_bits
  1354.      div   ebx
  1355.      add   dl,48
  1356.      mov   [edi],dl
  1357.      dec   edi
  1358.      loop  d_binnum
  1359.      pop   eax
  1360.      call  normalize_number
  1361.      call  draw_num_text
  1362.      add   esp,64
  1363.      popad
  1364.      ret
  1365.    no_display_binnum:
  1366.  
  1367.      add   esp,64
  1368.      popad
  1369.      ret
  1370.  
  1371. normalize_number:
  1372.      test  ah,0x80
  1373.      jz   .continue
  1374.      mov  ecx,48
  1375.      and   eax,0x3f
  1376. @@:
  1377.      inc   edi
  1378.      cmp   [edi],cl
  1379.      jne   .continue
  1380.      dec   eax
  1381.      cmp   eax,1
  1382.      ja    @r
  1383.      mov   al,1
  1384. .continue:
  1385.      and   eax,0x3f
  1386.      ret
  1387.  
  1388. division_64_bits:
  1389.      test  [esp+1+4],byte 0x40
  1390.      jz   .continue
  1391.      push  eax
  1392.      mov   eax,ebp
  1393.      div   ebx
  1394.      mov   ebp,eax
  1395.      pop   eax
  1396. .continue:
  1397.      ret
  1398.  
  1399. draw_num_text:
  1400.      mov   esi,eax
  1401.      mov   edx,64+4
  1402.      sub   edx,eax
  1403.      add   edx,esp
  1404.      mov   ebx,[esp+64+32-8+4]
  1405. ; add window start x & y
  1406.      mov   ecx,[TASK_BASE]
  1407.  
  1408.      mov   edi,[CURRENT_TASK]
  1409.      shl   edi,8
  1410.  
  1411.      mov   eax,[ecx-twdw+WDATA.box.left]
  1412.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1413.      shl   eax,16
  1414.      add   eax,[ecx-twdw+WDATA.box.top]
  1415.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1416.      add   ebx,eax
  1417.      mov   ecx,[esp+64+32-12+4]
  1418.         and     ecx, not 0x80000000     ; force counted string
  1419.         mov     eax, [esp+64+8]         ; background color (if given)
  1420.         mov     edi, [esp+64+4]
  1421.      jmp   dtext
  1422.  
  1423. align 4
  1424.  
  1425. sys_setup:
  1426.  
  1427. ; 1=roland mpu midi base , base io address
  1428. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1429. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1430. ; 5=system language, 1eng 2fi 3ger 4rus
  1431. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1432. ; 8=fat32 partition in hd
  1433. ; 9
  1434. ; 10 = sound dma channel
  1435. ; 11 = enable lba read
  1436. ; 12 = enable pci access
  1437.  
  1438.  
  1439.         and  [esp+32],dword 0
  1440.         dec     ebx                             ; MIDI
  1441.         jnz  nsyse1
  1442.         cmp  ecx,0x100
  1443.  
  1444.         jb   nsyse1
  1445.         mov     esi,65535
  1446.         cmp     esi,ecx
  1447.  
  1448.         jb   nsyse1
  1449.         mov  [midi_base],cx     ;bx
  1450.         mov  word [mididp],cx   ;bx
  1451.         inc  cx                 ;bx
  1452.         mov  word [midisp],cx   ;bx
  1453.         ret
  1454.  
  1455. iglobal
  1456. midi_base dw 0
  1457. endg
  1458.  
  1459.    nsyse1:
  1460.         dec     ebx                              ; KEYBOARD
  1461.         jnz  nsyse2
  1462.         mov  edi,[TASK_BASE]
  1463.         mov  eax,[edi+TASKDATA.mem_start]
  1464.         add  eax,edx
  1465.  
  1466.         dec     ecx
  1467.         jnz  kbnobase
  1468.         mov  ebx,keymap
  1469.         mov  ecx,128
  1470.         call memmove
  1471.         ret
  1472.    kbnobase:
  1473.         dec  ecx
  1474.         jnz  kbnoshift
  1475.  
  1476.         mov  ebx,keymap_shift
  1477.         mov  ecx,128
  1478.         call memmove
  1479.         ret
  1480.    kbnoshift:
  1481.         dec  ecx
  1482.         jnz  kbnoalt
  1483.         mov  ebx,keymap_alt
  1484.         mov  ecx,128
  1485.         call memmove
  1486.         ret
  1487.    kbnoalt:
  1488.         sub  ecx,6
  1489.         jnz  kbnocountry
  1490.         mov  word [keyboard],dx
  1491.         ret
  1492.    kbnocountry:
  1493.         mov  [esp+32],dword 1
  1494.         ret
  1495.    nsyse2:
  1496.         dec  ebx                            ; CD
  1497.         jnz  nsyse4
  1498.  
  1499.         test ecx,ecx
  1500.         jz   nosesl
  1501.  
  1502.         cmp  ecx, 4
  1503.         ja   nosesl
  1504.         mov  [cd_base],cl
  1505.  
  1506.         dec     ecx
  1507.         jnz  noprma
  1508.         mov  [cdbase],0x1f0
  1509.         mov  [cdid],0xa0
  1510.    noprma:
  1511.  
  1512.         dec     ecx
  1513.         jnz  noprsl
  1514.         mov  [cdbase],0x1f0
  1515.         mov  [cdid],0xb0
  1516.    noprsl:
  1517.         dec     ecx
  1518.         jnz  nosema
  1519.         mov  [cdbase],0x170
  1520.         mov  [cdid],0xa0
  1521.    nosema:
  1522.         dec     ecx
  1523.         jnz  nosesl
  1524.         mov  [cdbase],0x170
  1525.         mov  [cdid],0xb0
  1526.    nosesl:
  1527.         ret
  1528.  
  1529. iglobal
  1530. cd_base db 0
  1531.  
  1532. endg
  1533.    nsyse4:
  1534.                  
  1535.         sub  ebx,2               ; SYSTEM LANGUAGE
  1536.         jnz  nsyse5
  1537.         mov  [syslang],ecx
  1538.         ret
  1539.    nsyse5:
  1540.          
  1541.         sub  ebx,2              ; HD BASE
  1542.         jnz  nsyse7
  1543.  
  1544.         test ecx,ecx
  1545.         jz   nosethd
  1546.  
  1547.         cmp  ecx,4
  1548.         ja   nosethd
  1549.         mov  [hd_base],cl
  1550.  
  1551.         cmp  ecx,1
  1552.         jnz  noprmahd
  1553.         mov  [hdbase],0x1f0
  1554.         and  dword [hdid],0x0
  1555.         mov  dword [hdpos],ecx
  1556. ;     call set_FAT32_variables
  1557.    noprmahd:
  1558.  
  1559.         cmp  ecx,2
  1560.         jnz  noprslhd
  1561.         mov  [hdbase],0x1f0
  1562.         mov  [hdid],0x10
  1563.         mov  dword [hdpos],ecx
  1564. ;     call set_FAT32_variables
  1565.    noprslhd:
  1566.  
  1567.         cmp  ecx,3
  1568.         jnz  nosemahd
  1569.         mov  [hdbase],0x170
  1570.         and  dword [hdid],0x0
  1571.         mov  dword [hdpos],ecx
  1572. ;     call set_FAT32_variables
  1573.    nosemahd:
  1574.  
  1575.         cmp  ecx,4
  1576.         jnz  noseslhd
  1577.         mov  [hdbase],0x170
  1578.         mov  [hdid],0x10
  1579.         mov  dword [hdpos],ecx
  1580. ;     call set_FAT32_variables
  1581.    noseslhd:
  1582.         call  reserve_hd1
  1583.         call  reserve_hd_channel
  1584.         call  free_hd_channel
  1585.         and   dword [hd1_status],0        ; free
  1586.    nosethd:
  1587.         ret
  1588.  
  1589. iglobal
  1590. hd_base db 0
  1591. endg
  1592.  
  1593. nsyse7:
  1594.  
  1595. ;     cmp  eax,8                      ; HD PARTITION
  1596.         dec  ebx
  1597.         jnz  nsyse8
  1598.         mov  [fat32part],ecx
  1599. ;     call set_FAT32_variables
  1600.         call  reserve_hd1
  1601.         call  reserve_hd_channel
  1602.         call  free_hd_channel
  1603. ;       pusha
  1604.         call  choice_necessity_partition_1
  1605. ;       popa
  1606.         and dword [hd1_status],0        ; free
  1607.         ret
  1608.  
  1609. nsyse8:
  1610. ;     cmp  eax,11                     ; ENABLE LBA READ
  1611.         and  ecx,1
  1612.         sub  ebx,3
  1613.         jnz  no_set_lba_read
  1614.         mov  [lba_read_enabled],ecx
  1615.         ret
  1616.  
  1617. no_set_lba_read:
  1618. ;     cmp  eax,12                     ; ENABLE PCI ACCESS
  1619.         dec  ebx
  1620.         jnz  no_set_pci_access
  1621.         mov  [pci_access_enabled],ecx
  1622.         ret
  1623. no_set_pci_access:
  1624.  
  1625. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1626. include 'vmodeint.inc'
  1627. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1628.  
  1629. sys_setup_err:
  1630.         or  [esp+32],dword -1
  1631.         ret
  1632.  
  1633. align 4
  1634.  
  1635. sys_getsetup:
  1636.  
  1637. ; 1=roland mpu midi base , base io address
  1638. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1639. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1640. ; 5=system language, 1eng 2fi 3ger 4rus
  1641. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1642. ; 8=fat32 partition in hd
  1643. ; 9=get hs timer tic
  1644.  
  1645. ;     cmp  eax,1
  1646.         dec     ebx
  1647.         jnz  ngsyse1
  1648.         movzx eax,[midi_base]
  1649.         mov  [esp+32],eax
  1650.         ret
  1651. ngsyse1:
  1652. ;     cmp  eax,2
  1653.         dec     ebx
  1654.         jnz  ngsyse2
  1655.  
  1656.         mov  edi,[TASK_BASE]
  1657.         mov  ebx,[edi+TASKDATA.mem_start]
  1658.         add  ebx,edx
  1659.  
  1660. ;     cmp  ebx,1
  1661.         dec     ecx
  1662.         jnz  kbnobaseret
  1663.         mov  eax,keymap
  1664.         mov  ecx,128
  1665.         call memmove
  1666.         ret
  1667. kbnobaseret:
  1668. ;     cmp  ebx,2
  1669.         dec     ecx
  1670.         jnz  kbnoshiftret
  1671.  
  1672.         mov  eax,keymap_shift
  1673.         mov  ecx,128
  1674.         call memmove
  1675.         ret
  1676. kbnoshiftret:
  1677. ;     cmp  ebx,3
  1678.         dec     ecx
  1679.         jne  kbnoaltret
  1680.  
  1681.         mov  eax,keymap_alt
  1682.         mov  ecx,128
  1683.         call memmove
  1684.         ret
  1685. kbnoaltret:
  1686. ;     cmp  ebx,9
  1687.         sub     ecx,6
  1688.         jnz  ngsyse2
  1689.         movzx eax,word [keyboard]
  1690.         mov  [esp+32],eax
  1691.         ret
  1692.  
  1693.  
  1694. ngsyse2:
  1695. ;         cmp  eax,3
  1696.         dec     ebx
  1697.         jnz  ngsyse3
  1698.         movzx eax,[cd_base]
  1699.         mov  [esp+32],eax
  1700.         ret
  1701. ngsyse3:
  1702. ;         cmp  eax,5
  1703.         sub     ebx,2
  1704.         jnz  ngsyse5
  1705.         mov  eax,[syslang]
  1706.         mov  [esp+32],eax
  1707.         ret
  1708. ngsyse5:
  1709. ;     cmp  eax,7
  1710.         sub     ebx,2
  1711.         jnz  ngsyse7
  1712.         movzx eax,[hd_base]
  1713.         mov  [esp+32],eax
  1714.         ret
  1715. ngsyse7:
  1716. ;     cmp  eax,8
  1717.         dec     ebx
  1718.         jnz  ngsyse8
  1719.         mov eax,[fat32part]
  1720.         mov  [esp+32],eax
  1721.         ret
  1722. ngsyse8:
  1723. ;     cmp  eax,9
  1724.         dec     ebx
  1725.         jnz  ngsyse9
  1726.         mov  eax,[timer_ticks] ;[0xfdf0]
  1727.         mov  [esp+32],eax
  1728.         ret
  1729. ngsyse9:
  1730. ;     cmp  eax,11
  1731.         sub     ebx,2
  1732.         jnz  ngsyse11
  1733.         mov eax,[lba_read_enabled]
  1734.         mov  [esp+32],eax
  1735.         ret
  1736. ngsyse11:
  1737. ;     cmp  eax,12
  1738.         dec     ebx
  1739.         jnz  ngsyse12
  1740.         mov eax,[pci_access_enabled]
  1741.         mov  [esp+32],eax
  1742.         ret
  1743. ngsyse12:
  1744.         mov  [esp+32],dword 1
  1745.         ret
  1746.  
  1747.        
  1748. get_timer_ticks:
  1749.         mov eax,[timer_ticks]
  1750.         ret
  1751.  
  1752. iglobal
  1753. align 4
  1754. mousefn dd msscreen, mswin, msbutton, msset
  1755.         dd app_load_cursor
  1756.         dd app_set_cursor
  1757.         dd app_delete_cursor
  1758.         dd msz
  1759. endg
  1760.  
  1761. readmousepos:
  1762.  
  1763. ; eax=0 screen relative
  1764. ; eax=1 window relative
  1765. ; eax=2 buttons pressed
  1766. ; eax=3 set mouse pos   ; reserved
  1767. ; eax=4 load cursor
  1768. ; eax=5 set cursor
  1769. ; eax=6 delete cursor   ; reserved
  1770. ; eax=7 get mouse_z
  1771.  
  1772.            cmp ebx, 7
  1773.            ja msset
  1774.            jmp [mousefn+ebx*4]
  1775. msscreen:
  1776.            mov  eax,[MOUSE_X]
  1777.            shl  eax,16
  1778.            mov  ax,[MOUSE_Y]
  1779.            mov  [esp+36-4],eax
  1780.            ret
  1781. mswin:
  1782.            mov  eax,[MOUSE_X]
  1783.            shl  eax,16
  1784.            mov  ax,[MOUSE_Y]
  1785.            mov  esi,[TASK_BASE]
  1786.            mov  bx, word [esi-twdw+WDATA.box.left]
  1787.            shl  ebx,16
  1788.            mov  bx, word [esi-twdw+WDATA.box.top]
  1789.            sub  eax,ebx
  1790.  
  1791.            mov  edi,[CURRENT_TASK]
  1792.            shl  edi,8
  1793.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1794.            rol  eax,16
  1795.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1796.            rol  eax,16
  1797.            mov  [esp+36-4],eax
  1798.            ret
  1799. msbutton:
  1800.            movzx eax,byte [BTN_DOWN]
  1801.            mov  [esp+36-4],eax
  1802.            ret
  1803. msz:
  1804.            mov   edi, [TASK_COUNT]
  1805.            movzx edi, word [WIN_POS + edi*2]
  1806.            cmp   edi, [CURRENT_TASK]
  1807.            jne   @f
  1808.            mov   ax,[MOUSE_SCROLL_H]
  1809.            shl   eax,16
  1810.            mov   ax,[MOUSE_SCROLL_V]
  1811.            mov   [esp+36-4],eax
  1812.            and   [MOUSE_SCROLL_H],word 0
  1813.            and   [MOUSE_SCROLL_V],word 0
  1814.            ret
  1815.        @@:
  1816.            and  [esp+36-4],dword 0
  1817. ;           ret
  1818. msset:
  1819.            ret
  1820.  
  1821. app_load_cursor:
  1822.            cmp ecx, OS_BASE
  1823.            jae msset
  1824.            stdcall load_cursor, ecx, edx
  1825.            mov [esp+36-4], eax
  1826.            ret
  1827.  
  1828. app_set_cursor:
  1829.            stdcall set_cursor, ecx
  1830.            mov [esp+36-4], eax
  1831.            ret
  1832.  
  1833. app_delete_cursor:
  1834.            stdcall delete_cursor, ecx
  1835.            mov [esp+36-4], eax
  1836.            ret
  1837.  
  1838. is_input:
  1839.  
  1840.    push edx
  1841.    mov  dx,word [midisp]
  1842.    in   al,dx
  1843.    and  al,0x80
  1844.    pop  edx
  1845.    ret
  1846.  
  1847. is_output:
  1848.  
  1849.    push edx
  1850.    mov  dx,word [midisp]
  1851.    in   al,dx
  1852.    and  al,0x40
  1853.    pop  edx
  1854.    ret
  1855.  
  1856.  
  1857. get_mpu_in:
  1858.  
  1859.    push edx
  1860.    mov  dx,word [mididp]
  1861.    in   al,dx
  1862.    pop  edx
  1863.    ret
  1864.  
  1865.  
  1866. put_mpu_out:
  1867.  
  1868.    push edx
  1869.    mov  dx,word [mididp]
  1870.    out  dx,al
  1871.    pop  edx
  1872.    ret
  1873.  
  1874.  
  1875.  
  1876. align 4
  1877.  
  1878. sys_midi:
  1879.         cmp  [mididp],0
  1880.         jnz  sm0
  1881.         mov  [esp+36],dword 1
  1882.         ret
  1883. sm0:
  1884.         and  [esp+36],dword 0
  1885.         dec  ebx
  1886.         jnz  smn1
  1887.  ;    call setuart
  1888. su1:
  1889.         call is_output
  1890.         test al,al
  1891.         jnz  su1
  1892.         mov  dx,word [midisp]
  1893.         mov  al,0xff
  1894.         out  dx,al
  1895. su2:
  1896.         mov  dx,word [midisp]
  1897.         mov  al,0xff
  1898.         out  dx,al
  1899.         call is_input
  1900.         test al,al
  1901.         jnz  su2
  1902.         call get_mpu_in
  1903.         cmp  al,0xfe
  1904.         jnz  su2
  1905. su3:
  1906.         call is_output
  1907.         test  al,al
  1908.         jnz  su3
  1909.         mov  dx,word [midisp]
  1910.         mov  al,0x3f
  1911.         out  dx,al
  1912.         ret
  1913. smn1:
  1914.         dec  ebx
  1915.         jnz  smn2
  1916. sm10:
  1917.         call get_mpu_in
  1918.         call is_output
  1919.         test al,al
  1920.         jnz  sm10
  1921.         mov  al,bl
  1922.         call put_mpu_out
  1923.         smn2:
  1924.         ret
  1925.  
  1926. detect_devices:
  1927. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1928. ;include 'detect/commouse.inc'
  1929. ;include 'detect/ps2mouse.inc'
  1930. ;include 'detect/dev_fd.inc'
  1931. ;include 'detect/dev_hdcd.inc'
  1932. ;include 'detect/sear_par.inc'
  1933. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1934.     ret
  1935.  
  1936. sys_end:
  1937.  
  1938.      mov ecx, [current_slot]
  1939.      mov eax, [ecx+APPDATA.tls_base]
  1940.      test eax, eax
  1941.      jz @F
  1942.  
  1943.      stdcall user_free, eax
  1944. @@:
  1945.  
  1946.      mov   eax,[TASK_BASE]
  1947.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1948.  
  1949.     waitterm:            ; wait here for termination
  1950.      mov   ebx,100
  1951.      call  delay_hs
  1952.      jmp   waitterm
  1953.  
  1954. iglobal
  1955. align 4
  1956. sys_system_table:
  1957.         dd      exit_for_anyone         ; 1 = obsolete
  1958.         dd      sysfn_terminate         ; 2 = terminate thread
  1959.         dd      sysfn_activate          ; 3 = activate window
  1960.         dd      sysfn_getidletime       ; 4 = get idle time
  1961.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1962.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1963.         dd      sysfn_getactive         ; 7 = get active window
  1964.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1965.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1966.         dd      sysfn_minimize          ; 10 = minimize window
  1967.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1968.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1969.         dd      sysfn_getversion        ; 13 = get kernel version
  1970.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1971.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1972.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1973.         dd      sysfn_getallmem         ; 17 = get total memory size
  1974.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1975.                                         ;                 instead of slot
  1976.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1977.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1978.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1979.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1980. sysfn_num = ($ - sys_system_table)/4
  1981. endg
  1982.  
  1983. sys_system:
  1984.         dec     ebx
  1985.         cmp     ebx, sysfn_num
  1986.         jae     @f
  1987.         jmp     dword [sys_system_table + ebx*4]
  1988. @@:
  1989.         ret
  1990.  
  1991.  
  1992. sysfn_shutdown:          ; 18.9 = system shutdown
  1993.      cmp  ecx,1
  1994.      jl   exit_for_anyone
  1995.      cmp  ecx,4
  1996.      jg   exit_for_anyone
  1997.      mov  [BOOT_VAR+0x9030],cl
  1998.  
  1999.      mov  eax,[TASK_COUNT]
  2000.      mov  [SYS_SHUTDOWN],al
  2001.      mov  [shutdown_processes],eax
  2002.      and  dword [esp+32], 0
  2003.  exit_for_anyone:
  2004.      ret
  2005.   uglobal
  2006.    shutdown_processes: dd 0x0
  2007.   endg
  2008.  
  2009. sysfn_terminate:        ; 18.2 = TERMINATE
  2010.      cmp  ecx,2
  2011.      jb   noprocessterminate
  2012.      mov  edx,[TASK_COUNT]
  2013.      cmp  ecx,edx
  2014.      ja   noprocessterminate
  2015.      mov  eax,[TASK_COUNT]
  2016.      shl  ecx,5
  2017.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  2018.      add  ecx,CURRENT_TASK+TASKDATA.state
  2019.      cmp  byte [ecx], 9
  2020.      jz   noprocessterminate
  2021.  
  2022.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2023.      mov  [ecx],byte 3       ; clear possible i40's
  2024.      ;call MEM_Heap_UnLock
  2025.  
  2026.      cmp  edx,[application_table_status]    ; clear app table stat
  2027.      jne  noatsc
  2028.      and  [application_table_status],0
  2029.    noatsc:
  2030.    noprocessterminate:
  2031.      ret
  2032.  
  2033. sysfn_terminate2:
  2034. ;lock application_table_status mutex
  2035. .table_status:
  2036.     cli
  2037.     cmp    [application_table_status],0
  2038.     je     .stf
  2039.     sti
  2040.     call   change_task
  2041.     jmp    .table_status
  2042. .stf:
  2043.     call   set_application_table_status
  2044.     mov    eax,ecx
  2045.     call   pid_to_slot
  2046.     test   eax,eax
  2047.     jz     .not_found
  2048.     mov    ecx,eax
  2049.     cli
  2050.     call   sysfn_terminate
  2051.     and    [application_table_status],0
  2052.     sti
  2053.     and    dword [esp+32],0
  2054.     ret
  2055. .not_found:
  2056.     mov    [application_table_status],0
  2057.     or     dword [esp+32],-1
  2058.     ret
  2059.  
  2060. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2061.      cmp  ecx,2
  2062.      jb   .nowindowactivate
  2063.      cmp  ecx,[TASK_COUNT]
  2064.      ja   .nowindowactivate
  2065.  
  2066.      mov   [window_minimize], 2   ; restore window if minimized
  2067.  
  2068.      movzx esi, word [WIN_STACK + ecx*2]
  2069.      cmp   esi, [TASK_COUNT]
  2070.      je    .nowindowactivate ; already active
  2071.  
  2072.      mov   edi, ecx
  2073.      shl   edi, 5
  2074.      add   edi, window_data
  2075.      movzx esi, word [WIN_STACK + ecx * 2]
  2076.      lea   esi, [WIN_POS + esi * 2]
  2077.      call  waredraw
  2078. .nowindowactivate:
  2079.      ret
  2080.  
  2081. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2082.      mov  eax,[idleusesec]
  2083.      mov  [esp+32], eax
  2084.      ret
  2085.  
  2086. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2087.      mov  eax,[CPU_FREQ]
  2088.      mov  [esp+32], eax
  2089.      ret
  2090.  
  2091. ;  SAVE ramdisk to /hd/1/menuet.img
  2092. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2093.    include 'blkdev/rdsave.inc'
  2094. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2095. align 4
  2096. sysfn_getactive:        ; 18.7 = get active window
  2097.      mov  eax, [TASK_COUNT]
  2098.    movzx  eax, word [WIN_POS + eax*2]
  2099.      mov  [esp+32],eax
  2100.      ret
  2101.  
  2102. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2103. ;     cmp  ecx,1
  2104.      dec  ecx
  2105.      jnz  nogetsoundflag
  2106.      movzx  eax,byte [sound_flag] ; get sound_flag
  2107.      mov  [esp+32],eax
  2108.      ret
  2109.  nogetsoundflag:
  2110. ;     cmp  ecx,2
  2111.      dec  ecx
  2112.      jnz  nosoundflag
  2113.      xor  byte [sound_flag], 1
  2114.  nosoundflag:
  2115.      ret
  2116.  
  2117. sysfn_minimize:         ; 18.10 = minimize window
  2118.      mov   [window_minimize],1
  2119.      ret
  2120. align 4
  2121. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2122. ;     cmp  ecx,1
  2123.      dec  ecx
  2124.      jnz  full_table
  2125.   small_table:
  2126.      call for_all_tables
  2127.      mov ecx,10
  2128.      cld
  2129.      rep movsb
  2130.      ret
  2131.    for_all_tables:
  2132.      mov edi,edx
  2133.      mov esi,DRIVE_DATA
  2134.      ret
  2135.   full_table:
  2136. ;     cmp  ecx,2
  2137.      dec  ecx
  2138.      jnz  exit_for_anyone
  2139.      call for_all_tables
  2140.      mov ecx,16384
  2141.      cld
  2142.      rep movsd
  2143.      ret
  2144.  
  2145. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2146.         and     dword [esp+32], 0
  2147.         ret
  2148.  
  2149. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2150.      mov edi,ebx
  2151.      mov esi,version_inf
  2152.      mov ecx,version_end-version_inf
  2153.      rep movsb
  2154.      ret
  2155.  
  2156. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2157.      ;wait retrace functions
  2158.  sys_wait_retrace:
  2159.      mov edx,0x3da
  2160.  WaitRetrace_loop:
  2161.      in al,dx
  2162.      test al,1000b
  2163.      jz WaitRetrace_loop
  2164.      and [esp+32],dword 0
  2165.      ret
  2166.  
  2167. align 4
  2168. sysfn_centermouse:      ; 18.15 = mouse centered
  2169. ; removed here by <Lrz>
  2170. ;     call  mouse_centered
  2171. ;* mouse centered - start code- Mario79
  2172. ;mouse_centered:
  2173. ;        push  eax
  2174.         mov   eax,[Screen_Max_X]
  2175.         shr   eax,1
  2176.         mov   [MOUSE_X],ax
  2177.         mov   eax,[Screen_Max_Y]
  2178.         shr   eax,1
  2179.         mov   [MOUSE_Y],ax
  2180. ;        ret
  2181. ;* mouse centered - end code- Mario79
  2182.         xor   eax,eax
  2183.         and   [esp+32],eax
  2184. ;        pop   eax
  2185.  
  2186.      ret
  2187. align 4
  2188. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2189.      test ecx,ecx  ; get mouse speed factor
  2190.      jnz  .set_mouse_acceleration
  2191.      xor  eax,eax
  2192.      mov  ax,[mouse_speed_factor]
  2193.      mov  [esp+32],eax
  2194.      ret
  2195.  .set_mouse_acceleration:
  2196. ;     cmp  ecx,1  ; set mouse speed factor
  2197.      dec  ecx
  2198.      jnz  .get_mouse_delay
  2199.      mov  [mouse_speed_factor],dx
  2200.      ret
  2201.  .get_mouse_delay:
  2202. ;     cmp  ecx,2  ; get mouse delay
  2203.      dec  ecx
  2204.      jnz  .set_mouse_delay
  2205.      mov  eax,[mouse_delay]
  2206.      mov  [esp+32],eax
  2207.      ret
  2208.  .set_mouse_delay:
  2209. ;     cmp  ecx,3  ; set mouse delay
  2210.      dec  ecx
  2211.      jnz  .set_pointer_position
  2212.      mov  [mouse_delay],edx
  2213.      ret
  2214.  .set_pointer_position:
  2215. ;     cmp  ecx,4  ; set mouse pointer position
  2216.      dec   ecx
  2217.      jnz  .set_mouse_button
  2218.      mov   [MOUSE_Y],dx    ;y
  2219.      ror   edx,16
  2220.      mov   [MOUSE_X],dx    ;x
  2221.      rol   edx,16
  2222.      ret
  2223.  .set_mouse_button:
  2224. ;     cmp   ecx,5  ; set mouse button features
  2225.      dec   ecx
  2226.      jnz  .end
  2227.      mov   [BTN_DOWN],dl
  2228.      mov   [mouse_active],1
  2229.  .end:
  2230.      ret
  2231.  
  2232. sysfn_getfreemem:
  2233.      mov eax, [pg_data.pages_free]
  2234.      shl eax, 2
  2235.      mov [esp+32],eax
  2236.      ret
  2237.  
  2238. sysfn_getallmem:
  2239.      mov  eax,[MEM_AMOUNT]
  2240.      shr eax, 10
  2241.      mov  [esp+32],eax
  2242.      ret
  2243.  
  2244. ; // Alver, 2007-22-08 // {
  2245. sysfn_pid_to_slot:
  2246.      mov   eax, ecx
  2247.      call  pid_to_slot
  2248.      mov   [esp+32], eax
  2249.      ret
  2250.  
  2251. sysfn_min_rest_window:
  2252.      pushad
  2253.      mov   eax, edx      ; ebx - operating
  2254.      shr   ecx, 1
  2255.      jnc    @f
  2256.      call  pid_to_slot
  2257. @@:
  2258.      or    eax, eax      ; eax - number of slot
  2259.      jz    .error
  2260.      cmp   eax, 255         ; varify maximal slot number
  2261.      ja    .error
  2262.      movzx eax, word [WIN_STACK + eax*2]
  2263.      shr   ecx, 1
  2264.      jc    .restore
  2265.  ; .minimize:
  2266.      call  minimize_window
  2267.      jmp   .exit
  2268. .restore:
  2269.      call  restore_minimized_window
  2270. .exit:
  2271.      popad
  2272.      xor   eax, eax
  2273.      mov   [esp+32], eax
  2274.      ret
  2275. .error:
  2276.      popad
  2277.      xor   eax, eax
  2278.      dec   eax
  2279.      mov   [esp+32], eax
  2280.      ret
  2281. ; } \\ Alver, 2007-22-08 \\
  2282.  
  2283. uglobal
  2284. ;// mike.dld, 2006-29-01 [
  2285. screen_workarea RECT
  2286. ;// mike.dld, 2006-29-01 ]
  2287. window_minimize db 0
  2288. sound_flag      db 0
  2289. endg
  2290.  
  2291. iglobal
  2292. version_inf:
  2293.   db 0,7,7,0  ; version 0.7.7.0
  2294.   db UID_KOLIBRI
  2295.   dd __REV__
  2296. version_end:
  2297. endg
  2298.  
  2299. UID_NONE=0
  2300. UID_MENUETOS=1   ;official
  2301. UID_KOLIBRI=2    ;russian
  2302.  
  2303. sys_cachetodiskette:
  2304.         cmp     ebx, 1
  2305.         jne     .no_floppy_a_save
  2306.         mov     [flp_number], 1
  2307.         jmp     .save_image_on_floppy
  2308. .no_floppy_a_save:
  2309.         cmp     ebx, 2
  2310.         jne     .no_floppy_b_save
  2311.         mov     [flp_number], 2
  2312. .save_image_on_floppy:
  2313.         call    save_image
  2314.         mov     [esp + 32], dword 0
  2315.         cmp     [FDC_Status], 0
  2316.         je      .yes_floppy_save
  2317. .no_floppy_b_save:
  2318.         mov     [esp + 32], dword 1
  2319. .yes_floppy_save:
  2320.         ret
  2321.  
  2322. uglobal
  2323. ;  bgrchanged  dd  0x0
  2324. align 4
  2325. bgrlockpid dd 0
  2326. bgrlock db 0
  2327. endg
  2328.  
  2329. sys_background:
  2330.  
  2331.     cmp   ebx,1                            ; BACKGROUND SIZE
  2332.     jnz   nosb1
  2333.     test  ecx,ecx
  2334. ;    cmp   ecx,0
  2335.     jz    sbgrr
  2336.     test  edx,edx
  2337. ;    cmp   edx,0
  2338.     jz    sbgrr
  2339. @@:
  2340. ;;Maxis use atomic bts for mutexes  4.4.2009
  2341.         bts     dword [bgrlock], 0
  2342.         jnc     @f
  2343.         call    change_task
  2344.         jmp     @b
  2345. @@:
  2346.     mov   [BgrDataWidth],ecx
  2347.     mov   [BgrDataHeight],edx
  2348. ;    mov   [bgrchanged],1
  2349.  
  2350.     pushad
  2351. ; return memory for old background
  2352.         mov     eax, [img_background]
  2353.         cmp     eax, static_background_data
  2354.         jz      @f
  2355.         stdcall kernel_free, eax
  2356. @@:
  2357. ; calculate RAW size
  2358.     xor  eax,eax
  2359.     inc  eax
  2360.     cmp  [BgrDataWidth],eax
  2361.     jae   @f
  2362.     mov [BgrDataWidth],eax
  2363. @@:
  2364.     cmp  [BgrDataHeight],eax
  2365.     jae   @f
  2366.     mov [BgrDataHeight],eax
  2367. @@:
  2368.     mov  eax,[BgrDataWidth]
  2369.     imul eax,[BgrDataHeight]
  2370.     lea  eax,[eax*3]
  2371.     mov  [mem_BACKGROUND],eax
  2372. ; get memory for new background
  2373.     stdcall kernel_alloc, eax
  2374.     test eax, eax
  2375.     jz .memfailed
  2376.     mov [img_background], eax
  2377.     jmp .exit
  2378. .memfailed:
  2379. ; revert to static monotone data
  2380.         mov     [img_background], static_background_data
  2381.         xor     eax, eax
  2382.         inc     eax
  2383.         mov     [BgrDataWidth], eax
  2384.         mov     [BgrDataHeight], eax
  2385.         mov     [mem_BACKGROUND], 4
  2386. .exit:
  2387.     popad
  2388.         mov     [bgrlock], 0
  2389.  
  2390.   sbgrr:
  2391.     ret
  2392.  
  2393. nosb1:
  2394.  
  2395.     cmp   ebx,2                            ; SET PIXEL
  2396.     jnz   nosb2
  2397.  
  2398.     mov   eax, [img_background]
  2399.     test  ecx, ecx
  2400.     jz    @f
  2401.     cmp   eax, static_background_data
  2402.     jz    .ret
  2403. @@:
  2404.     mov ebx, [mem_BACKGROUND]
  2405.     add ebx, 4095
  2406.     and ebx, -4096
  2407.     sub ebx, 4
  2408.     cmp   ecx, ebx
  2409.     ja   .ret
  2410.  
  2411.     mov   ebx,[eax+ecx]
  2412.     and   ebx,0xFF000000 ;255*256*256*256
  2413.     and   edx,0x00FFFFFF ;255*256*256+255*256+255
  2414.     add   edx,ebx
  2415.     mov   [eax+ecx],edx
  2416. .ret:
  2417.     ret
  2418. nosb2:
  2419.  
  2420.     cmp   ebx,3                            ; DRAW BACKGROUND
  2421.     jnz   nosb3
  2422. draw_background_temp:
  2423. ;    cmp   [bgrchanged],1 ;0
  2424. ;    je    nosb31
  2425. ;draw_background_temp:
  2426. ;    mov   [bgrchanged],1 ;0
  2427.     mov    [background_defined], 1
  2428.     call  force_redraw_background
  2429.     mov    [REDRAW_BACKGROUND], byte 2
  2430.    nosb31:
  2431.     ret
  2432.   nosb3:
  2433.  
  2434.     cmp   ebx,4                            ; TILED / STRETCHED
  2435.     jnz   nosb4
  2436.     cmp   ecx,[BgrDrawMode]
  2437.     je    nosb41
  2438.     mov   [BgrDrawMode],ecx
  2439. ;    mov   [bgrchanged],1
  2440.    nosb41:
  2441.     ret
  2442.   nosb4:
  2443.  
  2444.     cmp   ebx,5                            ; BLOCK MOVE TO BGR
  2445.     jnz   nosb5
  2446.     cmp   [img_background], static_background_data
  2447.     jnz   @f
  2448.     test  edx, edx
  2449.     jnz   .fin
  2450.     cmp   esi, 4
  2451.     ja    .fin
  2452.   @@:
  2453.   ; bughere
  2454.     mov   eax, ecx
  2455.     mov   ebx, edx
  2456.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2457.     mov   ecx, esi
  2458.     call  memmove
  2459.   .fin:
  2460.     ret
  2461.   nosb5:
  2462.  
  2463.         cmp     ebx, 6
  2464.         jnz     nosb6
  2465. ;;Maxis use atomic bts for mutex 4.4.2009
  2466. @@:
  2467.         bts     dword [bgrlock], 0
  2468.         jnc     @f
  2469.         call    change_task
  2470.         jmp     @b
  2471. @@:
  2472.         mov     eax, [CURRENT_TASK]
  2473.         mov     [bgrlockpid], eax
  2474.         cmp     [img_background], static_background_data
  2475.         jz      .nomem
  2476.         stdcall user_alloc, [mem_BACKGROUND]
  2477.         mov     [esp+32], eax
  2478.         test    eax, eax
  2479.         jz      .nomem
  2480.         mov     ebx, eax
  2481.         shr     ebx, 12
  2482.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2483.         mov     esi, [img_background]
  2484.         shr     esi, 12
  2485.         mov     ecx, [mem_BACKGROUND]
  2486.         add     ecx, 0xFFF
  2487.         shr     ecx, 12
  2488. .z:
  2489.         mov     eax, [page_tabs+ebx*4]
  2490.         test    al, 1
  2491.         jz      @f
  2492.         call    free_page
  2493. @@:
  2494.         mov     eax, [page_tabs+esi*4]
  2495.         or      al, PG_UW
  2496.         mov     [page_tabs+ebx*4], eax
  2497.         mov     eax, ebx
  2498.         shl     eax, 12
  2499.         invlpg  [eax]
  2500.         inc     ebx
  2501.         inc     esi
  2502.         loop    .z
  2503.         ret
  2504. .nomem:
  2505.         and     [bgrlockpid], 0
  2506.         mov     [bgrlock], 0
  2507. nosb6:
  2508.         cmp     ebx, 7
  2509.         jnz     nosb7
  2510.         cmp     [bgrlock], 0
  2511.         jz      .err
  2512.         mov     eax, [CURRENT_TASK]
  2513.         cmp     [bgrlockpid], eax
  2514.         jnz     .err
  2515.         mov     eax, ecx
  2516.         mov     ebx, ecx
  2517.         shr     eax, 12
  2518.         mov     ecx, [page_tabs+(eax-1)*4]
  2519.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2520.         jz      .err
  2521.         jnp     .err
  2522.         push    eax
  2523.         shr     ecx, 12
  2524.         dec     ecx
  2525. @@:
  2526.         and     dword [page_tabs+eax*4], 0
  2527.         mov     edx, eax
  2528.         shl     edx, 12
  2529.         push eax
  2530.         invlpg  [edx]
  2531.         pop eax
  2532.         inc     eax
  2533.         loop    @b
  2534.         pop     eax
  2535.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2536.         stdcall user_free, ebx
  2537.         mov     [esp+32], eax
  2538.         and     [bgrlockpid], 0
  2539.         mov     [bgrlock], 0
  2540.         ret
  2541. .err:
  2542.         and     dword [esp+32], 0
  2543.         ret
  2544.  
  2545. nosb7:
  2546.     ret
  2547.  
  2548. force_redraw_background:
  2549.     and   [draw_data+32 + RECT.left],dword 0
  2550.     and   [draw_data+32 + RECT.top],dword 0
  2551.     push  eax ebx
  2552.     mov   eax,[Screen_Max_X]
  2553.     mov   ebx,[Screen_Max_Y]
  2554.     mov   [draw_data+32 + RECT.right],eax
  2555.     mov   [draw_data+32 + RECT.bottom],ebx
  2556.     pop   ebx eax
  2557.     mov   byte [REDRAW_BACKGROUND], 1
  2558.     ret
  2559.  
  2560. align 4
  2561.  
  2562. sys_getbackground:
  2563. ;    cmp   eax,1                                  ; SIZE
  2564.     dec   ebx
  2565.     jnz   nogb1
  2566.     mov   eax,[BgrDataWidth]
  2567.     shl   eax,16
  2568.     mov   ax,[BgrDataHeight]
  2569.     mov   [esp+32],eax
  2570.     ret
  2571.  
  2572. nogb1:
  2573. ;    cmp   eax,2                                  ; PIXEL
  2574.     dec   ebx
  2575.     jnz   nogb2
  2576.  
  2577.         mov     eax, [img_background]
  2578.         test    ecx, ecx
  2579.         jz      @f
  2580.         cmp     eax, static_background_data
  2581.         jz      .ret
  2582. @@:
  2583.     mov ebx, [mem_BACKGROUND]
  2584.     add ebx, 4095
  2585.     and ebx, -4096
  2586.     sub ebx, 4
  2587.     cmp ecx, ebx
  2588.     ja  .ret
  2589.  
  2590.     mov   eax,[ecx+eax]
  2591.  
  2592.     and   eax, 0xFFFFFF
  2593.     mov   [esp+32],eax
  2594. .ret:
  2595.     ret
  2596.   nogb2:
  2597.  
  2598. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2599.     dec   ebx
  2600.     dec   ebx
  2601.     jnz   nogb4
  2602.     mov   eax,[BgrDrawMode]
  2603.   nogb4:
  2604.     mov   [esp+32],eax
  2605.     ret
  2606.  
  2607. align 4
  2608.  
  2609. sys_getkey:
  2610.         mov     [esp + 32],dword 1
  2611.         ; test main buffer
  2612.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2613.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2614.         mov     edx, [TASK_COUNT]
  2615.         cmp     ecx, edx
  2616.         jne     .finish
  2617.         cmp     [KEY_COUNT], byte 0
  2618.         je      .finish
  2619.         movzx   eax, byte [KEY_BUFF]
  2620.         shl     eax, 8
  2621.         push    eax
  2622.         dec     byte [KEY_COUNT]
  2623.         and     byte [KEY_COUNT], 127
  2624.         movzx   ecx, byte [KEY_COUNT]
  2625.         add     ecx, 2
  2626.         mov     eax, KEY_BUFF + 1
  2627.         mov     ebx, KEY_BUFF
  2628.         call    memmove
  2629.         pop     eax
  2630. .ret_eax:
  2631.         mov     [esp + 32], eax
  2632.         ret
  2633. .finish:
  2634. ; test hotkeys buffer
  2635.         mov     ecx, hotkey_buffer
  2636. @@:
  2637.         cmp     [ecx], ebx
  2638.         jz      .found
  2639.         add     ecx, 8
  2640.         cmp     ecx, hotkey_buffer + 120 * 8
  2641.         jb      @b
  2642.         ret
  2643. .found:
  2644.         mov     ax, [ecx + 6]
  2645.         shl     eax, 16
  2646.         mov     ah, [ecx + 4]
  2647.         mov     al, 2
  2648.         and     dword [ecx + 4], 0
  2649.         and     dword [ecx], 0
  2650.         jmp     .ret_eax
  2651.  
  2652. align 4
  2653.  
  2654. sys_getbutton:
  2655.  
  2656.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2657.         mov     [esp + 32], dword 1
  2658.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2659.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2660.         cmp     ecx, edx
  2661.         jne     .exit
  2662.         movzx   eax, byte [BTN_COUNT]
  2663.         test    eax, eax
  2664.         jz      .exit
  2665.         mov     eax, [BTN_BUFF]
  2666.         shl     eax, 8
  2667. ; // Alver 22.06.2008 // {
  2668.         mov       al, byte [btn_down_determ]
  2669.         and       al,0xFE                                       ; delete left button bit
  2670. ; } \\ Alver \\
  2671.         mov     [BTN_COUNT], byte 0
  2672.         mov     [esp + 32], eax
  2673. .exit:
  2674.         ret
  2675.  
  2676.  
  2677. align 4
  2678.  
  2679. sys_cpuusage:
  2680.  
  2681. ;  RETURN:
  2682. ;
  2683. ;  +00 dword     process cpu usage
  2684. ;  +04  word     position in windowing stack
  2685. ;  +06  word     windowing stack value at current position (cpu nro)
  2686. ;  +10 12 bytes  name
  2687. ;  +22 dword     start in mem
  2688. ;  +26 dword     used mem
  2689. ;  +30 dword     PID , process idenfification number
  2690. ;
  2691.  
  2692.     cmp  ecx,-1         ; who am I ?
  2693.     jne  .no_who_am_i
  2694.     mov  ecx,[CURRENT_TASK]
  2695.   .no_who_am_i:
  2696.         cmp     ecx, max_processes
  2697.         ja      .nofillbuf
  2698.  
  2699. ; +4: word: position of the window of thread in the window stack
  2700.         mov     ax, [WIN_STACK + ecx * 2]
  2701.         mov     [ebx+4], ax
  2702. ; +6: word: number of the thread slot, which window has in the window stack
  2703. ;           position ecx (has no relation to the specific thread)
  2704.         mov     ax, [WIN_POS + ecx * 2]
  2705.         mov     [ebx+6], ax
  2706.  
  2707.         shl     ecx, 5
  2708.  
  2709. ; +0: dword: memory usage
  2710.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2711.         mov     [ebx], eax
  2712. ; +10: 11 bytes: name of the process
  2713.         push    ecx
  2714.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2715.         add     ebx, 10
  2716.         mov     ecx, 11
  2717.         call    memmove
  2718.         pop     ecx
  2719.  
  2720. ; +22: address of the process in memory
  2721. ; +26: size of used memory - 1
  2722.         push    edi
  2723.         lea     edi, [ebx+12]
  2724.         xor     eax, eax
  2725.         mov     edx, 0x100000*16
  2726.         cmp     ecx, 1 shl 5
  2727.         je      .os_mem
  2728.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2729.         mov     eax, std_application_base_address
  2730. .os_mem:
  2731.         stosd
  2732.         lea     eax, [edx-1]
  2733.         stosd
  2734.  
  2735. ; +30: PID/TID
  2736.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2737.         stosd
  2738.  
  2739.     ; window position and size
  2740.         push    esi
  2741.         lea     esi, [ecx + window_data + WDATA.box]
  2742.         movsd
  2743.         movsd
  2744.         movsd
  2745.         movsd
  2746.  
  2747.     ; Process state (+50)
  2748.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2749.         stosd
  2750.  
  2751.     ; Window client area box
  2752.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2753.         movsd
  2754.         movsd
  2755.         movsd
  2756.         movsd
  2757.  
  2758.     ; Window state
  2759.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2760.         stosb
  2761.  
  2762.         pop     esi
  2763.         pop     edi
  2764.  
  2765. .nofillbuf:
  2766.     ; return number of processes
  2767.  
  2768.     mov    eax,[TASK_COUNT]
  2769.     mov    [esp+32],eax
  2770.     ret
  2771.  
  2772. align 4
  2773. sys_clock:
  2774.         cli
  2775.   ; Mikhail Lisovin  xx Jan 2005
  2776.   @@:   mov   al, 10
  2777.         out   0x70, al
  2778.         in    al, 0x71
  2779.         test  al, al
  2780.         jns   @f
  2781.         mov   esi, 1
  2782.         call  delay_ms
  2783.         jmp   @b
  2784.   @@:
  2785.   ; end Lisovin's fix
  2786.  
  2787.         xor   al,al           ; seconds
  2788.         out   0x70,al
  2789.         in    al,0x71
  2790.         movzx ecx,al
  2791.         mov   al,02           ; minutes
  2792.         shl   ecx,16
  2793.         out   0x70,al
  2794.         in    al,0x71
  2795.         movzx edx,al
  2796.         mov   al,04           ; hours
  2797.         shl   edx,8
  2798.         out   0x70,al
  2799.         in    al,0x71
  2800.         add   ecx,edx
  2801.         movzx edx,al
  2802.         add   ecx,edx
  2803.         sti
  2804.         mov     [esp + 32], ecx
  2805.         ret
  2806.  
  2807.  
  2808. align 4
  2809.  
  2810. sys_date:
  2811.  
  2812.         cli
  2813.   @@:   mov   al, 10
  2814.         out   0x70, al
  2815.         in    al, 0x71
  2816.         test  al, al
  2817.         jns   @f
  2818.         mov   esi, 1
  2819.         call  delay_ms
  2820.         jmp   @b
  2821.   @@:
  2822.  
  2823.         mov     ch,0
  2824.         mov     al,7            ; date
  2825.         out     0x70,al
  2826.         in      al,0x71
  2827.         mov     cl,al
  2828.         mov     al,8            ; month
  2829.         shl     ecx,16
  2830.         out     0x70,al
  2831.         in      al,0x71
  2832.         mov     ch,al
  2833.         mov     al,9            ; year
  2834.         out     0x70,al
  2835.         in      al,0x71
  2836.         mov     cl,al
  2837.         sti
  2838.         mov     [esp+32], ecx
  2839.         ret
  2840.  
  2841.  
  2842. ; redraw status
  2843.  
  2844. sys_redrawstat:
  2845.         cmp     ebx, 1
  2846.         jne     no_widgets_away
  2847.         ; buttons away
  2848.         mov     ecx,[CURRENT_TASK]
  2849.   sys_newba2:
  2850.         mov     edi,[BTN_ADDR]
  2851.         cmp     [edi], dword 0  ; empty button list ?
  2852.         je      end_of_buttons_away
  2853.         movzx   ebx, word [edi]
  2854.         inc     ebx
  2855.         mov     eax,edi
  2856.   sys_newba:
  2857.         dec     ebx
  2858.         jz      end_of_buttons_away
  2859.  
  2860.         add     eax, 0x10
  2861.         cmp     cx, [eax]
  2862.         jnz     sys_newba
  2863.  
  2864.         push    eax ebx ecx
  2865.         mov     ecx,ebx
  2866.         inc     ecx
  2867.         shl     ecx, 4
  2868.         mov     ebx, eax
  2869.         add     eax, 0x10
  2870.         call    memmove
  2871.         dec     dword [edi]
  2872.         pop     ecx ebx eax
  2873.  
  2874.         jmp     sys_newba2
  2875.  
  2876.   end_of_buttons_away:
  2877.  
  2878.         ret
  2879.  
  2880.   no_widgets_away:
  2881.  
  2882.         cmp     ebx, 2
  2883.         jnz     srl1
  2884.  
  2885.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2886.         add     edx, draw_data - CURRENT_TASK
  2887.         mov     [edx + RECT.left], 0
  2888.         mov     [edx + RECT.top], 0
  2889.         mov     eax, [Screen_Max_X]
  2890.         mov     [edx + RECT.right], eax
  2891.         mov     eax, [Screen_Max_Y]
  2892.         mov     [edx + RECT.bottom], eax
  2893.  
  2894.         mov     edi, [TASK_BASE]
  2895.         or      [edi - twdw + WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2896.         call    sys_window_mouse
  2897.         ret
  2898.  
  2899.   srl1:
  2900.         ret
  2901.  
  2902.  
  2903. sys_drawwindow:
  2904.  
  2905.     mov   eax,edx
  2906.     shr   eax,16+8
  2907.     and   eax,15
  2908.  
  2909. ;    cmp   eax,0   ; type I    - original style
  2910.     jne   nosyswI
  2911.     inc   [mouse_pause]
  2912.     call  [_display.disable_mouse]
  2913.     call  sys_set_window
  2914.     call  [_display.disable_mouse]
  2915.     call  drawwindow_I
  2916.     ;dec   [mouse_pause]
  2917.     ;call   [draw_pointer]
  2918.     ;ret
  2919.     jmp   draw_window_caption.2
  2920.   nosyswI:
  2921.  
  2922.     cmp   al,1    ; type II   - only reserve area, no draw
  2923.     jne   nosyswII
  2924.     inc   [mouse_pause]
  2925.     call  [_display.disable_mouse]
  2926.     call  sys_set_window
  2927.     call  [_display.disable_mouse]
  2928.     call  sys_window_mouse
  2929.     dec   [mouse_pause]
  2930.     call   [draw_pointer]
  2931.     ret
  2932.   nosyswII:
  2933.  
  2934.     cmp   al,2    ; type III  - new style
  2935.     jne   nosyswIII
  2936.     inc   [mouse_pause]
  2937.     call  [_display.disable_mouse]
  2938.     call  sys_set_window
  2939.     call  [_display.disable_mouse]
  2940.     call  drawwindow_III
  2941.     ;dec   [mouse_pause]
  2942.     ;call   [draw_pointer]
  2943.     ;ret
  2944.     jmp   draw_window_caption.2
  2945.   nosyswIII:
  2946.  
  2947.     cmp   al,3    ; type IV - skinned window
  2948.     je    draw_skin_window
  2949.     cmp   al,4    ; type V - skinned window not sized! {not_sized_skin_window}
  2950.     jne   nosyswV
  2951.   draw_skin_window:
  2952.  
  2953.     inc   [mouse_pause]
  2954.     call  [_display.disable_mouse]
  2955.     call  sys_set_window
  2956.     call  [_display.disable_mouse]
  2957.     mov   eax, [TASK_COUNT]
  2958.     movzx eax, word [WIN_POS + eax*2]
  2959.     cmp   eax, [CURRENT_TASK]
  2960.     setz  al
  2961.     movzx eax, al
  2962.     push  eax
  2963.     call  drawwindow_IV
  2964.     ;dec   [mouse_pause]
  2965.     ;call   [draw_pointer]
  2966.     ;ret
  2967.     jmp   draw_window_caption.2
  2968.   nosyswV:
  2969.  
  2970.     ret
  2971.  
  2972.  
  2973. draw_window_caption:
  2974.         inc     [mouse_pause]
  2975.         call    [_display.disable_mouse]
  2976.  
  2977.         xor     eax,eax
  2978.         mov     edx,[TASK_COUNT]
  2979.         movzx   edx,word[WIN_POS+edx*2]
  2980.         cmp     edx,[CURRENT_TASK]
  2981.         jne     @f
  2982.         inc     eax
  2983.     @@: mov     edx,[CURRENT_TASK]
  2984.         shl     edx,5
  2985.         add     edx,window_data
  2986.         movzx   ebx,[edx+WDATA.fl_wstyle]
  2987.         and     bl,0x0F
  2988.         cmp     bl,3
  2989.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  2990.         cmp     bl,4
  2991.         je      .draw_caption_style_3
  2992.  
  2993.         jmp     .not_style_3
  2994.   .draw_caption_style_3:
  2995.  
  2996.         push    edx
  2997.         call    drawwindow_IV_caption
  2998.         add     esp,4
  2999.         jmp     .2
  3000.  
  3001.   .not_style_3:
  3002.         cmp     bl,2
  3003.         jne     .not_style_2
  3004.  
  3005.         call    drawwindow_III_caption
  3006.         jmp     .2
  3007.  
  3008.   .not_style_2:
  3009.         cmp     bl,0
  3010.         jne     .2
  3011.  
  3012.         call    drawwindow_I_caption
  3013.  
  3014. ;--------------------------------------------------------------
  3015.   .2:   ;jmp     @f
  3016.         mov     edi,[CURRENT_TASK]
  3017.         shl     edi,5
  3018.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3019.         jz      @f
  3020.         mov     edx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  3021.         or      edx,edx
  3022.         jz      @f
  3023.  
  3024.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  3025.         and     al,0x0F
  3026.         cmp     al,3
  3027.         je      .skinned
  3028.         cmp     al,4
  3029.         je      .skinned
  3030.  
  3031.         jmp     .not_skinned
  3032.   .skinned:
  3033.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3034.         mov     bp,word[edi+window_data+WDATA.box.top]
  3035.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3036.         sub     ax,[_skinmargins.left]
  3037.         sub     ax,[_skinmargins.right]
  3038.         push    edx
  3039.         cwde
  3040.         cdq
  3041.         mov     ebx,6
  3042.         idiv    ebx
  3043.         pop     edx
  3044.         or      eax,eax
  3045.         js      @f
  3046.         mov     esi,eax
  3047.         mov     ebx,dword[_skinmargins.left-2]
  3048.         mov     bx,word[_skinh]
  3049.         sub     bx,[_skinmargins.bottom]
  3050.         sub     bx,[_skinmargins.top]
  3051.         sar     bx,1
  3052.         adc     bx,0
  3053.         add     bx,[_skinmargins.top]
  3054.         add     bx,-3
  3055.         add     ebx,ebp
  3056.         jmp     .dodraw
  3057.  
  3058.   .not_skinned:
  3059.         cmp     al,1
  3060.         je      @f
  3061.  
  3062.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3063.         mov     bp,word[edi+window_data+WDATA.box.top]
  3064.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3065.         sub     eax,16
  3066.         push    edx
  3067.         cwde
  3068.         cdq
  3069.         mov     ebx,6
  3070.         idiv    ebx
  3071.         pop     edx
  3072.         or      eax,eax
  3073.         js      @f
  3074.         mov     esi,eax
  3075.         mov     ebx,0x00080007
  3076.         add     ebx,ebp
  3077. .dodraw:
  3078.         mov     ecx,[common_colours+16];0x00FFFFFF
  3079.         or      ecx, 0x80000000
  3080.         xor     edi,edi
  3081. ; // Alver 22.06.2008 // {
  3082. ;       call    dtext
  3083.         call dtext_asciiz_esi
  3084. ; } \\ Alver \\
  3085.  
  3086.     @@:
  3087. ;--------------------------------------------------------------
  3088.         dec     [mouse_pause]
  3089.         call    [draw_pointer]
  3090.         ret
  3091.  
  3092. iglobal
  3093. align 4
  3094. window_topleft dd \
  3095.   1, 21,\               ;type 0
  3096.   0,  0,\       ;type 1
  3097.   5, 20,\       ;type 2
  3098.   5,  ?,\       ;type 3 {set by skin}
  3099.   5,  ?         ;type 4 {set by skin}
  3100. endg
  3101.  
  3102. set_window_clientbox:
  3103.         push    eax ecx edi
  3104.  
  3105.         mov     eax,[_skinh]
  3106.         mov     [window_topleft+4*7],eax
  3107.         mov     [window_topleft+4*9],eax
  3108.  
  3109.         mov     ecx,edi
  3110.         sub     edi,window_data
  3111.         shl     edi,3
  3112.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3113.         jz      @f
  3114.  
  3115.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3116.         and     eax,0x0F
  3117.         mov     eax,[eax*8+window_topleft+0]
  3118.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3119.         shl     eax,1
  3120.         neg     eax
  3121.         add     eax,[ecx+WDATA.box.width]
  3122.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3123.  
  3124.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3125.         and     eax,0x0F
  3126.         push    [eax*8+window_topleft+0]
  3127.         mov     eax,[eax*8+window_topleft+4]
  3128.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3129.         neg     eax
  3130.         sub     eax,[esp]
  3131.         add     eax,[ecx+WDATA.box.height]
  3132.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3133.         add     esp,4
  3134.  
  3135.         pop     edi ecx eax
  3136.         ret
  3137.     @@:
  3138.         xor     eax,eax
  3139.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3140.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3141.         mov     eax,[ecx+WDATA.box.width]
  3142.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3143.         mov     eax,[ecx+WDATA.box.height]
  3144.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3145.  
  3146.         pop     edi ecx eax
  3147.         ret
  3148.  
  3149. sys_set_window:
  3150.  
  3151.     mov   eax,[CURRENT_TASK]
  3152.     shl   eax,5
  3153.     add   eax,window_data
  3154.  
  3155.     ; colors
  3156.     mov   [eax+WDATA.cl_workarea],edx
  3157.     mov   [eax+WDATA.cl_titlebar],esi
  3158.     mov   [eax+WDATA.cl_frames],edi
  3159.  
  3160.     mov   edi, eax
  3161.  
  3162.     ; check flag (?)
  3163.     test  [edi+WDATA.fl_wdrawn],1
  3164.     jnz   newd
  3165.  
  3166.     mov   eax,[timer_ticks] ;[0xfdf0]
  3167.     add   eax,100
  3168.     mov   [new_window_starting],eax
  3169.  
  3170.     movsx eax,bx
  3171.     mov   [edi+WDATA.box.width],eax
  3172.     movsx eax,cx
  3173.     mov   [edi+WDATA.box.height],eax
  3174.     sar   ebx,16
  3175.     sar   ecx,16
  3176.     mov   [edi+WDATA.box.left],ebx
  3177.     mov   [edi+WDATA.box.top],ecx
  3178.  
  3179.     call  check_window_position
  3180.  
  3181.     push  ecx esi edi               ; save for window fullscreen/resize
  3182.     ;mov   esi,edi
  3183.  
  3184.         mov     cl, [edi+WDATA.fl_wstyle]
  3185.         mov     eax, [edi+WDATA.cl_frames]
  3186.  
  3187.     sub   edi,window_data
  3188.     shl   edi,3
  3189.     add   edi,SLOT_BASE
  3190.  
  3191.         and     cl,0x0F
  3192.         mov     [edi+APPDATA.wnd_caption],0
  3193.         cmp     cl,3
  3194.         je      set_APPDATA_wnd_caption
  3195.         cmp     cl,4                                                            ; {SPraid.simba}
  3196.         je      set_APPDATA_wnd_caption
  3197.  
  3198.         jmp     @f
  3199.     set_APPDATA_wnd_caption:
  3200.         mov     [edi+APPDATA.wnd_caption],eax
  3201.     @@: mov     esi,[esp+0]
  3202.  
  3203.     add   edi, APPDATA.saved_box
  3204.         movsd
  3205.         movsd
  3206.         movsd
  3207.         movsd
  3208.     pop   edi esi ecx
  3209.  
  3210.         mov     esi, [CURRENT_TASK]
  3211.         movzx   esi, word [WIN_STACK+esi*2]
  3212.         lea     esi, [WIN_POS+esi*2]
  3213.         call    waredraw
  3214.  
  3215. ;;;    mov   ebx, 1
  3216. ;;;    call  delay_hs
  3217.     mov   eax, [edi+WDATA.box.left]
  3218.     mov   ebx, [edi+WDATA.box.top]
  3219.     mov   ecx, [edi+WDATA.box.width]
  3220.     mov   edx, [edi+WDATA.box.height]
  3221.     add   ecx, eax
  3222.     add   edx, ebx
  3223.     call  calculatescreen
  3224.  
  3225.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3226.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3227.  
  3228.   newd:
  3229.     call  set_window_clientbox
  3230.  
  3231.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3232.     mov   edx,edi
  3233.  
  3234.     ret
  3235.  
  3236. syscall_windowsettings:
  3237.  
  3238.   .set_window_caption:
  3239.         dec     ebx     ; subfunction #1 - set window caption
  3240.         jnz     .exit_fail
  3241.  
  3242.         ; NOTE: only window owner thread can set its caption,
  3243.         ;       so there's no parameter for PID/TID
  3244.  
  3245.         mov     edi,[CURRENT_TASK]
  3246.         shl     edi,5
  3247.  
  3248.         ; have to check if caption is within application memory limit
  3249.         ; check is trivial, and if application resizes its memory,
  3250.         ;   caption still can become over bounds
  3251. ; diamond, 31.10.2006: check removed because with new memory manager
  3252. ; there can be valid data after APPDATA.mem_size bound
  3253. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3254. ;        add     ecx,255 ; max caption length
  3255. ;        cmp     ebx,ecx
  3256. ;        ja      .exit_fail
  3257.  
  3258.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ecx
  3259.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3260.  
  3261.         call    draw_window_caption
  3262.  
  3263.         xor     eax,eax ; eax = 0 (success)
  3264.         ret
  3265.  
  3266. ;  .get_window_caption:
  3267. ;        dec     eax     ; subfunction #2 - get window caption
  3268. ;        jnz     .exit_fail
  3269.  
  3270.         ; not implemented yet
  3271.  
  3272.   .exit_fail:
  3273.         xor     eax,eax
  3274.         inc     eax     ; eax = 1 (fail)
  3275.         ret
  3276.  
  3277.  
  3278. sys_window_move:
  3279.  
  3280.         mov     edi,[CURRENT_TASK]
  3281.         shl     edi,5
  3282.         add     edi,window_data
  3283.  
  3284.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3285.         jnz     .window_move_return
  3286.  
  3287.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3288.         push    dword [edi + WDATA.box.top]
  3289.         push    dword [edi + WDATA.box.width]
  3290.         push    dword [edi + WDATA.box.height]
  3291.  
  3292.         cmp   eax,-1                  ; set new position and size
  3293.         je    .no_x_reposition
  3294.         mov     [edi + WDATA.box.left], eax
  3295.       .no_x_reposition:
  3296.         cmp   ebx,-1
  3297.         je    .no_y_reposition
  3298.         mov     [edi + WDATA.box.top], ebx
  3299.       .no_y_reposition:
  3300.  
  3301.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3302.         jnz     .no_y_resizing
  3303.  
  3304.         cmp   ecx,-1
  3305.         je    .no_x_resizing
  3306.         mov     [edi + WDATA.box.width], ecx
  3307.       .no_x_resizing:
  3308.         cmp   edx,-1
  3309.         je    .no_y_resizing
  3310.         mov     [edi + WDATA.box.height], edx
  3311.       .no_y_resizing:
  3312.  
  3313.         call  check_window_position
  3314.         call  set_window_clientbox
  3315.  
  3316.         pushad                       ; save for window fullscreen/resize
  3317.         mov   esi,edi
  3318.         sub   edi,window_data
  3319.         shr   edi,5
  3320.         shl   edi,8
  3321.         add   edi, SLOT_BASE + APPDATA.saved_box
  3322.         mov   ecx,4
  3323.         cld
  3324.         rep   movsd
  3325.         popad
  3326.  
  3327.         pushad                       ; calculcate screen at new position
  3328.         mov   eax, [edi + WDATA.box.left]
  3329.         mov   ebx, [edi + WDATA.box.top]
  3330.         mov   ecx, [edi + WDATA.box.width]
  3331.         mov   edx, [edi + WDATA.box.height]
  3332.         add   ecx,eax
  3333.         add   edx,ebx
  3334.  
  3335.         call  calculatescreen
  3336.         popad
  3337.  
  3338.         pop   edx                   ; calculcate screen at old position
  3339.         pop   ecx
  3340.         pop   ebx
  3341.         pop   eax
  3342.         add   ecx,eax
  3343.         add   edx,ebx
  3344.         mov   [draw_limits.left],eax             ; save for drawlimits
  3345.         mov   [draw_limits.top],ebx
  3346.         mov   [draw_limits.right],ecx
  3347.         mov   [draw_limits.bottom],edx
  3348.         call  calculatescreen
  3349.  
  3350.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3351.  
  3352.         mov   eax,edi               ; redraw screen at old position
  3353.         xor   esi,esi
  3354.         call  redrawscreen
  3355.  
  3356.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3357.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3358.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3359.  
  3360.         call  [draw_pointer]
  3361.  
  3362.         mov   [window_move_pr],0
  3363.  
  3364.       .window_move_return:
  3365.  
  3366.         ret
  3367.  
  3368. uglobal
  3369.   window_move_pr   dd  0x0
  3370.   window_move_eax  dd  0x0
  3371.   window_move_ebx  dd  0x0
  3372.   window_move_ecx  dd  0x0
  3373.   window_move_edx  dd  0x0
  3374. endg
  3375.  
  3376. ;ok - 100% work
  3377. ;nt - not tested
  3378. ;---------------------------------------------------------------------------------------------
  3379. ;eax
  3380. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3381. ;1 - change task. Ret nothing. Block. ok.
  3382. ;2 - performance control
  3383. ; ebx
  3384. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3385. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3386. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3387. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3388. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3389. ;eax
  3390. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3391. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3392. ;---------------------------------------------------------------------------------------------
  3393. iglobal
  3394. align 4
  3395. sheduler:
  3396.         dd      sys_sheduler.00
  3397.         dd      change_task
  3398.         dd      sys_sheduler.02
  3399.         dd      sys_sheduler.03
  3400.         dd      sys_sheduler.04
  3401. endg
  3402. sys_sheduler:  
  3403. ;rewritten by <Lrz>  29.12.2009
  3404.         jmp     dword [sheduler+ebx*4]
  3405. ;.shed_counter:
  3406. .00:
  3407.         mov eax,[context_counter]
  3408.         mov [esp+32],eax
  3409.         ret
  3410.  
  3411. .02:
  3412. ;.perf_control:
  3413.         inc     ebx                     ;before ebx=2, ebx=3
  3414.         cmp     ebx,ecx                 ;if ecx=3, ebx=3
  3415.         jz      cache_disable          
  3416.  
  3417.         dec     ebx                     ;ebx=2
  3418.         cmp     ebx,ecx                 ;
  3419.         jz      cache_enable            ;if ecx=2 and ebx=2
  3420.  
  3421.         dec     ebx                     ;ebx=1
  3422.         cmp     ebx,ecx
  3423.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3424.  
  3425.         dec     ebx
  3426.         test    ebx,ecx                 ;ebx=0 and ecx=0
  3427.         jz      modify_pce              ;if ecx=0
  3428.  
  3429.         ret
  3430.  
  3431. .03:   
  3432. ;.rdmsr_instr:
  3433. ;now counter in ecx
  3434. ;(edx:eax) esi:edi => edx:esi
  3435.         mov     eax,esi
  3436.         mov     ecx,edx
  3437.         rdmsr
  3438.         mov     [esp+32],eax
  3439.         mov     [esp+20],edx            ;ret in ebx?
  3440.         ret
  3441.  
  3442. .04:
  3443. ;.wrmsr_instr:
  3444. ;now counter in ecx
  3445. ;(edx:eax) esi:edi => edx:esi
  3446.         ; Fast Call MSR can't be destroy
  3447.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  3448.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  3449.         cmp     edx,MSR_SYSENTER_CS
  3450.         je      @f
  3451.         cmp     edx,MSR_SYSENTER_ESP
  3452.         je      @f
  3453.         cmp     edx,MSR_SYSENTER_EIP
  3454.         je      @f
  3455.         cmp     edx,MSR_AMD_STAR
  3456.         je      @f
  3457.  
  3458.         mov     eax,esi
  3459.         mov     ecx,edx
  3460.         wrmsr
  3461.         ; mov   [esp + 32], eax
  3462.         ; mov   [esp + 20], edx ;ret in ebx?
  3463. @@:
  3464.         ret
  3465.  
  3466. cache_disable:
  3467.        mov eax,cr0
  3468.        or  eax,01100000000000000000000000000000b
  3469.        mov cr0,eax
  3470.        wbinvd ;set MESI
  3471. ret
  3472.  
  3473. cache_enable:
  3474.        mov eax,cr0
  3475.        and eax,10011111111111111111111111111111b
  3476.        mov cr0,eax
  3477. ret
  3478.  
  3479. is_cache_enabled:
  3480.        mov eax,cr0
  3481.        mov ebx,eax
  3482.        and eax,01100000000000000000000000000000b
  3483.        jz cache_disabled
  3484.        mov [esp+32],ebx
  3485. cache_disabled:
  3486.        mov dword [esp+32],eax ;0
  3487. ret
  3488.  
  3489. modify_pce:
  3490.        mov eax,cr4
  3491. ;       mov ebx,0
  3492. ;       or  bx,100000000b ;pce
  3493. ;       xor eax,ebx ;invert pce
  3494.        bts eax,8 ;pce=cr4[8]
  3495.        mov cr4,eax
  3496.        mov [esp+32],eax
  3497. ret
  3498. ;---------------------------------------------------------------------------------------------
  3499.  
  3500.  
  3501. ; check if pixel is allowed to be drawn
  3502.  
  3503. checkpixel:
  3504.         push eax edx
  3505.  
  3506.         mov  edx,[Screen_Max_X]     ; screen x size
  3507.         inc  edx
  3508.         imul edx, ebx
  3509.         add  eax, [_WinMapAddress]
  3510.         mov  dl, [eax+edx] ; lea eax, [...]
  3511.  
  3512.         xor  ecx, ecx
  3513.         mov  eax, [CURRENT_TASK]
  3514.         cmp  al, dl
  3515.         setne cl
  3516.  
  3517.         pop  edx eax
  3518.         ret
  3519.  
  3520. iglobal
  3521.   cpustring db 'CPU',0
  3522. endg
  3523.  
  3524. uglobal
  3525. background_defined    db    0    ; diamond, 11.04.2006
  3526. endg
  3527.  
  3528. align 4
  3529. ; check misc
  3530.  
  3531. checkmisc:
  3532.  
  3533.     cmp   [ctrl_alt_del], 1
  3534.     jne   nocpustart
  3535.  
  3536.         mov     ebp, cpustring
  3537.         call    fs_execute_from_sysdir
  3538.  
  3539.     mov   [ctrl_alt_del], 0
  3540.  
  3541. nocpustart:
  3542.     cmp   [mouse_active], 1
  3543.     jne   mouse_not_active
  3544.     mov   [mouse_active], 0
  3545.     xor   edi, edi
  3546.     mov   ecx,  [TASK_COUNT]
  3547. set_mouse_event:
  3548.     add   edi, 256
  3549.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3550.     loop  set_mouse_event
  3551.  
  3552. mouse_not_active:
  3553.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3554.     jz    nobackgr
  3555.     cmp    [background_defined], 0
  3556.     jz    nobackgr
  3557.     cmp   [REDRAW_BACKGROUND], byte 2
  3558.     jnz   no_set_bgr_event
  3559.     xor   edi, edi
  3560.     mov   ecx,  [TASK_COUNT]
  3561. set_bgr_event:
  3562.     add   edi, 256
  3563.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3564.     loop  set_bgr_event
  3565. no_set_bgr_event:
  3566. ;    mov   [draw_data+32 + RECT.left],dword 0
  3567. ;    mov   [draw_data+32 + RECT.top],dword 0
  3568. ;    mov   eax,[Screen_Max_X]
  3569. ;    mov   ebx,[Screen_Max_Y]
  3570. ;    mov   [draw_data+32 + RECT.right],eax
  3571. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3572.     call  drawbackground
  3573.     mov   [REDRAW_BACKGROUND],byte 0
  3574.     mov   [MOUSE_BACKGROUND],byte 0
  3575.  
  3576. nobackgr:
  3577.  
  3578.     ; system shutdown request
  3579.  
  3580.     cmp  [SYS_SHUTDOWN],byte 0
  3581.     je   noshutdown
  3582.  
  3583.     mov  edx,[shutdown_processes]
  3584.  
  3585.     cmp  [SYS_SHUTDOWN],dl
  3586.     jne  no_mark_system_shutdown
  3587.  
  3588.     lea   ecx,[edx-1]
  3589.     mov   edx,OS_BASE+0x3040
  3590.     jecxz @f
  3591. markz:
  3592.     mov   [edx+TASKDATA.state],byte 3
  3593.     add   edx,0x20
  3594.     loop  markz
  3595. @@:
  3596.  
  3597.   no_mark_system_shutdown:
  3598.  
  3599.     call [_display.disable_mouse]
  3600.  
  3601.     dec  byte [SYS_SHUTDOWN]
  3602.     je   system_shutdown
  3603.  
  3604. noshutdown:
  3605.  
  3606.  
  3607.     mov   eax,[TASK_COUNT]                  ; termination
  3608.     mov   ebx,TASK_DATA+TASKDATA.state
  3609.     mov   esi,1
  3610.  
  3611. newct:
  3612.     mov   cl,[ebx]
  3613.     cmp   cl,byte 3
  3614.     jz    terminate
  3615.     cmp   cl,byte 4
  3616.     jz    terminate
  3617.  
  3618.     add   ebx,0x20
  3619.     inc   esi
  3620.     dec   eax
  3621.     jnz   newct
  3622.     ret
  3623.  
  3624. ; redraw screen
  3625.  
  3626. redrawscreen:
  3627.  
  3628. ; eax , if process window_data base is eax, do not set flag/limits
  3629.  
  3630.          pushad
  3631.          push  eax
  3632.  
  3633. ;;;         mov   ebx,2
  3634. ;;;         call  delay_hs
  3635.  
  3636.          ;mov   ecx,0               ; redraw flags for apps
  3637.          xor   ecx,ecx
  3638.        newdw2:
  3639.  
  3640.          inc   ecx
  3641.          push  ecx
  3642.  
  3643.          mov   eax,ecx
  3644.          shl   eax,5
  3645.          add   eax,window_data
  3646.  
  3647.          cmp   eax,[esp+4]
  3648.          je    not_this_task
  3649.                                    ; check if window in redraw area
  3650.          mov   edi,eax
  3651.  
  3652.          cmp   ecx,1               ; limit for background
  3653.          jz    bgli
  3654.  
  3655.          mov   eax, [edi + WDATA.box.left]
  3656.          mov   ebx, [edi + WDATA.box.top]
  3657.          mov   ecx, [edi + WDATA.box.width]
  3658.          mov   edx, [edi + WDATA.box.height]
  3659.          add   ecx,eax
  3660.          add   edx,ebx
  3661.  
  3662.          mov   ecx,[draw_limits.bottom]   ; ecx = area y end     ebx = window y start
  3663.          cmp   ecx,ebx
  3664.          jb    ricino
  3665.  
  3666.          mov   ecx,[draw_limits.right]   ; ecx = area x end     eax = window x start
  3667.          cmp   ecx,eax
  3668.          jb    ricino
  3669.  
  3670.          mov   eax, [edi + WDATA.box.left]
  3671.          mov   ebx, [edi + WDATA.box.top]
  3672.          mov   ecx, [edi + WDATA.box.width]
  3673.          mov   edx, [edi + WDATA.box.height]
  3674.          add   ecx, eax
  3675.          add   edx, ebx
  3676.  
  3677.          mov   eax,[draw_limits.top]    ; eax = area y start     edx = window y end
  3678.          cmp   edx,eax
  3679.          jb    ricino
  3680.  
  3681.          mov   eax,[draw_limits.left]    ; eax = area x start     ecx = window x end
  3682.          cmp   ecx,eax
  3683.          jb    ricino
  3684.  
  3685.         bgli:
  3686.  
  3687.          cmp   ecx,1
  3688.          jnz   .az
  3689.          mov   al,[REDRAW_BACKGROUND]
  3690.          cmp   al,2
  3691.          jz    newdw8
  3692.          test  al,al
  3693.          jz    .az
  3694.          lea   eax,[edi+draw_data-window_data]
  3695.          mov   ebx,[draw_limits.left]
  3696.          cmp   ebx,[eax+RECT.left]
  3697.          jae   @f
  3698.          mov   [eax+RECT.left],ebx
  3699.         @@:
  3700.          mov   ebx,[draw_limits.top]
  3701.          cmp   ebx,[eax+RECT.top]
  3702.          jae   @f
  3703.          mov   [eax+RECT.top],ebx
  3704.         @@:
  3705.          mov   ebx,[draw_limits.right]
  3706.          cmp   ebx,[eax+RECT.right]
  3707.          jbe   @f
  3708.          mov   [eax+RECT.right],ebx
  3709.         @@:
  3710.          mov   ebx,[draw_limits.bottom]
  3711.          cmp   ebx,[eax+RECT.bottom]
  3712.          jbe   @f
  3713.          mov   [eax+RECT.bottom],ebx
  3714.         @@:
  3715.          jmp   newdw8
  3716.         .az:
  3717.  
  3718.          mov   eax,edi
  3719.          add   eax,draw_data-window_data
  3720.  
  3721.          mov   ebx,[draw_limits.left]          ; set limits
  3722.          mov   [eax + RECT.left], ebx
  3723.          mov   ebx,[draw_limits.top]
  3724.          mov   [eax + RECT.top], ebx
  3725.          mov   ebx,[draw_limits.right]
  3726.          mov   [eax + RECT.right], ebx
  3727.          mov   ebx,[draw_limits.bottom]
  3728.          mov   [eax + RECT.bottom], ebx
  3729.  
  3730.          sub   eax,draw_data-window_data
  3731.  
  3732.          cmp   dword [esp],1
  3733.          jne   nobgrd
  3734.          mov   byte [REDRAW_BACKGROUND], 1
  3735.  
  3736.        newdw8:
  3737.        nobgrd:
  3738.  
  3739.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3740.  
  3741.        ricino:
  3742.  
  3743.        not_this_task:
  3744.  
  3745.          pop   ecx
  3746.  
  3747.          cmp   ecx,[TASK_COUNT]
  3748.          jle   newdw2
  3749.  
  3750.          pop  eax
  3751.          popad
  3752.  
  3753.          ret
  3754.  
  3755. calculatebackground:   ; background
  3756.  
  3757.         mov   edi, [_WinMapAddress]                ; set os to use all pixels
  3758.         mov   eax,0x01010101
  3759.         mov   ecx, [_WinMapSize]
  3760.         shr   ecx, 2
  3761.         rep   stosd
  3762.  
  3763.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3764.  
  3765.         ret
  3766.  
  3767. uglobal
  3768.   imax    dd 0x0
  3769. endg
  3770.  
  3771.  
  3772.  
  3773. delay_ms:     ; delay in 1/1000 sec
  3774.  
  3775.  
  3776.         push  eax
  3777.         push  ecx
  3778.  
  3779.         mov   ecx,esi
  3780.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3781.         imul  ecx, 33941
  3782.         shr   ecx, 9
  3783.         ; </CPU clock fix>
  3784.  
  3785.         in    al,0x61
  3786.         and   al,0x10
  3787.         mov   ah,al
  3788.         cld
  3789.  
  3790.  cnt1:  in    al,0x61
  3791.         and   al,0x10
  3792.         cmp   al,ah
  3793.         jz    cnt1
  3794.  
  3795.         mov   ah,al
  3796.         loop  cnt1
  3797.  
  3798.         pop   ecx
  3799.         pop   eax
  3800.  
  3801.         ret
  3802.  
  3803.  
  3804. set_app_param:
  3805.         mov     edi, [TASK_BASE]
  3806.         mov     eax, [edi + TASKDATA.event_mask]
  3807.         mov     [edi + TASKDATA.event_mask], ebx
  3808.         mov     [esp+32], eax
  3809.         ret
  3810.  
  3811.  
  3812.  
  3813. delay_hs:     ; delay in 1/100 secs
  3814. ; ebx = delay time
  3815.         push  ecx
  3816.         push  edx
  3817.  
  3818.         mov   edx,[timer_ticks]
  3819.  
  3820.       newtic:
  3821.         mov   ecx,[timer_ticks]
  3822.         sub   ecx,edx
  3823.         cmp   ecx,ebx
  3824.         jae   zerodelay
  3825.  
  3826.         call  change_task
  3827.  
  3828.         jmp   newtic
  3829.  
  3830.       zerodelay:
  3831.         pop   edx
  3832.         pop   ecx
  3833.  
  3834.         ret
  3835.  
  3836. align 16        ;very often call this subrutine
  3837. memmove:       ; memory move in bytes
  3838.  
  3839. ; eax = from
  3840. ; ebx = to
  3841. ; ecx = no of bytes
  3842.     test ecx, ecx
  3843.     jle  .ret
  3844.  
  3845.     push esi edi ecx
  3846.  
  3847.     mov  edi, ebx
  3848.     mov  esi, eax
  3849.  
  3850.     test ecx, not 11b
  3851.     jz   @f
  3852.  
  3853.     push ecx
  3854.     shr  ecx, 2
  3855.     rep  movsd
  3856.     pop  ecx
  3857.     and  ecx, 11b
  3858.     jz   .finish
  3859.   @@:
  3860.     rep  movsb
  3861.  
  3862.   .finish:
  3863.     pop  ecx edi esi
  3864.   .ret:
  3865.     ret
  3866.  
  3867.  
  3868. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3869. ;align 4
  3870. ;
  3871. ;read_floppy_file:
  3872. ;
  3873. ;; as input
  3874. ;;
  3875. ;; eax pointer to file
  3876. ;; ebx file lenght
  3877. ;; ecx start 512 byte block number
  3878. ;; edx number of blocks to read
  3879. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3880. ;;
  3881. ;;
  3882. ;; on return
  3883. ;;
  3884. ;; eax = 0 command succesful
  3885. ;;       1 no fd base and/or partition defined
  3886. ;;       2 yet unsupported FS
  3887. ;;       3 unknown FS
  3888. ;;       4 partition not defined at hd
  3889. ;;       5 file not found
  3890. ;; ebx = size of file
  3891. ;
  3892. ;     mov   edi,[TASK_BASE]
  3893. ;     add   edi,0x10
  3894. ;     add   esi,[edi]
  3895. ;     add   eax,[edi]
  3896. ;
  3897. ;     pushad
  3898. ;     mov  edi,esi
  3899. ;     add  edi,1024
  3900. ;     mov  esi,0x100000+19*512
  3901. ;     sub  ecx,1
  3902. ;     shl  ecx,9
  3903. ;     add  esi,ecx
  3904. ;     shl  edx,9
  3905. ;     mov  ecx,edx
  3906. ;     cld
  3907. ;     rep  movsb
  3908. ;     popad
  3909. ;
  3910. ;     mov   [esp+36],eax
  3911. ;     mov   [esp+24],ebx
  3912. ;     ret
  3913.  
  3914.  
  3915.  
  3916. align 4
  3917.  
  3918. sys_programirq:
  3919.  
  3920.     mov   eax, [TASK_BASE]
  3921.     add   ebx, [eax + TASKDATA.mem_start]
  3922.  
  3923.     cmp   ecx, 16
  3924.     jae   .not_owner
  3925.     mov   edi, [eax + TASKDATA.pid]
  3926.     cmp   edi, [irq_owner + 4 * ecx]
  3927.     je    .spril1
  3928. .not_owner:
  3929.     xor   ecx, ecx
  3930.     inc   ecx
  3931.     jmp   .end
  3932.   .spril1:
  3933.  
  3934.     shl   ecx, 6
  3935.     mov   esi, ebx
  3936.     lea   edi, [irq00read + ecx]
  3937.     push  16
  3938.     pop   ecx
  3939.  
  3940.     cld
  3941.     rep   movsd
  3942.   .end:
  3943.     mov   [esp+32], ecx
  3944.     ret
  3945.  
  3946.  
  3947. align 4
  3948.  
  3949. get_irq_data:
  3950.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3951.      xor   bh, bh
  3952.      cmp   ebx, 16
  3953.      jae   .not_owner
  3954.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3955.  
  3956.      mov   eax,[TASK_BASE]
  3957.  
  3958.      cmp   edx,[eax+TASKDATA.pid]
  3959.      je    gidril1
  3960. .not_owner:
  3961.      xor   edx, edx
  3962.      dec   edx
  3963.      jmp   gid1
  3964.  
  3965.   gidril1:
  3966.  
  3967.      shl   ebx, 12
  3968.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3969.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3970.      dec   esi
  3971.      jz    gid1
  3972.      test  edx, edx                      ; check if buffer is empty
  3973.      jz    gid1
  3974.  
  3975.      mov   ebx, [eax + 0x4]
  3976.      mov   edi, ecx
  3977.  
  3978.      mov   ecx, 4000                     ; buffer size, used frequently
  3979.  
  3980.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3981.      jb    @f
  3982.  
  3983.      xor   ebx, ebx
  3984.  
  3985.    @@:
  3986.  
  3987.      lea   esi, [ebx + edx]              ; calculate data size and offset
  3988.      cld
  3989.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  3990.      jbe   @f
  3991.  
  3992.      sub   ecx, ebx
  3993.      sub   edx, ecx
  3994.  
  3995.      lea   esi, [eax + ebx + 0x10]
  3996.      rep   movsb
  3997.  
  3998.      xor   ebx, ebx
  3999.    @@:
  4000.      lea   esi, [eax + ebx + 0x10]
  4001.      mov   ecx, edx
  4002.      add   ebx, edx
  4003.  
  4004.      rep   movsb
  4005.      mov   edx, [eax]
  4006.      mov   [eax], ecx                    ; set data size to zero
  4007.      mov   [eax + 0x4], ebx              ; set data offset
  4008.  
  4009.    gid1:
  4010.      mov   [esp+32], edx                 ; eax
  4011.      ret
  4012.  
  4013.  
  4014. set_io_access_rights:
  4015.       push edi eax
  4016.       mov edi, tss._io_map_0
  4017. ;     mov   ecx,eax
  4018. ;     and   ecx,7    ; offset in byte
  4019. ;     shr   eax,3    ; number of byte
  4020. ;     add   edi,eax
  4021. ;     mov   ebx,1
  4022. ;     shl   ebx,cl
  4023.      test  ebp,ebp
  4024. ;     cmp   ebp,0                ; enable access - ebp = 0
  4025.      jnz   siar1
  4026. ;     not   ebx
  4027. ;     and   [edi],byte bl
  4028.      btr [edi], eax
  4029.      pop eax edi
  4030.      ret
  4031. siar1:
  4032.      bts [edi], eax
  4033.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4034.      pop eax edi
  4035.      ret
  4036. ;reserve/free group of ports
  4037. ;  * eax = 46 - number function
  4038. ;  * ebx = 0 - reserve, 1 - free
  4039. ;  * ecx = number start arrea of ports
  4040. ;  * edx = number end arrea of ports (include last number of port)
  4041. ;Return value:
  4042. ;  * eax = 0 - succesful
  4043. ;  * eax = 1 - error
  4044. ;  * The system has reserve this ports:
  4045. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4046. ;destroys eax,ebx, ebp
  4047. r_f_port_area:
  4048.  
  4049.      test  ebx, ebx
  4050.      jnz   free_port_area
  4051. ;     je    r_port_area
  4052. ;     jmp   free_port_area
  4053.  
  4054. ;   r_port_area:
  4055.  
  4056. ;     pushad
  4057.  
  4058.      cmp   ecx,edx            ; beginning > end ?
  4059.      ja    rpal1
  4060.      cmp   edx,65536
  4061.      jae   rpal1
  4062.      mov   eax,[RESERVED_PORTS]
  4063.      test  eax,eax            ; no reserved areas ?
  4064.      je    rpal2
  4065.      cmp   eax,255            ; max reserved
  4066.      jae   rpal1
  4067.  rpal3:
  4068.      mov   ebx,eax
  4069.      shl   ebx,4
  4070.      add   ebx,RESERVED_PORTS
  4071.      cmp   ecx,[ebx+8]
  4072.      ja    rpal4
  4073.      cmp   edx,[ebx+4]
  4074.      jae   rpal1
  4075. ;     jb    rpal4
  4076. ;     jmp   rpal1
  4077.  rpal4:
  4078.      dec   eax
  4079.      jnz   rpal3
  4080.      jmp   rpal2
  4081.    rpal1:
  4082. ;     popad
  4083. ;     mov   eax,1
  4084.      xor    eax,eax
  4085.      inc    eax
  4086.      ret
  4087.    rpal2:
  4088. ;     popad
  4089.      ; enable port access at port IO map
  4090.         cli
  4091.         pushad                        ; start enable io map
  4092.  
  4093.         cmp   edx,65536 ;16384
  4094.         jae   no_unmask_io ; jge
  4095.         mov   eax,ecx
  4096. ;       push    ebp
  4097.         xor     ebp,ebp                ; enable - eax = port
  4098. new_port_access:
  4099. ;     pushad
  4100.         call    set_io_access_rights
  4101. ;     popad
  4102.      inc   eax
  4103.      cmp   eax,edx
  4104.      jbe   new_port_access
  4105. ;       pop     ebp
  4106. no_unmask_io:
  4107.         popad                         ; end enable io map
  4108.      sti
  4109.  
  4110.      mov   eax,[RESERVED_PORTS]
  4111.      add   eax,1
  4112.      mov   [RESERVED_PORTS],eax
  4113.      shl   eax,4
  4114.      add   eax,RESERVED_PORTS
  4115.      mov   ebx,[TASK_BASE]
  4116.      mov   ebx,[ebx+TASKDATA.pid]
  4117.      mov   [eax],ebx
  4118.      mov   [eax+4],ecx
  4119.      mov   [eax+8],edx
  4120.  
  4121.      xor   eax, eax
  4122.      ret
  4123.  
  4124. free_port_area:
  4125.  
  4126. ;     pushad
  4127.      mov   eax,[RESERVED_PORTS]     ; no reserved areas ?
  4128.      test  eax,eax
  4129.      jz    frpal2
  4130.      mov   ebx,[TASK_BASE]
  4131.      mov   ebx,[ebx+TASKDATA.pid]
  4132.    frpal3:
  4133.      mov   edi,eax
  4134.      shl   edi,4
  4135.      add   edi,RESERVED_PORTS
  4136.      cmp   ebx,[edi]
  4137.      jne   frpal4
  4138.      cmp   ecx,[edi+4]
  4139.      jne   frpal4
  4140.      cmp   edx,[edi+8]
  4141.      jne   frpal4
  4142.      jmp   frpal1
  4143.    frpal4:
  4144.      dec   eax
  4145.      jnz   frpal3
  4146.    frpal2:
  4147. ;     popad
  4148.      inc   eax
  4149.      ret
  4150.    frpal1:
  4151.         push    ecx
  4152.         mov   ecx,256
  4153.         sub   ecx,eax
  4154.         shl   ecx,4
  4155.         mov   esi,edi
  4156.         add   esi,16
  4157.         cld
  4158.         rep   movsb
  4159.  
  4160.         dec   dword [RESERVED_PORTS]
  4161. ;popad
  4162. ;disable port access at port IO map
  4163.  
  4164. ;     pushad                        ; start disable io map
  4165.      pop        eax     ;start port
  4166.      cmp   edx,65536 ;16384
  4167.      jge   no_mask_io
  4168.  
  4169. ;     mov   eax,ecx
  4170.         xor     ebp,ebp
  4171.         inc     ebp
  4172. new_port_access_disable:
  4173. ;     pushad
  4174. ;     mov   ebp,1                  ; disable - eax = port
  4175.      call  set_io_access_rights
  4176. ;     popad
  4177.      inc   eax
  4178.      cmp   eax,edx
  4179.      jbe   new_port_access_disable
  4180. no_mask_io:
  4181. ;     popad                         ; end disable io map
  4182.      xor   eax, eax
  4183.      ret
  4184.  
  4185.  
  4186. reserve_free_irq:
  4187.  
  4188.      xor   esi, esi
  4189.      inc   esi
  4190.      cmp   ecx, 16
  4191.      jae   ril1
  4192.  
  4193.      push  ecx
  4194.      lea   ecx, [irq_owner + 4 * ecx]
  4195.      mov   edx, [ecx]
  4196.      mov   eax, [TASK_BASE]
  4197.      mov   edi, [eax + TASKDATA.pid]
  4198.      pop   eax
  4199.      dec   ebx
  4200.      jnz   reserve_irq
  4201.  
  4202.      cmp   edx, edi
  4203.      jne   ril1
  4204.      dec   esi
  4205.      mov   [ecx], esi
  4206.  
  4207.      jmp   ril1
  4208.  
  4209.   reserve_irq:
  4210.  
  4211.      cmp   dword [ecx], 0
  4212.      jne   ril1
  4213.  
  4214.      mov   ebx, [f_irqs + 4 * eax]
  4215.  
  4216.      stdcall attach_int_handler, eax, ebx, dword 0
  4217.  
  4218.      mov   [ecx], edi
  4219.  
  4220.      dec   esi
  4221.    ril1:
  4222.      mov   [esp+32], esi ; return in eax
  4223.      ret
  4224.  
  4225. iglobal
  4226. f_irqs:
  4227.      dd 0x0
  4228.      dd 0x0
  4229.      dd p_irq2
  4230.      dd p_irq3
  4231.      dd p_irq4
  4232.      dd p_irq5
  4233.      dd p_irq6
  4234.      dd p_irq7
  4235.      dd p_irq8
  4236.      dd p_irq9
  4237.      dd p_irq10
  4238.      dd p_irq11
  4239.      dd 0x0
  4240.      dd 0x0
  4241.      dd p_irq14
  4242.      dd p_irq15
  4243.  
  4244. endg
  4245.  
  4246. drawbackground:
  4247.        inc   [mouse_pause]
  4248.        cmp   [SCR_MODE],word 0x12
  4249.        je   dbrv20
  4250.      dbrv12:
  4251.        cmp  [SCR_MODE],word 0100000000000000b
  4252.        jge  dbrv20
  4253.        cmp  [SCR_MODE],word 0x13
  4254.        je   dbrv20
  4255.        call  vesa12_drawbackground
  4256.        dec   [mouse_pause]
  4257.        call   [draw_pointer]
  4258.        ret
  4259.      dbrv20:
  4260.        cmp   [BgrDrawMode],dword 1
  4261.        jne   bgrstr
  4262.        call  vesa20_drawbackground_tiled
  4263.        dec   [mouse_pause]
  4264.        call   [draw_pointer]
  4265.        ret
  4266.      bgrstr:
  4267.        call  vesa20_drawbackground_stretch
  4268.        dec   [mouse_pause]
  4269.        call   [draw_pointer]
  4270.        ret
  4271.  
  4272. align 4
  4273.  
  4274. syscall_putimage:                       ; PutImage
  4275. sys_putimage:
  4276.      test  ecx,0x80008000
  4277.      jnz   .exit
  4278.      test  ecx,0x0000FFFF
  4279.      jz    .exit
  4280.      test  ecx,0xFFFF0000
  4281.      jnz   @f
  4282.   .exit:
  4283.      ret
  4284.  @@:
  4285.         mov     edi,[current_slot]
  4286.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4287.         rol     edx,16
  4288.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4289.         rol     edx,16
  4290.   .forced:
  4291.         push    ebp esi 0
  4292.         mov     ebp, putimage_get24bpp
  4293.         mov     esi, putimage_init24bpp
  4294. sys_putimage_bpp:
  4295. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4296. ;        mov     eax, vga_putimage
  4297.         cmp     [SCR_MODE], word 0x12
  4298.         jz      @f   ;.doit
  4299.         mov     eax, vesa12_putimage
  4300.         cmp     [SCR_MODE], word 0100000000000000b
  4301.         jae     @f
  4302.         cmp     [SCR_MODE], word 0x13
  4303.         jnz     .doit
  4304. @@:
  4305.         mov     eax, vesa20_putimage
  4306. .doit:
  4307.         inc     [mouse_pause]
  4308.         call    eax
  4309.         dec     [mouse_pause]
  4310.         pop     ebp esi ebp
  4311.         jmp     [draw_pointer]
  4312.  
  4313. syscall_putimage_palette:
  4314.         mov     edi, esi
  4315.         mov     esi, edx
  4316.         mov     edx, ecx
  4317.         mov     ecx, ebx
  4318.         mov     ebx, eax
  4319. sys_putimage_palette:
  4320. ; ebx = pointer to image
  4321. ; ecx = [xsize]*65536 + [ysize]
  4322. ; edx = [xstart]*65536 + [ystart]
  4323. ; esi = number of bits per pixel, must be 8, 24 or 32
  4324. ; edi = pointer to palette
  4325. ; ebp = row delta
  4326.         mov     eax, [CURRENT_TASK]
  4327.         shl     eax, 8
  4328.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4329.         rol     edx, 16
  4330.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4331.         rol     edx, 16
  4332. .forced:
  4333.         cmp     esi, 1
  4334.         jnz     @f
  4335.         push    edi
  4336.         mov     eax, [edi+4]
  4337.         sub     eax, [edi]
  4338.         push    eax
  4339.         push    dword [edi]
  4340.         push    0ffffff80h
  4341.         mov     edi, esp
  4342.         call    put_mono_image
  4343.         add     esp, 12
  4344.         pop     edi
  4345.         ret
  4346. @@:
  4347.         cmp     esi, 2
  4348.         jnz     @f
  4349.         push    edi
  4350.         push    0ffffff80h
  4351.         mov     edi, esp
  4352.         call    put_2bit_image
  4353.         pop     eax
  4354.         pop     edi
  4355.         ret
  4356. @@:
  4357.         cmp     esi, 4
  4358.         jnz     @f
  4359.         push    edi
  4360.         push    0ffffff80h
  4361.         mov     edi, esp
  4362.         call    put_4bit_image
  4363.         pop     eax
  4364.         pop     edi
  4365.         ret
  4366. @@:
  4367.         push    ebp esi ebp
  4368.         cmp     esi, 8
  4369.         jnz     @f
  4370.         mov     ebp, putimage_get8bpp
  4371.         mov     esi, putimage_init8bpp
  4372.         jmp     sys_putimage_bpp
  4373. @@:
  4374.         cmp     esi, 15
  4375.         jnz     @f
  4376.         mov     ebp, putimage_get15bpp
  4377.         mov     esi, putimage_init15bpp
  4378.         jmp     sys_putimage_bpp
  4379. @@:
  4380.         cmp     esi, 16
  4381.         jnz     @f
  4382.         mov     ebp, putimage_get16bpp
  4383.         mov     esi, putimage_init16bpp
  4384.         jmp     sys_putimage_bpp
  4385. @@:
  4386.         cmp     esi, 24
  4387.         jnz     @f
  4388.         mov     ebp, putimage_get24bpp
  4389.         mov     esi, putimage_init24bpp
  4390.         jmp     sys_putimage_bpp
  4391. @@:
  4392.         cmp     esi, 32
  4393.         jnz     @f
  4394.         mov     ebp, putimage_get32bpp
  4395.         mov     esi, putimage_init32bpp
  4396.         jmp     sys_putimage_bpp
  4397. @@:
  4398.         pop     ebp esi ebp
  4399.         ret
  4400.  
  4401. put_mono_image:
  4402.         push    ebp esi ebp
  4403.         mov     ebp, putimage_get1bpp
  4404.         mov     esi, putimage_init1bpp
  4405.         jmp     sys_putimage_bpp
  4406. put_2bit_image:
  4407.         push    ebp esi ebp
  4408.         mov     ebp, putimage_get2bpp
  4409.         mov     esi, putimage_init2bpp
  4410.         jmp     sys_putimage_bpp
  4411. put_4bit_image:
  4412.         push    ebp esi ebp
  4413.         mov     ebp, putimage_get4bpp
  4414.         mov     esi, putimage_init4bpp
  4415.         jmp     sys_putimage_bpp
  4416.  
  4417. putimage_init24bpp:
  4418.         lea     eax, [eax*3]
  4419. putimage_init8bpp:
  4420.         ret
  4421.  
  4422. align 16
  4423. putimage_get24bpp:
  4424.         movzx   eax, byte [esi+2]
  4425.         shl     eax, 16
  4426.         mov     ax, [esi]
  4427.         add     esi, 3
  4428.         ret     4
  4429. align 16
  4430. putimage_get8bpp:
  4431.         movzx   eax, byte [esi]
  4432.         push    edx
  4433.         mov     edx, [esp+8]
  4434.         mov     eax, [edx+eax*4]
  4435.         pop     edx
  4436.         inc     esi
  4437.         ret     4
  4438.  
  4439. putimage_init1bpp:
  4440.         add     eax, ecx
  4441.         push    ecx
  4442.         add     eax, 7
  4443.         add     ecx, 7
  4444.         shr     eax, 3
  4445.         shr     ecx, 3
  4446.         sub     eax, ecx
  4447.         pop     ecx
  4448.         ret
  4449. align 16
  4450. putimage_get1bpp:
  4451.         push    edx
  4452.         mov     edx, [esp+8]
  4453.         mov     al, [edx]
  4454.         add     al, al
  4455.         jnz     @f
  4456.         lodsb
  4457.         adc     al, al
  4458. @@:
  4459.         mov     [edx], al
  4460.         sbb     eax, eax
  4461.         and     eax, [edx+8]
  4462.         add     eax, [edx+4]
  4463.         pop     edx
  4464.         ret     4
  4465.  
  4466. putimage_init2bpp:
  4467.         add     eax, ecx
  4468.         push    ecx
  4469.         add     ecx, 3
  4470.         add     eax, 3
  4471.         shr     ecx, 2
  4472.         shr     eax, 2
  4473.         sub     eax, ecx
  4474.         pop     ecx
  4475.         ret
  4476. align 16
  4477. putimage_get2bpp:
  4478.         push    edx
  4479.         mov     edx, [esp+8]
  4480.         mov     al, [edx]
  4481.         mov     ah, al
  4482.         shr     al, 6
  4483.         shl     ah, 2
  4484.         jnz     .nonewbyte
  4485.         lodsb
  4486.         mov     ah, al
  4487.         shr     al, 6
  4488.         shl     ah, 2
  4489.         add     ah, 1
  4490. .nonewbyte:
  4491.         mov     [edx], ah
  4492.         mov     edx, [edx+4]
  4493.         movzx   eax, al
  4494.         mov     eax, [edx+eax*4]
  4495.         pop     edx
  4496.         ret     4
  4497.  
  4498. putimage_init4bpp:
  4499.         add     eax, ecx
  4500.         push    ecx
  4501.         add     ecx, 1
  4502.         add     eax, 1
  4503.         shr     ecx, 1
  4504.         shr     eax, 1
  4505.         sub     eax, ecx
  4506.         pop     ecx
  4507.         ret
  4508. align 16
  4509. putimage_get4bpp:
  4510.         push    edx
  4511.         mov     edx, [esp+8]
  4512.         add     byte [edx], 80h
  4513.         jc      @f
  4514.         movzx   eax, byte [edx+1]
  4515.         mov     edx, [edx+4]
  4516.         and     eax, 0x0F
  4517.         mov     eax, [edx+eax*4]
  4518.         pop     edx
  4519.         ret     4
  4520. @@:
  4521.         movzx   eax, byte [esi]
  4522.         add     esi, 1
  4523.         mov     [edx+1], al
  4524.         shr     eax, 4
  4525.         mov     edx, [edx+4]
  4526.         mov     eax, [edx+eax*4]
  4527.         pop     edx
  4528.         ret     4
  4529.  
  4530. putimage_init32bpp:
  4531.         shl     eax, 2
  4532.         ret
  4533. align 16
  4534. putimage_get32bpp:
  4535.         lodsd
  4536.         ret     4
  4537.  
  4538. putimage_init15bpp:
  4539. putimage_init16bpp:
  4540.         add     eax, eax
  4541.         ret
  4542. align 16
  4543. putimage_get15bpp:
  4544. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4545.         push    ecx edx
  4546.         movzx   eax, word [esi]
  4547.         add     esi, 2
  4548.         mov     ecx, eax
  4549.         mov     edx, eax
  4550.         and     eax, 0x1F
  4551.         and     ecx, 0x1F shl 5
  4552.         and     edx, 0x1F shl 10
  4553.         shl     eax, 3
  4554.         shl     ecx, 6
  4555.         shl     edx, 9
  4556.         or      eax, ecx
  4557.         or      eax, edx
  4558.         pop     edx ecx
  4559.         ret     4
  4560.  
  4561. align 16
  4562. putimage_get16bpp:
  4563. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4564.         push    ecx edx
  4565.         movzx   eax, word [esi]
  4566.         add     esi, 2
  4567.         mov     ecx, eax
  4568.         mov     edx, eax
  4569.         and     eax, 0x1F
  4570.         and     ecx, 0x3F shl 5
  4571.         and     edx, 0x1F shl 11
  4572.         shl     eax, 3
  4573.         shl     ecx, 5
  4574.         shl     edx, 8
  4575.         or      eax, ecx
  4576.         or      eax, edx
  4577.         pop     edx ecx
  4578.         ret     4
  4579.  
  4580. ; eax x beginning
  4581. ; ebx y beginning
  4582. ; ecx x end
  4583.         ; edx y end
  4584. ; edi color
  4585.  
  4586. __sys_drawbar:
  4587.         mov     esi,[current_slot]
  4588.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4589.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4590.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4591.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4592.   .forced:
  4593.     inc   [mouse_pause]
  4594. ;        call    [disable_mouse]
  4595.     cmp   [SCR_MODE],word 0x12
  4596.     je   dbv20
  4597.    sdbv20:
  4598.     cmp  [SCR_MODE],word 0100000000000000b
  4599.     jge  dbv20
  4600.     cmp  [SCR_MODE],word 0x13
  4601.     je   dbv20
  4602.     call vesa12_drawbar
  4603.     dec   [mouse_pause]
  4604.     call   [draw_pointer]
  4605.     ret
  4606.   dbv20:
  4607.     call vesa20_drawbar
  4608.     dec   [mouse_pause]
  4609.     call   [draw_pointer]
  4610.     ret
  4611.  
  4612.  
  4613.  
  4614. kb_read:
  4615.  
  4616.         push    ecx edx
  4617.  
  4618.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4619.       kr_loop:
  4620.         in      al,0x64
  4621.         test    al,1
  4622.         jnz     kr_ready
  4623.         loop    kr_loop
  4624.         mov     ah,1
  4625.         jmp     kr_exit
  4626.       kr_ready:
  4627.         push    ecx
  4628.         mov     ecx,32
  4629.       kr_delay:
  4630.         loop    kr_delay
  4631.         pop     ecx
  4632.         in      al,0x60
  4633.         xor     ah,ah
  4634.       kr_exit:
  4635.  
  4636.         pop     edx ecx
  4637.  
  4638.         ret
  4639.  
  4640.  
  4641. kb_write:
  4642.  
  4643.         push    ecx edx
  4644.  
  4645.         mov     dl,al
  4646. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4647. ;      kw_loop1:
  4648. ;        in      al,0x64
  4649. ;        test    al,0x20
  4650. ;        jz      kw_ok1
  4651. ;        loop    kw_loop1
  4652. ;        mov     ah,1
  4653. ;        jmp     kw_exit
  4654. ;      kw_ok1:
  4655.         in      al,0x60
  4656.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4657.       kw_loop:
  4658.         in      al,0x64
  4659.         test    al,2
  4660.         jz      kw_ok
  4661.         loop    kw_loop
  4662.         mov     ah,1
  4663.         jmp     kw_exit
  4664.       kw_ok:
  4665.         mov     al,dl
  4666.         out     0x60,al
  4667.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4668.       kw_loop3:
  4669.         in      al,0x64
  4670.         test    al,2
  4671.         jz      kw_ok3
  4672.         loop    kw_loop3
  4673.         mov     ah,1
  4674.         jmp     kw_exit
  4675.       kw_ok3:
  4676.         mov     ah,8
  4677.       kw_loop4:
  4678.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4679.       kw_loop5:
  4680.         in      al,0x64
  4681.         test    al,1
  4682.         jnz     kw_ok4
  4683.         loop    kw_loop5
  4684.         dec     ah
  4685.         jnz     kw_loop4
  4686.       kw_ok4:
  4687.         xor     ah,ah
  4688.       kw_exit:
  4689.  
  4690.         pop     edx ecx
  4691.  
  4692.         ret
  4693.  
  4694.  
  4695. kb_cmd:
  4696.  
  4697.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4698.       c_wait:
  4699.         in      al,0x64
  4700.         test    al,2
  4701.         jz      c_send
  4702.         loop    c_wait
  4703.         jmp     c_error
  4704.       c_send:
  4705.         mov     al,bl
  4706.         out     0x64,al
  4707.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4708.       c_accept:
  4709.         in      al,0x64
  4710.         test    al,2
  4711.         jz      c_ok
  4712.         loop    c_accept
  4713.       c_error:
  4714.         mov     ah,1
  4715.         jmp     c_exit
  4716.       c_ok:
  4717.         xor     ah,ah
  4718.       c_exit:
  4719.         ret
  4720.  
  4721.  
  4722. setmouse:  ; set mousepicture -pointer
  4723.            ; ps2 mouse enable
  4724.  
  4725.      mov     [MOUSE_PICTURE],dword mousepointer
  4726.  
  4727.      cli
  4728.  
  4729.      ret
  4730.  
  4731. if used _rdtsc
  4732. _rdtsc:
  4733.      bt [cpu_caps], CAPS_TSC
  4734.      jnc ret_rdtsc
  4735.      rdtsc
  4736.      ret
  4737.    ret_rdtsc:
  4738.      mov   edx,0xffffffff
  4739.      mov   eax,0xffffffff
  4740.      ret
  4741. end if
  4742.  
  4743. rerouteirqs:
  4744.  
  4745.         cli
  4746.  
  4747.         mov     al,0x11         ;  icw4, edge triggered
  4748.         out     0x20,al
  4749.         call    pic_delay
  4750.         out     0xA0,al
  4751.         call    pic_delay
  4752.  
  4753.         mov     al,0x20         ;  generate 0x20 +
  4754.         out     0x21,al
  4755.         call    pic_delay
  4756.         mov     al,0x28         ;  generate 0x28 +
  4757.         out     0xA1,al
  4758.         call    pic_delay
  4759.  
  4760.         mov     al,0x04         ;  slave at irq2
  4761.         out     0x21,al
  4762.         call    pic_delay
  4763.         mov     al,0x02         ;  at irq9
  4764.         out     0xA1,al
  4765.         call    pic_delay
  4766.  
  4767.         mov     al,0x01         ;  8086 mode
  4768.         out     0x21,al
  4769.         call    pic_delay
  4770.         out     0xA1,al
  4771.         call    pic_delay
  4772.  
  4773.         mov     al,255          ; mask all irq's
  4774.         out     0xA1,al
  4775.         call    pic_delay
  4776.         out     0x21,al
  4777.         call    pic_delay
  4778.  
  4779.         mov     ecx,0x1000
  4780.         cld
  4781. picl1:  call    pic_delay
  4782.         loop    picl1
  4783.  
  4784.         mov     al,255          ; mask all irq's
  4785.         out     0xA1,al
  4786.         call    pic_delay
  4787.         out     0x21,al
  4788.         call    pic_delay
  4789.  
  4790.         cli
  4791.  
  4792.         ret
  4793.  
  4794.  
  4795. pic_delay:
  4796.  
  4797.         jmp     pdl1
  4798. pdl1:   ret
  4799.  
  4800.  
  4801. sys_msg_board_str:
  4802.  
  4803.      pushad
  4804.    @@:
  4805.      cmp    [esi],byte 0
  4806.      je     @f
  4807.      mov    eax,1
  4808.      movzx  ebx,byte [esi]
  4809.      call   sys_msg_board
  4810.      inc    esi
  4811.      jmp    @b
  4812.    @@:
  4813.      popad
  4814.      ret
  4815.  
  4816. sys_msg_board_byte:
  4817. ; in: al = byte to display
  4818. ; out: nothing
  4819. ; destroys: nothing
  4820.         pushad
  4821.         mov     ecx, 2
  4822.         shl     eax, 24
  4823.         jmp     @f
  4824.  
  4825. sys_msg_board_word:
  4826. ; in: ax = word to display
  4827. ; out: nothing
  4828. ; destroys: nothing
  4829.         pushad
  4830.         mov     ecx, 4
  4831.         shl     eax, 16
  4832.         jmp     @f
  4833.  
  4834. sys_msg_board_dword:
  4835. ; in: eax = dword to display
  4836. ; out: nothing
  4837. ; destroys: nothing
  4838.         pushad
  4839.         mov     ecx, 8
  4840. @@:
  4841.         push    ecx
  4842.         rol     eax, 4
  4843.         push    eax
  4844.         and     al, 0xF
  4845.         cmp     al, 10
  4846.         sbb     al, 69h
  4847.         das
  4848.         mov     bl, al
  4849.         xor     eax, eax
  4850.         inc     eax
  4851.         call    sys_msg_board
  4852.         pop     eax
  4853.         pop     ecx
  4854.         loop    @b
  4855.         popad
  4856.         ret
  4857.  
  4858. uglobal
  4859.   msg_board_data: times 4096 db 0
  4860.   msg_board_count dd 0x0
  4861. endg
  4862.  
  4863. sys_msg_board:
  4864.  
  4865. ; eax=1 : write :  bl byte to write
  4866. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4867.  
  4868.         mov     ecx, [msg_board_count]
  4869.         cmp     eax, 1
  4870.         jne     .smbl1
  4871.  
  4872. if defined debug_com_base
  4873.  
  4874.         push    dx ax
  4875.  
  4876.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4877.         mov     dx, debug_com_base+5
  4878.         in      al, dx
  4879.         test    al, 1 shl 5
  4880.         jz      @r
  4881.  
  4882.         mov     dx, debug_com_base      ; Output the byte
  4883.         mov     al, bl
  4884.         out     dx, al
  4885.  
  4886.         pop     ax dx
  4887.  
  4888. end if
  4889.  
  4890.         mov     [msg_board_data+ecx],bl
  4891.         inc     ecx
  4892.         and     ecx, 4095
  4893.         mov     [msg_board_count], ecx
  4894.         mov     [check_idle_semaphore], 5
  4895.         ret
  4896. .smbl1:
  4897.         cmp     eax, 2
  4898.         jne     .smbl2
  4899.         test    ecx, ecx
  4900.         jz      .smbl21
  4901.         mov     eax, msg_board_data+1
  4902.         mov     ebx, msg_board_data
  4903.         movzx   edx, byte [ebx]
  4904.         call    memmove
  4905.         dec     [msg_board_count]
  4906.         mov     [esp + 36], edx ;eax
  4907.         mov     [esp + 24], dword 1
  4908.         ret
  4909. .smbl21:
  4910.         mov     [esp+36], ecx
  4911.         mov     [esp+24], ecx
  4912. .smbl2:
  4913.         ret
  4914.  
  4915.  
  4916.  
  4917. sys_process_def:
  4918.         mov     edi, [CURRENT_TASK]
  4919.  
  4920.         dec     eax             ; 1 = set keyboard mode
  4921.      jne   no_set_keyboard_setup
  4922.  
  4923.      shl   edi,8
  4924.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4925.  
  4926.      ret
  4927.  
  4928.    no_set_keyboard_setup:
  4929.  
  4930.         dec     eax             ; 2 = get keyboard mode
  4931.      jne   no_get_keyboard_setup
  4932.  
  4933.      shl   edi,8
  4934.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4935.  
  4936.      mov   [esp+36],eax
  4937.  
  4938.      ret
  4939.  
  4940.    no_get_keyboard_setup:
  4941.  
  4942.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4943.      jne   no_get_keyboard_cas
  4944.  
  4945. ;     xor   eax,eax
  4946. ;     movzx eax,byte [shift]
  4947. ;     movzx ebx,byte [ctrl]
  4948. ;     shl   ebx,2
  4949. ;     add   eax,ebx
  4950. ;     movzx ebx,byte [alt]
  4951. ;     shl   ebx,3
  4952. ;     add   eax,ebx
  4953.  
  4954.  ;// mike.dld [
  4955.      mov   eax, [kb_state]
  4956.  ;// mike.dld ]
  4957.  
  4958.      mov   [esp+36],eax
  4959.  
  4960.      ret
  4961.  
  4962.    no_get_keyboard_cas:
  4963.  
  4964.         dec     eax
  4965.         jnz     no_add_keyboard_hotkey
  4966.  
  4967.         mov     eax, hotkey_list
  4968. @@:
  4969.         cmp     dword [eax+8], 0
  4970.         jz      .found_free
  4971.         add     eax, 16
  4972.         cmp     eax, hotkey_list+16*256
  4973.         jb      @b
  4974.         mov     dword [esp+36], 1
  4975.         ret
  4976. .found_free:
  4977.         mov     [eax+8], edi
  4978.         mov     [eax+4], ecx
  4979.         movzx   ebx, bl
  4980.         lea     ebx, [hotkey_scancodes+ebx*4]
  4981.         mov     ecx, [ebx]
  4982.         mov     [eax], ecx
  4983.         mov     [ebx], eax
  4984.         mov     [eax+12], ebx
  4985.         jecxz   @f
  4986.         mov     [ecx+12], eax
  4987. @@:
  4988.         and     dword [esp+36], 0
  4989.         ret
  4990.  
  4991. no_add_keyboard_hotkey:
  4992.  
  4993.         dec     eax
  4994.         jnz     no_del_keyboard_hotkey
  4995.  
  4996.         movzx   ebx, bl
  4997.         lea     ebx, [hotkey_scancodes+ebx*4]
  4998.         mov     eax, [ebx]
  4999. .scan:
  5000.         test    eax, eax
  5001.         jz      .notfound
  5002.         cmp     [eax+8], edi
  5003.         jnz     .next
  5004.         cmp     [eax+4], ecx
  5005.         jz      .found
  5006. .next:
  5007.         mov     eax, [eax]
  5008.         jmp     .scan
  5009. .notfound:
  5010.         mov     dword [esp+36], 1
  5011.         ret
  5012. .found:
  5013.         mov     ecx, [eax]
  5014.         jecxz   @f
  5015.         mov     edx, [eax+12]
  5016.         mov     [ecx+12], edx
  5017. @@:
  5018.         mov     ecx, [eax+12]
  5019.         mov     edx, [eax]
  5020.         mov     [ecx], edx
  5021.         xor     edx, edx
  5022.         mov     [eax+4], edx
  5023.         mov     [eax+8], edx
  5024.         mov     [eax+12], edx
  5025.         mov     [eax], edx
  5026.         mov     [esp+36], edx
  5027.         ret
  5028.  
  5029. no_del_keyboard_hotkey:
  5030.      ret
  5031.  
  5032.  
  5033. align 4
  5034.  
  5035. sys_gs:                         ; direct screen access
  5036.  
  5037.      cmp  eax,1                 ; resolution
  5038.      jne  no_gs1
  5039.      mov  eax,[Screen_Max_X]
  5040.      shl  eax,16
  5041.      mov  ax,[Screen_Max_Y]
  5042.      add  eax,0x00010001
  5043.      mov  [esp+36],eax
  5044.      ret
  5045.    no_gs1:
  5046.  
  5047.      cmp   eax,2                ; bits per pixel
  5048.      jne   no_gs2
  5049.      movzx eax,byte [ScreenBPP]
  5050.      mov   [esp+36],eax
  5051.      ret
  5052.    no_gs2:
  5053.  
  5054.      cmp   eax,3                ; bytes per scanline
  5055.      jne   no_gs3
  5056.      mov   eax,[BytesPerScanLine]
  5057.      mov   [esp+36],eax
  5058.      ret
  5059.    no_gs3:
  5060.  
  5061.      or  [esp+36],dword -1
  5062.      ret
  5063.  
  5064.  
  5065. align 4 ; PCI functions
  5066.  
  5067. sys_pci:
  5068.  
  5069.      call  pci_api
  5070.      mov   [esp+36],eax
  5071.      ret
  5072.  
  5073.  
  5074. align 4  ;  system functions
  5075.  
  5076. syscall_setpixel:                       ; SetPixel
  5077.  
  5078.         mov     eax, ebx
  5079.         mov     ebx, ecx
  5080.         mov     ecx, edx
  5081.         mov     edx, [TASK_BASE]
  5082.         add     eax, [edx-twdw+WDATA.box.left]
  5083.         add     ebx, [edx-twdw+WDATA.box.top]
  5084.         mov     edi, [current_slot]
  5085.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5086.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5087.         xor     edi, edi ; no force
  5088. ;       mov     edi, 1
  5089.         call    [_display.disable_mouse]
  5090.         jmp     [putpixel]
  5091.  
  5092. align 4
  5093.  
  5094. syscall_writetext:                      ; WriteText
  5095.  
  5096.         mov   eax,[TASK_BASE]
  5097.         mov   ebp,[eax-twdw+WDATA.box.left]
  5098.         push  esi
  5099.         mov   esi,[current_slot]
  5100.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  5101.         shl   ebp,16
  5102.         add   ebp,[eax-twdw+WDATA.box.top]
  5103.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  5104.         pop   esi
  5105.         add   ebx,ebp
  5106.         mov   eax,edi
  5107.         xor   edi,edi
  5108.         jmp   dtext
  5109.  
  5110. align 4
  5111.  
  5112. syscall_openramdiskfile:                ; OpenRamdiskFile
  5113.  
  5114.         mov     eax, ebx
  5115.         mov     ebx, ecx
  5116.         mov     ecx, edx
  5117.         mov     edx, esi
  5118.         mov     esi, 12
  5119.         call    fileread
  5120.         mov     [esp+32], eax
  5121.         ret
  5122.  
  5123. align 4
  5124.  
  5125. syscall_drawrect:                       ; DrawRect
  5126.  
  5127.         mov     edi, edx ; color + gradient
  5128.         and     edi, 0x80FFFFFF
  5129.         test    bx, bx  ; x.size
  5130.         je      .drectr
  5131.         test    cx, cx ; y.size
  5132.         je      .drectr
  5133.  
  5134.         mov     eax, ebx ; bad idea
  5135.         mov     ebx, ecx
  5136.  
  5137.         movzx   ecx, ax ; ecx - x.size
  5138.         shr     eax, 16 ; eax - x.coord
  5139.         movzx   edx, bx ; edx - y.size
  5140.         shr     ebx, 16 ; ebx - y.coord
  5141.         mov     esi, [current_slot]
  5142.  
  5143.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5144.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5145.         add     ecx, eax
  5146.         add     edx, ebx
  5147.         jmp     [drawbar]
  5148. .drectr:
  5149.         ret
  5150.  
  5151. align 4
  5152. syscall_getscreensize:                  ; GetScreenSize
  5153.         mov     ax, [Screen_Max_X]
  5154.         shl     eax, 16
  5155.         mov     ax, [Screen_Max_Y]
  5156.         mov     [esp + 32], eax
  5157.         ret
  5158.  
  5159. align 4
  5160.  
  5161. syscall_cdaudio:                        ; CD
  5162.  
  5163.         cmp     ebx, 4
  5164.         jb      .audio
  5165.         jz      .eject
  5166.         cmp     ebx, 5
  5167.         jnz     .ret
  5168. .load:
  5169.         call    .reserve
  5170.         call    LoadMedium
  5171.         ;call    .free
  5172.                 jmp             .free
  5173. ;        ret
  5174. .eject:
  5175.         call    .reserve
  5176.         call    clear_CD_cache
  5177.         call    allow_medium_removal
  5178.         call    EjectMedium
  5179. ;        call    .free
  5180.                 jmp             .free
  5181. ;        ret
  5182. .audio:
  5183.      call  sys_cd_audio
  5184.      mov   [esp+36-4],eax
  5185. .ret:
  5186.      ret
  5187.  
  5188. .reserve:
  5189.         call    reserve_cd
  5190.         mov     eax, ecx
  5191.         shr     eax, 1
  5192.         and     eax, 1
  5193.         inc     eax
  5194.         mov     [ChannelNumber], ax
  5195.         mov     eax, ecx
  5196.         and     eax, 1
  5197.         mov     [DiskNumber], al
  5198.         call    reserve_cd_channel
  5199.         and     ebx, 3
  5200.         inc     ebx
  5201.         mov     [cdpos], ebx
  5202.         add     ebx, ebx
  5203.         mov     cl, 8
  5204.         sub     cl, bl
  5205.         mov     al, [DRIVE_DATA+1]
  5206.         shr     al, cl
  5207.         test    al, 2
  5208.         jz      .free;.err
  5209.         ret
  5210. .free:
  5211.         call    free_cd_channel
  5212.         and     [cd_status], 0
  5213.         ret
  5214. .err:
  5215.         call    .free
  5216. ;        pop     eax
  5217.         ret
  5218.  
  5219. align 4
  5220.  
  5221. syscall_getpixel:                       ; GetPixel
  5222.      mov   ecx, [Screen_Max_X]
  5223.      inc   ecx
  5224.      xor   edx, edx
  5225.      mov   eax, ebx
  5226.      div   ecx
  5227.      mov   ebx, edx
  5228.      xchg  eax, ebx
  5229.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5230.      mov   [esp + 32], ecx
  5231.      ret
  5232.  
  5233. align 4
  5234.  
  5235. syscall_getarea:
  5236. ;eax = 36
  5237. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5238. ;ecx = [size x]*65536 + [size y]
  5239. ;edx = [start x]*65536 + [start y]
  5240.      pushad
  5241.          inc   [mouse_pause]
  5242. ; Check of use of the hardware cursor.
  5243.       cmp  [_display.disable_mouse],__sys_disable_mouse
  5244.           jne  @f
  5245. ; Since the test for the coordinates of the mouse should not be used,
  5246. ; then use the call [disable_mouse] is not possible!
  5247.       cmp  dword [MOUSE_VISIBLE],dword 0
  5248.       jne  @f
  5249.       pushf
  5250.       cli
  5251.       call draw_mouse_under
  5252.       popf
  5253.       mov  [MOUSE_VISIBLE],dword 1
  5254. @@:
  5255.      mov   edi,ebx
  5256.      mov   eax,edx
  5257.      shr   eax,16
  5258.      mov   ebx,edx
  5259.      and   ebx,0xffff
  5260.      dec   eax
  5261.            dec   ebx
  5262.      ; eax - x, ebx - y
  5263.      mov   edx,ecx
  5264.      
  5265.      shr   ecx,16
  5266.      and   edx,0xffff
  5267.      mov   esi,ecx
  5268.      ; ecx - size x, edx - size y
  5269.          
  5270.          mov   ebp,edx
  5271.          dec   ebp
  5272.      lea   ebp,[ebp*3]
  5273.          
  5274.          imul  ebp,esi
  5275.          
  5276.          mov   esi,ecx
  5277.          dec   esi
  5278.          lea   esi,[esi*3]
  5279.          
  5280.      add   ebp,esi
  5281.      add   ebp,edi
  5282.  
  5283.      add   ebx,edx
  5284.          
  5285. .start_y:
  5286.      push  ecx edx
  5287. .start_x:
  5288.      push  eax ebx ecx
  5289.      add   eax,ecx
  5290.  
  5291.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5292.      
  5293.      mov   [ebp],cx
  5294.      shr   ecx,16
  5295.      mov   [ebp+2],cl
  5296.  
  5297.      pop   ecx ebx eax
  5298.      sub   ebp,3
  5299.      dec   ecx
  5300.      jnz   .start_x
  5301.          pop   edx ecx
  5302.          dec   ebx
  5303.      dec   edx
  5304.      jnz   .start_y
  5305.      dec        [mouse_pause]
  5306. ; Check of use of the hardware cursor.
  5307.       cmp  [_display.disable_mouse],__sys_disable_mouse
  5308.           jne  @f
  5309.          call  [draw_pointer]
  5310. @@:
  5311.      popad
  5312.      ret
  5313.  
  5314. align 4
  5315.  
  5316. syscall_drawline:                       ; DrawLine
  5317.  
  5318.         mov     edi, [TASK_BASE]
  5319.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5320.         mov     ebp, eax
  5321.         mov     esi, [current_slot]
  5322.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5323.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5324.         add     ebp,ebx
  5325.         shl     eax, 16
  5326.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5327.         add     eax, ebp
  5328.         mov     ebp, ebx
  5329.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5330.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5331.         add     ebp, ecx
  5332.         shl     ebx, 16
  5333.         xor     edi, edi
  5334.         add     ebx, ebp
  5335.         mov     ecx, edx
  5336.         jmp     [draw_line]
  5337.  
  5338. align 4
  5339.  
  5340. syscall_getirqowner:                    ; GetIrqOwner
  5341.  
  5342.      cmp   ebx,16
  5343.      jae   .err
  5344.  
  5345.      cmp   [irq_rights + 4 * ebx], dword 2
  5346.      je    .err
  5347.  
  5348.      mov   eax,[4 * ebx + irq_owner]
  5349.      mov   [esp+32],eax
  5350.  
  5351.      ret
  5352. .err:
  5353.      or    dword [esp+32], -1
  5354.      ret
  5355.  
  5356. align 4
  5357.  
  5358. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5359.  
  5360.      call  r_f_port_area
  5361.      mov   [esp+32],eax
  5362.      ret
  5363.  
  5364. align 4
  5365.  
  5366. syscall_threads:                        ; CreateThreads
  5367. ; eax=1 create thread
  5368. ;
  5369. ;   ebx=thread start
  5370. ;   ecx=thread stack value
  5371. ;
  5372. ; on return : eax = pid
  5373.  
  5374.      call  new_sys_threads
  5375.  
  5376.      mov   [esp+32],eax
  5377.      ret
  5378.  
  5379. align 4
  5380.  
  5381. read_from_hd:                           ; Read from hd - fn not in use
  5382.  
  5383.      mov   edi,[TASK_BASE]
  5384.      add   edi,TASKDATA.mem_start
  5385.      add   eax,[edi]
  5386.      add   ecx,[edi]
  5387.      add   edx,[edi]
  5388.      call  file_read
  5389.  
  5390.      mov   [esp+36],eax
  5391.      mov   [esp+24],ebx
  5392.  
  5393.      ret
  5394.  
  5395. paleholder:
  5396.         ret
  5397.  
  5398. align 4
  5399. set_screen:
  5400.         cmp eax, [Screen_Max_X]
  5401.         jne .set
  5402.  
  5403.         cmp edx, [Screen_Max_Y]
  5404.         jne .set
  5405.         ret
  5406. .set:
  5407.         pushfd
  5408.         cli
  5409.  
  5410.         mov [Screen_Max_X], eax
  5411.         mov [Screen_Max_Y], edx
  5412.         mov [BytesPerScanLine], ecx
  5413.  
  5414.         mov [screen_workarea.right],eax
  5415.         mov [screen_workarea.bottom], edx
  5416.  
  5417.         push ebx
  5418.         push esi
  5419.         push edi
  5420.  
  5421.         pushad
  5422.  
  5423.         stdcall kernel_free, [_WinMapAddress]
  5424.  
  5425.         mov eax, [_display.width]
  5426.         mul [_display.height]
  5427.         mov [_WinMapSize], eax
  5428.  
  5429.         stdcall kernel_alloc, eax
  5430.         mov [_WinMapAddress], eax
  5431.         test eax, eax
  5432.         jz .epic_fail
  5433.  
  5434.         popad
  5435.  
  5436.         call    repos_windows
  5437.         xor eax, eax
  5438.         xor ebx, ebx
  5439.         mov     ecx, [Screen_Max_X]
  5440.         mov     edx, [Screen_Max_Y]
  5441.         call    calculatescreen
  5442.         pop edi
  5443.         pop esi
  5444.         pop ebx
  5445.  
  5446.         popfd
  5447.         ret
  5448.  
  5449. .epic_fail:
  5450.         hlt                     ; Houston, we've had a problem
  5451.  
  5452. ; --------------- APM ---------------------
  5453. uglobal
  5454. apm_entry       dp      0
  5455. apm_vf          dd      0
  5456. endg
  5457.  
  5458. align 4
  5459. sys_apm:
  5460.         xor     eax,eax
  5461.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5462.         jne     @f
  5463.         inc     eax
  5464.         or      dword [esp + 44], eax   ; error
  5465.         add     eax,7
  5466.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5467.         ret
  5468.  
  5469. @@:
  5470. ;       xchg    eax, ecx
  5471. ;       xchg    ebx, ecx
  5472.  
  5473.         cmp     dx, 3
  5474.         ja      @f
  5475.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5476.         mov     eax,[apm_vf]
  5477.         mov     [esp + 32], eax
  5478.         shr     eax, 16
  5479.         mov     [esp + 28], eax
  5480.         ret
  5481.  
  5482. @@:
  5483.  
  5484.         mov     esi,[master_tab+(OS_BASE shr 20)]
  5485.         xchg    [master_tab], esi
  5486.         push    esi
  5487.         mov     edi, cr3
  5488.         mov     cr3, edi                ;flush TLB
  5489.  
  5490.         call    pword [apm_entry]       ;call APM BIOS
  5491.  
  5492.         xchg    eax, [esp]
  5493.         mov     [master_tab], eax
  5494.         mov     eax, cr3
  5495.         mov     cr3, eax
  5496.         pop eax
  5497.  
  5498.         mov     [esp + 4 ], edi
  5499.         mov     [esp + 8], esi
  5500.         mov     [esp + 20], ebx
  5501.         mov     [esp + 24], edx
  5502.         mov     [esp + 28], ecx
  5503.         mov     [esp + 32], eax
  5504.         setc    al
  5505.         and     [esp + 44], byte 0xfe
  5506.         or      [esp + 44], al
  5507.         ret
  5508. ; -----------------------------------------
  5509.  
  5510. align 4
  5511.  
  5512. undefined_syscall:                      ; Undefined system call
  5513.      mov   [esp + 32], dword -1
  5514.      ret
  5515.  
  5516. align 4
  5517. system_shutdown:          ; shut down the system
  5518.  
  5519.            cmp byte [BOOT_VAR+0x9030], 1
  5520.            jne @F
  5521.            ret
  5522. @@:
  5523.            call stop_all_services
  5524.            push 3                ; stop playing cd
  5525.            pop  eax
  5526.            call sys_cd_audio
  5527.  
  5528. yes_shutdown_param:
  5529.            cli
  5530.  
  5531.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  5532.            push 12
  5533.            pop  esi
  5534.            xor  ebx,ebx
  5535.            or   ecx,-1
  5536.            mov  edx, OS_BASE+0x70000
  5537.            call fileread
  5538.  
  5539.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5540.            mov  edi,OS_BASE+0x40000
  5541.            mov  ecx,1000
  5542.            rep  movsb
  5543.  
  5544.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5545.            mov  edi, OS_BASE
  5546.            mov  ecx,0x10000/4
  5547.            cld
  5548.            rep movsd
  5549.  
  5550.            call restorefatchain
  5551.  
  5552.            mov al, 0xFF
  5553.            out 0x21, al
  5554.            out 0xA1, al
  5555.  
  5556. if 0
  5557.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5558.            mov  word [OS_BASE+0x467+2],0x1000
  5559.  
  5560.            mov  al,0x0F
  5561.            out  0x70,al
  5562.            mov  al,0x05
  5563.            out  0x71,al
  5564.  
  5565.            mov  al,0xFE
  5566.            out  0x64,al
  5567.  
  5568.            hlt
  5569.            jmp $-1
  5570.  
  5571. else
  5572.         cmp     byte [OS_BASE + 0x9030], 2
  5573.         jnz     no_acpi_power_off
  5574.  
  5575. ; scan for RSDP
  5576. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5577.         movzx   eax, word [OS_BASE + 0x40E]
  5578.         shl     eax, 4
  5579.         jz      @f
  5580.         mov     ecx, 1024/16
  5581.         call    scan_rsdp
  5582.         jnc     .rsdp_found
  5583. @@:
  5584. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5585.         mov     eax, 0xE0000
  5586.         mov     ecx, 0x2000
  5587.         call    scan_rsdp
  5588.         jc      no_acpi_power_off
  5589. .rsdp_found:
  5590.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5591.         mov     ebp, [ipc_tmp]
  5592.         stdcall map_page, ebp, esi, PG_MAP
  5593.         lea     eax, [esi+1000h]
  5594.         lea     edx, [ebp+1000h]
  5595.         stdcall map_page, edx, eax, PG_MAP
  5596.         and     esi, 0xFFF
  5597.         add     esi, ebp
  5598.         cmp     dword [esi], 'RSDT'
  5599.         jnz     no_acpi_power_off
  5600.         mov     ecx, [esi+4]
  5601.         sub     ecx, 24h
  5602.         jbe     no_acpi_power_off
  5603.         shr     ecx, 2
  5604.         add     esi, 24h
  5605. .scan_fadt:
  5606.         lodsd
  5607.         mov     ebx, eax
  5608.         lea     eax, [ebp+2000h]
  5609.         stdcall map_page, eax, ebx, PG_MAP
  5610.         lea     eax, [ebp+3000h]
  5611.         add     ebx, 0x1000
  5612.         stdcall map_page, eax, ebx, PG_MAP
  5613.         and     ebx, 0xFFF
  5614.         lea     ebx, [ebx+ebp+2000h]
  5615.         cmp     dword [ebx], 'FACP'
  5616.         jz      .fadt_found
  5617.         loop    .scan_fadt
  5618.         jmp     no_acpi_power_off
  5619. .fadt_found:
  5620. ; ebx is linear address of FADT
  5621.         mov     edi, [ebx+40] ; physical address of the DSDT
  5622.         lea     eax, [ebp+4000h]
  5623.         stdcall map_page, eax, edi, PG_MAP
  5624.         lea     eax, [ebp+5000h]
  5625.         lea     esi, [edi+0x1000]
  5626.         stdcall map_page, eax, esi, PG_MAP
  5627.         and     esi, 0xFFF
  5628.         sub     edi, esi
  5629.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5630.         jnz     no_acpi_power_off
  5631.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5632.         sub     eax, 36+4
  5633.         jbe     no_acpi_power_off
  5634.         add     esi, 36
  5635. .scan_dsdt:
  5636.         cmp     dword [esi+ebp+4000h], '_S5_'
  5637.         jnz     .scan_dsdt_cont
  5638.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5639.         jnz     .scan_dsdt_cont
  5640.         mov     dl, [esi+ebp+4000h+6]
  5641.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5642.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5643.         ja      .scan_dsdt_cont
  5644.         cmp     dl, 1
  5645.         jb      .scan_dsdt_cont
  5646.         lea     esi, [esi+ebp+4000h+7]
  5647.         xor     ecx, ecx
  5648.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5649.         jz      @f
  5650.         cmp     byte [esi], 0xA
  5651.         jnz     no_acpi_power_off
  5652.         inc     esi
  5653.         mov     cl, [esi]
  5654. @@:
  5655.         inc     esi
  5656.         cmp     dl, 2
  5657.         jb      @f
  5658.         cmp     byte [esi], 0
  5659.         jz      @f
  5660.         cmp     byte [esi], 0xA
  5661.         jnz     no_acpi_power_off
  5662.         inc     esi
  5663.         mov     ch, [esi]
  5664. @@:
  5665.         jmp     do_acpi_power_off
  5666. .scan_dsdt_cont:
  5667.         inc     esi
  5668.         cmp     esi, 0x1000
  5669.         jb      @f
  5670.         sub     esi, 0x1000
  5671.         add     edi, 0x1000
  5672.         push    eax
  5673.         lea     eax, [ebp+4000h]
  5674.         stdcall map_page, eax, edi, PG_MAP
  5675.         push    PG_MAP
  5676.         lea     eax, [edi+1000h]
  5677.         push    eax
  5678.         lea     eax, [ebp+5000h]
  5679.         push    eax
  5680.         stdcall map_page
  5681.         pop     eax
  5682. @@:
  5683.         dec     eax
  5684.         jnz     .scan_dsdt
  5685.         jmp     no_acpi_power_off
  5686. do_acpi_power_off:
  5687.         mov     edx, [ebx+48]
  5688.         test    edx, edx
  5689.         jz      .nosmi
  5690.         mov     al, [ebx+52]
  5691.         out     dx, al
  5692.         mov     edx, [ebx+64]
  5693. @@:
  5694.         in      ax, dx
  5695.         test    al, 1
  5696.         jz      @b
  5697. .nosmi:
  5698.         and     cx, 0x0707
  5699.         shl     cx, 2
  5700.         or      cx, 0x2020
  5701.         mov     edx, [ebx+64]
  5702.         in      ax, dx
  5703.         and     ax, 203h
  5704.         or      ah, cl
  5705.         out     dx, ax
  5706.         mov     edx, [ebx+68]
  5707.         test    edx, edx
  5708.         jz      @f
  5709.         in      ax, dx
  5710.         and     ax, 203h
  5711.         or      ah, ch
  5712.         out     dx, ax
  5713. @@:
  5714.         jmp     $
  5715.  
  5716.  
  5717. no_acpi_power_off:
  5718.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5719.            mov  word [OS_BASE+0x467+2],0x1000
  5720.  
  5721.            mov  al,0x0F
  5722.            out  0x70,al
  5723.            mov  al,0x05
  5724.            out  0x71,al
  5725.  
  5726.            mov  al,0xFE
  5727.            out  0x64,al
  5728.  
  5729.            hlt
  5730.            jmp $-1
  5731.  
  5732. scan_rsdp:
  5733.         add     eax, OS_BASE
  5734. .s:
  5735.         cmp     dword [eax], 'RSD '
  5736.         jnz     .n
  5737.         cmp     dword [eax+4], 'PTR '
  5738.         jnz     .n
  5739.         xor     edx, edx
  5740.         xor     esi, esi
  5741. @@:
  5742.         add     dl, [eax+esi]
  5743.         inc     esi
  5744.         cmp     esi, 20
  5745.         jnz     @b
  5746.         test    dl, dl
  5747.         jz      .ok
  5748. .n:
  5749.         add     eax, 10h
  5750.         loop    .s
  5751.         stc
  5752. .ok:
  5753.         ret
  5754. end if
  5755.  
  5756. include "data32.inc"
  5757.  
  5758. __REV__ = __REV
  5759.  
  5760. uglobals_size = $ - endofcode
  5761. diff16 "end of kernel code",0,$
  5762.