Subversion Repositories Kolibri OS

Rev

Rev 760 | Rev 765 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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