Subversion Repositories Kolibri OS

Rev

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