Subversion Repositories Kolibri OS

Rev

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