Subversion Repositories Kolibri OS

Rev

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