Subversion Repositories Kolibri OS

Rev

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