Subversion Repositories Kolibri OS

Rev

Rev 2867 | Blame | Last modification | View Log | Download | RSS feed

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