Subversion Repositories Kolibri OS

Rev

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