Subversion Repositories Kolibri OS

Rev

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