Subversion Repositories Kolibri OS

Rev

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