Subversion Repositories Kolibri OS

Rev

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

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