Subversion Repositories Kolibri OS

Rev

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