Subversion Repositories Kolibri OS

Rev

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