Subversion Repositories Kolibri OS

Rev

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