Subversion Repositories Kolibri OS

Rev

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