Subversion Repositories Kolibri OS

Rev

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

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