Subversion Repositories Kolibri OS

Rev

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