Subversion Repositories Kolibri OS

Rev

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