Subversion Repositories Kolibri OS

Rev

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