Subversion Repositories Kolibri OS

Rev

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

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