Subversion Repositories Kolibri OS

Rev

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