Subversion Repositories Kolibri OS

Rev

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