Subversion Repositories Kolibri OS

Rev

Rev 1055 | Rev 1063 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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