Subversion Repositories Kolibri OS

Rev

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