Subversion Repositories Kolibri OS

Rev

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