Subversion Repositories Kolibri OS

Rev

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