Subversion Repositories Kolibri OS

Rev

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