Subversion Repositories Kolibri OS

Rev

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