Subversion Repositories Kolibri OS

Rev

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

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