Subversion Repositories Kolibri OS

Rev

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