Subversion Repositories Kolibri OS

Rev

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