Subversion Repositories Kolibri OS

Rev

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