Subversion Repositories Kolibri OS

Rev

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