Subversion Repositories Kolibri OS

Rev

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