Subversion Repositories Kolibri OS

Rev

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

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