Subversion Repositories Kolibri OS

Rev

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