Subversion Repositories Kolibri OS

Rev

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