Subversion Repositories Kolibri OS

Rev

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