Subversion Repositories Kolibri OS

Rev

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