Subversion Repositories Kolibri OS

Rev

Rev 2464 | Rev 2468 | 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: 2466 $
  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.         mov     eax, [def_cursor]
  1953.         mov     [ecx+APPDATA.cursor], eax
  1954.  
  1955.         movzx   eax, word [MOUSE_Y]
  1956.         movzx   ebx, word [MOUSE_X]
  1957. ;        mov     ecx, [Screen_Max_X]
  1958. ;        inc     ecx
  1959. ;        mul     ecx
  1960.         mov     eax, [d_width_calc_area + eax*4]
  1961.  
  1962.         add     eax, [_WinMapAddress]
  1963.         movzx   edx, byte [ebx+eax]
  1964.         shl     edx, 8
  1965.         mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
  1966.  
  1967.         cmp     esi, [current_cursor]
  1968.         je      @f
  1969.  
  1970.         push    esi
  1971.         call    [_display.select_cursor]
  1972.         mov     [current_cursor], esi
  1973. @@:
  1974.         mov     [redrawmouse_unconditional], 1
  1975. ;        call    [draw_pointer]
  1976.         call    __sys_draw_pointer
  1977.         ret
  1978. ;------------------------------------------------------------------------------
  1979. iglobal
  1980. align 4
  1981. sys_system_table:
  1982.         dd      sysfn_deactivate        ; 1 = deactivate window
  1983.         dd      sysfn_terminate         ; 2 = terminate thread
  1984.         dd      sysfn_activate          ; 3 = activate window
  1985.         dd      sysfn_getidletime       ; 4 = get idle time
  1986.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1987.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1988.         dd      sysfn_getactive         ; 7 = get active window
  1989.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1990.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1991.         dd      sysfn_minimize          ; 10 = minimize window
  1992.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1993.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1994.         dd      sysfn_getversion        ; 13 = get kernel version
  1995.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1996.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1997.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1998.         dd      sysfn_getallmem         ; 17 = get total memory size
  1999.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  2000.                                         ;                 instead of slot
  2001.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2002.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2003.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2004.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2005. sysfn_num = ($ - sys_system_table)/4
  2006. endg
  2007. ;------------------------------------------------------------------------------
  2008. sys_system:
  2009.         dec     ebx
  2010.         cmp     ebx, sysfn_num
  2011.         jae     @f
  2012.         jmp     dword [sys_system_table + ebx*4]
  2013. @@:
  2014.         ret
  2015. ;------------------------------------------------------------------------------
  2016. sysfn_shutdown:          ; 18.9 = system shutdown
  2017.         cmp     ecx, 1
  2018.         jl      exit_for_anyone
  2019.         cmp     ecx, 4
  2020.         jg      exit_for_anyone
  2021.         mov     [BOOT_VAR+0x9030], cl
  2022.  
  2023.         mov     eax, [TASK_COUNT]
  2024.         mov     [SYS_SHUTDOWN], al
  2025.         mov     [shutdown_processes], eax
  2026.         and     dword [esp+32], 0
  2027.  exit_for_anyone:
  2028.         ret
  2029.   uglobal
  2030.    shutdown_processes:
  2031.                        dd 0x0
  2032.   endg
  2033. ;------------------------------------------------------------------------------
  2034. sysfn_terminate:        ; 18.2 = TERMINATE
  2035.         cmp     ecx, 2
  2036.         jb      noprocessterminate
  2037.         mov     edx, [TASK_COUNT]
  2038.         cmp     ecx, edx
  2039.         ja      noprocessterminate
  2040.         mov     eax, [TASK_COUNT]
  2041.         push    ecx
  2042.         shl     ecx, 5
  2043.         mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
  2044.         add     ecx, CURRENT_TASK+TASKDATA.state
  2045.         cmp     byte [ecx], 9
  2046.         jz      noprocessterminate
  2047. ;--------------------------------------
  2048.         cmp     [_display.select_cursor], 0
  2049.         je      .restore_end
  2050. ; restore default cursor before killing
  2051.         pusha
  2052.         mov     ecx, [esp+32]
  2053.         shl     ecx, 8
  2054.         add     ecx, SLOT_BASE
  2055.         mov     eax, [def_cursor]
  2056.         cmp     [ecx+APPDATA.cursor], eax
  2057.         je      @f
  2058.         call    restore_default_cursor_before_killing
  2059. @@:
  2060.         popa
  2061. .restore_end:
  2062.         add     esp, 4
  2063. ;--------------------------------------
  2064.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2065.         mov     [ecx], byte 3; clear possible i40's
  2066.      ;call MEM_Heap_UnLock
  2067.  
  2068.         cmp     edx, [application_table_status]; clear app table stat
  2069.         jne     noatsc
  2070.         and     [application_table_status], 0
  2071.    noatsc:
  2072.    noprocessterminate:
  2073.         ret
  2074. ;------------------------------------------------------------------------------
  2075. sysfn_terminate2:
  2076. ;lock application_table_status mutex
  2077. .table_status:
  2078.         cli
  2079.         cmp     [application_table_status], 0
  2080.         je      .stf
  2081.         sti
  2082.         call    change_task
  2083.         jmp     .table_status
  2084. .stf:
  2085.         call    set_application_table_status
  2086.         mov     eax, ecx
  2087.         call    pid_to_slot
  2088.         test    eax, eax
  2089.         jz      .not_found
  2090.         mov     ecx, eax
  2091.         cli
  2092.         call    sysfn_terminate
  2093.         and     [application_table_status], 0
  2094.         sti
  2095.         and     dword [esp+32], 0
  2096.         ret
  2097. .not_found:
  2098.         mov     [application_table_status], 0
  2099.         or      dword [esp+32], -1
  2100.         ret
  2101. ;------------------------------------------------------------------------------
  2102. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2103.         cmp     ecx, 2
  2104.         jb      .nowindowdeactivate
  2105.         cmp     ecx, [TASK_COUNT]
  2106.         ja      .nowindowdeactivate
  2107.  
  2108.         movzx   esi, word [WIN_STACK + ecx*2]
  2109.         cmp     esi, 1
  2110.         je      .nowindowdeactivate ; already deactive
  2111.  
  2112.         mov     edi, ecx
  2113.         shl     edi, 5
  2114.         add     edi, window_data
  2115.         movzx   esi, word [WIN_STACK + ecx * 2]
  2116.         lea     esi, [WIN_POS + esi * 2]
  2117.         call    window._.window_deactivate
  2118.  
  2119.         xor     eax, eax
  2120.         mov     byte[MOUSE_BACKGROUND], al
  2121.         mov     byte[DONT_DRAW_MOUSE], al
  2122.         mov     byte[MOUSE_DOWN], 0
  2123.  
  2124.         call    syscall_display_settings._.calculate_whole_screen
  2125.         call    syscall_display_settings._.redraw_whole_screen
  2126. .nowindowdeactivate:
  2127.         ret
  2128.  ;------------------------------------------------------------------------------
  2129. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2130.         cmp     ecx, 2
  2131.         jb      .nowindowactivate
  2132.         cmp     ecx, [TASK_COUNT]
  2133.         ja      .nowindowactivate
  2134.  
  2135.         mov     [window_minimize], 2; restore window if minimized
  2136.  
  2137.         movzx   esi, word [WIN_STACK + ecx*2]
  2138.         cmp     esi, [TASK_COUNT]
  2139.         je      .nowindowactivate; already active
  2140.  
  2141.         mov     edi, ecx
  2142.         shl     edi, 5
  2143.         add     edi, window_data
  2144.         movzx   esi, word [WIN_STACK + ecx * 2]
  2145.         lea     esi, [WIN_POS + esi * 2]
  2146.         call    waredraw
  2147. .nowindowactivate:
  2148.         ret
  2149. ;------------------------------------------------------------------------------
  2150. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2151.         mov     eax, [idleusesec]
  2152.         mov     [esp+32], eax
  2153.         ret
  2154. ;------------------------------------------------------------------------------
  2155. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2156.         mov     eax, [CPU_FREQ]
  2157.         mov     [esp+32], eax
  2158.         ret
  2159. ;------------------------------------------------------------------------------
  2160. ;  SAVE ramdisk to /hd/1/menuet.img
  2161. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2162.    include 'blkdev/rdsave.inc'
  2163. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2164. ;------------------------------------------------------------------------------
  2165. align 4
  2166. sysfn_getactive:        ; 18.7 = get active window
  2167.         mov     eax, [TASK_COUNT]
  2168.         movzx   eax, word [WIN_POS + eax*2]
  2169.         mov     [esp+32], eax
  2170.         ret
  2171. ;------------------------------------------------------------------------------
  2172. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2173. ;     cmp  ecx,1
  2174.         dec     ecx
  2175.         jnz     nogetsoundflag
  2176.         movzx   eax, byte [sound_flag]; get sound_flag
  2177.         mov     [esp+32], eax
  2178.         ret
  2179.  nogetsoundflag:
  2180. ;     cmp  ecx,2
  2181.         dec     ecx
  2182.         jnz     nosoundflag
  2183.         xor     byte [sound_flag], 1
  2184.  nosoundflag:
  2185.         ret
  2186. ;------------------------------------------------------------------------------
  2187. sysfn_minimize:         ; 18.10 = minimize window
  2188.         mov     [window_minimize], 1
  2189.         ret
  2190. ;------------------------------------------------------------------------------
  2191. align 4
  2192. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2193. ;     cmp  ecx,1
  2194.         dec     ecx
  2195.         jnz     full_table
  2196.   small_table:
  2197.         call    for_all_tables
  2198.         mov     ecx, 10
  2199.         cld
  2200.         rep movsb
  2201.         ret
  2202.    for_all_tables:
  2203.         mov     edi, edx
  2204.         mov     esi, DRIVE_DATA
  2205.         ret
  2206.   full_table:
  2207. ;     cmp  ecx,2
  2208.         dec     ecx
  2209.         jnz     exit_for_anyone
  2210.         call    for_all_tables
  2211.         mov     ecx, 16384
  2212.         cld
  2213.         rep movsd
  2214.         ret
  2215. ;------------------------------------------------------------------------------
  2216. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2217.         and     dword [esp+32], 0
  2218.         ret
  2219. ;------------------------------------------------------------------------------
  2220. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2221.         mov     edi, ecx
  2222.         mov     esi, version_inf
  2223.         mov     ecx, version_end-version_inf
  2224.         rep movsb
  2225.         ret
  2226. ;------------------------------------------------------------------------------
  2227. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2228.      ;wait retrace functions
  2229.  sys_wait_retrace:
  2230.         mov     edx, 0x3da
  2231.  WaitRetrace_loop:
  2232.         in      al, dx
  2233.         test    al, 1000b
  2234.         jz      WaitRetrace_loop
  2235.         and     [esp+32], dword 0
  2236.         ret
  2237. ;------------------------------------------------------------------------------
  2238. align 4
  2239. sysfn_centermouse:      ; 18.15 = mouse centered
  2240. ; removed here by <Lrz>
  2241. ;     call  mouse_centered
  2242. ;* mouse centered - start code- Mario79
  2243. ;mouse_centered:
  2244. ;        push  eax
  2245.         mov     eax, [Screen_Max_X]
  2246.         shr     eax, 1
  2247.         mov     [MOUSE_X], ax
  2248.         mov     eax, [Screen_Max_Y]
  2249.         shr     eax, 1
  2250.         mov     [MOUSE_Y], ax
  2251. ;        ret
  2252. ;* mouse centered - end code- Mario79
  2253.         xor     eax, eax
  2254.         and     [esp+32], eax
  2255. ;        pop   eax
  2256.         ret
  2257. ;------------------------------------------------------------------------------
  2258. align 4
  2259. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2260.         test    ecx, ecx; get mouse speed factor
  2261.         jnz     .set_mouse_acceleration
  2262.         xor     eax, eax
  2263.         mov     ax, [mouse_speed_factor]
  2264.         mov     [esp+32], eax
  2265.         ret
  2266.  .set_mouse_acceleration:
  2267. ;     cmp  ecx,1  ; set mouse speed factor
  2268.         dec     ecx
  2269.         jnz     .get_mouse_delay
  2270.         mov     [mouse_speed_factor], dx
  2271.         ret
  2272.  .get_mouse_delay:
  2273. ;     cmp  ecx,2  ; get mouse delay
  2274.         dec     ecx
  2275.         jnz     .set_mouse_delay
  2276.         mov     eax, [mouse_delay]
  2277.         mov     [esp+32], eax
  2278.         ret
  2279.  .set_mouse_delay:
  2280. ;     cmp  ecx,3  ; set mouse delay
  2281.         dec     ecx
  2282.         jnz     .set_pointer_position
  2283.         mov     [mouse_delay], edx
  2284.         ret
  2285.  .set_pointer_position:
  2286. ;     cmp  ecx,4  ; set mouse pointer position
  2287.         dec     ecx
  2288.         jnz     .set_mouse_button
  2289.         cmp     dx, word[Screen_Max_Y]
  2290.         ja      .end
  2291.         rol     edx, 16
  2292.         cmp     dx, word[Screen_Max_X]
  2293.         ja      .end
  2294.         mov     [MOUSE_X], edx
  2295.         ret
  2296.  .set_mouse_button:
  2297. ;     cmp   ecx,5  ; set mouse button features
  2298.         dec     ecx
  2299.         jnz     .end
  2300.         mov     [BTN_DOWN], dl
  2301.         mov     [mouse_active], 1
  2302.  .end:
  2303.         ret
  2304. ;------------------------------------------------------------------------------
  2305. sysfn_getfreemem:
  2306.         mov     eax, [pg_data.pages_free]
  2307.         shl     eax, 2
  2308.         mov     [esp+32], eax
  2309.         ret
  2310.  
  2311. sysfn_getallmem:
  2312.         mov     eax, [MEM_AMOUNT]
  2313.         shr     eax, 10
  2314.         mov     [esp+32], eax
  2315.         ret
  2316.  
  2317. ; // Alver, 2007-22-08 // {
  2318. sysfn_pid_to_slot:
  2319.         mov     eax, ecx
  2320.         call    pid_to_slot
  2321.         mov     [esp+32], eax
  2322.         ret
  2323.  
  2324. sysfn_min_rest_window:
  2325.         pushad
  2326.         mov     eax, edx ; ebx - operating
  2327.         shr     ecx, 1
  2328.         jnc     @f
  2329.         call    pid_to_slot
  2330. @@:
  2331.         or      eax, eax ; eax - number of slot
  2332.         jz      .error
  2333.         cmp     eax, 255    ; varify maximal slot number
  2334.         ja      .error
  2335.         movzx   eax, word [WIN_STACK + eax*2]
  2336.         shr     ecx, 1
  2337.         jc      .restore
  2338.  ; .minimize:
  2339.         call    minimize_window
  2340.         jmp     .exit
  2341. .restore:
  2342.         call    restore_minimized_window
  2343. .exit:
  2344.         popad
  2345.         xor     eax, eax
  2346.         mov     [esp+32], eax
  2347.         ret
  2348. .error:
  2349.         popad
  2350.         xor     eax, eax
  2351.         dec     eax
  2352.         mov     [esp+32], eax
  2353.         ret
  2354. ; } \\ Alver, 2007-22-08 \\
  2355.  
  2356. uglobal
  2357. ;// mike.dld, 2006-29-01 [
  2358. screen_workarea RECT
  2359. ;// mike.dld, 2006-29-01 ]
  2360. window_minimize db 0
  2361. sound_flag      db 0
  2362. endg
  2363.  
  2364. UID_NONE=0
  2365. UID_MENUETOS=1   ;official
  2366. UID_KOLIBRI=2    ;russian
  2367.  
  2368. iglobal
  2369. version_inf:
  2370.   db 0,7,7,0  ; version 0.7.7.0
  2371.   db 0
  2372.   dd __REV__
  2373. version_end:
  2374. endg
  2375.  
  2376. sys_cachetodiskette:
  2377.         cmp     ebx, 1
  2378.         jne     .no_floppy_a_save
  2379.         mov     [flp_number], 1
  2380.         jmp     .save_image_on_floppy
  2381. .no_floppy_a_save:
  2382.         cmp     ebx, 2
  2383.         jne     .no_floppy_b_save
  2384.         mov     [flp_number], 2
  2385. .save_image_on_floppy:
  2386.         call    save_image
  2387.         mov     [esp + 32], dword 0
  2388.         cmp     [FDC_Status], 0
  2389.         je      .yes_floppy_save
  2390. .no_floppy_b_save:
  2391.         mov     [esp + 32], dword 1
  2392. .yes_floppy_save:
  2393.         ret
  2394.  
  2395. uglobal
  2396. ;  bgrchanged  dd  0x0
  2397. align 4
  2398. bgrlockpid dd 0
  2399. bgrlock db 0
  2400. endg
  2401.  
  2402. sys_background:
  2403.  
  2404.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2405.         jnz     nosb1
  2406.         test    ecx, ecx
  2407. ;    cmp   ecx,0
  2408.         jz      sbgrr
  2409.         test    edx, edx
  2410. ;    cmp   edx,0
  2411.         jz      sbgrr
  2412. @@:
  2413. ;;Maxis use atomic bts for mutexes  4.4.2009
  2414.         bts     dword [bgrlock], 0
  2415.         jnc     @f
  2416.         call    change_task
  2417.         jmp     @b
  2418. @@:
  2419.         mov     [BgrDataWidth], ecx
  2420.         mov     [BgrDataHeight], edx
  2421. ;    mov   [bgrchanged],1
  2422.  
  2423.         pushad
  2424. ; return memory for old background
  2425.         mov     eax, [img_background]
  2426.         cmp     eax, static_background_data
  2427.         jz      @f
  2428.         stdcall kernel_free, eax
  2429. @@:
  2430. ; calculate RAW size
  2431.         xor     eax, eax
  2432.         inc     eax
  2433.         cmp     [BgrDataWidth], eax
  2434.         jae     @f
  2435.         mov     [BgrDataWidth], eax
  2436. @@:
  2437.         cmp     [BgrDataHeight], eax
  2438.         jae     @f
  2439.         mov     [BgrDataHeight], eax
  2440. @@:
  2441.         mov     eax, [BgrDataWidth]
  2442.         imul    eax, [BgrDataHeight]
  2443.         lea     eax, [eax*3]
  2444. ; it is reserved with aligned to the boundary of 4 KB pages,
  2445. ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
  2446. ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
  2447.         shr     eax, 12
  2448.         inc     eax
  2449.         shl     eax, 12
  2450.         mov     [mem_BACKGROUND], eax
  2451. ; get memory for new background
  2452.         stdcall kernel_alloc, eax
  2453.         test    eax, eax
  2454.         jz      .memfailed
  2455.         mov     [img_background], eax
  2456.         jmp     .exit
  2457. .memfailed:
  2458. ; revert to static monotone data
  2459.         mov     [img_background], static_background_data
  2460.         xor     eax, eax
  2461.         inc     eax
  2462.         mov     [BgrDataWidth], eax
  2463.         mov     [BgrDataHeight], eax
  2464.         mov     [mem_BACKGROUND], 4
  2465. .exit:
  2466.         popad
  2467.         mov     [bgrlock], 0
  2468.  
  2469.   sbgrr:
  2470.         ret
  2471.  
  2472. nosb1:
  2473.  
  2474.         cmp     ebx, 2                     ; SET PIXEL
  2475.         jnz     nosb2
  2476.  
  2477.         mov     eax, [img_background]
  2478.         test    ecx, ecx
  2479.         jz      @f
  2480.         cmp     eax, static_background_data
  2481.         jz      .ret
  2482. @@:
  2483.         mov     ebx, [mem_BACKGROUND]
  2484.         add     ebx, 4095
  2485.         and     ebx, -4096
  2486.         sub     ebx, 4
  2487.         cmp     ecx, ebx
  2488.         ja      .ret
  2489.  
  2490.         mov     ebx, [eax+ecx]
  2491.         and     ebx, 0xFF000000;255*256*256*256
  2492.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2493.         add     edx, ebx
  2494.         mov     [eax+ecx], edx
  2495. .ret:
  2496.         ret
  2497. nosb2:
  2498.  
  2499.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2500.         jnz     nosb3
  2501. draw_background_temp:
  2502. ;    cmp   [bgrchanged],1 ;0
  2503. ;    je    nosb31
  2504. ;draw_background_temp:
  2505. ;    mov   [bgrchanged],1 ;0
  2506.         mov     [background_defined], 1
  2507.         mov     byte[BACKGROUND_CHANGED], 1
  2508.         call    force_redraw_background
  2509.    nosb31:
  2510.         ret
  2511.   nosb3:
  2512.  
  2513.         cmp     ebx, 4                     ; TILED / STRETCHED
  2514.         jnz     nosb4
  2515.         cmp     ecx, [BgrDrawMode]
  2516.         je      nosb41
  2517.         mov     [BgrDrawMode], ecx
  2518. ;    mov   [bgrchanged],1
  2519.    nosb41:
  2520.         ret
  2521.   nosb4:
  2522.  
  2523.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2524.         jnz     nosb5
  2525.         cmp     [img_background], static_background_data
  2526.         jnz     @f
  2527.         test    edx, edx
  2528.         jnz     .fin
  2529.         cmp     esi, 4
  2530.         ja      .fin
  2531.   @@:
  2532.   ; bughere
  2533.         mov     eax, ecx
  2534.         mov     ebx, edx
  2535.         add     ebx, [img_background];IMG_BACKGROUND
  2536.         mov     ecx, esi
  2537.         call    memmove
  2538.   .fin:
  2539.         ret
  2540.   nosb5:
  2541.  
  2542.         cmp     ebx, 6
  2543.         jnz     nosb6
  2544. ;;Maxis use atomic bts for mutex 4.4.2009
  2545. @@:
  2546.         bts     dword [bgrlock], 0
  2547.         jnc     @f
  2548.         call    change_task
  2549.         jmp     @b
  2550. @@:
  2551.         mov     eax, [CURRENT_TASK]
  2552.         mov     [bgrlockpid], eax
  2553.         cmp     [img_background], static_background_data
  2554.         jz      .nomem
  2555.         stdcall user_alloc, [mem_BACKGROUND]
  2556.         mov     [esp+32], eax
  2557.         test    eax, eax
  2558.         jz      .nomem
  2559.         mov     ebx, eax
  2560.         shr     ebx, 12
  2561.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2562.         mov     esi, [img_background]
  2563.         shr     esi, 12
  2564.         mov     ecx, [mem_BACKGROUND]
  2565.         add     ecx, 0xFFF
  2566.         shr     ecx, 12
  2567. .z:
  2568.         mov     eax, [page_tabs+ebx*4]
  2569.         test    al, 1
  2570.         jz      @f
  2571.         call    free_page
  2572. @@:
  2573.         mov     eax, [page_tabs+esi*4]
  2574.         or      al, PG_UW
  2575.         mov     [page_tabs+ebx*4], eax
  2576.         mov     eax, ebx
  2577.         shl     eax, 12
  2578.         invlpg  [eax]
  2579.         inc     ebx
  2580.         inc     esi
  2581.         loop    .z
  2582.         ret
  2583. .nomem:
  2584.         and     [bgrlockpid], 0
  2585.         mov     [bgrlock], 0
  2586. nosb6:
  2587.         cmp     ebx, 7
  2588.         jnz     nosb7
  2589.         cmp     [bgrlock], 0
  2590.         jz      .err
  2591.         mov     eax, [CURRENT_TASK]
  2592.         cmp     [bgrlockpid], eax
  2593.         jnz     .err
  2594.         mov     eax, ecx
  2595.         mov     ebx, ecx
  2596.         shr     eax, 12
  2597.         mov     ecx, [page_tabs+(eax-1)*4]
  2598.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2599.         jz      .err
  2600.         jnp     .err
  2601.         push    eax
  2602.         shr     ecx, 12
  2603.         dec     ecx
  2604. @@:
  2605.         and     dword [page_tabs+eax*4], 0
  2606.         mov     edx, eax
  2607.         shl     edx, 12
  2608.         push    eax
  2609.         invlpg  [edx]
  2610.         pop     eax
  2611.         inc     eax
  2612.         loop    @b
  2613.         pop     eax
  2614.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2615.         stdcall user_free, ebx
  2616.         mov     [esp+32], eax
  2617.         and     [bgrlockpid], 0
  2618.         mov     [bgrlock], 0
  2619.         ret
  2620. .err:
  2621.         and     dword [esp+32], 0
  2622.         ret
  2623.  
  2624. nosb7:
  2625.         ret
  2626.  
  2627. force_redraw_background:
  2628.         and     [draw_data+32 + RECT.left], 0
  2629.         and     [draw_data+32 + RECT.top], 0
  2630.         push    eax ebx
  2631.         mov     eax, [Screen_Max_X]
  2632.         mov     ebx, [Screen_Max_Y]
  2633.         mov     [draw_data+32 + RECT.right], eax
  2634.         mov     [draw_data+32 + RECT.bottom], ebx
  2635.         pop     ebx eax
  2636.         inc     byte[REDRAW_BACKGROUND]
  2637.         ret
  2638.  
  2639. align 4
  2640.  
  2641. sys_getbackground:
  2642. ;    cmp   eax,1                                  ; SIZE
  2643.         dec     ebx
  2644.         jnz     nogb1
  2645.         mov     eax, [BgrDataWidth]
  2646.         shl     eax, 16
  2647.         mov     ax, [BgrDataHeight]
  2648.         mov     [esp+32], eax
  2649.         ret
  2650.  
  2651. nogb1:
  2652. ;    cmp   eax,2                                  ; PIXEL
  2653.         dec     ebx
  2654.         jnz     nogb2
  2655.  
  2656.         mov     eax, [img_background]
  2657.         test    ecx, ecx
  2658.         jz      @f
  2659.         cmp     eax, static_background_data
  2660.         jz      .ret
  2661. @@:
  2662.         mov     ebx, [mem_BACKGROUND]
  2663.         add     ebx, 4095
  2664.         and     ebx, -4096
  2665.         sub     ebx, 4
  2666.         cmp     ecx, ebx
  2667.         ja      .ret
  2668.  
  2669.         mov     eax, [ecx+eax]
  2670.  
  2671.         and     eax, 0xFFFFFF
  2672.         mov     [esp+32], eax
  2673. .ret:
  2674.         ret
  2675.   nogb2:
  2676.  
  2677. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2678.         dec     ebx
  2679.         dec     ebx
  2680.         jnz     nogb4
  2681.         mov     eax, [BgrDrawMode]
  2682.   nogb4:
  2683.         mov     [esp+32], eax
  2684.         ret
  2685.  
  2686. align 4
  2687.  
  2688. sys_getkey:
  2689.         mov     [esp + 32], dword 1
  2690.         ; test main buffer
  2691.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2692.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2693.         mov     edx, [TASK_COUNT]
  2694.         cmp     ecx, edx
  2695.         jne     .finish
  2696.         cmp     [KEY_COUNT], byte 0
  2697.         je      .finish
  2698.         movzx   eax, byte [KEY_BUFF]
  2699.         shl     eax, 8
  2700.         push    eax
  2701.         dec     byte [KEY_COUNT]
  2702.         and     byte [KEY_COUNT], 127
  2703.         movzx   ecx, byte [KEY_COUNT]
  2704.         add     ecx, 2
  2705.         mov     eax, KEY_BUFF + 1
  2706.         mov     ebx, KEY_BUFF
  2707.         call    memmove
  2708.         pop     eax
  2709. .ret_eax:
  2710.         mov     [esp + 32], eax
  2711.         ret
  2712. .finish:
  2713. ; test hotkeys buffer
  2714.         mov     ecx, hotkey_buffer
  2715. @@:
  2716.         cmp     [ecx], ebx
  2717.         jz      .found
  2718.         add     ecx, 8
  2719.         cmp     ecx, hotkey_buffer + 120 * 8
  2720.         jb      @b
  2721.         ret
  2722. .found:
  2723.         mov     ax, [ecx + 6]
  2724.         shl     eax, 16
  2725.         mov     ah, [ecx + 4]
  2726.         mov     al, 2
  2727.         and     dword [ecx + 4], 0
  2728.         and     dword [ecx], 0
  2729.         jmp     .ret_eax
  2730.  
  2731. align 4
  2732.  
  2733. sys_getbutton:
  2734.  
  2735.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2736.         mov     [esp + 32], dword 1
  2737.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2738.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2739.         cmp     ecx, edx
  2740.         jne     .exit
  2741.         movzx   eax, byte [BTN_COUNT]
  2742.         test    eax, eax
  2743.         jz      .exit
  2744.         mov     eax, [BTN_BUFF]
  2745.         and     al, 0xFE                                    ; delete left button bit
  2746.         mov     [BTN_COUNT], byte 0
  2747.         mov     [esp + 32], eax
  2748. .exit:
  2749.         ret
  2750.  
  2751.  
  2752. align 4
  2753.  
  2754. sys_cpuusage:
  2755.  
  2756. ;  RETURN:
  2757. ;
  2758. ;  +00 dword     process cpu usage
  2759. ;  +04  word     position in windowing stack
  2760. ;  +06  word     windowing stack value at current position (cpu nro)
  2761. ;  +10 12 bytes  name
  2762. ;  +22 dword     start in mem
  2763. ;  +26 dword     used mem
  2764. ;  +30 dword     PID , process idenfification number
  2765. ;
  2766.  
  2767.         cmp     ecx, -1 ; who am I ?
  2768.         jne     .no_who_am_i
  2769.         mov     ecx, [CURRENT_TASK]
  2770.   .no_who_am_i:
  2771.         cmp     ecx, max_processes
  2772.         ja      .nofillbuf
  2773.  
  2774. ; +4: word: position of the window of thread in the window stack
  2775.         mov     ax, [WIN_STACK + ecx * 2]
  2776.         mov     [ebx+4], ax
  2777. ; +6: word: number of the thread slot, which window has in the window stack
  2778. ;           position ecx (has no relation to the specific thread)
  2779.         mov     ax, [WIN_POS + ecx * 2]
  2780.         mov     [ebx+6], ax
  2781.  
  2782.         shl     ecx, 5
  2783.  
  2784. ; +0: dword: memory usage
  2785.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2786.         mov     [ebx], eax
  2787. ; +10: 11 bytes: name of the process
  2788.         push    ecx
  2789.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2790.         add     ebx, 10
  2791.         mov     ecx, 11
  2792.         call    memmove
  2793.         pop     ecx
  2794.  
  2795. ; +22: address of the process in memory
  2796. ; +26: size of used memory - 1
  2797.         push    edi
  2798.         lea     edi, [ebx+12]
  2799.         xor     eax, eax
  2800.         mov     edx, 0x100000*16
  2801.         cmp     ecx, 1 shl 5
  2802.         je      .os_mem
  2803.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2804.         mov     eax, std_application_base_address
  2805. .os_mem:
  2806.         stosd
  2807.         lea     eax, [edx-1]
  2808.         stosd
  2809.  
  2810. ; +30: PID/TID
  2811.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2812.         stosd
  2813.  
  2814.     ; window position and size
  2815.         push    esi
  2816.         lea     esi, [ecx + window_data + WDATA.box]
  2817.         movsd
  2818.         movsd
  2819.         movsd
  2820.         movsd
  2821.  
  2822.     ; Process state (+50)
  2823.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2824.         stosd
  2825.  
  2826.     ; Window client area box
  2827.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2828.         movsd
  2829.         movsd
  2830.         movsd
  2831.         movsd
  2832.  
  2833.     ; Window state
  2834.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2835.         stosb
  2836.  
  2837.     ; Event mask (+71)
  2838.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  2839.         stosd
  2840.  
  2841.         pop     esi
  2842.         pop     edi
  2843.  
  2844. .nofillbuf:
  2845.     ; return number of processes
  2846.  
  2847.         mov     eax, [TASK_COUNT]
  2848.         mov     [esp+32], eax
  2849.         ret
  2850.  
  2851. align 4
  2852. sys_clock:
  2853.         cli
  2854.   ; Mikhail Lisovin  xx Jan 2005
  2855.   @@:
  2856.         mov     al, 10
  2857.         out     0x70, al
  2858.         in      al, 0x71
  2859.         test    al, al
  2860.         jns     @f
  2861.         mov     esi, 1
  2862.         call    delay_ms
  2863.         jmp     @b
  2864.   @@:
  2865.   ; end Lisovin's fix
  2866.  
  2867.         xor     al, al        ; seconds
  2868.         out     0x70, al
  2869.         in      al, 0x71
  2870.         movzx   ecx, al
  2871.         mov     al, 02        ; minutes
  2872.         shl     ecx, 16
  2873.         out     0x70, al
  2874.         in      al, 0x71
  2875.         movzx   edx, al
  2876.         mov     al, 04        ; hours
  2877.         shl     edx, 8
  2878.         out     0x70, al
  2879.         in      al, 0x71
  2880.         add     ecx, edx
  2881.         movzx   edx, al
  2882.         add     ecx, edx
  2883.         sti
  2884.         mov     [esp + 32], ecx
  2885.         ret
  2886.  
  2887.  
  2888. align 4
  2889.  
  2890. sys_date:
  2891.  
  2892.         cli
  2893.   @@:
  2894.         mov     al, 10
  2895.         out     0x70, al
  2896.         in      al, 0x71
  2897.         test    al, al
  2898.         jns     @f
  2899.         mov     esi, 1
  2900.         call    delay_ms
  2901.         jmp     @b
  2902.   @@:
  2903.  
  2904.         mov     ch, 0
  2905.         mov     al, 7           ; date
  2906.         out     0x70, al
  2907.         in      al, 0x71
  2908.         mov     cl, al
  2909.         mov     al, 8           ; month
  2910.         shl     ecx, 16
  2911.         out     0x70, al
  2912.         in      al, 0x71
  2913.         mov     ch, al
  2914.         mov     al, 9           ; year
  2915.         out     0x70, al
  2916.         in      al, 0x71
  2917.         mov     cl, al
  2918.         sti
  2919.         mov     [esp+32], ecx
  2920.         ret
  2921.  
  2922.  
  2923. ; redraw status
  2924.  
  2925. sys_redrawstat:
  2926.         cmp     ebx, 1
  2927.         jne     no_widgets_away
  2928.         ; buttons away
  2929.         mov     ecx, [CURRENT_TASK]
  2930.   sys_newba2:
  2931.         mov     edi, [BTN_ADDR]
  2932.         cmp     [edi], dword 0  ; empty button list ?
  2933.         je      end_of_buttons_away
  2934.         movzx   ebx, word [edi]
  2935.         inc     ebx
  2936.         mov     eax, edi
  2937.   sys_newba:
  2938.         dec     ebx
  2939.         jz      end_of_buttons_away
  2940.  
  2941.         add     eax, 0x10
  2942.         cmp     cx, [eax]
  2943.         jnz     sys_newba
  2944.  
  2945.         push    eax ebx ecx
  2946.         mov     ecx, ebx
  2947.         inc     ecx
  2948.         shl     ecx, 4
  2949.         mov     ebx, eax
  2950.         add     eax, 0x10
  2951.         call    memmove
  2952.         dec     dword [edi]
  2953.         pop     ecx ebx eax
  2954.  
  2955.         jmp     sys_newba2
  2956.  
  2957.   end_of_buttons_away:
  2958.  
  2959.         ret
  2960.  
  2961.   no_widgets_away:
  2962.  
  2963.         cmp     ebx, 2
  2964.         jnz     srl1
  2965.  
  2966.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2967.         add     edx, draw_data - CURRENT_TASK
  2968.         mov     [edx + RECT.left], 0
  2969.         mov     [edx + RECT.top], 0
  2970.         mov     eax, [Screen_Max_X]
  2971.         mov     [edx + RECT.right], eax
  2972.         mov     eax, [Screen_Max_Y]
  2973.         mov     [edx + RECT.bottom], eax
  2974.  
  2975.   srl1:
  2976.         ret
  2977.  
  2978. ;ok - 100% work
  2979. ;nt - not tested
  2980. ;---------------------------------------------------------------------------------------------
  2981. ;eax
  2982. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2983. ;1 - change task. Ret nothing. Block. ok.
  2984. ;2 - performance control
  2985. ; ebx
  2986. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2987. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2988. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2989. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2990. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2991. ;eax
  2992. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2993. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2994. ;---------------------------------------------------------------------------------------------
  2995. iglobal
  2996. align 4
  2997. sheduler:
  2998.         dd      sys_sheduler.00
  2999.         dd      change_task
  3000.         dd      sys_sheduler.02
  3001.         dd      sys_sheduler.03
  3002.         dd      sys_sheduler.04
  3003. endg
  3004. sys_sheduler:
  3005. ;rewritten by <Lrz>  29.12.2009
  3006.         jmp     dword [sheduler+ebx*4]
  3007. ;.shed_counter:
  3008. .00:
  3009.         mov     eax, [context_counter]
  3010.         mov     [esp+32], eax
  3011.         ret
  3012.  
  3013. .02:
  3014. ;.perf_control:
  3015.         inc     ebx                     ;before ebx=2, ebx=3
  3016.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3017.         jz      cache_disable
  3018.  
  3019.         dec     ebx                     ;ebx=2
  3020.         cmp     ebx, ecx                ;
  3021.         jz      cache_enable            ;if ecx=2 and ebx=2
  3022.  
  3023.         dec     ebx                     ;ebx=1
  3024.         cmp     ebx, ecx
  3025.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3026.  
  3027.         dec     ebx
  3028.         test    ebx, ecx                ;ebx=0 and ecx=0
  3029.         jz      modify_pce              ;if ecx=0
  3030.  
  3031.         ret
  3032.  
  3033. .03:
  3034. ;.rdmsr_instr:
  3035. ;now counter in ecx
  3036. ;(edx:eax) esi:edi => edx:esi
  3037.         mov     eax, esi
  3038.         mov     ecx, edx
  3039.         rdmsr
  3040.         mov     [esp+32], eax
  3041.         mov     [esp+20], edx           ;ret in ebx?
  3042.         ret
  3043.  
  3044. .04:
  3045. ;.wrmsr_instr:
  3046. ;now counter in ecx
  3047. ;(edx:eax) esi:edi => edx:esi
  3048.         ; Fast Call MSR can't be destroy
  3049.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  3050.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  3051.         cmp     edx, MSR_SYSENTER_CS
  3052.         je      @f
  3053.         cmp     edx, MSR_SYSENTER_ESP
  3054.         je      @f
  3055.         cmp     edx, MSR_SYSENTER_EIP
  3056.         je      @f
  3057.         cmp     edx, MSR_AMD_STAR
  3058.         je      @f
  3059.  
  3060.         mov     eax, esi
  3061.         mov     ecx, edx
  3062.         wrmsr
  3063.         ; mov   [esp + 32], eax
  3064.         ; mov   [esp + 20], edx ;ret in ebx?
  3065. @@:
  3066.         ret
  3067.  
  3068. cache_disable:
  3069.         mov     eax, cr0
  3070.         or      eax, 01100000000000000000000000000000b
  3071.         mov     cr0, eax
  3072.         wbinvd  ;set MESI
  3073.         ret
  3074.  
  3075. cache_enable:
  3076.         mov     eax, cr0
  3077.         and     eax, 10011111111111111111111111111111b
  3078.         mov     cr0, eax
  3079.         ret
  3080.  
  3081. is_cache_enabled:
  3082.         mov     eax, cr0
  3083.         mov     ebx, eax
  3084.         and     eax, 01100000000000000000000000000000b
  3085.         jz      cache_disabled
  3086.         mov     [esp+32], ebx
  3087. cache_disabled:
  3088.         mov     dword [esp+32], eax;0
  3089.         ret
  3090.  
  3091. modify_pce:
  3092.         mov     eax, cr4
  3093. ;       mov ebx,0
  3094. ;       or  bx,100000000b ;pce
  3095. ;       xor eax,ebx ;invert pce
  3096.         bts     eax, 8;pce=cr4[8]
  3097.         mov     cr4, eax
  3098.         mov     [esp+32], eax
  3099.         ret
  3100. ;---------------------------------------------------------------------------------------------
  3101.  
  3102.  
  3103. ; check if pixel is allowed to be drawn
  3104.  
  3105. ;checkpixel:
  3106. ;        push    eax edx
  3107.  
  3108. ;;        mov     edx, [Screen_Max_X] ; screen x size
  3109. ;;        inc     edx
  3110. ;;        imul    edx, ebx
  3111. ;        mov     edx, [d_width_calc_area + ebx*4]
  3112. ;        add     eax, [_WinMapAddress]
  3113. ;        mov     dl, [eax+edx]; lea eax, [...]
  3114.  
  3115. ;        xor     ecx, ecx
  3116. ;        mov     eax, [CURRENT_TASK]
  3117. ;        cmp     al, dl
  3118. ;        setne   cl
  3119.  
  3120. ;        pop     edx eax
  3121. ;        ret
  3122.  
  3123. iglobal
  3124.   cpustring db 'CPU',0
  3125. endg
  3126.  
  3127. uglobal
  3128. background_defined    db    0    ; diamond, 11.04.2006
  3129. endg
  3130.  
  3131. align 4
  3132. ; check misc
  3133.  
  3134. checkmisc:
  3135.  
  3136.         cmp     [ctrl_alt_del], 1
  3137.         jne     nocpustart
  3138.  
  3139.         mov     ebp, cpustring
  3140.         call    fs_execute_from_sysdir
  3141.  
  3142.         mov     [ctrl_alt_del], 0
  3143.  
  3144. nocpustart:
  3145.         cmp     [mouse_active], 1
  3146.         jne     mouse_not_active
  3147.         mov     [mouse_active], 0
  3148.  
  3149.         xor     edi, edi
  3150.         mov     ebx, CURRENT_TASK
  3151.  
  3152.         mov     ecx, [TASK_COUNT]
  3153.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3154.         shl     eax, 8
  3155.         push    eax
  3156.  
  3157.         movzx   eax, word [MOUSE_X]
  3158.         movzx   edx, word [MOUSE_Y]
  3159.  
  3160. align 4
  3161. .set_mouse_event:
  3162.         add     edi, 256
  3163.         add     ebx, 32
  3164.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3165.         jz      .pos_filter
  3166.  
  3167.         cmp     edi, [esp]                      ; skip if filtration active
  3168.         jne     .skip
  3169.  
  3170. .pos_filter:
  3171.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3172.         jz      .set
  3173.  
  3174.         mov     esi, [ebx-twdw+WDATA.box.left]
  3175.         cmp     eax, esi
  3176.         jb      .skip
  3177.         add     esi, [ebx-twdw+WDATA.box.width]
  3178.         cmp     eax, esi
  3179.         ja      .skip
  3180.  
  3181.         mov     esi, [ebx-twdw+WDATA.box.top]
  3182.         cmp     edx, esi
  3183.         jb      .skip
  3184.         add     esi, [ebx-twdw+WDATA.box.height]
  3185.         cmp     edx, esi
  3186.         ja      .skip
  3187. .set:
  3188.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
  3189. .skip:
  3190.         loop    .set_mouse_event
  3191.  
  3192.         pop     eax
  3193.  
  3194. mouse_not_active:
  3195.         cmp     byte[BACKGROUND_CHANGED], 0
  3196.         jz      no_set_bgr_event
  3197.         xor     edi, edi
  3198.         mov     ecx, [TASK_COUNT]
  3199. set_bgr_event:
  3200.         add     edi, 256
  3201.         or      [edi+SLOT_BASE+APPDATA.event_mask], 16
  3202.         loop    set_bgr_event
  3203.         mov     byte[BACKGROUND_CHANGED], 0
  3204. no_set_bgr_event:
  3205.         cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
  3206.         jz      nobackgr
  3207.         cmp     [background_defined], 0
  3208.         jz      nobackgr
  3209. ;    mov   [draw_data+32 + RECT.left],dword 0
  3210. ;    mov   [draw_data+32 + RECT.top],dword 0
  3211. ;    mov   eax,[Screen_Max_X]
  3212. ;    mov   ebx,[Screen_Max_Y]
  3213. ;    mov   [draw_data+32 + RECT.right],eax
  3214. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3215. @@:
  3216.         call    drawbackground
  3217.         xor     eax, eax
  3218.         xchg    al, [REDRAW_BACKGROUND]
  3219.         test    al, al                             ; got new update request?
  3220.         jnz     @b
  3221.         mov     [draw_data+32 + RECT.left], eax
  3222.         mov     [draw_data+32 + RECT.top], eax
  3223.         mov     [draw_data+32 + RECT.right], eax
  3224.         mov     [draw_data+32 + RECT.bottom], eax
  3225.         mov     [MOUSE_BACKGROUND], byte 0
  3226.  
  3227. nobackgr:
  3228.  
  3229.     ; system shutdown request
  3230.  
  3231.         cmp     [SYS_SHUTDOWN], byte 0
  3232.         je      noshutdown
  3233.  
  3234.         mov     edx, [shutdown_processes]
  3235.  
  3236.         cmp     [SYS_SHUTDOWN], dl
  3237.         jne     no_mark_system_shutdown
  3238.  
  3239.         lea     ecx, [edx-1]
  3240.         mov     edx, OS_BASE+0x3040
  3241.         jecxz   @f
  3242. markz:
  3243.         mov     [edx+TASKDATA.state], byte 3
  3244.         add     edx, 0x20
  3245.         loop    markz
  3246. @@:
  3247.  
  3248.   no_mark_system_shutdown:
  3249.  
  3250.         dec     byte [SYS_SHUTDOWN]
  3251.         je      system_shutdown
  3252.  
  3253. noshutdown:
  3254.  
  3255.  
  3256.         mov     eax, [TASK_COUNT]           ; termination
  3257.         mov     ebx, TASK_DATA+TASKDATA.state
  3258.         mov     esi, 1
  3259.  
  3260. newct:
  3261.         mov     cl, [ebx]
  3262.         cmp     cl, byte 3
  3263.         jz      terminate
  3264.         cmp     cl, byte 4
  3265.         jz      terminate
  3266.  
  3267.         add     ebx, 0x20
  3268.         inc     esi
  3269.         dec     eax
  3270.         jnz     newct
  3271.         ret
  3272.  
  3273. ; redraw screen
  3274.  
  3275. redrawscreen:
  3276.  
  3277. ; eax , if process window_data base is eax, do not set flag/limits
  3278.  
  3279.         pushad
  3280.         push    eax
  3281.  
  3282. ;;;         mov   ebx,2
  3283. ;;;         call  delay_hs
  3284.  
  3285.          ;mov   ecx,0               ; redraw flags for apps
  3286.         xor     ecx, ecx
  3287.        newdw2:
  3288.  
  3289.         inc     ecx
  3290.         push    ecx
  3291.  
  3292.         mov     eax, ecx
  3293.         shl     eax, 5
  3294.         add     eax, window_data
  3295.  
  3296.         cmp     eax, [esp+4]
  3297.         je      not_this_task
  3298.                                    ; check if window in redraw area
  3299.         mov     edi, eax
  3300.  
  3301.         cmp     ecx, 1             ; limit for background
  3302.         jz      bgli
  3303.  
  3304.         mov     eax, [edi + WDATA.box.left]
  3305.         mov     ebx, [edi + WDATA.box.top]
  3306.         mov     ecx, [edi + WDATA.box.width]
  3307.         mov     edx, [edi + WDATA.box.height]
  3308.         add     ecx, eax
  3309.         add     edx, ebx
  3310.  
  3311.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3312.         cmp     ecx, ebx
  3313.         jb      ricino
  3314.  
  3315.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3316.         cmp     ecx, eax
  3317.         jb      ricino
  3318.  
  3319.         mov     eax, [edi + WDATA.box.left]
  3320.         mov     ebx, [edi + WDATA.box.top]
  3321.         mov     ecx, [edi + WDATA.box.width]
  3322.         mov     edx, [edi + WDATA.box.height]
  3323.         add     ecx, eax
  3324.         add     edx, ebx
  3325.  
  3326.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3327.         cmp     edx, eax
  3328.         jb      ricino
  3329.  
  3330.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3331.         cmp     ecx, eax
  3332.         jb      ricino
  3333.  
  3334.         bgli:
  3335.  
  3336.         cmp     dword[esp], 1
  3337.         jnz     .az
  3338. ;         cmp   byte[BACKGROUND_CHANGED], 0
  3339. ;         jnz   newdw8
  3340.         cmp     byte[REDRAW_BACKGROUND], 0
  3341.         jz      .az
  3342.         mov     dl, 0
  3343.         lea     eax, [edi+draw_data-window_data]
  3344.         mov     ebx, [draw_limits.left]
  3345.         cmp     ebx, [eax+RECT.left]
  3346.         jae     @f
  3347.         mov     [eax+RECT.left], ebx
  3348.         mov     dl, 1
  3349.         @@:
  3350.         mov     ebx, [draw_limits.top]
  3351.         cmp     ebx, [eax+RECT.top]
  3352.         jae     @f
  3353.         mov     [eax+RECT.top], ebx
  3354.         mov     dl, 1
  3355.         @@:
  3356.         mov     ebx, [draw_limits.right]
  3357.         cmp     ebx, [eax+RECT.right]
  3358.         jbe     @f
  3359.         mov     [eax+RECT.right], ebx
  3360.         mov     dl, 1
  3361.         @@:
  3362.         mov     ebx, [draw_limits.bottom]
  3363.         cmp     ebx, [eax+RECT.bottom]
  3364.         jbe     @f
  3365.         mov     [eax+RECT.bottom], ebx
  3366.         mov     dl, 1
  3367.         @@:
  3368.         add     byte[REDRAW_BACKGROUND], dl
  3369.         jmp     newdw8
  3370.         .az:
  3371.  
  3372.         mov     eax, edi
  3373.         add     eax, draw_data-window_data
  3374.  
  3375.         mov     ebx, [draw_limits.left]        ; set limits
  3376.         mov     [eax + RECT.left], ebx
  3377.         mov     ebx, [draw_limits.top]
  3378.         mov     [eax + RECT.top], ebx
  3379.         mov     ebx, [draw_limits.right]
  3380.         mov     [eax + RECT.right], ebx
  3381.         mov     ebx, [draw_limits.bottom]
  3382.         mov     [eax + RECT.bottom], ebx
  3383.  
  3384.         sub     eax, draw_data-window_data
  3385.  
  3386.         cmp     dword [esp], 1
  3387.         jne     nobgrd
  3388.         inc     byte[REDRAW_BACKGROUND]
  3389.  
  3390.        newdw8:
  3391.        nobgrd:
  3392.  
  3393.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3394.  
  3395.        ricino:
  3396.  
  3397.        not_this_task:
  3398.  
  3399.         pop     ecx
  3400.  
  3401.         cmp     ecx, [TASK_COUNT]
  3402.         jle     newdw2
  3403.  
  3404.         pop     eax
  3405.         popad
  3406.  
  3407.         ret
  3408.  
  3409. calculatebackground:   ; background
  3410.  
  3411.         mov     edi, [_WinMapAddress]              ; set os to use all pixels
  3412.         mov     eax, 0x01010101
  3413.         mov     ecx, [_WinMapSize]
  3414.         shr     ecx, 2
  3415.         rep stosd
  3416.  
  3417.         mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
  3418.         mov     byte[BACKGROUND_CHANGED], 0
  3419.  
  3420.         ret
  3421.  
  3422. uglobal
  3423.   imax    dd 0x0
  3424. endg
  3425.  
  3426.  
  3427.  
  3428. delay_ms:     ; delay in 1/1000 sec
  3429.  
  3430.  
  3431.         push    eax
  3432.         push    ecx
  3433.  
  3434.         mov     ecx, esi
  3435.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3436.         imul    ecx, 33941
  3437.         shr     ecx, 9
  3438.         ; </CPU clock fix>
  3439.  
  3440.         in      al, 0x61
  3441.         and     al, 0x10
  3442.         mov     ah, al
  3443.         cld
  3444.  
  3445.  cnt1:
  3446.         in      al, 0x61
  3447.         and     al, 0x10
  3448.         cmp     al, ah
  3449.         jz      cnt1
  3450.  
  3451.         mov     ah, al
  3452.         loop    cnt1
  3453.  
  3454.         pop     ecx
  3455.         pop     eax
  3456.  
  3457.         ret
  3458.  
  3459. align 4
  3460. set_app_param:
  3461.         mov     edi, [TASK_BASE]
  3462.         mov     eax, ebx
  3463.         btr     eax, 3                           ; move MOUSE_FILTRATION
  3464.         mov     ebx, [current_slot]              ; bit into event_filter
  3465.         setc    byte [ebx+APPDATA.event_filter]
  3466.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3467.         mov     [esp+32], eax                    ; return old mask value
  3468.         ret
  3469.  
  3470.  
  3471.  
  3472. delay_hs:     ; delay in 1/100 secs
  3473. ; ebx = delay time
  3474.         push    ecx
  3475.         push    edx
  3476.  
  3477.         mov     edx, [timer_ticks]
  3478.  
  3479.       newtic:
  3480.         mov     ecx, [timer_ticks]
  3481.         sub     ecx, edx
  3482.         cmp     ecx, ebx
  3483.         jae     zerodelay
  3484.  
  3485.         call    change_task
  3486.  
  3487.         jmp     newtic
  3488.  
  3489.       zerodelay:
  3490.         pop     edx
  3491.         pop     ecx
  3492.  
  3493.         ret
  3494.  
  3495. align 16        ;very often call this subrutine
  3496. memmove:       ; memory move in bytes
  3497.  
  3498. ; eax = from
  3499. ; ebx = to
  3500. ; ecx = no of bytes
  3501.         test    ecx, ecx
  3502.         jle     .ret
  3503.  
  3504.         push    esi edi ecx
  3505.  
  3506.         mov     edi, ebx
  3507.         mov     esi, eax
  3508.  
  3509.         test    ecx, not 11b
  3510.         jz      @f
  3511.  
  3512.         push    ecx
  3513.         shr     ecx, 2
  3514.         rep movsd
  3515.         pop     ecx
  3516.         and     ecx, 11b
  3517.         jz      .finish
  3518.   @@:
  3519.         rep movsb
  3520.  
  3521.   .finish:
  3522.         pop     ecx edi esi
  3523.   .ret:
  3524.         ret
  3525.  
  3526.  
  3527. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3528. ;align 4
  3529. ;
  3530. ;read_floppy_file:
  3531. ;
  3532. ;; as input
  3533. ;;
  3534. ;; eax pointer to file
  3535. ;; ebx file lenght
  3536. ;; ecx start 512 byte block number
  3537. ;; edx number of blocks to read
  3538. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3539. ;;
  3540. ;;
  3541. ;; on return
  3542. ;;
  3543. ;; eax = 0 command succesful
  3544. ;;       1 no fd base and/or partition defined
  3545. ;;       2 yet unsupported FS
  3546. ;;       3 unknown FS
  3547. ;;       4 partition not defined at hd
  3548. ;;       5 file not found
  3549. ;; ebx = size of file
  3550. ;
  3551. ;     mov   edi,[TASK_BASE]
  3552. ;     add   edi,0x10
  3553. ;     add   esi,[edi]
  3554. ;     add   eax,[edi]
  3555. ;
  3556. ;     pushad
  3557. ;     mov  edi,esi
  3558. ;     add  edi,1024
  3559. ;     mov  esi,0x100000+19*512
  3560. ;     sub  ecx,1
  3561. ;     shl  ecx,9
  3562. ;     add  esi,ecx
  3563. ;     shl  edx,9
  3564. ;     mov  ecx,edx
  3565. ;     cld
  3566. ;     rep  movsb
  3567. ;     popad
  3568. ;
  3569. ;     mov   [esp+36],eax
  3570. ;     mov   [esp+24],ebx
  3571. ;     ret
  3572.  
  3573.  
  3574.  
  3575. align 4
  3576. set_io_access_rights:
  3577.         push    edi eax
  3578.         mov     edi, tss._io_map_0
  3579. ;     mov   ecx,eax
  3580. ;     and   ecx,7    ; offset in byte
  3581. ;     shr   eax,3    ; number of byte
  3582. ;     add   edi,eax
  3583. ;     mov   ebx,1
  3584. ;     shl   ebx,cl
  3585.         test    ebp, ebp
  3586. ;     cmp   ebp,0                ; enable access - ebp = 0
  3587.         jnz     .siar1
  3588. ;     not   ebx
  3589. ;     and   [edi],byte bl
  3590.         btr     [edi], eax
  3591.         pop     eax edi
  3592.         ret
  3593. .siar1:
  3594.         bts     [edi], eax
  3595.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3596.         pop     eax edi
  3597.         ret
  3598. ;reserve/free group of ports
  3599. ;  * eax = 46 - number function
  3600. ;  * ebx = 0 - reserve, 1 - free
  3601. ;  * ecx = number start arrea of ports
  3602. ;  * edx = number end arrea of ports (include last number of port)
  3603. ;Return value:
  3604. ;  * eax = 0 - succesful
  3605. ;  * eax = 1 - error
  3606. ;  * The system has reserve this ports:
  3607. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3608. ;destroys eax,ebx, ebp
  3609. r_f_port_area:
  3610.  
  3611.         test    ebx, ebx
  3612.         jnz     free_port_area
  3613. ;     je    r_port_area
  3614. ;     jmp   free_port_area
  3615.  
  3616. ;   r_port_area:
  3617.  
  3618. ;     pushad
  3619.  
  3620.         cmp     ecx, edx      ; beginning > end ?
  3621.         ja      rpal1
  3622.         cmp     edx, 65536
  3623.         jae     rpal1
  3624.         mov     eax, [RESERVED_PORTS]
  3625.         test    eax, eax      ; no reserved areas ?
  3626.         je      rpal2
  3627.         cmp     eax, 255      ; max reserved
  3628.         jae     rpal1
  3629.  rpal3:
  3630.         mov     ebx, eax
  3631.         shl     ebx, 4
  3632.         add     ebx, RESERVED_PORTS
  3633.         cmp     ecx, [ebx+8]
  3634.         ja      rpal4
  3635.         cmp     edx, [ebx+4]
  3636.         jae     rpal1
  3637. ;     jb    rpal4
  3638. ;     jmp   rpal1
  3639.  rpal4:
  3640.         dec     eax
  3641.         jnz     rpal3
  3642.         jmp     rpal2
  3643.    rpal1:
  3644. ;     popad
  3645. ;     mov   eax,1
  3646.         xor     eax, eax
  3647.         inc     eax
  3648.         ret
  3649.    rpal2:
  3650. ;     popad
  3651.      ; enable port access at port IO map
  3652.         cli
  3653.         pushad                        ; start enable io map
  3654.  
  3655.         cmp     edx, 65536;16384
  3656.         jae     no_unmask_io; jge
  3657.         mov     eax, ecx
  3658. ;       push    ebp
  3659.         xor     ebp, ebp               ; enable - eax = port
  3660. new_port_access:
  3661. ;     pushad
  3662.         call    set_io_access_rights
  3663. ;     popad
  3664.         inc     eax
  3665.         cmp     eax, edx
  3666.         jbe     new_port_access
  3667. ;       pop     ebp
  3668. no_unmask_io:
  3669.         popad                         ; end enable io map
  3670.         sti
  3671.  
  3672.         mov     eax, [RESERVED_PORTS]
  3673.         add     eax, 1
  3674.         mov     [RESERVED_PORTS], eax
  3675.         shl     eax, 4
  3676.         add     eax, RESERVED_PORTS
  3677.         mov     ebx, [TASK_BASE]
  3678.         mov     ebx, [ebx+TASKDATA.pid]
  3679.         mov     [eax], ebx
  3680.         mov     [eax+4], ecx
  3681.         mov     [eax+8], edx
  3682.  
  3683.         xor     eax, eax
  3684.         ret
  3685.  
  3686. free_port_area:
  3687.  
  3688. ;     pushad
  3689.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3690.         test    eax, eax
  3691.         jz      frpal2
  3692.         mov     ebx, [TASK_BASE]
  3693.         mov     ebx, [ebx+TASKDATA.pid]
  3694.    frpal3:
  3695.         mov     edi, eax
  3696.         shl     edi, 4
  3697.         add     edi, RESERVED_PORTS
  3698.         cmp     ebx, [edi]
  3699.         jne     frpal4
  3700.         cmp     ecx, [edi+4]
  3701.         jne     frpal4
  3702.         cmp     edx, [edi+8]
  3703.         jne     frpal4
  3704.         jmp     frpal1
  3705.    frpal4:
  3706.         dec     eax
  3707.         jnz     frpal3
  3708.    frpal2:
  3709. ;     popad
  3710.         inc     eax
  3711.         ret
  3712.    frpal1:
  3713.         push    ecx
  3714.         mov     ecx, 256
  3715.         sub     ecx, eax
  3716.         shl     ecx, 4
  3717.         mov     esi, edi
  3718.         add     esi, 16
  3719.         cld
  3720.         rep movsb
  3721.  
  3722.         dec     dword [RESERVED_PORTS]
  3723. ;popad
  3724. ;disable port access at port IO map
  3725.  
  3726. ;     pushad                        ; start disable io map
  3727.         pop     eax     ;start port
  3728.         cmp     edx, 65536;16384
  3729.         jge     no_mask_io
  3730.  
  3731. ;     mov   eax,ecx
  3732.         xor     ebp, ebp
  3733.         inc     ebp
  3734. new_port_access_disable:
  3735. ;     pushad
  3736. ;     mov   ebp,1                  ; disable - eax = port
  3737.         call    set_io_access_rights
  3738. ;     popad
  3739.         inc     eax
  3740.         cmp     eax, edx
  3741.         jbe     new_port_access_disable
  3742. no_mask_io:
  3743. ;     popad                         ; end disable io map
  3744.         xor     eax, eax
  3745.         ret
  3746. ;-----------------------------------------------------------------------------
  3747. align 4
  3748. drawbackground:
  3749. dbrv20:
  3750.         cmp     [BgrDrawMode], dword 1
  3751.         jne     bgrstr
  3752.         call    vesa20_drawbackground_tiled
  3753. ;        call    [draw_pointer]
  3754.         call    __sys_draw_pointer
  3755.         ret
  3756. ;--------------------------------------
  3757. align 4
  3758. bgrstr:
  3759.         call    vesa20_drawbackground_stretch
  3760. ;        call    [draw_pointer]
  3761.         call    __sys_draw_pointer
  3762.         ret
  3763. ;-----------------------------------------------------------------------------
  3764. align 4
  3765. syscall_putimage:                       ; PutImage
  3766. sys_putimage:
  3767.         test    ecx, 0x80008000
  3768.         jnz     .exit
  3769.         test    ecx, 0x0000FFFF
  3770.         jz      .exit
  3771.         test    ecx, 0xFFFF0000
  3772.         jnz     @f
  3773. ;--------------------------------------
  3774. align 4
  3775. .exit:
  3776.         ret
  3777. ;--------------------------------------
  3778. align 4
  3779. @@:
  3780.         mov     edi, [current_slot]
  3781.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3782.         rol     edx, 16
  3783.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3784.         rol     edx, 16
  3785. ;--------------------------------------
  3786. align 4
  3787. .forced:
  3788.         push    ebp esi 0
  3789.         mov     ebp, putimage_get24bpp
  3790.         mov     esi, putimage_init24bpp
  3791. ;--------------------------------------
  3792. align 4
  3793. sys_putimage_bpp:
  3794.         call    vesa20_putimage
  3795.         pop     ebp esi ebp
  3796.         ret
  3797. ;        jmp     [draw_pointer]
  3798. ;-----------------------------------------------------------------------------
  3799. align 4
  3800. sys_putimage_palette:
  3801. ; ebx = pointer to image
  3802. ; ecx = [xsize]*65536 + [ysize]
  3803. ; edx = [xstart]*65536 + [ystart]
  3804. ; esi = number of bits per pixel, must be 8, 24 or 32
  3805. ; edi = pointer to palette
  3806. ; ebp = row delta
  3807.         mov     eax, [CURRENT_TASK]
  3808.         shl     eax, 8
  3809.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3810.         rol     edx, 16
  3811.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3812.         rol     edx, 16
  3813. ;--------------------------------------
  3814. align 4
  3815. .forced:
  3816.         cmp     esi, 1
  3817.         jnz     @f
  3818.         push    edi
  3819.         mov     eax, [edi+4]
  3820.         sub     eax, [edi]
  3821.         push    eax
  3822.         push    dword [edi]
  3823.         push    0ffffff80h
  3824.         mov     edi, esp
  3825.         call    put_mono_image
  3826.         add     esp, 12
  3827.         pop     edi
  3828.         ret
  3829. ;--------------------------------------
  3830. align 4
  3831. @@:
  3832.         cmp     esi, 2
  3833.         jnz     @f
  3834.         push    edi
  3835.         push    0ffffff80h
  3836.         mov     edi, esp
  3837.         call    put_2bit_image
  3838.         pop     eax
  3839.         pop     edi
  3840.         ret
  3841. ;--------------------------------------
  3842. align 4
  3843. @@:
  3844.         cmp     esi, 4
  3845.         jnz     @f
  3846.         push    edi
  3847.         push    0ffffff80h
  3848.         mov     edi, esp
  3849.         call    put_4bit_image
  3850.         pop     eax
  3851.         pop     edi
  3852.         ret
  3853. ;--------------------------------------
  3854. align 4
  3855. @@:
  3856.         push    ebp esi ebp
  3857.         cmp     esi, 8
  3858.         jnz     @f
  3859.         mov     ebp, putimage_get8bpp
  3860.         mov     esi, putimage_init8bpp
  3861.         jmp     sys_putimage_bpp
  3862. ;--------------------------------------
  3863. align 4
  3864. @@:
  3865.         cmp     esi, 15
  3866.         jnz     @f
  3867.         mov     ebp, putimage_get15bpp
  3868.         mov     esi, putimage_init15bpp
  3869.         jmp     sys_putimage_bpp
  3870. ;--------------------------------------
  3871. align 4
  3872. @@:
  3873.         cmp     esi, 16
  3874.         jnz     @f
  3875.         mov     ebp, putimage_get16bpp
  3876.         mov     esi, putimage_init16bpp
  3877.         jmp     sys_putimage_bpp
  3878. ;--------------------------------------
  3879. align 4
  3880. @@:
  3881.         cmp     esi, 24
  3882.         jnz     @f
  3883.         mov     ebp, putimage_get24bpp
  3884.         mov     esi, putimage_init24bpp
  3885.         jmp     sys_putimage_bpp
  3886. ;--------------------------------------
  3887. align 4
  3888. @@:
  3889.         cmp     esi, 32
  3890.         jnz     @f
  3891.         mov     ebp, putimage_get32bpp
  3892.         mov     esi, putimage_init32bpp
  3893.         jmp     sys_putimage_bpp
  3894. ;--------------------------------------
  3895. align 4
  3896. @@:
  3897.         pop     ebp esi ebp
  3898.         ret
  3899. ;-----------------------------------------------------------------------------
  3900. align 4
  3901. put_mono_image:
  3902.         push    ebp esi ebp
  3903.         mov     ebp, putimage_get1bpp
  3904.         mov     esi, putimage_init1bpp
  3905.         jmp     sys_putimage_bpp
  3906. ;-----------------------------------------------------------------------------
  3907. align 4
  3908. put_2bit_image:
  3909.         push    ebp esi ebp
  3910.         mov     ebp, putimage_get2bpp
  3911.         mov     esi, putimage_init2bpp
  3912.         jmp     sys_putimage_bpp
  3913. ;-----------------------------------------------------------------------------
  3914. align 4
  3915. put_4bit_image:
  3916.         push    ebp esi ebp
  3917.         mov     ebp, putimage_get4bpp
  3918.         mov     esi, putimage_init4bpp
  3919.         jmp     sys_putimage_bpp
  3920. ;-----------------------------------------------------------------------------
  3921. align 4
  3922. putimage_init24bpp:
  3923.         lea     eax, [eax*3]
  3924. putimage_init8bpp:
  3925.         ret
  3926. ;-----------------------------------------------------------------------------
  3927. align 16
  3928. putimage_get24bpp:
  3929.         movzx   eax, byte [esi+2]
  3930.         shl     eax, 16
  3931.         mov     ax, [esi]
  3932.         add     esi, 3
  3933.         ret     4
  3934. ;-----------------------------------------------------------------------------
  3935. align 16
  3936. putimage_get8bpp:
  3937.         movzx   eax, byte [esi]
  3938.         push    edx
  3939.         mov     edx, [esp+8]
  3940.         mov     eax, [edx+eax*4]
  3941.         pop     edx
  3942.         inc     esi
  3943.         ret     4
  3944. ;-----------------------------------------------------------------------------
  3945. align 4
  3946. putimage_init1bpp:
  3947.         add     eax, ecx
  3948.         push    ecx
  3949.         add     eax, 7
  3950.         add     ecx, 7
  3951.         shr     eax, 3
  3952.         shr     ecx, 3
  3953.         sub     eax, ecx
  3954.         pop     ecx
  3955.         ret
  3956. ;-----------------------------------------------------------------------------
  3957. align 16
  3958. putimage_get1bpp:
  3959.         push    edx
  3960.         mov     edx, [esp+8]
  3961.         mov     al, [edx]
  3962.         add     al, al
  3963.         jnz     @f
  3964.         lodsb
  3965.         adc     al, al
  3966. @@:
  3967.         mov     [edx], al
  3968.         sbb     eax, eax
  3969.         and     eax, [edx+8]
  3970.         add     eax, [edx+4]
  3971.         pop     edx
  3972.         ret     4
  3973. ;-----------------------------------------------------------------------------
  3974. align 4
  3975. putimage_init2bpp:
  3976.         add     eax, ecx
  3977.         push    ecx
  3978.         add     ecx, 3
  3979.         add     eax, 3
  3980.         shr     ecx, 2
  3981.         shr     eax, 2
  3982.         sub     eax, ecx
  3983.         pop     ecx
  3984.         ret
  3985. ;-----------------------------------------------------------------------------
  3986. align 16
  3987. putimage_get2bpp:
  3988.         push    edx
  3989.         mov     edx, [esp+8]
  3990.         mov     al, [edx]
  3991.         mov     ah, al
  3992.         shr     al, 6
  3993.         shl     ah, 2
  3994.         jnz     .nonewbyte
  3995.         lodsb
  3996.         mov     ah, al
  3997.         shr     al, 6
  3998.         shl     ah, 2
  3999.         add     ah, 1
  4000. .nonewbyte:
  4001.         mov     [edx], ah
  4002.         mov     edx, [edx+4]
  4003.         movzx   eax, al
  4004.         mov     eax, [edx+eax*4]
  4005.         pop     edx
  4006.         ret     4
  4007. ;-----------------------------------------------------------------------------
  4008. align 4
  4009. putimage_init4bpp:
  4010.         add     eax, ecx
  4011.         push    ecx
  4012.         add     ecx, 1
  4013.         add     eax, 1
  4014.         shr     ecx, 1
  4015.         shr     eax, 1
  4016.         sub     eax, ecx
  4017.         pop     ecx
  4018.         ret
  4019. ;-----------------------------------------------------------------------------
  4020. align 16
  4021. putimage_get4bpp:
  4022.         push    edx
  4023.         mov     edx, [esp+8]
  4024.         add     byte [edx], 80h
  4025.         jc      @f
  4026.         movzx   eax, byte [edx+1]
  4027.         mov     edx, [edx+4]
  4028.         and     eax, 0x0F
  4029.         mov     eax, [edx+eax*4]
  4030.         pop     edx
  4031.         ret     4
  4032. @@:
  4033.         movzx   eax, byte [esi]
  4034.         add     esi, 1
  4035.         mov     [edx+1], al
  4036.         shr     eax, 4
  4037.         mov     edx, [edx+4]
  4038.         mov     eax, [edx+eax*4]
  4039.         pop     edx
  4040.         ret     4
  4041. ;-----------------------------------------------------------------------------
  4042. align 4
  4043. putimage_init32bpp:
  4044.         shl     eax, 2
  4045.         ret
  4046. ;-----------------------------------------------------------------------------
  4047. align 16
  4048. putimage_get32bpp:
  4049.         lodsd
  4050.         ret     4
  4051. ;-----------------------------------------------------------------------------
  4052. align 4
  4053. putimage_init15bpp:
  4054. putimage_init16bpp:
  4055.         add     eax, eax
  4056.         ret
  4057. ;-----------------------------------------------------------------------------
  4058. align 16
  4059. putimage_get15bpp:
  4060. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4061.         push    ecx edx
  4062.         movzx   eax, word [esi]
  4063.         add     esi, 2
  4064.         mov     ecx, eax
  4065.         mov     edx, eax
  4066.         and     eax, 0x1F
  4067.         and     ecx, 0x1F shl 5
  4068.         and     edx, 0x1F shl 10
  4069.         shl     eax, 3
  4070.         shl     ecx, 6
  4071.         shl     edx, 9
  4072.         or      eax, ecx
  4073.         or      eax, edx
  4074.         pop     edx ecx
  4075.         ret     4
  4076. ;-----------------------------------------------------------------------------
  4077. align 16
  4078. putimage_get16bpp:
  4079. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4080.         push    ecx edx
  4081.         movzx   eax, word [esi]
  4082.         add     esi, 2
  4083.         mov     ecx, eax
  4084.         mov     edx, eax
  4085.         and     eax, 0x1F
  4086.         and     ecx, 0x3F shl 5
  4087.         and     edx, 0x1F shl 11
  4088.         shl     eax, 3
  4089.         shl     ecx, 5
  4090.         shl     edx, 8
  4091.         or      eax, ecx
  4092.         or      eax, edx
  4093.         pop     edx ecx
  4094.         ret     4
  4095. ;-----------------------------------------------------------------------------
  4096. ;align 4
  4097. ; eax x beginning
  4098. ; ebx y beginning
  4099. ; ecx x end
  4100.         ; edx y end
  4101. ; edi color
  4102. ;__sys_drawbar:
  4103. ;        mov     esi, [current_slot]
  4104. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4105. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4106. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4107. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4108. ;--------------------------------------
  4109. ;align 4
  4110. ;.forced:
  4111. ;        call    vesa20_drawbar
  4112. ;        call    [draw_pointer]
  4113. ;        ret
  4114. ;-----------------------------------------------------------------------------
  4115. align 4
  4116. kb_read:
  4117.  
  4118.         push    ecx edx
  4119.  
  4120.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4121.       kr_loop:
  4122.         in      al, 0x64
  4123.         test    al, 1
  4124.         jnz     kr_ready
  4125.         loop    kr_loop
  4126.         mov     ah, 1
  4127.         jmp     kr_exit
  4128.       kr_ready:
  4129.         push    ecx
  4130.         mov     ecx, 32
  4131.       kr_delay:
  4132.         loop    kr_delay
  4133.         pop     ecx
  4134.         in      al, 0x60
  4135.         xor     ah, ah
  4136.       kr_exit:
  4137.  
  4138.         pop     edx ecx
  4139.  
  4140.         ret
  4141. ;-----------------------------------------------------------------------------
  4142. align 4
  4143. kb_write:
  4144.  
  4145.         push    ecx edx
  4146.  
  4147.         mov     dl, al
  4148. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4149. ;      kw_loop1:
  4150. ;        in      al,0x64
  4151. ;        test    al,0x20
  4152. ;        jz      kw_ok1
  4153. ;        loop    kw_loop1
  4154. ;        mov     ah,1
  4155. ;        jmp     kw_exit
  4156. ;      kw_ok1:
  4157.         in      al, 0x60
  4158.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4159.       kw_loop:
  4160.         in      al, 0x64
  4161.         test    al, 2
  4162.         jz      kw_ok
  4163.         loop    kw_loop
  4164.         mov     ah, 1
  4165.         jmp     kw_exit
  4166.       kw_ok:
  4167.         mov     al, dl
  4168.         out     0x60, al
  4169.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4170.       kw_loop3:
  4171.         in      al, 0x64
  4172.         test    al, 2
  4173.         jz      kw_ok3
  4174.         loop    kw_loop3
  4175.         mov     ah, 1
  4176.         jmp     kw_exit
  4177.       kw_ok3:
  4178.         mov     ah, 8
  4179.       kw_loop4:
  4180.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4181.       kw_loop5:
  4182.         in      al, 0x64
  4183.         test    al, 1
  4184.         jnz     kw_ok4
  4185.         loop    kw_loop5
  4186.         dec     ah
  4187.         jnz     kw_loop4
  4188.       kw_ok4:
  4189.         xor     ah, ah
  4190.       kw_exit:
  4191.  
  4192.         pop     edx ecx
  4193.  
  4194.         ret
  4195. ;-----------------------------------------------------------------------------
  4196. align 4
  4197. kb_cmd:
  4198.  
  4199.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4200.       c_wait:
  4201.         in      al, 0x64
  4202.         test    al, 2
  4203.         jz      c_send
  4204.         loop    c_wait
  4205.         jmp     c_error
  4206.       c_send:
  4207.         mov     al, bl
  4208.         out     0x64, al
  4209.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4210.       c_accept:
  4211.         in      al, 0x64
  4212.         test    al, 2
  4213.         jz      c_ok
  4214.         loop    c_accept
  4215.       c_error:
  4216.         mov     ah, 1
  4217.         jmp     c_exit
  4218.       c_ok:
  4219.         xor     ah, ah
  4220.       c_exit:
  4221.         ret
  4222.  
  4223.  
  4224. setmouse:  ; set mousepicture -pointer
  4225.            ; ps2 mouse enable
  4226.  
  4227. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4228.  
  4229.         cli
  4230.  
  4231.         ret
  4232.  
  4233. if used _rdtsc
  4234. _rdtsc:
  4235.         bt      [cpu_caps], CAPS_TSC
  4236.         jnc     ret_rdtsc
  4237.         rdtsc
  4238.         ret
  4239.    ret_rdtsc:
  4240.         mov     edx, 0xffffffff
  4241.         mov     eax, 0xffffffff
  4242.         ret
  4243. end if
  4244.  
  4245. sys_msg_board_str:
  4246.  
  4247.         pushad
  4248.    @@:
  4249.         cmp     [esi], byte 0
  4250.         je      @f
  4251.         mov     eax, 1
  4252.         movzx   ebx, byte [esi]
  4253.         call    sys_msg_board
  4254.         inc     esi
  4255.         jmp     @b
  4256.    @@:
  4257.         popad
  4258.         ret
  4259.  
  4260. sys_msg_board_byte:
  4261. ; in: al = byte to display
  4262. ; out: nothing
  4263. ; destroys: nothing
  4264.         pushad
  4265.         mov     ecx, 2
  4266.         shl     eax, 24
  4267.         jmp     @f
  4268.  
  4269. sys_msg_board_word:
  4270. ; in: ax = word to display
  4271. ; out: nothing
  4272. ; destroys: nothing
  4273.         pushad
  4274.         mov     ecx, 4
  4275.         shl     eax, 16
  4276.         jmp     @f
  4277.  
  4278. sys_msg_board_dword:
  4279. ; in: eax = dword to display
  4280. ; out: nothing
  4281. ; destroys: nothing
  4282.         pushad
  4283.         mov     ecx, 8
  4284. @@:
  4285.         push    ecx
  4286.         rol     eax, 4
  4287.         push    eax
  4288.         and     al, 0xF
  4289.         cmp     al, 10
  4290.         sbb     al, 69h
  4291.         das
  4292.         mov     bl, al
  4293.         xor     eax, eax
  4294.         inc     eax
  4295.         call    sys_msg_board
  4296.         pop     eax
  4297.         pop     ecx
  4298.         loop    @b
  4299.         popad
  4300.         ret
  4301.  
  4302. uglobal
  4303.   msg_board_data:
  4304.                   times 4096 db 0
  4305.   msg_board_count dd 0x0
  4306. endg
  4307.  
  4308. sys_msg_board:
  4309.  
  4310. ; eax=1 : write :  bl byte to write
  4311. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4312.  
  4313.         mov     ecx, [msg_board_count]
  4314.         cmp     eax, 1
  4315.         jne     .smbl1
  4316.  
  4317. if defined debug_com_base
  4318.  
  4319.         push    dx ax
  4320.  
  4321.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4322.         mov     dx, debug_com_base+5
  4323.         in      al, dx
  4324.         test    al, 1 shl 5
  4325.         jz      @r
  4326.  
  4327.         mov     dx, debug_com_base      ; Output the byte
  4328.         mov     al, bl
  4329.         out     dx, al
  4330.  
  4331.         pop     ax dx
  4332.  
  4333. end if
  4334.  
  4335.         mov     [msg_board_data+ecx], bl
  4336.         inc     ecx
  4337.         and     ecx, 4095
  4338.         mov     [msg_board_count], ecx
  4339.         mov     [check_idle_semaphore], 5
  4340.         ret
  4341. .smbl1:
  4342.         cmp     eax, 2
  4343.         jne     .smbl2
  4344.         test    ecx, ecx
  4345.         jz      .smbl21
  4346.         mov     eax, msg_board_data+1
  4347.         mov     ebx, msg_board_data
  4348.         movzx   edx, byte [ebx]
  4349.         call    memmove
  4350.         dec     [msg_board_count]
  4351.         mov     [esp + 36], edx ;eax
  4352.         mov     [esp + 24], dword 1
  4353.         ret
  4354. .smbl21:
  4355.         mov     [esp+36], ecx
  4356.         mov     [esp+24], ecx
  4357. .smbl2:
  4358.         ret
  4359.  
  4360. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4361. ;; 66 sys function.                                                ;;
  4362. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4363. ;; out eax                                                         ;;
  4364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4365. iglobal
  4366. align 4
  4367. f66call:
  4368.            dd sys_process_def.1   ; 1 = set keyboard mode
  4369.            dd sys_process_def.2   ; 2 = get keyboard mode
  4370.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4371.            dd sys_process_def.4
  4372.            dd sys_process_def.5
  4373. endg
  4374.  
  4375.  
  4376.  
  4377.  
  4378. sys_process_def:
  4379.         dec     ebx
  4380.         cmp     ebx, 5
  4381.         jae     .not_support    ;if >=6 then or eax,-1
  4382.  
  4383.         mov     edi, [CURRENT_TASK]
  4384.         jmp     dword [f66call+ebx*4]
  4385.  
  4386. .not_support:
  4387.         or      eax, -1
  4388.         ret
  4389.  
  4390. .1:
  4391.         shl     edi, 8
  4392.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4393.  
  4394.         ret
  4395.  
  4396. .2:                             ; 2 = get keyboard mode
  4397.         shl     edi, 8
  4398.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4399.         mov     [esp+32], eax
  4400.         ret
  4401. ;     xor   eax,eax
  4402. ;     movzx eax,byte [shift]
  4403. ;     movzx ebx,byte [ctrl]
  4404. ;     shl   ebx,2
  4405. ;     add   eax,ebx
  4406. ;     movzx ebx,byte [alt]
  4407. ;     shl   ebx,3
  4408. ;     add   eax,ebx
  4409. .3:                             ;3 = get keyboard ctrl, alt, shift
  4410.  ;// mike.dld [
  4411.         mov     eax, [kb_state]
  4412.  ;// mike.dld ]
  4413.         mov     [esp+32], eax
  4414.         ret
  4415.  
  4416. .4:
  4417.         mov     eax, hotkey_list
  4418. @@:
  4419.         cmp     dword [eax+8], 0
  4420.         jz      .found_free
  4421.         add     eax, 16
  4422.         cmp     eax, hotkey_list+16*256
  4423.         jb      @b
  4424.         mov     dword [esp+32], 1
  4425.         ret
  4426. .found_free:
  4427.         mov     [eax+8], edi
  4428.         mov     [eax+4], edx
  4429.         movzx   ecx, cl
  4430.         lea     ecx, [hotkey_scancodes+ecx*4]
  4431.         mov     edx, [ecx]
  4432.         mov     [eax], edx
  4433.         mov     [ecx], eax
  4434.         mov     [eax+12], ecx
  4435.         jecxz   @f
  4436.         mov     [edx+12], eax
  4437. @@:
  4438.         and     dword [esp+32], 0
  4439.         ret
  4440.  
  4441. .5:
  4442.         movzx   ebx, cl
  4443.         lea     ebx, [hotkey_scancodes+ebx*4]
  4444.         mov     eax, [ebx]
  4445. .scan:
  4446.         test    eax, eax
  4447.         jz      .notfound
  4448.         cmp     [eax+8], edi
  4449.         jnz     .next
  4450.         cmp     [eax+4], edx
  4451.         jz      .found
  4452. .next:
  4453.         mov     eax, [eax]
  4454.         jmp     .scan
  4455. .notfound:
  4456.         mov     dword [esp+32], 1
  4457.         ret
  4458. .found:
  4459.         mov     ecx, [eax]
  4460.         jecxz   @f
  4461.         mov     edx, [eax+12]
  4462.         mov     [ecx+12], edx
  4463. @@:
  4464.         mov     ecx, [eax+12]
  4465.         mov     edx, [eax]
  4466.         mov     [ecx], edx
  4467.         xor     edx, edx
  4468.         mov     [eax+4], edx
  4469.         mov     [eax+8], edx
  4470.         mov     [eax+12], edx
  4471.         mov     [eax], edx
  4472.         mov     [esp+32], edx
  4473.         ret
  4474.  
  4475.  
  4476. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4477. ;; 61 sys function.                                                ;;
  4478. ;; in eax=61,ebx in [1..3]                                         ;;
  4479. ;; out eax                                                         ;;
  4480. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4481. iglobal
  4482. align 4
  4483. f61call:
  4484.            dd sys_gs.1   ; resolution
  4485.            dd sys_gs.2   ; bits per pixel
  4486.            dd sys_gs.3   ; bytes per scanline
  4487. endg
  4488.  
  4489.  
  4490. align 4
  4491.  
  4492. sys_gs:                         ; direct screen access
  4493.         dec     ebx
  4494.         cmp     ebx, 2
  4495.         ja      .not_support
  4496.         jmp     dword [f61call+ebx*4]
  4497. .not_support:
  4498.         or      [esp+32], dword -1
  4499.         ret
  4500.  
  4501.  
  4502. .1:                             ; resolution
  4503.         mov     eax, [Screen_Max_X]
  4504.         shl     eax, 16
  4505.         mov     ax, [Screen_Max_Y]
  4506.         add     eax, 0x00010001
  4507.         mov     [esp+32], eax
  4508.         ret
  4509. .2:                             ; bits per pixel
  4510.         movzx   eax, byte [ScreenBPP]
  4511.         mov     [esp+32], eax
  4512.         ret
  4513. .3:                             ; bytes per scanline
  4514.         mov     eax, [BytesPerScanLine]
  4515.         mov     [esp+32], eax
  4516.         ret
  4517.  
  4518. align 4  ;  system functions
  4519.  
  4520. syscall_setpixel:                       ; SetPixel
  4521.  
  4522.         mov     eax, ebx
  4523.         mov     ebx, ecx
  4524.         mov     ecx, edx
  4525.         mov     edx, [TASK_BASE]
  4526.         add     eax, [edx-twdw+WDATA.box.left]
  4527.         add     ebx, [edx-twdw+WDATA.box.top]
  4528.         mov     edi, [current_slot]
  4529.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4530.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4531.         xor     edi, edi ; no force
  4532.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  4533. ;        jmp     [putpixel]
  4534.         jmp     __sys_putpixel
  4535.  
  4536. align 4
  4537.  
  4538. syscall_writetext:                      ; WriteText
  4539.  
  4540.         mov     eax, [TASK_BASE]
  4541.         mov     ebp, [eax-twdw+WDATA.box.left]
  4542.         push    esi
  4543.         mov     esi, [current_slot]
  4544.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4545.         shl     ebp, 16
  4546.         add     ebp, [eax-twdw+WDATA.box.top]
  4547.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  4548.         pop     esi
  4549.         add     ebx, ebp
  4550.         mov     eax, edi
  4551.         xor     edi, edi
  4552.         jmp     dtext
  4553.  
  4554. align 4
  4555.  
  4556. syscall_openramdiskfile:                ; OpenRamdiskFile
  4557.  
  4558.         mov     eax, ebx
  4559.         mov     ebx, ecx
  4560.         mov     ecx, edx
  4561.         mov     edx, esi
  4562.         mov     esi, 12
  4563.         call    fileread
  4564.         mov     [esp+32], eax
  4565.         ret
  4566.  
  4567. align 4
  4568.  
  4569. syscall_drawrect:                       ; DrawRect
  4570.  
  4571.         mov     edi, edx ; color + gradient
  4572.         and     edi, 0x80FFFFFF
  4573.         test    bx, bx  ; x.size
  4574.         je      .drectr
  4575.         test    cx, cx ; y.size
  4576.         je      .drectr
  4577.  
  4578.         mov     eax, ebx ; bad idea
  4579.         mov     ebx, ecx
  4580.  
  4581.         movzx   ecx, ax ; ecx - x.size
  4582.         shr     eax, 16 ; eax - x.coord
  4583.         movzx   edx, bx ; edx - y.size
  4584.         shr     ebx, 16 ; ebx - y.coord
  4585.         mov     esi, [current_slot]
  4586.  
  4587.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4588.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4589.         add     ecx, eax
  4590.         add     edx, ebx
  4591. ;        jmp     [drawbar]
  4592.         jmp     vesa20_drawbar
  4593. .drectr:
  4594.         ret
  4595.  
  4596. align 4
  4597. syscall_getscreensize:                  ; GetScreenSize
  4598.         mov     ax, [Screen_Max_X]
  4599.         shl     eax, 16
  4600.         mov     ax, [Screen_Max_Y]
  4601.         mov     [esp + 32], eax
  4602.         ret
  4603.  
  4604. align 4
  4605.  
  4606. syscall_cdaudio:                        ; CD
  4607.  
  4608.         cmp     ebx, 4
  4609.         jb      .audio
  4610.         jz      .eject
  4611.         cmp     ebx, 5
  4612.         jnz     .ret
  4613. .load:
  4614.         call    .reserve
  4615.         call    LoadMedium
  4616.         ;call    .free
  4617.         jmp     .free
  4618. ;        ret
  4619. .eject:
  4620.         call    .reserve
  4621.         call    clear_CD_cache
  4622.         call    allow_medium_removal
  4623.         call    EjectMedium
  4624. ;        call    .free
  4625.         jmp     .free
  4626. ;        ret
  4627. .audio:
  4628.         call    sys_cd_audio
  4629.         mov     [esp+36-4], eax
  4630. .ret:
  4631.         ret
  4632.  
  4633. .reserve:
  4634.         call    reserve_cd
  4635.         mov     eax, ecx
  4636.         shr     eax, 1
  4637.         and     eax, 1
  4638.         inc     eax
  4639.         mov     [ChannelNumber], ax
  4640.         mov     eax, ecx
  4641.         and     eax, 1
  4642.         mov     [DiskNumber], al
  4643.         call    reserve_cd_channel
  4644.         and     ebx, 3
  4645.         inc     ebx
  4646.         mov     [cdpos], ebx
  4647.         add     ebx, ebx
  4648.         mov     cl, 8
  4649.         sub     cl, bl
  4650.         mov     al, [DRIVE_DATA+1]
  4651.         shr     al, cl
  4652.         test    al, 2
  4653.         jz      .free;.err
  4654.         ret
  4655. .free:
  4656.         call    free_cd_channel
  4657.         and     [cd_status], 0
  4658.         ret
  4659. .err:
  4660.         call    .free
  4661. ;        pop     eax
  4662.         ret
  4663.  
  4664. align 4
  4665.  
  4666. syscall_getpixel:                       ; GetPixel
  4667.         mov     ecx, [Screen_Max_X]
  4668.         inc     ecx
  4669.         xor     edx, edx
  4670.         mov     eax, ebx
  4671.         div     ecx
  4672.         mov     ebx, edx
  4673.         xchg    eax, ebx
  4674.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4675.         call    dword [GETPIXEL]; eax - x, ebx - y
  4676.         mov     [esp + 32], ecx
  4677.         ret
  4678.  
  4679. align 4
  4680.  
  4681. syscall_getarea:
  4682. ;eax = 36
  4683. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4684. ;ecx = [size x]*65536 + [size y]
  4685. ;edx = [start x]*65536 + [start y]
  4686.         pushad
  4687.         mov     edi, ebx
  4688.         mov     eax, edx
  4689.         shr     eax, 16
  4690.         mov     ebx, edx
  4691.         and     ebx, 0xffff
  4692.         dec     eax
  4693.         dec     ebx
  4694.      ; eax - x, ebx - y
  4695.         mov     edx, ecx
  4696.  
  4697.         shr     ecx, 16
  4698.         and     edx, 0xffff
  4699.         mov     esi, ecx
  4700.      ; ecx - size x, edx - size y
  4701.  
  4702.         mov     ebp, edx
  4703.         dec     ebp
  4704.         lea     ebp, [ebp*3]
  4705.  
  4706.         imul    ebp, esi
  4707.  
  4708.         mov     esi, ecx
  4709.         dec     esi
  4710.         lea     esi, [esi*3]
  4711.  
  4712.         add     ebp, esi
  4713.         add     ebp, edi
  4714.  
  4715.         add     ebx, edx
  4716.  
  4717. .start_y:
  4718.         push    ecx edx
  4719. .start_x:
  4720.         push    eax ebx ecx
  4721.         add     eax, ecx
  4722.  
  4723.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4724.         call    dword [GETPIXEL]; eax - x, ebx - y
  4725.  
  4726.         mov     [ebp], cx
  4727.         shr     ecx, 16
  4728.         mov     [ebp+2], cl
  4729.  
  4730.         pop     ecx ebx eax
  4731.         sub     ebp, 3
  4732.         dec     ecx
  4733.         jnz     .start_x
  4734.         pop     edx ecx
  4735.         dec     ebx
  4736.         dec     edx
  4737.         jnz     .start_y
  4738.         popad
  4739.         ret
  4740.  
  4741. align 4
  4742.  
  4743. syscall_drawline:                       ; DrawLine
  4744.  
  4745.         mov     edi, [TASK_BASE]
  4746.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4747.         mov     ebp, eax
  4748.         mov     esi, [current_slot]
  4749.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4750.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4751.         add     ebp, ebx
  4752.         shl     eax, 16
  4753.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4754.         add     eax, ebp
  4755.         mov     ebp, ebx
  4756.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4757.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4758.         add     ebp, ecx
  4759.         shl     ebx, 16
  4760.         xor     edi, edi
  4761.         add     ebx, ebp
  4762.         mov     ecx, edx
  4763. ;        jmp     [draw_line]
  4764.         jmp     __sys_draw_line
  4765.  
  4766.  
  4767. align 4
  4768. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4769.  
  4770.         call    r_f_port_area
  4771.         mov     [esp+32], eax
  4772.         ret
  4773.  
  4774. align 4
  4775. syscall_threads:                        ; CreateThreads
  4776. ; eax=1 create thread
  4777. ;
  4778. ;   ebx=thread start
  4779. ;   ecx=thread stack value
  4780. ;
  4781. ; on return : eax = pid
  4782.  
  4783.         call    new_sys_threads
  4784.  
  4785.         mov     [esp+32], eax
  4786.         ret
  4787.  
  4788. align 4
  4789.  
  4790. stack_driver_stat:
  4791.  
  4792.         call    app_stack_handler       ; Stack status
  4793.  
  4794. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4795. ;     call  change_task                 ; between sent packet
  4796.  
  4797.         mov     [esp+32], eax
  4798.         ret
  4799.  
  4800. align 4
  4801.  
  4802. socket:                                 ; Socket interface
  4803.         call    app_socket_handler
  4804.  
  4805. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4806. ;     call  change_task                 ; between sent packet
  4807.  
  4808.         mov     [esp+36], eax
  4809.         mov     [esp+24], ebx
  4810.         ret
  4811.  
  4812. align 4
  4813.  
  4814. read_from_hd:                           ; Read from hd - fn not in use
  4815.  
  4816.         mov     edi, [TASK_BASE]
  4817.         add     edi, TASKDATA.mem_start
  4818.         add     eax, [edi]
  4819.         add     ecx, [edi]
  4820.         add     edx, [edi]
  4821.         call    file_read
  4822.  
  4823.         mov     [esp+36], eax
  4824.         mov     [esp+24], ebx
  4825.  
  4826.         ret
  4827.  
  4828. paleholder:
  4829.         ret
  4830. ;------------------------------------------------------------------------------
  4831. align 4
  4832. calculate_fast_getting_offset_for_WinMapAddress:
  4833. ; calculate data area for fast getting offset to _WinMapAddress
  4834.         xor     eax, eax
  4835.         mov     ecx, [_display.height]
  4836.         inc     ecx
  4837.         mov     edi, d_width_calc_area
  4838.         cld
  4839. @@:
  4840.         stosd
  4841.         add     eax, [_display.width]
  4842.         dec     ecx
  4843.         jnz     @r
  4844.         ret
  4845. ;------------------------------------------------------------------------------
  4846. align 4
  4847. set_screen:
  4848.         cmp     eax, [Screen_Max_X]
  4849.         jne     .set
  4850.  
  4851.         cmp     edx, [Screen_Max_Y]
  4852.         jne     .set
  4853.         ret
  4854. .set:
  4855.         pushfd
  4856.         cli
  4857.  
  4858.         mov     [Screen_Max_X], eax
  4859.         mov     [Screen_Max_Y], edx
  4860.         mov     [BytesPerScanLine], ecx
  4861.  
  4862.         mov     [screen_workarea.right], eax
  4863.         mov     [screen_workarea.bottom], edx
  4864.  
  4865.         push    ebx
  4866.         push    esi
  4867.         push    edi
  4868.  
  4869.         pushad
  4870.  
  4871.         stdcall kernel_free, [_WinMapAddress]
  4872.  
  4873.         mov     eax, [_display.width]
  4874.         mul     [_display.height]
  4875.         mov     [_WinMapSize], eax
  4876.  
  4877.         stdcall kernel_alloc, eax
  4878.         mov     [_WinMapAddress], eax
  4879.         test    eax, eax
  4880.         jz      .epic_fail
  4881.  
  4882.         call    calculate_fast_getting_offset_for_WinMapAddress
  4883.  
  4884.         popad
  4885.  
  4886.         call    repos_windows
  4887.         xor     eax, eax
  4888.         xor     ebx, ebx
  4889.         mov     ecx, [Screen_Max_X]
  4890.         mov     edx, [Screen_Max_Y]
  4891.         call    calculatescreen
  4892.         pop     edi
  4893.         pop     esi
  4894.         pop     ebx
  4895.  
  4896.         popfd
  4897.         ret
  4898.  
  4899. .epic_fail:
  4900.         hlt                     ; Houston, we've had a problem
  4901.  
  4902. ; --------------- APM ---------------------
  4903. uglobal
  4904. apm_entry       dp      0
  4905. apm_vf          dd      0
  4906. endg
  4907.  
  4908. align 4
  4909. sys_apm:
  4910.         xor     eax, eax
  4911.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4912.         jne     @f
  4913.         inc     eax
  4914.         or      dword [esp + 44], eax   ; error
  4915.         add     eax, 7
  4916.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4917.         ret
  4918.  
  4919. @@:
  4920. ;       xchg    eax, ecx
  4921. ;       xchg    ebx, ecx
  4922.  
  4923.         cmp     dx, 3
  4924.         ja      @f
  4925.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4926.         mov     eax, [apm_vf]
  4927.         mov     [esp + 32], eax
  4928.         shr     eax, 16
  4929.         mov     [esp + 28], eax
  4930.         ret
  4931.  
  4932. @@:
  4933.  
  4934.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4935.         xchg    [master_tab], esi
  4936.         push    esi
  4937.         mov     edi, cr3
  4938.         mov     cr3, edi                ;flush TLB
  4939.  
  4940.         call    pword [apm_entry]       ;call APM BIOS
  4941.  
  4942.         xchg    eax, [esp]
  4943.         mov     [master_tab], eax
  4944.         mov     eax, cr3
  4945.         mov     cr3, eax
  4946.         pop     eax
  4947.  
  4948.         mov     [esp + 4 ], edi
  4949.         mov     [esp + 8], esi
  4950.         mov     [esp + 20], ebx
  4951.         mov     [esp + 24], edx
  4952.         mov     [esp + 28], ecx
  4953.         mov     [esp + 32], eax
  4954.         setc    al
  4955.         and     [esp + 44], byte 0xfe
  4956.         or      [esp + 44], al
  4957.         ret
  4958. ; -----------------------------------------
  4959.  
  4960. align 4
  4961.  
  4962. undefined_syscall:                      ; Undefined system call
  4963.         mov     [esp + 32], dword -1
  4964.         ret
  4965.  
  4966. align 4
  4967. system_shutdown:          ; shut down the system
  4968.  
  4969.         cmp     byte [BOOT_VAR+0x9030], 1
  4970.         jne     @F
  4971.         ret
  4972. @@:
  4973.         call    stop_all_services
  4974.         push    3                ; stop playing cd
  4975.         pop     eax
  4976.         call    sys_cd_audio
  4977.  
  4978. yes_shutdown_param:
  4979.         cli
  4980.  
  4981. if ~ defined extended_primary_loader
  4982.         mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
  4983.         push    12
  4984.         pop     esi
  4985.         xor     ebx, ebx
  4986.         or      ecx, -1
  4987.         mov     edx, OS_BASE+0x70000
  4988.         call    fileread
  4989.  
  4990.         mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  4991.         mov     edi, OS_BASE+0x40000
  4992.         mov     ecx, 1000
  4993.         rep movsb
  4994. end if
  4995.  
  4996.         mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
  4997.         mov     edi, OS_BASE
  4998.         mov     ecx, 0x10000/4
  4999.         cld
  5000.         rep movsd
  5001.  
  5002.         call    restorefatchain
  5003.  
  5004.         call    IRQ_mask_all
  5005.  
  5006. if 0
  5007.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5008.         mov     word [OS_BASE+0x467+2], 0x1000
  5009.  
  5010.         mov     al, 0x0F
  5011.         out     0x70, al
  5012.         mov     al, 0x05
  5013.         out     0x71, al
  5014.  
  5015.         mov     al, 0xFE
  5016.         out     0x64, al
  5017.  
  5018.         hlt
  5019.         jmp     $-1
  5020.  
  5021. else
  5022.         cmp     byte [OS_BASE + 0x9030], 2
  5023.         jnz     no_acpi_power_off
  5024.  
  5025. ; scan for RSDP
  5026. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5027.         movzx   eax, word [OS_BASE + 0x40E]
  5028.         shl     eax, 4
  5029.         jz      @f
  5030.         mov     ecx, 1024/16
  5031.         call    scan_rsdp
  5032.         jnc     .rsdp_found
  5033. @@:
  5034. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5035.         mov     eax, 0xE0000
  5036.         mov     ecx, 0x2000
  5037.         call    scan_rsdp
  5038.         jc      no_acpi_power_off
  5039. .rsdp_found:
  5040.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5041.         mov     ebp, [ipc_tmp]
  5042.         stdcall map_page, ebp, esi, PG_MAP
  5043.         lea     eax, [esi+1000h]
  5044.         lea     edx, [ebp+1000h]
  5045.         stdcall map_page, edx, eax, PG_MAP
  5046.         and     esi, 0xFFF
  5047.         add     esi, ebp
  5048.         cmp     dword [esi], 'RSDT'
  5049.         jnz     no_acpi_power_off
  5050.         mov     ecx, [esi+4]
  5051.         sub     ecx, 24h
  5052.         jbe     no_acpi_power_off
  5053.         shr     ecx, 2
  5054.         add     esi, 24h
  5055. .scan_fadt:
  5056.         lodsd
  5057.         mov     ebx, eax
  5058.         lea     eax, [ebp+2000h]
  5059.         stdcall map_page, eax, ebx, PG_MAP
  5060.         lea     eax, [ebp+3000h]
  5061.         add     ebx, 0x1000
  5062.         stdcall map_page, eax, ebx, PG_MAP
  5063.         and     ebx, 0xFFF
  5064.         lea     ebx, [ebx+ebp+2000h]
  5065.         cmp     dword [ebx], 'FACP'
  5066.         jz      .fadt_found
  5067.         loop    .scan_fadt
  5068.         jmp     no_acpi_power_off
  5069. .fadt_found:
  5070. ; ebx is linear address of FADT
  5071.         mov     edi, [ebx+40] ; physical address of the DSDT
  5072.         lea     eax, [ebp+4000h]
  5073.         stdcall map_page, eax, edi, PG_MAP
  5074.         lea     eax, [ebp+5000h]
  5075.         lea     esi, [edi+0x1000]
  5076.         stdcall map_page, eax, esi, PG_MAP
  5077.         and     esi, 0xFFF
  5078.         sub     edi, esi
  5079.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5080.         jnz     no_acpi_power_off
  5081.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5082.         sub     eax, 36+4
  5083.         jbe     no_acpi_power_off
  5084.         add     esi, 36
  5085. .scan_dsdt:
  5086.         cmp     dword [esi+ebp+4000h], '_S5_'
  5087.         jnz     .scan_dsdt_cont
  5088.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5089.         jnz     .scan_dsdt_cont
  5090.         mov     dl, [esi+ebp+4000h+6]
  5091.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5092.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5093.         ja      .scan_dsdt_cont
  5094.         cmp     dl, 1
  5095.         jb      .scan_dsdt_cont
  5096.         lea     esi, [esi+ebp+4000h+7]
  5097.         xor     ecx, ecx
  5098.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5099.         jz      @f
  5100.         cmp     byte [esi], 0xA
  5101.         jnz     no_acpi_power_off
  5102.         inc     esi
  5103.         mov     cl, [esi]
  5104. @@:
  5105.         inc     esi
  5106.         cmp     dl, 2
  5107.         jb      @f
  5108.         cmp     byte [esi], 0
  5109.         jz      @f
  5110.         cmp     byte [esi], 0xA
  5111.         jnz     no_acpi_power_off
  5112.         inc     esi
  5113.         mov     ch, [esi]
  5114. @@:
  5115.         jmp     do_acpi_power_off
  5116. .scan_dsdt_cont:
  5117.         inc     esi
  5118.         cmp     esi, 0x1000
  5119.         jb      @f
  5120.         sub     esi, 0x1000
  5121.         add     edi, 0x1000
  5122.         push    eax
  5123.         lea     eax, [ebp+4000h]
  5124.         stdcall map_page, eax, edi, PG_MAP
  5125.         push    PG_MAP
  5126.         lea     eax, [edi+1000h]
  5127.         push    eax
  5128.         lea     eax, [ebp+5000h]
  5129.         push    eax
  5130.         stdcall map_page
  5131.         pop     eax
  5132. @@:
  5133.         dec     eax
  5134.         jnz     .scan_dsdt
  5135.         jmp     no_acpi_power_off
  5136. do_acpi_power_off:
  5137.         mov     edx, [ebx+48]
  5138.         test    edx, edx
  5139.         jz      .nosmi
  5140.         mov     al, [ebx+52]
  5141.         out     dx, al
  5142.         mov     edx, [ebx+64]
  5143. @@:
  5144.         in      ax, dx
  5145.         test    al, 1
  5146.         jz      @b
  5147. .nosmi:
  5148.         and     cx, 0x0707
  5149.         shl     cx, 2
  5150.         or      cx, 0x2020
  5151.         mov     edx, [ebx+64]
  5152.         in      ax, dx
  5153.         and     ax, 203h
  5154.         or      ah, cl
  5155.         out     dx, ax
  5156.         mov     edx, [ebx+68]
  5157.         test    edx, edx
  5158.         jz      @f
  5159.         in      ax, dx
  5160.         and     ax, 203h
  5161.         or      ah, ch
  5162.         out     dx, ax
  5163. @@:
  5164.         jmp     $
  5165.  
  5166.  
  5167. no_acpi_power_off:
  5168.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5169.         mov     word [OS_BASE+0x467+2], 0x1000
  5170.  
  5171.         mov     al, 0x0F
  5172.         out     0x70, al
  5173.         mov     al, 0x05
  5174.         out     0x71, al
  5175.  
  5176.         mov     al, 0xFE
  5177.         out     0x64, al
  5178.  
  5179.         hlt
  5180.         jmp     $-1
  5181.  
  5182. scan_rsdp:
  5183.         add     eax, OS_BASE
  5184. .s:
  5185.         cmp     dword [eax], 'RSD '
  5186.         jnz     .n
  5187.         cmp     dword [eax+4], 'PTR '
  5188.         jnz     .n
  5189.         xor     edx, edx
  5190.         xor     esi, esi
  5191. @@:
  5192.         add     dl, [eax+esi]
  5193.         inc     esi
  5194.         cmp     esi, 20
  5195.         jnz     @b
  5196.         test    dl, dl
  5197.         jz      .ok
  5198. .n:
  5199.         add     eax, 10h
  5200.         loop    .s
  5201.         stc
  5202. .ok:
  5203.         ret
  5204. end if
  5205.  
  5206. include "data32.inc"
  5207.  
  5208. __REV__ = __REV
  5209.  
  5210. uglobals_size = $ - endofcode
  5211. diff16 "end of kernel code",0,$
  5212.