Subversion Repositories Kolibri OS

Rev

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