Subversion Repositories Kolibri OS

Rev

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

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