Subversion Repositories Kolibri OS

Rev

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