Subversion Repositories Kolibri OS

Rev

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