Subversion Repositories Kolibri OS

Rev

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

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