Subversion Repositories Kolibri OS

Rev

Rev 897 | Rev 914 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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