Subversion Repositories Kolibri OS

Rev

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