Subversion Repositories Kolibri OS

Rev

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

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