Subversion Repositories Kolibri OS

Rev

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