Subversion Repositories Kolibri OS

Rev

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