Subversion Repositories Kolibri OS

Rev

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