Subversion Repositories Kolibri OS

Rev

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

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