Subversion Repositories Kolibri OS

Rev

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