Subversion Repositories Kolibri OS

Rev

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