Subversion Repositories Kolibri OS

Rev

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

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