Subversion Repositories Kolibri OS

Rev

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

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