Subversion Repositories Kolibri OS

Rev

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