Subversion Repositories Kolibri OS

Rev

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

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