Subversion Repositories Kolibri OS

Rev

Rev 2547 | Rev 2580 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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