Subversion Repositories Kolibri OS

Rev

Rev 3193 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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