Subversion Repositories Kolibri OS

Rev

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