Subversion Repositories Kolibri OS

Rev

Rev 2453 | Rev 2464 | 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: 2455 $
  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.         mov     [mem_BACKGROUND], eax
  2448. ; get memory for new background
  2449.         stdcall kernel_alloc, eax
  2450.         test    eax, eax
  2451.         jz      .memfailed
  2452.         mov     [img_background], eax
  2453.         jmp     .exit
  2454. .memfailed:
  2455. ; revert to static monotone data
  2456.         mov     [img_background], static_background_data
  2457.         xor     eax, eax
  2458.         inc     eax
  2459.         mov     [BgrDataWidth], eax
  2460.         mov     [BgrDataHeight], eax
  2461.         mov     [mem_BACKGROUND], 4
  2462. .exit:
  2463.         popad
  2464.         mov     [bgrlock], 0
  2465.  
  2466.   sbgrr:
  2467.         ret
  2468.  
  2469. nosb1:
  2470.  
  2471.         cmp     ebx, 2                     ; SET PIXEL
  2472.         jnz     nosb2
  2473.  
  2474.         mov     eax, [img_background]
  2475.         test    ecx, ecx
  2476.         jz      @f
  2477.         cmp     eax, static_background_data
  2478.         jz      .ret
  2479. @@:
  2480.         mov     ebx, [mem_BACKGROUND]
  2481.         add     ebx, 4095
  2482.         and     ebx, -4096
  2483.         sub     ebx, 4
  2484.         cmp     ecx, ebx
  2485.         ja      .ret
  2486.  
  2487.         mov     ebx, [eax+ecx]
  2488.         and     ebx, 0xFF000000;255*256*256*256
  2489.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2490.         add     edx, ebx
  2491.         mov     [eax+ecx], edx
  2492. .ret:
  2493.         ret
  2494. nosb2:
  2495.  
  2496.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2497.         jnz     nosb3
  2498. draw_background_temp:
  2499. ;    cmp   [bgrchanged],1 ;0
  2500. ;    je    nosb31
  2501. ;draw_background_temp:
  2502. ;    mov   [bgrchanged],1 ;0
  2503.         mov     [background_defined], 1
  2504.         mov     byte[BACKGROUND_CHANGED], 1
  2505.         call    force_redraw_background
  2506.    nosb31:
  2507.         ret
  2508.   nosb3:
  2509.  
  2510.         cmp     ebx, 4                     ; TILED / STRETCHED
  2511.         jnz     nosb4
  2512.         cmp     ecx, [BgrDrawMode]
  2513.         je      nosb41
  2514.         mov     [BgrDrawMode], ecx
  2515. ;    mov   [bgrchanged],1
  2516.    nosb41:
  2517.         ret
  2518.   nosb4:
  2519.  
  2520.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2521.         jnz     nosb5
  2522.         cmp     [img_background], static_background_data
  2523.         jnz     @f
  2524.         test    edx, edx
  2525.         jnz     .fin
  2526.         cmp     esi, 4
  2527.         ja      .fin
  2528.   @@:
  2529.   ; bughere
  2530.         mov     eax, ecx
  2531.         mov     ebx, edx
  2532.         add     ebx, [img_background];IMG_BACKGROUND
  2533.         mov     ecx, esi
  2534.         call    memmove
  2535.   .fin:
  2536.         ret
  2537.   nosb5:
  2538.  
  2539.         cmp     ebx, 6
  2540.         jnz     nosb6
  2541. ;;Maxis use atomic bts for mutex 4.4.2009
  2542. @@:
  2543.         bts     dword [bgrlock], 0
  2544.         jnc     @f
  2545.         call    change_task
  2546.         jmp     @b
  2547. @@:
  2548.         mov     eax, [CURRENT_TASK]
  2549.         mov     [bgrlockpid], eax
  2550.         cmp     [img_background], static_background_data
  2551.         jz      .nomem
  2552.         stdcall user_alloc, [mem_BACKGROUND]
  2553.         mov     [esp+32], eax
  2554.         test    eax, eax
  2555.         jz      .nomem
  2556.         mov     ebx, eax
  2557.         shr     ebx, 12
  2558.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2559.         mov     esi, [img_background]
  2560.         shr     esi, 12
  2561.         mov     ecx, [mem_BACKGROUND]
  2562.         add     ecx, 0xFFF
  2563.         shr     ecx, 12
  2564. .z:
  2565.         mov     eax, [page_tabs+ebx*4]
  2566.         test    al, 1
  2567.         jz      @f
  2568.         call    free_page
  2569. @@:
  2570.         mov     eax, [page_tabs+esi*4]
  2571.         or      al, PG_UW
  2572.         mov     [page_tabs+ebx*4], eax
  2573.         mov     eax, ebx
  2574.         shl     eax, 12
  2575.         invlpg  [eax]
  2576.         inc     ebx
  2577.         inc     esi
  2578.         loop    .z
  2579.         ret
  2580. .nomem:
  2581.         and     [bgrlockpid], 0
  2582.         mov     [bgrlock], 0
  2583. nosb6:
  2584.         cmp     ebx, 7
  2585.         jnz     nosb7
  2586.         cmp     [bgrlock], 0
  2587.         jz      .err
  2588.         mov     eax, [CURRENT_TASK]
  2589.         cmp     [bgrlockpid], eax
  2590.         jnz     .err
  2591.         mov     eax, ecx
  2592.         mov     ebx, ecx
  2593.         shr     eax, 12
  2594.         mov     ecx, [page_tabs+(eax-1)*4]
  2595.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2596.         jz      .err
  2597.         jnp     .err
  2598.         push    eax
  2599.         shr     ecx, 12
  2600.         dec     ecx
  2601. @@:
  2602.         and     dword [page_tabs+eax*4], 0
  2603.         mov     edx, eax
  2604.         shl     edx, 12
  2605.         push    eax
  2606.         invlpg  [edx]
  2607.         pop     eax
  2608.         inc     eax
  2609.         loop    @b
  2610.         pop     eax
  2611.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2612.         stdcall user_free, ebx
  2613.         mov     [esp+32], eax
  2614.         and     [bgrlockpid], 0
  2615.         mov     [bgrlock], 0
  2616.         ret
  2617. .err:
  2618.         and     dword [esp+32], 0
  2619.         ret
  2620.  
  2621. nosb7:
  2622.         ret
  2623.  
  2624. force_redraw_background:
  2625.         and     [draw_data+32 + RECT.left], 0
  2626.         and     [draw_data+32 + RECT.top], 0
  2627.         push    eax ebx
  2628.         mov     eax, [Screen_Max_X]
  2629.         mov     ebx, [Screen_Max_Y]
  2630.         mov     [draw_data+32 + RECT.right], eax
  2631.         mov     [draw_data+32 + RECT.bottom], ebx
  2632.         pop     ebx eax
  2633.         inc     byte[REDRAW_BACKGROUND]
  2634.         ret
  2635.  
  2636. align 4
  2637.  
  2638. sys_getbackground:
  2639. ;    cmp   eax,1                                  ; SIZE
  2640.         dec     ebx
  2641.         jnz     nogb1
  2642.         mov     eax, [BgrDataWidth]
  2643.         shl     eax, 16
  2644.         mov     ax, [BgrDataHeight]
  2645.         mov     [esp+32], eax
  2646.         ret
  2647.  
  2648. nogb1:
  2649. ;    cmp   eax,2                                  ; PIXEL
  2650.         dec     ebx
  2651.         jnz     nogb2
  2652.  
  2653.         mov     eax, [img_background]
  2654.         test    ecx, ecx
  2655.         jz      @f
  2656.         cmp     eax, static_background_data
  2657.         jz      .ret
  2658. @@:
  2659.         mov     ebx, [mem_BACKGROUND]
  2660.         add     ebx, 4095
  2661.         and     ebx, -4096
  2662.         sub     ebx, 4
  2663.         cmp     ecx, ebx
  2664.         ja      .ret
  2665.  
  2666.         mov     eax, [ecx+eax]
  2667.  
  2668.         and     eax, 0xFFFFFF
  2669.         mov     [esp+32], eax
  2670. .ret:
  2671.         ret
  2672.   nogb2:
  2673.  
  2674. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2675.         dec     ebx
  2676.         dec     ebx
  2677.         jnz     nogb4
  2678.         mov     eax, [BgrDrawMode]
  2679.   nogb4:
  2680.         mov     [esp+32], eax
  2681.         ret
  2682.  
  2683. align 4
  2684.  
  2685. sys_getkey:
  2686.         mov     [esp + 32], dword 1
  2687.         ; test main buffer
  2688.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2689.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2690.         mov     edx, [TASK_COUNT]
  2691.         cmp     ecx, edx
  2692.         jne     .finish
  2693.         cmp     [KEY_COUNT], byte 0
  2694.         je      .finish
  2695.         movzx   eax, byte [KEY_BUFF]
  2696.         shl     eax, 8
  2697.         push    eax
  2698.         dec     byte [KEY_COUNT]
  2699.         and     byte [KEY_COUNT], 127
  2700.         movzx   ecx, byte [KEY_COUNT]
  2701.         add     ecx, 2
  2702.         mov     eax, KEY_BUFF + 1
  2703.         mov     ebx, KEY_BUFF
  2704.         call    memmove
  2705.         pop     eax
  2706. .ret_eax:
  2707.         mov     [esp + 32], eax
  2708.         ret
  2709. .finish:
  2710. ; test hotkeys buffer
  2711.         mov     ecx, hotkey_buffer
  2712. @@:
  2713.         cmp     [ecx], ebx
  2714.         jz      .found
  2715.         add     ecx, 8
  2716.         cmp     ecx, hotkey_buffer + 120 * 8
  2717.         jb      @b
  2718.         ret
  2719. .found:
  2720.         mov     ax, [ecx + 6]
  2721.         shl     eax, 16
  2722.         mov     ah, [ecx + 4]
  2723.         mov     al, 2
  2724.         and     dword [ecx + 4], 0
  2725.         and     dword [ecx], 0
  2726.         jmp     .ret_eax
  2727.  
  2728. align 4
  2729.  
  2730. sys_getbutton:
  2731.  
  2732.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2733.         mov     [esp + 32], dword 1
  2734.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2735.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2736.         cmp     ecx, edx
  2737.         jne     .exit
  2738.         movzx   eax, byte [BTN_COUNT]
  2739.         test    eax, eax
  2740.         jz      .exit
  2741.         mov     eax, [BTN_BUFF]
  2742.         and     al, 0xFE                                    ; delete left button bit
  2743.         mov     [BTN_COUNT], byte 0
  2744.         mov     [esp + 32], eax
  2745. .exit:
  2746.         ret
  2747.  
  2748.  
  2749. align 4
  2750.  
  2751. sys_cpuusage:
  2752.  
  2753. ;  RETURN:
  2754. ;
  2755. ;  +00 dword     process cpu usage
  2756. ;  +04  word     position in windowing stack
  2757. ;  +06  word     windowing stack value at current position (cpu nro)
  2758. ;  +10 12 bytes  name
  2759. ;  +22 dword     start in mem
  2760. ;  +26 dword     used mem
  2761. ;  +30 dword     PID , process idenfification number
  2762. ;
  2763.  
  2764.         cmp     ecx, -1 ; who am I ?
  2765.         jne     .no_who_am_i
  2766.         mov     ecx, [CURRENT_TASK]
  2767.   .no_who_am_i:
  2768.         cmp     ecx, max_processes
  2769.         ja      .nofillbuf
  2770.  
  2771. ; +4: word: position of the window of thread in the window stack
  2772.         mov     ax, [WIN_STACK + ecx * 2]
  2773.         mov     [ebx+4], ax
  2774. ; +6: word: number of the thread slot, which window has in the window stack
  2775. ;           position ecx (has no relation to the specific thread)
  2776.         mov     ax, [WIN_POS + ecx * 2]
  2777.         mov     [ebx+6], ax
  2778.  
  2779.         shl     ecx, 5
  2780.  
  2781. ; +0: dword: memory usage
  2782.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2783.         mov     [ebx], eax
  2784. ; +10: 11 bytes: name of the process
  2785.         push    ecx
  2786.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2787.         add     ebx, 10
  2788.         mov     ecx, 11
  2789.         call    memmove
  2790.         pop     ecx
  2791.  
  2792. ; +22: address of the process in memory
  2793. ; +26: size of used memory - 1
  2794.         push    edi
  2795.         lea     edi, [ebx+12]
  2796.         xor     eax, eax
  2797.         mov     edx, 0x100000*16
  2798.         cmp     ecx, 1 shl 5
  2799.         je      .os_mem
  2800.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2801.         mov     eax, std_application_base_address
  2802. .os_mem:
  2803.         stosd
  2804.         lea     eax, [edx-1]
  2805.         stosd
  2806.  
  2807. ; +30: PID/TID
  2808.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2809.         stosd
  2810.  
  2811.     ; window position and size
  2812.         push    esi
  2813.         lea     esi, [ecx + window_data + WDATA.box]
  2814.         movsd
  2815.         movsd
  2816.         movsd
  2817.         movsd
  2818.  
  2819.     ; Process state (+50)
  2820.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2821.         stosd
  2822.  
  2823.     ; Window client area box
  2824.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2825.         movsd
  2826.         movsd
  2827.         movsd
  2828.         movsd
  2829.  
  2830.     ; Window state
  2831.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2832.         stosb
  2833.  
  2834.     ; Event mask (+71)
  2835.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  2836.         stosd
  2837.  
  2838.         pop     esi
  2839.         pop     edi
  2840.  
  2841. .nofillbuf:
  2842.     ; return number of processes
  2843.  
  2844.         mov     eax, [TASK_COUNT]
  2845.         mov     [esp+32], eax
  2846.         ret
  2847.  
  2848. align 4
  2849. sys_clock:
  2850.         cli
  2851.   ; Mikhail Lisovin  xx Jan 2005
  2852.   @@:
  2853.         mov     al, 10
  2854.         out     0x70, al
  2855.         in      al, 0x71
  2856.         test    al, al
  2857.         jns     @f
  2858.         mov     esi, 1
  2859.         call    delay_ms
  2860.         jmp     @b
  2861.   @@:
  2862.   ; end Lisovin's fix
  2863.  
  2864.         xor     al, al        ; seconds
  2865.         out     0x70, al
  2866.         in      al, 0x71
  2867.         movzx   ecx, al
  2868.         mov     al, 02        ; minutes
  2869.         shl     ecx, 16
  2870.         out     0x70, al
  2871.         in      al, 0x71
  2872.         movzx   edx, al
  2873.         mov     al, 04        ; hours
  2874.         shl     edx, 8
  2875.         out     0x70, al
  2876.         in      al, 0x71
  2877.         add     ecx, edx
  2878.         movzx   edx, al
  2879.         add     ecx, edx
  2880.         sti
  2881.         mov     [esp + 32], ecx
  2882.         ret
  2883.  
  2884.  
  2885. align 4
  2886.  
  2887. sys_date:
  2888.  
  2889.         cli
  2890.   @@:
  2891.         mov     al, 10
  2892.         out     0x70, al
  2893.         in      al, 0x71
  2894.         test    al, al
  2895.         jns     @f
  2896.         mov     esi, 1
  2897.         call    delay_ms
  2898.         jmp     @b
  2899.   @@:
  2900.  
  2901.         mov     ch, 0
  2902.         mov     al, 7           ; date
  2903.         out     0x70, al
  2904.         in      al, 0x71
  2905.         mov     cl, al
  2906.         mov     al, 8           ; month
  2907.         shl     ecx, 16
  2908.         out     0x70, al
  2909.         in      al, 0x71
  2910.         mov     ch, al
  2911.         mov     al, 9           ; year
  2912.         out     0x70, al
  2913.         in      al, 0x71
  2914.         mov     cl, al
  2915.         sti
  2916.         mov     [esp+32], ecx
  2917.         ret
  2918.  
  2919.  
  2920. ; redraw status
  2921.  
  2922. sys_redrawstat:
  2923.         cmp     ebx, 1
  2924.         jne     no_widgets_away
  2925.         ; buttons away
  2926.         mov     ecx, [CURRENT_TASK]
  2927.   sys_newba2:
  2928.         mov     edi, [BTN_ADDR]
  2929.         cmp     [edi], dword 0  ; empty button list ?
  2930.         je      end_of_buttons_away
  2931.         movzx   ebx, word [edi]
  2932.         inc     ebx
  2933.         mov     eax, edi
  2934.   sys_newba:
  2935.         dec     ebx
  2936.         jz      end_of_buttons_away
  2937.  
  2938.         add     eax, 0x10
  2939.         cmp     cx, [eax]
  2940.         jnz     sys_newba
  2941.  
  2942.         push    eax ebx ecx
  2943.         mov     ecx, ebx
  2944.         inc     ecx
  2945.         shl     ecx, 4
  2946.         mov     ebx, eax
  2947.         add     eax, 0x10
  2948.         call    memmove
  2949.         dec     dword [edi]
  2950.         pop     ecx ebx eax
  2951.  
  2952.         jmp     sys_newba2
  2953.  
  2954.   end_of_buttons_away:
  2955.  
  2956.         ret
  2957.  
  2958.   no_widgets_away:
  2959.  
  2960.         cmp     ebx, 2
  2961.         jnz     srl1
  2962.  
  2963.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2964.         add     edx, draw_data - CURRENT_TASK
  2965.         mov     [edx + RECT.left], 0
  2966.         mov     [edx + RECT.top], 0
  2967.         mov     eax, [Screen_Max_X]
  2968.         mov     [edx + RECT.right], eax
  2969.         mov     eax, [Screen_Max_Y]
  2970.         mov     [edx + RECT.bottom], eax
  2971.  
  2972.   srl1:
  2973.         ret
  2974.  
  2975. ;ok - 100% work
  2976. ;nt - not tested
  2977. ;---------------------------------------------------------------------------------------------
  2978. ;eax
  2979. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2980. ;1 - change task. Ret nothing. Block. ok.
  2981. ;2 - performance control
  2982. ; ebx
  2983. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2984. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2985. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2986. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2987. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2988. ;eax
  2989. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2990. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2991. ;---------------------------------------------------------------------------------------------
  2992. iglobal
  2993. align 4
  2994. sheduler:
  2995.         dd      sys_sheduler.00
  2996.         dd      change_task
  2997.         dd      sys_sheduler.02
  2998.         dd      sys_sheduler.03
  2999.         dd      sys_sheduler.04
  3000. endg
  3001. sys_sheduler:
  3002. ;rewritten by <Lrz>  29.12.2009
  3003.         jmp     dword [sheduler+ebx*4]
  3004. ;.shed_counter:
  3005. .00:
  3006.         mov     eax, [context_counter]
  3007.         mov     [esp+32], eax
  3008.         ret
  3009.  
  3010. .02:
  3011. ;.perf_control:
  3012.         inc     ebx                     ;before ebx=2, ebx=3
  3013.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3014.         jz      cache_disable
  3015.  
  3016.         dec     ebx                     ;ebx=2
  3017.         cmp     ebx, ecx                ;
  3018.         jz      cache_enable            ;if ecx=2 and ebx=2
  3019.  
  3020.         dec     ebx                     ;ebx=1
  3021.         cmp     ebx, ecx
  3022.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3023.  
  3024.         dec     ebx
  3025.         test    ebx, ecx                ;ebx=0 and ecx=0
  3026.         jz      modify_pce              ;if ecx=0
  3027.  
  3028.         ret
  3029.  
  3030. .03:
  3031. ;.rdmsr_instr:
  3032. ;now counter in ecx
  3033. ;(edx:eax) esi:edi => edx:esi
  3034.         mov     eax, esi
  3035.         mov     ecx, edx
  3036.         rdmsr
  3037.         mov     [esp+32], eax
  3038.         mov     [esp+20], edx           ;ret in ebx?
  3039.         ret
  3040.  
  3041. .04:
  3042. ;.wrmsr_instr:
  3043. ;now counter in ecx
  3044. ;(edx:eax) esi:edi => edx:esi
  3045.         ; Fast Call MSR can't be destroy
  3046.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  3047.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  3048.         cmp     edx, MSR_SYSENTER_CS
  3049.         je      @f
  3050.         cmp     edx, MSR_SYSENTER_ESP
  3051.         je      @f
  3052.         cmp     edx, MSR_SYSENTER_EIP
  3053.         je      @f
  3054.         cmp     edx, MSR_AMD_STAR
  3055.         je      @f
  3056.  
  3057.         mov     eax, esi
  3058.         mov     ecx, edx
  3059.         wrmsr
  3060.         ; mov   [esp + 32], eax
  3061.         ; mov   [esp + 20], edx ;ret in ebx?
  3062. @@:
  3063.         ret
  3064.  
  3065. cache_disable:
  3066.         mov     eax, cr0
  3067.         or      eax, 01100000000000000000000000000000b
  3068.         mov     cr0, eax
  3069.         wbinvd  ;set MESI
  3070.         ret
  3071.  
  3072. cache_enable:
  3073.         mov     eax, cr0
  3074.         and     eax, 10011111111111111111111111111111b
  3075.         mov     cr0, eax
  3076.         ret
  3077.  
  3078. is_cache_enabled:
  3079.         mov     eax, cr0
  3080.         mov     ebx, eax
  3081.         and     eax, 01100000000000000000000000000000b
  3082.         jz      cache_disabled
  3083.         mov     [esp+32], ebx
  3084. cache_disabled:
  3085.         mov     dword [esp+32], eax;0
  3086.         ret
  3087.  
  3088. modify_pce:
  3089.         mov     eax, cr4
  3090. ;       mov ebx,0
  3091. ;       or  bx,100000000b ;pce
  3092. ;       xor eax,ebx ;invert pce
  3093.         bts     eax, 8;pce=cr4[8]
  3094.         mov     cr4, eax
  3095.         mov     [esp+32], eax
  3096.         ret
  3097. ;---------------------------------------------------------------------------------------------
  3098.  
  3099.  
  3100. ; check if pixel is allowed to be drawn
  3101.  
  3102. ;checkpixel:
  3103. ;        push    eax edx
  3104.  
  3105. ;;        mov     edx, [Screen_Max_X] ; screen x size
  3106. ;;        inc     edx
  3107. ;;        imul    edx, ebx
  3108. ;        mov     edx, [d_width_calc_area + ebx*4]
  3109. ;        add     eax, [_WinMapAddress]
  3110. ;        mov     dl, [eax+edx]; lea eax, [...]
  3111.  
  3112. ;        xor     ecx, ecx
  3113. ;        mov     eax, [CURRENT_TASK]
  3114. ;        cmp     al, dl
  3115. ;        setne   cl
  3116.  
  3117. ;        pop     edx eax
  3118. ;        ret
  3119.  
  3120. iglobal
  3121.   cpustring db 'CPU',0
  3122. endg
  3123.  
  3124. uglobal
  3125. background_defined    db    0    ; diamond, 11.04.2006
  3126. endg
  3127.  
  3128. align 4
  3129. ; check misc
  3130.  
  3131. checkmisc:
  3132.  
  3133.         cmp     [ctrl_alt_del], 1
  3134.         jne     nocpustart
  3135.  
  3136.         mov     ebp, cpustring
  3137.         call    fs_execute_from_sysdir
  3138.  
  3139.         mov     [ctrl_alt_del], 0
  3140.  
  3141. nocpustart:
  3142.         cmp     [mouse_active], 1
  3143.         jne     mouse_not_active
  3144.         mov     [mouse_active], 0
  3145.  
  3146.         xor     edi, edi
  3147.         mov     ebx, CURRENT_TASK
  3148.  
  3149.         mov     ecx, [TASK_COUNT]
  3150.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3151.         shl     eax, 8
  3152.         push    eax
  3153.  
  3154.         movzx   eax, word [MOUSE_X]
  3155.         movzx   edx, word [MOUSE_Y]
  3156.  
  3157. align 4
  3158. .set_mouse_event:
  3159.         add     edi, 256
  3160.         add     ebx, 32
  3161.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3162.         jz      .pos_filter
  3163.  
  3164.         cmp     edi, [esp]                      ; skip if filtration active
  3165.         jne     .skip
  3166.  
  3167. .pos_filter:
  3168.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3169.         jz      .set
  3170.  
  3171.         mov     esi, [ebx-twdw+WDATA.box.left]
  3172.         cmp     eax, esi
  3173.         jb      .skip
  3174.         add     esi, [ebx-twdw+WDATA.box.width]
  3175.         cmp     eax, esi
  3176.         ja      .skip
  3177.  
  3178.         mov     esi, [ebx-twdw+WDATA.box.top]
  3179.         cmp     edx, esi
  3180.         jb      .skip
  3181.         add     esi, [ebx-twdw+WDATA.box.height]
  3182.         cmp     edx, esi
  3183.         ja      .skip
  3184. .set:
  3185.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
  3186. .skip:
  3187.         loop    .set_mouse_event
  3188.  
  3189.         pop     eax
  3190.  
  3191. mouse_not_active:
  3192.         cmp     byte[BACKGROUND_CHANGED], 0
  3193.         jz      no_set_bgr_event
  3194.         xor     edi, edi
  3195.         mov     ecx, [TASK_COUNT]
  3196. set_bgr_event:
  3197.         add     edi, 256
  3198.         or      [edi+SLOT_BASE+APPDATA.event_mask], 16
  3199.         loop    set_bgr_event
  3200.         mov     byte[BACKGROUND_CHANGED], 0
  3201. no_set_bgr_event:
  3202.         cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
  3203.         jz      nobackgr
  3204.         cmp     [background_defined], 0
  3205.         jz      nobackgr
  3206. ;    mov   [draw_data+32 + RECT.left],dword 0
  3207. ;    mov   [draw_data+32 + RECT.top],dword 0
  3208. ;    mov   eax,[Screen_Max_X]
  3209. ;    mov   ebx,[Screen_Max_Y]
  3210. ;    mov   [draw_data+32 + RECT.right],eax
  3211. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3212. @@:
  3213.         call    drawbackground
  3214.         xor     eax, eax
  3215.         xchg    al, [REDRAW_BACKGROUND]
  3216.         test    al, al                             ; got new update request?
  3217.         jnz     @b
  3218.         mov     [draw_data+32 + RECT.left], eax
  3219.         mov     [draw_data+32 + RECT.top], eax
  3220.         mov     [draw_data+32 + RECT.right], eax
  3221.         mov     [draw_data+32 + RECT.bottom], eax
  3222.         mov     [MOUSE_BACKGROUND], byte 0
  3223.  
  3224. nobackgr:
  3225.  
  3226.     ; system shutdown request
  3227.  
  3228.         cmp     [SYS_SHUTDOWN], byte 0
  3229.         je      noshutdown
  3230.  
  3231.         mov     edx, [shutdown_processes]
  3232.  
  3233.         cmp     [SYS_SHUTDOWN], dl
  3234.         jne     no_mark_system_shutdown
  3235.  
  3236.         lea     ecx, [edx-1]
  3237.         mov     edx, OS_BASE+0x3040
  3238.         jecxz   @f
  3239. markz:
  3240.         mov     [edx+TASKDATA.state], byte 3
  3241.         add     edx, 0x20
  3242.         loop    markz
  3243. @@:
  3244.  
  3245.   no_mark_system_shutdown:
  3246.  
  3247.         dec     byte [SYS_SHUTDOWN]
  3248.         je      system_shutdown
  3249.  
  3250. noshutdown:
  3251.  
  3252.  
  3253.         mov     eax, [TASK_COUNT]           ; termination
  3254.         mov     ebx, TASK_DATA+TASKDATA.state
  3255.         mov     esi, 1
  3256.  
  3257. newct:
  3258.         mov     cl, [ebx]
  3259.         cmp     cl, byte 3
  3260.         jz      terminate
  3261.         cmp     cl, byte 4
  3262.         jz      terminate
  3263.  
  3264.         add     ebx, 0x20
  3265.         inc     esi
  3266.         dec     eax
  3267.         jnz     newct
  3268.         ret
  3269.  
  3270. ; redraw screen
  3271.  
  3272. redrawscreen:
  3273.  
  3274. ; eax , if process window_data base is eax, do not set flag/limits
  3275.  
  3276.         pushad
  3277.         push    eax
  3278.  
  3279. ;;;         mov   ebx,2
  3280. ;;;         call  delay_hs
  3281.  
  3282.          ;mov   ecx,0               ; redraw flags for apps
  3283.         xor     ecx, ecx
  3284.        newdw2:
  3285.  
  3286.         inc     ecx
  3287.         push    ecx
  3288.  
  3289.         mov     eax, ecx
  3290.         shl     eax, 5
  3291.         add     eax, window_data
  3292.  
  3293.         cmp     eax, [esp+4]
  3294.         je      not_this_task
  3295.                                    ; check if window in redraw area
  3296.         mov     edi, eax
  3297.  
  3298.         cmp     ecx, 1             ; limit for background
  3299.         jz      bgli
  3300.  
  3301.         mov     eax, [edi + WDATA.box.left]
  3302.         mov     ebx, [edi + WDATA.box.top]
  3303.         mov     ecx, [edi + WDATA.box.width]
  3304.         mov     edx, [edi + WDATA.box.height]
  3305.         add     ecx, eax
  3306.         add     edx, ebx
  3307.  
  3308.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3309.         cmp     ecx, ebx
  3310.         jb      ricino
  3311.  
  3312.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3313.         cmp     ecx, eax
  3314.         jb      ricino
  3315.  
  3316.         mov     eax, [edi + WDATA.box.left]
  3317.         mov     ebx, [edi + WDATA.box.top]
  3318.         mov     ecx, [edi + WDATA.box.width]
  3319.         mov     edx, [edi + WDATA.box.height]
  3320.         add     ecx, eax
  3321.         add     edx, ebx
  3322.  
  3323.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3324.         cmp     edx, eax
  3325.         jb      ricino
  3326.  
  3327.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3328.         cmp     ecx, eax
  3329.         jb      ricino
  3330.  
  3331.         bgli:
  3332.  
  3333.         cmp     dword[esp], 1
  3334.         jnz     .az
  3335. ;         cmp   byte[BACKGROUND_CHANGED], 0
  3336. ;         jnz   newdw8
  3337.         cmp     byte[REDRAW_BACKGROUND], 0
  3338.         jz      .az
  3339.         mov     dl, 0
  3340.         lea     eax, [edi+draw_data-window_data]
  3341.         mov     ebx, [draw_limits.left]
  3342.         cmp     ebx, [eax+RECT.left]
  3343.         jae     @f
  3344.         mov     [eax+RECT.left], ebx
  3345.         mov     dl, 1
  3346.         @@:
  3347.         mov     ebx, [draw_limits.top]
  3348.         cmp     ebx, [eax+RECT.top]
  3349.         jae     @f
  3350.         mov     [eax+RECT.top], ebx
  3351.         mov     dl, 1
  3352.         @@:
  3353.         mov     ebx, [draw_limits.right]
  3354.         cmp     ebx, [eax+RECT.right]
  3355.         jbe     @f
  3356.         mov     [eax+RECT.right], ebx
  3357.         mov     dl, 1
  3358.         @@:
  3359.         mov     ebx, [draw_limits.bottom]
  3360.         cmp     ebx, [eax+RECT.bottom]
  3361.         jbe     @f
  3362.         mov     [eax+RECT.bottom], ebx
  3363.         mov     dl, 1
  3364.         @@:
  3365.         add     byte[REDRAW_BACKGROUND], dl
  3366.         jmp     newdw8
  3367.         .az:
  3368.  
  3369.         mov     eax, edi
  3370.         add     eax, draw_data-window_data
  3371.  
  3372.         mov     ebx, [draw_limits.left]        ; set limits
  3373.         mov     [eax + RECT.left], ebx
  3374.         mov     ebx, [draw_limits.top]
  3375.         mov     [eax + RECT.top], ebx
  3376.         mov     ebx, [draw_limits.right]
  3377.         mov     [eax + RECT.right], ebx
  3378.         mov     ebx, [draw_limits.bottom]
  3379.         mov     [eax + RECT.bottom], ebx
  3380.  
  3381.         sub     eax, draw_data-window_data
  3382.  
  3383.         cmp     dword [esp], 1
  3384.         jne     nobgrd
  3385.         inc     byte[REDRAW_BACKGROUND]
  3386.  
  3387.        newdw8:
  3388.        nobgrd:
  3389.  
  3390.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3391.  
  3392.        ricino:
  3393.  
  3394.        not_this_task:
  3395.  
  3396.         pop     ecx
  3397.  
  3398.         cmp     ecx, [TASK_COUNT]
  3399.         jle     newdw2
  3400.  
  3401.         pop     eax
  3402.         popad
  3403.  
  3404.         ret
  3405.  
  3406. calculatebackground:   ; background
  3407.  
  3408.         mov     edi, [_WinMapAddress]              ; set os to use all pixels
  3409.         mov     eax, 0x01010101
  3410.         mov     ecx, [_WinMapSize]
  3411.         shr     ecx, 2
  3412.         rep stosd
  3413.  
  3414.         mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
  3415.         mov     byte[BACKGROUND_CHANGED], 0
  3416.  
  3417.         ret
  3418.  
  3419. uglobal
  3420.   imax    dd 0x0
  3421. endg
  3422.  
  3423.  
  3424.  
  3425. delay_ms:     ; delay in 1/1000 sec
  3426.  
  3427.  
  3428.         push    eax
  3429.         push    ecx
  3430.  
  3431.         mov     ecx, esi
  3432.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3433.         imul    ecx, 33941
  3434.         shr     ecx, 9
  3435.         ; </CPU clock fix>
  3436.  
  3437.         in      al, 0x61
  3438.         and     al, 0x10
  3439.         mov     ah, al
  3440.         cld
  3441.  
  3442.  cnt1:
  3443.         in      al, 0x61
  3444.         and     al, 0x10
  3445.         cmp     al, ah
  3446.         jz      cnt1
  3447.  
  3448.         mov     ah, al
  3449.         loop    cnt1
  3450.  
  3451.         pop     ecx
  3452.         pop     eax
  3453.  
  3454.         ret
  3455.  
  3456. align 4
  3457. set_app_param:
  3458.         mov     edi, [TASK_BASE]
  3459.         mov     eax, ebx
  3460.         btr     eax, 3                           ; move MOUSE_FILTRATION
  3461.         mov     ebx, [current_slot]              ; bit into event_filter
  3462.         setc    byte [ebx+APPDATA.event_filter]
  3463.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3464.         mov     [esp+32], eax                    ; return old mask value
  3465.         ret
  3466.  
  3467.  
  3468.  
  3469. delay_hs:     ; delay in 1/100 secs
  3470. ; ebx = delay time
  3471.         push    ecx
  3472.         push    edx
  3473.  
  3474.         mov     edx, [timer_ticks]
  3475.  
  3476.       newtic:
  3477.         mov     ecx, [timer_ticks]
  3478.         sub     ecx, edx
  3479.         cmp     ecx, ebx
  3480.         jae     zerodelay
  3481.  
  3482.         call    change_task
  3483.  
  3484.         jmp     newtic
  3485.  
  3486.       zerodelay:
  3487.         pop     edx
  3488.         pop     ecx
  3489.  
  3490.         ret
  3491.  
  3492. align 16        ;very often call this subrutine
  3493. memmove:       ; memory move in bytes
  3494.  
  3495. ; eax = from
  3496. ; ebx = to
  3497. ; ecx = no of bytes
  3498.         test    ecx, ecx
  3499.         jle     .ret
  3500.  
  3501.         push    esi edi ecx
  3502.  
  3503.         mov     edi, ebx
  3504.         mov     esi, eax
  3505.  
  3506.         test    ecx, not 11b
  3507.         jz      @f
  3508.  
  3509.         push    ecx
  3510.         shr     ecx, 2
  3511.         rep movsd
  3512.         pop     ecx
  3513.         and     ecx, 11b
  3514.         jz      .finish
  3515.   @@:
  3516.         rep movsb
  3517.  
  3518.   .finish:
  3519.         pop     ecx edi esi
  3520.   .ret:
  3521.         ret
  3522.  
  3523.  
  3524. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3525. ;align 4
  3526. ;
  3527. ;read_floppy_file:
  3528. ;
  3529. ;; as input
  3530. ;;
  3531. ;; eax pointer to file
  3532. ;; ebx file lenght
  3533. ;; ecx start 512 byte block number
  3534. ;; edx number of blocks to read
  3535. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3536. ;;
  3537. ;;
  3538. ;; on return
  3539. ;;
  3540. ;; eax = 0 command succesful
  3541. ;;       1 no fd base and/or partition defined
  3542. ;;       2 yet unsupported FS
  3543. ;;       3 unknown FS
  3544. ;;       4 partition not defined at hd
  3545. ;;       5 file not found
  3546. ;; ebx = size of file
  3547. ;
  3548. ;     mov   edi,[TASK_BASE]
  3549. ;     add   edi,0x10
  3550. ;     add   esi,[edi]
  3551. ;     add   eax,[edi]
  3552. ;
  3553. ;     pushad
  3554. ;     mov  edi,esi
  3555. ;     add  edi,1024
  3556. ;     mov  esi,0x100000+19*512
  3557. ;     sub  ecx,1
  3558. ;     shl  ecx,9
  3559. ;     add  esi,ecx
  3560. ;     shl  edx,9
  3561. ;     mov  ecx,edx
  3562. ;     cld
  3563. ;     rep  movsb
  3564. ;     popad
  3565. ;
  3566. ;     mov   [esp+36],eax
  3567. ;     mov   [esp+24],ebx
  3568. ;     ret
  3569.  
  3570.  
  3571.  
  3572. align 4
  3573. set_io_access_rights:
  3574.         push    edi eax
  3575.         mov     edi, tss._io_map_0
  3576. ;     mov   ecx,eax
  3577. ;     and   ecx,7    ; offset in byte
  3578. ;     shr   eax,3    ; number of byte
  3579. ;     add   edi,eax
  3580. ;     mov   ebx,1
  3581. ;     shl   ebx,cl
  3582.         test    ebp, ebp
  3583. ;     cmp   ebp,0                ; enable access - ebp = 0
  3584.         jnz     .siar1
  3585. ;     not   ebx
  3586. ;     and   [edi],byte bl
  3587.         btr     [edi], eax
  3588.         pop     eax edi
  3589.         ret
  3590. .siar1:
  3591.         bts     [edi], eax
  3592.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3593.         pop     eax edi
  3594.         ret
  3595. ;reserve/free group of ports
  3596. ;  * eax = 46 - number function
  3597. ;  * ebx = 0 - reserve, 1 - free
  3598. ;  * ecx = number start arrea of ports
  3599. ;  * edx = number end arrea of ports (include last number of port)
  3600. ;Return value:
  3601. ;  * eax = 0 - succesful
  3602. ;  * eax = 1 - error
  3603. ;  * The system has reserve this ports:
  3604. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3605. ;destroys eax,ebx, ebp
  3606. r_f_port_area:
  3607.  
  3608.         test    ebx, ebx
  3609.         jnz     free_port_area
  3610. ;     je    r_port_area
  3611. ;     jmp   free_port_area
  3612.  
  3613. ;   r_port_area:
  3614.  
  3615. ;     pushad
  3616.  
  3617.         cmp     ecx, edx      ; beginning > end ?
  3618.         ja      rpal1
  3619.         cmp     edx, 65536
  3620.         jae     rpal1
  3621.         mov     eax, [RESERVED_PORTS]
  3622.         test    eax, eax      ; no reserved areas ?
  3623.         je      rpal2
  3624.         cmp     eax, 255      ; max reserved
  3625.         jae     rpal1
  3626.  rpal3:
  3627.         mov     ebx, eax
  3628.         shl     ebx, 4
  3629.         add     ebx, RESERVED_PORTS
  3630.         cmp     ecx, [ebx+8]
  3631.         ja      rpal4
  3632.         cmp     edx, [ebx+4]
  3633.         jae     rpal1
  3634. ;     jb    rpal4
  3635. ;     jmp   rpal1
  3636.  rpal4:
  3637.         dec     eax
  3638.         jnz     rpal3
  3639.         jmp     rpal2
  3640.    rpal1:
  3641. ;     popad
  3642. ;     mov   eax,1
  3643.         xor     eax, eax
  3644.         inc     eax
  3645.         ret
  3646.    rpal2:
  3647. ;     popad
  3648.      ; enable port access at port IO map
  3649.         cli
  3650.         pushad                        ; start enable io map
  3651.  
  3652.         cmp     edx, 65536;16384
  3653.         jae     no_unmask_io; jge
  3654.         mov     eax, ecx
  3655. ;       push    ebp
  3656.         xor     ebp, ebp               ; enable - eax = port
  3657. new_port_access:
  3658. ;     pushad
  3659.         call    set_io_access_rights
  3660. ;     popad
  3661.         inc     eax
  3662.         cmp     eax, edx
  3663.         jbe     new_port_access
  3664. ;       pop     ebp
  3665. no_unmask_io:
  3666.         popad                         ; end enable io map
  3667.         sti
  3668.  
  3669.         mov     eax, [RESERVED_PORTS]
  3670.         add     eax, 1
  3671.         mov     [RESERVED_PORTS], eax
  3672.         shl     eax, 4
  3673.         add     eax, RESERVED_PORTS
  3674.         mov     ebx, [TASK_BASE]
  3675.         mov     ebx, [ebx+TASKDATA.pid]
  3676.         mov     [eax], ebx
  3677.         mov     [eax+4], ecx
  3678.         mov     [eax+8], edx
  3679.  
  3680.         xor     eax, eax
  3681.         ret
  3682.  
  3683. free_port_area:
  3684.  
  3685. ;     pushad
  3686.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3687.         test    eax, eax
  3688.         jz      frpal2
  3689.         mov     ebx, [TASK_BASE]
  3690.         mov     ebx, [ebx+TASKDATA.pid]
  3691.    frpal3:
  3692.         mov     edi, eax
  3693.         shl     edi, 4
  3694.         add     edi, RESERVED_PORTS
  3695.         cmp     ebx, [edi]
  3696.         jne     frpal4
  3697.         cmp     ecx, [edi+4]
  3698.         jne     frpal4
  3699.         cmp     edx, [edi+8]
  3700.         jne     frpal4
  3701.         jmp     frpal1
  3702.    frpal4:
  3703.         dec     eax
  3704.         jnz     frpal3
  3705.    frpal2:
  3706. ;     popad
  3707.         inc     eax
  3708.         ret
  3709.    frpal1:
  3710.         push    ecx
  3711.         mov     ecx, 256
  3712.         sub     ecx, eax
  3713.         shl     ecx, 4
  3714.         mov     esi, edi
  3715.         add     esi, 16
  3716.         cld
  3717.         rep movsb
  3718.  
  3719.         dec     dword [RESERVED_PORTS]
  3720. ;popad
  3721. ;disable port access at port IO map
  3722.  
  3723. ;     pushad                        ; start disable io map
  3724.         pop     eax     ;start port
  3725.         cmp     edx, 65536;16384
  3726.         jge     no_mask_io
  3727.  
  3728. ;     mov   eax,ecx
  3729.         xor     ebp, ebp
  3730.         inc     ebp
  3731. new_port_access_disable:
  3732. ;     pushad
  3733. ;     mov   ebp,1                  ; disable - eax = port
  3734.         call    set_io_access_rights
  3735. ;     popad
  3736.         inc     eax
  3737.         cmp     eax, edx
  3738.         jbe     new_port_access_disable
  3739. no_mask_io:
  3740. ;     popad                         ; end disable io map
  3741.         xor     eax, eax
  3742.         ret
  3743. ;-----------------------------------------------------------------------------
  3744. align 4
  3745. drawbackground:
  3746. dbrv20:
  3747.         cmp     [BgrDrawMode], dword 1
  3748.         jne     bgrstr
  3749.         call    vesa20_drawbackground_tiled
  3750. ;        call    [draw_pointer]
  3751.         call    __sys_draw_pointer
  3752.         ret
  3753. ;--------------------------------------
  3754. align 4
  3755. bgrstr:
  3756.         call    vesa20_drawbackground_stretch
  3757. ;        call    [draw_pointer]
  3758.         call    __sys_draw_pointer
  3759.         ret
  3760. ;-----------------------------------------------------------------------------
  3761. align 4
  3762. syscall_putimage:                       ; PutImage
  3763. sys_putimage:
  3764.         test    ecx, 0x80008000
  3765.         jnz     .exit
  3766.         test    ecx, 0x0000FFFF
  3767.         jz      .exit
  3768.         test    ecx, 0xFFFF0000
  3769.         jnz     @f
  3770. ;--------------------------------------
  3771. align 4
  3772. .exit:
  3773.         ret
  3774. ;--------------------------------------
  3775. align 4
  3776. @@:
  3777.         mov     edi, [current_slot]
  3778.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3779.         rol     edx, 16
  3780.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3781.         rol     edx, 16
  3782. ;--------------------------------------
  3783. align 4
  3784. .forced:
  3785.         push    ebp esi 0
  3786.         mov     ebp, putimage_get24bpp
  3787.         mov     esi, putimage_init24bpp
  3788. ;--------------------------------------
  3789. align 4
  3790. sys_putimage_bpp:
  3791.         call    vesa20_putimage
  3792.         pop     ebp esi ebp
  3793.         ret
  3794. ;        jmp     [draw_pointer]
  3795. ;-----------------------------------------------------------------------------
  3796. align 4
  3797. sys_putimage_palette:
  3798. ; ebx = pointer to image
  3799. ; ecx = [xsize]*65536 + [ysize]
  3800. ; edx = [xstart]*65536 + [ystart]
  3801. ; esi = number of bits per pixel, must be 8, 24 or 32
  3802. ; edi = pointer to palette
  3803. ; ebp = row delta
  3804.         mov     eax, [CURRENT_TASK]
  3805.         shl     eax, 8
  3806.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3807.         rol     edx, 16
  3808.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3809.         rol     edx, 16
  3810. ;--------------------------------------
  3811. align 4
  3812. .forced:
  3813.         cmp     esi, 1
  3814.         jnz     @f
  3815.         push    edi
  3816.         mov     eax, [edi+4]
  3817.         sub     eax, [edi]
  3818.         push    eax
  3819.         push    dword [edi]
  3820.         push    0ffffff80h
  3821.         mov     edi, esp
  3822.         call    put_mono_image
  3823.         add     esp, 12
  3824.         pop     edi
  3825.         ret
  3826. ;--------------------------------------
  3827. align 4
  3828. @@:
  3829.         cmp     esi, 2
  3830.         jnz     @f
  3831.         push    edi
  3832.         push    0ffffff80h
  3833.         mov     edi, esp
  3834.         call    put_2bit_image
  3835.         pop     eax
  3836.         pop     edi
  3837.         ret
  3838. ;--------------------------------------
  3839. align 4
  3840. @@:
  3841.         cmp     esi, 4
  3842.         jnz     @f
  3843.         push    edi
  3844.         push    0ffffff80h
  3845.         mov     edi, esp
  3846.         call    put_4bit_image
  3847.         pop     eax
  3848.         pop     edi
  3849.         ret
  3850. ;--------------------------------------
  3851. align 4
  3852. @@:
  3853.         push    ebp esi ebp
  3854.         cmp     esi, 8
  3855.         jnz     @f
  3856.         mov     ebp, putimage_get8bpp
  3857.         mov     esi, putimage_init8bpp
  3858.         jmp     sys_putimage_bpp
  3859. ;--------------------------------------
  3860. align 4
  3861. @@:
  3862.         cmp     esi, 15
  3863.         jnz     @f
  3864.         mov     ebp, putimage_get15bpp
  3865.         mov     esi, putimage_init15bpp
  3866.         jmp     sys_putimage_bpp
  3867. ;--------------------------------------
  3868. align 4
  3869. @@:
  3870.         cmp     esi, 16
  3871.         jnz     @f
  3872.         mov     ebp, putimage_get16bpp
  3873.         mov     esi, putimage_init16bpp
  3874.         jmp     sys_putimage_bpp
  3875. ;--------------------------------------
  3876. align 4
  3877. @@:
  3878.         cmp     esi, 24
  3879.         jnz     @f
  3880.         mov     ebp, putimage_get24bpp
  3881.         mov     esi, putimage_init24bpp
  3882.         jmp     sys_putimage_bpp
  3883. ;--------------------------------------
  3884. align 4
  3885. @@:
  3886.         cmp     esi, 32
  3887.         jnz     @f
  3888.         mov     ebp, putimage_get32bpp
  3889.         mov     esi, putimage_init32bpp
  3890.         jmp     sys_putimage_bpp
  3891. ;--------------------------------------
  3892. align 4
  3893. @@:
  3894.         pop     ebp esi ebp
  3895.         ret
  3896. ;-----------------------------------------------------------------------------
  3897. align 4
  3898. put_mono_image:
  3899.         push    ebp esi ebp
  3900.         mov     ebp, putimage_get1bpp
  3901.         mov     esi, putimage_init1bpp
  3902.         jmp     sys_putimage_bpp
  3903. ;-----------------------------------------------------------------------------
  3904. align 4
  3905. put_2bit_image:
  3906.         push    ebp esi ebp
  3907.         mov     ebp, putimage_get2bpp
  3908.         mov     esi, putimage_init2bpp
  3909.         jmp     sys_putimage_bpp
  3910. ;-----------------------------------------------------------------------------
  3911. align 4
  3912. put_4bit_image:
  3913.         push    ebp esi ebp
  3914.         mov     ebp, putimage_get4bpp
  3915.         mov     esi, putimage_init4bpp
  3916.         jmp     sys_putimage_bpp
  3917. ;-----------------------------------------------------------------------------
  3918. align 4
  3919. putimage_init24bpp:
  3920.         lea     eax, [eax*3]
  3921. putimage_init8bpp:
  3922.         ret
  3923. ;-----------------------------------------------------------------------------
  3924. align 16
  3925. putimage_get24bpp:
  3926.         movzx   eax, byte [esi+2]
  3927.         shl     eax, 16
  3928.         mov     ax, [esi]
  3929.         add     esi, 3
  3930.         ret     4
  3931. ;-----------------------------------------------------------------------------
  3932. align 16
  3933. putimage_get8bpp:
  3934.         movzx   eax, byte [esi]
  3935.         push    edx
  3936.         mov     edx, [esp+8]
  3937.         mov     eax, [edx+eax*4]
  3938.         pop     edx
  3939.         inc     esi
  3940.         ret     4
  3941. ;-----------------------------------------------------------------------------
  3942. align 4
  3943. putimage_init1bpp:
  3944.         add     eax, ecx
  3945.         push    ecx
  3946.         add     eax, 7
  3947.         add     ecx, 7
  3948.         shr     eax, 3
  3949.         shr     ecx, 3
  3950.         sub     eax, ecx
  3951.         pop     ecx
  3952.         ret
  3953. ;-----------------------------------------------------------------------------
  3954. align 16
  3955. putimage_get1bpp:
  3956.         push    edx
  3957.         mov     edx, [esp+8]
  3958.         mov     al, [edx]
  3959.         add     al, al
  3960.         jnz     @f
  3961.         lodsb
  3962.         adc     al, al
  3963. @@:
  3964.         mov     [edx], al
  3965.         sbb     eax, eax
  3966.         and     eax, [edx+8]
  3967.         add     eax, [edx+4]
  3968.         pop     edx
  3969.         ret     4
  3970. ;-----------------------------------------------------------------------------
  3971. align 4
  3972. putimage_init2bpp:
  3973.         add     eax, ecx
  3974.         push    ecx
  3975.         add     ecx, 3
  3976.         add     eax, 3
  3977.         shr     ecx, 2
  3978.         shr     eax, 2
  3979.         sub     eax, ecx
  3980.         pop     ecx
  3981.         ret
  3982. ;-----------------------------------------------------------------------------
  3983. align 16
  3984. putimage_get2bpp:
  3985.         push    edx
  3986.         mov     edx, [esp+8]
  3987.         mov     al, [edx]
  3988.         mov     ah, al
  3989.         shr     al, 6
  3990.         shl     ah, 2
  3991.         jnz     .nonewbyte
  3992.         lodsb
  3993.         mov     ah, al
  3994.         shr     al, 6
  3995.         shl     ah, 2
  3996.         add     ah, 1
  3997. .nonewbyte:
  3998.         mov     [edx], ah
  3999.         mov     edx, [edx+4]
  4000.         movzx   eax, al
  4001.         mov     eax, [edx+eax*4]
  4002.         pop     edx
  4003.         ret     4
  4004. ;-----------------------------------------------------------------------------
  4005. align 4
  4006. putimage_init4bpp:
  4007.         add     eax, ecx
  4008.         push    ecx
  4009.         add     ecx, 1
  4010.         add     eax, 1
  4011.         shr     ecx, 1
  4012.         shr     eax, 1
  4013.         sub     eax, ecx
  4014.         pop     ecx
  4015.         ret
  4016. ;-----------------------------------------------------------------------------
  4017. align 16
  4018. putimage_get4bpp:
  4019.         push    edx
  4020.         mov     edx, [esp+8]
  4021.         add     byte [edx], 80h
  4022.         jc      @f
  4023.         movzx   eax, byte [edx+1]
  4024.         mov     edx, [edx+4]
  4025.         and     eax, 0x0F
  4026.         mov     eax, [edx+eax*4]
  4027.         pop     edx
  4028.         ret     4
  4029. @@:
  4030.         movzx   eax, byte [esi]
  4031.         add     esi, 1
  4032.         mov     [edx+1], al
  4033.         shr     eax, 4
  4034.         mov     edx, [edx+4]
  4035.         mov     eax, [edx+eax*4]
  4036.         pop     edx
  4037.         ret     4
  4038. ;-----------------------------------------------------------------------------
  4039. align 4
  4040. putimage_init32bpp:
  4041.         shl     eax, 2
  4042.         ret
  4043. ;-----------------------------------------------------------------------------
  4044. align 16
  4045. putimage_get32bpp:
  4046.         lodsd
  4047.         ret     4
  4048. ;-----------------------------------------------------------------------------
  4049. align 4
  4050. putimage_init15bpp:
  4051. putimage_init16bpp:
  4052.         add     eax, eax
  4053.         ret
  4054. ;-----------------------------------------------------------------------------
  4055. align 16
  4056. putimage_get15bpp:
  4057. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4058.         push    ecx edx
  4059.         movzx   eax, word [esi]
  4060.         add     esi, 2
  4061.         mov     ecx, eax
  4062.         mov     edx, eax
  4063.         and     eax, 0x1F
  4064.         and     ecx, 0x1F shl 5
  4065.         and     edx, 0x1F shl 10
  4066.         shl     eax, 3
  4067.         shl     ecx, 6
  4068.         shl     edx, 9
  4069.         or      eax, ecx
  4070.         or      eax, edx
  4071.         pop     edx ecx
  4072.         ret     4
  4073. ;-----------------------------------------------------------------------------
  4074. align 16
  4075. putimage_get16bpp:
  4076. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4077.         push    ecx edx
  4078.         movzx   eax, word [esi]
  4079.         add     esi, 2
  4080.         mov     ecx, eax
  4081.         mov     edx, eax
  4082.         and     eax, 0x1F
  4083.         and     ecx, 0x3F shl 5
  4084.         and     edx, 0x1F shl 11
  4085.         shl     eax, 3
  4086.         shl     ecx, 5
  4087.         shl     edx, 8
  4088.         or      eax, ecx
  4089.         or      eax, edx
  4090.         pop     edx ecx
  4091.         ret     4
  4092. ;-----------------------------------------------------------------------------
  4093. ;align 4
  4094. ; eax x beginning
  4095. ; ebx y beginning
  4096. ; ecx x end
  4097.         ; edx y end
  4098. ; edi color
  4099. ;__sys_drawbar:
  4100. ;        mov     esi, [current_slot]
  4101. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4102. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4103. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4104. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4105. ;--------------------------------------
  4106. ;align 4
  4107. ;.forced:
  4108. ;        call    vesa20_drawbar
  4109. ;        call    [draw_pointer]
  4110. ;        ret
  4111. ;-----------------------------------------------------------------------------
  4112. align 4
  4113. kb_read:
  4114.  
  4115.         push    ecx edx
  4116.  
  4117.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4118.       kr_loop:
  4119.         in      al, 0x64
  4120.         test    al, 1
  4121.         jnz     kr_ready
  4122.         loop    kr_loop
  4123.         mov     ah, 1
  4124.         jmp     kr_exit
  4125.       kr_ready:
  4126.         push    ecx
  4127.         mov     ecx, 32
  4128.       kr_delay:
  4129.         loop    kr_delay
  4130.         pop     ecx
  4131.         in      al, 0x60
  4132.         xor     ah, ah
  4133.       kr_exit:
  4134.  
  4135.         pop     edx ecx
  4136.  
  4137.         ret
  4138. ;-----------------------------------------------------------------------------
  4139. align 4
  4140. kb_write:
  4141.  
  4142.         push    ecx edx
  4143.  
  4144.         mov     dl, al
  4145. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4146. ;      kw_loop1:
  4147. ;        in      al,0x64
  4148. ;        test    al,0x20
  4149. ;        jz      kw_ok1
  4150. ;        loop    kw_loop1
  4151. ;        mov     ah,1
  4152. ;        jmp     kw_exit
  4153. ;      kw_ok1:
  4154.         in      al, 0x60
  4155.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4156.       kw_loop:
  4157.         in      al, 0x64
  4158.         test    al, 2
  4159.         jz      kw_ok
  4160.         loop    kw_loop
  4161.         mov     ah, 1
  4162.         jmp     kw_exit
  4163.       kw_ok:
  4164.         mov     al, dl
  4165.         out     0x60, al
  4166.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4167.       kw_loop3:
  4168.         in      al, 0x64
  4169.         test    al, 2
  4170.         jz      kw_ok3
  4171.         loop    kw_loop3
  4172.         mov     ah, 1
  4173.         jmp     kw_exit
  4174.       kw_ok3:
  4175.         mov     ah, 8
  4176.       kw_loop4:
  4177.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4178.       kw_loop5:
  4179.         in      al, 0x64
  4180.         test    al, 1
  4181.         jnz     kw_ok4
  4182.         loop    kw_loop5
  4183.         dec     ah
  4184.         jnz     kw_loop4
  4185.       kw_ok4:
  4186.         xor     ah, ah
  4187.       kw_exit:
  4188.  
  4189.         pop     edx ecx
  4190.  
  4191.         ret
  4192. ;-----------------------------------------------------------------------------
  4193. align 4
  4194. kb_cmd:
  4195.  
  4196.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4197.       c_wait:
  4198.         in      al, 0x64
  4199.         test    al, 2
  4200.         jz      c_send
  4201.         loop    c_wait
  4202.         jmp     c_error
  4203.       c_send:
  4204.         mov     al, bl
  4205.         out     0x64, al
  4206.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4207.       c_accept:
  4208.         in      al, 0x64
  4209.         test    al, 2
  4210.         jz      c_ok
  4211.         loop    c_accept
  4212.       c_error:
  4213.         mov     ah, 1
  4214.         jmp     c_exit
  4215.       c_ok:
  4216.         xor     ah, ah
  4217.       c_exit:
  4218.         ret
  4219.  
  4220.  
  4221. setmouse:  ; set mousepicture -pointer
  4222.            ; ps2 mouse enable
  4223.  
  4224. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4225.  
  4226.         cli
  4227.  
  4228.         ret
  4229.  
  4230. if used _rdtsc
  4231. _rdtsc:
  4232.         bt      [cpu_caps], CAPS_TSC
  4233.         jnc     ret_rdtsc
  4234.         rdtsc
  4235.         ret
  4236.    ret_rdtsc:
  4237.         mov     edx, 0xffffffff
  4238.         mov     eax, 0xffffffff
  4239.         ret
  4240. end if
  4241.  
  4242. sys_msg_board_str:
  4243.  
  4244.         pushad
  4245.    @@:
  4246.         cmp     [esi], byte 0
  4247.         je      @f
  4248.         mov     eax, 1
  4249.         movzx   ebx, byte [esi]
  4250.         call    sys_msg_board
  4251.         inc     esi
  4252.         jmp     @b
  4253.    @@:
  4254.         popad
  4255.         ret
  4256.  
  4257. sys_msg_board_byte:
  4258. ; in: al = byte to display
  4259. ; out: nothing
  4260. ; destroys: nothing
  4261.         pushad
  4262.         mov     ecx, 2
  4263.         shl     eax, 24
  4264.         jmp     @f
  4265.  
  4266. sys_msg_board_word:
  4267. ; in: ax = word to display
  4268. ; out: nothing
  4269. ; destroys: nothing
  4270.         pushad
  4271.         mov     ecx, 4
  4272.         shl     eax, 16
  4273.         jmp     @f
  4274.  
  4275. sys_msg_board_dword:
  4276. ; in: eax = dword to display
  4277. ; out: nothing
  4278. ; destroys: nothing
  4279.         pushad
  4280.         mov     ecx, 8
  4281. @@:
  4282.         push    ecx
  4283.         rol     eax, 4
  4284.         push    eax
  4285.         and     al, 0xF
  4286.         cmp     al, 10
  4287.         sbb     al, 69h
  4288.         das
  4289.         mov     bl, al
  4290.         xor     eax, eax
  4291.         inc     eax
  4292.         call    sys_msg_board
  4293.         pop     eax
  4294.         pop     ecx
  4295.         loop    @b
  4296.         popad
  4297.         ret
  4298.  
  4299. uglobal
  4300.   msg_board_data:
  4301.                   times 4096 db 0
  4302.   msg_board_count dd 0x0
  4303. endg
  4304.  
  4305. sys_msg_board:
  4306.  
  4307. ; eax=1 : write :  bl byte to write
  4308. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4309.  
  4310.         mov     ecx, [msg_board_count]
  4311.         cmp     eax, 1
  4312.         jne     .smbl1
  4313.  
  4314. if defined debug_com_base
  4315.  
  4316.         push    dx ax
  4317.  
  4318.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4319.         mov     dx, debug_com_base+5
  4320.         in      al, dx
  4321.         test    al, 1 shl 5
  4322.         jz      @r
  4323.  
  4324.         mov     dx, debug_com_base      ; Output the byte
  4325.         mov     al, bl
  4326.         out     dx, al
  4327.  
  4328.         pop     ax dx
  4329.  
  4330. end if
  4331.  
  4332.         mov     [msg_board_data+ecx], bl
  4333.         inc     ecx
  4334.         and     ecx, 4095
  4335.         mov     [msg_board_count], ecx
  4336.         mov     [check_idle_semaphore], 5
  4337.         ret
  4338. .smbl1:
  4339.         cmp     eax, 2
  4340.         jne     .smbl2
  4341.         test    ecx, ecx
  4342.         jz      .smbl21
  4343.         mov     eax, msg_board_data+1
  4344.         mov     ebx, msg_board_data
  4345.         movzx   edx, byte [ebx]
  4346.         call    memmove
  4347.         dec     [msg_board_count]
  4348.         mov     [esp + 36], edx ;eax
  4349.         mov     [esp + 24], dword 1
  4350.         ret
  4351. .smbl21:
  4352.         mov     [esp+36], ecx
  4353.         mov     [esp+24], ecx
  4354. .smbl2:
  4355.         ret
  4356.  
  4357. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4358. ;; 66 sys function.                                                ;;
  4359. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4360. ;; out eax                                                         ;;
  4361. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4362. iglobal
  4363. align 4
  4364. f66call:
  4365.            dd sys_process_def.1   ; 1 = set keyboard mode
  4366.            dd sys_process_def.2   ; 2 = get keyboard mode
  4367.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4368.            dd sys_process_def.4
  4369.            dd sys_process_def.5
  4370. endg
  4371.  
  4372.  
  4373.  
  4374.  
  4375. sys_process_def:
  4376.         dec     ebx
  4377.         cmp     ebx, 5
  4378.         jae     .not_support    ;if >=6 then or eax,-1
  4379.  
  4380.         mov     edi, [CURRENT_TASK]
  4381.         jmp     dword [f66call+ebx*4]
  4382.  
  4383. .not_support:
  4384.         or      eax, -1
  4385.         ret
  4386.  
  4387. .1:
  4388.         shl     edi, 8
  4389.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4390.  
  4391.         ret
  4392.  
  4393. .2:                             ; 2 = get keyboard mode
  4394.         shl     edi, 8
  4395.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4396.         mov     [esp+32], eax
  4397.         ret
  4398. ;     xor   eax,eax
  4399. ;     movzx eax,byte [shift]
  4400. ;     movzx ebx,byte [ctrl]
  4401. ;     shl   ebx,2
  4402. ;     add   eax,ebx
  4403. ;     movzx ebx,byte [alt]
  4404. ;     shl   ebx,3
  4405. ;     add   eax,ebx
  4406. .3:                             ;3 = get keyboard ctrl, alt, shift
  4407.  ;// mike.dld [
  4408.         mov     eax, [kb_state]
  4409.  ;// mike.dld ]
  4410.         mov     [esp+32], eax
  4411.         ret
  4412.  
  4413. .4:
  4414.         mov     eax, hotkey_list
  4415. @@:
  4416.         cmp     dword [eax+8], 0
  4417.         jz      .found_free
  4418.         add     eax, 16
  4419.         cmp     eax, hotkey_list+16*256
  4420.         jb      @b
  4421.         mov     dword [esp+32], 1
  4422.         ret
  4423. .found_free:
  4424.         mov     [eax+8], edi
  4425.         mov     [eax+4], edx
  4426.         movzx   ecx, cl
  4427.         lea     ecx, [hotkey_scancodes+ecx*4]
  4428.         mov     edx, [ecx]
  4429.         mov     [eax], edx
  4430.         mov     [ecx], eax
  4431.         mov     [eax+12], ecx
  4432.         jecxz   @f
  4433.         mov     [edx+12], eax
  4434. @@:
  4435.         and     dword [esp+32], 0
  4436.         ret
  4437.  
  4438. .5:
  4439.         movzx   ebx, cl
  4440.         lea     ebx, [hotkey_scancodes+ebx*4]
  4441.         mov     eax, [ebx]
  4442. .scan:
  4443.         test    eax, eax
  4444.         jz      .notfound
  4445.         cmp     [eax+8], edi
  4446.         jnz     .next
  4447.         cmp     [eax+4], edx
  4448.         jz      .found
  4449. .next:
  4450.         mov     eax, [eax]
  4451.         jmp     .scan
  4452. .notfound:
  4453.         mov     dword [esp+32], 1
  4454.         ret
  4455. .found:
  4456.         mov     ecx, [eax]
  4457.         jecxz   @f
  4458.         mov     edx, [eax+12]
  4459.         mov     [ecx+12], edx
  4460. @@:
  4461.         mov     ecx, [eax+12]
  4462.         mov     edx, [eax]
  4463.         mov     [ecx], edx
  4464.         xor     edx, edx
  4465.         mov     [eax+4], edx
  4466.         mov     [eax+8], edx
  4467.         mov     [eax+12], edx
  4468.         mov     [eax], edx
  4469.         mov     [esp+32], edx
  4470.         ret
  4471.  
  4472.  
  4473. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4474. ;; 61 sys function.                                                ;;
  4475. ;; in eax=61,ebx in [1..3]                                         ;;
  4476. ;; out eax                                                         ;;
  4477. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4478. iglobal
  4479. align 4
  4480. f61call:
  4481.            dd sys_gs.1   ; resolution
  4482.            dd sys_gs.2   ; bits per pixel
  4483.            dd sys_gs.3   ; bytes per scanline
  4484. endg
  4485.  
  4486.  
  4487. align 4
  4488.  
  4489. sys_gs:                         ; direct screen access
  4490.         dec     ebx
  4491.         cmp     ebx, 2
  4492.         ja      .not_support
  4493.         jmp     dword [f61call+ebx*4]
  4494. .not_support:
  4495.         or      [esp+32], dword -1
  4496.         ret
  4497.  
  4498.  
  4499. .1:                             ; resolution
  4500.         mov     eax, [Screen_Max_X]
  4501.         shl     eax, 16
  4502.         mov     ax, [Screen_Max_Y]
  4503.         add     eax, 0x00010001
  4504.         mov     [esp+32], eax
  4505.         ret
  4506. .2:                             ; bits per pixel
  4507.         movzx   eax, byte [ScreenBPP]
  4508.         mov     [esp+32], eax
  4509.         ret
  4510. .3:                             ; bytes per scanline
  4511.         mov     eax, [BytesPerScanLine]
  4512.         mov     [esp+32], eax
  4513.         ret
  4514.  
  4515. align 4  ;  system functions
  4516.  
  4517. syscall_setpixel:                       ; SetPixel
  4518.  
  4519.         mov     eax, ebx
  4520.         mov     ebx, ecx
  4521.         mov     ecx, edx
  4522.         mov     edx, [TASK_BASE]
  4523.         add     eax, [edx-twdw+WDATA.box.left]
  4524.         add     ebx, [edx-twdw+WDATA.box.top]
  4525.         mov     edi, [current_slot]
  4526.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4527.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4528.         xor     edi, edi ; no force
  4529.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  4530. ;        jmp     [putpixel]
  4531.         jmp     __sys_putpixel
  4532.  
  4533. align 4
  4534.  
  4535. syscall_writetext:                      ; WriteText
  4536.  
  4537.         mov     eax, [TASK_BASE]
  4538.         mov     ebp, [eax-twdw+WDATA.box.left]
  4539.         push    esi
  4540.         mov     esi, [current_slot]
  4541.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4542.         shl     ebp, 16
  4543.         add     ebp, [eax-twdw+WDATA.box.top]
  4544.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  4545.         pop     esi
  4546.         add     ebx, ebp
  4547.         mov     eax, edi
  4548.         xor     edi, edi
  4549.         jmp     dtext
  4550.  
  4551. align 4
  4552.  
  4553. syscall_openramdiskfile:                ; OpenRamdiskFile
  4554.  
  4555.         mov     eax, ebx
  4556.         mov     ebx, ecx
  4557.         mov     ecx, edx
  4558.         mov     edx, esi
  4559.         mov     esi, 12
  4560.         call    fileread
  4561.         mov     [esp+32], eax
  4562.         ret
  4563.  
  4564. align 4
  4565.  
  4566. syscall_drawrect:                       ; DrawRect
  4567.  
  4568.         mov     edi, edx ; color + gradient
  4569.         and     edi, 0x80FFFFFF
  4570.         test    bx, bx  ; x.size
  4571.         je      .drectr
  4572.         test    cx, cx ; y.size
  4573.         je      .drectr
  4574.  
  4575.         mov     eax, ebx ; bad idea
  4576.         mov     ebx, ecx
  4577.  
  4578.         movzx   ecx, ax ; ecx - x.size
  4579.         shr     eax, 16 ; eax - x.coord
  4580.         movzx   edx, bx ; edx - y.size
  4581.         shr     ebx, 16 ; ebx - y.coord
  4582.         mov     esi, [current_slot]
  4583.  
  4584.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4585.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4586.         add     ecx, eax
  4587.         add     edx, ebx
  4588. ;        jmp     [drawbar]
  4589.         jmp     vesa20_drawbar
  4590. .drectr:
  4591.         ret
  4592.  
  4593. align 4
  4594. syscall_getscreensize:                  ; GetScreenSize
  4595.         mov     ax, [Screen_Max_X]
  4596.         shl     eax, 16
  4597.         mov     ax, [Screen_Max_Y]
  4598.         mov     [esp + 32], eax
  4599.         ret
  4600.  
  4601. align 4
  4602.  
  4603. syscall_cdaudio:                        ; CD
  4604.  
  4605.         cmp     ebx, 4
  4606.         jb      .audio
  4607.         jz      .eject
  4608.         cmp     ebx, 5
  4609.         jnz     .ret
  4610. .load:
  4611.         call    .reserve
  4612.         call    LoadMedium
  4613.         ;call    .free
  4614.         jmp     .free
  4615. ;        ret
  4616. .eject:
  4617.         call    .reserve
  4618.         call    clear_CD_cache
  4619.         call    allow_medium_removal
  4620.         call    EjectMedium
  4621. ;        call    .free
  4622.         jmp     .free
  4623. ;        ret
  4624. .audio:
  4625.         call    sys_cd_audio
  4626.         mov     [esp+36-4], eax
  4627. .ret:
  4628.         ret
  4629.  
  4630. .reserve:
  4631.         call    reserve_cd
  4632.         mov     eax, ecx
  4633.         shr     eax, 1
  4634.         and     eax, 1
  4635.         inc     eax
  4636.         mov     [ChannelNumber], ax
  4637.         mov     eax, ecx
  4638.         and     eax, 1
  4639.         mov     [DiskNumber], al
  4640.         call    reserve_cd_channel
  4641.         and     ebx, 3
  4642.         inc     ebx
  4643.         mov     [cdpos], ebx
  4644.         add     ebx, ebx
  4645.         mov     cl, 8
  4646.         sub     cl, bl
  4647.         mov     al, [DRIVE_DATA+1]
  4648.         shr     al, cl
  4649.         test    al, 2
  4650.         jz      .free;.err
  4651.         ret
  4652. .free:
  4653.         call    free_cd_channel
  4654.         and     [cd_status], 0
  4655.         ret
  4656. .err:
  4657.         call    .free
  4658. ;        pop     eax
  4659.         ret
  4660.  
  4661. align 4
  4662.  
  4663. syscall_getpixel:                       ; GetPixel
  4664.         mov     ecx, [Screen_Max_X]
  4665.         inc     ecx
  4666.         xor     edx, edx
  4667.         mov     eax, ebx
  4668.         div     ecx
  4669.         mov     ebx, edx
  4670.         xchg    eax, ebx
  4671.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4672.         call    dword [GETPIXEL]; eax - x, ebx - y
  4673.         mov     [esp + 32], ecx
  4674.         ret
  4675.  
  4676. align 4
  4677.  
  4678. syscall_getarea:
  4679. ;eax = 36
  4680. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4681. ;ecx = [size x]*65536 + [size y]
  4682. ;edx = [start x]*65536 + [start y]
  4683.         pushad
  4684.         mov     edi, ebx
  4685.         mov     eax, edx
  4686.         shr     eax, 16
  4687.         mov     ebx, edx
  4688.         and     ebx, 0xffff
  4689.         dec     eax
  4690.         dec     ebx
  4691.      ; eax - x, ebx - y
  4692.         mov     edx, ecx
  4693.  
  4694.         shr     ecx, 16
  4695.         and     edx, 0xffff
  4696.         mov     esi, ecx
  4697.      ; ecx - size x, edx - size y
  4698.  
  4699.         mov     ebp, edx
  4700.         dec     ebp
  4701.         lea     ebp, [ebp*3]
  4702.  
  4703.         imul    ebp, esi
  4704.  
  4705.         mov     esi, ecx
  4706.         dec     esi
  4707.         lea     esi, [esi*3]
  4708.  
  4709.         add     ebp, esi
  4710.         add     ebp, edi
  4711.  
  4712.         add     ebx, edx
  4713.  
  4714. .start_y:
  4715.         push    ecx edx
  4716. .start_x:
  4717.         push    eax ebx ecx
  4718.         add     eax, ecx
  4719.  
  4720.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4721.         call    dword [GETPIXEL]; eax - x, ebx - y
  4722.  
  4723.         mov     [ebp], cx
  4724.         shr     ecx, 16
  4725.         mov     [ebp+2], cl
  4726.  
  4727.         pop     ecx ebx eax
  4728.         sub     ebp, 3
  4729.         dec     ecx
  4730.         jnz     .start_x
  4731.         pop     edx ecx
  4732.         dec     ebx
  4733.         dec     edx
  4734.         jnz     .start_y
  4735.         popad
  4736.         ret
  4737.  
  4738. align 4
  4739.  
  4740. syscall_drawline:                       ; DrawLine
  4741.  
  4742.         mov     edi, [TASK_BASE]
  4743.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4744.         mov     ebp, eax
  4745.         mov     esi, [current_slot]
  4746.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4747.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4748.         add     ebp, ebx
  4749.         shl     eax, 16
  4750.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4751.         add     eax, ebp
  4752.         mov     ebp, ebx
  4753.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4754.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4755.         add     ebp, ecx
  4756.         shl     ebx, 16
  4757.         xor     edi, edi
  4758.         add     ebx, ebp
  4759.         mov     ecx, edx
  4760. ;        jmp     [draw_line]
  4761.         jmp     __sys_draw_line
  4762.  
  4763.  
  4764. align 4
  4765. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4766.  
  4767.         call    r_f_port_area
  4768.         mov     [esp+32], eax
  4769.         ret
  4770.  
  4771. align 4
  4772. syscall_threads:                        ; CreateThreads
  4773. ; eax=1 create thread
  4774. ;
  4775. ;   ebx=thread start
  4776. ;   ecx=thread stack value
  4777. ;
  4778. ; on return : eax = pid
  4779.  
  4780.         call    new_sys_threads
  4781.  
  4782.         mov     [esp+32], eax
  4783.         ret
  4784.  
  4785. align 4
  4786.  
  4787. stack_driver_stat:
  4788.  
  4789.         call    app_stack_handler       ; Stack status
  4790.  
  4791. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4792. ;     call  change_task                 ; between sent packet
  4793.  
  4794.         mov     [esp+32], eax
  4795.         ret
  4796.  
  4797. align 4
  4798.  
  4799. socket:                                 ; Socket interface
  4800.         call    app_socket_handler
  4801.  
  4802. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4803. ;     call  change_task                 ; between sent packet
  4804.  
  4805.         mov     [esp+36], eax
  4806.         mov     [esp+24], ebx
  4807.         ret
  4808.  
  4809. align 4
  4810.  
  4811. read_from_hd:                           ; Read from hd - fn not in use
  4812.  
  4813.         mov     edi, [TASK_BASE]
  4814.         add     edi, TASKDATA.mem_start
  4815.         add     eax, [edi]
  4816.         add     ecx, [edi]
  4817.         add     edx, [edi]
  4818.         call    file_read
  4819.  
  4820.         mov     [esp+36], eax
  4821.         mov     [esp+24], ebx
  4822.  
  4823.         ret
  4824.  
  4825. paleholder:
  4826.         ret
  4827. ;------------------------------------------------------------------------------
  4828. align 4
  4829. calculate_fast_getting_offset_for_WinMapAddress:
  4830. ; calculate data area for fast getting offset to _WinMapAddress
  4831.         xor     eax, eax
  4832.         mov     ecx, [_display.height]
  4833.         inc     ecx
  4834.         mov     edi, d_width_calc_area
  4835.         cld
  4836. @@:
  4837.         stosd
  4838.         add     eax, [_display.width]
  4839.         dec     ecx
  4840.         jnz     @r
  4841.         ret
  4842. ;------------------------------------------------------------------------------
  4843. align 4
  4844. set_screen:
  4845.         cmp     eax, [Screen_Max_X]
  4846.         jne     .set
  4847.  
  4848.         cmp     edx, [Screen_Max_Y]
  4849.         jne     .set
  4850.         ret
  4851. .set:
  4852.         pushfd
  4853.         cli
  4854.  
  4855.         mov     [Screen_Max_X], eax
  4856.         mov     [Screen_Max_Y], edx
  4857.         mov     [BytesPerScanLine], ecx
  4858.  
  4859.         mov     [screen_workarea.right], eax
  4860.         mov     [screen_workarea.bottom], edx
  4861.  
  4862.         push    ebx
  4863.         push    esi
  4864.         push    edi
  4865.  
  4866.         pushad
  4867.  
  4868.         stdcall kernel_free, [_WinMapAddress]
  4869.  
  4870.         mov     eax, [_display.width]
  4871.         mul     [_display.height]
  4872.         mov     [_WinMapSize], eax
  4873.  
  4874.         stdcall kernel_alloc, eax
  4875.         mov     [_WinMapAddress], eax
  4876.         test    eax, eax
  4877.         jz      .epic_fail
  4878.  
  4879.         call    calculate_fast_getting_offset_for_WinMapAddress
  4880.  
  4881.         popad
  4882.  
  4883.         call    repos_windows
  4884.         xor     eax, eax
  4885.         xor     ebx, ebx
  4886.         mov     ecx, [Screen_Max_X]
  4887.         mov     edx, [Screen_Max_Y]
  4888.         call    calculatescreen
  4889.         pop     edi
  4890.         pop     esi
  4891.         pop     ebx
  4892.  
  4893.         popfd
  4894.         ret
  4895.  
  4896. .epic_fail:
  4897.         hlt                     ; Houston, we've had a problem
  4898.  
  4899. ; --------------- APM ---------------------
  4900. uglobal
  4901. apm_entry       dp      0
  4902. apm_vf          dd      0
  4903. endg
  4904.  
  4905. align 4
  4906. sys_apm:
  4907.         xor     eax, eax
  4908.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4909.         jne     @f
  4910.         inc     eax
  4911.         or      dword [esp + 44], eax   ; error
  4912.         add     eax, 7
  4913.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4914.         ret
  4915.  
  4916. @@:
  4917. ;       xchg    eax, ecx
  4918. ;       xchg    ebx, ecx
  4919.  
  4920.         cmp     dx, 3
  4921.         ja      @f
  4922.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4923.         mov     eax, [apm_vf]
  4924.         mov     [esp + 32], eax
  4925.         shr     eax, 16
  4926.         mov     [esp + 28], eax
  4927.         ret
  4928.  
  4929. @@:
  4930.  
  4931.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4932.         xchg    [master_tab], esi
  4933.         push    esi
  4934.         mov     edi, cr3
  4935.         mov     cr3, edi                ;flush TLB
  4936.  
  4937.         call    pword [apm_entry]       ;call APM BIOS
  4938.  
  4939.         xchg    eax, [esp]
  4940.         mov     [master_tab], eax
  4941.         mov     eax, cr3
  4942.         mov     cr3, eax
  4943.         pop     eax
  4944.  
  4945.         mov     [esp + 4 ], edi
  4946.         mov     [esp + 8], esi
  4947.         mov     [esp + 20], ebx
  4948.         mov     [esp + 24], edx
  4949.         mov     [esp + 28], ecx
  4950.         mov     [esp + 32], eax
  4951.         setc    al
  4952.         and     [esp + 44], byte 0xfe
  4953.         or      [esp + 44], al
  4954.         ret
  4955. ; -----------------------------------------
  4956.  
  4957. align 4
  4958.  
  4959. undefined_syscall:                      ; Undefined system call
  4960.         mov     [esp + 32], dword -1
  4961.         ret
  4962.  
  4963. align 4
  4964. system_shutdown:          ; shut down the system
  4965.  
  4966.         cmp     byte [BOOT_VAR+0x9030], 1
  4967.         jne     @F
  4968.         ret
  4969. @@:
  4970.         call    stop_all_services
  4971.         push    3                ; stop playing cd
  4972.         pop     eax
  4973.         call    sys_cd_audio
  4974.  
  4975. yes_shutdown_param:
  4976.         cli
  4977.  
  4978. if ~ defined extended_primary_loader
  4979.         mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
  4980.         push    12
  4981.         pop     esi
  4982.         xor     ebx, ebx
  4983.         or      ecx, -1
  4984.         mov     edx, OS_BASE+0x70000
  4985.         call    fileread
  4986.  
  4987.         mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  4988.         mov     edi, OS_BASE+0x40000
  4989.         mov     ecx, 1000
  4990.         rep movsb
  4991. end if
  4992.  
  4993.         mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
  4994.         mov     edi, OS_BASE
  4995.         mov     ecx, 0x10000/4
  4996.         cld
  4997.         rep movsd
  4998.  
  4999.         call    restorefatchain
  5000.  
  5001.         call    IRQ_mask_all
  5002.  
  5003. if 0
  5004.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5005.         mov     word [OS_BASE+0x467+2], 0x1000
  5006.  
  5007.         mov     al, 0x0F
  5008.         out     0x70, al
  5009.         mov     al, 0x05
  5010.         out     0x71, al
  5011.  
  5012.         mov     al, 0xFE
  5013.         out     0x64, al
  5014.  
  5015.         hlt
  5016.         jmp     $-1
  5017.  
  5018. else
  5019.         cmp     byte [OS_BASE + 0x9030], 2
  5020.         jnz     no_acpi_power_off
  5021.  
  5022. ; scan for RSDP
  5023. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5024.         movzx   eax, word [OS_BASE + 0x40E]
  5025.         shl     eax, 4
  5026.         jz      @f
  5027.         mov     ecx, 1024/16
  5028.         call    scan_rsdp
  5029.         jnc     .rsdp_found
  5030. @@:
  5031. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5032.         mov     eax, 0xE0000
  5033.         mov     ecx, 0x2000
  5034.         call    scan_rsdp
  5035.         jc      no_acpi_power_off
  5036. .rsdp_found:
  5037.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5038.         mov     ebp, [ipc_tmp]
  5039.         stdcall map_page, ebp, esi, PG_MAP
  5040.         lea     eax, [esi+1000h]
  5041.         lea     edx, [ebp+1000h]
  5042.         stdcall map_page, edx, eax, PG_MAP
  5043.         and     esi, 0xFFF
  5044.         add     esi, ebp
  5045.         cmp     dword [esi], 'RSDT'
  5046.         jnz     no_acpi_power_off
  5047.         mov     ecx, [esi+4]
  5048.         sub     ecx, 24h
  5049.         jbe     no_acpi_power_off
  5050.         shr     ecx, 2
  5051.         add     esi, 24h
  5052. .scan_fadt:
  5053.         lodsd
  5054.         mov     ebx, eax
  5055.         lea     eax, [ebp+2000h]
  5056.         stdcall map_page, eax, ebx, PG_MAP
  5057.         lea     eax, [ebp+3000h]
  5058.         add     ebx, 0x1000
  5059.         stdcall map_page, eax, ebx, PG_MAP
  5060.         and     ebx, 0xFFF
  5061.         lea     ebx, [ebx+ebp+2000h]
  5062.         cmp     dword [ebx], 'FACP'
  5063.         jz      .fadt_found
  5064.         loop    .scan_fadt
  5065.         jmp     no_acpi_power_off
  5066. .fadt_found:
  5067. ; ebx is linear address of FADT
  5068.         mov     edi, [ebx+40] ; physical address of the DSDT
  5069.         lea     eax, [ebp+4000h]
  5070.         stdcall map_page, eax, edi, PG_MAP
  5071.         lea     eax, [ebp+5000h]
  5072.         lea     esi, [edi+0x1000]
  5073.         stdcall map_page, eax, esi, PG_MAP
  5074.         and     esi, 0xFFF
  5075.         sub     edi, esi
  5076.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5077.         jnz     no_acpi_power_off
  5078.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5079.         sub     eax, 36+4
  5080.         jbe     no_acpi_power_off
  5081.         add     esi, 36
  5082. .scan_dsdt:
  5083.         cmp     dword [esi+ebp+4000h], '_S5_'
  5084.         jnz     .scan_dsdt_cont
  5085.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5086.         jnz     .scan_dsdt_cont
  5087.         mov     dl, [esi+ebp+4000h+6]
  5088.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5089.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5090.         ja      .scan_dsdt_cont
  5091.         cmp     dl, 1
  5092.         jb      .scan_dsdt_cont
  5093.         lea     esi, [esi+ebp+4000h+7]
  5094.         xor     ecx, ecx
  5095.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5096.         jz      @f
  5097.         cmp     byte [esi], 0xA
  5098.         jnz     no_acpi_power_off
  5099.         inc     esi
  5100.         mov     cl, [esi]
  5101. @@:
  5102.         inc     esi
  5103.         cmp     dl, 2
  5104.         jb      @f
  5105.         cmp     byte [esi], 0
  5106.         jz      @f
  5107.         cmp     byte [esi], 0xA
  5108.         jnz     no_acpi_power_off
  5109.         inc     esi
  5110.         mov     ch, [esi]
  5111. @@:
  5112.         jmp     do_acpi_power_off
  5113. .scan_dsdt_cont:
  5114.         inc     esi
  5115.         cmp     esi, 0x1000
  5116.         jb      @f
  5117.         sub     esi, 0x1000
  5118.         add     edi, 0x1000
  5119.         push    eax
  5120.         lea     eax, [ebp+4000h]
  5121.         stdcall map_page, eax, edi, PG_MAP
  5122.         push    PG_MAP
  5123.         lea     eax, [edi+1000h]
  5124.         push    eax
  5125.         lea     eax, [ebp+5000h]
  5126.         push    eax
  5127.         stdcall map_page
  5128.         pop     eax
  5129. @@:
  5130.         dec     eax
  5131.         jnz     .scan_dsdt
  5132.         jmp     no_acpi_power_off
  5133. do_acpi_power_off:
  5134.         mov     edx, [ebx+48]
  5135.         test    edx, edx
  5136.         jz      .nosmi
  5137.         mov     al, [ebx+52]
  5138.         out     dx, al
  5139.         mov     edx, [ebx+64]
  5140. @@:
  5141.         in      ax, dx
  5142.         test    al, 1
  5143.         jz      @b
  5144. .nosmi:
  5145.         and     cx, 0x0707
  5146.         shl     cx, 2
  5147.         or      cx, 0x2020
  5148.         mov     edx, [ebx+64]
  5149.         in      ax, dx
  5150.         and     ax, 203h
  5151.         or      ah, cl
  5152.         out     dx, ax
  5153.         mov     edx, [ebx+68]
  5154.         test    edx, edx
  5155.         jz      @f
  5156.         in      ax, dx
  5157.         and     ax, 203h
  5158.         or      ah, ch
  5159.         out     dx, ax
  5160. @@:
  5161.         jmp     $
  5162.  
  5163.  
  5164. no_acpi_power_off:
  5165.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5166.         mov     word [OS_BASE+0x467+2], 0x1000
  5167.  
  5168.         mov     al, 0x0F
  5169.         out     0x70, al
  5170.         mov     al, 0x05
  5171.         out     0x71, al
  5172.  
  5173.         mov     al, 0xFE
  5174.         out     0x64, al
  5175.  
  5176.         hlt
  5177.         jmp     $-1
  5178.  
  5179. scan_rsdp:
  5180.         add     eax, OS_BASE
  5181. .s:
  5182.         cmp     dword [eax], 'RSD '
  5183.         jnz     .n
  5184.         cmp     dword [eax+4], 'PTR '
  5185.         jnz     .n
  5186.         xor     edx, edx
  5187.         xor     esi, esi
  5188. @@:
  5189.         add     dl, [eax+esi]
  5190.         inc     esi
  5191.         cmp     esi, 20
  5192.         jnz     @b
  5193.         test    dl, dl
  5194.         jz      .ok
  5195. .n:
  5196.         add     eax, 10h
  5197.         loop    .s
  5198.         stc
  5199. .ok:
  5200.         ret
  5201. end if
  5202.  
  5203. include "data32.inc"
  5204.  
  5205. __REV__ = __REV
  5206.  
  5207. uglobals_size = $ - endofcode
  5208. diff16 "end of kernel code",0,$
  5209.