Subversion Repositories Kolibri OS

Rev

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