Subversion Repositories Kolibri OS

Rev

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