Subversion Repositories Kolibri OS

Rev

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