Subversion Repositories Kolibri OS

Rev

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