Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. format MS COFF
  3.  
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5. ;;
  6. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.
  7. ;; PROGRAMMING:
  8. ;; Ivan Poddubny
  9. ;; Marat Zakiyanov (Mario79)
  10. ;; VaStaNi
  11. ;; Trans
  12. ;; Mihail Semenyako (mike.dld)
  13. ;; Sergey Kuzmin (Wildwest)
  14. ;; Andrey Halyavin (halyavin)
  15. ;; Mihail Lisovin (Mihasik)
  16. ;; Andrey Ignatiev (andrew_programmer)
  17. ;; NoName
  18. ;; Evgeny Grechnikov (Diamond)
  19. ;; Iliya Mihailov (Ghost)
  20. ;; Sergey Semyonov (Serge)
  21. ;; Johnny_B
  22. ;; SPraid (simba)
  23. ;;
  24. ;; Data in this file was originally part of MenuetOS project which is
  25. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  26. ;; part of KolibriOS project under the terms of GNU GPL.
  27. ;;
  28. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  29. ;; PROGRAMMING:
  30. ;;
  31. ;; Ville Mikael Turjanmaa, villemt@itu.jyu.fi
  32. ;; - main os coding/design
  33. ;; Jan-Michael Brummer, BUZZ2@gmx.de
  34. ;; Felix Kaiser, info@felix-kaiser.de
  35. ;; Paolo Minazzi, paolo.minazzi@inwind.it
  36. ;; quickcode@mail.ru
  37. ;; Alexey, kgaz@crosswinds.net
  38. ;; Juan M. Caravaca, bitrider@wanadoo.es
  39. ;; kristol@nic.fi
  40. ;; Mike Hibbett, mikeh@oceanfree.net
  41. ;; Lasse Kuusijarvi, kuusijar@lut.fi
  42. ;; Jarek Pelczar, jarekp3@wp.pl
  43. ;;
  44. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  45. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  46. ;; consequences of using it or for whether it serves any particular purpose or
  47. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  48. ;; License (the "GPL") for full details.
  49. ;
  50. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  51. ;; but only under the conditions described in the GPL. A copy of this license
  52. ;; is supposed to have been given to you along with KolibriOS so you can know
  53. ;; your rights and responsibilities. It should be in a file named COPYING.
  54. ;; Among other things, the copyright notice and this notice must be preserved
  55. ;; on all copies.
  56. ;;
  57. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58.  
  59. include 'macros.inc'
  60.  
  61. $Revision: 864 $
  62.  
  63.  
  64. fastcall equ call
  65.  
  66. USE_COM_IRQ equ 1      ;make irq 3 and irq 4 available for PCI devices
  67.  
  68. include "proc32.inc"
  69. include "kglobals.inc"
  70. include "lang.inc"
  71.  
  72. include "const.inc"
  73. max_processes    equ   255
  74. tss_step         equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
  75.  
  76.  
  77. os_stack       equ  (os_data_l-_gdts)    ; GDTs
  78. os_code        equ  (os_code_l-_gdts)
  79. graph_data     equ  (3+graph_data_l-_gdts)
  80. tss0           equ  (tss0_l-_gdts)
  81. app_code       equ  (3+app_code_l-_gdts)
  82. app_data       equ  (3+app_data_l-_gdts)
  83. pci_code_sel   equ  (pci_code_32-_gdts)
  84. pci_data_sel   equ  (pci_data_32-_gdts)
  85.  
  86.  
  87. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  88. ;;
  89. ;;   Included files:
  90. ;;
  91. ;;   Kernel16.inc
  92. ;;    - Booteng.inc   English text for bootup
  93. ;;    - Bootcode.inc  Hardware setup
  94. ;;    - Pci16.inc     PCI functions
  95. ;;
  96. ;;   Kernel32.inc
  97. ;;    - Sys32.inc     Process management
  98. ;;    - Shutdown.inc  Shutdown and restart
  99. ;;    - Fat32.inc     Read / write hd
  100. ;;    - Vesa12.inc    Vesa 1.2 driver
  101. ;;    - Vesa20.inc    Vesa 2.0 driver
  102. ;;    - Vga.inc       VGA driver
  103. ;;    - Stack.inc     Network interface
  104. ;;    - Mouse.inc     Mouse pointer
  105. ;;    - Scincode.inc  Window skinning
  106. ;;    - Pci32.inc     PCI functions
  107. ;;
  108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  109.  
  110.  
  111. public __os_stack
  112. public _boot_mbi
  113. public _sys_pdbr
  114. public _gdts
  115. public _high_code
  116.  
  117. public __hlt
  118. public _panic_printf
  119. public _printf
  120. public _pg_balloc
  121. public _mem_amount
  122. public @balloc@4
  123.  
  124. public __setvars
  125.  
  126. public _rd_base
  127. public _rd_fat
  128. public _rd_fat_end
  129. public _rd_root
  130. public _rd_root_end
  131.  
  132.  
  133. extrn __edata
  134.  
  135. extrn _16bit_start
  136. extrn _16bit_end
  137.  
  138. extrn _enter_bootscreen
  139. extrn _poweroff
  140.  
  141. extrn _init
  142. extrn _init_mm
  143.  
  144. extrn @core_alloc@4
  145.  
  146. extrn @init_heap@8
  147. extrn @find_large_md@4
  148. extrn @find_small_md@4
  149. extrn @phis_alloc@4
  150. extrn @mem_alloc@8
  151.  
  152. extrn @heap_alloc@8
  153.  
  154. extrn _slab_cache_init
  155. extrn _alloc_page
  156.  
  157. extrn _get_free_mem
  158.  
  159. extrn _bx_from_load
  160.  
  161.  
  162. @mem_alloc@8         equ @heap_alloc@8
  163.  
  164.  
  165. section '.flat' code readable align 4096
  166.  
  167. use32
  168.  
  169. align 4
  170.  
  171. use32
  172.  
  173.         ;   call test_cpu
  174.         ;   bts [cpu_caps-OS_BASE], CAPS_TSC     ;force use rdtsc
  175.  
  176.         ;   call init_BIOS32
  177.  
  178. ;           jmp pword os_code:high_code
  179.  
  180. align 4
  181. ;bios32_entry    dd ?
  182. ;tmp_page_tabs   dd ?
  183.  
  184.  
  185. __DEBUG__ fix 1
  186. __DEBUG_LEVEL__ fix 1
  187.  
  188.  
  189. MEM_WB     equ 6               ;write-back memory
  190. MEM_WC     equ 1               ;write combined memory
  191. MEM_UC     equ 0               ;uncached memory
  192.  
  193.  
  194. __hlt:
  195.            cli
  196. @@:
  197.            hlt
  198.            jmp @B
  199.  
  200. align 4
  201. _panic_printf:
  202.  
  203.            mov dword [esp], __hlt
  204.            jmp _printf
  205.  
  206. align 4
  207. include 'printf.inc'
  208.  
  209. align 4
  210. proc test_cpu
  211.            locals
  212.               cpu_type   dd ?
  213.               cpu_id     dd ?
  214.               cpu_Intel  dd ?
  215.               cpu_AMD    dd ?
  216.            endl
  217.  
  218.            mov [cpu_type], 0
  219.            xor eax, eax
  220.            mov [cpu_caps], eax
  221.            mov [cpu_caps+4], eax
  222.  
  223.            xor eax, eax
  224.            cpuid
  225.  
  226.            mov [cpu_vendor], ebx
  227.            mov [cpu_vendor+4], edx
  228.            mov [cpu_vendor+8], ecx
  229.            cmp ebx, dword [intel_str]
  230.            jne .check_AMD
  231.            cmp edx, dword [intel_str+4]
  232.            jne .check_AMD
  233.            cmp ecx, dword [intel_str+8]
  234.            jne .check_AMD
  235.            mov [cpu_Intel], 1
  236.            cmp eax, 1
  237.            jl .end_cpuid
  238.            mov eax, 1
  239.            cpuid
  240.            mov [cpu_sign], eax
  241.            mov [cpu_info],  ebx
  242.            mov [cpu_caps],  edx
  243.            mov [cpu_caps+4],ecx
  244.  
  245.            shr eax, 8
  246.            and eax, 0x0f
  247.            ret
  248. .end_cpuid:
  249.            mov eax, [cpu_type]
  250.            ret
  251.  
  252. .check_AMD:
  253.            cmp ebx, dword [AMD_str]
  254.            jne .unknown
  255.            cmp edx, dword [AMD_str+4]
  256.            jne .unknown
  257.            cmp ecx, dword [AMD_str+8]
  258.            jne .unknown
  259.            mov [cpu_AMD], 1
  260.            cmp eax, 1
  261.            jl .unknown
  262.            mov eax, 1
  263.            cpuid
  264.            mov [cpu_sign], eax
  265.            mov [cpu_info],  ebx
  266.            mov [cpu_caps],  edx
  267.            mov [cpu_caps+4],ecx
  268.            shr eax, 8
  269.            and eax, 0x0f
  270.            ret
  271. .unknown:
  272.            mov eax, 1
  273.            cpuid
  274.            mov [cpu_sign], eax
  275.            mov [cpu_info],  ebx
  276.            mov [cpu_caps],  edx
  277.            mov [cpu_caps+4],ecx
  278.            shr eax, 8
  279.            and eax, 0x0f
  280.            ret
  281. endp
  282.  
  283. align 4
  284. _copy_pg_balloc  dd 0
  285.  
  286.  
  287. _high_code:
  288.  
  289.            mov ax,os_stack
  290.            mov dx,app_data
  291.            mov ss,ax
  292.            mov esp, __os_stack
  293.  
  294.            mov ds, dx
  295.            mov es, dx
  296.            mov fs, dx
  297.            mov gs, dx
  298.  
  299.  
  300.          ;  bt [cpu_caps], CAPS_PGE
  301.          ;  jnc @F
  302.  
  303.          ;  or dword [sys_pgdir-OS_BASE+(OS_BASE shr 20)], PG_GLOBAL
  304.  
  305.          ;  mov ebx, cr4
  306.          ;  or ebx, CR4_PGE
  307.          ;  mov cr4, ebx
  308. @@:
  309.          ;  mov eax, cr3
  310.          ;  mov cr3, eax           ; flush TLB
  311.  
  312.            mov edx, 0x3fB
  313.            mov eax, 3
  314.            out dx, al
  315.  
  316.            call test_cpu
  317.            call _init
  318.  
  319.            mov eax, [_pg_balloc]
  320.            mov [_copy_pg_balloc], eax
  321.  
  322.            mov [tss._ss0], os_stack
  323.            mov [tss._esp0], __os_stack
  324.            mov [tss._esp], __os_stack
  325.            mov [tss._cs],os_code
  326.            mov [tss._ss],os_stack
  327.            mov [tss._ds],app_data
  328.            mov [tss._es],app_data
  329.            mov [tss._fs],app_data
  330.            mov [tss._gs],app_data
  331.            mov [tss._io],128
  332. ;Add IO access table - bit array of permitted ports
  333.            mov edi, tss._io_map_0
  334.            xor eax, eax
  335.            not eax
  336.            mov ecx, 8192/4
  337.            rep stosd                 ; access to 4096*8=65536 ports
  338.  
  339.            mov  ax,tss0
  340.            ltr  ax
  341.  
  342. __core_restart:
  343.  
  344.            mov ecx, 1280*1024
  345.            fastcall _balloc
  346.            mov [_display_data], eax
  347.  
  348.            mov ecx, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
  349.                     (unpack.lc+unpack.lp)))*4
  350.            fastcall _balloc
  351.            mov [unpack.p], eax
  352.  
  353. ; MEMORY MODEL
  354.  
  355.            call _init_mm
  356.            mov [pg_data.pg_mutex], 0
  357.  
  358.            call _slab_cache_init
  359.  
  360.            mov ecx, 0x80000000
  361.            mov edx, 0x40000000
  362.            call @init_heap@8
  363.  
  364.            mov esi, _16bit_start
  365.            mov ecx, _16bit_end
  366.            shr ecx, 2
  367.            mov edi, _16BIT_BASE
  368.            cld
  369.            rep movsd
  370.  
  371.            jmp far 0x60:_enter_bootscreen;
  372.  
  373. align 4
  374. __setvars:
  375.            mov ax,os_stack
  376.            mov dx,app_data
  377.            mov ss,ax
  378.            mov esp, __os_stack
  379.  
  380.            mov ds, dx
  381.            mov es, dx
  382.            mov fs, dx
  383.            mov gs, dx
  384.  
  385. ; SAVE & CLEAR 0-0xffff
  386.  
  387.            cld
  388.            xor esi, esi
  389.            mov   edi,BOOT_VAR
  390.            mov   ecx,0x10000 / 4
  391.            rep   movsd
  392.  
  393.            xor edi, edi
  394.            xor eax, eax
  395.            mov   ecx,0x10000 / 4
  396.            rep   stosd
  397.  
  398.            mov edi, 0x40000
  399.            mov ecx, (0x90000-0x40000)/4
  400.            rep stosd
  401.  
  402.            mov dword [_sys_pdbr], eax
  403.            mov dword [_sys_pdbr+4], eax
  404.            mov eax, cr3
  405.            mov cr3, eax
  406.  
  407. ; SAVE REAL MODE VARIABLES
  408.         mov     ax, [BOOT_VAR + 0x9031]
  409.         mov     [IDEContrRegsBaseAddr], ax
  410. ; --------------- APM ---------------------
  411.  
  412. ; init selectors
  413.     mov ebx,    [BOOT_VAR +0x9040]    ; offset of APM entry point
  414.     movzx eax, word [BOOT_VAR+0x9050] ; real-mode segment base address of
  415.                                       ; protected-mode 32-bit code segment
  416.     movzx ecx, word [BOOT_VAR+0x9052] ; real-mode segment base address of
  417.                                       ; protected-mode 16-bit code segment
  418.     movzx edx, word [BOOT_VAR+0x9054] ; real-mode segment base address of
  419.                                       ; protected-mode 16-bit data segment
  420.  
  421.     shl    eax, 4
  422.     mov    [dword apm_code_32 + 2], ax
  423.     shr    eax, 16
  424.     mov    [dword apm_code_32 + 4], al
  425.  
  426.     shl    ecx, 4
  427.     mov    [dword apm_code_16 + 2], cx
  428.     shr    ecx, 16
  429.     mov    [dword apm_code_16 + 4], cl
  430.  
  431.     shl    edx, 4
  432.     mov    [dword apm_data_16 + 2], dx
  433.     shr    edx, 16
  434.     mov    [dword apm_data_16 + 4], dl
  435.  
  436.     mov    dword[apm_entry], ebx
  437.     mov    word [apm_entry + 4], apm_code_32 - _gdts
  438.  
  439.     mov    eax, [BOOT_VAR + 0x9044]    ; version & flags
  440.     mov    [apm_vf], eax
  441. ; -----------------------------------------
  442. ;        movzx eax,byte [BOOT_VAR+0x9010]  ; mouse port
  443. ;        mov   [0xF604],byte 1  ;al
  444.         mov     al, [BOOT_VAR+0x901F]   ; DMA access
  445.         mov     [allow_dma_access], al
  446.         mov   al,[BOOT_VAR+0x9000]        ; bpp
  447.         mov   [ScreenBPP],al
  448.  
  449.         movzx eax,word [BOOT_VAR+0x900A]  ; X max
  450.         dec   eax
  451.         mov   [Screen_Max_X],eax
  452.         mov   [screen_workarea.right],eax
  453.         movzx eax,word [BOOT_VAR+0x900C]  ; Y max
  454.         dec   eax
  455.         mov   [Screen_Max_Y],eax
  456.         mov   [screen_workarea.bottom],eax
  457.         movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  458.         mov   [SCR_MODE],eax
  459.         mov   eax,[BOOT_VAR+0x9014]       ; Vesa 1.2 bnk sw add
  460.         mov   [BANK_SWITCH],eax
  461.         mov   [BytesPerScanLine],word 640*4         ; Bytes PerScanLine
  462.         cmp   [SCR_MODE],word 0x13          ; 320x200
  463.         je    @f
  464.         cmp   [SCR_MODE],word 0x12          ; VGA 640x480
  465.         je    @f
  466.  
  467.         mov   ax,[BOOT_VAR+0x9001]        ; for other modes
  468.         mov   [BytesPerScanLine],ax
  469. @@:
  470.         mov     esi, BOOT_VAR+0x9080
  471.         movzx   ecx, byte [esi-1]
  472.         mov     [NumBiosDisks], ecx
  473.         mov     edi, BiosDisksData
  474.         rep     movsd
  475.  
  476. ; GRAPHICS ADDRESSES
  477.  
  478.         mov     byte [BOOT_VAR+0x901e],0x0
  479.         mov     eax,[BOOT_VAR+0x9018]
  480.         mov     [LFBAddress],eax
  481.  
  482.         cmp     [SCR_MODE],word 0100000000000000b
  483.         jge     setvesa20
  484.         cmp     [SCR_MODE],word 0x13
  485.         je      v20ga32
  486.         mov     [PUTPIXEL],dword Vesa12_putpixel24  ; Vesa 1.2
  487.         mov     [GETPIXEL],dword Vesa12_getpixel24
  488.         cmp     [ScreenBPP],byte 24
  489.         jz      ga24
  490.         mov     [PUTPIXEL],dword Vesa12_putpixel32
  491.         mov     [GETPIXEL],dword Vesa12_getpixel32
  492.       ga24:
  493.         jmp     v20ga24
  494.       setvesa20:
  495.         mov     [PUTPIXEL],dword Vesa20_putpixel24  ; Vesa 2.0
  496.         mov     [GETPIXEL],dword Vesa20_getpixel24
  497.         cmp     [ScreenBPP],byte 24
  498.         jz      v20ga24
  499.       v20ga32:
  500.         mov     [PUTPIXEL],dword Vesa20_putpixel32
  501.         mov     [GETPIXEL],dword Vesa20_getpixel32
  502.       v20ga24:
  503.         cmp     [SCR_MODE],word 0x12                ; 16 C VGA 640x480
  504.         jne     no_mode_0x12
  505.         mov     [PUTPIXEL],dword VGA_putpixel
  506.         mov     [GETPIXEL],dword Vesa20_getpixel32
  507.       no_mode_0x12:
  508.  
  509. ; -------- Fast System Call init ----------
  510. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  511.            bt [cpu_caps], CAPS_SEP
  512.            jnc .SEnP   ; SysEnter not Present
  513.            xor edx, edx
  514.            mov ecx, MSR_SYSENTER_CS
  515.            mov eax, os_code
  516.            wrmsr
  517.            mov ecx, MSR_SYSENTER_ESP
  518. ;           mov eax, sysenter_stack ; Check it
  519.            xor     eax, eax
  520.            wrmsr
  521.            mov ecx, MSR_SYSENTER_EIP
  522.            mov eax, sysenter_entry
  523.            wrmsr
  524. .SEnP:
  525. ; AMD SYSCALL/SYSRET
  526.            cmp byte[cpu_vendor], 'A'
  527.            jne .noSYSCALL
  528.            mov eax, 0x80000001
  529.            cpuid
  530.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  531.            jz .noSYSCALL
  532.            mov ecx, MSR_AMD_EFER
  533.            rdmsr
  534.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  535.            wrmsr
  536.  
  537.         ; !!!! It`s dirty hack, fix it !!!
  538.         ; Bits of EDX :
  539.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  540.         ;  and the contents of this field, plus 8, are copied into the SS register.
  541.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  542.         ;  and the contents of this field, plus 8, are copied into the SS register.
  543.  
  544.         ; mov   edx, (os_code + 16) * 65536 + os_code
  545.            mov edx, 0x1B0008
  546.  
  547.            mov eax, syscall_entry
  548.            mov ecx, MSR_AMD_STAR
  549.            wrmsr
  550. .noSYSCALL:
  551. ; -----------------------------------------
  552. ; LOAD IDT
  553.  
  554.            call _init_idt
  555.  
  556.            mov [LFBSize], 0x800000
  557.            call init_LFB
  558.            call init_fpu
  559.            call init_malloc
  560.  
  561.            mov ecx, 0x51000
  562.            xor edx, edx
  563.            call @mem_alloc@8
  564.            mov [default_io_map], eax
  565.  
  566.            add eax, 0x2000
  567.            mov [ipc_tmp], eax
  568.            mov ebx, 0x1000
  569.  
  570.            add eax, 0x40000
  571.            mov [proc_mem_map], eax
  572.  
  573.            add eax, 0x8000
  574.            mov [proc_mem_pdir], eax
  575.  
  576.            add eax, ebx
  577.            mov [proc_mem_tab], eax
  578.  
  579.            add eax, ebx
  580.            mov [tmp_task_pdir], eax
  581.  
  582.            add eax, ebx
  583.            mov [tmp_task_ptab], eax
  584.  
  585.            add eax, ebx
  586.            mov [ipc_pdir], eax
  587.  
  588.            add eax, ebx
  589.            mov [ipc_ptab], eax
  590.  
  591.            call init_events
  592.            mov eax, srv.fd-SRV_FD_OFFSET
  593.            mov [srv.fd], eax
  594.            mov [srv.bk], eax
  595.  
  596.            mov edi, irq_tab
  597.            xor eax, eax
  598.            mov ecx, 16
  599.            rep stosd
  600.  
  601. ;Set base of graphic segment to linear address of LFB
  602.         mov     eax,[LFBAddress]          ; set for gs
  603.         mov     [graph_data_l+2],ax
  604.         shr     eax,16
  605.         mov     [graph_data_l+4],al
  606.         mov     [graph_data_l+7],ah
  607.  
  608.         mov [CURRENT_TASK],dword 1
  609.         mov [TASK_COUNT],dword 1
  610.         mov [TASK_BASE],dword TASK_DATA
  611.         mov [current_slot], SLOT_BASE+256
  612.  
  613. ; set background
  614.         xor  eax,eax
  615.         inc  eax
  616.         mov   [BgrDrawMode],eax
  617.         mov   [BgrDataWidth],eax
  618.         mov   [BgrDataHeight],eax
  619.         mov ecx, 4095
  620.         mov edx, PG_SW
  621.         mov    [mem_BACKGROUND],4095
  622.         call @mem_alloc@8
  623.         mov [img_background], eax
  624.  
  625.         mov     [SLOT_BASE + 256 + APPDATA.dir_table], _sys_pdbr + (0x100000000-OS_BASE)
  626.  
  627. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  628.  
  629.         call  rerouteirqs
  630.  
  631. ; Initialize system V86 machine
  632.         call    init_sys_v86
  633.  
  634. ; TIMER SET TO 1/100 S
  635.  
  636.         mov   al,0x34              ; set to 100Hz
  637.         out   0x43,al
  638.         mov   al,0x9b              ; lsb    1193180 / 1193
  639.         out   0x40,al
  640.         mov   al,0x2e              ; msb
  641.         out   0x40,al
  642.  
  643. ; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
  644. ; they are used: when partitions are scanned, hd_read relies on timer
  645.         mov     al, 0xFE
  646.         out     0x21, al
  647.         mov     al, 0x3F
  648.         out     0xA1, al
  649.  
  650. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  651. include 'detect/disks.inc'
  652. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  653.  
  654.   call Parser_params
  655.  
  656. ; READ RAMDISK IMAGE FROM HD
  657.  
  658. ;!!!!!!!!!!!!!!!!!!!!!!!
  659. ;include 'boot/rdload.inc'
  660. ;!!!!!!!!!!!!!!!!!!!!!!!
  661. ;    mov    [dma_hdd],1
  662. ; CALCULATE FAT CHAIN FOR RAMDISK
  663.  
  664.         call  calculatefatchain
  665.  
  666.  
  667.  ; mov ax,[OS_BASE+0x10000+bx_from_load]
  668.  ; cmp ax,'r1'           ; if using not ram disk, then load librares and parameters {SPraid.simba}
  669.  ; je  no_lib_load
  670. ; LOADING LIBRARES
  671. ;   stdcall dll.Load,@IMPORT                 ; loading librares for kernel (.obj files)
  672. ;   call load_file_parse_table               ; prepare file parse table
  673. ;   call set_kernel_conf                     ; configure devices and gui
  674. ;no_lib_load:
  675.  
  676. ; LOAD FONTS I and II
  677.  
  678.         stdcall read_file, char, FONT_I, 0, 2304
  679.         stdcall read_file, char2, FONT_II, 0, 2560
  680.  
  681.         mov   esi,boot_fonts
  682.         call  boot_log
  683.  
  684. ; PRINT AMOUNT OF MEMORY
  685.         mov     esi, boot_memdetect
  686.         call    boot_log
  687.  
  688.         movzx   ecx, word [boot_y]
  689.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  690.         sub     ecx, 10
  691.         mov     edx, 0xFFFFFF
  692.         mov     ebx, [_mem_amount]
  693.         shr     ebx, 20
  694.         mov     edi, 1
  695.         mov     eax, 0x00040000
  696.         call    display_number_force
  697.  
  698. ; BUILD SCHEDULER
  699.  
  700.         call   build_scheduler ; sys32.inc
  701.  
  702.         mov    esi,boot_devices
  703.         call   boot_log
  704.  
  705.         mov  [pci_access_enabled],1
  706.  
  707.  
  708. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  709.  
  710.         mov   esi,boot_windefs
  711.         call  boot_log
  712.         call  setwindowdefaults
  713.  
  714. ; SET BACKGROUND DEFAULTS
  715.  
  716.         mov   esi,boot_bgr
  717.         call  boot_log
  718.         call  init_background
  719.         call  calculatebackground
  720.  
  721. ; RESERVE SYSTEM IRQ'S JA PORT'S
  722.  
  723.         mov   esi,boot_resirqports
  724.         call  boot_log
  725.         call  reserve_irqs_ports
  726.  
  727. ; SET PORTS FOR IRQ HANDLERS
  728.  
  729.         mov  esi,boot_setrports
  730.         call boot_log
  731.         ;call setirqreadports
  732.  
  733. ; SET UP OS TASK
  734.  
  735.         mov  esi,boot_setostask
  736.         call boot_log
  737.  
  738.         xor  eax, eax
  739.         mov  dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
  740.         mov  dword [SLOT_BASE+APPDATA.fpu_handler], eax
  741.         mov  dword [SLOT_BASE+APPDATA.sse_handler], eax
  742.  
  743.         ; name for OS/IDLE process
  744.  
  745.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  746.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  747.         mov edi,  __os_stack-8192+512
  748.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  749.         add edi, 0x2000-512
  750.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  751.         mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi ; just for case
  752.         mov dword [SLOT_BASE+256+APPDATA.io_map],\
  753.                   (tss._io_map_0-OS_BASE+PG_MAP)
  754.         mov dword [SLOT_BASE+256+APPDATA.io_map+4],\
  755.                   (tss._io_map_1-OS_BASE+PG_MAP)
  756.  
  757.         mov esi, fpu_data
  758.         mov ecx, 512/4
  759.         cld
  760.         rep movsd
  761.  
  762.         mov dword [SLOT_BASE+256+APPDATA.fpu_handler], eax
  763.         mov dword [SLOT_BASE+256+APPDATA.sse_handler], eax
  764.  
  765.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  766.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  767.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  768.  
  769.         mov  dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
  770.  
  771.         ; task list
  772.         mov  [CURRENT_TASK],dword 1
  773.         mov  [TASK_COUNT],dword 1
  774.         mov  [current_slot], SLOT_BASE+256
  775.         mov  [TASK_BASE],dword TASK_DATA
  776.         mov  [TASK_DATA+TASKDATA.wnd_number], 1 ; on screen number
  777.         mov  [TASK_DATA+TASKDATA.pid], 1        ; process id number
  778.         mov  [TASK_DATA+TASKDATA.mem_start], 0  ; process base address
  779.  
  780.         call init_cursors
  781.         mov eax, [def_cursor]
  782.         mov [SLOT_BASE+APPDATA.cursor],eax
  783.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  784.  
  785.        ; stdcall load_pe_driver, szAtiHW
  786.  
  787.   ; READ TSC / SECOND
  788.  
  789.         mov   esi,boot_tsc
  790.         call  boot_log
  791.  
  792.         call  _rdtsc
  793.         mov   ecx,eax
  794.         mov   esi,250               ; wait 1/4 a second
  795.         call  delay_ms
  796.         call  _rdtsc
  797.  
  798.         sub   eax,ecx
  799.         shl   eax,2
  800.         mov   [CPU_FREQ],eax          ; save tsc / sec
  801.         mov ebx, 1000000
  802.         div ebx
  803.         mov [stall_mcs], eax
  804.  
  805. ; SET VARIABLES
  806.  
  807.         call  set_variables
  808.  
  809. ; SET MOUSE
  810.  
  811.         stdcall load_driver, szPS2MDriver
  812.        ; stdcall load_driver, szCOM_MDriver
  813.  
  814.         mov   esi,boot_setmouse
  815.         call  boot_log
  816.         call  setmouse
  817.  
  818.  
  819. ; STACK AND FDC
  820.  
  821.         call  stack_init
  822.         call  fdc_init
  823.  
  824. ; PALETTE FOR 320x200 and 640x480 16 col
  825.  
  826.         cmp   [SCR_MODE],word 0x12
  827.         jne   no_pal_vga
  828.         mov   esi,boot_pal_vga
  829.         call  boot_log
  830.         call  paletteVGA
  831.       no_pal_vga:
  832.  
  833.         cmp   [SCR_MODE],word 0x13
  834.         jne   no_pal_ega
  835.         mov   esi,boot_pal_ega
  836.         call  boot_log
  837.         call  palette320x200
  838.       no_pal_ega:
  839.  
  840. ; LOAD DEFAULT SKIN
  841.  
  842.         call    load_default_skin
  843.  
  844. ;protect io permission map
  845.  
  846.            mov esi, [default_io_map]
  847.          ;  stdcall map_page,esi,(tss._io_map_0-OS_BASE), PG_MAP
  848.          ;  add esi, 0x1000
  849.          ;  stdcall map_page,esi,(tss._io_map_1-OS_BASE), PG_MAP
  850.  
  851.          ;  stdcall map_page,tss._io_map_0,\
  852.          ;          (tss._io_map_0-OS_BASE), PG_MAP
  853.          ;  stdcall map_page,tss._io_map_1,\
  854.          ;          (tss._io_map_1-OS_BASE), PG_MAP
  855.  
  856. ;  mov ax,[OS_BASE+0x10000+bx_from_load]
  857. ;  cmp ax,'r1'           ; if not rused ram disk - load network configuration from files {SPraid.simba}
  858. ;  je  no_st_network
  859. ;        call set_network_conf
  860. ;  no_st_network:
  861.  
  862. ; LOAD FIRST APPLICATION
  863.  
  864.  
  865.         mov     ebp, firstapp
  866.         call    fs_execute_from_sysdir
  867.  
  868.         cmp   eax,2                  ; continue if a process has been loaded
  869.         je    first_app_found
  870.  
  871.         mov     esi, boot_failed
  872.         call    boot_log
  873.  
  874.         mov   eax, 0xDEADBEEF        ; otherwise halt
  875.         hlt
  876.  
  877. first_app_found:
  878.  
  879.         cli
  880.  
  881.         ;mov   [TASK_COUNT],dword 2
  882.         mov   [CURRENT_TASK],dword 1       ; set OS task fisrt
  883.  
  884. ; SET KEYBOARD PARAMETERS
  885.         mov   al, 0xf6         ; reset keyboard, scan enabled
  886.         call  kb_write
  887.  
  888.         ; wait until 8042 is ready
  889.         xor ecx,ecx
  890. @@:
  891.         in     al,64h
  892.         and    al,00000010b
  893.         loopnz @b
  894.  
  895.         mov   al, 0xF3       ; set repeat rate & delay
  896.         call  kb_write
  897. ;        call  kb_read
  898.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  899.         call  kb_write
  900. ;        call  kb_read
  901.      ;// mike.dld [
  902.         call  set_lights
  903.      ;// mike.dld ]
  904.  
  905. ; START MULTITASKING
  906.  
  907. ;if preboot_blogesc
  908. ;        mov     esi, boot_tasking
  909. ;        call    boot_log
  910. ;.bll1:  in      al, 0x60        ; wait for ESC key press
  911. ;        cmp     al, 129
  912. ;        jne     .bll1
  913. ;end if
  914.  
  915. ;       mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  916.  
  917. ; UNMASK ALL IRQ'S
  918.  
  919.         mov   esi,boot_allirqs
  920.         call  boot_log
  921.  
  922.         mov   al,0                   ; unmask all irq's
  923.         out   0xA1,al
  924.         out   0x21,al
  925.  
  926.         mov   ecx,32
  927.  
  928.      ready_for_irqs:
  929.  
  930.         mov   al,0x20                ; ready for irqs
  931.         out   0x20,al
  932.         out   0xa0,al
  933.  
  934.         loop  ready_for_irqs         ; flush the queue
  935.  
  936.         stdcall attach_int_handler, dword 1, irq1, dword 0
  937.  
  938. ;        mov    [dma_hdd],1
  939.         cmp     [IDEContrRegsBaseAddr], 0
  940.         setnz   [dma_hdd]
  941.         mov [timer_ticks_enable],1              ; for cd driver
  942.  
  943.         sti
  944.         call change_task
  945.  
  946.         jmp osloop
  947.  
  948. ;        jmp   $                      ; wait here for timer to take control
  949.  
  950.         ; Fly :)
  951.  
  952. include 'unpacker.inc'
  953. include 'fdo.inc'
  954.  
  955. align 4
  956. boot_log:
  957.          pushad
  958.  
  959.          mov   ebx,10*65536
  960.          mov   bx,word [boot_y]
  961.          add   [boot_y],dword 10
  962.          mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  963.          mov   edx,esi
  964.          mov   edi,1
  965.  
  966.          call  dtext
  967.  
  968.          mov   [novesachecksum],1000
  969.          call  checkVga_N13
  970.  
  971.          popad
  972.  
  973.          ret
  974.  
  975.  
  976. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  977. ;                                                                    ;
  978. ;                    MAIN OS LOOP START                              ;
  979. ;                                                                    ;
  980. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  981. align 32
  982. osloop:
  983.         call   [draw_pointer]
  984.         call   checkbuttons
  985.         call   checkwindows
  986. ;       call   check_window_move_request
  987.         call   checkmisc
  988.         call   checkVga_N13
  989.         call   stack_handler
  990.         call   checkidle
  991.         call   check_fdd_motor_status
  992.         call   check_ATAPI_device_event
  993.         jmp    osloop
  994. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  995. ;                                                                    ;
  996. ;                      MAIN OS LOOP END                              ;
  997. ;                                                                    ;
  998. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  999.  
  1000. checkidle:
  1001.         pushad
  1002.  
  1003.         cmp  [check_idle_semaphore],0
  1004.         jne  no_idle_state
  1005.  
  1006.         call change_task
  1007.         mov  eax,[idlemem]
  1008.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1009.         cmp  eax,ebx
  1010.         jnz  idle_exit
  1011.         call _rdtsc
  1012.         mov  ecx,eax
  1013.       idle_loop:
  1014.         hlt
  1015.         cmp  [check_idle_semaphore],0
  1016.         jne  idle_loop_exit
  1017.         mov  eax,[timer_ticks] ;[0xfdf0]
  1018.         cmp  ebx,eax
  1019.         jz   idle_loop
  1020.       idle_loop_exit:
  1021.         mov  [idlemem],eax
  1022.         call _rdtsc
  1023.         sub  eax,ecx
  1024.         mov  ebx,[idleuse]
  1025.         add  ebx,eax
  1026.         mov  [idleuse],ebx
  1027.  
  1028.         popad
  1029.         ret
  1030.  
  1031.       idle_exit:
  1032.  
  1033.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1034.         mov  [idlemem],ebx
  1035.         call change_task
  1036.  
  1037.         popad
  1038.         ret
  1039.  
  1040.       no_idle_state:
  1041.  
  1042.         dec  [check_idle_semaphore]
  1043.  
  1044.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1045.         mov  [idlemem],ebx
  1046.         call change_task
  1047.  
  1048.         popad
  1049.         ret
  1050.  
  1051. uglobal
  1052.   idlemem               dd   0x0
  1053.   idleuse               dd   0x0
  1054.   idleusesec            dd   0x0
  1055.   check_idle_semaphore  dd   0x0
  1056. endg
  1057.  
  1058.  
  1059.  
  1060. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1061. ;                                                                      ;
  1062. ;                   INCLUDED SYSTEM FILES                              ;
  1063. ;                                                                      ;
  1064. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1065.  
  1066.  
  1067. include "kernel32.inc"
  1068.  
  1069.  
  1070. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1071. ;                                                                      ;
  1072. ;                       KERNEL FUNCTIONS                               ;
  1073. ;                                                                      ;
  1074. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1075.  
  1076. reserve_irqs_ports:
  1077.  
  1078.         pushad
  1079.  
  1080.         mov  [irq_owner+4*0], 1    ; timer
  1081.         ;mov  [irq_owner+4*1], 1    ; keyboard
  1082.         mov  [irq_owner+4*6], 1    ; floppy diskette
  1083.         mov  [irq_owner+4*13], 1   ; math co-pros
  1084.         mov  [irq_owner+4*14], 1   ; ide I
  1085.         mov  [irq_owner+4*15], 1   ; ide II
  1086.  
  1087.         ; RESERVE PORTS
  1088.         mov   edi,1                    ; 0x00-0x2d
  1089.         mov   [RESERVED_PORTS],edi
  1090.         shl   edi,4
  1091.         mov   [RESERVED_PORTS+edi+0],dword 1
  1092.         mov   [RESERVED_PORTS+edi+4],dword 0x0
  1093.         mov   [RESERVED_PORTS+edi+8],dword 0x2d
  1094.  
  1095.         inc   dword [RESERVED_PORTS]          ; 0x30-0x4d
  1096.         mov   edi,[RESERVED_PORTS]
  1097.         shl   edi,4
  1098.         mov   [RESERVED_PORTS+edi+0],dword 1
  1099.         mov   [RESERVED_PORTS+edi+4],dword 0x30
  1100.         mov   [RESERVED_PORTS+edi+8],dword 0x4d
  1101.  
  1102.         inc   dword [RESERVED_PORTS]          ; 0x50-0xdf
  1103.         mov   edi,[RESERVED_PORTS]
  1104.         shl   edi,4
  1105.         mov   [RESERVED_PORTS+edi+0],dword 1
  1106.         mov   [RESERVED_PORTS+edi+4],dword 0x50
  1107.         mov   [RESERVED_PORTS+edi+8],dword 0xdf
  1108.  
  1109.         inc   dword [RESERVED_PORTS]          ; 0xe5-0xff
  1110.         mov   edi,[RESERVED_PORTS]
  1111.         shl   edi,4
  1112.         mov   [RESERVED_PORTS+edi+0],dword 1
  1113.         mov   [RESERVED_PORTS+edi+4],dword 0xe5
  1114.         mov   [RESERVED_PORTS+edi+8],dword 0xff
  1115.  
  1116.         popad
  1117.         ret
  1118.  
  1119. setirqreadports:
  1120.  
  1121.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1122.         mov   [irq12read+4],dword 0                  ; end of port list
  1123.         ;mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1124.         ;mov   [irq04read+4],dword 0                  ; end of port list
  1125.         ;mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1126.         ;mov   [irq03read+4],dword 0                  ; end of port list
  1127.  
  1128.         ret
  1129.  
  1130. iglobal
  1131.   process_number dd 0x1
  1132. endg
  1133.  
  1134. set_variables:
  1135.  
  1136.         mov   ecx,0x100                          ; flush port 0x60
  1137. .fl60:  in    al,0x60
  1138.         loop  .fl60
  1139.         mov   [MOUSE_BUFF_COUNT],byte 0          ; mouse buffer
  1140.         mov   [KEY_COUNT],byte 0                 ; keyboard buffer
  1141.         mov   [BTN_COUNT],byte 0                 ; button buffer
  1142. ;        mov   [MOUSE_X],dword 100*65536+100     ; mouse x/y
  1143.  
  1144.         push  eax
  1145.         mov   ax,[BOOT_VAR+0x900c]
  1146.         shr   ax,1
  1147.         shl   eax,16
  1148.         mov   ax,[BOOT_VAR+0x900A]
  1149.         shr   ax,1
  1150.         mov   [MOUSE_X],eax
  1151.         pop   eax
  1152.  
  1153.         mov   [BTN_ADDR],dword BUTTON_INFO       ; address of button list
  1154.  
  1155.      ;!! IP 04.02.2005:
  1156.         mov   [next_usage_update], 100
  1157.         mov   byte [DONT_SWITCH], 0              ; change task if possible
  1158.  
  1159.         ret
  1160.  
  1161. ;* mouse centered - start code- Mario79
  1162. mouse_centered:
  1163.         push  eax
  1164.         mov   eax,[Screen_Max_X]
  1165.         shr   eax,1
  1166.         mov   [MOUSE_X],ax
  1167.         mov   eax,[Screen_Max_Y]
  1168.         shr   eax,1
  1169.         mov   [MOUSE_Y],ax
  1170.         pop   eax
  1171.         ret
  1172. ;* mouse centered - end code- Mario79
  1173.  
  1174. align 4
  1175.  
  1176. sys_outport:
  1177.  
  1178.     mov   edi,ebx          ; separate flag for read / write
  1179.     and   ebx,65535
  1180.  
  1181.     mov   ecx,[RESERVED_PORTS]
  1182.     test  ecx,ecx
  1183.     jne   sopl8
  1184.     mov   [esp+36],dword 1
  1185.     ret
  1186.  
  1187.   sopl8:
  1188.     mov   edx,[TASK_BASE]
  1189.     mov   edx,[edx+0x4]
  1190.     and   ebx,65535
  1191.     cld
  1192.   sopl1:
  1193.  
  1194.     mov   esi,ecx
  1195.     shl   esi,4
  1196.     add   esi,RESERVED_PORTS
  1197.     cmp   edx,[esi+0]
  1198.     jne   sopl2
  1199.     cmp   ebx,[esi+4]
  1200.     jb    sopl2
  1201.     cmp   ebx,[esi+8]
  1202.     jg    sopl2
  1203.     jmp   sopl3
  1204.  
  1205.   sopl2:
  1206.  
  1207.     dec   ecx
  1208.     jnz   sopl1
  1209.     mov   [esp+36],dword 1
  1210.     ret
  1211.  
  1212.   sopl3:
  1213.  
  1214.     test  edi,0x80000000 ; read ?
  1215.     jnz   sopl4
  1216.  
  1217.     mov   dx,bx          ; write
  1218.     out   dx,al
  1219.     mov   [esp+36],dword 0
  1220.     ret
  1221.  
  1222.   sopl4:
  1223.  
  1224.     mov   dx,bx          ; read
  1225.     in    al,dx
  1226.     and   eax,0xff
  1227.     mov   [esp+36],dword 0
  1228.     mov   [esp+24],eax
  1229.     ret
  1230.  
  1231. display_number:
  1232.  
  1233. ; eax = print type, al=0 -> ebx is number
  1234. ;                   al=1 -> ebx is pointer
  1235. ;                   ah=0 -> display decimal
  1236. ;                   ah=1 -> display hexadecimal
  1237. ;                   ah=2 -> display binary
  1238. ;                   eax bits 16-21 = number of digits to display (0-32)
  1239. ;                   eax bits 22-31 = reserved
  1240. ;
  1241. ; ebx = number or pointer
  1242. ; ecx = x shl 16 + y
  1243. ; edx = color
  1244.         xor     edi, edi
  1245. display_number_force:
  1246.      push  eax
  1247.      and   eax,0x3fffffff
  1248.      cmp   eax,0xffff            ; length > 0 ?
  1249.      pop   eax
  1250.      jge   cont_displ
  1251.      ret
  1252.    cont_displ:
  1253.      push  eax
  1254.      and   eax,0x3fffffff
  1255.      cmp   eax,61*0x10000        ; length <= 60 ?
  1256.      pop   eax
  1257.      jb    cont_displ2
  1258.      ret
  1259.    cont_displ2:
  1260.  
  1261.      pushad
  1262.  
  1263.      cmp   al,1                  ; ecx is a pointer ?
  1264.      jne   displnl1
  1265.      mov   ebp,ebx
  1266.      add   ebp,4
  1267.      mov   ebp,[ebp]
  1268.      mov   ebx,[ebx]
  1269.    displnl1:
  1270.      sub   esp,64
  1271.  
  1272.      cmp   ah,0                  ; DECIMAL
  1273.      jne   no_display_desnum
  1274.      shr   eax,16
  1275.      and   eax,0xC03f
  1276. ;     and   eax,0x3f
  1277.      push  eax
  1278.      and   eax,0x3f
  1279.      mov   edi,esp
  1280.      add   edi,4+64-1
  1281.      mov   ecx,eax
  1282.      mov   eax,ebx
  1283.      mov   ebx,10
  1284.    d_desnum:
  1285.      xor   edx,edx
  1286.      call  division_64_bits
  1287.      div   ebx
  1288.      add   dl,48
  1289.      mov   [edi],dl
  1290.      dec   edi
  1291.      loop  d_desnum
  1292.      pop   eax
  1293.      call  normalize_number
  1294.      call  draw_num_text
  1295.      add   esp,64
  1296.      popad
  1297.      ret
  1298.    no_display_desnum:
  1299.  
  1300.      cmp   ah,0x01               ; HEXADECIMAL
  1301.      jne   no_display_hexnum
  1302.      shr   eax,16
  1303.      and   eax,0xC03f
  1304. ;     and   eax,0x3f
  1305.      push  eax
  1306.      and   eax,0x3f
  1307.      mov   edi,esp
  1308.      add   edi,4+64-1
  1309.      mov   ecx,eax
  1310.      mov   eax,ebx
  1311.      mov   ebx,16
  1312.    d_hexnum:
  1313.      xor   edx,edx
  1314.      call  division_64_bits
  1315.      div   ebx
  1316.      add   edx,hexletters
  1317.      mov   dl,[edx]
  1318.      mov   [edi],dl
  1319.      dec   edi
  1320.      loop  d_hexnum
  1321.      pop   eax
  1322.      call  normalize_number
  1323.      call  draw_num_text
  1324.      add   esp,64
  1325.      popad
  1326.      ret
  1327.    no_display_hexnum:
  1328.  
  1329.      cmp   ah,0x02               ; BINARY
  1330.      jne   no_display_binnum
  1331.      shr   eax,16
  1332.      and   eax,0xC03f
  1333. ;     and   eax,0x3f
  1334.      push  eax
  1335.      and   eax,0x3f
  1336.      mov   edi,esp
  1337.      add   edi,4+64-1
  1338.      mov   ecx,eax
  1339.      mov   eax,ebx
  1340.      mov   ebx,2
  1341.    d_binnum:
  1342.      xor   edx,edx
  1343.      call  division_64_bits
  1344.      div   ebx
  1345.      add   dl,48
  1346.      mov   [edi],dl
  1347.      dec   edi
  1348.      loop  d_binnum
  1349.      pop   eax
  1350.      call  normalize_number
  1351.      call  draw_num_text
  1352.      add   esp,64
  1353.      popad
  1354.      ret
  1355.    no_display_binnum:
  1356.  
  1357.      add   esp,64
  1358.      popad
  1359.      ret
  1360.  
  1361. normalize_number:
  1362.      test  ah,0x80
  1363.      jz   .continue
  1364.      mov  ecx,48
  1365.      and   eax,0x3f
  1366. @@:
  1367.      inc   edi
  1368.      cmp   [edi],cl
  1369.      jne   .continue
  1370.      dec   eax
  1371.      cmp   eax,1
  1372.      ja    @r
  1373.      mov   al,1
  1374. .continue:
  1375.      and   eax,0x3f
  1376.      ret
  1377.  
  1378. division_64_bits:
  1379.      test  [esp+1+4],byte 0x40
  1380.      jz   .continue
  1381.      push  eax
  1382.      mov   eax,ebp
  1383.      div   ebx
  1384.      mov   ebp,eax
  1385.      pop   eax
  1386. .continue:
  1387.      ret
  1388.  
  1389. draw_num_text:
  1390.      mov   esi,eax
  1391.      mov   edx,64+4
  1392.      sub   edx,eax
  1393.      add   edx,esp
  1394.      mov   ebx,[esp+64+32-8+4]
  1395. ; add window start x & y
  1396.      mov   ecx,[TASK_BASE]
  1397.  
  1398.      mov   edi,[CURRENT_TASK]
  1399.      shl   edi,8
  1400.  
  1401.      mov   eax,[ecx-twdw+WDATA.box.left]
  1402.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1403.      shl   eax,16
  1404.      add   eax,[ecx-twdw+WDATA.box.top]
  1405.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1406.      add   ebx,eax
  1407.      mov   ecx,[esp+64+32-12+4]
  1408.         and     ecx, not 0x80000000     ; force counted string
  1409.         mov     eax, [esp+64+8]         ; background color (if given)
  1410.         mov     edi, [esp+64+4]
  1411.      jmp   dtext
  1412.  
  1413. align 4
  1414.  
  1415. sys_setup:
  1416.  
  1417. ; 1=roland mpu midi base , base io address
  1418. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1419. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1420. ; 5=system language, 1eng 2fi 3ger 4rus
  1421. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1422. ; 8=fat32 partition in hd
  1423. ; 9
  1424. ; 10 = sound dma channel
  1425. ; 11 = enable lba read
  1426. ; 12 = enable pci access
  1427.  
  1428.  
  1429.      mov  [esp+36],dword 0
  1430.      cmp  eax,1                      ; MIDI
  1431.      jnz  nsyse1
  1432.      cmp  ebx,0x100
  1433.      jb   nsyse1
  1434.      mov  edx,65535
  1435.      cmp  edx,ebx
  1436.      jb   nsyse1
  1437.      mov  [midi_base],bx
  1438.      mov  word [mididp],bx
  1439.      inc  bx
  1440.      mov  word [midisp],bx
  1441.      ret
  1442.  
  1443. iglobal
  1444. midi_base dw 0
  1445. endg
  1446.  
  1447.    nsyse1:
  1448.  
  1449.      cmp  eax,2                      ; KEYBOARD
  1450.      jnz  nsyse2
  1451.      cmp  ebx,1
  1452.      jnz  kbnobase
  1453.      mov  edi,[TASK_BASE]
  1454.      add  ecx,[edi+TASKDATA.mem_start]
  1455.      mov  eax,ecx
  1456.      mov  ebx,keymap
  1457.      mov  ecx,128
  1458.      call memmove
  1459.      ret
  1460.    kbnobase:
  1461.      cmp  ebx,2
  1462.      jnz  kbnoshift
  1463.      mov  edi,[TASK_BASE]
  1464.      add  ecx,[edi+TASKDATA.mem_start]
  1465.      mov  eax,ecx
  1466.      mov  ebx,keymap_shift
  1467.      mov  ecx,128
  1468.      call memmove
  1469.      ret
  1470.    kbnoshift:
  1471.      cmp  ebx,3
  1472.      jne  kbnoalt
  1473.      mov  edi,[TASK_BASE]
  1474.      add  ecx,[edi+TASKDATA.mem_start]
  1475.      mov  eax,ecx
  1476.      mov  ebx,keymap_alt
  1477.      mov  ecx,128
  1478.      call memmove
  1479.      ret
  1480.    kbnoalt:
  1481.      cmp  ebx,9
  1482.      jnz  kbnocountry
  1483.      mov  word [keyboard],cx
  1484.      ret
  1485.    kbnocountry:
  1486.      mov  [esp+36],dword 1
  1487.      ret
  1488.    nsyse2:
  1489.      cmp  eax,3                      ; CD
  1490.      jnz  nsyse4
  1491.      test ebx,ebx
  1492.      jz   nosesl
  1493.      cmp  ebx, 4
  1494.      ja   nosesl
  1495.      mov  [cd_base],bl
  1496.      cmp  ebx,1
  1497.      jnz  noprma
  1498.      mov  [cdbase],0x1f0
  1499.      mov  [cdid],0xa0
  1500.    noprma:
  1501.      cmp  ebx,2
  1502.      jnz  noprsl
  1503.      mov  [cdbase],0x1f0
  1504.      mov  [cdid],0xb0
  1505.    noprsl:
  1506.      cmp  ebx,3
  1507.      jnz  nosema
  1508.      mov  [cdbase],0x170
  1509.      mov  [cdid],0xa0
  1510.    nosema:
  1511.      cmp  ebx,4
  1512.      jnz  nosesl
  1513.      mov  [cdbase],0x170
  1514.      mov  [cdid],0xb0
  1515.    nosesl:
  1516.      ret
  1517.  
  1518. cd_base db 0
  1519.  
  1520.    nsyse4:
  1521.  
  1522.      cmp  eax,5                      ; SYSTEM LANGUAGE
  1523.      jnz  nsyse5
  1524.      mov  [syslang],ebx
  1525.      ret
  1526.    nsyse5:
  1527.  
  1528.      cmp  eax,7                      ; HD BASE
  1529.      jne  nsyse7
  1530.      test ebx,ebx
  1531.      jz   nosethd
  1532.      cmp  ebx,4
  1533.      ja   nosethd
  1534.      mov  [hd_base],bl
  1535.      cmp  ebx,1
  1536.      jnz  noprmahd
  1537.      mov  [hdbase],0x1f0
  1538.      mov  [hdid],0x0
  1539.      mov  [hdpos],1
  1540. ;     call set_FAT32_variables
  1541.    noprmahd:
  1542.      cmp  ebx,2
  1543.      jnz  noprslhd
  1544.      mov  [hdbase],0x1f0
  1545.      mov  [hdid],0x10
  1546.      mov  [hdpos],2
  1547. ;     call set_FAT32_variables
  1548.    noprslhd:
  1549.      cmp  ebx,3
  1550.      jnz  nosemahd
  1551.      mov  [hdbase],0x170
  1552.      mov  [hdid],0x0
  1553.      mov  [hdpos],3
  1554. ;     call set_FAT32_variables
  1555.    nosemahd:
  1556.      cmp  ebx,4
  1557.      jnz  noseslhd
  1558.      mov  [hdbase],0x170
  1559.      mov  [hdid],0x10
  1560.      mov  [hdpos],4
  1561. ;     call set_FAT32_variables
  1562.    noseslhd:
  1563.     call  reserve_hd1
  1564.     call  reserve_hd_channel
  1565.     call  free_hd_channel
  1566.     mov   [hd1_status],0        ; free
  1567.    nosethd:
  1568.      ret
  1569.  
  1570. iglobal
  1571. hd_base db 0
  1572. endg
  1573.  
  1574. nsyse7:
  1575.  
  1576.      cmp  eax,8                      ; HD PARTITION
  1577.      jne  nsyse8
  1578.      mov  [fat32part],ebx
  1579. ;     call set_FAT32_variables
  1580.     call  reserve_hd1
  1581.     call  reserve_hd_channel
  1582.     call  free_hd_channel
  1583.      pusha
  1584.      call  choice_necessity_partition_1
  1585.      popa
  1586.     mov   [hd1_status],0        ; free
  1587.      ret
  1588.  
  1589. nsyse8:
  1590.      cmp  eax,11                     ; ENABLE LBA READ
  1591.      jne  no_set_lba_read
  1592.      and  ebx,1
  1593.      mov  [lba_read_enabled],ebx
  1594.      ret
  1595.  
  1596. no_set_lba_read:
  1597.      cmp  eax,12                     ; ENABLE PCI ACCESS
  1598.      jne  no_set_pci_access
  1599.      and  ebx,1
  1600.      mov  [pci_access_enabled],ebx
  1601.      ret
  1602.    no_set_pci_access:
  1603.  
  1604. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1605. include 'vmodeint.inc'
  1606. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1607.  
  1608. sys_setup_err:
  1609.      mov  [esp+36],dword -1
  1610.      ret
  1611.  
  1612. align 4
  1613.  
  1614. sys_getsetup:
  1615.  
  1616. ; 1=roland mpu midi base , base io address
  1617. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1618. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1619. ; 5=system language, 1eng 2fi 3ger 4rus
  1620. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1621. ; 8=fat32 partition in hd
  1622. ; 9=get hs timer tic
  1623.  
  1624.      cmp  eax,1
  1625.      jne  ngsyse1
  1626.      movzx eax,[midi_base]
  1627.      mov  [esp+36],eax
  1628.      ret
  1629. ngsyse1:
  1630.  
  1631.      cmp  eax,2
  1632.      jne  ngsyse2
  1633.      cmp  ebx,1
  1634.      jnz  kbnobaseret
  1635.      mov  edi,[TASK_BASE]
  1636.      add  ecx,[edi+TASKDATA.mem_start]
  1637.      mov  ebx,ecx
  1638.      mov  eax,keymap
  1639.      mov  ecx,128
  1640.      call memmove
  1641.      ret
  1642. kbnobaseret:
  1643.      cmp  ebx,2
  1644.      jnz  kbnoshiftret
  1645.      mov  edi,[TASK_BASE]
  1646.      add  ecx,[edi+TASKDATA.mem_start]
  1647.      mov  ebx,ecx
  1648.      mov  eax,keymap_shift
  1649.      mov  ecx,128
  1650.      call memmove
  1651.      ret
  1652. kbnoshiftret:
  1653.      cmp  ebx,3
  1654.      jne  kbnoaltret
  1655.      mov  edi,[TASK_BASE]
  1656.      add  ecx,[edi+TASKDATA.mem_start]
  1657.      mov  ebx,ecx
  1658.      mov  eax,keymap_alt
  1659.      mov  ecx,128
  1660.      call memmove
  1661.      ret
  1662. kbnoaltret:
  1663.      cmp  ebx,9
  1664.      jnz  ngsyse2
  1665.      movzx eax,word [keyboard]
  1666.      mov  [esp+36],eax
  1667.      ret
  1668. ngsyse2:
  1669.  
  1670.          cmp  eax,3
  1671.          jnz  ngsyse3
  1672.          movzx eax,[cd_base]
  1673.          mov  [esp+36],eax
  1674.          ret
  1675. ngsyse3:
  1676.          cmp  eax,5
  1677.          jnz  ngsyse5
  1678.          mov  eax,[syslang]
  1679.          mov  [esp+36],eax
  1680.          ret
  1681. ngsyse5:
  1682.      cmp  eax,7
  1683.      jnz  ngsyse7
  1684.      movzx eax,[hd_base]
  1685.      mov  [esp+36],eax
  1686.      ret
  1687. ngsyse7:
  1688.      cmp  eax,8
  1689.      jnz  ngsyse8
  1690.      mov eax,[fat32part]
  1691.      mov  [esp+36],eax
  1692.      ret
  1693. ngsyse8:
  1694.      cmp  eax,9
  1695.      jne  ngsyse9
  1696.      mov  eax,[timer_ticks] ;[0xfdf0]
  1697.      mov  [esp+36],eax
  1698.      ret
  1699. ngsyse9:
  1700.      cmp  eax,11
  1701.      jnz  ngsyse11
  1702.      mov eax,[lba_read_enabled]
  1703.      mov  [esp+36],eax
  1704.      ret
  1705. ngsyse11:
  1706.      cmp  eax,12
  1707.      jnz  ngsyse12
  1708.      mov eax,[pci_access_enabled]
  1709.      mov  [esp+36],eax
  1710.      ret
  1711. ngsyse12:
  1712.      mov  [esp+36],dword 1
  1713.      ret
  1714.  
  1715. get_timer_ticks:
  1716.     mov eax,[timer_ticks]
  1717.     ret
  1718.  
  1719. iglobal
  1720. align 4
  1721. mousefn dd msscreen, mswin, msbutton, msset
  1722.         dd app_load_cursor
  1723.         dd app_set_cursor
  1724.         dd app_delete_cursor
  1725.         dd msz
  1726. endg
  1727.  
  1728. readmousepos:
  1729.  
  1730. ; eax=0 screen relative
  1731. ; eax=1 window relative
  1732. ; eax=2 buttons pressed
  1733. ; eax=3 set mouse pos   ; reserved
  1734. ; eax=4 load cursor
  1735. ; eax=5 set cursor
  1736. ; eax=6 delete cursor   ; reserved
  1737. ; eax=7 get mouse_z
  1738.  
  1739.            cmp eax, 7
  1740.            ja msset
  1741.            jmp [mousefn+eax*4]
  1742. msscreen:
  1743.            mov  eax,[MOUSE_X]
  1744.            shl  eax,16
  1745.            mov  ax,[MOUSE_Y]
  1746.            mov  [esp+36],eax
  1747.            ret
  1748. mswin:
  1749.            mov  eax,[MOUSE_X]
  1750.            shl  eax,16
  1751.            mov  ax,[MOUSE_Y]
  1752.            mov  esi,[TASK_BASE]
  1753.            mov  bx, word [esi-twdw+WDATA.box.left]
  1754.            shl  ebx,16
  1755.            mov  bx, word [esi-twdw+WDATA.box.top]
  1756.            sub  eax,ebx
  1757.  
  1758.            mov  edi,[CURRENT_TASK]
  1759.            shl  edi,8
  1760.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1761.            rol  eax,16
  1762.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1763.            rol  eax,16
  1764.            mov  [esp+36],eax
  1765.            ret
  1766. msbutton:
  1767.            movzx eax,byte [BTN_DOWN]
  1768.            mov  [esp+36],eax
  1769.            ret
  1770. msz:
  1771.            mov   edi, [TASK_COUNT]
  1772.            movzx edi, word [WIN_POS + edi*2]
  1773.            cmp   edi, [CURRENT_TASK]
  1774.            jne   @f
  1775.            mov   ax,[MOUSE_SCROLL_H]
  1776.            shl   eax,16
  1777.            mov   ax,[MOUSE_SCROLL_V]
  1778.            mov   [esp+36],eax
  1779.            mov   [MOUSE_SCROLL_H],word 0
  1780.            mov   [MOUSE_SCROLL_V],word 0
  1781.            ret
  1782.        @@:
  1783.            mov  [esp+36],dword 0
  1784.            ret
  1785. msset:
  1786.            ret
  1787.  
  1788. app_load_cursor:
  1789.       ;     add ebx, new_app_base
  1790.            cmp ebx, OS_BASE
  1791.            jae msset
  1792.            stdcall load_cursor, ebx, ecx
  1793.            mov [esp+36], eax
  1794.            ret
  1795.  
  1796. app_set_cursor:
  1797.            stdcall set_cursor, ebx
  1798.            mov [esp+36], eax
  1799.            ret
  1800.  
  1801. app_delete_cursor:
  1802.            stdcall delete_cursor, ebx
  1803.            mov [esp+36], eax
  1804.            ret
  1805.  
  1806. is_input:
  1807.  
  1808.    push edx
  1809.    mov  dx,word [midisp]
  1810.    in   al,dx
  1811.    and  al,0x80
  1812.    pop  edx
  1813.    ret
  1814.  
  1815. is_output:
  1816.  
  1817.    push edx
  1818.    mov  dx,word [midisp]
  1819.    in   al,dx
  1820.    and  al,0x40
  1821.    pop  edx
  1822.    ret
  1823.  
  1824.  
  1825. get_mpu_in:
  1826.  
  1827.    push edx
  1828.    mov  dx,word [mididp]
  1829.    in   al,dx
  1830.    pop  edx
  1831.    ret
  1832.  
  1833.  
  1834. put_mpu_out:
  1835.  
  1836.    push edx
  1837.    mov  dx,word [mididp]
  1838.    out  dx,al
  1839.    pop  edx
  1840.    ret
  1841.  
  1842.  
  1843. setuart:
  1844.  
  1845.  su1:
  1846.    call is_output
  1847.    cmp  al,0
  1848.    jnz  su1
  1849.    mov  dx,word [midisp]
  1850.    mov  al,0xff
  1851.    out  dx,al
  1852.  su2:
  1853.    mov  dx,word [midisp]
  1854.    mov  al,0xff
  1855.    out  dx,al
  1856.    call is_input
  1857.    cmp  al,0
  1858.    jnz  su2
  1859.    call get_mpu_in
  1860.    cmp  al,0xfe
  1861.    jnz  su2
  1862.  su3:
  1863.    call is_output
  1864.    cmp  al,0
  1865.    jnz  su3
  1866.    mov  dx,word [midisp]
  1867.    mov  al,0x3f
  1868.    out  dx,al
  1869.  
  1870.    ret
  1871.  
  1872.  
  1873. align 4
  1874.  
  1875. sys_midi:
  1876.  
  1877.      cmp  [mididp],0
  1878.      jnz  sm0
  1879.      mov  [esp+36],dword 1
  1880.      ret
  1881.    sm0:
  1882.  
  1883.      cmp  eax,1
  1884.      mov  [esp+36],dword 0
  1885.      jnz  smn1
  1886.      call setuart
  1887.      ret
  1888.    smn1:
  1889.  
  1890.      cmp  eax,2
  1891.      jnz  smn2
  1892.    sm10:
  1893.      call get_mpu_in
  1894.      call is_output
  1895.      test al,al
  1896.      jnz  sm10
  1897.      mov  al,bl
  1898.      call put_mpu_out
  1899.      ret
  1900.    smn2:
  1901.  
  1902.      ret
  1903.  
  1904. sys_end:
  1905.  
  1906.      mov   eax,[TASK_BASE]
  1907.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1908.  
  1909.     waitterm:            ; wait here for termination
  1910.      mov   ebx,100
  1911.      call  delay_hs
  1912.      jmp   waitterm
  1913.  
  1914. iglobal
  1915. align 4
  1916. sys_system_table:
  1917.         dd      exit_for_anyone         ; 1 = obsolete
  1918.         dd      sysfn_terminate         ; 2 = terminate thread
  1919.         dd      sysfn_activate          ; 3 = activate window
  1920.         dd      sysfn_getidletime       ; 4 = get idle time
  1921.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1922.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1923.         dd      sysfn_getactive         ; 7 = get active window
  1924.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1925.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1926.         dd      sysfn_minimize          ; 10 = minimize window
  1927.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1928.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1929.         dd      sysfn_getversion        ; 13 = get kernel version
  1930.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1931.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1932.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1933.         dd      sysfn_getallmem         ; 17 = get total memory size
  1934.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1935.                                         ;                 instead of slot
  1936.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1937.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1938.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1939.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1940. sysfn_num = ($ - sys_system_table)/4
  1941. endg
  1942.  
  1943. sys_system:
  1944.         dec     ebx
  1945.         cmp     ebx, sysfn_num
  1946.         jae     @f
  1947.         jmp     dword [sys_system_table + ebx*4]
  1948. @@:
  1949.         ret
  1950.  
  1951.  
  1952. sysfn_shutdown:          ; 18.9 = system shutdown
  1953.      cmp  ecx,1
  1954.      jl   exit_for_anyone
  1955.      cmp  ecx,4
  1956.      jg   exit_for_anyone
  1957.      mov  [BOOT_VAR+0x9030],cl
  1958.  
  1959.      mov  eax,[TASK_COUNT]
  1960.      mov  [SYS_SHUTDOWN],al
  1961.      mov  [shutdown_processes],eax
  1962.      and  dword [esp+32], 0
  1963.  exit_for_anyone:
  1964.      ret
  1965.   uglobal
  1966.    shutdown_processes: dd 0x0
  1967.   endg
  1968.  
  1969. sysfn_terminate:        ; 18.2 = TERMINATE
  1970.      cmp  ecx,2
  1971.      jb   noprocessterminate
  1972.      mov  edx,[TASK_COUNT]
  1973.      cmp  ecx,edx
  1974.      ja   noprocessterminate
  1975.      mov  eax,[TASK_COUNT]
  1976.      shl  ecx,5
  1977.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  1978.      add  ecx,CURRENT_TASK+TASKDATA.state
  1979.      cmp  byte [ecx], 9
  1980.      jz   noprocessterminate
  1981.  
  1982.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  1983.      mov  [ecx],byte 3       ; clear possible i40's
  1984.      ;call MEM_Heap_UnLock
  1985.  
  1986.      cmp  edx,[application_table_status]    ; clear app table stat
  1987.      jne  noatsc
  1988.      mov  [application_table_status],0
  1989.    noatsc:
  1990.    noprocessterminate:
  1991.      ret
  1992.  
  1993. sysfn_terminate2:
  1994. ;lock application_table_status mutex
  1995. .table_status:
  1996.     cli
  1997.     cmp    [application_table_status],0
  1998.     je     .stf
  1999.     sti
  2000.     call   change_task
  2001.     jmp    .table_status
  2002. .stf:
  2003.     call   set_application_table_status
  2004.     mov    eax,ecx
  2005.     call   pid_to_slot
  2006.     test   eax,eax
  2007.     jz     .not_found
  2008.     mov    ecx,eax
  2009.     pushfd
  2010.     cli
  2011.     call   sysfn_terminate
  2012.     mov    [application_table_status],0
  2013.     popfd
  2014.     and    dword [esp+32],0
  2015.     ret
  2016. .not_found:
  2017.     mov    [application_table_status],0
  2018.     or     dword [esp+32],-1
  2019.     ret
  2020.  
  2021. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2022.      cmp  ecx,2
  2023.      jb   .nowindowactivate
  2024.      cmp  ecx,[TASK_COUNT]
  2025.      ja   .nowindowactivate
  2026.  
  2027.      mov   [window_minimize], 2   ; restore window if minimized
  2028.  
  2029.      movzx esi, word [WIN_STACK + ecx*2]
  2030.      cmp   esi, [TASK_COUNT]
  2031.      je    .nowindowactivate ; already active
  2032.  
  2033.      mov   edi, ecx
  2034.      shl   edi, 5
  2035.      add   edi, window_data
  2036.      movzx esi, word [WIN_STACK + ecx * 2]
  2037.      lea   esi, [WIN_POS + esi * 2]
  2038.      call  waredraw
  2039. .nowindowactivate:
  2040.      ret
  2041.  
  2042. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2043.      mov  eax,[idleusesec]
  2044.      mov  [esp+32], eax
  2045.      ret
  2046.  
  2047. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2048.      mov  eax,[CPU_FREQ]
  2049.      mov  [esp+32], eax
  2050.      ret
  2051.  
  2052. ;  SAVE ramdisk to /hd/1/menuet.img
  2053. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2054.    include 'blkdev/rdsave.inc'
  2055. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2056.  
  2057. sysfn_getactive:        ; 18.7 = get active window
  2058.      mov  eax, [TASK_COUNT]
  2059.    movzx  eax, word [WIN_POS + eax*2]
  2060.      mov  [esp+32],eax
  2061.      ret
  2062.  
  2063. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2064.      cmp  ecx,1
  2065.      jne  nogetsoundflag
  2066.      movzx  eax,byte [sound_flag] ; get sound_flag
  2067.      mov  [esp+32],eax
  2068.      ret
  2069.  nogetsoundflag:
  2070.      cmp  ecx,2
  2071.      jnz  nosoundflag
  2072.      xor  byte [sound_flag], 1
  2073.  nosoundflag:
  2074.      ret
  2075.  
  2076. sysfn_minimize:         ; 18.10 = minimize window
  2077.      mov   [window_minimize],1
  2078.      ret
  2079.  
  2080. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2081.      cmp  ecx,1
  2082.      jnz  full_table
  2083.   small_table:
  2084.      call for_all_tables
  2085.      mov ecx,10
  2086.      cld
  2087.      rep movsb
  2088.      ret
  2089.    for_all_tables:
  2090.      mov edi,edx
  2091.      mov esi,DRIVE_DATA
  2092.      ret
  2093.   full_table:
  2094.      cmp  ecx,2
  2095.      jnz  exit_for_anyone
  2096.      call for_all_tables
  2097.      mov ecx,16384
  2098.      cld
  2099.      rep movsd
  2100.      ret
  2101.  
  2102. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2103.         and     dword [esp+32], 0
  2104.         ret
  2105.  
  2106. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2107.      mov edi,ebx
  2108.      mov esi,version_inf
  2109.      mov ecx,version_end-version_inf
  2110.      rep movsb
  2111.      ret
  2112.  
  2113. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2114.      ;wait retrace functions
  2115.  sys_wait_retrace:
  2116.      mov edx,0x3da
  2117.  WaitRetrace_loop:
  2118.      in al,dx
  2119.      test al,1000b
  2120.      jz WaitRetrace_loop
  2121.      and [esp+32],dword 0
  2122.      ret
  2123.  
  2124. sysfn_centermouse:      ; 18.15 = mouse centered
  2125.      call  mouse_centered
  2126.      and [esp+32],dword 0
  2127.      ret
  2128.  
  2129. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2130.      cmp  ecx,0  ; get mouse speed factor
  2131.      jnz  .set_mouse_acceleration
  2132.      xor  eax,eax
  2133.      mov  ax,[mouse_speed_factor]
  2134.      mov  [esp+32],eax
  2135.      ret
  2136.  .set_mouse_acceleration:
  2137.      cmp  ecx,1  ; set mouse speed factor
  2138.      jnz  .get_mouse_delay
  2139.      mov  [mouse_speed_factor],dx
  2140.      ret
  2141.  .get_mouse_delay:
  2142.      cmp  ecx,2  ; get mouse delay
  2143.      jnz  .set_mouse_delay
  2144.      mov  eax,[mouse_delay]
  2145.      mov  [esp+32],eax
  2146.      ret
  2147.  .set_mouse_delay:
  2148.      cmp  ecx,3  ; set mouse delay
  2149.      jnz  .set_pointer_position
  2150.      mov  [mouse_delay],edx
  2151.      ret
  2152.  .set_pointer_position:
  2153.      cmp  ecx,4  ; set mouse pointer position
  2154.      jnz  .set_mouse_button
  2155.      mov   [MOUSE_Y],dx    ;y
  2156.      ror   edx,16
  2157.      mov   [MOUSE_X],dx    ;x
  2158.      rol   edx,16
  2159.      ret
  2160.  .set_mouse_button:
  2161.      cmp   ecx,5  ; set mouse button features
  2162.      jnz  .end
  2163.      mov   [BTN_DOWN],dl
  2164.      mov   [mouse_active],1
  2165.  .end:
  2166.      ret
  2167.  
  2168. sysfn_getfreemem:
  2169.      call _get_free_mem
  2170.      shl eax, 2
  2171.      mov [esp+32],eax
  2172.      ret
  2173.  
  2174. sysfn_getallmem:
  2175.      mov  eax,[_mem_amount]
  2176.      shr eax, 10
  2177.      mov  [esp+32],eax
  2178.      ret
  2179.  
  2180. ; // Alver, 2007-22-08 // {
  2181. sysfn_pid_to_slot:
  2182.      mov   eax, ecx
  2183.      call  pid_to_slot
  2184.      mov   [esp+32], eax
  2185.      ret
  2186.  
  2187. sysfn_min_rest_window:
  2188.      pushad
  2189.      mov   eax, edx      ; ebx - operating
  2190.      shr   ecx, 1
  2191.      jnc    @f
  2192.      call  pid_to_slot
  2193. @@:
  2194.      or    eax, eax      ; eax - number of slot
  2195.      jz    .error
  2196.      cmp   eax, 255         ; varify maximal slot number
  2197.      ja    .error
  2198.      movzx eax, word [WIN_STACK + eax*2]
  2199.      shr   ecx, 1
  2200.      jc    .restore
  2201.  ; .minimize:
  2202.      call  minimize_window
  2203.      jmp   .exit
  2204. .restore:
  2205.      call  restore_minimized_window
  2206. .exit:
  2207.      popad
  2208.      xor   eax, eax
  2209.      mov   [esp+32], eax
  2210.      ret
  2211. .error:
  2212.      popad
  2213.      xor   eax, eax
  2214.      dec   eax
  2215.      mov   [esp+32], eax
  2216.      ret
  2217. ; } \\ Alver, 2007-22-08 \\
  2218.  
  2219. uglobal
  2220. ;// mike.dld, 2006-29-01 [
  2221. screen_workarea RECT
  2222. ;// mike.dld, 2006-29-01 ]
  2223. window_minimize db 0
  2224. sound_flag      db 0
  2225. endg
  2226.  
  2227. iglobal
  2228. version_inf:
  2229.   db 0,7,1,0  ; version 0.7.1.0
  2230.   db UID_KOLIBRI
  2231.   dd __REV__
  2232. version_end:
  2233. endg
  2234.  
  2235. UID_NONE=0
  2236. UID_MENUETOS=1   ;official
  2237. UID_KOLIBRI=2    ;russian
  2238.  
  2239. sys_cachetodiskette:
  2240.         cmp     ebx, 1
  2241.         jne     .no_floppy_a_save
  2242.         mov     [flp_number], 1
  2243.         jmp     .save_image_on_floppy
  2244. .no_floppy_a_save:
  2245.         cmp     ebx, 2
  2246.         jne     .no_floppy_b_save
  2247.         mov     [flp_number], 2
  2248. .save_image_on_floppy:
  2249.         call    save_image
  2250.         mov     [esp + 32], dword 0
  2251.         cmp     [FDC_Status], 0
  2252.         je      .yes_floppy_save
  2253. .no_floppy_b_save:
  2254.         mov     [esp + 32], dword 1
  2255. .yes_floppy_save:
  2256.         ret
  2257.  
  2258. uglobal
  2259. ;  bgrchanged  dd  0x0
  2260. bgrlock db 0
  2261. bgrlockpid dd 0
  2262. endg
  2263.  
  2264. sys_background:
  2265.  
  2266.     cmp   ebx,1                            ; BACKGROUND SIZE
  2267.     jnz   nosb1
  2268.     cmp   ecx,0
  2269.     je    sbgrr
  2270.     cmp   edx,0
  2271.     je    sbgrr
  2272. @@:
  2273.         mov     al, 1
  2274.         xchg    [bgrlock], al
  2275.         test    al, al
  2276.         jz      @f
  2277.         call    change_task
  2278.         jmp     @b
  2279. @@:
  2280.     mov   [BgrDataWidth],ecx
  2281.     mov   [BgrDataHeight],edx
  2282. ;    mov   [bgrchanged],1
  2283.  
  2284.     pushad
  2285. ; return memory for old background
  2286.     stdcall kernel_free, [img_background]
  2287. ; calculate RAW size
  2288.     xor  eax,eax
  2289.     inc  eax
  2290.     cmp  [BgrDataWidth],eax
  2291.     jae   @f
  2292.     mov [BgrDataWidth],eax
  2293. @@:
  2294.     cmp  [BgrDataHeight],eax
  2295.     jae   @f
  2296.     mov [BgrDataHeight],eax
  2297. @@:
  2298.     mov  eax,[BgrDataWidth]
  2299.     imul eax,[BgrDataHeight]
  2300.     lea  ecx,[eax*3]
  2301.     mov  [mem_BACKGROUND],ecx
  2302. ; get memory for new background
  2303.     mov edx, PG_SW
  2304.     stdcall @mem_alloc@8
  2305.     test eax, eax
  2306.     jz .exit_mem
  2307.     mov [img_background], eax
  2308. .exit_mem:
  2309.     popad
  2310.         mov     [bgrlock], 0
  2311.  
  2312.   sbgrr:
  2313.     ret
  2314.  
  2315. nosb1:
  2316.  
  2317.     cmp   ebx,2                            ; SET PIXEL
  2318.     jnz   nosb2
  2319.  
  2320.     mov ebx, [mem_BACKGROUND]
  2321.     add ebx, 4095
  2322.     and ebx, -4096
  2323.     sub ebx, 4
  2324.     cmp   ecx, ebx
  2325.     ja   @F
  2326.  
  2327.     mov   eax,[img_background]
  2328.     mov   ebx,[eax+ecx]
  2329.     and   ebx,0xFF000000 ;255*256*256*256
  2330.     and   edx,0x00FFFFFF ;255*256*256+255*256+255
  2331.     add   edx,ebx
  2332.     mov   [eax+ecx],edx
  2333. @@:
  2334.     ret
  2335.  
  2336. nosb2:
  2337.  
  2338.     cmp   ebx,3                            ; DRAW BACKGROUND
  2339.     jnz   nosb3
  2340. draw_background_temp:
  2341. ;    cmp   [bgrchanged],1 ;0
  2342. ;    je    nosb31
  2343. ;draw_background_temp:
  2344. ;    mov   [bgrchanged],1 ;0
  2345.     mov    [background_defined], 1
  2346.     call  force_redraw_background
  2347.     mov    [REDRAW_BACKGROUND], byte 2
  2348.    nosb31:
  2349.     ret
  2350.   nosb3:
  2351.  
  2352.     cmp   ebx,4                            ; TILED / STRETCHED
  2353.     jnz   nosb4
  2354.     cmp   ecx,[BgrDrawMode]
  2355.     je    nosb41
  2356.     mov   [BgrDrawMode],ecx
  2357. ;    mov   [bgrchanged],1
  2358.    nosb41:
  2359.     ret
  2360.   nosb4:
  2361.  
  2362.     cmp   ebx,5                            ; BLOCK MOVE TO BGR
  2363.     jnz   nosb5
  2364.   ; bughere
  2365.     mov   eax, ecx
  2366.     mov   ebx, edx
  2367.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2368.     mov   ecx, esi
  2369.     call  memmove
  2370.   .fin:
  2371.     ret
  2372.   nosb5:
  2373.  
  2374.         cmp     ebx, 6
  2375.         jnz     nosb6
  2376. @@:
  2377.         mov     al, 1
  2378.         xchg    [bgrlock], al
  2379.         test    al, al
  2380.         jz      @f
  2381.         call    change_task
  2382.         jmp     @b
  2383. @@:
  2384.         mov     eax, [CURRENT_TASK]
  2385.         mov     [bgrlockpid], eax
  2386.         stdcall user_alloc, [mem_BACKGROUND]
  2387.         mov     [esp+32], eax
  2388.         test    eax, eax
  2389.         jz      .nomem
  2390.         mov     ebx, eax
  2391.         shr     ebx, 12
  2392.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2393.         mov     esi, [img_background]
  2394.         shr     esi, 12
  2395.         mov     ecx, [mem_BACKGROUND]
  2396.         add     ecx, 0xFFF
  2397.         shr     ecx, 12
  2398. .z:
  2399.         mov     eax, [page_tabs+ebx*4]
  2400.         test    al, 1
  2401.         jz      @f
  2402.         call    free_page
  2403. @@:
  2404.         mov     eax, [page_tabs+esi*4]
  2405.         or      al, PG_UW
  2406.         mov     [page_tabs+ebx*4], eax
  2407.         mov     eax, ebx
  2408.         shl     eax, 12
  2409.         invlpg  [eax]
  2410.         inc     ebx
  2411.         inc     esi
  2412.         loop    .z
  2413.         ret
  2414. .nomem:
  2415.         and     [bgrlockpid], 0
  2416.         mov     [bgrlock], 0
  2417. nosb6:
  2418.         cmp     ebx, 7
  2419.         jnz     nosb7
  2420.         cmp     [bgrlock], 0
  2421.         jz      .err
  2422.         mov     eax, [CURRENT_TASK]
  2423.         cmp     [bgrlockpid], eax
  2424.         jnz     .err
  2425.         mov     eax, ecx
  2426.         mov     ebx, ecx
  2427.         shr     eax, 12
  2428.         mov     ecx, [page_tabs+(eax-1)*4]
  2429.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2430.         jz      .err
  2431.         jnp     .err
  2432.         push    eax
  2433.         shr     ecx, 12
  2434. @@:
  2435.         and     dword [page_tabs+eax*4], 0
  2436.         mov     edx, eax
  2437.         shl     edx, 12
  2438.         push eax
  2439.         invlpg  [edx]
  2440.         pop eax
  2441.         inc     eax
  2442.         loop    @b
  2443.         pop     eax
  2444.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2445.         stdcall user_free, ebx
  2446.         mov     [esp+32], eax
  2447.         and     [bgrlockpid], 0
  2448.         mov     [bgrlock], 0
  2449.         ret
  2450. .err:
  2451.         and     dword [esp+32], 0
  2452.         ret
  2453.  
  2454. nosb7:
  2455.     ret
  2456.  
  2457. force_redraw_background:
  2458.     mov   [draw_data+32 + RECT.left],dword 0
  2459.     mov   [draw_data+32 + RECT.top],dword 0
  2460.     push  eax ebx
  2461.     mov   eax,[Screen_Max_X]
  2462.     mov   ebx,[Screen_Max_Y]
  2463.     mov   [draw_data+32 + RECT.right],eax
  2464.     mov   [draw_data+32 + RECT.bottom],ebx
  2465.     pop   ebx eax
  2466.     mov   byte [REDRAW_BACKGROUND], 1
  2467.     ret
  2468.  
  2469. align 4
  2470.  
  2471. sys_getbackground:
  2472.  
  2473.     cmp   eax,1                                  ; SIZE
  2474.     jnz   nogb1
  2475.     mov   eax,[BgrDataWidth]
  2476.     shl   eax,16
  2477.     mov   ax,[BgrDataHeight]
  2478.     mov   [esp+36],eax
  2479.     ret
  2480.  
  2481. nogb1:
  2482.  
  2483.     cmp   eax,2                                  ; PIXEL
  2484.     jnz   nogb2
  2485.  
  2486.     mov ecx, [mem_BACKGROUND]
  2487.     add ecx, 4095
  2488.     and ecx, -4096
  2489.     sub ecx, 4
  2490.     cmp ebx, ecx
  2491.     ja  @F
  2492.  
  2493.     mov   eax,[img_background]
  2494.     mov   eax,[ebx+eax]
  2495.  
  2496.     and   eax, 0xFFFFFF
  2497.     mov   [esp+36],eax
  2498. @@:
  2499.     ret
  2500.  
  2501.   nogb2:
  2502.  
  2503.     cmp   eax,4                                  ; TILED / STRETCHED
  2504.     jnz   nogb4
  2505.     mov   eax,[BgrDrawMode]
  2506.   nogb4:
  2507.     mov   [esp+36],eax
  2508.     ret
  2509.  
  2510.  
  2511. align 4
  2512.  
  2513. sys_getkey:
  2514.         mov     [esp + 32],dword 1
  2515.         ; test main buffer
  2516.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2517.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2518.         mov     edx, [TASK_COUNT]
  2519.         cmp     ecx, edx
  2520.         jne     .finish
  2521.         cmp     [KEY_COUNT], byte 0
  2522.         je      .finish
  2523.         movzx   eax, byte [KEY_BUFF]
  2524.         shl     eax, 8
  2525.         push    eax
  2526.         dec     byte [KEY_COUNT]
  2527.         and     byte [KEY_COUNT], 127
  2528.         movzx   ecx, byte [KEY_COUNT]
  2529.         add     ecx, 2
  2530.         mov     eax, KEY_BUFF + 1
  2531.         mov     ebx, KEY_BUFF
  2532.         call    memmove
  2533.         pop     eax
  2534. .ret_eax:
  2535.         mov     [esp + 32], eax
  2536.         ret
  2537. .finish:
  2538. ; test hotkeys buffer
  2539.         mov     ecx, hotkey_buffer
  2540. @@:
  2541.         cmp     [ecx], ebx
  2542.         jz      .found
  2543.         add     ecx, 8
  2544.         cmp     ecx, hotkey_buffer + 120 * 8
  2545.         jb      @b
  2546.         ret
  2547. .found:
  2548.         mov     ax, [ecx + 6]
  2549.         shl     eax, 16
  2550.         mov     ah, [ecx + 4]
  2551.         mov     al, 2
  2552.         and     dword [ecx + 4], 0
  2553.         and     dword [ecx], 0
  2554.         jmp     .ret_eax
  2555.  
  2556. align 4
  2557.  
  2558. sys_getbutton:
  2559.  
  2560.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2561.         mov     [esp + 32], dword 1
  2562.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2563.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2564.         cmp     ecx, edx
  2565.         jne     .exit
  2566.         movzx   eax, byte [BTN_COUNT]
  2567.         test    eax, eax
  2568.         jz      .exit
  2569.         mov     eax, [BTN_BUFF]
  2570.         shl     eax, 8
  2571. ; // Alver 22.06.2008 // {
  2572.         mov       al, byte [btn_down_determ]
  2573.         and       al,0xFE                                       ; delete left button bit
  2574. ; } \\ Alver \\
  2575.         mov     [BTN_COUNT], byte 0
  2576.         mov     [esp + 32], eax
  2577. .exit:
  2578.         ret
  2579.  
  2580.  
  2581. align 4
  2582.  
  2583. sys_cpuusage:
  2584.  
  2585. ;  RETURN:
  2586. ;
  2587. ;  +00 dword     process cpu usage
  2588. ;  +04  word     position in windowing stack
  2589. ;  +06  word     windowing stack value at current position (cpu nro)
  2590. ;  +10 12 bytes  name
  2591. ;  +22 dword     start in mem
  2592. ;  +26 dword     used mem
  2593. ;  +30 dword     PID , process idenfification number
  2594. ;
  2595.  
  2596.     cmp  ecx,-1         ; who am I ?
  2597.     jne  .no_who_am_i
  2598.     mov  ecx,[CURRENT_TASK]
  2599.   .no_who_am_i:
  2600.         cmp     ecx, max_processes
  2601.         ja      .nofillbuf
  2602.  
  2603. ; +4: word: position of the window of thread in the window stack
  2604.         mov     ax, [WIN_STACK + ecx * 2]
  2605.         mov     [ebx+4], ax
  2606. ; +6: word: number of the thread slot, which window has in the window stack
  2607. ;           position ecx (has no relation to the specific thread)
  2608.         mov     ax, [WIN_POS + ecx * 2]
  2609.         mov     [ebx+6], ax
  2610.  
  2611.         shl     ecx, 5
  2612.  
  2613. ; +0: dword: memory usage
  2614.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2615.         mov     [ebx], eax
  2616. ; +10: 11 bytes: name of the process
  2617.         push    ecx
  2618.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2619.         add     ebx, 10
  2620.         mov     ecx, 11
  2621.         call    memmove
  2622.         pop     ecx
  2623.  
  2624. ; +22: address of the process in memory
  2625. ; +26: size of used memory - 1
  2626.         push    edi
  2627.         lea     edi, [ebx+12]
  2628.         xor     eax, eax
  2629.         mov     edx, 0x100000*16
  2630.         cmp     ecx, 1 shl 5
  2631.         je      .os_mem
  2632.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2633.         xor eax, eax
  2634. .os_mem:
  2635.         stosd
  2636.         lea     eax, [edx-1]
  2637.         stosd
  2638.  
  2639. ; +30: PID/TID
  2640.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2641.         stosd
  2642.  
  2643.     ; window position and size
  2644.         push    esi
  2645.         lea     esi, [ecx + window_data + WDATA.box]
  2646.         movsd
  2647.         movsd
  2648.         movsd
  2649.         movsd
  2650.  
  2651.     ; Process state (+50)
  2652.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2653.         stosd
  2654.  
  2655.     ; Window client area box
  2656.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2657.         movsd
  2658.         movsd
  2659.         movsd
  2660.         movsd
  2661.  
  2662.     ; Window state
  2663.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2664.         stosb
  2665.  
  2666.         pop     esi
  2667.         pop     edi
  2668.  
  2669. .nofillbuf:
  2670.     ; return number of processes
  2671.  
  2672.     mov    eax,[TASK_COUNT]
  2673.     mov    [esp+32],eax
  2674.     ret
  2675.  
  2676. align 4
  2677. sys_clock:
  2678.         cli
  2679.   ; Mikhail Lisovin  xx Jan 2005
  2680.   @@:   mov   al, 10
  2681.         out   0x70, al
  2682.         in    al, 0x71
  2683.         test  al, al
  2684.         jns   @f
  2685.         mov   esi, 1
  2686.         call  delay_ms
  2687.         jmp   @b
  2688.   @@:
  2689.   ; end Lisovin's fix
  2690.  
  2691.         xor   al,al           ; seconds
  2692.         out   0x70,al
  2693.         in    al,0x71
  2694.         movzx ecx,al
  2695.         mov   al,02           ; minutes
  2696.         shl   ecx,16
  2697.         out   0x70,al
  2698.         in    al,0x71
  2699.         movzx edx,al
  2700.         mov   al,04           ; hours
  2701.         shl   edx,8
  2702.         out   0x70,al
  2703.         in    al,0x71
  2704.         add   ecx,edx
  2705.         movzx edx,al
  2706.         add   ecx,edx
  2707.         sti
  2708.         mov     [esp + 32], ecx
  2709.         ret
  2710.  
  2711.  
  2712. align 4
  2713.  
  2714. sys_date:
  2715.  
  2716.         cli
  2717.   @@:   mov   al, 10
  2718.         out   0x70, al
  2719.         in    al, 0x71
  2720.         test  al, al
  2721.         jns   @f
  2722.         mov   esi, 1
  2723.         call  delay_ms
  2724.         jmp   @b
  2725.   @@:
  2726.  
  2727.         mov     ch,0
  2728.         mov     al,7            ; date
  2729.         out     0x70,al
  2730.         in      al,0x71
  2731.         mov     cl,al
  2732.         mov     al,8            ; month
  2733.         shl     ecx,16
  2734.         out     0x70,al
  2735.         in      al,0x71
  2736.         mov     ch,al
  2737.         mov     al,9            ; year
  2738.         out     0x70,al
  2739.         in      al,0x71
  2740.         mov     cl,al
  2741.         sti
  2742.         mov     [esp+32], ecx
  2743.         ret
  2744.  
  2745.  
  2746. ; redraw status
  2747.  
  2748. sys_redrawstat:
  2749.         cmp     ebx, 1
  2750.         jne     no_widgets_away
  2751.         ; buttons away
  2752.         mov     ecx,[CURRENT_TASK]
  2753.   sys_newba2:
  2754.         mov     edi,[BTN_ADDR]
  2755.         cmp     [edi], dword 0  ; empty button list ?
  2756.         je      end_of_buttons_away
  2757.         movzx   ebx, word [edi]
  2758.         inc     ebx
  2759.         mov     eax,edi
  2760.   sys_newba:
  2761.         dec     ebx
  2762.         jz      end_of_buttons_away
  2763.  
  2764.         add     eax, 0x10
  2765.         cmp     cx, [eax]
  2766.         jnz     sys_newba
  2767.  
  2768.         push    eax ebx ecx
  2769.         mov     ecx,ebx
  2770.         inc     ecx
  2771.         shl     ecx, 4
  2772.         mov     ebx, eax
  2773.         add     eax, 0x10
  2774.         call    memmove
  2775.         dec     dword [edi]
  2776.         pop     ecx ebx eax
  2777.  
  2778.         jmp     sys_newba2
  2779.  
  2780.   end_of_buttons_away:
  2781.  
  2782.         ret
  2783.  
  2784.   no_widgets_away:
  2785.  
  2786.         cmp     ebx, 2
  2787.         jnz     srl1
  2788.  
  2789.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2790.         add     edx, draw_data - CURRENT_TASK
  2791.         mov     [edx + RECT.left], 0
  2792.         mov     [edx + RECT.top], 0
  2793.         mov     eax, [Screen_Max_X]
  2794.         mov     [edx + RECT.right], eax
  2795.         mov     eax, [Screen_Max_Y]
  2796.         mov     [edx + RECT.bottom], eax
  2797.  
  2798.         mov     edi, [TASK_BASE]
  2799.         or      [edi - twdw + WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2800.         call    sys_window_mouse
  2801.         ret
  2802.  
  2803.   srl1:
  2804.         ret
  2805.  
  2806.  
  2807. sys_drawwindow:
  2808.  
  2809.     mov   eax,edx
  2810.     shr   eax,16+8
  2811.     and   eax,15
  2812.  
  2813. ;    cmp   eax,0   ; type I    - original style
  2814.     jne   nosyswI
  2815.     inc   [mouse_pause]
  2816.     call  [disable_mouse]
  2817.     call  sys_set_window
  2818.     call  [disable_mouse]
  2819.     call  drawwindow_I
  2820.     ;dec   [mouse_pause]
  2821.     ;call   [draw_pointer]
  2822.     ;ret
  2823.     jmp   draw_window_caption.2
  2824.   nosyswI:
  2825.  
  2826.     cmp   al,1    ; type II   - only reserve area, no draw
  2827.     jne   nosyswII
  2828.     inc   [mouse_pause]
  2829.     call  [disable_mouse]
  2830.     call  sys_set_window
  2831.     call  [disable_mouse]
  2832.     call  sys_window_mouse
  2833.     dec   [mouse_pause]
  2834.     call   [draw_pointer]
  2835.     ret
  2836.   nosyswII:
  2837.  
  2838.     cmp   al,2    ; type III  - new style
  2839.     jne   nosyswIII
  2840.     inc   [mouse_pause]
  2841.     call  [disable_mouse]
  2842.     call  sys_set_window
  2843.     call  [disable_mouse]
  2844.     call  drawwindow_III
  2845.     ;dec   [mouse_pause]
  2846.     ;call   [draw_pointer]
  2847.     ;ret
  2848.     jmp   draw_window_caption.2
  2849.   nosyswIII:
  2850.  
  2851.     cmp   al,3    ; type IV - skinned window
  2852.     je    draw_skin_window
  2853.     cmp   al,4    ; type V - skinned window not sized! {not_sized_skin_window}
  2854.     jne   nosyswV
  2855.   draw_skin_window:
  2856.  
  2857.     inc   [mouse_pause]
  2858.     call  [disable_mouse]
  2859.     call  sys_set_window
  2860.     call  [disable_mouse]
  2861.     mov   eax, [TASK_COUNT]
  2862.     movzx eax, word [WIN_POS + eax*2]
  2863.     cmp   eax, [CURRENT_TASK]
  2864.     setz  al
  2865.     movzx eax, al
  2866.     push  eax
  2867.     call  drawwindow_IV
  2868.     ;dec   [mouse_pause]
  2869.     ;call   [draw_pointer]
  2870.     ;ret
  2871.     jmp   draw_window_caption.2
  2872.   nosyswV:
  2873.  
  2874.     ret
  2875.  
  2876.  
  2877. draw_window_caption:
  2878.         inc     [mouse_pause]
  2879.         call    [disable_mouse]
  2880.  
  2881.         xor     eax,eax
  2882.         mov     edx,[TASK_COUNT]
  2883.         movzx   edx,word[WIN_POS+edx*2]
  2884.         cmp     edx,[CURRENT_TASK]
  2885.         jne     @f
  2886.         inc     eax
  2887.     @@: mov     edx,[CURRENT_TASK]
  2888.         shl     edx,5
  2889.         add     edx,window_data
  2890.         movzx   ebx,[edx+WDATA.fl_wstyle]
  2891.         and     bl,0x0F
  2892.         cmp     bl,3
  2893.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  2894.         cmp     bl,4
  2895.         je      .draw_caption_style_3
  2896.  
  2897.         jmp     .not_style_3
  2898.   .draw_caption_style_3:
  2899.  
  2900.         push    edx
  2901.         call    drawwindow_IV_caption
  2902.         add     esp,4
  2903.         jmp     .2
  2904.  
  2905.   .not_style_3:
  2906.         cmp     bl,2
  2907.         jne     .not_style_2
  2908.  
  2909.         call    drawwindow_III_caption
  2910.         jmp     .2
  2911.  
  2912.   .not_style_2:
  2913.         cmp     bl,0
  2914.         jne     .2
  2915.  
  2916.         call    drawwindow_I_caption
  2917.  
  2918. ;--------------------------------------------------------------
  2919.   .2:   ;jmp     @f
  2920.         mov     edi,[CURRENT_TASK]
  2921.         shl     edi,5
  2922.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  2923.         jz      @f
  2924.         mov     edx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  2925.         or      edx,edx
  2926.         jz      @f
  2927.  
  2928.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  2929.         and     al,0x0F
  2930.         cmp     al,3
  2931.         je      .skinned
  2932.         cmp     al,4
  2933.         je      .skinned
  2934.  
  2935.         jmp     .not_skinned
  2936.   .skinned:
  2937.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  2938.         mov     bp,word[edi+window_data+WDATA.box.top]
  2939.         movzx   eax,word[edi+window_data+WDATA.box.width]
  2940.         sub     ax,[_skinmargins.left]
  2941.         sub     ax,[_skinmargins.right]
  2942.         push    edx
  2943.         cwde
  2944.         cdq
  2945.         mov     ebx,6
  2946.         idiv    ebx
  2947.         pop     edx
  2948.         or      eax,eax
  2949.         js      @f
  2950.         mov     esi,eax
  2951.         mov     ebx,dword[_skinmargins.left-2]
  2952.         mov     bx,word[_skinh]
  2953.         sub     bx,[_skinmargins.bottom]
  2954.         sub     bx,[_skinmargins.top]
  2955.         sar     bx,1
  2956.         adc     bx,0
  2957.         add     bx,[_skinmargins.top]
  2958.         add     bx,-3
  2959.         add     ebx,ebp
  2960.         jmp     .dodraw
  2961.  
  2962.   .not_skinned:
  2963.         cmp     al,1
  2964.         je      @f
  2965.  
  2966.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  2967.         mov     bp,word[edi+window_data+WDATA.box.top]
  2968.         movzx   eax,word[edi+window_data+WDATA.box.width]
  2969.         sub     eax,16
  2970.         push    edx
  2971.         cwde
  2972.         cdq
  2973.         mov     ebx,6
  2974.         idiv    ebx
  2975.         pop     edx
  2976.         or      eax,eax
  2977.         js      @f
  2978.         mov     esi,eax
  2979.         mov     ebx,0x00080007
  2980.         add     ebx,ebp
  2981. .dodraw:
  2982.         mov     ecx,[common_colours+16];0x00FFFFFF
  2983.         or      ecx, 0x80000000
  2984.         xor     edi,edi
  2985. ; // Alver 22.06.2008 // {
  2986. ;       call    dtext
  2987.         call dtext_asciiz_esi
  2988. ; } \\ Alver \\
  2989.  
  2990.     @@:
  2991. ;--------------------------------------------------------------
  2992.         dec     [mouse_pause]
  2993.         call    [draw_pointer]
  2994.         ret
  2995.  
  2996. iglobal
  2997. align 4
  2998. window_topleft dd \
  2999.   1, 21,\               ;type 0
  3000.   0,  0,\       ;type 1
  3001.   5, 20,\       ;type 2
  3002.   5,  ?,\       ;type 3 {set by skin}
  3003.   5,  ?         ;type 4 {set by skin}
  3004. endg
  3005.  
  3006. set_window_clientbox:
  3007.         push    eax ecx edi
  3008.  
  3009.         mov     eax,[_skinh]
  3010.         mov     [window_topleft+4*7],eax
  3011.         mov     [window_topleft+4*9],eax
  3012.  
  3013.         mov     ecx,edi
  3014.         sub     edi,window_data
  3015.         shl     edi,3
  3016.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3017.         jz      @f
  3018.  
  3019.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3020.         and     eax,0x0F
  3021.         mov     eax,[eax*8+window_topleft+0]
  3022.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3023.         shl     eax,1
  3024.         neg     eax
  3025.         add     eax,[ecx+WDATA.box.width]
  3026.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3027.  
  3028.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3029.         and     eax,0x0F
  3030.         push    [eax*8+window_topleft+0]
  3031.         mov     eax,[eax*8+window_topleft+4]
  3032.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3033.         neg     eax
  3034.         sub     eax,[esp]
  3035.         add     eax,[ecx+WDATA.box.height]
  3036.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3037.         add     esp,4
  3038.  
  3039.         pop     edi ecx eax
  3040.         ret
  3041.     @@:
  3042.         xor     eax,eax
  3043.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3044.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3045.         mov     eax,[ecx+WDATA.box.width]
  3046.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3047.         mov     eax,[ecx+WDATA.box.height]
  3048.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3049.  
  3050.         pop     edi ecx eax
  3051.         ret
  3052.  
  3053. sys_set_window:
  3054.  
  3055.     mov   eax,[CURRENT_TASK]
  3056.     shl   eax,5
  3057.     add   eax,window_data
  3058.  
  3059.     ; colors
  3060.     mov   [eax+WDATA.cl_workarea],edx
  3061.     mov   [eax+WDATA.cl_titlebar],esi
  3062.     mov   [eax+WDATA.cl_frames],edi
  3063.  
  3064.     mov   edi, eax
  3065.  
  3066.     ; check flag (?)
  3067.     test  [edi+WDATA.fl_wdrawn],1
  3068.     jnz   newd
  3069.  
  3070.     mov   eax,[timer_ticks] ;[0xfdf0]
  3071.     add   eax,100
  3072.     mov   [new_window_starting],eax
  3073.  
  3074.     movsx eax,bx
  3075.     mov   [edi+WDATA.box.width],eax
  3076.     movsx eax,cx
  3077.     mov   [edi+WDATA.box.height],eax
  3078.     sar   ebx,16
  3079.     sar   ecx,16
  3080.     mov   [edi+WDATA.box.left],ebx
  3081.     mov   [edi+WDATA.box.top],ecx
  3082.  
  3083.     call  check_window_position
  3084.  
  3085.     call  set_window_clientbox
  3086.  
  3087.     push  ecx esi edi               ; save for window fullscreen/resize
  3088.     ;mov   esi,edi
  3089.  
  3090.         mov     cl, [edi+WDATA.fl_wstyle]
  3091.         mov     eax, [edi+WDATA.cl_frames]
  3092.  
  3093.     sub   edi,window_data
  3094.     shl   edi,3
  3095.     add   edi,SLOT_BASE
  3096.  
  3097.         and     cl,0x0F
  3098.         mov     [edi+APPDATA.wnd_caption],0
  3099.         cmp     cl,3
  3100.         je      set_APPDATA_wnd_caption
  3101.         cmp     cl,4                                                            ; {SPraid.simba}
  3102.         je      set_APPDATA_wnd_caption
  3103.  
  3104.         jmp     @f
  3105.     set_APPDATA_wnd_caption:
  3106.         mov     [edi+APPDATA.wnd_caption],eax
  3107.     @@: mov     esi,[esp+0]
  3108.  
  3109.     add   edi, APPDATA.saved_box
  3110.         movsd
  3111.         movsd
  3112.         movsd
  3113.         movsd
  3114.     pop   edi esi ecx
  3115.  
  3116.         mov     esi, [CURRENT_TASK]
  3117.         movzx   esi, word [WIN_STACK+esi*2]
  3118.         lea     esi, [WIN_POS+esi*2]
  3119.         call    waredraw
  3120.  
  3121. ;;;    mov   ebx, 1
  3122. ;;;    call  delay_hs
  3123.     mov   eax, [edi+WDATA.box.left]
  3124.     mov   ebx, [edi+WDATA.box.top]
  3125.     mov   ecx, [edi+WDATA.box.width]
  3126.     mov   edx, [edi+WDATA.box.height]
  3127.     add   ecx, eax
  3128.     add   edx, ebx
  3129.     call  calculatescreen
  3130.  
  3131.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3132.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3133.  
  3134.   newd:
  3135.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3136.     mov   edx,edi
  3137.  
  3138.     ret
  3139.  
  3140. syscall_windowsettings:
  3141.  
  3142.   .set_window_caption:
  3143.         dec     eax     ; subfunction #1 - set window caption
  3144.         jnz     .get_window_caption
  3145.  
  3146.         ; NOTE: only window owner thread can set its caption,
  3147.         ;       so there's no parameter for PID/TID
  3148.  
  3149.         mov     edi,[CURRENT_TASK]
  3150.         shl     edi,5
  3151.  
  3152.         ; have to check if caption is within application memory limit
  3153.         ; check is trivial, and if application resizes its memory,
  3154.         ;   caption still can become over bounds
  3155. ; diamond, 31.10.2006: check removed because with new memory manager
  3156. ; there can be valid data after APPDATA.mem_size bound
  3157. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3158. ;        add     ecx,255 ; max caption length
  3159. ;        cmp     ebx,ecx
  3160. ;        ja      .exit_fail
  3161.  
  3162.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3163.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3164.  
  3165.         call    draw_window_caption
  3166.  
  3167.         xor     eax,eax ; eax = 0 (success)
  3168.         ret
  3169.  
  3170.   .get_window_caption:
  3171.         dec     eax     ; subfunction #2 - get window caption
  3172.         jnz     .exit_fail
  3173.  
  3174.         ; not implemented yet
  3175.  
  3176.   .exit_fail:
  3177.         xor     eax,eax
  3178.         inc     eax     ; eax = 1 (fail)
  3179.         ret
  3180.  
  3181.  
  3182. sys_window_move:
  3183.  
  3184.         mov     edi,[CURRENT_TASK]
  3185.         shl     edi,5
  3186.         add     edi,window_data
  3187.  
  3188.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3189.         jnz     .window_move_return
  3190.  
  3191.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3192.         push    dword [edi + WDATA.box.top]
  3193.         push    dword [edi + WDATA.box.width]
  3194.         push    dword [edi + WDATA.box.height]
  3195.  
  3196.         cmp   eax,-1                  ; set new position and size
  3197.         je    .no_x_reposition
  3198.         mov     [edi + WDATA.box.left], eax
  3199.       .no_x_reposition:
  3200.         cmp   ebx,-1
  3201.         je    .no_y_reposition
  3202.         mov     [edi + WDATA.box.top], ebx
  3203.       .no_y_reposition:
  3204.  
  3205.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3206.         jnz     .no_y_resizing
  3207.  
  3208.         cmp   ecx,-1
  3209.         je    .no_x_resizing
  3210.         mov     [edi + WDATA.box.width], ecx
  3211.       .no_x_resizing:
  3212.         cmp   edx,-1
  3213.         je    .no_y_resizing
  3214.         mov     [edi + WDATA.box.height], edx
  3215.       .no_y_resizing:
  3216.  
  3217.         call  check_window_position
  3218.         call  set_window_clientbox
  3219.  
  3220.         pushad                       ; save for window fullscreen/resize
  3221.         mov   esi,edi
  3222.         sub   edi,window_data
  3223.         shr   edi,5
  3224.         shl   edi,8
  3225.         add   edi, SLOT_BASE + APPDATA.saved_box
  3226.         mov   ecx,4
  3227.         cld
  3228.         rep   movsd
  3229.         popad
  3230.  
  3231.         pushad                       ; calculcate screen at new position
  3232.         mov   eax, [edi + WDATA.box.left]
  3233.         mov   ebx, [edi + WDATA.box.top]
  3234.         mov   ecx, [edi + WDATA.box.width]
  3235.         mov   edx, [edi + WDATA.box.height]
  3236.         add   ecx,eax
  3237.         add   edx,ebx
  3238.  
  3239.         call  calculatescreen
  3240.         popad
  3241.  
  3242.         pop   edx                   ; calculcate screen at old position
  3243.         pop   ecx
  3244.         pop   ebx
  3245.         pop   eax
  3246.         add   ecx,eax
  3247.         add   edx,ebx
  3248.         mov   [dlx],eax             ; save for drawlimits
  3249.         mov   [dly],ebx
  3250.         mov   [dlxe],ecx
  3251.         mov   [dlye],edx
  3252.         call  calculatescreen
  3253.  
  3254.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3255.  
  3256.         mov   eax,edi               ; redraw screen at old position
  3257.         xor   esi,esi
  3258.         call  redrawscreen
  3259.  
  3260.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3261.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3262.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3263.  
  3264.         call  [draw_pointer]
  3265.  
  3266.         mov   [window_move_pr],0
  3267.  
  3268.       .window_move_return:
  3269.  
  3270.         ret
  3271.  
  3272. uglobal
  3273.   window_move_pr   dd  0x0
  3274.   window_move_eax  dd  0x0
  3275.   window_move_ebx  dd  0x0
  3276.   window_move_ecx  dd  0x0
  3277.   window_move_edx  dd  0x0
  3278. endg
  3279.  
  3280. ;ok - 100% work
  3281. ;nt - not tested
  3282. ;---------------------------------------------------------------------------------------------
  3283. ;eax
  3284. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3285. ;1 - change task. Ret nothing. Block. ok.
  3286. ;2 - performance control
  3287. ; ebx
  3288. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3289. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3290. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3291. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3292. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3293. ;eax
  3294. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3295. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3296. ;---------------------------------------------------------------------------------------------
  3297. sys_sheduler: ;noname & halyavin
  3298.     cmp eax,0
  3299.     je shed_counter
  3300.     cmp eax,2
  3301.     je perf_control
  3302.     cmp eax,3
  3303.     je rdmsr_instr
  3304.     cmp eax,4
  3305.     je wrmsr_instr
  3306.     cmp eax,1
  3307.     jne not_supported
  3308.     call change_task ;delay,0
  3309. ret
  3310. shed_counter:
  3311.     mov eax,[context_counter]
  3312.     mov [esp+36],eax
  3313. not_supported:
  3314. ret
  3315. perf_control:
  3316.     inc eax ;now eax=3
  3317.     cmp ebx,eax
  3318.     je cache_disable
  3319.     dec eax
  3320.     cmp ebx,eax
  3321.     je cache_enable
  3322.     dec eax
  3323.     cmp ebx,eax
  3324.     je is_cache_enabled
  3325.     dec eax
  3326.     cmp ebx,eax
  3327.     je modify_pce
  3328. ret
  3329.  
  3330. rdmsr_instr:
  3331. ;now counter in ecx
  3332. ;(edx:eax) esi:edi => edx:esi
  3333. mov eax,esi
  3334. rdmsr
  3335. mov [esp+36],eax
  3336. mov [esp+24],edx ;ret in ebx?
  3337. ret
  3338.  
  3339. wrmsr_instr:
  3340. ;now counter in ecx
  3341. ;(edx:eax) esi:edi => edx:esi
  3342.         ; Fast Call MSR can't be destroy
  3343.         ; Íî MSR_AMD_EFER ìîæíî èçìåíÿòü, ò.ê. â ýòîì ðåãèñòðå ëèø
  3344.         ; âêëþ÷àþòñÿ/âûêëþ÷àþòñÿ ðàñøèðåííûå âîçìîæíîñòè
  3345.         cmp     ecx, MSR_SYSENTER_CS
  3346.         je      @f
  3347.         cmp     ecx, MSR_SYSENTER_ESP
  3348.         je      @f
  3349.         cmp     ecx, MSR_SYSENTER_EIP
  3350.         je      @f
  3351.         cmp     ecx, MSR_AMD_STAR
  3352.         je      @f
  3353.  
  3354.         mov     eax, esi
  3355.         wrmsr
  3356.         ; mov   [esp + 36], eax
  3357.         ; mov   [esp + 24], edx ;ret in ebx?
  3358. @@:
  3359. ret
  3360.  
  3361. cache_disable:
  3362.        mov eax,cr0
  3363.        or  eax,01100000000000000000000000000000b
  3364.        mov cr0,eax
  3365.        wbinvd ;set MESI
  3366. ret
  3367.  
  3368. cache_enable:
  3369.        mov eax,cr0
  3370.        and eax,10011111111111111111111111111111b
  3371.        mov cr0,eax
  3372. ret
  3373.  
  3374. is_cache_enabled:
  3375.        mov eax,cr0
  3376.        mov ebx,eax
  3377.        and eax,01100000000000000000000000000000b
  3378.        jz cache_disabled
  3379.        mov [esp+36],ebx
  3380. cache_disabled:
  3381.        mov dword [esp+36],eax ;0
  3382. ret
  3383.  
  3384. modify_pce:
  3385.        mov eax,cr4
  3386. ;       mov ebx,0
  3387. ;       or  bx,100000000b ;pce
  3388. ;       xor eax,ebx ;invert pce
  3389.        bts eax,8 ;pce=cr4[8]
  3390.        mov cr4,eax
  3391.        mov [esp+36],eax
  3392. ret
  3393. ;---------------------------------------------------------------------------------------------
  3394.  
  3395.  
  3396. ; check if pixel is allowed to be drawn
  3397.  
  3398. checkpixel:
  3399.         push eax edx
  3400.  
  3401.         mov  edx,[Screen_Max_X]     ; screen x size
  3402.         mov  ecx, [_display_data]
  3403.  
  3404.         inc  edx
  3405.         imul edx, ebx
  3406.  
  3407.         add edx, ecx
  3408.         mov  dl, [eax+edx] ; lea eax, [...]
  3409.  
  3410.         xor  ecx, ecx
  3411.         mov  eax, [CURRENT_TASK]
  3412.         cmp  al, dl
  3413.         setne cl
  3414.  
  3415.         pop  edx eax
  3416.         ret
  3417.  
  3418. iglobal
  3419.   cpustring db 'CPU',0
  3420. endg
  3421.  
  3422. uglobal
  3423. background_defined    db    0    ; diamond, 11.04.2006
  3424. endg
  3425.  
  3426. align 4
  3427. ; check misc
  3428.  
  3429. checkmisc:
  3430.  
  3431.     cmp   [ctrl_alt_del], 1
  3432.     jne   nocpustart
  3433.  
  3434.         mov     ebp, cpustring
  3435.         call    fs_execute_from_sysdir
  3436.  
  3437.     mov   [ctrl_alt_del], 0
  3438.  
  3439. nocpustart:
  3440.     cmp   [mouse_active], 1
  3441.     jne   mouse_not_active
  3442.     mov   [mouse_active], 0
  3443.     xor   edi, edi
  3444.     mov   ecx,  [TASK_COUNT]
  3445. set_mouse_event:
  3446.     add   edi, 256
  3447.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3448.     loop  set_mouse_event
  3449.  
  3450. mouse_not_active:
  3451.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3452.     jz    nobackgr
  3453.     cmp    [background_defined], 0
  3454.     jz    nobackgr
  3455.     cmp   [REDRAW_BACKGROUND], byte 2
  3456.     jnz   no_set_bgr_event
  3457.     xor   edi, edi
  3458.     mov   ecx,  [TASK_COUNT]
  3459. set_bgr_event:
  3460.     add   edi, 256
  3461.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3462.     loop  set_bgr_event
  3463. no_set_bgr_event:
  3464. ;    mov   [draw_data+32 + RECT.left],dword 0
  3465. ;    mov   [draw_data+32 + RECT.top],dword 0
  3466. ;    mov   eax,[Screen_Max_X]
  3467. ;    mov   ebx,[Screen_Max_Y]
  3468. ;    mov   [draw_data+32 + RECT.right],eax
  3469. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3470.     call  drawbackground
  3471.     mov   [REDRAW_BACKGROUND],byte 0
  3472.     mov   [MOUSE_BACKGROUND],byte 0
  3473.  
  3474. nobackgr:
  3475.  
  3476.     ; system shutdown request
  3477.  
  3478.     cmp  [SYS_SHUTDOWN],byte 0
  3479.     je   noshutdown
  3480.  
  3481.     mov  edx,[shutdown_processes]
  3482.  
  3483.     cmp  [SYS_SHUTDOWN],dl
  3484.     jne  no_mark_system_shutdown
  3485.  
  3486.     lea   ecx,[edx-1]
  3487.     mov   edx,OS_BASE+0x3040
  3488.     jecxz @f
  3489. markz:
  3490.     mov   [edx+TASKDATA.state],byte 3
  3491.     add   edx,0x20
  3492.     loop  markz
  3493. @@:
  3494.  
  3495.   no_mark_system_shutdown:
  3496.  
  3497.     call [disable_mouse]
  3498.  
  3499.     dec  byte [SYS_SHUTDOWN]
  3500.     je   system_shutdown
  3501.  
  3502. noshutdown:
  3503.  
  3504.  
  3505.     mov   eax,[TASK_COUNT]                  ; termination
  3506.     mov   ebx,TASK_DATA+TASKDATA.state
  3507.     mov   esi,1
  3508.  
  3509. newct:
  3510.     mov   cl,[ebx]
  3511.     cmp   cl,byte 3
  3512.     jz    terminate
  3513.     cmp   cl,byte 4
  3514.     jz    terminate
  3515.  
  3516.     add   ebx,0x20
  3517.     inc   esi
  3518.     dec   eax
  3519.     jnz   newct
  3520.     ret
  3521.  
  3522. ; redraw screen
  3523.  
  3524. redrawscreen:
  3525.  
  3526. ; eax , if process window_data base is eax, do not set flag/limits
  3527.  
  3528.          pushad
  3529.          push  eax
  3530.  
  3531. ;;;         mov   ebx,2
  3532. ;;;         call  delay_hs
  3533.  
  3534.          ;mov   ecx,0               ; redraw flags for apps
  3535.          xor   ecx,ecx
  3536.        newdw2:
  3537.  
  3538.          inc   ecx
  3539.          push  ecx
  3540.  
  3541.          mov   eax,ecx
  3542.          shl   eax,5
  3543.          add   eax,window_data
  3544.  
  3545.          cmp   eax,[esp+4]
  3546.          je    not_this_task
  3547.                                    ; check if window in redraw area
  3548.          mov   edi,eax
  3549.  
  3550.          cmp   ecx,1               ; limit for background
  3551.          jz    bgli
  3552.  
  3553.          mov   eax, [edi + WDATA.box.left]
  3554.          mov   ebx, [edi + WDATA.box.top]
  3555.          mov   ecx, [edi + WDATA.box.width]
  3556.          mov   edx, [edi + WDATA.box.height]
  3557.          add   ecx,eax
  3558.          add   edx,ebx
  3559.  
  3560.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3561.          cmp   ecx,ebx
  3562.          jb    ricino
  3563.  
  3564.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3565.          cmp   ecx,eax
  3566.          jb    ricino
  3567.  
  3568.          mov   eax, [edi + WDATA.box.left]
  3569.          mov   ebx, [edi + WDATA.box.top]
  3570.          mov   ecx, [edi + WDATA.box.width]
  3571.          mov   edx, [edi + WDATA.box.height]
  3572.          add   ecx, eax
  3573.          add   edx, ebx
  3574.  
  3575.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3576.          cmp   edx,eax
  3577.          jb    ricino
  3578.  
  3579.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3580.          cmp   ecx,eax
  3581.          jb    ricino
  3582.  
  3583.         bgli:
  3584.  
  3585.          cmp   ecx,1
  3586.          jnz   .az
  3587.          mov   al,[REDRAW_BACKGROUND]
  3588.          cmp   al,2
  3589.          jz    newdw8
  3590.          test  al,al
  3591.          jz    .az
  3592.          lea   eax,[edi+draw_data-window_data]
  3593.          mov   ebx,[dlx]
  3594.          cmp   ebx,[eax+RECT.left]
  3595.          jae   @f
  3596.          mov   [eax+RECT.left],ebx
  3597.         @@:
  3598.          mov   ebx,[dly]
  3599.          cmp   ebx,[eax+RECT.top]
  3600.          jae   @f
  3601.          mov   [eax+RECT.top],ebx
  3602.         @@:
  3603.          mov   ebx,[dlxe]
  3604.          cmp   ebx,[eax+RECT.right]
  3605.          jbe   @f
  3606.          mov   [eax+RECT.right],ebx
  3607.         @@:
  3608.          mov   ebx,[dlye]
  3609.          cmp   ebx,[eax+RECT.bottom]
  3610.          jbe   @f
  3611.          mov   [eax+RECT.bottom],ebx
  3612.         @@:
  3613.          jmp   newdw8
  3614.         .az:
  3615.  
  3616.          mov   eax,edi
  3617.          add   eax,draw_data-window_data
  3618.  
  3619.          mov   ebx,[dlx]          ; set limits
  3620.          mov   [eax + RECT.left], ebx
  3621.          mov   ebx,[dly]
  3622.          mov   [eax + RECT.top], ebx
  3623.          mov   ebx,[dlxe]
  3624.          mov   [eax + RECT.right], ebx
  3625.          mov   ebx,[dlye]
  3626.          mov   [eax + RECT.bottom], ebx
  3627.  
  3628.          sub   eax,draw_data-window_data
  3629.  
  3630.          cmp   dword [esp],1
  3631.          jne   nobgrd
  3632.          mov   byte [REDRAW_BACKGROUND], 1
  3633.  
  3634.        newdw8:
  3635.        nobgrd:
  3636.  
  3637.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3638.  
  3639.        ricino:
  3640.  
  3641.        not_this_task:
  3642.  
  3643.          pop   ecx
  3644.  
  3645.          cmp   ecx,[TASK_COUNT]
  3646.          jle   newdw2
  3647.  
  3648.          pop  eax
  3649.          popad
  3650.  
  3651.          ret
  3652.  
  3653. calculatebackground:   ; background
  3654.  
  3655.         ; all black
  3656.  
  3657.         mov   edi, [img_background]  ;IMG_BACKGROUND                 ; set background to black
  3658.         xor   eax, eax
  3659.         mov   ecx, 1023    ;0x0fff00 / 4
  3660.         cld
  3661.         rep   stosd
  3662.  
  3663.         mov   edi, [_display_data]              ; set os to use all pixels
  3664.         mov   eax,0x01010101
  3665.         mov   ecx,1280*1024 / 4
  3666.         rep   stosd
  3667.  
  3668.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3669.  
  3670.         ret
  3671.  
  3672. uglobal
  3673.   imax    dd 0x0
  3674. endg
  3675.  
  3676.  
  3677.  
  3678. delay_ms:     ; delay in 1/1000 sec
  3679.  
  3680.  
  3681.         push  eax
  3682.         push  ecx
  3683.  
  3684.         mov   ecx,esi
  3685.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3686.         imul  ecx, 33941
  3687.         shr   ecx, 9
  3688.         ; </CPU clock fix>
  3689.  
  3690.         in    al,0x61
  3691.         and   al,0x10
  3692.         mov   ah,al
  3693.         cld
  3694.  
  3695.  cnt1:  in    al,0x61
  3696.         and   al,0x10
  3697.         cmp   al,ah
  3698.         jz    cnt1
  3699.  
  3700.         mov   ah,al
  3701.         loop  cnt1
  3702.  
  3703.         pop   ecx
  3704.         pop   eax
  3705.  
  3706.         ret
  3707.  
  3708.  
  3709. set_app_param:
  3710.         mov     edi, [TASK_BASE]
  3711.         mov     [edi + TASKDATA.event_mask], ebx
  3712.         ret
  3713.  
  3714.  
  3715.  
  3716. delay_hs:     ; delay in 1/100 secs
  3717. ; ebx = delay time
  3718.         push  ecx
  3719.         push  edx
  3720.  
  3721.         mov   edx,[timer_ticks]
  3722.  
  3723.       newtic:
  3724.         mov   ecx,[timer_ticks]
  3725.         sub   ecx,edx
  3726.         cmp   ecx,ebx
  3727.         jae   zerodelay
  3728.  
  3729.         call  change_task
  3730.  
  3731.         jmp   newtic
  3732.  
  3733.       zerodelay:
  3734.         pop   edx
  3735.         pop   ecx
  3736.  
  3737.         ret
  3738.  
  3739.  
  3740. memmove:       ; memory move in bytes
  3741.  
  3742. ; eax = from
  3743. ; ebx = to
  3744. ; ecx = no of bytes
  3745.     test ecx, ecx
  3746.     jle  .ret
  3747.  
  3748.  
  3749.     push esi edi ecx
  3750.  
  3751.     mov  edi, ebx
  3752.     mov  esi, eax
  3753.  
  3754.     test ecx, not 11b
  3755.     jz   @f
  3756.  
  3757.     push ecx
  3758.     shr  ecx, 2
  3759.     rep  movsd
  3760.     pop  ecx
  3761.     and  ecx, 11b
  3762.     jz   .finish
  3763.   @@:
  3764.     rep  movsb
  3765.  
  3766.   .finish:
  3767.     pop  ecx edi esi
  3768.   .ret:
  3769.     ret
  3770.  
  3771.  
  3772. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3773. ;align 4
  3774. ;
  3775. ;read_floppy_file:
  3776. ;
  3777. ;; as input
  3778. ;;
  3779. ;; eax pointer to file
  3780. ;; ebx file lenght
  3781. ;; ecx start 512 byte block number
  3782. ;; edx number of blocks to read
  3783. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3784. ;;
  3785. ;;
  3786. ;; on return
  3787. ;;
  3788. ;; eax = 0 command succesful
  3789. ;;       1 no fd base and/or partition defined
  3790. ;;       2 yet unsupported FS
  3791. ;;       3 unknown FS
  3792. ;;       4 partition not defined at hd
  3793. ;;       5 file not found
  3794. ;; ebx = size of file
  3795. ;
  3796. ;     mov   edi,[TASK_BASE]
  3797. ;     add   edi,0x10
  3798. ;     add   esi,[edi]
  3799. ;     add   eax,[edi]
  3800. ;
  3801. ;     pushad
  3802. ;     mov  edi,esi
  3803. ;     add  edi,1024
  3804. ;     mov  esi,0x100000+19*512
  3805. ;     sub  ecx,1
  3806. ;     shl  ecx,9
  3807. ;     add  esi,ecx
  3808. ;     shl  edx,9
  3809. ;     mov  ecx,edx
  3810. ;     cld
  3811. ;     rep  movsb
  3812. ;     popad
  3813. ;
  3814. ;     mov   [esp+36],eax
  3815. ;     mov   [esp+24],ebx
  3816. ;     ret
  3817.  
  3818.  
  3819.  
  3820. align 4
  3821.  
  3822. sys_programirq:
  3823.  
  3824.     mov   eax, [TASK_BASE]
  3825.     add   ebx, [eax + TASKDATA.mem_start]
  3826.  
  3827.     cmp   ecx, 16
  3828.     jae   .not_owner
  3829.     mov   edi, [eax + TASKDATA.pid]
  3830.     cmp   edi, [irq_owner + 4 * ecx]
  3831.     je    .spril1
  3832. .not_owner:
  3833.     xor   ecx, ecx
  3834.     jmp   .end
  3835.   .spril1:
  3836.  
  3837.     shl   ecx, 6
  3838.     mov   esi, ebx
  3839.     lea   edi, [irq00read + ecx]
  3840.     push  16
  3841.     pop   ecx
  3842.  
  3843.     cld
  3844.     rep   movsd
  3845.   .end:
  3846.     mov   [esp+32], ecx
  3847.     ret
  3848.  
  3849.  
  3850. align 4
  3851.  
  3852. get_irq_data:
  3853.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3854.      xor   bh, bh
  3855.      cmp   ebx, 16
  3856.      jae   .not_owner
  3857.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3858.  
  3859.      mov   eax,[TASK_BASE]
  3860.  
  3861.      cmp   edx,[eax+TASKDATA.pid]
  3862.      je    gidril1
  3863. .not_owner:
  3864.      xor   edx, edx
  3865.      dec   edx
  3866.      jmp   gid1
  3867.  
  3868.   gidril1:
  3869.  
  3870.      shl   ebx, 12
  3871.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3872.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3873.      dec   esi
  3874.      jz    gid1
  3875.      test  edx, edx                      ; check if buffer is empty
  3876.      jz    gid1
  3877.  
  3878.      mov   ebx, [eax + 0x4]
  3879.      mov   edi, ecx
  3880.  
  3881.      mov   ecx, 4000                     ; buffer size, used frequently
  3882.  
  3883.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3884.      jb    @f
  3885.  
  3886.      xor   ebx, ebx
  3887.  
  3888.    @@:
  3889.  
  3890.      lea   esi, [ebx + edx]              ; calculate data size and offset
  3891.      cld
  3892.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  3893.      jbe   @f
  3894.  
  3895.      sub   ecx, ebx
  3896.      sub   edx, ecx
  3897.  
  3898.      lea   esi, [eax + ebx + 0x10]
  3899.      rep   movsb
  3900.  
  3901.      xor   ebx, ebx
  3902.    @@:
  3903.      lea   esi, [eax + ebx + 0x10]
  3904.      mov   ecx, edx
  3905.      add   ebx, edx
  3906.  
  3907.      rep   movsb
  3908.      mov   edx, [eax]
  3909.      mov   [eax], ecx                    ; set data size to zero
  3910.      mov   [eax + 0x4], ebx              ; set data offset
  3911.  
  3912.    gid1:
  3913.      mov   [esp+32], edx                 ; eax
  3914.      ret
  3915.  
  3916.  
  3917. set_io_access_rights:
  3918.  
  3919.      pushad
  3920.  
  3921.      mov edi, tss._io_map_0
  3922.  
  3923. ;     mov   ecx,eax
  3924. ;     and   ecx,7    ; offset in byte
  3925.  
  3926. ;     shr   eax,3    ; number of byte
  3927. ;     add   edi,eax
  3928.  
  3929. ;     mov   ebx,1
  3930. ;     shl   ebx,cl
  3931.  
  3932.      cmp   ebp,0                ; enable access - ebp = 0
  3933.      jne   siar1
  3934.  
  3935. ;     not   ebx
  3936. ;     and   [edi],byte bl
  3937.      btr [edi], eax
  3938.  
  3939.      popad
  3940.  
  3941.      ret
  3942.  
  3943. siar1:
  3944.  
  3945.      bts [edi], eax
  3946.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3947.  
  3948.      popad
  3949.  
  3950.      ret
  3951.  
  3952. r_f_port_area:
  3953.  
  3954.      test  eax, eax
  3955.      jnz   free_port_area
  3956. ;     je    r_port_area
  3957. ;     jmp   free_port_area
  3958.  
  3959. ;   r_port_area:
  3960.  
  3961.      pushad
  3962.  
  3963.      cmp   ebx,ecx            ; beginning > end ?
  3964.      ja    rpal1
  3965.      cmp   ecx,65536
  3966.      jae   rpal1
  3967.      mov   esi,[RESERVED_PORTS]
  3968.      test  esi,esi            ; no reserved areas ?
  3969.      je    rpal2
  3970.      cmp   esi,255            ; max reserved
  3971.      jae   rpal1
  3972.  rpal3:
  3973.      mov   edi,esi
  3974.      shl   edi,4
  3975.      add   edi,RESERVED_PORTS
  3976.      cmp   ebx,[edi+8]
  3977.      ja    rpal4
  3978.      cmp   ecx,[edi+4]
  3979.      jae   rpal1
  3980. ;     jb    rpal4
  3981. ;     jmp   rpal1
  3982.  rpal4:
  3983.  
  3984.      dec   esi
  3985.      jnz   rpal3
  3986.      jmp   rpal2
  3987.    rpal1:
  3988.      popad
  3989.      mov   eax,1
  3990.      ret
  3991.  
  3992.    rpal2:
  3993.      popad
  3994.  
  3995.  
  3996.      ; enable port access at port IO map
  3997.      cli
  3998.      pushad                        ; start enable io map
  3999.  
  4000.      cmp   ecx,65536 ;16384
  4001.      jae   no_unmask_io ; jge
  4002.  
  4003.      mov   eax,ebx
  4004.  
  4005.    new_port_access:
  4006.  
  4007.      pushad
  4008.  
  4009.      xor   ebp,ebp                ; enable - eax = port
  4010.      call  set_io_access_rights
  4011.  
  4012.      popad
  4013.  
  4014.      inc   eax
  4015.      cmp   eax,ecx
  4016.      jbe   new_port_access
  4017.  
  4018.    no_unmask_io:
  4019.  
  4020.      popad                         ; end enable io map
  4021.      sti
  4022.  
  4023.      mov   edi,[RESERVED_PORTS]
  4024.      add   edi,1
  4025.      mov   [RESERVED_PORTS],edi
  4026.      shl   edi,4
  4027.      add   edi,RESERVED_PORTS
  4028.      mov   esi,[TASK_BASE]
  4029.      mov   esi,[esi+TASKDATA.pid]
  4030.      mov   [edi],esi
  4031.      mov   [edi+4],ebx
  4032.      mov   [edi+8],ecx
  4033.  
  4034.      xor   eax, eax
  4035.      ret
  4036.  
  4037. free_port_area:
  4038.  
  4039.      pushad
  4040.  
  4041.      mov   esi,[RESERVED_PORTS]     ; no reserved areas ?
  4042.      test  esi,esi
  4043.      je    frpal2
  4044.      mov   edx,[TASK_BASE]
  4045.      mov   edx,[edx+TASKDATA.pid]
  4046.    frpal3:
  4047.      mov   edi,esi
  4048.      shl   edi,4
  4049.      add   edi,RESERVED_PORTS
  4050.      cmp   edx,[edi]
  4051.      jne   frpal4
  4052.      cmp   ebx,[edi+4]
  4053.      jne   frpal4
  4054.      cmp   ecx,[edi+8]
  4055.      jne   frpal4
  4056.      jmp   frpal1
  4057.    frpal4:
  4058.      dec   esi
  4059.      jnz   frpal3
  4060.    frpal2:
  4061.      popad
  4062.      mov   eax,1
  4063.      ret
  4064.    frpal1:
  4065.      mov   ecx,256
  4066.      sub   ecx,esi
  4067.      shl   ecx,4
  4068.      mov   esi,edi
  4069.      add   esi,16
  4070.      cld
  4071.      rep   movsb
  4072.  
  4073.      dec   dword [RESERVED_PORTS]
  4074.  
  4075.      popad
  4076.  
  4077.  
  4078.      ; disable port access at port IO map
  4079.  
  4080.      pushad                        ; start disable io map
  4081.  
  4082.      cmp   ecx,65536 ;16384
  4083.      jge   no_mask_io
  4084.  
  4085.      mov   eax,ebx
  4086.  
  4087.    new_port_access_disable:
  4088.  
  4089.      pushad
  4090.  
  4091.      mov   ebp,1                  ; disable - eax = port
  4092.      call  set_io_access_rights
  4093.  
  4094.      popad
  4095.  
  4096.      inc   eax
  4097.      cmp   eax,ecx
  4098.      jbe   new_port_access_disable
  4099.  
  4100.    no_mask_io:
  4101.  
  4102.      popad                         ; end disable io map
  4103.  
  4104.      xor   eax, eax
  4105.      ret
  4106.  
  4107.  
  4108. reserve_free_irq:
  4109.  
  4110.      xor   esi, esi
  4111.      inc   esi
  4112.      cmp   ecx, 16
  4113.      jae   ril1
  4114.  
  4115.      push  ecx
  4116.      lea   ecx, [irq_owner + 4 * ecx]
  4117.      mov   edx, [ecx]
  4118.      mov   eax, [TASK_BASE]
  4119.      mov   edi, [eax + TASKDATA.pid]
  4120.      pop   eax
  4121.      dec   ebx
  4122.      jnz   reserve_irq
  4123.  
  4124.      cmp   edx, edi
  4125.      jne   ril1
  4126.      dec   esi
  4127.      mov   [ecx], esi
  4128.  
  4129.      jmp   ril1
  4130.  
  4131.   reserve_irq:
  4132.  
  4133.      cmp   dword [ecx], 0
  4134.      jne   ril1
  4135.  
  4136.      mov   ebx, [f_irqs + 4 * eax]
  4137.  
  4138.      stdcall attach_int_handler, eax, ebx, dword 0
  4139.  
  4140.      mov   [ecx], edi
  4141.  
  4142.      dec   esi
  4143.    ril1:
  4144.      mov   [esp+32], esi ; return in eax
  4145.      ret
  4146.  
  4147. iglobal
  4148. f_irqs:
  4149.      dd 0x0
  4150.      dd 0x0
  4151.      dd p_irq2
  4152.      dd p_irq3
  4153.      dd p_irq4
  4154.      dd p_irq5
  4155.      dd p_irq6
  4156.      dd p_irq7
  4157.      dd p_irq8
  4158.      dd p_irq9
  4159.      dd p_irq10
  4160.      dd p_irq11
  4161.      dd 0x0
  4162.      dd 0x0
  4163.      dd p_irq14
  4164.      dd p_irq15
  4165.  
  4166. endg
  4167.  
  4168. drawbackground:
  4169.        inc   [mouse_pause]
  4170.        cmp   [SCR_MODE],word 0x12
  4171.        je   dbrv20
  4172.      dbrv12:
  4173.        cmp  [SCR_MODE],word 0100000000000000b
  4174.        jge  dbrv20
  4175.        cmp  [SCR_MODE],word 0x13
  4176.        je   dbrv20
  4177.        call  vesa12_drawbackground
  4178.        dec   [mouse_pause]
  4179.        call   [draw_pointer]
  4180.        ret
  4181.      dbrv20:
  4182.        cmp   [BgrDrawMode],dword 1
  4183.        jne   bgrstr
  4184.        call  vesa20_drawbackground_tiled
  4185.        dec   [mouse_pause]
  4186.        call   [draw_pointer]
  4187.        ret
  4188.      bgrstr:
  4189.        call  vesa20_drawbackground_stretch
  4190.        dec   [mouse_pause]
  4191.        call   [draw_pointer]
  4192.        ret
  4193.  
  4194. align 4
  4195.  
  4196. syscall_putimage:                       ; PutImage
  4197. sys_putimage:
  4198.      test  ecx,0x80008000
  4199.      jnz   .exit
  4200.      test  ecx,0x0000FFFF
  4201.      jz    .exit
  4202.      test  ecx,0xFFFF0000
  4203.      jnz   @f
  4204.   .exit:
  4205.      ret
  4206.  @@:
  4207.         mov     edi,[current_slot]
  4208.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4209.         rol     edx,16
  4210.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4211.         rol     edx,16
  4212.   .forced:
  4213.         push    ebp esi 0
  4214.         mov     ebp, putimage_get24bpp
  4215.         mov     esi, putimage_init24bpp
  4216. sys_putimage_bpp:
  4217. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4218. ;        mov     eax, vga_putimage
  4219.         cmp     [SCR_MODE], word 0x12
  4220.         jz      @f   ;.doit
  4221.         mov     eax, vesa12_putimage
  4222.         cmp     [SCR_MODE], word 0100000000000000b
  4223.         jae     @f
  4224.         cmp     [SCR_MODE], word 0x13
  4225.         jnz     .doit
  4226. @@:
  4227.         mov     eax, vesa20_putimage
  4228. .doit:
  4229.         inc     [mouse_pause]
  4230.         call    eax
  4231.         dec     [mouse_pause]
  4232.         pop     ebp esi ebp
  4233.         jmp     [draw_pointer]
  4234.  
  4235. syscall_putimage_palette:
  4236.         mov     edi, esi
  4237.         mov     esi, edx
  4238.         mov     edx, ecx
  4239.         mov     ecx, ebx
  4240.         mov     ebx, eax
  4241. sys_putimage_palette:
  4242. ; ebx = pointer to image
  4243. ; ecx = [xsize]*65536 + [ysize]
  4244. ; edx = [xstart]*65536 + [ystart]
  4245. ; esi = number of bits per pixel, must be 8, 24 or 32
  4246. ; edi = pointer to palette
  4247. ; ebp = row delta
  4248.         mov     eax, [CURRENT_TASK]
  4249.         shl     eax, 8
  4250.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4251.         rol     edx, 16
  4252.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4253.         rol     edx, 16
  4254. .forced:
  4255.         push    ebp esi ebp
  4256.         cmp     esi, 8
  4257.         jnz     @f
  4258.         mov     ebp, putimage_get8bpp
  4259.         mov     esi, putimage_init8bpp
  4260.         jmp     sys_putimage_bpp
  4261. @@:
  4262.         cmp     esi, 16
  4263.         jnz     @f
  4264.         mov     ebp, putimage_get16bpp
  4265.         mov     esi, putimage_init16bpp
  4266.         jmp     sys_putimage_bpp
  4267. @@:
  4268.         cmp     esi, 24
  4269.         jnz     @f
  4270.         mov     ebp, putimage_get24bpp
  4271.         mov     esi, putimage_init24bpp
  4272.         jmp     sys_putimage_bpp
  4273. @@:
  4274.         cmp     esi, 32
  4275.         jnz     @f
  4276.         mov     ebp, putimage_get32bpp
  4277.         mov     esi, putimage_init32bpp
  4278.         jmp     sys_putimage_bpp
  4279. @@:
  4280.         pop     ebp esi ebp
  4281.         ret
  4282.  
  4283. putimage_init24bpp:
  4284.         lea     eax, [eax*3]
  4285. putimage_init8bpp:
  4286.         ret
  4287.  
  4288. putimage_get24bpp:
  4289.         mov     eax, [esi]
  4290.         add     esi, 3
  4291.         ret     4
  4292. putimage_get8bpp:
  4293.         movzx   eax, byte [esi]
  4294.         push    edx
  4295.         mov     edx, [esp+8]
  4296.         mov     eax, [edx+eax*4]
  4297.         pop     edx
  4298.         inc     esi
  4299.         ret     4
  4300.  
  4301. putimage_init32bpp:
  4302.         shl     eax, 2
  4303.         ret
  4304. putimage_get32bpp:
  4305.         lodsd
  4306.         ret     4
  4307.  
  4308. putimage_init16bpp:
  4309.         add     eax, eax
  4310.         ret
  4311. putimage_get16bpp:
  4312. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4313.         push    ecx edx
  4314.         movzx   eax, word [esi]
  4315.         add     esi, 2
  4316.         mov     ecx, eax
  4317.         mov     edx, eax
  4318.         and     eax, 0x1F
  4319.         and     ecx, 0x1F shl 5
  4320.         and     edx, 0x1F shl 10
  4321.         shl     eax, 3
  4322.         shl     ecx, 6
  4323.         shl     edx, 9
  4324.         or      eax, ecx
  4325.         or      eax, edx
  4326.         pop     edx ecx
  4327.         ret
  4328.  
  4329. ; eax x beginning
  4330. ; ebx y beginning
  4331. ; ecx x end
  4332.         ; edx y end
  4333. ; edi color
  4334.  
  4335. __sys_drawbar:
  4336.         mov     esi,[current_slot]
  4337.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4338.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4339.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4340.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4341.   .forced:
  4342.     inc   [mouse_pause]
  4343. ;        call    [disable_mouse]
  4344.     cmp   [SCR_MODE],word 0x12
  4345.     je   dbv20
  4346.    sdbv20:
  4347.     cmp  [SCR_MODE],word 0100000000000000b
  4348.     jge  dbv20
  4349.     cmp  [SCR_MODE],word 0x13
  4350.     je   dbv20
  4351.     call vesa12_drawbar
  4352.     dec   [mouse_pause]
  4353.     call   [draw_pointer]
  4354.     ret
  4355.   dbv20:
  4356.     call vesa20_drawbar
  4357.     dec   [mouse_pause]
  4358.     call   [draw_pointer]
  4359.     ret
  4360.  
  4361.  
  4362.  
  4363. kb_read:
  4364.  
  4365.         push    ecx edx
  4366.  
  4367.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4368.       kr_loop:
  4369.         in      al,0x64
  4370.         test    al,1
  4371.         jnz     kr_ready
  4372.         loop    kr_loop
  4373.         mov     ah,1
  4374.         jmp     kr_exit
  4375.       kr_ready:
  4376.         push    ecx
  4377.         mov     ecx,32
  4378.       kr_delay:
  4379.         loop    kr_delay
  4380.         pop     ecx
  4381.         in      al,0x60
  4382.         xor     ah,ah
  4383.       kr_exit:
  4384.  
  4385.         pop     edx ecx
  4386.  
  4387.         ret
  4388.  
  4389.  
  4390. kb_write:
  4391.  
  4392.         push    ecx edx
  4393.  
  4394.         mov     dl,al
  4395. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4396. ;      kw_loop1:
  4397. ;        in      al,0x64
  4398. ;        test    al,0x20
  4399. ;        jz      kw_ok1
  4400. ;        loop    kw_loop1
  4401. ;        mov     ah,1
  4402. ;        jmp     kw_exit
  4403. ;      kw_ok1:
  4404.         in      al,0x60
  4405.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4406.       kw_loop:
  4407.         in      al,0x64
  4408.         test    al,2
  4409.         jz      kw_ok
  4410.         loop    kw_loop
  4411.         mov     ah,1
  4412.         jmp     kw_exit
  4413.       kw_ok:
  4414.         mov     al,dl
  4415.         out     0x60,al
  4416.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4417.       kw_loop3:
  4418.         in      al,0x64
  4419.         test    al,2
  4420.         jz      kw_ok3
  4421.         loop    kw_loop3
  4422.         mov     ah,1
  4423.         jmp     kw_exit
  4424.       kw_ok3:
  4425.         mov     ah,8
  4426.       kw_loop4:
  4427.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4428.       kw_loop5:
  4429.         in      al,0x64
  4430.         test    al,1
  4431.         jnz     kw_ok4
  4432.         loop    kw_loop5
  4433.         dec     ah
  4434.         jnz     kw_loop4
  4435.       kw_ok4:
  4436.         xor     ah,ah
  4437.       kw_exit:
  4438.  
  4439.         pop     edx ecx
  4440.  
  4441.         ret
  4442.  
  4443.  
  4444. kb_cmd:
  4445.  
  4446.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4447.       c_wait:
  4448.         in      al,0x64
  4449.         test    al,2
  4450.         jz      c_send
  4451.         loop    c_wait
  4452.         jmp     c_error
  4453.       c_send:
  4454.         mov     al,bl
  4455.         out     0x64,al
  4456.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4457.       c_accept:
  4458.         in      al,0x64
  4459.         test    al,2
  4460.         jz      c_ok
  4461.         loop    c_accept
  4462.       c_error:
  4463.         mov     ah,1
  4464.         jmp     c_exit
  4465.       c_ok:
  4466.         xor     ah,ah
  4467.       c_exit:
  4468.         ret
  4469.  
  4470.  
  4471. setmouse:  ; set mousepicture -pointer
  4472.            ; ps2 mouse enable
  4473.  
  4474.      mov     [MOUSE_PICTURE],dword mousepointer
  4475.  
  4476.      cli
  4477.  
  4478.      ret
  4479.  
  4480.  
  4481. _rdtsc:
  4482.      bt [cpu_caps], CAPS_TSC
  4483.      jnc ret_rdtsc
  4484.      rdtsc
  4485.      ret
  4486.    ret_rdtsc:
  4487.      mov   edx,0xffffffff
  4488.      mov   eax,0xffffffff
  4489.      ret
  4490.  
  4491. rerouteirqs:
  4492.  
  4493.         mov     al,0x11         ;  icw4, edge triggered
  4494.         out     0x20,al
  4495.         call    pic_delay
  4496.         out     0xA0,al
  4497.         call    pic_delay
  4498.  
  4499.         mov     al,0x20         ;  generate 0x20 +
  4500.         out     0x21,al
  4501.         call    pic_delay
  4502.         mov     al,0x28         ;  generate 0x28 +
  4503.         out     0xA1,al
  4504.         call    pic_delay
  4505.  
  4506.         mov     al,0x04         ;  slave at irq2
  4507.         out     0x21,al
  4508.         call    pic_delay
  4509.         mov     al,0x02         ;  at irq9
  4510.         out     0xA1,al
  4511.         call    pic_delay
  4512.  
  4513.         mov     al,0x01         ;  8086 mode
  4514.         out     0x21,al
  4515.         call    pic_delay
  4516.         out     0xA1,al
  4517.         call    pic_delay
  4518.  
  4519.         mov     al,255          ; mask all irq's
  4520.         out     0xA1,al
  4521.         call    pic_delay
  4522.         out     0x21,al
  4523.         call    pic_delay
  4524.  
  4525.         mov     ecx,0x1000
  4526.         cld
  4527. picl1:  call    pic_delay
  4528.         loop    picl1
  4529.  
  4530.         mov     al,255          ; mask all irq's
  4531.         out     0xA1,al
  4532.         call    pic_delay
  4533.         out     0x21,al
  4534.         call    pic_delay
  4535.  
  4536.         ret
  4537.  
  4538.  
  4539. pic_delay:
  4540.  
  4541.         jmp     pdl1
  4542. pdl1:   ret
  4543.  
  4544.  
  4545. sys_msg_board_str:
  4546.  
  4547.      pushad
  4548.    @@:
  4549.      cmp    [esi],byte 0
  4550.      je     @f
  4551.      mov    eax,1
  4552.      movzx  ebx,byte [esi]
  4553.      call   sys_msg_board
  4554.      inc    esi
  4555.      jmp    @b
  4556.    @@:
  4557.      popad
  4558.      ret
  4559.  
  4560. sys_msg_board_byte:
  4561. ; in: al = byte to display
  4562. ; out: nothing
  4563. ; destroys: nothing
  4564.         pushad
  4565.         mov     ecx, 2
  4566.         shl     eax, 24
  4567.         jmp     @f
  4568.  
  4569. sys_msg_board_word:
  4570. ; in: ax = word to display
  4571. ; out: nothing
  4572. ; destroys: nothing
  4573.         pushad
  4574.         mov     ecx, 4
  4575.         shl     eax, 16
  4576.         jmp     @f
  4577.  
  4578. sys_msg_board_dword:
  4579. ; in: eax = dword to display
  4580. ; out: nothing
  4581. ; destroys: nothing
  4582.         pushad
  4583.         mov     ecx, 8
  4584. @@:
  4585.         push    ecx
  4586.         rol     eax, 4
  4587.         push    eax
  4588.         and     al, 0xF
  4589.         cmp     al, 10
  4590.         sbb     al, 69h
  4591.         das
  4592.         mov     bl, al
  4593.         xor     eax, eax
  4594.         inc     eax
  4595.         call    sys_msg_board
  4596.         pop     eax
  4597.         pop     ecx
  4598.         loop    @b
  4599.         popad
  4600.         ret
  4601.  
  4602. uglobal
  4603.   msg_board_data: times 4096 db 0
  4604.   msg_board_count dd 0x0
  4605. endg
  4606.  
  4607. sys_msg_board:
  4608.  
  4609. ; eax=1 : write :  bl byte to write
  4610. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4611.  
  4612.         mov     ecx, [msg_board_count]
  4613.         cmp     eax, 1
  4614.         jne     .smbl1
  4615.  
  4616.  
  4617.         mov     [msg_board_data+ecx],bl
  4618.         inc     ecx
  4619.         and     ecx, 4095
  4620.         mov     [msg_board_count], ecx
  4621.         mov     [check_idle_semaphore], 5
  4622.         ret
  4623. .smbl1:
  4624.         cmp     eax, 2
  4625.         jne     .smbl2
  4626.         test    ecx, ecx
  4627.         jz      .smbl21
  4628.         mov     eax, msg_board_data+1
  4629.         mov     ebx, msg_board_data
  4630.         movzx   edx, byte [ebx]
  4631.         call    memmove
  4632.         dec     [msg_board_count]
  4633.         mov     [esp + 36], edx ;eax
  4634.         mov     [esp + 24], dword 1
  4635.         ret
  4636. .smbl21:
  4637.         mov     [esp+36], ecx
  4638.         mov     [esp+24], ecx
  4639. .smbl2:
  4640.         ret
  4641.  
  4642.  
  4643.  
  4644. sys_process_def:
  4645.         mov     edi, [CURRENT_TASK]
  4646.  
  4647.         dec     eax             ; 1 = set keyboard mode
  4648.      jne   no_set_keyboard_setup
  4649.  
  4650.      shl   edi,8
  4651.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4652.  
  4653.      ret
  4654.  
  4655.    no_set_keyboard_setup:
  4656.  
  4657.         dec     eax             ; 2 = get keyboard mode
  4658.      jne   no_get_keyboard_setup
  4659.  
  4660.      shl   edi,8
  4661.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4662.  
  4663.      mov   [esp+36],eax
  4664.  
  4665.      ret
  4666.  
  4667.    no_get_keyboard_setup:
  4668.  
  4669.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4670.      jne   no_get_keyboard_cas
  4671.  
  4672. ;     xor   eax,eax
  4673. ;     movzx eax,byte [shift]
  4674. ;     movzx ebx,byte [ctrl]
  4675. ;     shl   ebx,2
  4676. ;     add   eax,ebx
  4677. ;     movzx ebx,byte [alt]
  4678. ;     shl   ebx,3
  4679. ;     add   eax,ebx
  4680.  
  4681.  ;// mike.dld [
  4682.      mov   eax, [kb_state]
  4683.  ;// mike.dld ]
  4684.  
  4685.      mov   [esp+36],eax
  4686.  
  4687.      ret
  4688.  
  4689.    no_get_keyboard_cas:
  4690.  
  4691.         dec     eax
  4692.         jnz     no_add_keyboard_hotkey
  4693.  
  4694.         mov     eax, hotkey_list
  4695. @@:
  4696.         cmp     dword [eax+8], 0
  4697.         jz      .found_free
  4698.         add     eax, 16
  4699.         cmp     eax, hotkey_list+16*256
  4700.         jb      @b
  4701.         mov     dword [esp+36], 1
  4702.         ret
  4703. .found_free:
  4704.         mov     [eax+8], edi
  4705.         mov     [eax+4], ecx
  4706.         movzx   ebx, bl
  4707.         lea     ebx, [hotkey_scancodes+ebx*4]
  4708.         mov     ecx, [ebx]
  4709.         mov     [eax], ecx
  4710.         mov     [ebx], eax
  4711.         mov     [eax+12], ebx
  4712.         jecxz   @f
  4713.         mov     [ecx+12], eax
  4714. @@:
  4715.         and     dword [esp+36], 0
  4716.         ret
  4717.  
  4718. no_add_keyboard_hotkey:
  4719.  
  4720.         dec     eax
  4721.         jnz     no_del_keyboard_hotkey
  4722.  
  4723.         movzx   ebx, bl
  4724.         lea     ebx, [hotkey_scancodes+ebx*4]
  4725.         mov     eax, [ebx]
  4726. .scan:
  4727.         test    eax, eax
  4728.         jz      .notfound
  4729.         cmp     [eax+8], edi
  4730.         jnz     .next
  4731.         cmp     [eax+4], ecx
  4732.         jz      .found
  4733. .next:
  4734.         mov     eax, [eax]
  4735.         jmp     .scan
  4736. .notfound:
  4737.         mov     dword [esp+36], 1
  4738.         ret
  4739. .found:
  4740.         mov     ecx, [eax]
  4741.         jecxz   @f
  4742.         mov     edx, [eax+12]
  4743.         mov     [ecx+12], edx
  4744. @@:
  4745.         mov     ecx, [eax+12]
  4746.         mov     edx, [eax]
  4747.         mov     [ecx], edx
  4748.         xor     edx, edx
  4749.         mov     [eax+4], edx
  4750.         mov     [eax+8], edx
  4751.         mov     [eax+12], edx
  4752.         mov     [eax], edx
  4753.         mov     [esp+36], edx
  4754.         ret
  4755.  
  4756. no_del_keyboard_hotkey:
  4757.      ret
  4758.  
  4759.  
  4760. align 4
  4761.  
  4762. sys_gs:                         ; direct screen access
  4763.  
  4764.      cmp  eax,1                 ; resolution
  4765.      jne  no_gs1
  4766.      mov  eax,[Screen_Max_X]
  4767.      shl  eax,16
  4768.      mov  ax,[Screen_Max_Y]
  4769.      add  eax,0x00010001
  4770.      mov  [esp+36],eax
  4771.      ret
  4772.    no_gs1:
  4773.  
  4774.      cmp   eax,2                ; bits per pixel
  4775.      jne   no_gs2
  4776.      movzx eax,byte [ScreenBPP]
  4777.      mov   [esp+36],eax
  4778.      ret
  4779.    no_gs2:
  4780.  
  4781.      cmp   eax,3                ; bytes per scanline
  4782.      jne   no_gs3
  4783.      mov   eax,[BytesPerScanLine]
  4784.      mov   [esp+36],eax
  4785.      ret
  4786.    no_gs3:
  4787.  
  4788.      mov  [esp+36],dword -1
  4789.      ret
  4790.  
  4791.  
  4792. align 4 ; PCI functions
  4793.  
  4794. sys_pci:
  4795.  
  4796.      call  pci_api
  4797.      mov   [esp+36],eax
  4798.      ret
  4799.  
  4800.  
  4801. align 4  ;  system functions
  4802.  
  4803. syscall_setpixel:                       ; SetPixel
  4804.  
  4805.         mov     eax, ebx
  4806.         mov     ebx, ecx
  4807.         mov     ecx, edx
  4808.         mov     edx, [TASK_BASE]
  4809.         add     eax, [edx-twdw+WDATA.box.left]
  4810.         add     ebx, [edx-twdw+WDATA.box.top]
  4811.         mov     edi, [current_slot]
  4812.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4813.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4814.         xor     edi, edi ; no force
  4815. ;       mov     edi, 1
  4816.         call    [disable_mouse]
  4817.         jmp     [putpixel]
  4818.  
  4819. align 4
  4820.  
  4821. syscall_writetext:                      ; WriteText
  4822.  
  4823.         mov   eax,[TASK_BASE]
  4824.         mov   ebp,[eax-twdw+WDATA.box.left]
  4825.         push  esi
  4826.         mov   esi,[current_slot]
  4827.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4828.         shl   ebp,16
  4829.         add   ebp,[eax-twdw+WDATA.box.top]
  4830.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4831.         pop   esi
  4832.         add   ebx,ebp
  4833.         mov   eax,edi
  4834.         xor   edi,edi
  4835.         jmp   dtext
  4836.  
  4837. align 4
  4838.  
  4839. syscall_openramdiskfile:                ; OpenRamdiskFile
  4840.  
  4841.         mov     eax, ebx
  4842.         mov     ebx, ecx
  4843.         mov     ecx, edx
  4844.         mov     edx, esi
  4845.         mov     esi, 12
  4846.         call    fileread
  4847.         mov     [esp+32], eax
  4848.         ret
  4849.  
  4850. align 4
  4851.  
  4852. syscall_drawrect:                       ; DrawRect
  4853.  
  4854.         mov     edi, edx ; color + gradient
  4855.         and     edi, 0x80FFFFFF
  4856.         test    bx, bx  ; x.size
  4857.         je      .drectr
  4858.         test    cx, cx ; y.size
  4859.         je      .drectr
  4860.  
  4861.         mov     eax, ebx ; bad idea
  4862.         mov     ebx, ecx
  4863.  
  4864.         movzx   ecx, ax ; ecx - x.size
  4865.         shr     eax, 16 ; eax - x.coord
  4866.         movzx   edx, bx ; edx - y.size
  4867.         shr     ebx, 16 ; ebx - y.coord
  4868.         mov     esi, [current_slot]
  4869.  
  4870.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4871.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4872.         add     ecx, eax
  4873.         add     edx, ebx
  4874.         jmp     [drawbar]
  4875. .drectr:
  4876.         ret
  4877.  
  4878. align 4
  4879. syscall_getscreensize:                  ; GetScreenSize
  4880.         mov     ax, [Screen_Max_X]
  4881.         shl     eax, 16
  4882.         mov     ax, [Screen_Max_Y]
  4883.         mov     [esp + 32], eax
  4884.         ret
  4885.  
  4886. align 4
  4887.  
  4888. syscall_cdaudio:                        ; CD
  4889.  
  4890.         cmp     eax, 4
  4891.         jb      .audio
  4892.         jz      .eject
  4893.         cmp     eax, 5
  4894.         jnz     .ret
  4895. .load:
  4896.         call    .reserve
  4897.         call    LoadMedium
  4898.         call    .free
  4899.         ret
  4900. .eject:
  4901.         call    .reserve
  4902.         call    clear_CD_cache
  4903.         call    allow_medium_removal
  4904.         call    EjectMedium
  4905.         call    .free
  4906.         ret
  4907. .audio:
  4908.      call  sys_cd_audio
  4909.      mov   [esp+36],eax
  4910. .ret:
  4911.      ret
  4912.  
  4913. .reserve:
  4914.         call    reserve_cd
  4915.         mov     eax, ebx
  4916.         shr     eax, 1
  4917.         and     eax, 1
  4918.         inc     eax
  4919.         mov     [ChannelNumber], ax
  4920.         mov     eax, ebx
  4921.         and     eax, 1
  4922.         mov     [DiskNumber], al
  4923.         call    reserve_cd_channel
  4924.         and     ebx, 3
  4925.         inc     ebx
  4926.         mov     [cdpos], ebx
  4927.         add     ebx, ebx
  4928.         mov     cl, 8
  4929.         sub     cl, bl
  4930.         mov     al, [DRIVE_DATA+1]
  4931.         shr     al, cl
  4932.         test    al, 2
  4933.         jz      .err
  4934.         ret
  4935. .free:
  4936.         call    free_cd_channel
  4937.         and     [cd_status], 0
  4938.         ret
  4939. .err:
  4940.         call    .free
  4941.         pop     eax
  4942.         ret
  4943.  
  4944. align 4
  4945.  
  4946. syscall_getpixel:                       ; GetPixel
  4947.      mov   ecx, [Screen_Max_X]
  4948.      inc   ecx
  4949.      xor   edx, edx
  4950.      mov   eax, ebx
  4951.      div   ecx
  4952.      mov   ebx, edx
  4953.      xchg  eax, ebx
  4954.      call  dword [GETPIXEL] ; eax - x, ebx - y
  4955.      mov   [esp + 32], ecx
  4956.      ret
  4957.  
  4958.  
  4959. align 4
  4960.  
  4961. syscall_drawline:                       ; DrawLine
  4962.  
  4963.         mov     edi, [TASK_BASE]
  4964.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4965.         mov     ebp, eax
  4966.         mov     esi, [current_slot]
  4967.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4968.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4969.         add     ebp,ebx
  4970.         shl     eax, 16
  4971.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4972.         add     eax, ebp
  4973.         mov     ebp, ebx
  4974.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4975.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4976.         add     ebp, ecx
  4977.         shl     ebx, 16
  4978.         xor     edi, edi
  4979.         add     ebx, ebp
  4980.         mov     ecx, edx
  4981.         jmp     [draw_line]
  4982.  
  4983. align 4
  4984.  
  4985. syscall_getirqowner:                    ; GetIrqOwner
  4986.  
  4987.      cmp   ebx,16
  4988.      jae   .err
  4989.  
  4990.      cmp   [irq_rights + 4 * ebx], dword 2
  4991.      je    .err
  4992.  
  4993.      mov   eax,[4 * ebx + irq_owner]
  4994.      mov   [esp+32],eax
  4995.  
  4996.      ret
  4997. .err:
  4998.      or    dword [esp+32], -1
  4999.      ret
  5000.  
  5001. align 4
  5002.  
  5003. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5004.  
  5005.      call  r_f_port_area
  5006.      mov   [esp+36],eax
  5007.      ret
  5008.  
  5009. align 4
  5010.  
  5011. syscall_threads:                        ; CreateThreads
  5012.  
  5013.      call  sys_threads
  5014.      mov   [esp+36],eax
  5015.      ret
  5016.  
  5017. align 4
  5018.  
  5019. stack_driver_stat:
  5020.  
  5021.      call  app_stack_handler            ; Stack status
  5022.  
  5023. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5024. ;     call  change_task                 ; between sent packet
  5025.  
  5026.      mov   [esp+36],eax
  5027.      ret
  5028.  
  5029. align 4
  5030.  
  5031. socket:                                 ; Socket interface
  5032.      call  app_socket_handler
  5033.  
  5034. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5035. ;     call  change_task                 ; between sent packet
  5036.  
  5037.      mov   [esp+36],eax
  5038.      mov   [esp+24],ebx
  5039.      ret
  5040.  
  5041. align 4
  5042.  
  5043. read_from_hd:                           ; Read from hd - fn not in use
  5044.  
  5045.      mov   edi,[TASK_BASE]
  5046.      add   edi,TASKDATA.mem_start
  5047.      add   eax,[edi]
  5048.      add   ecx,[edi]
  5049.      add   edx,[edi]
  5050.      call  file_read
  5051.  
  5052.      mov   [esp+36],eax
  5053.      mov   [esp+24],ebx
  5054.  
  5055.      ret
  5056.  
  5057. paleholder:
  5058.         ret
  5059.  
  5060. align 4
  5061. set_screen:
  5062.         cmp eax, [Screen_Max_X]
  5063.         jne .set
  5064.  
  5065.         cmp edx, [Screen_Max_Y]
  5066.         jne .set
  5067.         ret
  5068. .set:
  5069.         pushfd
  5070.         cli
  5071.  
  5072.         mov [Screen_Max_X], eax
  5073.         mov [Screen_Max_Y], edx
  5074.  
  5075.         mov [screen_workarea.right],eax
  5076.         mov [screen_workarea.bottom], edx
  5077.         inc eax
  5078.         shl eax, 2                      ;32 bpp
  5079.         mov [BytesPerScanLine], eax
  5080.         push ebx
  5081.         push esi
  5082.         push edi
  5083.         call    repos_windows
  5084.         mov     eax, 0
  5085.         mov     ebx, 0
  5086.         mov     ecx, [Screen_Max_X]
  5087.         mov     edx, [Screen_Max_Y]
  5088.         call    calculatescreen
  5089.         pop edi
  5090.         pop esi
  5091.         pop ebx
  5092.  
  5093.         popfd
  5094.         ret
  5095.  
  5096. ; --------------- APM ---------------------
  5097. apm_entry    dp    0
  5098. apm_vf        dd    0
  5099. align 4
  5100. sys_apm:
  5101.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5102.     jne    @f
  5103.     or    [esp + 56], byte 1    ; error
  5104.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5105.     ret
  5106.  
  5107. @@:
  5108.     xchg    eax, ecx
  5109.     xchg    ebx, ecx
  5110.  
  5111.     cmp    al, 3
  5112.     ja    @f
  5113.     and    [esp + 56], byte 0xfe    ; emulate func 0..3 as func 0
  5114.     mov    eax, [apm_vf]
  5115.     mov    [esp + 36], eax
  5116.     shr    eax, 16
  5117.     mov    [esp + 32], eax
  5118.     ret
  5119.  
  5120. @@:
  5121.  
  5122.     mov esi, [master_tab+(OS_BASE shr 20)]
  5123.     xchg [master_tab], esi
  5124.     push esi
  5125.     mov edi, cr3
  5126.     mov cr3, edi                 ;flush TLB
  5127.  
  5128.     call    pword [apm_entry]    ; call APM BIOS
  5129.  
  5130.     xchg eax, [esp]
  5131.     mov [master_tab], eax
  5132.     mov eax, cr3
  5133.     mov cr3, eax
  5134.     pop eax
  5135.  
  5136.     mov    [esp + 8 ], edi
  5137.     mov    [esp + 12], esi
  5138.     mov    [esp + 24], ebx
  5139.     mov    [esp + 28], edx
  5140.     mov    [esp + 32], ecx
  5141.     mov    [esp + 36], eax
  5142.     setc    al
  5143.     and    [esp + 56], byte 0xfe
  5144.     or    [esp + 56], al
  5145.  
  5146.  
  5147.     ret
  5148. ; -----------------------------------------
  5149.  
  5150. align 4
  5151.  
  5152. undefined_syscall:                      ; Undefined system call
  5153.      mov   [esp + 32], dword -1
  5154.      ret
  5155.  
  5156. align 4
  5157. system_shutdown:          ; shut down the system
  5158.  
  5159.            cmp byte [BOOT_VAR+0x9030], 1
  5160.            jne @F
  5161.            ret
  5162. @@:
  5163.            call stop_all_services
  5164.            push 3                ; stop playing cd
  5165.            pop  eax
  5166.            call sys_cd_audio
  5167.  
  5168. yes_shutdown_param:
  5169.  
  5170.            cli
  5171.  
  5172.            cmp byte [BOOT_VAR+0x9030], 3
  5173.            je _sys_reboot
  5174.  
  5175.            cmp byte [BOOT_VAR+0x9030], 4
  5176.            je _sys_restart
  5177.  
  5178.            cld
  5179.            mov  esi, BOOT_VAR    ; restore 0x0 - 0xffff
  5180.            mov  edi, OS_BASE
  5181.            mov  ecx,0x10000/4
  5182.            rep movsd
  5183.  
  5184.            mov esi, _16bit_start + OS_BASE
  5185.            mov ecx, _16bit_end
  5186.            shr ecx, 2
  5187.            mov edi, _16BIT_BASE + OS_BASE
  5188.            rep movsd
  5189.  
  5190.            mov dword [_sys_pdbr],   PG_LARGE+PG_SW
  5191.            mov eax, _sys_pdbr + (0x100000000-OS_BASE)
  5192.            mov cr3, eax
  5193.  
  5194.            jmp far 0x60:_poweroff;
  5195.  
  5196.  
  5197. _sys_restart:
  5198.  
  5199.            mov ax,os_stack
  5200.            mov dx,app_data
  5201.            mov ss,ax
  5202.            mov esp, __os_stack
  5203.  
  5204.            mov ds, dx
  5205.            mov es, dx
  5206.            mov fs, dx
  5207.            mov gs, dx
  5208.  
  5209.            call restorefatchain
  5210.  
  5211.            cld
  5212.            mov  esi, BOOT_VAR    ; restore 0x0 - 0xffff
  5213.            mov  edi, OS_BASE
  5214.            mov  ecx,0x10000/4
  5215.            rep movsd
  5216.  
  5217.            mov eax, [_copy_pg_balloc]
  5218.            mov [_pg_balloc], eax
  5219.  
  5220.            mov dword [_sys_pdbr],   PG_LARGE+PG_SW
  5221.            mov eax, _sys_pdbr + (0x100000000-OS_BASE)
  5222.            mov cr3, eax
  5223.  
  5224.            mov ecx, LAST_PAGE
  5225.            mov edi, cur_saved_data
  5226.            sub edi, OS_BASE
  5227.            sub ecx, edi
  5228.            shr ecx, 2
  5229.            xor eax, eax
  5230.            rep stosd
  5231.  
  5232.            call test_cpu
  5233.  
  5234.    ;        jmp __core_restart
  5235.  
  5236. _sys_reboot:
  5237.            mov  word [OS_BASE+0x467+0],0xFFF0
  5238.            mov  word [OS_BASE+0x467+2],0xF000
  5239.  
  5240.            mov  al,0x0F
  5241.            out  0x70,al
  5242.            mov  al,0x05
  5243.            out  0x71,al
  5244.  
  5245.            mov  al,0xFE
  5246.            out  0x64,al
  5247.  
  5248.            hlt
  5249.  
  5250. include "data32.inc"
  5251.  
  5252. __REV__ = __REV
  5253.  
  5254. ;uglobals_size = $ - endofcode
  5255. ;diff16 "end of kernel code",0,$
  5256.  
  5257.  
  5258.