Subversion Repositories Kolibri OS

Rev

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