Subversion Repositories Kolibri OS

Rev

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