Subversion Repositories Kolibri OS

Rev

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