Subversion Repositories Kolibri OS

Rev

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

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