Subversion Repositories Kolibri OS

Rev

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