Subversion Repositories Kolibri OS

Rev

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