Subversion Repositories Kolibri OS

Rev

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