Subversion Repositories Kolibri OS

Rev

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