Subversion Repositories Kolibri OS

Rev

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

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