Subversion Repositories Kolibri OS

Rev

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