Subversion Repositories Kolibri OS

Rev

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