Subversion Repositories Kolibri OS

Rev

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

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