Subversion Repositories Kolibri OS

Rev

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

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