Subversion Repositories Kolibri OS

Rev

Rev 2408 | Rev 2414 | 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: 2411 $
  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.  
  3083.         xor     edi, edi
  3084.         mov     ebx, CURRENT_TASK
  3085.  
  3086.         mov     ecx, [TASK_COUNT]
  3087.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3088.         shl     eax, 8
  3089.  
  3090. align 4
  3091. .set_mouse_event:
  3092.         add     edi, 256
  3093.         add     ebx, 32
  3094.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3095.         jz      .set
  3096.  
  3097.         cmp     eax, edi                        ; skip if filtration active
  3098.         jne     .skip
  3099. .set:
  3100.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
  3101. .skip:
  3102.         loop    .set_mouse_event
  3103.  
  3104. mouse_not_active:
  3105.         cmp     byte[BACKGROUND_CHANGED], 0
  3106.         jz      no_set_bgr_event
  3107.         xor     edi, edi
  3108.         mov     ecx, [TASK_COUNT]
  3109. set_bgr_event:
  3110.         add     edi, 256
  3111.         or      [edi+SLOT_BASE+APPDATA.event_mask], 16
  3112.         loop    set_bgr_event
  3113.         mov     byte[BACKGROUND_CHANGED], 0
  3114. no_set_bgr_event:
  3115.         cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
  3116.         jz      nobackgr
  3117.         cmp     [background_defined], 0
  3118.         jz      nobackgr
  3119. ;    mov   [draw_data+32 + RECT.left],dword 0
  3120. ;    mov   [draw_data+32 + RECT.top],dword 0
  3121. ;    mov   eax,[Screen_Max_X]
  3122. ;    mov   ebx,[Screen_Max_Y]
  3123. ;    mov   [draw_data+32 + RECT.right],eax
  3124. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3125. @@:
  3126.         call    drawbackground
  3127.         xor     eax, eax
  3128.         xchg    al, [REDRAW_BACKGROUND]
  3129.         test    al, al                             ; got new update request?
  3130.         jnz     @b
  3131.         mov     [draw_data+32 + RECT.left], eax
  3132.         mov     [draw_data+32 + RECT.top], eax
  3133.         mov     [draw_data+32 + RECT.right], eax
  3134.         mov     [draw_data+32 + RECT.bottom], eax
  3135.         mov     [MOUSE_BACKGROUND], byte 0
  3136.  
  3137. nobackgr:
  3138.  
  3139.     ; system shutdown request
  3140.  
  3141.         cmp     [SYS_SHUTDOWN], byte 0
  3142.         je      noshutdown
  3143.  
  3144.         mov     edx, [shutdown_processes]
  3145.  
  3146.         cmp     [SYS_SHUTDOWN], dl
  3147.         jne     no_mark_system_shutdown
  3148.  
  3149.         lea     ecx, [edx-1]
  3150.         mov     edx, OS_BASE+0x3040
  3151.         jecxz   @f
  3152. markz:
  3153.         mov     [edx+TASKDATA.state], byte 3
  3154.         add     edx, 0x20
  3155.         loop    markz
  3156. @@:
  3157.  
  3158.   no_mark_system_shutdown:
  3159.  
  3160.         call    [_display.disable_mouse]
  3161.  
  3162.         dec     byte [SYS_SHUTDOWN]
  3163.         je      system_shutdown
  3164.  
  3165. noshutdown:
  3166.  
  3167.  
  3168.         mov     eax, [TASK_COUNT]           ; termination
  3169.         mov     ebx, TASK_DATA+TASKDATA.state
  3170.         mov     esi, 1
  3171.  
  3172. newct:
  3173.         mov     cl, [ebx]
  3174.         cmp     cl, byte 3
  3175.         jz      terminate
  3176.         cmp     cl, byte 4
  3177.         jz      terminate
  3178.  
  3179.         add     ebx, 0x20
  3180.         inc     esi
  3181.         dec     eax
  3182.         jnz     newct
  3183.         ret
  3184.  
  3185. ; redraw screen
  3186.  
  3187. redrawscreen:
  3188.  
  3189. ; eax , if process window_data base is eax, do not set flag/limits
  3190.  
  3191.         pushad
  3192.         push    eax
  3193.  
  3194. ;;;         mov   ebx,2
  3195. ;;;         call  delay_hs
  3196.  
  3197.          ;mov   ecx,0               ; redraw flags for apps
  3198.         xor     ecx, ecx
  3199.        newdw2:
  3200.  
  3201.         inc     ecx
  3202.         push    ecx
  3203.  
  3204.         mov     eax, ecx
  3205.         shl     eax, 5
  3206.         add     eax, window_data
  3207.  
  3208.         cmp     eax, [esp+4]
  3209.         je      not_this_task
  3210.                                    ; check if window in redraw area
  3211.         mov     edi, eax
  3212.  
  3213.         cmp     ecx, 1             ; limit for background
  3214.         jz      bgli
  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     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3224.         cmp     ecx, ebx
  3225.         jb      ricino
  3226.  
  3227.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3228.         cmp     ecx, eax
  3229.         jb      ricino
  3230.  
  3231.         mov     eax, [edi + WDATA.box.left]
  3232.         mov     ebx, [edi + WDATA.box.top]
  3233.         mov     ecx, [edi + WDATA.box.width]
  3234.         mov     edx, [edi + WDATA.box.height]
  3235.         add     ecx, eax
  3236.         add     edx, ebx
  3237.  
  3238.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3239.         cmp     edx, eax
  3240.         jb      ricino
  3241.  
  3242.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3243.         cmp     ecx, eax
  3244.         jb      ricino
  3245.  
  3246.         bgli:
  3247.  
  3248.         cmp     dword[esp], 1
  3249.         jnz     .az
  3250. ;         cmp   byte[BACKGROUND_CHANGED], 0
  3251. ;         jnz   newdw8
  3252.         cmp     byte[REDRAW_BACKGROUND], 0
  3253.         jz      .az
  3254.         mov     dl, 0
  3255.         lea     eax, [edi+draw_data-window_data]
  3256.         mov     ebx, [draw_limits.left]
  3257.         cmp     ebx, [eax+RECT.left]
  3258.         jae     @f
  3259.         mov     [eax+RECT.left], ebx
  3260.         mov     dl, 1
  3261.         @@:
  3262.         mov     ebx, [draw_limits.top]
  3263.         cmp     ebx, [eax+RECT.top]
  3264.         jae     @f
  3265.         mov     [eax+RECT.top], ebx
  3266.         mov     dl, 1
  3267.         @@:
  3268.         mov     ebx, [draw_limits.right]
  3269.         cmp     ebx, [eax+RECT.right]
  3270.         jbe     @f
  3271.         mov     [eax+RECT.right], ebx
  3272.         mov     dl, 1
  3273.         @@:
  3274.         mov     ebx, [draw_limits.bottom]
  3275.         cmp     ebx, [eax+RECT.bottom]
  3276.         jbe     @f
  3277.         mov     [eax+RECT.bottom], ebx
  3278.         mov     dl, 1
  3279.         @@:
  3280.         add     byte[REDRAW_BACKGROUND], dl
  3281.         jmp     newdw8
  3282.         .az:
  3283.  
  3284.         mov     eax, edi
  3285.         add     eax, draw_data-window_data
  3286.  
  3287.         mov     ebx, [draw_limits.left]        ; set limits
  3288.         mov     [eax + RECT.left], ebx
  3289.         mov     ebx, [draw_limits.top]
  3290.         mov     [eax + RECT.top], ebx
  3291.         mov     ebx, [draw_limits.right]
  3292.         mov     [eax + RECT.right], ebx
  3293.         mov     ebx, [draw_limits.bottom]
  3294.         mov     [eax + RECT.bottom], ebx
  3295.  
  3296.         sub     eax, draw_data-window_data
  3297.  
  3298.         cmp     dword [esp], 1
  3299.         jne     nobgrd
  3300.         inc     byte[REDRAW_BACKGROUND]
  3301.  
  3302.        newdw8:
  3303.        nobgrd:
  3304.  
  3305.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3306.  
  3307.        ricino:
  3308.  
  3309.        not_this_task:
  3310.  
  3311.         pop     ecx
  3312.  
  3313.         cmp     ecx, [TASK_COUNT]
  3314.         jle     newdw2
  3315.  
  3316.         pop     eax
  3317.         popad
  3318.  
  3319.         ret
  3320.  
  3321. calculatebackground:   ; background
  3322.  
  3323.         mov     edi, [_WinMapAddress]              ; set os to use all pixels
  3324.         mov     eax, 0x01010101
  3325.         mov     ecx, [_WinMapSize]
  3326.         shr     ecx, 2
  3327.         rep stosd
  3328.  
  3329.         mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
  3330.         mov     byte[BACKGROUND_CHANGED], 0
  3331.  
  3332.         ret
  3333.  
  3334. uglobal
  3335.   imax    dd 0x0
  3336. endg
  3337.  
  3338.  
  3339.  
  3340. delay_ms:     ; delay in 1/1000 sec
  3341.  
  3342.  
  3343.         push    eax
  3344.         push    ecx
  3345.  
  3346.         mov     ecx, esi
  3347.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3348.         imul    ecx, 33941
  3349.         shr     ecx, 9
  3350.         ; </CPU clock fix>
  3351.  
  3352.         in      al, 0x61
  3353.         and     al, 0x10
  3354.         mov     ah, al
  3355.         cld
  3356.  
  3357.  cnt1:
  3358.         in      al, 0x61
  3359.         and     al, 0x10
  3360.         cmp     al, ah
  3361.         jz      cnt1
  3362.  
  3363.         mov     ah, al
  3364.         loop    cnt1
  3365.  
  3366.         pop     ecx
  3367.         pop     eax
  3368.  
  3369.         ret
  3370.  
  3371. align 4
  3372. set_app_param:
  3373.         mov     edi, [TASK_BASE]
  3374.         mov     eax, ebx
  3375.         btr     eax, 3                           ; move MOUSE_FILTRATION
  3376.         mov     ebx, [current_slot]              ; bit into event_filter
  3377.         setc    byte [ebx+APPDATA.event_filter]
  3378.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3379.         mov     [esp+32], eax                    ; return old mask value
  3380.         ret
  3381.  
  3382.  
  3383.  
  3384. delay_hs:     ; delay in 1/100 secs
  3385. ; ebx = delay time
  3386.         push    ecx
  3387.         push    edx
  3388.  
  3389.         mov     edx, [timer_ticks]
  3390.  
  3391.       newtic:
  3392.         mov     ecx, [timer_ticks]
  3393.         sub     ecx, edx
  3394.         cmp     ecx, ebx
  3395.         jae     zerodelay
  3396.  
  3397.         call    change_task
  3398.  
  3399.         jmp     newtic
  3400.  
  3401.       zerodelay:
  3402.         pop     edx
  3403.         pop     ecx
  3404.  
  3405.         ret
  3406.  
  3407. align 16        ;very often call this subrutine
  3408. memmove:       ; memory move in bytes
  3409.  
  3410. ; eax = from
  3411. ; ebx = to
  3412. ; ecx = no of bytes
  3413.         test    ecx, ecx
  3414.         jle     .ret
  3415.  
  3416.         push    esi edi ecx
  3417.  
  3418.         mov     edi, ebx
  3419.         mov     esi, eax
  3420.  
  3421.         test    ecx, not 11b
  3422.         jz      @f
  3423.  
  3424.         push    ecx
  3425.         shr     ecx, 2
  3426.         rep movsd
  3427.         pop     ecx
  3428.         and     ecx, 11b
  3429.         jz      .finish
  3430.   @@:
  3431.         rep movsb
  3432.  
  3433.   .finish:
  3434.         pop     ecx edi esi
  3435.   .ret:
  3436.         ret
  3437.  
  3438.  
  3439. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3440. ;align 4
  3441. ;
  3442. ;read_floppy_file:
  3443. ;
  3444. ;; as input
  3445. ;;
  3446. ;; eax pointer to file
  3447. ;; ebx file lenght
  3448. ;; ecx start 512 byte block number
  3449. ;; edx number of blocks to read
  3450. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3451. ;;
  3452. ;;
  3453. ;; on return
  3454. ;;
  3455. ;; eax = 0 command succesful
  3456. ;;       1 no fd base and/or partition defined
  3457. ;;       2 yet unsupported FS
  3458. ;;       3 unknown FS
  3459. ;;       4 partition not defined at hd
  3460. ;;       5 file not found
  3461. ;; ebx = size of file
  3462. ;
  3463. ;     mov   edi,[TASK_BASE]
  3464. ;     add   edi,0x10
  3465. ;     add   esi,[edi]
  3466. ;     add   eax,[edi]
  3467. ;
  3468. ;     pushad
  3469. ;     mov  edi,esi
  3470. ;     add  edi,1024
  3471. ;     mov  esi,0x100000+19*512
  3472. ;     sub  ecx,1
  3473. ;     shl  ecx,9
  3474. ;     add  esi,ecx
  3475. ;     shl  edx,9
  3476. ;     mov  ecx,edx
  3477. ;     cld
  3478. ;     rep  movsb
  3479. ;     popad
  3480. ;
  3481. ;     mov   [esp+36],eax
  3482. ;     mov   [esp+24],ebx
  3483. ;     ret
  3484.  
  3485.  
  3486.  
  3487. align 4
  3488. set_io_access_rights:
  3489.         push    edi eax
  3490.         mov     edi, tss._io_map_0
  3491. ;     mov   ecx,eax
  3492. ;     and   ecx,7    ; offset in byte
  3493. ;     shr   eax,3    ; number of byte
  3494. ;     add   edi,eax
  3495. ;     mov   ebx,1
  3496. ;     shl   ebx,cl
  3497.         test    ebp, ebp
  3498. ;     cmp   ebp,0                ; enable access - ebp = 0
  3499.         jnz     .siar1
  3500. ;     not   ebx
  3501. ;     and   [edi],byte bl
  3502.         btr     [edi], eax
  3503.         pop     eax edi
  3504.         ret
  3505. .siar1:
  3506.         bts     [edi], eax
  3507.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3508.         pop     eax edi
  3509.         ret
  3510. ;reserve/free group of ports
  3511. ;  * eax = 46 - number function
  3512. ;  * ebx = 0 - reserve, 1 - free
  3513. ;  * ecx = number start arrea of ports
  3514. ;  * edx = number end arrea of ports (include last number of port)
  3515. ;Return value:
  3516. ;  * eax = 0 - succesful
  3517. ;  * eax = 1 - error
  3518. ;  * The system has reserve this ports:
  3519. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3520. ;destroys eax,ebx, ebp
  3521. r_f_port_area:
  3522.  
  3523.         test    ebx, ebx
  3524.         jnz     free_port_area
  3525. ;     je    r_port_area
  3526. ;     jmp   free_port_area
  3527.  
  3528. ;   r_port_area:
  3529.  
  3530. ;     pushad
  3531.  
  3532.         cmp     ecx, edx      ; beginning > end ?
  3533.         ja      rpal1
  3534.         cmp     edx, 65536
  3535.         jae     rpal1
  3536.         mov     eax, [RESERVED_PORTS]
  3537.         test    eax, eax      ; no reserved areas ?
  3538.         je      rpal2
  3539.         cmp     eax, 255      ; max reserved
  3540.         jae     rpal1
  3541.  rpal3:
  3542.         mov     ebx, eax
  3543.         shl     ebx, 4
  3544.         add     ebx, RESERVED_PORTS
  3545.         cmp     ecx, [ebx+8]
  3546.         ja      rpal4
  3547.         cmp     edx, [ebx+4]
  3548.         jae     rpal1
  3549. ;     jb    rpal4
  3550. ;     jmp   rpal1
  3551.  rpal4:
  3552.         dec     eax
  3553.         jnz     rpal3
  3554.         jmp     rpal2
  3555.    rpal1:
  3556. ;     popad
  3557. ;     mov   eax,1
  3558.         xor     eax, eax
  3559.         inc     eax
  3560.         ret
  3561.    rpal2:
  3562. ;     popad
  3563.      ; enable port access at port IO map
  3564.         cli
  3565.         pushad                        ; start enable io map
  3566.  
  3567.         cmp     edx, 65536;16384
  3568.         jae     no_unmask_io; jge
  3569.         mov     eax, ecx
  3570. ;       push    ebp
  3571.         xor     ebp, ebp               ; enable - eax = port
  3572. new_port_access:
  3573. ;     pushad
  3574.         call    set_io_access_rights
  3575. ;     popad
  3576.         inc     eax
  3577.         cmp     eax, edx
  3578.         jbe     new_port_access
  3579. ;       pop     ebp
  3580. no_unmask_io:
  3581.         popad                         ; end enable io map
  3582.         sti
  3583.  
  3584.         mov     eax, [RESERVED_PORTS]
  3585.         add     eax, 1
  3586.         mov     [RESERVED_PORTS], eax
  3587.         shl     eax, 4
  3588.         add     eax, RESERVED_PORTS
  3589.         mov     ebx, [TASK_BASE]
  3590.         mov     ebx, [ebx+TASKDATA.pid]
  3591.         mov     [eax], ebx
  3592.         mov     [eax+4], ecx
  3593.         mov     [eax+8], edx
  3594.  
  3595.         xor     eax, eax
  3596.         ret
  3597.  
  3598. free_port_area:
  3599.  
  3600. ;     pushad
  3601.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3602.         test    eax, eax
  3603.         jz      frpal2
  3604.         mov     ebx, [TASK_BASE]
  3605.         mov     ebx, [ebx+TASKDATA.pid]
  3606.    frpal3:
  3607.         mov     edi, eax
  3608.         shl     edi, 4
  3609.         add     edi, RESERVED_PORTS
  3610.         cmp     ebx, [edi]
  3611.         jne     frpal4
  3612.         cmp     ecx, [edi+4]
  3613.         jne     frpal4
  3614.         cmp     edx, [edi+8]
  3615.         jne     frpal4
  3616.         jmp     frpal1
  3617.    frpal4:
  3618.         dec     eax
  3619.         jnz     frpal3
  3620.    frpal2:
  3621. ;     popad
  3622.         inc     eax
  3623.         ret
  3624.    frpal1:
  3625.         push    ecx
  3626.         mov     ecx, 256
  3627.         sub     ecx, eax
  3628.         shl     ecx, 4
  3629.         mov     esi, edi
  3630.         add     esi, 16
  3631.         cld
  3632.         rep movsb
  3633.  
  3634.         dec     dword [RESERVED_PORTS]
  3635. ;popad
  3636. ;disable port access at port IO map
  3637.  
  3638. ;     pushad                        ; start disable io map
  3639.         pop     eax     ;start port
  3640.         cmp     edx, 65536;16384
  3641.         jge     no_mask_io
  3642.  
  3643. ;     mov   eax,ecx
  3644.         xor     ebp, ebp
  3645.         inc     ebp
  3646. new_port_access_disable:
  3647. ;     pushad
  3648. ;     mov   ebp,1                  ; disable - eax = port
  3649.         call    set_io_access_rights
  3650. ;     popad
  3651.         inc     eax
  3652.         cmp     eax, edx
  3653.         jbe     new_port_access_disable
  3654. no_mask_io:
  3655. ;     popad                         ; end disable io map
  3656.         xor     eax, eax
  3657.         ret
  3658.  
  3659.  
  3660. align 4
  3661. drawbackground:
  3662.         inc     [mouse_pause]
  3663. ;        cmp     [SCR_MODE], word 0x12
  3664. ;        je      dbrv20
  3665. ;     dbrv12:
  3666. ;        cmp     [SCR_MODE], word 0100000000000000b
  3667. ;        jge     dbrv20
  3668. ;        cmp     [SCR_MODE], word 0x13
  3669. ;        je      dbrv20
  3670. ;        call    vesa12_drawbackground
  3671. ;        dec     [mouse_pause]
  3672. ;        call    [draw_pointer]
  3673. ;        ret
  3674.      dbrv20:
  3675.         cmp     [BgrDrawMode], dword 1
  3676.         jne     bgrstr
  3677.         call    vesa20_drawbackground_tiled
  3678.         dec     [mouse_pause]
  3679.         call    [draw_pointer]
  3680.         ret
  3681.      bgrstr:
  3682.         call    vesa20_drawbackground_stretch
  3683.         dec     [mouse_pause]
  3684.         call    [draw_pointer]
  3685.         ret
  3686.  
  3687. align 4
  3688.  
  3689. syscall_putimage:                       ; PutImage
  3690. sys_putimage:
  3691.         test    ecx, 0x80008000
  3692.         jnz     .exit
  3693.         test    ecx, 0x0000FFFF
  3694.         jz      .exit
  3695.         test    ecx, 0xFFFF0000
  3696.         jnz     @f
  3697.   .exit:
  3698.         ret
  3699.  @@:
  3700.         mov     edi, [current_slot]
  3701.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3702.         rol     edx, 16
  3703.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3704.         rol     edx, 16
  3705.   .forced:
  3706.         push    ebp esi 0
  3707.         mov     ebp, putimage_get24bpp
  3708.         mov     esi, putimage_init24bpp
  3709. sys_putimage_bpp:
  3710. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  3711. ;        mov     eax, vga_putimage
  3712. ;        cmp     [SCR_MODE], word 0x12
  3713. ;        jz      @f   ;.doit
  3714. ;        mov     eax, vesa12_putimage
  3715. ;        cmp     [SCR_MODE], word 0100000000000000b
  3716. ;        jae     @f
  3717. ;        cmp     [SCR_MODE], word 0x13
  3718. ;        jnz     .doit
  3719. ;@@:
  3720.         mov     eax, vesa20_putimage
  3721. .doit:
  3722.         inc     [mouse_pause]
  3723.         call    eax
  3724.         dec     [mouse_pause]
  3725.         pop     ebp esi ebp
  3726.         jmp     [draw_pointer]
  3727. align 4
  3728. sys_putimage_palette:
  3729. ; ebx = pointer to image
  3730. ; ecx = [xsize]*65536 + [ysize]
  3731. ; edx = [xstart]*65536 + [ystart]
  3732. ; esi = number of bits per pixel, must be 8, 24 or 32
  3733. ; edi = pointer to palette
  3734. ; ebp = row delta
  3735.         mov     eax, [CURRENT_TASK]
  3736.         shl     eax, 8
  3737.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3738.         rol     edx, 16
  3739.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3740.         rol     edx, 16
  3741. .forced:
  3742.         cmp     esi, 1
  3743.         jnz     @f
  3744.         push    edi
  3745.         mov     eax, [edi+4]
  3746.         sub     eax, [edi]
  3747.         push    eax
  3748.         push    dword [edi]
  3749.         push    0ffffff80h
  3750.         mov     edi, esp
  3751.         call    put_mono_image
  3752.         add     esp, 12
  3753.         pop     edi
  3754.         ret
  3755. @@:
  3756.         cmp     esi, 2
  3757.         jnz     @f
  3758.         push    edi
  3759.         push    0ffffff80h
  3760.         mov     edi, esp
  3761.         call    put_2bit_image
  3762.         pop     eax
  3763.         pop     edi
  3764.         ret
  3765. @@:
  3766.         cmp     esi, 4
  3767.         jnz     @f
  3768.         push    edi
  3769.         push    0ffffff80h
  3770.         mov     edi, esp
  3771.         call    put_4bit_image
  3772.         pop     eax
  3773.         pop     edi
  3774.         ret
  3775. @@:
  3776.         push    ebp esi ebp
  3777.         cmp     esi, 8
  3778.         jnz     @f
  3779.         mov     ebp, putimage_get8bpp
  3780.         mov     esi, putimage_init8bpp
  3781.         jmp     sys_putimage_bpp
  3782. @@:
  3783.         cmp     esi, 15
  3784.         jnz     @f
  3785.         mov     ebp, putimage_get15bpp
  3786.         mov     esi, putimage_init15bpp
  3787.         jmp     sys_putimage_bpp
  3788. @@:
  3789.         cmp     esi, 16
  3790.         jnz     @f
  3791.         mov     ebp, putimage_get16bpp
  3792.         mov     esi, putimage_init16bpp
  3793.         jmp     sys_putimage_bpp
  3794. @@:
  3795.         cmp     esi, 24
  3796.         jnz     @f
  3797.         mov     ebp, putimage_get24bpp
  3798.         mov     esi, putimage_init24bpp
  3799.         jmp     sys_putimage_bpp
  3800. @@:
  3801.         cmp     esi, 32
  3802.         jnz     @f
  3803.         mov     ebp, putimage_get32bpp
  3804.         mov     esi, putimage_init32bpp
  3805.         jmp     sys_putimage_bpp
  3806. @@:
  3807.         pop     ebp esi ebp
  3808.         ret
  3809.  
  3810. put_mono_image:
  3811.         push    ebp esi ebp
  3812.         mov     ebp, putimage_get1bpp
  3813.         mov     esi, putimage_init1bpp
  3814.         jmp     sys_putimage_bpp
  3815. put_2bit_image:
  3816.         push    ebp esi ebp
  3817.         mov     ebp, putimage_get2bpp
  3818.         mov     esi, putimage_init2bpp
  3819.         jmp     sys_putimage_bpp
  3820. put_4bit_image:
  3821.         push    ebp esi ebp
  3822.         mov     ebp, putimage_get4bpp
  3823.         mov     esi, putimage_init4bpp
  3824.         jmp     sys_putimage_bpp
  3825.  
  3826. putimage_init24bpp:
  3827.         lea     eax, [eax*3]
  3828. putimage_init8bpp:
  3829.         ret
  3830.  
  3831. align 16
  3832. putimage_get24bpp:
  3833.         movzx   eax, byte [esi+2]
  3834.         shl     eax, 16
  3835.         mov     ax, [esi]
  3836.         add     esi, 3
  3837.         ret     4
  3838. align 16
  3839. putimage_get8bpp:
  3840.         movzx   eax, byte [esi]
  3841.         push    edx
  3842.         mov     edx, [esp+8]
  3843.         mov     eax, [edx+eax*4]
  3844.         pop     edx
  3845.         inc     esi
  3846.         ret     4
  3847.  
  3848. putimage_init1bpp:
  3849.         add     eax, ecx
  3850.         push    ecx
  3851.         add     eax, 7
  3852.         add     ecx, 7
  3853.         shr     eax, 3
  3854.         shr     ecx, 3
  3855.         sub     eax, ecx
  3856.         pop     ecx
  3857.         ret
  3858. align 16
  3859. putimage_get1bpp:
  3860.         push    edx
  3861.         mov     edx, [esp+8]
  3862.         mov     al, [edx]
  3863.         add     al, al
  3864.         jnz     @f
  3865.         lodsb
  3866.         adc     al, al
  3867. @@:
  3868.         mov     [edx], al
  3869.         sbb     eax, eax
  3870.         and     eax, [edx+8]
  3871.         add     eax, [edx+4]
  3872.         pop     edx
  3873.         ret     4
  3874.  
  3875. putimage_init2bpp:
  3876.         add     eax, ecx
  3877.         push    ecx
  3878.         add     ecx, 3
  3879.         add     eax, 3
  3880.         shr     ecx, 2
  3881.         shr     eax, 2
  3882.         sub     eax, ecx
  3883.         pop     ecx
  3884.         ret
  3885. align 16
  3886. putimage_get2bpp:
  3887.         push    edx
  3888.         mov     edx, [esp+8]
  3889.         mov     al, [edx]
  3890.         mov     ah, al
  3891.         shr     al, 6
  3892.         shl     ah, 2
  3893.         jnz     .nonewbyte
  3894.         lodsb
  3895.         mov     ah, al
  3896.         shr     al, 6
  3897.         shl     ah, 2
  3898.         add     ah, 1
  3899. .nonewbyte:
  3900.         mov     [edx], ah
  3901.         mov     edx, [edx+4]
  3902.         movzx   eax, al
  3903.         mov     eax, [edx+eax*4]
  3904.         pop     edx
  3905.         ret     4
  3906.  
  3907. putimage_init4bpp:
  3908.         add     eax, ecx
  3909.         push    ecx
  3910.         add     ecx, 1
  3911.         add     eax, 1
  3912.         shr     ecx, 1
  3913.         shr     eax, 1
  3914.         sub     eax, ecx
  3915.         pop     ecx
  3916.         ret
  3917. align 16
  3918. putimage_get4bpp:
  3919.         push    edx
  3920.         mov     edx, [esp+8]
  3921.         add     byte [edx], 80h
  3922.         jc      @f
  3923.         movzx   eax, byte [edx+1]
  3924.         mov     edx, [edx+4]
  3925.         and     eax, 0x0F
  3926.         mov     eax, [edx+eax*4]
  3927.         pop     edx
  3928.         ret     4
  3929. @@:
  3930.         movzx   eax, byte [esi]
  3931.         add     esi, 1
  3932.         mov     [edx+1], al
  3933.         shr     eax, 4
  3934.         mov     edx, [edx+4]
  3935.         mov     eax, [edx+eax*4]
  3936.         pop     edx
  3937.         ret     4
  3938.  
  3939. putimage_init32bpp:
  3940.         shl     eax, 2
  3941.         ret
  3942. align 16
  3943. putimage_get32bpp:
  3944.         lodsd
  3945.         ret     4
  3946.  
  3947. putimage_init15bpp:
  3948. putimage_init16bpp:
  3949.         add     eax, eax
  3950.         ret
  3951. align 16
  3952. putimage_get15bpp:
  3953. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3954.         push    ecx edx
  3955.         movzx   eax, word [esi]
  3956.         add     esi, 2
  3957.         mov     ecx, eax
  3958.         mov     edx, eax
  3959.         and     eax, 0x1F
  3960.         and     ecx, 0x1F shl 5
  3961.         and     edx, 0x1F shl 10
  3962.         shl     eax, 3
  3963.         shl     ecx, 6
  3964.         shl     edx, 9
  3965.         or      eax, ecx
  3966.         or      eax, edx
  3967.         pop     edx ecx
  3968.         ret     4
  3969.  
  3970. align 16
  3971. putimage_get16bpp:
  3972. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3973.         push    ecx edx
  3974.         movzx   eax, word [esi]
  3975.         add     esi, 2
  3976.         mov     ecx, eax
  3977.         mov     edx, eax
  3978.         and     eax, 0x1F
  3979.         and     ecx, 0x3F shl 5
  3980.         and     edx, 0x1F shl 11
  3981.         shl     eax, 3
  3982.         shl     ecx, 5
  3983.         shl     edx, 8
  3984.         or      eax, ecx
  3985.         or      eax, edx
  3986.         pop     edx ecx
  3987.         ret     4
  3988.  
  3989. ; eax x beginning
  3990. ; ebx y beginning
  3991. ; ecx x end
  3992.         ; edx y end
  3993. ; edi color
  3994.  
  3995. __sys_drawbar:
  3996.         mov     esi, [current_slot]
  3997.         add     eax, [esi+APPDATA.wnd_clientbox.left]
  3998.         add     ecx, [esi+APPDATA.wnd_clientbox.left]
  3999.         add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4000.         add     edx, [esi+APPDATA.wnd_clientbox.top]
  4001.   .forced:
  4002.         inc     [mouse_pause]
  4003. ;        call    [disable_mouse]
  4004. ;        cmp     [SCR_MODE], word 0x12
  4005. ;        je      dbv20
  4006. ;   sdbv20:
  4007. ;        cmp     [SCR_MODE], word 0100000000000000b
  4008. ;        jge     dbv20
  4009. ;        cmp     [SCR_MODE], word 0x13
  4010. ;        je      dbv20
  4011. ;        call    vesa12_drawbar
  4012. ;        dec     [mouse_pause]
  4013. ;        call    [draw_pointer]
  4014. ;        ret
  4015. ;  dbv20:
  4016.         call    vesa20_drawbar
  4017.         dec     [mouse_pause]
  4018.         call    [draw_pointer]
  4019.         ret
  4020.  
  4021.  
  4022.  
  4023. kb_read:
  4024.  
  4025.         push    ecx edx
  4026.  
  4027.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4028.       kr_loop:
  4029.         in      al, 0x64
  4030.         test    al, 1
  4031.         jnz     kr_ready
  4032.         loop    kr_loop
  4033.         mov     ah, 1
  4034.         jmp     kr_exit
  4035.       kr_ready:
  4036.         push    ecx
  4037.         mov     ecx, 32
  4038.       kr_delay:
  4039.         loop    kr_delay
  4040.         pop     ecx
  4041.         in      al, 0x60
  4042.         xor     ah, ah
  4043.       kr_exit:
  4044.  
  4045.         pop     edx ecx
  4046.  
  4047.         ret
  4048.  
  4049.  
  4050. kb_write:
  4051.  
  4052.         push    ecx edx
  4053.  
  4054.         mov     dl, al
  4055. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4056. ;      kw_loop1:
  4057. ;        in      al,0x64
  4058. ;        test    al,0x20
  4059. ;        jz      kw_ok1
  4060. ;        loop    kw_loop1
  4061. ;        mov     ah,1
  4062. ;        jmp     kw_exit
  4063. ;      kw_ok1:
  4064.         in      al, 0x60
  4065.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4066.       kw_loop:
  4067.         in      al, 0x64
  4068.         test    al, 2
  4069.         jz      kw_ok
  4070.         loop    kw_loop
  4071.         mov     ah, 1
  4072.         jmp     kw_exit
  4073.       kw_ok:
  4074.         mov     al, dl
  4075.         out     0x60, al
  4076.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4077.       kw_loop3:
  4078.         in      al, 0x64
  4079.         test    al, 2
  4080.         jz      kw_ok3
  4081.         loop    kw_loop3
  4082.         mov     ah, 1
  4083.         jmp     kw_exit
  4084.       kw_ok3:
  4085.         mov     ah, 8
  4086.       kw_loop4:
  4087.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4088.       kw_loop5:
  4089.         in      al, 0x64
  4090.         test    al, 1
  4091.         jnz     kw_ok4
  4092.         loop    kw_loop5
  4093.         dec     ah
  4094.         jnz     kw_loop4
  4095.       kw_ok4:
  4096.         xor     ah, ah
  4097.       kw_exit:
  4098.  
  4099.         pop     edx ecx
  4100.  
  4101.         ret
  4102.  
  4103.  
  4104. kb_cmd:
  4105.  
  4106.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4107.       c_wait:
  4108.         in      al, 0x64
  4109.         test    al, 2
  4110.         jz      c_send
  4111.         loop    c_wait
  4112.         jmp     c_error
  4113.       c_send:
  4114.         mov     al, bl
  4115.         out     0x64, al
  4116.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4117.       c_accept:
  4118.         in      al, 0x64
  4119.         test    al, 2
  4120.         jz      c_ok
  4121.         loop    c_accept
  4122.       c_error:
  4123.         mov     ah, 1
  4124.         jmp     c_exit
  4125.       c_ok:
  4126.         xor     ah, ah
  4127.       c_exit:
  4128.         ret
  4129.  
  4130.  
  4131. setmouse:  ; set mousepicture -pointer
  4132.            ; ps2 mouse enable
  4133.  
  4134.         mov     [MOUSE_PICTURE], dword mousepointer
  4135.  
  4136.         cli
  4137.  
  4138.         ret
  4139.  
  4140. if used _rdtsc
  4141. _rdtsc:
  4142.         bt      [cpu_caps], CAPS_TSC
  4143.         jnc     ret_rdtsc
  4144.         rdtsc
  4145.         ret
  4146.    ret_rdtsc:
  4147.         mov     edx, 0xffffffff
  4148.         mov     eax, 0xffffffff
  4149.         ret
  4150. end if
  4151.  
  4152. sys_msg_board_str:
  4153.  
  4154.         pushad
  4155.    @@:
  4156.         cmp     [esi], byte 0
  4157.         je      @f
  4158.         mov     eax, 1
  4159.         movzx   ebx, byte [esi]
  4160.         call    sys_msg_board
  4161.         inc     esi
  4162.         jmp     @b
  4163.    @@:
  4164.         popad
  4165.         ret
  4166.  
  4167. sys_msg_board_byte:
  4168. ; in: al = byte to display
  4169. ; out: nothing
  4170. ; destroys: nothing
  4171.         pushad
  4172.         mov     ecx, 2
  4173.         shl     eax, 24
  4174.         jmp     @f
  4175.  
  4176. sys_msg_board_word:
  4177. ; in: ax = word to display
  4178. ; out: nothing
  4179. ; destroys: nothing
  4180.         pushad
  4181.         mov     ecx, 4
  4182.         shl     eax, 16
  4183.         jmp     @f
  4184.  
  4185. sys_msg_board_dword:
  4186. ; in: eax = dword to display
  4187. ; out: nothing
  4188. ; destroys: nothing
  4189.         pushad
  4190.         mov     ecx, 8
  4191. @@:
  4192.         push    ecx
  4193.         rol     eax, 4
  4194.         push    eax
  4195.         and     al, 0xF
  4196.         cmp     al, 10
  4197.         sbb     al, 69h
  4198.         das
  4199.         mov     bl, al
  4200.         xor     eax, eax
  4201.         inc     eax
  4202.         call    sys_msg_board
  4203.         pop     eax
  4204.         pop     ecx
  4205.         loop    @b
  4206.         popad
  4207.         ret
  4208.  
  4209. uglobal
  4210.   msg_board_data:
  4211.                   times 4096 db 0
  4212.   msg_board_count dd 0x0
  4213. endg
  4214.  
  4215. sys_msg_board:
  4216.  
  4217. ; eax=1 : write :  bl byte to write
  4218. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4219.  
  4220.         mov     ecx, [msg_board_count]
  4221.         cmp     eax, 1
  4222.         jne     .smbl1
  4223.  
  4224. if defined debug_com_base
  4225.  
  4226.         push    dx ax
  4227.  
  4228.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4229.         mov     dx, debug_com_base+5
  4230.         in      al, dx
  4231.         test    al, 1 shl 5
  4232.         jz      @r
  4233.  
  4234.         mov     dx, debug_com_base      ; Output the byte
  4235.         mov     al, bl
  4236.         out     dx, al
  4237.  
  4238.         pop     ax dx
  4239.  
  4240. end if
  4241.  
  4242.         mov     [msg_board_data+ecx], bl
  4243.         inc     ecx
  4244.         and     ecx, 4095
  4245.         mov     [msg_board_count], ecx
  4246.         mov     [check_idle_semaphore], 5
  4247.         ret
  4248. .smbl1:
  4249.         cmp     eax, 2
  4250.         jne     .smbl2
  4251.         test    ecx, ecx
  4252.         jz      .smbl21
  4253.         mov     eax, msg_board_data+1
  4254.         mov     ebx, msg_board_data
  4255.         movzx   edx, byte [ebx]
  4256.         call    memmove
  4257.         dec     [msg_board_count]
  4258.         mov     [esp + 36], edx ;eax
  4259.         mov     [esp + 24], dword 1
  4260.         ret
  4261. .smbl21:
  4262.         mov     [esp+36], ecx
  4263.         mov     [esp+24], ecx
  4264. .smbl2:
  4265.         ret
  4266.  
  4267. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4268. ;; 66 sys function.                                                ;;
  4269. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4270. ;; out eax                                                         ;;
  4271. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4272. iglobal
  4273. align 4
  4274. f66call:
  4275.            dd sys_process_def.1   ; 1 = set keyboard mode
  4276.            dd sys_process_def.2   ; 2 = get keyboard mode
  4277.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4278.            dd sys_process_def.4
  4279.            dd sys_process_def.5
  4280. endg
  4281.  
  4282.  
  4283.  
  4284.  
  4285. sys_process_def:
  4286.         dec     ebx
  4287.         cmp     ebx, 5
  4288.         jae     .not_support    ;if >=6 then or eax,-1
  4289.  
  4290.         mov     edi, [CURRENT_TASK]
  4291.         jmp     dword [f66call+ebx*4]
  4292.  
  4293. .not_support:
  4294.         or      eax, -1
  4295.         ret
  4296.  
  4297. .1:
  4298.         shl     edi, 8
  4299.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4300.  
  4301.         ret
  4302.  
  4303. .2:                             ; 2 = get keyboard mode
  4304.         shl     edi, 8
  4305.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4306.         mov     [esp+32], eax
  4307.         ret
  4308. ;     xor   eax,eax
  4309. ;     movzx eax,byte [shift]
  4310. ;     movzx ebx,byte [ctrl]
  4311. ;     shl   ebx,2
  4312. ;     add   eax,ebx
  4313. ;     movzx ebx,byte [alt]
  4314. ;     shl   ebx,3
  4315. ;     add   eax,ebx
  4316. .3:                             ;3 = get keyboard ctrl, alt, shift
  4317.  ;// mike.dld [
  4318.         mov     eax, [kb_state]
  4319.  ;// mike.dld ]
  4320.         mov     [esp+32], eax
  4321.         ret
  4322.  
  4323. .4:
  4324.         mov     eax, hotkey_list
  4325. @@:
  4326.         cmp     dword [eax+8], 0
  4327.         jz      .found_free
  4328.         add     eax, 16
  4329.         cmp     eax, hotkey_list+16*256
  4330.         jb      @b
  4331.         mov     dword [esp+32], 1
  4332.         ret
  4333. .found_free:
  4334.         mov     [eax+8], edi
  4335.         mov     [eax+4], edx
  4336.         movzx   ecx, cl
  4337.         lea     ecx, [hotkey_scancodes+ecx*4]
  4338.         mov     edx, [ecx]
  4339.         mov     [eax], edx
  4340.         mov     [ecx], eax
  4341.         mov     [eax+12], ecx
  4342.         jecxz   @f
  4343.         mov     [edx+12], eax
  4344. @@:
  4345.         and     dword [esp+32], 0
  4346.         ret
  4347.  
  4348. .5:
  4349.         movzx   ebx, cl
  4350.         lea     ebx, [hotkey_scancodes+ebx*4]
  4351.         mov     eax, [ebx]
  4352. .scan:
  4353.         test    eax, eax
  4354.         jz      .notfound
  4355.         cmp     [eax+8], edi
  4356.         jnz     .next
  4357.         cmp     [eax+4], edx
  4358.         jz      .found
  4359. .next:
  4360.         mov     eax, [eax]
  4361.         jmp     .scan
  4362. .notfound:
  4363.         mov     dword [esp+32], 1
  4364.         ret
  4365. .found:
  4366.         mov     ecx, [eax]
  4367.         jecxz   @f
  4368.         mov     edx, [eax+12]
  4369.         mov     [ecx+12], edx
  4370. @@:
  4371.         mov     ecx, [eax+12]
  4372.         mov     edx, [eax]
  4373.         mov     [ecx], edx
  4374.         xor     edx, edx
  4375.         mov     [eax+4], edx
  4376.         mov     [eax+8], edx
  4377.         mov     [eax+12], edx
  4378.         mov     [eax], edx
  4379.         mov     [esp+32], edx
  4380.         ret
  4381.  
  4382.  
  4383. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4384. ;; 61 sys function.                                                ;;
  4385. ;; in eax=61,ebx in [1..3]                                         ;;
  4386. ;; out eax                                                         ;;
  4387. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4388. iglobal
  4389. align 4
  4390. f61call:
  4391.            dd sys_gs.1   ; resolution
  4392.            dd sys_gs.2   ; bits per pixel
  4393.            dd sys_gs.3   ; bytes per scanline
  4394. endg
  4395.  
  4396.  
  4397. align 4
  4398.  
  4399. sys_gs:                         ; direct screen access
  4400.         dec     ebx
  4401.         cmp     ebx, 2
  4402.         ja      .not_support
  4403.         jmp     dword [f61call+ebx*4]
  4404. .not_support:
  4405.         or      [esp+32], dword -1
  4406.         ret
  4407.  
  4408.  
  4409. .1:                             ; resolution
  4410.         mov     eax, [Screen_Max_X]
  4411.         shl     eax, 16
  4412.         mov     ax, [Screen_Max_Y]
  4413.         add     eax, 0x00010001
  4414.         mov     [esp+32], eax
  4415.         ret
  4416. .2:                             ; bits per pixel
  4417.         movzx   eax, byte [ScreenBPP]
  4418.         mov     [esp+32], eax
  4419.         ret
  4420. .3:                             ; bytes per scanline
  4421.         mov     eax, [BytesPerScanLine]
  4422.         mov     [esp+32], eax
  4423.         ret
  4424.  
  4425. align 4  ;  system functions
  4426.  
  4427. syscall_setpixel:                       ; SetPixel
  4428.  
  4429.         mov     eax, ebx
  4430.         mov     ebx, ecx
  4431.         mov     ecx, edx
  4432.         mov     edx, [TASK_BASE]
  4433.         add     eax, [edx-twdw+WDATA.box.left]
  4434.         add     ebx, [edx-twdw+WDATA.box.top]
  4435.         mov     edi, [current_slot]
  4436.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4437.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4438.         xor     edi, edi ; no force
  4439. ;       mov     edi, 1
  4440.         call    [_display.disable_mouse]
  4441.         jmp     [putpixel]
  4442.  
  4443. align 4
  4444.  
  4445. syscall_writetext:                      ; WriteText
  4446.  
  4447.         mov     eax, [TASK_BASE]
  4448.         mov     ebp, [eax-twdw+WDATA.box.left]
  4449.         push    esi
  4450.         mov     esi, [current_slot]
  4451.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4452.         shl     ebp, 16
  4453.         add     ebp, [eax-twdw+WDATA.box.top]
  4454.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  4455.         pop     esi
  4456.         add     ebx, ebp
  4457.         mov     eax, edi
  4458.         xor     edi, edi
  4459.         jmp     dtext
  4460.  
  4461. align 4
  4462.  
  4463. syscall_openramdiskfile:                ; OpenRamdiskFile
  4464.  
  4465.         mov     eax, ebx
  4466.         mov     ebx, ecx
  4467.         mov     ecx, edx
  4468.         mov     edx, esi
  4469.         mov     esi, 12
  4470.         call    fileread
  4471.         mov     [esp+32], eax
  4472.         ret
  4473.  
  4474. align 4
  4475.  
  4476. syscall_drawrect:                       ; DrawRect
  4477.  
  4478.         mov     edi, edx ; color + gradient
  4479.         and     edi, 0x80FFFFFF
  4480.         test    bx, bx  ; x.size
  4481.         je      .drectr
  4482.         test    cx, cx ; y.size
  4483.         je      .drectr
  4484.  
  4485.         mov     eax, ebx ; bad idea
  4486.         mov     ebx, ecx
  4487.  
  4488.         movzx   ecx, ax ; ecx - x.size
  4489.         shr     eax, 16 ; eax - x.coord
  4490.         movzx   edx, bx ; edx - y.size
  4491.         shr     ebx, 16 ; ebx - y.coord
  4492.         mov     esi, [current_slot]
  4493.  
  4494.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4495.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4496.         add     ecx, eax
  4497.         add     edx, ebx
  4498.         jmp     [drawbar]
  4499. .drectr:
  4500.         ret
  4501.  
  4502. align 4
  4503. syscall_getscreensize:                  ; GetScreenSize
  4504.         mov     ax, [Screen_Max_X]
  4505.         shl     eax, 16
  4506.         mov     ax, [Screen_Max_Y]
  4507.         mov     [esp + 32], eax
  4508.         ret
  4509.  
  4510. align 4
  4511.  
  4512. syscall_cdaudio:                        ; CD
  4513.  
  4514.         cmp     ebx, 4
  4515.         jb      .audio
  4516.         jz      .eject
  4517.         cmp     ebx, 5
  4518.         jnz     .ret
  4519. .load:
  4520.         call    .reserve
  4521.         call    LoadMedium
  4522.         ;call    .free
  4523.         jmp     .free
  4524. ;        ret
  4525. .eject:
  4526.         call    .reserve
  4527.         call    clear_CD_cache
  4528.         call    allow_medium_removal
  4529.         call    EjectMedium
  4530. ;        call    .free
  4531.         jmp     .free
  4532. ;        ret
  4533. .audio:
  4534.         call    sys_cd_audio
  4535.         mov     [esp+36-4], eax
  4536. .ret:
  4537.         ret
  4538.  
  4539. .reserve:
  4540.         call    reserve_cd
  4541.         mov     eax, ecx
  4542.         shr     eax, 1
  4543.         and     eax, 1
  4544.         inc     eax
  4545.         mov     [ChannelNumber], ax
  4546.         mov     eax, ecx
  4547.         and     eax, 1
  4548.         mov     [DiskNumber], al
  4549.         call    reserve_cd_channel
  4550.         and     ebx, 3
  4551.         inc     ebx
  4552.         mov     [cdpos], ebx
  4553.         add     ebx, ebx
  4554.         mov     cl, 8
  4555.         sub     cl, bl
  4556.         mov     al, [DRIVE_DATA+1]
  4557.         shr     al, cl
  4558.         test    al, 2
  4559.         jz      .free;.err
  4560.         ret
  4561. .free:
  4562.         call    free_cd_channel
  4563.         and     [cd_status], 0
  4564.         ret
  4565. .err:
  4566.         call    .free
  4567. ;        pop     eax
  4568.         ret
  4569.  
  4570. align 4
  4571.  
  4572. syscall_getpixel:                       ; GetPixel
  4573.         mov     ecx, [Screen_Max_X]
  4574.         inc     ecx
  4575.         xor     edx, edx
  4576.         mov     eax, ebx
  4577.         div     ecx
  4578.         mov     ebx, edx
  4579.         xchg    eax, ebx
  4580.         call    dword [GETPIXEL]; eax - x, ebx - y
  4581.         mov     [esp + 32], ecx
  4582.         ret
  4583.  
  4584. align 4
  4585.  
  4586. syscall_getarea:
  4587. ;eax = 36
  4588. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4589. ;ecx = [size x]*65536 + [size y]
  4590. ;edx = [start x]*65536 + [start y]
  4591.         pushad
  4592.         inc     [mouse_pause]
  4593. ; Check of use of the hardware cursor.
  4594.         cmp     [_display.disable_mouse], __sys_disable_mouse
  4595.         jne     @f
  4596. ; Since the test for the coordinates of the mouse should not be used,
  4597. ; then use the call [disable_mouse] is not possible!
  4598.         cmp     dword [MOUSE_VISIBLE], dword 0
  4599.         jne     @f
  4600.         pushf
  4601.         cli
  4602.         call    draw_mouse_under
  4603.         popf
  4604.         mov     [MOUSE_VISIBLE], dword 1
  4605. @@:
  4606.         mov     edi, ebx
  4607.         mov     eax, edx
  4608.         shr     eax, 16
  4609.         mov     ebx, edx
  4610.         and     ebx, 0xffff
  4611.         dec     eax
  4612.         dec     ebx
  4613.      ; eax - x, ebx - y
  4614.         mov     edx, ecx
  4615.  
  4616.         shr     ecx, 16
  4617.         and     edx, 0xffff
  4618.         mov     esi, ecx
  4619.      ; ecx - size x, edx - size y
  4620.  
  4621.         mov     ebp, edx
  4622.         dec     ebp
  4623.         lea     ebp, [ebp*3]
  4624.  
  4625.         imul    ebp, esi
  4626.  
  4627.         mov     esi, ecx
  4628.         dec     esi
  4629.         lea     esi, [esi*3]
  4630.  
  4631.         add     ebp, esi
  4632.         add     ebp, edi
  4633.  
  4634.         add     ebx, edx
  4635.  
  4636. .start_y:
  4637.         push    ecx edx
  4638. .start_x:
  4639.         push    eax ebx ecx
  4640.         add     eax, ecx
  4641.  
  4642.         call    dword [GETPIXEL]; eax - x, ebx - y
  4643.  
  4644.         mov     [ebp], cx
  4645.         shr     ecx, 16
  4646.         mov     [ebp+2], cl
  4647.  
  4648.         pop     ecx ebx eax
  4649.         sub     ebp, 3
  4650.         dec     ecx
  4651.         jnz     .start_x
  4652.         pop     edx ecx
  4653.         dec     ebx
  4654.         dec     edx
  4655.         jnz     .start_y
  4656.         dec     [mouse_pause]
  4657. ; Check of use of the hardware cursor.
  4658.         cmp     [_display.disable_mouse], __sys_disable_mouse
  4659.         jne     @f
  4660.         call    [draw_pointer]
  4661. @@:
  4662.         popad
  4663.         ret
  4664.  
  4665. align 4
  4666.  
  4667. syscall_drawline:                       ; DrawLine
  4668.  
  4669.         mov     edi, [TASK_BASE]
  4670.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4671.         mov     ebp, eax
  4672.         mov     esi, [current_slot]
  4673.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4674.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4675.         add     ebp, ebx
  4676.         shl     eax, 16
  4677.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4678.         add     eax, ebp
  4679.         mov     ebp, ebx
  4680.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4681.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4682.         add     ebp, ecx
  4683.         shl     ebx, 16
  4684.         xor     edi, edi
  4685.         add     ebx, ebp
  4686.         mov     ecx, edx
  4687.         jmp     [draw_line]
  4688.  
  4689.  
  4690.  
  4691. align 4
  4692. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4693.  
  4694.         call    r_f_port_area
  4695.         mov     [esp+32], eax
  4696.         ret
  4697.  
  4698. align 4
  4699. syscall_threads:                        ; CreateThreads
  4700. ; eax=1 create thread
  4701. ;
  4702. ;   ebx=thread start
  4703. ;   ecx=thread stack value
  4704. ;
  4705. ; on return : eax = pid
  4706.  
  4707.         call    new_sys_threads
  4708.  
  4709.         mov     [esp+32], eax
  4710.         ret
  4711.  
  4712. align 4
  4713.  
  4714. stack_driver_stat:
  4715.  
  4716.         call    app_stack_handler       ; Stack status
  4717.  
  4718. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4719. ;     call  change_task                 ; between sent packet
  4720.  
  4721.         mov     [esp+32], eax
  4722.         ret
  4723.  
  4724. align 4
  4725.  
  4726. socket:                                 ; Socket interface
  4727.         call    app_socket_handler
  4728.  
  4729. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4730. ;     call  change_task                 ; between sent packet
  4731.  
  4732.         mov     [esp+36], eax
  4733.         mov     [esp+24], ebx
  4734.         ret
  4735.  
  4736. align 4
  4737.  
  4738. read_from_hd:                           ; Read from hd - fn not in use
  4739.  
  4740.         mov     edi, [TASK_BASE]
  4741.         add     edi, TASKDATA.mem_start
  4742.         add     eax, [edi]
  4743.         add     ecx, [edi]
  4744.         add     edx, [edi]
  4745.         call    file_read
  4746.  
  4747.         mov     [esp+36], eax
  4748.         mov     [esp+24], ebx
  4749.  
  4750.         ret
  4751.  
  4752. paleholder:
  4753.         ret
  4754.  
  4755. align 4
  4756. set_screen:
  4757.         cmp     eax, [Screen_Max_X]
  4758.         jne     .set
  4759.  
  4760.         cmp     edx, [Screen_Max_Y]
  4761.         jne     .set
  4762.         ret
  4763. .set:
  4764.         pushfd
  4765.         cli
  4766.  
  4767.         mov     [Screen_Max_X], eax
  4768.         mov     [Screen_Max_Y], edx
  4769.         mov     [BytesPerScanLine], ecx
  4770.  
  4771.         mov     [screen_workarea.right], eax
  4772.         mov     [screen_workarea.bottom], edx
  4773.  
  4774.         push    ebx
  4775.         push    esi
  4776.         push    edi
  4777.  
  4778.         pushad
  4779.  
  4780.         stdcall kernel_free, [_WinMapAddress]
  4781.  
  4782.         mov     eax, [_display.width]
  4783.         mul     [_display.height]
  4784.         mov     [_WinMapSize], eax
  4785.  
  4786.         stdcall kernel_alloc, eax
  4787.         mov     [_WinMapAddress], eax
  4788.         test    eax, eax
  4789.         jz      .epic_fail
  4790.  
  4791.         popad
  4792.  
  4793.         call    repos_windows
  4794.         xor     eax, eax
  4795.         xor     ebx, ebx
  4796.         mov     ecx, [Screen_Max_X]
  4797.         mov     edx, [Screen_Max_Y]
  4798.         call    calculatescreen
  4799.         pop     edi
  4800.         pop     esi
  4801.         pop     ebx
  4802.  
  4803.         popfd
  4804.         ret
  4805.  
  4806. .epic_fail:
  4807.         hlt                     ; Houston, we've had a problem
  4808.  
  4809. ; --------------- APM ---------------------
  4810. uglobal
  4811. apm_entry       dp      0
  4812. apm_vf          dd      0
  4813. endg
  4814.  
  4815. align 4
  4816. sys_apm:
  4817.         xor     eax, eax
  4818.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4819.         jne     @f
  4820.         inc     eax
  4821.         or      dword [esp + 44], eax   ; error
  4822.         add     eax, 7
  4823.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4824.         ret
  4825.  
  4826. @@:
  4827. ;       xchg    eax, ecx
  4828. ;       xchg    ebx, ecx
  4829.  
  4830.         cmp     dx, 3
  4831.         ja      @f
  4832.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4833.         mov     eax, [apm_vf]
  4834.         mov     [esp + 32], eax
  4835.         shr     eax, 16
  4836.         mov     [esp + 28], eax
  4837.         ret
  4838.  
  4839. @@:
  4840.  
  4841.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4842.         xchg    [master_tab], esi
  4843.         push    esi
  4844.         mov     edi, cr3
  4845.         mov     cr3, edi                ;flush TLB
  4846.  
  4847.         call    pword [apm_entry]       ;call APM BIOS
  4848.  
  4849.         xchg    eax, [esp]
  4850.         mov     [master_tab], eax
  4851.         mov     eax, cr3
  4852.         mov     cr3, eax
  4853.         pop     eax
  4854.  
  4855.         mov     [esp + 4 ], edi
  4856.         mov     [esp + 8], esi
  4857.         mov     [esp + 20], ebx
  4858.         mov     [esp + 24], edx
  4859.         mov     [esp + 28], ecx
  4860.         mov     [esp + 32], eax
  4861.         setc    al
  4862.         and     [esp + 44], byte 0xfe
  4863.         or      [esp + 44], al
  4864.         ret
  4865. ; -----------------------------------------
  4866.  
  4867. align 4
  4868.  
  4869. undefined_syscall:                      ; Undefined system call
  4870.         mov     [esp + 32], dword -1
  4871.         ret
  4872.  
  4873. align 4
  4874. system_shutdown:          ; shut down the system
  4875.  
  4876.         cmp     byte [BOOT_VAR+0x9030], 1
  4877.         jne     @F
  4878.         ret
  4879. @@:
  4880.         call    stop_all_services
  4881.         push    3                ; stop playing cd
  4882.         pop     eax
  4883.         call    sys_cd_audio
  4884.  
  4885. yes_shutdown_param:
  4886.         cli
  4887.  
  4888. if ~ defined extended_primary_loader
  4889.         mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
  4890.         push    12
  4891.         pop     esi
  4892.         xor     ebx, ebx
  4893.         or      ecx, -1
  4894.         mov     edx, OS_BASE+0x70000
  4895.         call    fileread
  4896.  
  4897.         mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  4898.         mov     edi, OS_BASE+0x40000
  4899.         mov     ecx, 1000
  4900.         rep movsb
  4901. end if
  4902.  
  4903.         mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
  4904.         mov     edi, OS_BASE
  4905.         mov     ecx, 0x10000/4
  4906.         cld
  4907.         rep movsd
  4908.  
  4909.         call    restorefatchain
  4910.  
  4911.         call    IRQ_mask_all
  4912.  
  4913. if 0
  4914.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  4915.         mov     word [OS_BASE+0x467+2], 0x1000
  4916.  
  4917.         mov     al, 0x0F
  4918.         out     0x70, al
  4919.         mov     al, 0x05
  4920.         out     0x71, al
  4921.  
  4922.         mov     al, 0xFE
  4923.         out     0x64, al
  4924.  
  4925.         hlt
  4926.         jmp     $-1
  4927.  
  4928. else
  4929.         cmp     byte [OS_BASE + 0x9030], 2
  4930.         jnz     no_acpi_power_off
  4931.  
  4932. ; scan for RSDP
  4933. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  4934.         movzx   eax, word [OS_BASE + 0x40E]
  4935.         shl     eax, 4
  4936.         jz      @f
  4937.         mov     ecx, 1024/16
  4938.         call    scan_rsdp
  4939.         jnc     .rsdp_found
  4940. @@:
  4941. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  4942.         mov     eax, 0xE0000
  4943.         mov     ecx, 0x2000
  4944.         call    scan_rsdp
  4945.         jc      no_acpi_power_off
  4946. .rsdp_found:
  4947.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  4948.         mov     ebp, [ipc_tmp]
  4949.         stdcall map_page, ebp, esi, PG_MAP
  4950.         lea     eax, [esi+1000h]
  4951.         lea     edx, [ebp+1000h]
  4952.         stdcall map_page, edx, eax, PG_MAP
  4953.         and     esi, 0xFFF
  4954.         add     esi, ebp
  4955.         cmp     dword [esi], 'RSDT'
  4956.         jnz     no_acpi_power_off
  4957.         mov     ecx, [esi+4]
  4958.         sub     ecx, 24h
  4959.         jbe     no_acpi_power_off
  4960.         shr     ecx, 2
  4961.         add     esi, 24h
  4962. .scan_fadt:
  4963.         lodsd
  4964.         mov     ebx, eax
  4965.         lea     eax, [ebp+2000h]
  4966.         stdcall map_page, eax, ebx, PG_MAP
  4967.         lea     eax, [ebp+3000h]
  4968.         add     ebx, 0x1000
  4969.         stdcall map_page, eax, ebx, PG_MAP
  4970.         and     ebx, 0xFFF
  4971.         lea     ebx, [ebx+ebp+2000h]
  4972.         cmp     dword [ebx], 'FACP'
  4973.         jz      .fadt_found
  4974.         loop    .scan_fadt
  4975.         jmp     no_acpi_power_off
  4976. .fadt_found:
  4977. ; ebx is linear address of FADT
  4978.         mov     edi, [ebx+40] ; physical address of the DSDT
  4979.         lea     eax, [ebp+4000h]
  4980.         stdcall map_page, eax, edi, PG_MAP
  4981.         lea     eax, [ebp+5000h]
  4982.         lea     esi, [edi+0x1000]
  4983.         stdcall map_page, eax, esi, PG_MAP
  4984.         and     esi, 0xFFF
  4985.         sub     edi, esi
  4986.         cmp     dword [esi+ebp+4000h], 'DSDT'
  4987.         jnz     no_acpi_power_off
  4988.         mov     eax, [esi+ebp+4004h] ; DSDT length
  4989.         sub     eax, 36+4
  4990.         jbe     no_acpi_power_off
  4991.         add     esi, 36
  4992. .scan_dsdt:
  4993.         cmp     dword [esi+ebp+4000h], '_S5_'
  4994.         jnz     .scan_dsdt_cont
  4995.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  4996.         jnz     .scan_dsdt_cont
  4997.         mov     dl, [esi+ebp+4000h+6]
  4998.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  4999.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5000.         ja      .scan_dsdt_cont
  5001.         cmp     dl, 1
  5002.         jb      .scan_dsdt_cont
  5003.         lea     esi, [esi+ebp+4000h+7]
  5004.         xor     ecx, ecx
  5005.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5006.         jz      @f
  5007.         cmp     byte [esi], 0xA
  5008.         jnz     no_acpi_power_off
  5009.         inc     esi
  5010.         mov     cl, [esi]
  5011. @@:
  5012.         inc     esi
  5013.         cmp     dl, 2
  5014.         jb      @f
  5015.         cmp     byte [esi], 0
  5016.         jz      @f
  5017.         cmp     byte [esi], 0xA
  5018.         jnz     no_acpi_power_off
  5019.         inc     esi
  5020.         mov     ch, [esi]
  5021. @@:
  5022.         jmp     do_acpi_power_off
  5023. .scan_dsdt_cont:
  5024.         inc     esi
  5025.         cmp     esi, 0x1000
  5026.         jb      @f
  5027.         sub     esi, 0x1000
  5028.         add     edi, 0x1000
  5029.         push    eax
  5030.         lea     eax, [ebp+4000h]
  5031.         stdcall map_page, eax, edi, PG_MAP
  5032.         push    PG_MAP
  5033.         lea     eax, [edi+1000h]
  5034.         push    eax
  5035.         lea     eax, [ebp+5000h]
  5036.         push    eax
  5037.         stdcall map_page
  5038.         pop     eax
  5039. @@:
  5040.         dec     eax
  5041.         jnz     .scan_dsdt
  5042.         jmp     no_acpi_power_off
  5043. do_acpi_power_off:
  5044.         mov     edx, [ebx+48]
  5045.         test    edx, edx
  5046.         jz      .nosmi
  5047.         mov     al, [ebx+52]
  5048.         out     dx, al
  5049.         mov     edx, [ebx+64]
  5050. @@:
  5051.         in      ax, dx
  5052.         test    al, 1
  5053.         jz      @b
  5054. .nosmi:
  5055.         and     cx, 0x0707
  5056.         shl     cx, 2
  5057.         or      cx, 0x2020
  5058.         mov     edx, [ebx+64]
  5059.         in      ax, dx
  5060.         and     ax, 203h
  5061.         or      ah, cl
  5062.         out     dx, ax
  5063.         mov     edx, [ebx+68]
  5064.         test    edx, edx
  5065.         jz      @f
  5066.         in      ax, dx
  5067.         and     ax, 203h
  5068.         or      ah, ch
  5069.         out     dx, ax
  5070. @@:
  5071.         jmp     $
  5072.  
  5073.  
  5074. no_acpi_power_off:
  5075.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5076.         mov     word [OS_BASE+0x467+2], 0x1000
  5077.  
  5078.         mov     al, 0x0F
  5079.         out     0x70, al
  5080.         mov     al, 0x05
  5081.         out     0x71, al
  5082.  
  5083.         mov     al, 0xFE
  5084.         out     0x64, al
  5085.  
  5086.         hlt
  5087.         jmp     $-1
  5088.  
  5089. scan_rsdp:
  5090.         add     eax, OS_BASE
  5091. .s:
  5092.         cmp     dword [eax], 'RSD '
  5093.         jnz     .n
  5094.         cmp     dword [eax+4], 'PTR '
  5095.         jnz     .n
  5096.         xor     edx, edx
  5097.         xor     esi, esi
  5098. @@:
  5099.         add     dl, [eax+esi]
  5100.         inc     esi
  5101.         cmp     esi, 20
  5102.         jnz     @b
  5103.         test    dl, dl
  5104.         jz      .ok
  5105. .n:
  5106.         add     eax, 10h
  5107.         loop    .s
  5108.         stc
  5109. .ok:
  5110.         ret
  5111. end if
  5112.  
  5113. include "data32.inc"
  5114.  
  5115. __REV__ = __REV
  5116.  
  5117. uglobals_size = $ - endofcode
  5118. diff16 "end of kernel code",0,$
  5119.