Subversion Repositories Kolibri OS

Rev

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