Subversion Repositories Kolibri OS

Rev

Rev 1198 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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