Subversion Repositories Kolibri OS

Rev

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

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