Subversion Repositories Kolibri OS

Rev

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