Subversion Repositories Kolibri OS

Rev

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

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