Subversion Repositories Kolibri OS

Rev

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