Subversion Repositories Kolibri OS

Rev

Rev 2540 | Blame | Last modification | View Log | Download | RSS feed

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