Subversion Repositories Kolibri OS

Rev

Rev 652 | Rev 655 | 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: 653 $
  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   eax,10*65536
  896.          mov   ax,word [boot_y]
  897.          add   [boot_y],dword 10
  898.          mov   ebx,0x80ffffff   ; ASCIIZ string with white color
  899.          mov   ecx,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. ;        movzx eax,byte [0xf604]        ; mouse irq
  1038. ;        dec   eax
  1039. ;        add   eax,mouseirqtable
  1040. ;        movzx eax,byte [eax]
  1041. ;        shl   eax,2
  1042. ;        mov   [irq_owner+eax],byte 1
  1043.  
  1044.  
  1045.                                        ; RESERVE PORTS
  1046.         mov   edi,1                    ; 0x00-0x2d
  1047.         mov   [RESERVED_PORTS],edi
  1048.         shl   edi,4
  1049.         mov   [RESERVED_PORTS+edi+0],dword 1
  1050.         mov   [RESERVED_PORTS+edi+4],dword 0x0
  1051.         mov   [RESERVED_PORTS+edi+8],dword 0x2d
  1052.  
  1053.         inc   dword [RESERVED_PORTS]          ; 0x30-0x4d
  1054.         mov   edi,[RESERVED_PORTS]
  1055.         shl   edi,4
  1056.         mov   [RESERVED_PORTS+edi+0],dword 1
  1057.         mov   [RESERVED_PORTS+edi+4],dword 0x30
  1058.         mov   [RESERVED_PORTS+edi+8],dword 0x4d
  1059.  
  1060.         inc   dword [RESERVED_PORTS]          ; 0x50-0xdf
  1061.         mov   edi,[RESERVED_PORTS]
  1062.         shl   edi,4
  1063.         mov   [RESERVED_PORTS+edi+0],dword 1
  1064.         mov   [RESERVED_PORTS+edi+4],dword 0x50
  1065.         mov   [RESERVED_PORTS+edi+8],dword 0xdf
  1066.  
  1067.         inc   dword [RESERVED_PORTS]          ; 0xe5-0xff
  1068.         mov   edi,[RESERVED_PORTS]
  1069.         shl   edi,4
  1070.         mov   [RESERVED_PORTS+edi+0],dword 1
  1071.         mov   [RESERVED_PORTS+edi+4],dword 0xe5
  1072.         mov   [RESERVED_PORTS+edi+8],dword 0xff
  1073.  
  1074.  
  1075. ;        cmp   [0xf604],byte 2          ; com1 mouse -> 0x3f0-0x3ff
  1076. ;        jne   ripl1
  1077. ;        inc   dword [0x2d0000]
  1078. ;        mov   edi,[0x2d0000]
  1079. ;        shl   edi,4
  1080. ;        mov   [0x2d0000+edi+0],dword 1
  1081. ;        mov   [0x2d0000+edi+4],dword 0x3f0
  1082. ;        mov   [0x2d0000+edi+8],dword 0x3ff
  1083. ;      ripl1:
  1084. ;        cmp   [0xf604],byte 3          ; com2 mouse -> 0x2f0-0x2ff
  1085. ;        jne   ripl2
  1086. ;        inc   dword [0x2d0000]
  1087. ;        mov   edi,[0x2d0000]
  1088. ;        shl   edi,4
  1089. ;        mov   [0x2d0000+edi+0],dword 1
  1090. ;        mov   [0x2d0000+edi+4],dword 0x2f0
  1091. ;        mov   [0x2d0000+edi+8],dword 0x2ff
  1092. ;      ripl2:
  1093.  
  1094.         popad
  1095.         ret
  1096.  
  1097. iglobal
  1098. mouseirqtable   db  12    ; ps2
  1099.                 db  4     ; com1
  1100.                 db  3     ; com2
  1101. endg
  1102.  
  1103. setirqreadports:
  1104.  
  1105.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1106.         mov   [irq12read+4],dword 0                  ; end of port list
  1107.         mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1108.         mov   [irq04read+4],dword 0                  ; end of port list
  1109.         mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1110.         mov   [irq03read+4],dword 0                  ; end of port list
  1111.  
  1112.         ret
  1113.  
  1114. iglobal
  1115.   process_number dd 0x1
  1116. endg
  1117.  
  1118. set_variables:
  1119.  
  1120.         mov   ecx,0x100                       ; flush port 0x60
  1121. .fl60:  in    al,0x60
  1122.         loop  .fl60
  1123.         mov   [MOUSE_BUFF_COUNT],byte 0                 ; mouse buffer
  1124.         mov   [KEY_COUNT],byte 0                 ; keyboard buffer
  1125.         mov   [BTN_COUNT],byte 0                 ; button buffer
  1126. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1127.  
  1128.         push  eax
  1129.         mov   ax,[BOOT_VAR+0x900c]
  1130.         shr   ax,1
  1131.         shl   eax,16
  1132.         mov   ax,[BOOT_VAR+0x900A]
  1133.         shr   ax,1
  1134.         mov   [MOUSE_X],eax
  1135.         pop   eax
  1136.  
  1137.         mov   byte [SB16_Status],0            ; Minazzi Paolo
  1138.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  1139.  
  1140.      ;!! IP 04.02.2005:
  1141.         mov   [next_usage_update], 100
  1142.         mov   byte [DONT_SWITCH], 0 ; change task if possible
  1143.  
  1144.         ret
  1145.  
  1146. ;* mouse centered - start code- Mario79
  1147. mouse_centered:
  1148.         push  eax
  1149.         mov   eax,[ScreenWidth]
  1150.         shr   eax,1
  1151.         mov   [MOUSE_X],ax
  1152.         mov   eax,[ScreenHeight]
  1153.         shr   eax,1
  1154.         mov   [MOUSE_Y],ax
  1155.         pop   eax
  1156.         ret
  1157. ;* mouse centered - end code- Mario79
  1158.  
  1159. align 4
  1160.  
  1161. sys_outport:
  1162.  
  1163.     mov   edi,ebx          ; separate flag for read / write
  1164.     and   ebx,65535
  1165.  
  1166.     mov   ecx,[RESERVED_PORTS]
  1167.     test  ecx,ecx
  1168.     jne   sopl8
  1169.     mov   [esp+36],dword 1
  1170.     ret
  1171.  
  1172.   sopl8:
  1173.     mov   edx,[TASK_BASE]
  1174.     mov   edx,[edx+0x4]
  1175.     and   ebx,65535
  1176.     cld
  1177.   sopl1:
  1178.  
  1179.     mov   esi,ecx
  1180.     shl   esi,4
  1181.     add   esi,RESERVED_PORTS
  1182.     cmp   edx,[esi+0]
  1183.     jne   sopl2
  1184.     cmp   ebx,[esi+4]
  1185.     jb    sopl2
  1186.     cmp   ebx,[esi+8]
  1187.     jg    sopl2
  1188.     jmp   sopl3
  1189.  
  1190.   sopl2:
  1191.  
  1192.     dec   ecx
  1193.     jnz   sopl1
  1194.     mov   [esp+36],dword 1
  1195.     ret
  1196.  
  1197.   sopl3:
  1198.  
  1199.     test  edi,0x80000000 ; read ?
  1200.     jnz   sopl4
  1201.  
  1202.     mov   dx,bx          ; write
  1203.     out   dx,al
  1204.     mov   [esp+36],dword 0
  1205.     ret
  1206.  
  1207.   sopl4:
  1208.  
  1209.     mov   dx,bx          ; read
  1210.     in    al,dx
  1211.     and   eax,0xff
  1212.     mov   [esp+36],dword 0
  1213.     mov   [esp+24],eax
  1214.     ret
  1215.  
  1216.  
  1217.  
  1218. align 4
  1219. sys_sb16:
  1220.  
  1221.      cmp  word [sb16],word 0
  1222.      jnz  sb16l1
  1223.      mov  [esp+36],dword 1
  1224.      ret
  1225.    sb16l1:
  1226.      mov  [esp+36],dword 0
  1227.      cmp  eax,1    ; set volume - main
  1228.      jnz  sb16l2
  1229.      mov  dx,word [sb16]
  1230.      add  dx,4
  1231.      mov  al,0x22
  1232.      out  dx,al
  1233.      mov  esi,1
  1234.      call delay_ms
  1235.      mov  eax,ebx
  1236.      inc  edx
  1237.      out  dx,al
  1238.      ret
  1239.    sb16l2:
  1240.  
  1241.      cmp  eax,2    ; set volume - cd
  1242.      jnz  sb16l3
  1243.      mov  dx,word [sb16]
  1244.      add  dx,4
  1245.      mov  al,0x28
  1246.      out  dx,al
  1247.      mov  esi,1
  1248.      call delay_ms
  1249.      mov  eax,ebx
  1250.      add  edx,1
  1251.      out  dx,al
  1252.      ret
  1253.    sb16l3:
  1254.       mov  [esp+36],dword 2
  1255.       ret
  1256.  
  1257.  
  1258. align 4
  1259.  
  1260. sys_sb16II:
  1261.  
  1262.      cmp  word [sb16],word 0
  1263.      jnz  IIsb16l1
  1264.      mov  [esp+36],dword 1
  1265.      ret
  1266.    IIsb16l1:
  1267.  
  1268.      cmp  eax,1    ; set volume - main
  1269.      jnz  IIsb16l2
  1270.      ; L
  1271.      mov  dx,word [sb16]
  1272.      add  dx,4
  1273.      mov  al,0x30
  1274.      out  dx,al
  1275.      mov  eax,ebx
  1276.      inc  edx
  1277.      out  dx,al
  1278.      ; R
  1279.      mov  dx,word [sb16]
  1280.      add  dx,4
  1281.      mov  al,0x31
  1282.      out  dx,al
  1283.      mov  eax,ebx
  1284.      inc  edx
  1285.      out  dx,al
  1286.      mov  [esp+36],dword 0
  1287.      ret
  1288.    IIsb16l2:
  1289.  
  1290.      cmp  eax,2    ; set volume - cd
  1291.      jnz  IIsb16l3
  1292.      ; L
  1293.      mov  dx,word [sb16]
  1294.      add  dx,4
  1295.      mov  al,0x36
  1296.      out  dx,al
  1297.      mov  eax,ebx
  1298.      inc  edx
  1299.      out  dx,al
  1300.      ; R
  1301.      mov  dx,word [sb16]
  1302.      add  dx,4
  1303.      mov  al,0x37
  1304.      out  dx,al
  1305.      mov  eax,ebx
  1306.      inc  edx
  1307.      out  dx,al
  1308.      mov  [esp+36],dword 0
  1309.      ret
  1310.    IIsb16l3:
  1311.  
  1312.      mov  [esp+36],dword 2
  1313.      ret
  1314.  
  1315.  
  1316. display_number:
  1317.  
  1318. ; eax = print type, al=0 -> ebx is number
  1319. ;                   al=1 -> ebx is pointer
  1320. ;                   ah=0 -> display decimal
  1321. ;                   ah=1 -> display hexadecimal
  1322. ;                   ah=2 -> display binary
  1323. ;                   eax bits 16-21 = number of digits to display (0-32)
  1324. ;                   eax bits 22-31 = reserved
  1325. ;
  1326. ; ebx = number or pointer
  1327. ; ecx = x shl 16 + y
  1328. ; edx = color
  1329.         xor     edi, edi
  1330. display_number_force:
  1331.      push  eax
  1332.      and   eax,0x7fffffff
  1333.      cmp   eax,0xffff            ; length > 0 ?
  1334.      pop   eax    
  1335.      jge   cont_displ
  1336.      ret
  1337.    cont_displ:
  1338.      push  eax
  1339.      and   eax,0x7fffffff
  1340.      cmp   eax,61*0x10000        ; length <= 60 ?
  1341.      pop   eax  
  1342.      jb    cont_displ2
  1343.      ret
  1344.    cont_displ2:
  1345.  
  1346.      pushad
  1347.  
  1348.      cmp   al,1                  ; ecx is a pointer ?
  1349.      jne   displnl1
  1350.      mov   ebx,[ebx+std_application_base_address]
  1351.    displnl1:
  1352.      sub   esp,64
  1353.  
  1354.      cmp   ah,0                  ; DECIMAL
  1355.      jne   no_display_desnum
  1356.      shr   eax,16
  1357.      and   eax,0x803f
  1358. ;     and   eax,0x3f
  1359.      push  eax
  1360.      and   eax,0x3f
  1361.      mov   edi,esp
  1362.      add   edi,4+64-1
  1363.      mov   ecx,eax
  1364.      mov   eax,ebx
  1365.      mov   ebx,10
  1366.    d_desnum:
  1367.      xor   edx,edx
  1368.      div   ebx
  1369.      add   dl,48
  1370.      mov   [edi],dl
  1371.      dec   edi
  1372.      loop  d_desnum
  1373.      pop   eax
  1374.      call  normalize_number
  1375.      call  draw_num_text
  1376.      add   esp,64
  1377.      popad
  1378.      ret
  1379.    no_display_desnum:
  1380.  
  1381.      cmp   ah,0x01               ; HEXADECIMAL
  1382.      jne   no_display_hexnum
  1383.      shr   eax,16
  1384.      and   eax,0x803f
  1385. ;     and   eax,0x3f
  1386.      push  eax
  1387.      and   eax,0x3f
  1388.      mov   edi,esp
  1389.      add   edi,4+64-1
  1390.      mov   ecx,eax
  1391.      mov   eax,ebx
  1392.      mov   ebx,16
  1393.    d_hexnum:
  1394.      xor   edx,edx
  1395.      div   ebx
  1396.      add   edx,hexletters
  1397.      mov   dl,[edx]
  1398.      mov   [edi],dl
  1399.      dec   edi
  1400.      loop  d_hexnum
  1401.      pop   eax
  1402.      call  normalize_number
  1403.      call  draw_num_text
  1404.      add   esp,64
  1405.      popad
  1406.      ret
  1407.    no_display_hexnum:
  1408.  
  1409.      cmp   ah,0x02               ; BINARY
  1410.      jne   no_display_binnum
  1411.      shr   eax,16
  1412.      and   eax,0x803f
  1413. ;     and   eax,0x3f
  1414.      push  eax
  1415.      and   eax,0x3f
  1416.      mov   edi,esp
  1417.      add   edi,4+64-1
  1418.      mov   ecx,eax
  1419.      mov   eax,ebx
  1420.      mov   ebx,2
  1421.    d_binnum:
  1422.      xor   edx,edx
  1423.      div   ebx
  1424.      add   dl,48
  1425.      mov   [edi],dl
  1426.      dec   edi
  1427.      loop  d_binnum
  1428.      pop   eax
  1429.      call  normalize_number
  1430.      call  draw_num_text
  1431.      add   esp,64
  1432.      popad
  1433.      ret
  1434.    no_display_binnum:
  1435.  
  1436.      add   esp,64
  1437.      popad
  1438.      ret
  1439.  
  1440. normalize_number:
  1441.      test  ah,0x80
  1442.      jz   .continue
  1443.      mov  ecx,48
  1444.      and   eax,0x3f
  1445. @@:
  1446.      inc   edi
  1447.      cmp   [edi],cl
  1448.      jne   .continue
  1449.      dec   eax
  1450.      cmp   eax,1
  1451.      jne   @r
  1452. .continue:
  1453.      and   eax,0x3f
  1454.      ret
  1455.  
  1456.  
  1457.  
  1458. draw_num_text:
  1459.  
  1460.      ; dtext
  1461.      ;
  1462.      ; eax x & y
  1463.      ; ebx color
  1464.      ; ecx start of text
  1465.      ; edx length
  1466.      ; edi 1 force
  1467.  
  1468. ;        mov     edi,[CURRENT_TASK]
  1469. ;        shl     edi,8
  1470. ;        add     ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1471. ;        rol     eax,16
  1472. ;        add     ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1473. ;        rol     eax,16
  1474.  
  1475.      mov   edx,eax
  1476.      mov   ecx,64+4
  1477.      sub   ecx,eax
  1478.      add   ecx,esp
  1479.      mov   eax,[esp+64+32-8+4]
  1480.      push  edx                       ; add window start x & y
  1481.      mov   edx,[TASK_BASE]
  1482.  
  1483.      mov   edi,[CURRENT_TASK]
  1484.      shl   edi,8
  1485.  
  1486.      mov   ebx,[edx-twdw+WDATA.box.left]
  1487.      add   ebx,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1488.      shl   ebx,16
  1489.      add   ebx,[edx-twdw+WDATA.box.top]
  1490.      add   ebx,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1491.      add   eax,ebx
  1492.      pop   edx
  1493.      mov   ebx,[esp+64+32-12+4]
  1494.         and     ebx, not 0x80000000     ; force counted string
  1495.         mov     esi, [esp+64+4+4]
  1496.         mov     edi, [esp+64+4]
  1497.      jmp   dtext
  1498.  
  1499. read_string:
  1500.  
  1501.     ; eax  read_area
  1502.     ; ebx  color of letter
  1503.     ; ecx  color of background
  1504.     ; edx  number of letters to read
  1505.     ; esi  [x start]*65536 + [y_start]
  1506.  
  1507.     ret
  1508.  
  1509.  
  1510. align 4
  1511.  
  1512. sys_setup:
  1513.  
  1514. ; 1=roland mpu midi base , base io address
  1515. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1516. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1517. ; 4=sb16 base , base io address
  1518. ; 5=system language, 1eng 2fi 3ger 4rus
  1519. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1520. ; 8=fat32 partition in hd
  1521. ; 9
  1522. ; 10 = sound dma channel
  1523. ; 11 = enable lba read
  1524. ; 12 = enable pci access
  1525.  
  1526.  
  1527.      mov  [esp+36],dword 0
  1528.      cmp  eax,1                      ; MIDI
  1529.      jnz  nsyse1
  1530.      cmp  ebx,0x100
  1531.      jb   nsyse1
  1532.      mov  edx,65535
  1533.      cmp  edx,ebx
  1534.      jb   nsyse1
  1535.      mov  [midi_base],bx
  1536.      mov  word [mididp],bx
  1537.      inc  bx
  1538.      mov  word [midisp],bx
  1539.      ret
  1540.  
  1541. iglobal
  1542. midi_base dw 0
  1543. endg
  1544.  
  1545.    nsyse1:
  1546.  
  1547.      cmp  eax,2                      ; KEYBOARD
  1548.      jnz  nsyse2
  1549.      cmp  ebx,1
  1550.      jnz  kbnobase
  1551.      mov  edi,[TASK_BASE]
  1552.      add  ecx,[edi+TASKDATA.mem_start]
  1553.      mov  eax,ecx
  1554.      mov  ebx,keymap
  1555.      mov  ecx,128
  1556.      call memmove
  1557.      ret
  1558.    kbnobase:
  1559.      cmp  ebx,2
  1560.      jnz  kbnoshift
  1561.      mov  edi,[TASK_BASE]
  1562.      add  ecx,[edi+TASKDATA.mem_start]
  1563.      mov  eax,ecx
  1564.      mov  ebx,keymap_shift
  1565.      mov  ecx,128
  1566.      call memmove
  1567.      ret
  1568.    kbnoshift:
  1569.      cmp  ebx,3
  1570.      jne  kbnoalt
  1571.      mov  edi,[TASK_BASE]
  1572.      add  ecx,[edi+TASKDATA.mem_start]
  1573.      mov  eax,ecx
  1574.      mov  ebx,keymap_alt
  1575.      mov  ecx,128
  1576.      call memmove
  1577.      ret
  1578.    kbnoalt:
  1579.      cmp  ebx,9
  1580.      jnz  kbnocountry
  1581.      mov  word [keyboard],cx
  1582.      ret
  1583.    kbnocountry:
  1584.      mov  [esp+36],dword 1
  1585.      ret
  1586.    nsyse2:
  1587.      cmp  eax,3                      ; CD
  1588.      jnz  nsyse3
  1589.      test ebx,ebx
  1590.      jz   nosesl
  1591.      cmp  ebx, 4
  1592.      ja   nosesl
  1593.      mov  [cd_base],bl
  1594.      cmp  ebx,1
  1595.      jnz  noprma
  1596.      mov  [cdbase],0x1f0
  1597.      mov  [cdid],0xa0
  1598.    noprma:
  1599.      cmp  ebx,2
  1600.      jnz  noprsl
  1601.      mov  [cdbase],0x1f0
  1602.      mov  [cdid],0xb0
  1603.    noprsl:
  1604.      cmp  ebx,3
  1605.      jnz  nosema
  1606.      mov  [cdbase],0x170
  1607.      mov  [cdid],0xa0
  1608.    nosema:
  1609.      cmp  ebx,4
  1610.      jnz  nosesl
  1611.      mov  [cdbase],0x170
  1612.      mov  [cdid],0xb0
  1613.    nosesl:
  1614.      ret
  1615.  
  1616. cd_base db 0
  1617.  
  1618.    nsyse3:
  1619.  
  1620.      cmp  eax,4                      ; SB
  1621.      jnz  nsyse4
  1622.      cmp  ebx,0x100
  1623.      jb   nsyse4
  1624.      mov  edx,65535
  1625.      cmp  edx,ebx
  1626.      jb   nsyse4
  1627.      mov  word [sb16],bx
  1628.      ret
  1629.    nsyse4:
  1630.  
  1631.      cmp  eax,5                      ; SYSTEM LANGUAGE
  1632.      jnz  nsyse5
  1633.      mov  [syslang],ebx
  1634.      ret
  1635.    nsyse5:
  1636.  
  1637.      cmp  eax,7                      ; HD BASE
  1638.      jne  nsyse7
  1639.      test ebx,ebx
  1640.      jz   nosethd
  1641.      cmp  ebx,4
  1642.      ja   nosethd
  1643.      mov  [hd_base],bl
  1644.      cmp  ebx,1
  1645.      jnz  noprmahd
  1646.      mov  [hdbase],0x1f0
  1647.      mov  [hdid],0x0
  1648.      mov  [hdpos],1
  1649. ;     call set_FAT32_variables
  1650.    noprmahd:
  1651.      cmp  ebx,2
  1652.      jnz  noprslhd
  1653.      mov  [hdbase],0x1f0
  1654.      mov  [hdid],0x10
  1655.      mov  [hdpos],2
  1656. ;     call set_FAT32_variables
  1657.    noprslhd:
  1658.      cmp  ebx,3
  1659.      jnz  nosemahd
  1660.      mov  [hdbase],0x170
  1661.      mov  [hdid],0x0
  1662.      mov  [hdpos],3
  1663. ;     call set_FAT32_variables
  1664.    nosemahd:
  1665.      cmp  ebx,4
  1666.      jnz  noseslhd
  1667.      mov  [hdbase],0x170
  1668.      mov  [hdid],0x10
  1669.      mov  [hdpos],4
  1670. ;     call set_FAT32_variables
  1671.    noseslhd:
  1672.     call  reserve_hd1
  1673.     call  reserve_hd_channel
  1674.     call  free_hd_channel
  1675.     mov   [hd1_status],0        ; free
  1676.    nosethd:
  1677.      ret
  1678.  
  1679. iglobal
  1680. hd_base db 0
  1681. endg
  1682.  
  1683.    nsyse7:
  1684.  
  1685.      cmp  eax,8                      ; HD PARTITION
  1686.      jne  nsyse8
  1687.      mov  [fat32part],ebx
  1688. ;     call set_FAT32_variables
  1689.     call  reserve_hd1
  1690.     call  reserve_hd_channel
  1691.     call  free_hd_channel
  1692.      pusha
  1693.      call  choice_necessity_partition_1
  1694.      popa
  1695.     mov   [hd1_status],0        ; free
  1696.      ret
  1697.    nsyse8:
  1698.  
  1699.      cmp  eax,10                     ; SOUND DMA CHANNEL
  1700.      jne  no_set_sound_dma
  1701.      cmp  ebx,3
  1702.      ja   sys_setup_err
  1703.      mov  [sound_dma],ebx
  1704.      ret
  1705.    no_set_sound_dma:
  1706.  
  1707.      cmp  eax,11                     ; ENABLE LBA READ
  1708.      jne  no_set_lba_read
  1709.      and  ebx,1
  1710.      mov  [lba_read_enabled],ebx
  1711.      ret
  1712.    no_set_lba_read:
  1713.  
  1714.      cmp  eax,12                     ; ENABLE PCI ACCESS
  1715.      jne  no_set_pci_access
  1716.      and  ebx,1
  1717.      mov  [pci_access_enabled],ebx
  1718.      ret
  1719.    no_set_pci_access:
  1720.  
  1721. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1722. include 'vmodeint.inc'
  1723. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1724.  
  1725. sys_setup_err:
  1726.      mov  [esp+36],dword -1
  1727.      ret
  1728.  
  1729. align 4
  1730.  
  1731. sys_getsetup:
  1732.  
  1733. ; 1=roland mpu midi base , base io address
  1734. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1735. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1736. ; 4=sb16 base , base io address
  1737. ; 5=system language, 1eng 2fi 3ger 4rus
  1738. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1739. ; 8=fat32 partition in hd
  1740. ; 9=get hs timer tic
  1741.  
  1742.      cmp  eax,1
  1743.      jne  ngsyse1
  1744.      movzx eax,[midi_base]
  1745.      mov  [esp+36],eax
  1746.      ret
  1747.    ngsyse1:
  1748.  
  1749.      cmp  eax,2
  1750.      jne  ngsyse2
  1751.      cmp  ebx,1
  1752.      jnz  kbnobaseret
  1753.      mov  edi,[TASK_BASE]
  1754.      add  ecx,[edi+TASKDATA.mem_start]
  1755.      mov  ebx,ecx
  1756.      mov  eax,keymap
  1757.      mov  ecx,128
  1758.      call memmove
  1759.      ret
  1760.    kbnobaseret:
  1761.      cmp  ebx,2
  1762.      jnz  kbnoshiftret
  1763.      mov  edi,[TASK_BASE]
  1764.      add  ecx,[edi+TASKDATA.mem_start]
  1765.      mov  ebx,ecx
  1766.      mov  eax,keymap_shift
  1767.      mov  ecx,128
  1768.      call memmove
  1769.      ret
  1770.    kbnoshiftret:
  1771.      cmp  ebx,3
  1772.      jne  kbnoaltret
  1773.      mov  edi,[TASK_BASE]
  1774.      add  ecx,[edi+TASKDATA.mem_start]
  1775.      mov  ebx,ecx
  1776.      mov  eax,keymap_alt
  1777.      mov  ecx,128
  1778.      call memmove
  1779.      ret
  1780.    kbnoaltret:
  1781.      cmp  ebx,9
  1782.      jnz  ngsyse2
  1783.      movzx eax,word [keyboard]
  1784.      mov  [esp+36],eax
  1785.      ret
  1786.    ngsyse2:
  1787.  
  1788.      cmp  eax,3
  1789.      jnz  ngsyse3
  1790.      movzx eax,[cd_base]
  1791.      mov  [esp+36],eax
  1792.      ret
  1793.    ngsyse3:
  1794.  
  1795.      cmp  eax,4
  1796.      jne  ngsyse4
  1797.      mov  eax,[sb16]
  1798.      mov  [esp+36],eax
  1799.      ret
  1800.    ngsyse4:
  1801.  
  1802.      cmp  eax,5
  1803.      jnz  ngsyse5
  1804.      mov  eax,[syslang]
  1805.      mov  [esp+36],eax
  1806.      ret
  1807.    ngsyse5:
  1808.      cmp  eax,7
  1809.      jnz  ngsyse7
  1810.      movzx eax,[hd_base]
  1811.      mov  [esp+36],eax
  1812.      ret
  1813.    ngsyse7:
  1814.      cmp  eax,8
  1815.      jnz  ngsyse8
  1816.      mov eax,[fat32part]
  1817.      mov  [esp+36],eax
  1818.      ret
  1819.    ngsyse8:
  1820.      cmp  eax,9
  1821.      jne  ngsyse9
  1822.      mov  eax,[timer_ticks] ;[0xfdf0]
  1823.      mov  [esp+36],eax
  1824.      ret
  1825.    ngsyse9:
  1826.      cmp  eax,10
  1827.      jnz  ngsyse10
  1828.      mov eax,[sound_dma]
  1829.      mov  [esp+36],eax
  1830.      ret
  1831.    ngsyse10:
  1832.      cmp  eax,11
  1833.      jnz  ngsyse11
  1834.      mov eax,[lba_read_enabled]
  1835.      mov  [esp+36],eax
  1836.      ret
  1837.    ngsyse11:
  1838.      cmp  eax,12
  1839.      jnz  ngsyse12
  1840.      mov eax,[pci_access_enabled]
  1841.      mov  [esp+36],eax
  1842.      ret
  1843.    ngsyse12:
  1844.      mov  [esp+36],dword 1
  1845.      ret
  1846.  
  1847. get_timer_ticks:
  1848.     mov eax,[timer_ticks]
  1849.     ret
  1850.  
  1851. iglobal
  1852. align 4
  1853. mousefn dd msscreen, mswin, msbutton, msset
  1854.         dd app_load_cursor
  1855.         dd app_set_cursor
  1856.         dd app_delete_cursor
  1857.         dd msz
  1858. endg
  1859.  
  1860. readmousepos:
  1861.  
  1862. ; eax=0 screen relative
  1863. ; eax=1 window relative
  1864. ; eax=2 buttons pressed
  1865. ; eax=3 set mouse pos   ; reserved
  1866. ; eax=4 load cursor
  1867. ; eax=5 set cursor
  1868. ; eax=6 delete cursor   ; reserved
  1869. ; eax=7 get mouse_z
  1870.  
  1871.            cmp eax, 7
  1872.            ja msset
  1873.            jmp [mousefn+eax*4]
  1874. msscreen:
  1875.            mov  eax,[MOUSE_X]
  1876.            shl  eax,16
  1877.            mov  ax,[MOUSE_Y]
  1878.            mov  [esp+36],eax
  1879.            ret
  1880. mswin:
  1881.            mov  eax,[MOUSE_X]
  1882.            shl  eax,16
  1883.            mov  ax,[MOUSE_Y]
  1884.            mov  esi,[TASK_BASE]
  1885.            mov  bx, word [esi-twdw+WDATA.box.left]
  1886.            shl  ebx,16
  1887.            mov  bx, word [esi-twdw+WDATA.box.top]
  1888.            sub  eax,ebx
  1889.  
  1890.            mov  edi,[CURRENT_TASK]
  1891.            shl  edi,8
  1892.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1893.            rol  eax,16
  1894.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1895.            rol  eax,16
  1896.            mov  [esp+36],eax
  1897.            ret
  1898. msbutton:
  1899.            movzx eax,byte [BTN_DOWN]
  1900.            mov  [esp+36],eax
  1901.            ret
  1902. msz:
  1903.            mov   edi, [TASK_COUNT]
  1904.            movzx edi, word [WIN_POS + edi*2]
  1905.            cmp   edi, [CURRENT_TASK]
  1906.            jne   @f
  1907.            mov   ax,[MOUSE_SCROLL_H]
  1908.            shl   eax,16
  1909.            mov   ax,[MOUSE_SCROLL_V]
  1910.            mov   [esp+36],eax
  1911.            mov   [MOUSE_SCROLL_H],word 0
  1912.            mov   [MOUSE_SCROLL_V],word 0
  1913.            ret
  1914.        @@:
  1915.            mov  [esp+36],dword 0
  1916.            ret
  1917. msset:
  1918.            ret
  1919.  
  1920. app_load_cursor:
  1921.       ;     add ebx, new_app_base
  1922.            cmp ebx, OS_BASE
  1923.            jae msset
  1924.            stdcall load_cursor, ebx, ecx
  1925.            mov [esp+36], eax
  1926.            ret
  1927.  
  1928. app_set_cursor:
  1929.            stdcall set_cursor, ebx
  1930.            mov [esp+36], eax
  1931.            ret
  1932.  
  1933. app_delete_cursor:
  1934.            stdcall delete_cursor, ebx
  1935.            mov [esp+36], eax
  1936.            ret
  1937.  
  1938. is_input:
  1939.  
  1940.    push edx
  1941.    mov  dx,word [midisp]
  1942.    in   al,dx
  1943.    and  al,0x80
  1944.    pop  edx
  1945.    ret
  1946.  
  1947. is_output:
  1948.  
  1949.    push edx
  1950.    mov  dx,word [midisp]
  1951.    in   al,dx
  1952.    and  al,0x40
  1953.    pop  edx
  1954.    ret
  1955.  
  1956.  
  1957. get_mpu_in:
  1958.  
  1959.    push edx
  1960.    mov  dx,word [mididp]
  1961.    in   al,dx
  1962.    pop  edx
  1963.    ret
  1964.  
  1965.  
  1966. put_mpu_out:
  1967.  
  1968.    push edx
  1969.    mov  dx,word [mididp]
  1970.    out  dx,al
  1971.    pop  edx
  1972.    ret
  1973.  
  1974.  
  1975. setuart:
  1976.  
  1977.  su1:
  1978.    call is_output
  1979.    cmp  al,0
  1980.    jnz  su1
  1981.    mov  dx,word [midisp]
  1982.    mov  al,0xff
  1983.    out  dx,al
  1984.  su2:
  1985.    mov  dx,word [midisp]
  1986.    mov  al,0xff
  1987.    out  dx,al
  1988.    call is_input
  1989.    cmp  al,0
  1990.    jnz  su2
  1991.    call get_mpu_in
  1992.    cmp  al,0xfe
  1993.    jnz  su2
  1994.  su3:
  1995.    call is_output
  1996.    cmp  al,0
  1997.    jnz  su3
  1998.    mov  dx,word [midisp]
  1999.    mov  al,0x3f
  2000.    out  dx,al
  2001.  
  2002.    ret
  2003.  
  2004.  
  2005. align 4
  2006.  
  2007. sys_midi:
  2008.  
  2009.      cmp  [mididp],0
  2010.      jnz  sm0
  2011.      mov  [esp+36],dword 1
  2012.      ret
  2013.    sm0:
  2014.  
  2015.      cmp  eax,1
  2016.      mov  [esp+36],dword 0
  2017.      jnz  smn1
  2018.      call setuart
  2019.      ret
  2020.    smn1:
  2021.  
  2022.      cmp  eax,2
  2023.      jnz  smn2
  2024.    sm10:
  2025.      call get_mpu_in
  2026.      call is_output
  2027.      test al,al
  2028.      jnz  sm10
  2029.      mov  al,bl
  2030.      call put_mpu_out
  2031.      ret
  2032.    smn2:
  2033.  
  2034.      ret
  2035.  
  2036.  
  2037. detect_devices:
  2038. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2039. include 'detect/commouse.inc'
  2040. ;include 'detect/ps2mouse.inc'
  2041. ;include 'detect/dev_fd.inc'
  2042. ;include 'detect/dev_hdcd.inc'
  2043. ;include 'detect/sear_par.inc'
  2044. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2045.     ret
  2046.  
  2047.  
  2048. sys_end:
  2049.  
  2050.      mov   eax,[TASK_BASE]
  2051.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  2052.  
  2053.     waitterm:            ; wait here for termination
  2054.      mov   eax,5
  2055.      call  delay_hs
  2056.      jmp   waitterm
  2057.  
  2058. iglobal
  2059. align 4
  2060. sys_system_table:
  2061.         dd      sysfn_shutdown          ; 1 = system shutdown
  2062.         dd      sysfn_terminate         ; 2 = terminate thread
  2063.         dd      sysfn_activate          ; 3 = activate window
  2064.         dd      sysfn_getidletime       ; 4 = get idle time
  2065.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  2066.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  2067.         dd      sysfn_getactive         ; 7 = get active window
  2068.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  2069.         dd      sysfn_shutdown_param    ; 9 = shutdown with parameter
  2070.         dd      sysfn_minimize          ; 10 = minimize window
  2071.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  2072.         dd      sysfn_lastkey           ; 12 = get last pressed key
  2073.         dd      sysfn_getversion        ; 13 = get kernel version
  2074.         dd      sysfn_waitretrace       ; 14 = wait retrace
  2075.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  2076.         dd      sysfn_getfreemem        ; 16 = get free memory size
  2077.         dd      sysfn_getallmem         ; 17 = get total memory size
  2078.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  2079.                                         ;                 instead of slot
  2080.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2081.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2082.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2083.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2084. sysfn_num = ($ - sys_system_table)/4
  2085. endg
  2086.  
  2087. sys_system:
  2088.         dec     eax
  2089.         cmp     eax, sysfn_num
  2090.         jae     @f
  2091.         jmp     dword [sys_system_table + eax*4]
  2092. @@:
  2093.         ret
  2094.  
  2095. sysfn_shutdown:         ; 18.1 = BOOT
  2096.      mov  [BOOT_VAR+0x9030],byte 0
  2097.   for_shutdown_parameter:
  2098.  
  2099.      mov  eax,[TASK_COUNT]
  2100.      add  eax,2
  2101.      mov  [shutdown_processes],eax
  2102.      mov  [SYS_SHUTDOWN],al
  2103.      and  dword [esp+36], 0
  2104.      ret
  2105.   uglobal
  2106.    shutdown_processes: dd 0x0
  2107.   endg
  2108.  
  2109. sysfn_terminate:        ; 18.2 = TERMINATE
  2110.      cmp  ebx,2
  2111.      jb   noprocessterminate
  2112.      mov  edx,[TASK_COUNT]
  2113.      cmp  ebx,edx
  2114.      ja   noprocessterminate
  2115.      mov  eax,[TASK_COUNT]
  2116.      shl  ebx,5
  2117.      mov  edx,[ebx+CURRENT_TASK+TASKDATA.pid]
  2118.      add  ebx,CURRENT_TASK+TASKDATA.state
  2119.      cmp  byte [ebx], 9
  2120.      jz   noprocessterminate
  2121.  
  2122.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2123.      mov  [ebx],byte 3       ; clear possible i40's
  2124.      ;call MEM_Heap_UnLock
  2125.  
  2126.      cmp  edx,[application_table_status]    ; clear app table stat
  2127.      jne  noatsc
  2128.      mov  [application_table_status],0
  2129.    noatsc:
  2130.    noprocessterminate:
  2131.      ret
  2132.  
  2133. sysfn_terminate2:
  2134. ;lock application_table_status mutex
  2135. .table_status:
  2136.     cli
  2137.     cmp    [application_table_status],0
  2138.     je     .stf
  2139.     sti
  2140.     call   change_task
  2141.     jmp    .table_status
  2142. .stf:
  2143.     call   set_application_table_status
  2144.     mov    eax,ebx
  2145.     call   pid_to_slot
  2146.     test   eax,eax
  2147.     jz     .not_found
  2148.     mov    ebx,eax
  2149.     cli
  2150.     call   sysfn_terminate
  2151.     mov    [application_table_status],0
  2152.     sti
  2153.     and    dword [esp+36],0
  2154.     ret
  2155. .not_found:
  2156.     mov    [application_table_status],0
  2157.     or     dword [esp+36],-1
  2158.     ret
  2159.  
  2160. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2161.      cmp  ebx,2
  2162.      jb   .nowindowactivate
  2163.      cmp  ebx,[TASK_COUNT]
  2164.      ja   .nowindowactivate
  2165.  
  2166.      mov   [window_minimize], 2   ; restore window if minimized
  2167.  
  2168.      movzx esi, word [WIN_STACK + ebx*2]
  2169.      cmp   esi, [TASK_COUNT]
  2170.      je    .nowindowactivate ; already active
  2171.  
  2172.      mov   edi, ebx
  2173.      shl   edi, 5
  2174.      add   edi, window_data
  2175.      movzx esi, word [WIN_STACK + ebx * 2]
  2176.      lea   esi, [WIN_POS + esi * 2]
  2177.      call  waredraw
  2178. .nowindowactivate:
  2179.      ret
  2180.  
  2181. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2182.      mov  eax,[idleusesec]
  2183.      mov  [esp+36], eax
  2184.      ret
  2185.  
  2186. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2187.      mov  eax,[CPU_FREQ]
  2188.      mov  [esp+36], eax
  2189.      ret
  2190.  
  2191. ;  SAVE ramdisk to /hd/1/menuet.img
  2192. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2193.    include 'blkdev/rdsave.inc'
  2194. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2195.  
  2196. sysfn_getactive:        ; 18.7 = get active window
  2197.      mov  eax, [TASK_COUNT]
  2198.    movzx  eax, word [WIN_POS + eax*2]
  2199.      mov  [esp+36],eax
  2200.      ret
  2201.  
  2202. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2203.      cmp  ebx,1
  2204.      jne  nogetsoundflag
  2205.      movzx  eax,byte [sound_flag] ; get sound_flag
  2206.      mov  [esp+36],eax
  2207.      ret
  2208.  nogetsoundflag:
  2209.      cmp  ebx,2
  2210.      jnz  nosoundflag
  2211.      xor  byte [sound_flag], 1
  2212.  nosoundflag:
  2213.      ret
  2214.  
  2215. sysfn_shutdown_param:   ; 18.9 = system shutdown with param
  2216.      cmp  ebx,1
  2217.      jl   exit_for_anyone
  2218.      cmp  ebx,4
  2219.      jg   exit_for_anyone
  2220.      mov  [BOOT_VAR+0x9030],bl
  2221.      jmp  for_shutdown_parameter
  2222.  
  2223. sysfn_minimize:         ; 18.10 = minimize window
  2224.      mov   [window_minimize],1
  2225.  exit_for_anyone:
  2226.      ret
  2227.  
  2228. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2229.      cmp  ebx,1
  2230.      jnz  full_table
  2231.   small_table:
  2232.      call for_all_tables
  2233.      mov ecx,10
  2234.      cld
  2235.      rep movsb
  2236.      ret
  2237.    for_all_tables:
  2238.      mov edi,[TASK_BASE]
  2239.      mov edi,[edi+TASKDATA.mem_start]
  2240.      add edi,ecx
  2241.      mov esi,DRIVE_DATA
  2242.      ret
  2243.   full_table:
  2244.      cmp  ebx,2
  2245.      jnz  exit_for_anyone
  2246.      call for_all_tables
  2247.      mov ecx,16384
  2248.      cld
  2249.      rep movsd
  2250.      ret
  2251.  
  2252. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2253.         and     dword [esp+36], 0
  2254.         ret
  2255.  
  2256. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2257.      mov edi,[TASK_BASE]
  2258.      mov edi,[edi+TASKDATA.mem_start]
  2259.      add edi,ebx
  2260.      mov esi,version_inf
  2261.      mov ecx,version_end-version_inf
  2262.      cld
  2263.      rep movsb
  2264.      ret
  2265.  
  2266. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2267.      ;wait retrace functions
  2268.  sys_wait_retrace:
  2269.      mov edx,0x3da
  2270.  WaitRetrace_loop:
  2271.      in al,dx
  2272.      test al,1000b
  2273.      jz WaitRetrace_loop
  2274.      mov [esp+36],dword 0
  2275.      ret
  2276.  
  2277. sysfn_centermouse:      ; 18.15 = mouse centered
  2278.      call  mouse_centered
  2279.      mov [esp+36],dword 0
  2280.      ret
  2281.  
  2282. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2283.      cmp  ebx,0  ; get mouse speed factor
  2284.      jnz  .set_mouse_acceleration
  2285.      xor  eax,eax
  2286.      mov  ax,[mouse_speed_factor]
  2287.      mov  [esp+36],eax
  2288.      ret
  2289.  .set_mouse_acceleration:
  2290.      cmp  ebx,1  ; set mouse speed factor
  2291.      jnz  .get_mouse_delay
  2292.      mov  [mouse_speed_factor],cx
  2293.      ret
  2294.  .get_mouse_delay:
  2295.      cmp  ebx,2  ; get mouse delay
  2296.      jnz  .set_mouse_delay
  2297.      mov  eax,[mouse_delay]
  2298.      mov  [esp+36],eax
  2299.      ret
  2300.  .set_mouse_delay:
  2301.      cmp  ebx,3  ; set mouse delay
  2302.      jnz  .set_pointer_position
  2303.      mov  [mouse_delay],ecx
  2304.      ret
  2305.  .set_pointer_position:
  2306.      cmp  ebx,4  ; set mouse pointer position
  2307.      jnz  .set_mouse_button
  2308.      mov   [MOUSE_Y],cx    ;y
  2309.      ror   ecx,16
  2310.      mov   [MOUSE_X],cx    ;x
  2311.      rol   ecx,16
  2312.      ret
  2313.  .set_mouse_button:
  2314.      cmp   ebx,5  ; set mouse button features
  2315.      jnz  .end
  2316.      mov   [BTN_DOWN],cl
  2317.      mov   [mouse_active],1
  2318.  .end:
  2319.      ret
  2320.  
  2321. sysfn_getfreemem:
  2322.      mov eax, [pg_data.pages_free]
  2323.      shl eax, 2
  2324.      mov [esp+36],eax
  2325.      ret
  2326.  
  2327. sysfn_getallmem:
  2328.      mov  eax,[MEM_AMOUNT]
  2329.      shr eax, 10
  2330.      mov  [esp+36],eax
  2331.      ret
  2332.  
  2333. ; // Alver, 2007-22-08 // {
  2334. sysfn_pid_to_slot:
  2335.      mov   eax, ebx
  2336.      call  pid_to_slot
  2337.      mov   [esp+36], eax
  2338.      ret
  2339.  
  2340. sysfn_min_rest_window:
  2341.      pushad
  2342.      mov   eax, ecx      ; ebx - operating
  2343.      shr   ebx, 1
  2344.      jnc    @f
  2345.      call  pid_to_slot
  2346. @@:
  2347.      or    eax, eax      ; eax - number of slot
  2348.      jz    .error
  2349.      cmp   eax, 255         ; varify maximal slot number
  2350.      ja    .error
  2351.      xor   ecx, ecx
  2352.      mov   cx, [WIN_STACK + eax*2]  ; ecx - window number
  2353.      mov   eax, ecx
  2354.      shr   ebx, 1
  2355.      jc    .restore
  2356.  ; .minimize:
  2357.      call  minimize_window
  2358.      jmp   .exit
  2359. .restore:
  2360.      call  restore_minimized_window
  2361. .exit:
  2362.      popad
  2363.      xor   eax, eax
  2364.      mov   [esp+36], eax
  2365.      ret
  2366. .error:
  2367.      popad
  2368.      xor   eax, eax
  2369.      dec   eax
  2370.      mov   [esp+36], eax
  2371.      ret
  2372. ; } \\ Alver, 2007-22-08 \\
  2373.  
  2374. uglobal
  2375. ;// mike.dld, 2006-29-01 [
  2376. screen_workarea RECT
  2377. ;// mike.dld, 2006-29-01 ]
  2378. window_minimize db 0
  2379. sound_flag      db 0
  2380. endg
  2381.  
  2382. iglobal
  2383. version_inf:
  2384.   db 0,7,1,0  ; version 0.7.1.0
  2385.   db UID_KOLIBRI
  2386.   dd __REV__
  2387. version_end:
  2388. endg
  2389.  
  2390. UID_NONE=0
  2391. UID_MENUETOS=1   ;official
  2392. UID_KOLIBRI=2    ;russian
  2393.  
  2394. sys_cachetodiskette:
  2395. ;    pushad
  2396. ;    cmp  eax,1
  2397. ;    jne  no_write_all_of_ramdisk
  2398. ;    call fdc_writeramdisk
  2399. ;    popad
  2400. ;    ret
  2401. ;  no_write_all_of_ramdisk:
  2402. ;    cmp eax,2
  2403. ;    jne no_write_part_of_ramdisk
  2404. ;    call fdc_commitflush
  2405. ;    popad
  2406. ;    ret
  2407. ;  no_write_part_of_ramdisk:
  2408. ;    cmp  eax,3
  2409. ;    jne  no_set_fdc
  2410. ;    call fdc_set
  2411. ;    popad
  2412. ;    ret
  2413. ;  no_set_fdc:
  2414. ;    cmp  eax,4
  2415. ;    jne  no_get_fdc
  2416. ;    popad
  2417. ;    call fdc_get
  2418. ;    mov    [esp+36],ecx
  2419. ;    ret
  2420. ;  no_get_fdc:
  2421. ;    popad
  2422. ;    ret
  2423.     cmp eax,1
  2424.     jne no_floppy_a_save
  2425.     mov   [flp_number],1
  2426.     jmp save_image_on_floppy
  2427.   no_floppy_a_save:
  2428.     cmp eax,2
  2429.     jne no_floppy_b_save
  2430.     mov   [flp_number],2
  2431.   save_image_on_floppy:
  2432.     call save_image
  2433.     mov  [esp+36],dword 0
  2434.     cmp  [FDC_Status],0
  2435.     je   yes_floppy_save
  2436.   no_floppy_b_save:
  2437.     mov [esp+36],dword 1
  2438.   yes_floppy_save:
  2439.     ret
  2440.  
  2441. uglobal
  2442. ;  bgrchanged  dd  0x0
  2443. bgrlock db 0
  2444. bgrlockpid dd 0
  2445. endg
  2446.  
  2447. sys_background:
  2448.  
  2449.     cmp   eax,1                            ; BACKGROUND SIZE
  2450.     jnz   nosb1
  2451.     cmp   ebx,0
  2452.     je    sbgrr
  2453.     cmp   ecx,0
  2454.     je    sbgrr
  2455. @@:
  2456.         mov     al, 1
  2457.         xchg    [bgrlock], al
  2458.         test    al, al
  2459.         jz      @f
  2460.         call    change_task
  2461.         jmp     @b
  2462. @@:
  2463.     mov   [BgrDataWidth],ebx
  2464.     mov   [BgrDataHeight],ecx
  2465. ;    mov   [bgrchanged],1
  2466.  
  2467.     pushad
  2468. ; return memory for old background
  2469.     stdcall kernel_free, [img_background]
  2470. ; calculate RAW size
  2471.     xor  eax,eax
  2472.     inc  eax
  2473.     cmp  [BgrDataWidth],eax
  2474.     jae   @f
  2475.     mov [BgrDataWidth],eax
  2476. @@:
  2477.     cmp  [BgrDataHeight],eax
  2478.     jae   @f
  2479.     mov [BgrDataHeight],eax
  2480. @@:
  2481.     mov  eax,[BgrDataWidth]
  2482.     imul eax,[BgrDataHeight]
  2483.     lea  eax,[eax*3]
  2484.     mov  [mem_BACKGROUND],eax
  2485. ; get memory for new background
  2486.     stdcall kernel_alloc, eax
  2487.     test eax, eax
  2488.     jz .exit_mem
  2489.     mov [img_background], eax
  2490. .exit_mem:
  2491.     popad
  2492.         mov     [bgrlock], 0
  2493.  
  2494.   sbgrr:
  2495.     ret
  2496.  
  2497.   nosb1:
  2498.  
  2499.     cmp   eax,2                            ; SET PIXEL
  2500.     jnz   nosb2
  2501.     mov   edx,[mem_BACKGROUND]
  2502.     cmp   edx,ebx
  2503.     jbe   nosb2
  2504.     mov   edx,[ebx]
  2505.     and   edx,0xFF000000 ;255*256*256*256
  2506.     and   ecx,0x00FFFFFF ;255*256*256+255*256+255
  2507.     add   edx,ecx
  2508.     ;mov   [ebx+IMG_BACKGROUND],edx
  2509.     push  eax
  2510.     mov   eax,[img_background]
  2511.     mov   [ebx+eax],edx
  2512.     pop   eax
  2513. ;    mov   [bgrchanged],1
  2514.     ret
  2515.   nosb2:
  2516.  
  2517.     cmp   eax,3                            ; DRAW BACKGROUND
  2518.     jnz   nosb3
  2519. draw_background_temp:
  2520. ;    cmp   [bgrchanged],1 ;0
  2521. ;    je    nosb31
  2522. ;draw_background_temp:
  2523. ;    mov   [bgrchanged],1 ;0
  2524.     mov   [REDRAW_BACKGROUND],byte 1
  2525.     mov    [background_defined], 1
  2526.    nosb31:
  2527.     ret
  2528.   nosb3:
  2529.  
  2530.     cmp   eax,4                            ; TILED / STRETCHED
  2531.     jnz   nosb4
  2532.     cmp   ebx,[BgrDrawMode]
  2533.     je    nosb41
  2534.     mov   [BgrDrawMode],ebx
  2535. ;    mov   [bgrchanged],1
  2536.    nosb41:
  2537.     ret
  2538.   nosb4:
  2539.  
  2540.     cmp   eax,5                            ; BLOCK MOVE TO BGR
  2541.     jnz   nosb5
  2542.   ; bughere
  2543.     mov   eax, ebx
  2544.     mov   ebx, ecx
  2545.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2546.     mov   ecx, edx
  2547.     call  memmove
  2548.   .fin:
  2549.     ret
  2550.   nosb5:
  2551.  
  2552.         cmp     eax, 6
  2553.         jnz     nosb6
  2554. @@:
  2555.         mov     al, 1
  2556.         xchg    [bgrlock], al
  2557.         test    al, al
  2558.         jz      @f
  2559.         call    change_task
  2560.         jmp     @b
  2561. @@:
  2562.         mov     eax, [CURRENT_TASK]
  2563.         mov     [bgrlockpid], eax
  2564.         stdcall user_alloc, [mem_BACKGROUND]
  2565.         mov     [esp+36], eax
  2566.         test    eax, eax
  2567.         jz      .nomem
  2568.         mov     ebx, eax
  2569.         shr     ebx, 12
  2570.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2571.         mov     esi, [img_background]
  2572.         shr     esi, 12
  2573.         mov     ecx, [mem_BACKGROUND]
  2574.         add     ecx, 0xFFF
  2575.         shr     ecx, 12
  2576. .z:
  2577.         mov     eax, [page_tabs+ebx*4]
  2578.         test    al, 1
  2579.         jz      @f
  2580.         call    free_page
  2581. @@:
  2582.         mov     eax, [page_tabs+esi*4]
  2583.         or      al, PG_UW
  2584.         mov     [page_tabs+ebx*4], eax
  2585.         mov     eax, ebx
  2586.         shl     eax, 12
  2587.         invlpg  [eax]
  2588.         inc     ebx
  2589.         inc     esi
  2590.         loop    .z
  2591.         ret
  2592. .nomem:
  2593.         and     [bgrlockpid], 0
  2594.         mov     [bgrlock], 0
  2595. nosb6:
  2596.         cmp     eax, 7
  2597.         jnz     nosb7
  2598.         cmp     [bgrlock], 0
  2599.         jz      .err
  2600.         mov     eax, [CURRENT_TASK]
  2601.         cmp     [bgrlockpid], eax
  2602.         jnz     .err
  2603.         mov     eax, ebx
  2604.         shr     eax, 12
  2605.         mov     ecx, [page_tabs+(eax-1)*4]
  2606.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2607.         jz      .err
  2608.         jnp     .err
  2609.         push    eax
  2610.         shr     ecx, 12
  2611. @@:
  2612.         and     dword [page_tabs+eax*4], 0
  2613.         mov     edx, eax
  2614.         shl     edx, 12
  2615.         invlpg  [edx]
  2616.         inc     eax
  2617.         loop    @b
  2618.         pop     eax
  2619.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2620.         stdcall user_free, ebx
  2621.         mov     [esp+36], eax
  2622.         and     [bgrlockpid], 0
  2623.         mov     [bgrlock], 0
  2624.         ret
  2625. .err:
  2626.         and     dword [esp+36], 0
  2627.         ret
  2628.  
  2629. nosb7:
  2630.     ret
  2631.  
  2632.  
  2633. align 4
  2634.  
  2635. sys_getbackground:
  2636.  
  2637.     cmp   eax,1                                  ; SIZE
  2638.     jnz   nogb1
  2639.     mov   eax,[BgrDataWidth]
  2640.     shl   eax,16
  2641.     mov   ax,[BgrDataHeight]
  2642.     mov   [esp+36],eax
  2643.     ret
  2644.   nogb1:
  2645.  
  2646.     cmp   eax,2                                  ; PIXEL
  2647.     jnz   nogb2
  2648. ;    mov   edx,0x160000-16
  2649. ;    cmp   edx,ebx
  2650. ;    jbe   nogb2
  2651. ;    mov   eax, [ebx+IMG_BACKGROUND]
  2652.     mov   eax,[img_background]
  2653.     mov   eax,[ebx+eax]
  2654.  
  2655.     and   eax, 0xFFFFFF
  2656.     mov   [esp+36],eax
  2657.     ret
  2658.   nogb2:
  2659.  
  2660.     cmp   eax,4                                  ; TILED / STRETCHED
  2661.     jnz   nogb4
  2662.     mov   eax,[BgrDrawMode]
  2663.   nogb4:
  2664.     mov   [esp+36],eax
  2665.     ret
  2666.  
  2667.  
  2668. align 4
  2669.  
  2670. sys_getkey:
  2671.     mov   [esp+36],dword 1
  2672. ; test main buffer
  2673.     mov   ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2674.     movzx ecx,word [WIN_STACK + ebx * 2]
  2675.     mov   edx,[TASK_COUNT]
  2676.     cmp   ecx,edx
  2677.     jne   .finish
  2678.     cmp   [KEY_COUNT],byte 0
  2679.     je    .finish
  2680.     movzx eax,byte [KEY_BUFF]
  2681.     shl   eax,8
  2682.     push  eax
  2683.     dec   byte [KEY_COUNT]
  2684.     and   byte [KEY_COUNT],127
  2685.     movzx ecx,byte [KEY_COUNT]
  2686.     add   ecx,2
  2687.  ;   mov   esi,0xf402
  2688.  ;   mov   edi,0xf401
  2689.  ;   cld
  2690.  ;  rep   movsb
  2691.     mov   eax, KEY_BUFF+1
  2692.     mov   ebx, KEY_BUFF
  2693.     call  memmove
  2694.     pop   eax
  2695. .ret_eax:
  2696.     mov   [esp+36],eax
  2697.     ret
  2698.  .finish:
  2699. ; test hotkeys buffer
  2700.         mov     ecx, hotkey_buffer
  2701. @@:
  2702.         cmp     [ecx], ebx
  2703.         jz      .found
  2704.         add     ecx, 8
  2705.         cmp     ecx, hotkey_buffer+120*8
  2706.         jb      @b
  2707.         ret
  2708. .found:
  2709.         mov     ax, [ecx+6]
  2710.         shl     eax, 16
  2711.         mov     ah, [ecx+4]
  2712.         mov     al, 2
  2713.         and     dword [ecx+4], 0
  2714.         and     dword [ecx], 0
  2715.         jmp     .ret_eax
  2716.  
  2717. align 4
  2718.  
  2719. sys_getbutton:
  2720.  
  2721.     mov   ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2722.     mov   [esp+36],dword 1
  2723.     movzx ecx, word [WIN_STACK + ebx * 2]
  2724.     mov   edx, [TASK_COUNT] ; less than 256 processes
  2725.     cmp   ecx,edx
  2726.     jne   .exit
  2727.     movzx eax,byte [BTN_COUNT]
  2728.     test  eax,eax
  2729.     jz    .exit
  2730.     mov   eax,[BTN_BUFF]
  2731.     shl   eax,8
  2732.     mov   [BTN_COUNT],byte 0
  2733.     mov   [esp+36],eax
  2734.  .exit:
  2735.     ret
  2736.  
  2737.  
  2738. align 4
  2739.  
  2740. sys_cpuusage:
  2741.  
  2742. ;  RETURN:
  2743. ;
  2744. ;  +00 dword     process cpu usage
  2745. ;  +04  word     position in windowing stack
  2746. ;  +06  word     windowing stack value at current position (cpu nro)
  2747. ;  +10 12 bytes  name
  2748. ;  +22 dword     start in mem
  2749. ;  +26 dword     used mem
  2750. ;  +30 dword     PID , process idenfification number
  2751. ;
  2752.  
  2753.     mov  edi,[TASK_BASE]   ; eax = return area
  2754.     add  eax,[edi + TASKDATA.mem_start]
  2755.  
  2756.     cmp  ebx,-1         ; who am I ?
  2757.     jne  no_who_am_i
  2758.     mov  ebx,[CURRENT_TASK]
  2759.   no_who_am_i:
  2760.  
  2761.     push eax            ; return area
  2762.     push ebx            ; process number
  2763.  
  2764.     push ebx
  2765.     push ebx
  2766.     push eax
  2767.  
  2768.     ; return memory usage
  2769.  
  2770.     xor  edx,edx
  2771.     mov  eax,0x20
  2772.     mul  ebx
  2773.     add  eax,CURRENT_TASK+TASKDATA.cpu_usage
  2774.     mov  ebx,eax
  2775.     pop  eax
  2776.     mov  ecx,[ebx]
  2777.     mov  [eax],ecx
  2778.     pop  ebx
  2779.     mov  cx, [WIN_STACK + ebx * 2]
  2780.     mov  [eax+4],cx
  2781.     mov  cx, [WIN_POS + ebx * 2]
  2782.     mov  [eax+6],cx
  2783.     push eax
  2784.     mov  eax,ebx
  2785.     shl  eax,8
  2786.     add  eax,SLOT_BASE+APPDATA.app_name
  2787.     pop  ebx
  2788.     add  ebx,10
  2789.     mov  ecx,11
  2790.     call memmove
  2791.  
  2792.     ; memory usage
  2793.  
  2794.     xor    eax,eax
  2795.     mov    edx,0x100000*16
  2796.     pop    ecx                                   ; get gdt of tss
  2797.     cmp    ecx,1
  2798.     je     os_mem
  2799.     shl    ecx,8
  2800.     mov    edx,[SLOT_BASE+ecx+APPDATA.mem_size] ;0x8c
  2801.     mov    eax,std_application_base_address
  2802.     ; eax run base -> edx used memory
  2803.   os_mem:
  2804.     dec    edx
  2805.     mov    [ebx+12],eax
  2806.     mov    [ebx+16],edx
  2807.  
  2808.     ; PID (+30)
  2809.  
  2810.     mov    eax,[esp]
  2811.     shl    eax,5
  2812.     add    eax,CURRENT_TASK+TASKDATA.pid
  2813.     mov    eax,[eax]
  2814.     mov    [ebx+20],eax
  2815.  
  2816.     ; window position and size
  2817.  
  2818.     mov    esi,[esp]
  2819.     shl    esi,5
  2820.     add    esi,window_data + WDATA.box
  2821.     mov    edi,[esp+4]
  2822.     add    edi,34
  2823.     mov    ecx,4
  2824.     cld
  2825.     rep    movsd
  2826.  
  2827.     ; Process state (+50)
  2828.  
  2829.     mov    eax,[esp]
  2830.     shl    eax,5
  2831.     add    eax,CURRENT_TASK+TASKDATA.state
  2832.     mov    eax,[eax]
  2833.     mov    [ebx+40],ax
  2834.  
  2835.     ; Window client area box
  2836.  
  2837.     mov    esi,[esp]
  2838.     shl    esi,8
  2839.     add    esi,SLOT_BASE+APPDATA.wnd_clientbox
  2840.     lea    edi,[ebx+44]
  2841.     mov    ecx,4
  2842.     rep    movsd
  2843.  
  2844.     ; Window state
  2845.  
  2846.     mov    esi,[esp]
  2847.     shl    esi,5
  2848.     add    esi,window_data + WDATA.box
  2849.     mov    al,[esi+WDATA.fl_wstate]
  2850.     mov    [edi],al
  2851.  
  2852.     pop    ebx
  2853.     pop    eax
  2854.  
  2855.     ; return number of processes
  2856.  
  2857.     mov    eax,[TASK_COUNT]
  2858.     mov    [esp+36],eax
  2859.     ret
  2860.  
  2861. align 4
  2862. sys_clock:
  2863.         cli
  2864.   ; Mikhail Lisovin  xx Jan 2005
  2865.   @@:   mov   al, 10
  2866.         out   0x70, al
  2867.         in    al, 0x71
  2868.         test  al, al
  2869.         jns   @f
  2870.         mov   esi, 1
  2871.         call  delay_ms
  2872.         jmp   @b
  2873.   @@:
  2874.   ; end Lisovin's fix
  2875.  
  2876.         xor   al,al           ; seconds
  2877.         out   0x70,al
  2878.         in    al,0x71
  2879.         movzx ecx,al
  2880.         mov   al,02           ; minutes
  2881.         shl   ecx,16
  2882.         out   0x70,al
  2883.         in    al,0x71
  2884.         movzx edx,al
  2885.         mov   al,04           ; hours
  2886.         shl   edx,8
  2887.         out   0x70,al
  2888.         in    al,0x71
  2889.         add   ecx,edx
  2890.         movzx edx,al
  2891.         add   ecx,edx
  2892.         sti
  2893.         mov   [esp+36],ecx
  2894.         ret
  2895.  
  2896.  
  2897. align 4
  2898.  
  2899. sys_date:
  2900.  
  2901.         cli
  2902.  
  2903.   @@:   mov   al, 10
  2904.         out   0x70, al
  2905.         in    al, 0x71
  2906.         test  al, al
  2907.         jns   @f
  2908.         mov   esi, 1
  2909.         call  delay_ms
  2910.         jmp   @b
  2911.   @@:
  2912.  
  2913.         mov     ch,0
  2914.         mov     al,7            ; date
  2915.         out     0x70,al
  2916.         in      al,0x71
  2917.         mov     cl,al
  2918.         mov     al,8            ; month
  2919.         shl     ecx,16
  2920.         out     0x70,al
  2921.         in      al,0x71
  2922.         mov     ch,al
  2923.         mov     al,9            ; year
  2924.         out     0x70,al
  2925.         in      al,0x71
  2926.         mov     cl,al
  2927.         sti
  2928.         mov     [esp+36],ecx
  2929.         ret
  2930.  
  2931.  
  2932. ; redraw status
  2933.  
  2934. sys_redrawstat:
  2935.  
  2936.     cmp  eax,1
  2937.     jne  no_widgets_away
  2938.  
  2939.     ; buttons away
  2940.  
  2941.     mov   ecx,[CURRENT_TASK]
  2942.  
  2943.   sys_newba2:
  2944.  
  2945.     mov   edi,[BTN_ADDR]
  2946.     cmp   [edi],dword 0  ; empty button list ?
  2947.     je    end_of_buttons_away
  2948.  
  2949.     movzx ebx,word [edi]
  2950.     inc   ebx
  2951.  
  2952.     mov   eax,edi
  2953.  
  2954.   sys_newba:
  2955.  
  2956.     dec   ebx
  2957.     jz    end_of_buttons_away
  2958.  
  2959.     add   eax,0x10
  2960.     cmp   cx,[eax]
  2961.     jnz   sys_newba
  2962.  
  2963.     push  eax ebx ecx
  2964.     mov   ecx,ebx
  2965.     inc   ecx
  2966.     shl   ecx,4
  2967.     mov   ebx,eax
  2968.     add   eax,0x10
  2969.     call  memmove
  2970.     dec   dword [edi]
  2971.     pop   ecx ebx eax
  2972.  
  2973.     jmp   sys_newba2
  2974.  
  2975.   end_of_buttons_away:
  2976.  
  2977.     ret
  2978.  
  2979.   no_widgets_away:
  2980.  
  2981.     cmp   eax,2
  2982.     jnz   srl1
  2983.  
  2984.     mov   edx,[TASK_BASE]      ; return whole screen draw area for this app
  2985.     add   edx,draw_data-CURRENT_TASK
  2986.     mov   [edx+RECT.left], 0
  2987.     mov   [edx+RECT.top], 0
  2988.     mov   eax,[ScreenWidth]
  2989.     mov   [edx+RECT.right],eax
  2990.     mov   eax,[ScreenHeight]
  2991.     mov   [edx+RECT.bottom],eax
  2992.  
  2993.     mov   edi,[TASK_BASE]
  2994.     or    [edi-twdw+WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2995.  
  2996.     call  sys_window_mouse
  2997.  
  2998.     ret
  2999.  
  3000.   srl1:
  3001.  
  3002.     ret
  3003.  
  3004.  
  3005. sys_drawwindow:
  3006.  
  3007.     mov   edi,ecx
  3008.     shr   edi,16+8
  3009.     and   edi,15
  3010.  
  3011.     cmp   edi,0   ; type I    - original style
  3012.     jne   nosyswI
  3013.     inc   [mouse_pause]
  3014.     call  [disable_mouse]
  3015.     call  sys_set_window
  3016.     call  [disable_mouse]
  3017.     call  drawwindow_I
  3018.     ;dec   [mouse_pause]
  3019.     ;call   [draw_pointer]
  3020.     ;ret
  3021.     jmp   draw_window_caption.2
  3022.   nosyswI:
  3023.  
  3024.     cmp   edi,1   ; type II   - only reserve area, no draw
  3025.     jne   nosyswII
  3026.     inc   [mouse_pause]
  3027.     call  [disable_mouse]
  3028.     call  sys_set_window
  3029.     call  [disable_mouse]
  3030.     call  sys_window_mouse
  3031.     dec   [mouse_pause]
  3032.     call   [draw_pointer]
  3033.     ret
  3034.   nosyswII:
  3035.  
  3036.     cmp   edi,2   ; type III  - new style
  3037.     jne   nosyswIII
  3038.     inc   [mouse_pause]
  3039.     call  [disable_mouse]
  3040.     call  sys_set_window
  3041.     call  [disable_mouse]
  3042.     call  drawwindow_III
  3043.     ;dec   [mouse_pause]
  3044.     ;call   [draw_pointer]
  3045.     ;ret
  3046.     jmp   draw_window_caption.2
  3047.   nosyswIII:
  3048.  
  3049.     cmp   edi,3   ; type IV - skinned window
  3050.     je    draw_skin_window
  3051.     cmp   edi,4   ; type V - skinned window not sized!          {not_sized_skin_window}
  3052.     jne   nosyswV
  3053.   draw_skin_window:
  3054.  
  3055.     inc   [mouse_pause]
  3056.     call  [disable_mouse]
  3057.     call  sys_set_window
  3058.     call  [disable_mouse]
  3059.     push  0
  3060.     mov   edi, [TASK_COUNT]
  3061.     movzx edi, word [WIN_POS + edi*2]
  3062.     cmp   edi, [CURRENT_TASK]
  3063.     jne   @f
  3064.     inc   dword [esp]
  3065.  @@:
  3066.     call  drawwindow_IV
  3067.     ;dec   [mouse_pause]
  3068.     ;call   [draw_pointer]
  3069.     ;ret
  3070.     jmp   draw_window_caption.2
  3071.   nosyswV:
  3072.  
  3073.     ret
  3074.  
  3075.  
  3076. draw_window_caption:
  3077.         inc     [mouse_pause]
  3078.         call    [disable_mouse]
  3079.  
  3080.         xor     eax,eax
  3081.         mov     edx,[TASK_COUNT]
  3082.         movzx   edx,word[WIN_POS+edx*2]
  3083.         cmp     edx,[CURRENT_TASK]
  3084.         jne     @f
  3085.         inc     eax
  3086.     @@: mov     edx,[CURRENT_TASK]
  3087.         shl     edx,5
  3088.         add     edx,window_data
  3089.         movzx   ebx,[edx+WDATA.fl_wstyle]
  3090.         and     bl,0x0F
  3091.         cmp     bl,3
  3092.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  3093.         cmp     bl,4
  3094.         je      .draw_caption_style_3
  3095.  
  3096.         jmp     .not_style_3
  3097.   .draw_caption_style_3:
  3098.  
  3099.         push    edx
  3100.         call    drawwindow_IV_caption
  3101.         add     esp,4
  3102.         jmp     .2
  3103.  
  3104.   .not_style_3:
  3105.         cmp     bl,2
  3106.         jne     .not_style_2
  3107.  
  3108.         call    drawwindow_III_caption
  3109.         jmp     .2
  3110.  
  3111.   .not_style_2:
  3112.         cmp     bl,0
  3113.         jne     .2
  3114.  
  3115.         call    drawwindow_I_caption
  3116.  
  3117. ;--------------------------------------------------------------
  3118.   .2:   ;jmp     @f
  3119.         mov     edi,[CURRENT_TASK]
  3120.         shl     edi,5
  3121.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3122.         jz      @f
  3123.         mov     ecx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  3124.         or      ecx,ecx
  3125.         jz      @f
  3126.         add     ecx,[edi+CURRENT_TASK+TASKDATA.mem_start]
  3127.  
  3128.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  3129.         and     al,0x0F
  3130.         cmp     al,3
  3131.         je      .skinned
  3132.         cmp     al,4
  3133.         je      .skinned
  3134.  
  3135.         jmp     .not_skinned
  3136.   .skinned:
  3137.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3138.         mov     bp,word[edi+window_data+WDATA.box.top]
  3139.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3140.         sub     ax,[_skinmargins.left]
  3141.         sub     ax,[_skinmargins.right]
  3142.         cwde
  3143.         cdq
  3144.         mov     ebx,6
  3145.         idiv    ebx
  3146.         or      eax,eax
  3147.         js      @f
  3148.         mov     edx,eax
  3149.         mov     eax,dword[_skinmargins.left-2]
  3150.         mov     ax,word[_skinh]
  3151.         sub     ax,[_skinmargins.bottom]
  3152.         sub     ax,[_skinmargins.top]
  3153.         sar     ax,1
  3154.         adc     ax,0
  3155.         add     ax,[_skinmargins.top]
  3156.         add     ax,-3
  3157.         add     eax,ebp
  3158.         jmp     .dodraw
  3159.  
  3160.   .not_skinned:
  3161.         cmp     al,1
  3162.         je      @f
  3163.  
  3164.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3165.         mov     bp,word[edi+window_data+WDATA.box.top]
  3166.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3167.         sub     eax,16
  3168.         cwde
  3169.         cdq
  3170.         mov     ebx,6
  3171.         idiv    ebx
  3172.         or      eax,eax
  3173.         js      @f
  3174.         mov     edx,eax
  3175.         mov     eax,0x00080007
  3176.         add     eax,ebp
  3177. .dodraw:
  3178.         mov     ebx,[common_colours+16];0x00FFFFFF
  3179.         or      ebx, 0x80000000
  3180.         xor     edi,edi
  3181.         call    dtext
  3182.  
  3183.     @@:
  3184. ;--------------------------------------------------------------
  3185.         dec     [mouse_pause]
  3186.         call    [draw_pointer]
  3187.         ret
  3188.  
  3189. iglobal
  3190. align 4
  3191. window_topleft dd \
  3192.   1, 21,\               ;type 0
  3193.   0,  0,\       ;type 1
  3194.   5, 20,\       ;type 2
  3195.   5,  ?,\       ;type 3 {set by skin}
  3196.   5,  ?         ;type 4 {set by skin}
  3197. endg
  3198.  
  3199. set_window_clientbox:
  3200.         push    eax ecx edi
  3201.  
  3202.         mov     eax,[_skinh]
  3203.         mov     [window_topleft+4*7],eax
  3204.         mov     [window_topleft+4*9],eax
  3205.  
  3206.         mov     ecx,edi
  3207.         sub     edi,window_data
  3208.         shl     edi,3
  3209.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3210.         jz      @f
  3211.  
  3212.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3213.         and     eax,0x0F
  3214.         mov     eax,[eax*8+window_topleft+0]
  3215.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3216.         shl     eax,1
  3217.         neg     eax
  3218.         add     eax,[ecx+WDATA.box.width]
  3219.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3220.  
  3221.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3222.         and     eax,0x0F
  3223.         push    [eax*8+window_topleft+0]
  3224.         mov     eax,[eax*8+window_topleft+4]
  3225.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3226.         neg     eax
  3227.         sub     eax,[esp]
  3228.         add     eax,[ecx+WDATA.box.height]
  3229.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3230.         add     esp,4
  3231.  
  3232.         pop     edi ecx eax
  3233.         ret
  3234.     @@:
  3235.         xor     eax,eax
  3236.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3237.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3238.         mov     eax,[ecx+WDATA.box.width]
  3239.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3240.         mov     eax,[ecx+WDATA.box.height]
  3241.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3242.  
  3243.         pop     edi ecx eax
  3244.         ret
  3245.  
  3246. sys_set_window:
  3247.  
  3248.     mov   edi,[CURRENT_TASK]
  3249.     shl   edi,5
  3250.     add   edi,window_data
  3251.  
  3252.     ; colors
  3253.     mov   [edi+WDATA.cl_workarea],ecx
  3254.     mov   [edi+WDATA.cl_titlebar],edx
  3255.     mov   [edi+WDATA.cl_frames],esi
  3256.  
  3257.     ; check flag (?)
  3258.     test  [edi+WDATA.fl_wdrawn],1
  3259.     jnz   newd
  3260.  
  3261.     push  eax
  3262.     mov   eax,[timer_ticks] ;[0xfdf0]
  3263.     add   eax,100
  3264.     mov   [new_window_starting],eax
  3265.     pop   eax
  3266.  
  3267.     mov   word[edi+WDATA.box.width],ax
  3268.     mov   word[edi+WDATA.box.height],bx
  3269.     sar   eax,16
  3270.     sar   ebx,16
  3271.     mov   word[edi+WDATA.box.left],ax
  3272.     mov   word[edi+WDATA.box.top],bx
  3273.  
  3274.     call  check_window_position
  3275.  
  3276.     call  set_window_clientbox
  3277.  
  3278.     push  ecx esi edi               ; save for window fullscreen/resize
  3279.     ;mov   esi,edi
  3280.  
  3281.         mov     cl,[edi+WDATA.fl_wstyle]
  3282.  
  3283.     sub   edi,window_data
  3284.     shl   edi,3
  3285.     add   edi,SLOT_BASE
  3286.  
  3287.         and     cl,0x0F
  3288.         mov     [edi+APPDATA.wnd_caption],0
  3289.         cmp     cl,3
  3290.         je      set_APPDATA_wnd_caption
  3291.         cmp     cl,4                                                            ; {SPraid.simba}
  3292.         je      set_APPDATA_wnd_caption
  3293.  
  3294.         jmp     @f
  3295.     set_APPDATA_wnd_caption:
  3296.         mov     [edi+APPDATA.wnd_caption],esi
  3297.     @@: mov     esi,[esp+0]
  3298.  
  3299.     add   edi, APPDATA.saved_box
  3300.         movsd
  3301.         movsd
  3302.         movsd
  3303.         movsd
  3304.     pop   edi esi ecx
  3305.  
  3306.         mov     esi, [CURRENT_TASK]
  3307.         movzx   esi, word [WIN_STACK+esi*2]
  3308.         lea     esi, [WIN_POS+esi*2]
  3309.         call    waredraw
  3310.  
  3311.     push  eax ebx ecx edx
  3312. ;;;    mov   eax, 1
  3313. ;;;    call  delay_hs
  3314.     mov   eax, [edi+WDATA.box.left]
  3315.     mov   ebx, [edi+WDATA.box.top]
  3316.     mov   ecx, [edi+WDATA.box.width]
  3317.     mov   edx, [edi+WDATA.box.height]
  3318.     add   ecx, eax
  3319.     add   edx, ebx
  3320.     call  calculatescreen
  3321.     pop   edx ecx ebx eax
  3322.  
  3323.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3324.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3325.  
  3326.   newd:
  3327.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3328.     mov   edx,edi
  3329.  
  3330.     ret
  3331.  
  3332. syscall_windowsettings:
  3333.  
  3334.   .set_window_caption:
  3335.         dec     eax     ; subfunction #1 - set window caption
  3336.         jnz     .get_window_caption
  3337.  
  3338.         ; NOTE: only window owner thread can set its caption,
  3339.         ;       so there's no parameter for PID/TID
  3340.  
  3341.         mov     edi,[CURRENT_TASK]
  3342.         shl     edi,5
  3343.  
  3344.         ; have to check if caption is within application memory limit
  3345.         ; check is trivial, and if application resizes its memory,
  3346.         ;   caption still can become over bounds
  3347. ; diamond, 31.10.2006: check removed because with new memory manager
  3348. ; there can be valid data after APPDATA.mem_size bound
  3349. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3350. ;        add     ecx,255 ; max caption length
  3351. ;        cmp     ebx,ecx
  3352. ;        ja      .exit_fail
  3353.  
  3354.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3355.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3356.  
  3357.         call    draw_window_caption
  3358.  
  3359.         xor     eax,eax ; eax = 0 (success)
  3360.         ret
  3361.  
  3362.   .get_window_caption:
  3363.         dec     eax     ; subfunction #2 - get window caption
  3364.         jnz     .exit_fail
  3365.  
  3366.         ; not implemented yet
  3367.  
  3368.   .exit_fail:
  3369.         xor     eax,eax
  3370.         inc     eax     ; eax = 1 (fail)
  3371.         ret
  3372.  
  3373.  
  3374. sys_window_move:
  3375.  
  3376.         mov     edi,[CURRENT_TASK]
  3377.         shl     edi,5
  3378.         add     edi,window_data
  3379.  
  3380.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3381.         jnz     .window_move_return
  3382.  
  3383.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3384.         push    dword [edi + WDATA.box.top]
  3385.         push    dword [edi + WDATA.box.width]
  3386.         push    dword [edi + WDATA.box.height]
  3387.  
  3388.         cmp   eax,-1                  ; set new position and size
  3389.         je    .no_x_reposition
  3390.         mov     [edi + WDATA.box.left], eax
  3391.       .no_x_reposition:
  3392.         cmp   ebx,-1
  3393.         je    .no_y_reposition
  3394.         mov     [edi + WDATA.box.top], ebx
  3395.       .no_y_reposition:
  3396.  
  3397.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3398.         jnz     .no_y_resizing
  3399.  
  3400.         cmp   ecx,-1
  3401.         je    .no_x_resizing
  3402.         mov     [edi + WDATA.box.width], ecx
  3403.       .no_x_resizing:
  3404.         cmp   edx,-1
  3405.         je    .no_y_resizing
  3406.         mov     [edi + WDATA.box.height], edx
  3407.       .no_y_resizing:
  3408.  
  3409.         call  check_window_position
  3410.         call  set_window_clientbox
  3411.  
  3412.         pushad                       ; save for window fullscreen/resize
  3413.         mov   esi,edi
  3414.         sub   edi,window_data
  3415.         shr   edi,5
  3416.         shl   edi,8
  3417.         add   edi, SLOT_BASE + APPDATA.saved_box
  3418.         mov   ecx,4
  3419.         cld
  3420.         rep   movsd
  3421.         popad
  3422.  
  3423.         pushad                       ; calculcate screen at new position
  3424.         mov   eax, [edi + WDATA.box.left]
  3425.         mov   ebx, [edi + WDATA.box.top]
  3426.         mov   ecx, [edi + WDATA.box.width]
  3427.         mov   edx, [edi + WDATA.box.height]
  3428.         add   ecx,eax
  3429.         add   edx,ebx
  3430.  
  3431.         call  calculatescreen
  3432.         popad
  3433.  
  3434.         pop   edx                   ; calculcate screen at old position
  3435.         pop   ecx
  3436.         pop   ebx
  3437.         pop   eax
  3438.         add   ecx,eax
  3439.         add   edx,ebx
  3440.         mov   [dlx],eax             ; save for drawlimits
  3441.         mov   [dly],ebx
  3442.         mov   [dlxe],ecx
  3443.         mov   [dlye],edx
  3444.         call  calculatescreen
  3445.  
  3446.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3447.  
  3448.         mov   eax,edi               ; redraw screen at old position
  3449.         xor   esi,esi
  3450.         call  redrawscreen
  3451.  
  3452.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3453.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3454.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3455.  
  3456.         call  [draw_pointer]
  3457.  
  3458.         mov   [window_move_pr],0
  3459.  
  3460.       .window_move_return:
  3461.  
  3462.         ret
  3463.  
  3464. uglobal
  3465.   window_move_pr   dd  0x0
  3466.   window_move_eax  dd  0x0
  3467.   window_move_ebx  dd  0x0
  3468.   window_move_ecx  dd  0x0
  3469.   window_move_edx  dd  0x0
  3470. endg
  3471.  
  3472. ;ok - 100% work
  3473. ;nt - not tested
  3474. ;---------------------------------------------------------------------------------------------
  3475. ;eax
  3476. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3477. ;1 - change task. Ret nothing. Block. ok.
  3478. ;2 - performance control
  3479. ; ebx
  3480. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3481. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3482. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3483. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3484. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3485. ;eax
  3486. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3487. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3488. ;---------------------------------------------------------------------------------------------
  3489. sys_sheduler: ;noname & halyavin
  3490.     cmp eax,0
  3491.     je shed_counter
  3492.     cmp eax,2
  3493.     je perf_control
  3494.     cmp eax,3
  3495.     je rdmsr_instr
  3496.     cmp eax,4
  3497.     je wrmsr_instr
  3498.     cmp eax,1
  3499.     jne not_supported
  3500.     call change_task ;delay,0
  3501. ret
  3502. shed_counter:
  3503.     mov eax,[context_counter]
  3504.     mov [esp+36],eax
  3505. not_supported:
  3506. ret
  3507. perf_control:
  3508.     inc eax ;now eax=3
  3509.     cmp ebx,eax
  3510.     je cache_disable
  3511.     dec eax
  3512.     cmp ebx,eax
  3513.     je cache_enable
  3514.     dec eax
  3515.     cmp ebx,eax
  3516.     je is_cache_enabled
  3517.     dec eax
  3518.     cmp ebx,eax
  3519.     je modify_pce
  3520. ret
  3521.  
  3522. rdmsr_instr:
  3523. ;now counter in ecx
  3524. ;(edx:eax) esi:edi => edx:esi
  3525. mov eax,esi
  3526. rdmsr
  3527. mov [esp+36],eax
  3528. mov [esp+24],edx ;ret in ebx?
  3529. ret
  3530.  
  3531. wrmsr_instr:
  3532. ;now counter in ecx
  3533. ;(edx:eax) esi:edi => edx:esi
  3534.         ; Fast Call MSR can't be destroy
  3535.         ; Íî MSR_AMD_EFER ìîæíî èçìåíÿòü, ò.ê. â ýòîì ðåãèñòðå ëèø
  3536.         ; âêëþ÷àþòñÿ/âûêëþ÷àþòñÿ ðàñøèðåííûå âîçìîæíîñòè
  3537.         cmp     ecx, MSR_SYSENTER_CS
  3538.         je      @f
  3539.         cmp     ecx, MSR_SYSENTER_ESP
  3540.         je      @f
  3541.         cmp     ecx, MSR_SYSENTER_EIP
  3542.         je      @f
  3543.         cmp     ecx, MSR_AMD_STAR
  3544.         je      @f
  3545.  
  3546.         mov     eax, esi
  3547.         wrmsr
  3548.         ; mov   [esp + 36], eax
  3549.         ; mov   [esp + 24], edx ;ret in ebx?
  3550. @@:
  3551. ret
  3552.  
  3553. cache_disable:
  3554.        mov eax,cr0
  3555.        or  eax,01100000000000000000000000000000b
  3556.        mov cr0,eax
  3557.        wbinvd ;set MESI
  3558. ret
  3559.  
  3560. cache_enable:
  3561.        mov eax,cr0
  3562.        and eax,10011111111111111111111111111111b
  3563.        mov cr0,eax
  3564. ret
  3565.  
  3566. is_cache_enabled:
  3567.        mov eax,cr0
  3568.        mov ebx,eax
  3569.        and eax,01100000000000000000000000000000b
  3570.        jz cache_disabled
  3571.        mov [esp+36],ebx
  3572. cache_disabled:
  3573.        mov dword [esp+36],eax ;0
  3574. ret
  3575.  
  3576. modify_pce:
  3577.        mov eax,cr4
  3578. ;       mov ebx,0
  3579. ;       or  bx,100000000b ;pce
  3580. ;       xor eax,ebx ;invert pce
  3581.        bts eax,8 ;pce=cr4[8]
  3582.        mov cr4,eax
  3583.        mov [esp+36],eax
  3584. ret
  3585. ;---------------------------------------------------------------------------------------------
  3586.  
  3587.  
  3588. ; check if pixel is allowed to be drawn
  3589.  
  3590. checkpixel:
  3591.         push eax edx
  3592.  
  3593.         mov  edx,[ScreenWidth]     ; screen x size
  3594.         inc  edx
  3595.         imul edx, ebx
  3596.         mov  dl, [eax+edx+display_data] ; lea eax, [...]
  3597.  
  3598.         xor  ecx, ecx
  3599.         mov  eax, [CURRENT_TASK]
  3600.         cmp  al, dl
  3601.         setne cl
  3602.  
  3603.         pop  edx eax
  3604.         ret
  3605.  
  3606. iglobal
  3607.   cpustring db 'CPU',0
  3608. endg
  3609.  
  3610. uglobal
  3611. background_defined    db    0    ; diamond, 11.04.2006
  3612. endg
  3613.  
  3614. align 4
  3615. ; check misc
  3616.  
  3617. checkmisc:
  3618.  
  3619.     cmp   [ctrl_alt_del], 1
  3620.     jne   nocpustart
  3621.  
  3622.         mov     ebp, cpustring
  3623.         call    fs_execute_from_sysdir
  3624.  
  3625.     mov   [ctrl_alt_del], 0
  3626.  
  3627. nocpustart:
  3628.     cmp   [mouse_active], 1
  3629.     jne   mouse_not_active
  3630.     mov   [mouse_active], 0
  3631.     xor   edi, edi
  3632.     mov   ecx,  [TASK_COUNT]
  3633. set_mouse_event:
  3634.     add   edi, 256
  3635.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3636.     loop  set_mouse_event
  3637.  
  3638. mouse_not_active:
  3639.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3640.     jz    nobackgr
  3641.     cmp    [background_defined], 0
  3642.     jz    nobackgr
  3643. ;    mov   [REDRAW_BACKGROUND],byte 2
  3644. ;    call  change_task
  3645.     xor   edi, edi
  3646.     mov   ecx,  [TASK_COUNT]
  3647. set_bgr_event:
  3648.     add   edi, 256
  3649.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3650.     loop  set_bgr_event
  3651.     mov   [draw_data+32 + RECT.left],dword 0
  3652.     mov   [draw_data+32 + RECT.top],dword 0
  3653.     mov   eax,[ScreenWidth]
  3654.     mov   ebx,[ScreenHeight]
  3655.     mov   [draw_data+32 + RECT.right],eax
  3656.     mov   [draw_data+32 + RECT.bottom],ebx
  3657.     call  drawbackground
  3658.     mov   [REDRAW_BACKGROUND],byte 0
  3659.     mov   [MOUSE_BACKGROUND],byte 0
  3660.  
  3661. nobackgr:
  3662.  
  3663.     ; system shutdown request
  3664.  
  3665.     cmp  [SYS_SHUTDOWN],byte 0
  3666.     je   noshutdown
  3667.  
  3668.     mov  edx,[shutdown_processes]
  3669.     sub  dl,2
  3670.  
  3671.     cmp  [SYS_SHUTDOWN],dl
  3672.     jne  no_mark_system_shutdown
  3673.  
  3674.     mov   edx,OS_BASE+0x3040
  3675.     movzx ecx,byte [SYS_SHUTDOWN]
  3676.     add   ecx,5
  3677. markz:
  3678.     mov   [edx+TASKDATA.state],byte 3
  3679.     add   edx,0x20
  3680.     loop  markz
  3681.  
  3682.   no_mark_system_shutdown:
  3683.  
  3684.     call [disable_mouse]
  3685.  
  3686.     dec  byte [SYS_SHUTDOWN]
  3687.  
  3688.     cmp  [SYS_SHUTDOWN],byte 0
  3689.     je   system_shutdown
  3690.  
  3691. noshutdown:
  3692.  
  3693.  
  3694.     mov   eax,[TASK_COUNT]                  ; termination
  3695.     mov   ebx,TASK_DATA+TASKDATA.state
  3696.     mov   esi,1
  3697.  
  3698. newct:
  3699.     mov   cl,[ebx]
  3700.     cmp   cl,byte 3
  3701.     jz    terminate
  3702.     cmp   cl,byte 4
  3703.     jz    terminate
  3704.  
  3705.     add   ebx,0x20
  3706.     inc   esi
  3707.     dec   eax
  3708.     jnz   newct
  3709.  
  3710.     ret
  3711.  
  3712.  
  3713. ; redraw screen
  3714.  
  3715. redrawscreen:
  3716.  
  3717. ; eax , if process window_data base is eax, do not set flag/limits
  3718.  
  3719.          pushad
  3720.          push  eax
  3721.  
  3722. ;;;         mov   eax,2
  3723. ;;;         call  delay_hs
  3724.  
  3725.          ;mov   ecx,0               ; redraw flags for apps
  3726.          xor   ecx,ecx
  3727.        newdw2:
  3728.  
  3729.          inc   ecx
  3730.          push  ecx
  3731.  
  3732.          mov   eax,ecx
  3733.          shl   eax,5
  3734.          add   eax,window_data
  3735.  
  3736.          cmp   eax,[esp+4]
  3737.          je    not_this_task
  3738.                                    ; check if window in redraw area
  3739.          mov   edi,eax
  3740.  
  3741.          cmp   ecx,1               ; limit for background
  3742.          jz    bgli
  3743.  
  3744.          mov   eax, [edi + WDATA.box.left]
  3745.          mov   ebx, [edi + WDATA.box.top]
  3746.          mov   ecx, [edi + WDATA.box.width]
  3747.          mov   edx, [edi + WDATA.box.height]
  3748.          add   ecx,eax
  3749.          add   edx,ebx
  3750.  
  3751.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3752.          cmp   ecx,ebx
  3753.          jb    ricino
  3754.  
  3755.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3756.          cmp   ecx,eax
  3757.          jb    ricino
  3758.  
  3759.          mov   eax, [edi + WDATA.box.left]
  3760.          mov   ebx, [edi + WDATA.box.top]
  3761.          mov   ecx, [edi + WDATA.box.width]
  3762.          mov   edx, [edi + WDATA.box.height]
  3763.          add   ecx, eax
  3764.          add   edx, ebx
  3765.  
  3766.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3767.          cmp   edx,eax
  3768.          jb    ricino
  3769.  
  3770.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3771.          cmp   ecx,eax
  3772.          jb    ricino
  3773.  
  3774.         bgli:
  3775.  
  3776.          cmp   edi,esi
  3777.          jz    ricino
  3778.  
  3779.          mov   eax,edi
  3780.          add   eax,draw_data-window_data
  3781.  
  3782.          mov   ebx,[dlx]          ; set limits
  3783.          mov   [eax + RECT.left], ebx
  3784.          mov   ebx,[dly]
  3785.          mov   [eax + RECT.top], ebx
  3786.          mov   ebx,[dlxe]
  3787.          mov   [eax + RECT.right], ebx
  3788.          mov   ebx,[dlye]
  3789.          mov   [eax + RECT.bottom], ebx
  3790.  
  3791.          sub   eax,draw_data-window_data
  3792.  
  3793.          cmp   ecx,1
  3794.          jne   nobgrd
  3795.          cmp   esi,1
  3796.          je    newdw8
  3797.          call  drawbackground
  3798.  
  3799.        newdw8:
  3800.        nobgrd:
  3801.  
  3802.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3803.  
  3804.        ricino:
  3805.  
  3806.        not_this_task:
  3807.  
  3808.          pop   ecx
  3809.  
  3810.          cmp   ecx,[TASK_COUNT]
  3811.          jle   newdw2
  3812.  
  3813.          pop  eax
  3814.          popad
  3815.  
  3816.          ret
  3817.  
  3818. calculatebackground:   ; background
  3819.  
  3820.         ; all black
  3821.  
  3822.         mov   edi, [img_background]  ;IMG_BACKGROUND                 ; set background to black
  3823.         xor   eax, eax
  3824.         mov   ecx, 1023    ;0x0fff00 / 4
  3825.         cld
  3826.         rep   stosd
  3827.  
  3828.         mov   edi,display_data              ; set os to use all pixels
  3829.         mov   eax,0x01010101
  3830.         mov   ecx,1280*1024 / 4
  3831.         rep   stosd
  3832.  
  3833.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3834.  
  3835.         ret
  3836.  
  3837. uglobal
  3838.   imax    dd 0x0
  3839. endg
  3840.  
  3841.  
  3842.  
  3843. delay_ms:     ; delay in 1/1000 sec
  3844.  
  3845.  
  3846.         push  eax
  3847.         push  ecx
  3848.  
  3849.         mov   ecx,esi
  3850.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3851.         imul  ecx, 33941
  3852.         shr   ecx, 9
  3853.         ; </CPU clock fix>
  3854.  
  3855.         in    al,0x61
  3856.         and   al,0x10
  3857.         mov   ah,al
  3858.         cld
  3859.  
  3860.  cnt1:  in    al,0x61
  3861.         and   al,0x10
  3862.         cmp   al,ah
  3863.         jz    cnt1
  3864.  
  3865.         mov   ah,al
  3866.         loop  cnt1
  3867.  
  3868.         pop   ecx
  3869.         pop   eax
  3870.  
  3871.         ret
  3872.  
  3873.  
  3874. set_app_param:
  3875.         push edi
  3876.  
  3877.         mov  edi,[TASK_BASE]
  3878.         mov  [edi+TASKDATA.event_mask],eax
  3879.  
  3880.         pop  edi
  3881.         ret
  3882.  
  3883.  
  3884.  
  3885. delay_hs:     ; delay in 1/100 secs
  3886.         push  eax
  3887.         push  ecx
  3888.         push  edx
  3889.  
  3890.         mov   edx,[timer_ticks]
  3891.         add   edx,eax
  3892.  
  3893.       newtic:
  3894.         mov   ecx,[timer_ticks]
  3895.         cmp   edx,ecx
  3896.         jbe   zerodelay
  3897.  
  3898.         call  change_task
  3899.  
  3900.         jmp   newtic
  3901.  
  3902.       zerodelay:
  3903.         pop   edx
  3904.         pop   ecx
  3905.         pop   eax
  3906.  
  3907.         ret
  3908.  
  3909.  
  3910. memmove:       ; memory move in bytes
  3911.  
  3912. ; eax = from
  3913. ; ebx = to
  3914. ; ecx = no of bytes
  3915.     test ecx, ecx
  3916.     jle  .ret
  3917.  
  3918.  
  3919.     push esi edi ecx
  3920.  
  3921.     mov  edi, ebx
  3922.     mov  esi, eax
  3923.  
  3924.     test ecx, not 11b
  3925.     jz   @f
  3926.  
  3927.     push ecx
  3928.     shr  ecx, 2
  3929.     rep  movsd
  3930.     pop  ecx
  3931.     and  ecx, 11b
  3932.     jz   .finish
  3933.   @@:
  3934.     rep  movsb
  3935.  
  3936.   .finish:
  3937.     pop  ecx edi esi
  3938.   .ret:
  3939.     ret
  3940.  
  3941.  
  3942. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3943. ;align 4
  3944. ;
  3945. ;read_floppy_file:
  3946. ;
  3947. ;; as input
  3948. ;;
  3949. ;; eax pointer to file
  3950. ;; ebx file lenght
  3951. ;; ecx start 512 byte block number
  3952. ;; edx number of blocks to read
  3953. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3954. ;;
  3955. ;;
  3956. ;; on return
  3957. ;;
  3958. ;; eax = 0 command succesful
  3959. ;;       1 no fd base and/or partition defined
  3960. ;;       2 yet unsupported FS
  3961. ;;       3 unknown FS
  3962. ;;       4 partition not defined at hd
  3963. ;;       5 file not found
  3964. ;; ebx = size of file
  3965. ;
  3966. ;     mov   edi,[TASK_BASE]
  3967. ;     add   edi,0x10
  3968. ;     add   esi,[edi]
  3969. ;     add   eax,[edi]
  3970. ;
  3971. ;     pushad
  3972. ;     mov  edi,esi
  3973. ;     add  edi,1024
  3974. ;     mov  esi,0x100000+19*512
  3975. ;     sub  ecx,1
  3976. ;     shl  ecx,9
  3977. ;     add  esi,ecx
  3978. ;     shl  edx,9
  3979. ;     mov  ecx,edx
  3980. ;     cld
  3981. ;     rep  movsb
  3982. ;     popad
  3983. ;
  3984. ;     mov   [esp+36],eax
  3985. ;     mov   [esp+24],ebx
  3986. ;     ret
  3987.  
  3988.  
  3989.  
  3990. align 4
  3991.  
  3992. sys_programirq:
  3993.  
  3994.     mov   edi,[TASK_BASE]
  3995.     add   eax,[edi+TASKDATA.mem_start]
  3996.  
  3997.     cmp   ebx,16
  3998.     jae   .not_owner
  3999.     mov   edi,[TASK_BASE]
  4000.     mov   edi,[edi+TASKDATA.pid]
  4001.     cmp   edi,[irq_owner+ebx*4]
  4002.     je    spril1
  4003. .not_owner:
  4004.     mov   [esp+36],dword 1
  4005.     ret
  4006.   spril1:
  4007.  
  4008.     mov   esi,eax
  4009.     shl   ebx,6
  4010.     add   ebx,irq00read
  4011.     mov   edi,ebx
  4012.     mov   ecx,16
  4013.     cld
  4014.     rep   movsd
  4015.     mov   [esp+36],dword 0
  4016.     ret
  4017.  
  4018.  
  4019. align 4
  4020.  
  4021. get_irq_data:
  4022.      cmp   eax,16
  4023.      jae   .not_owner
  4024.      mov   edx,eax           ; check for correct owner
  4025.      shl   edx,2
  4026.      add   edx,irq_owner
  4027.      mov   edx,[edx]
  4028.      mov   edi,[TASK_BASE]
  4029.      mov   edi,[edi+TASKDATA.pid]
  4030.      cmp   edx,edi
  4031.      je    gidril1
  4032. .not_owner:
  4033.      mov   [esp+32],dword 2     ; ecx=2
  4034.      ret
  4035.  
  4036.   gidril1:
  4037.  
  4038.      mov   ebx,eax
  4039.      shl   ebx,12
  4040.      add   ebx,IRQ_SAVE
  4041.      mov   eax,[ebx]
  4042.      mov   ecx,1
  4043.      test  eax,eax
  4044.      jz    gid1
  4045.  
  4046.      dec   eax
  4047.      mov   esi,ebx
  4048.      mov   [ebx],eax
  4049.      movzx ebx,byte [ebx+0x10]
  4050.      add   esi,0x10
  4051.      mov   edi,esi
  4052.      inc   esi
  4053.      mov   ecx,4000 / 4
  4054.      cld
  4055.      rep   movsd
  4056. ;     xor   ecx,ecx     ; as result of 'rep' ecx=0
  4057.    gid1:
  4058.      mov   [esp+36],eax
  4059.      mov   [esp+32],ecx
  4060.      mov   [esp+24],ebx
  4061.      ret
  4062.  
  4063.  
  4064. set_io_access_rights:
  4065.  
  4066.      pushad
  4067.  
  4068.      mov edi, tss._io_map_0
  4069.  
  4070. ;     mov   ecx,eax
  4071. ;     and   ecx,7    ; offset in byte
  4072.  
  4073. ;     shr   eax,3    ; number of byte
  4074. ;     add   edi,eax
  4075.  
  4076. ;     mov   ebx,1
  4077. ;     shl   ebx,cl
  4078.  
  4079.      cmp   ebp,0                ; enable access - ebp = 0
  4080.      jne   siar1
  4081.  
  4082. ;     not   ebx
  4083. ;     and   [edi],byte bl
  4084.      btr [edi], eax
  4085.  
  4086.      popad
  4087.  
  4088.      ret
  4089.  
  4090. siar1:
  4091.  
  4092.      bts [edi], eax
  4093.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4094.  
  4095.      popad
  4096.  
  4097.      ret
  4098.  
  4099. r_f_port_area:
  4100.  
  4101.      test  eax, eax
  4102.      jnz   free_port_area
  4103. ;     je    r_port_area
  4104. ;     jmp   free_port_area
  4105.  
  4106. ;   r_port_area:
  4107.  
  4108.      pushad
  4109.  
  4110.      cmp   ebx,ecx            ; beginning > end ?
  4111.      ja    rpal1
  4112.      cmp   ecx,65536
  4113.      jae   rpal1
  4114.      mov   esi,[RESERVED_PORTS]
  4115.      test  esi,esi            ; no reserved areas ?
  4116.      je    rpal2
  4117.      cmp   esi,255            ; max reserved
  4118.      jae   rpal1
  4119.  rpal3:
  4120.      mov   edi,esi
  4121.      shl   edi,4
  4122.      add   edi,RESERVED_PORTS
  4123.      cmp   ebx,[edi+8]
  4124.      ja    rpal4
  4125.      cmp   ecx,[edi+4]
  4126.      jae   rpal1
  4127. ;     jb    rpal4
  4128. ;     jmp   rpal1
  4129.  rpal4:
  4130.  
  4131.      dec   esi
  4132.      jnz   rpal3
  4133.      jmp   rpal2
  4134.    rpal1:
  4135.      popad
  4136.      mov   eax,1
  4137.      ret
  4138.  
  4139.    rpal2:
  4140.      popad
  4141.  
  4142.  
  4143.      ; enable port access at port IO map
  4144.      cli
  4145.      pushad                        ; start enable io map
  4146.  
  4147.      cmp   ecx,65536 ;16384
  4148.      jae   no_unmask_io ; jge
  4149.  
  4150.      mov   eax,ebx
  4151.  
  4152.    new_port_access:
  4153.  
  4154.      pushad
  4155.  
  4156.      xor   ebp,ebp                ; enable - eax = port
  4157.      call  set_io_access_rights
  4158.  
  4159.      popad
  4160.  
  4161.      inc   eax
  4162.      cmp   eax,ecx
  4163.      jbe   new_port_access
  4164.  
  4165.    no_unmask_io:
  4166.  
  4167.      popad                         ; end enable io map
  4168.      sti
  4169.  
  4170.      mov   edi,[RESERVED_PORTS]
  4171.      add   edi,1
  4172.      mov   [RESERVED_PORTS],edi
  4173.      shl   edi,4
  4174.      add   edi,RESERVED_PORTS
  4175.      mov   esi,[TASK_BASE]
  4176.      mov   esi,[esi+TASKDATA.pid]
  4177.      mov   [edi],esi
  4178.      mov   [edi+4],ebx
  4179.      mov   [edi+8],ecx
  4180.  
  4181.      xor   eax, eax
  4182.      ret
  4183.  
  4184. free_port_area:
  4185.  
  4186.      pushad
  4187.  
  4188.      mov   esi,[RESERVED_PORTS]     ; no reserved areas ?
  4189.      test  esi,esi
  4190.      je    frpal2
  4191.      mov   edx,[TASK_BASE]
  4192.      mov   edx,[edx+TASKDATA.pid]
  4193.    frpal3:
  4194.      mov   edi,esi
  4195.      shl   edi,4
  4196.      add   edi,RESERVED_PORTS
  4197.      cmp   edx,[edi]
  4198.      jne   frpal4
  4199.      cmp   ebx,[edi+4]
  4200.      jne   frpal4
  4201.      cmp   ecx,[edi+8]
  4202.      jne   frpal4
  4203.      jmp   frpal1
  4204.    frpal4:
  4205.      dec   esi
  4206.      jnz   frpal3
  4207.    frpal2:
  4208.      popad
  4209.      mov   eax,1
  4210.      ret
  4211.    frpal1:
  4212.      mov   ecx,256
  4213.      sub   ecx,esi
  4214.      shl   ecx,4
  4215.      mov   esi,edi
  4216.      add   esi,16
  4217.      cld
  4218.      rep   movsb
  4219.  
  4220.      dec   dword [RESERVED_PORTS]
  4221.  
  4222.      popad
  4223.  
  4224.  
  4225.      ; disable port access at port IO map
  4226.  
  4227.      pushad                        ; start disable io map
  4228.  
  4229.      cmp   ecx,65536 ;16384
  4230.      jge   no_mask_io
  4231.  
  4232.      mov   eax,ebx
  4233.  
  4234.    new_port_access_disable:
  4235.  
  4236.      pushad
  4237.  
  4238.      mov   ebp,1                  ; disable - eax = port
  4239.      call  set_io_access_rights
  4240.  
  4241.      popad
  4242.  
  4243.      inc   eax
  4244.      cmp   eax,ecx
  4245.      jbe   new_port_access_disable
  4246.  
  4247.    no_mask_io:
  4248.  
  4249.      popad                         ; end disable io map
  4250.  
  4251.      xor   eax, eax
  4252.      ret
  4253.  
  4254.  
  4255. reserve_free_irq:
  4256.  
  4257.      mov   ecx, 1
  4258.      cmp   ebx, 16
  4259.      jae   fril1
  4260.      test  eax,eax
  4261.      jz    reserve_irq
  4262.  
  4263.      lea   edi,[irq_owner+ebx*4]
  4264.      mov   edx,[edi]
  4265.      mov   eax,[TASK_BASE]
  4266.      cmp   edx,[eax+TASKDATA.pid]
  4267.      jne   fril1
  4268.      dec   ecx
  4269.      mov   [edi],ecx
  4270.    fril1:
  4271.      mov   [esp+36],ecx ; return in eax
  4272.      ret
  4273.  
  4274.   reserve_irq:
  4275.  
  4276.      lea   edi,[irq_owner+ebx*4]
  4277.      cmp   dword [edi], 0
  4278.      jnz   ril1
  4279.  
  4280.      mov   edx,[TASK_BASE]
  4281.      mov   edx,[edx+TASKDATA.pid]
  4282.      mov   [edi],edx
  4283.      dec   ecx
  4284.    ril1:
  4285.      mov   [esp+36],ecx ; return in eax
  4286.      ret
  4287.  
  4288. drawbackground:
  4289.        inc   [mouse_pause]
  4290.        cmp   [SCR_MODE],word 0x12
  4291.        je   dbrv20
  4292.      dbrv12:
  4293.        cmp  [SCR_MODE],word 0100000000000000b
  4294.        jge  dbrv20
  4295.        cmp  [SCR_MODE],word 0x13
  4296.        je   dbrv20
  4297.        call  vesa12_drawbackground
  4298.        dec   [mouse_pause]
  4299.        call   [draw_pointer]
  4300.        ret
  4301.      dbrv20:
  4302.        cmp   [BgrDrawMode],dword 1
  4303.        jne   bgrstr
  4304.        call  vesa20_drawbackground_tiled
  4305.        dec   [mouse_pause]
  4306.        call   [draw_pointer]
  4307.        ret
  4308.      bgrstr:
  4309.        call  vesa20_drawbackground_stretch
  4310.        dec   [mouse_pause]
  4311.        call   [draw_pointer]
  4312.        ret
  4313.  
  4314. align 4
  4315.  
  4316. syscall_putimage:                       ; PutImage
  4317.  
  4318.      mov   edx,ecx
  4319.      mov   ecx,ebx
  4320.      mov   ebx, eax
  4321.  
  4322. sys_putimage:
  4323.      test  ecx,0x80008000
  4324.      jnz   .exit
  4325.      test  ecx,0x0000FFFF
  4326.      jz    .exit
  4327.      test  ecx,0xFFFF0000
  4328.      jnz   @f
  4329.   .exit:
  4330.      ret
  4331.  @@:
  4332.         mov     edi,[current_slot]
  4333.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4334.         rol     edx,16
  4335.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4336.         rol     edx,16
  4337.   .forced:
  4338.         push    ebp esi 0
  4339.         mov     ebp, putimage_get24bpp
  4340.         mov     esi, putimage_init24bpp
  4341. sys_putimage_bpp:
  4342. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4343. ;        mov     eax, vga_putimage
  4344.         cmp     [SCR_MODE], word 0x12
  4345.         jz      @f   ;.doit
  4346.         mov     eax, vesa12_putimage
  4347.         cmp     [SCR_MODE], word 0100000000000000b
  4348.         jae     @f
  4349.         cmp     [SCR_MODE], word 0x13
  4350.         jnz     .doit
  4351. @@:
  4352.         mov     eax, vesa20_putimage
  4353. .doit:
  4354.         inc     [mouse_pause]
  4355.         call    eax
  4356.         dec     [mouse_pause]
  4357.         pop     ebp esi ebp
  4358.         jmp     [draw_pointer]
  4359.  
  4360. syscall_putimage_palette:
  4361.         mov     edi, esi
  4362.         mov     esi, edx
  4363.         mov     edx, ecx
  4364.         mov     ecx, ebx
  4365.         mov     ebx, eax
  4366. sys_putimage_palette:
  4367. ; ebx = pointer to image
  4368. ; ecx = [xsize]*65536 + [ysize]
  4369. ; edx = [xstart]*65536 + [ystart]
  4370. ; esi = number of bits per pixel, must be 8, 24 or 32
  4371. ; edi = pointer to palette
  4372. ; ebp = row delta
  4373.         mov     eax, [CURRENT_TASK]
  4374.         shl     eax, 8
  4375.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4376.         rol     edx, 16
  4377.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4378.         rol     edx, 16
  4379. .forced:
  4380.         push    ebp esi ebp
  4381.         cmp     esi, 8
  4382.         jnz     @f
  4383.         mov     ebp, putimage_get8bpp
  4384.         mov     esi, putimage_init8bpp
  4385.         jmp     sys_putimage_bpp
  4386. @@:
  4387.         cmp     esi, 24
  4388.         jnz     @f
  4389.         mov     ebp, putimage_get24bpp
  4390.         mov     esi, putimage_init24bpp
  4391.         jmp     sys_putimage_bpp
  4392. @@:
  4393.         cmp     esi, 32
  4394.         jnz     @f
  4395.         mov     ebp, putimage_get32bpp
  4396.         mov     esi, putimage_init32bpp
  4397.         jmp     sys_putimage_bpp
  4398. @@:
  4399.         pop     ebp esi
  4400.         ret
  4401.  
  4402. putimage_init24bpp:
  4403.         lea     eax, [eax*3]
  4404. putimage_init8bpp:
  4405.         ret
  4406.  
  4407. putimage_get24bpp:
  4408.         mov     eax, [esi]
  4409.         add     esi, 3
  4410.         ret     4
  4411. putimage_get8bpp:
  4412.         movzx   eax, byte [esi]
  4413.         push    edx
  4414.         mov     edx, [esp+8]
  4415.         mov     eax, [edx+eax*4]
  4416.         pop     edx
  4417.         inc     esi
  4418.         ret     4
  4419.  
  4420. putimage_init32bpp:
  4421.         shl     eax, 2
  4422.         ret
  4423. putimage_get32bpp:
  4424.         lodsd
  4425.         ret     4
  4426.  
  4427. ; eax x beginning
  4428. ; ebx y beginning
  4429. ; ecx x end
  4430.         ; edx y end
  4431. ; edi color
  4432.  
  4433. __sys_drawbar:
  4434.         mov     esi,[current_slot]
  4435.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4436.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4437.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4438.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4439.   .forced:
  4440.     inc   [mouse_pause]
  4441. ;        call    [disable_mouse]
  4442.     cmp   [SCR_MODE],word 0x12
  4443.     je   dbv20
  4444.    sdbv20:
  4445.     cmp  [SCR_MODE],word 0100000000000000b
  4446.     jge  dbv20
  4447.     cmp  [SCR_MODE],word 0x13
  4448.     je   dbv20
  4449.     call vesa12_drawbar
  4450.     dec   [mouse_pause]
  4451.     call   [draw_pointer]
  4452.     ret
  4453.   dbv20:
  4454.     call vesa20_drawbar
  4455.     dec   [mouse_pause]
  4456.     call   [draw_pointer]
  4457.     ret
  4458.  
  4459.  
  4460.  
  4461. kb_read:
  4462.  
  4463.         push    ecx edx
  4464.  
  4465.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4466.       kr_loop:
  4467.         in      al,0x64
  4468.         test    al,1
  4469.         jnz     kr_ready
  4470.         loop    kr_loop
  4471.         mov     ah,1
  4472.         jmp     kr_exit
  4473.       kr_ready:
  4474.         push    ecx
  4475.         mov     ecx,32
  4476.       kr_delay:
  4477.         loop    kr_delay
  4478.         pop     ecx
  4479.         in      al,0x60
  4480.         xor     ah,ah
  4481.       kr_exit:
  4482.  
  4483.         pop     edx ecx
  4484.  
  4485.         ret
  4486.  
  4487.  
  4488. kb_write:
  4489.  
  4490.         push    ecx edx
  4491.  
  4492.         mov     dl,al
  4493. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4494. ;      kw_loop1:
  4495. ;        in      al,0x64
  4496. ;        test    al,0x20
  4497. ;        jz      kw_ok1
  4498. ;        loop    kw_loop1
  4499. ;        mov     ah,1
  4500. ;        jmp     kw_exit
  4501. ;      kw_ok1:
  4502.         in      al,0x60
  4503.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4504.       kw_loop:
  4505.         in      al,0x64
  4506.         test    al,2
  4507.         jz      kw_ok
  4508.         loop    kw_loop
  4509.         mov     ah,1
  4510.         jmp     kw_exit
  4511.       kw_ok:
  4512.         mov     al,dl
  4513.         out     0x60,al
  4514.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4515.       kw_loop3:
  4516.         in      al,0x64
  4517.         test    al,2
  4518.         jz      kw_ok3
  4519.         loop    kw_loop3
  4520.         mov     ah,1
  4521.         jmp     kw_exit
  4522.       kw_ok3:
  4523.         mov     ah,8
  4524.       kw_loop4:
  4525.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4526.       kw_loop5:
  4527.         in      al,0x64
  4528.         test    al,1
  4529.         jnz     kw_ok4
  4530.         loop    kw_loop5
  4531.         dec     ah
  4532.         jnz     kw_loop4
  4533.       kw_ok4:
  4534.         xor     ah,ah
  4535.       kw_exit:
  4536.  
  4537.         pop     edx ecx
  4538.  
  4539.         ret
  4540.  
  4541.  
  4542. kb_cmd:
  4543.  
  4544.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4545.       c_wait:
  4546.         in      al,0x64
  4547.         test    al,2
  4548.         jz      c_send
  4549.         loop    c_wait
  4550.         jmp     c_error
  4551.       c_send:
  4552.         mov     al,bl
  4553.         out     0x64,al
  4554.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4555.       c_accept:
  4556.         in      al,0x64
  4557.         test    al,2
  4558.         jz      c_ok
  4559.         loop    c_accept
  4560.       c_error:
  4561.         mov     ah,1
  4562.         jmp     c_exit
  4563.       c_ok:
  4564.         xor     ah,ah
  4565.       c_exit:
  4566.         ret
  4567.  
  4568.  
  4569. setmouse:  ; set mousepicture -pointer
  4570.            ; ps2 mouse enable
  4571.  
  4572.      mov     [MOUSE_PICTURE],dword mousepointer
  4573.  
  4574.      cli
  4575. ;     mov     bl,0xa8                 ; enable mouse cmd
  4576. ;     call    kb_cmd
  4577. ;     call    kb_read                 ; read status
  4578. ;     mov     bl,0x20                 ; get command byte
  4579. ;     call    kb_cmd
  4580. ;     call    kb_read
  4581. ;     or      al,3                    ; enable interrupt
  4582. ;     mov     bl,0x60                 ; write command
  4583. ;     push    eax
  4584. ;     call    kb_cmd
  4585. ;     pop     eax
  4586. ;     call    kb_write
  4587. ;     mov     bl,0xd4                 ; for mouse
  4588. ;     call    kb_cmd
  4589. ;     mov     al,0xf4                 ; enable mouse device
  4590. ;     call    kb_write
  4591. ;     call    kb_read           ; read status return
  4592.  
  4593.      ; com1 mouse enable
  4594.  
  4595.      mov   bx,0x3f8 ; combase
  4596.  
  4597.      mov   dx,bx
  4598.      add   dx,3
  4599.      mov   al,0x80
  4600.      out   dx,al
  4601.  
  4602.      mov   dx,bx
  4603.      add   dx,1
  4604.      mov   al,0
  4605.      out   dx,al
  4606.  
  4607.      mov   dx,bx
  4608.      add   dx,0
  4609.      mov   al,0x30*2    ; 0x30 / 4
  4610.      out   dx,al
  4611.  
  4612.      mov   dx,bx
  4613.      add   dx,3
  4614.      mov   al,2         ; 3
  4615.      out   dx,al
  4616.  
  4617.      mov   dx,bx
  4618.      add   dx,4
  4619.      mov   al,0xb
  4620.      out   dx,al
  4621.  
  4622.      mov   dx,bx
  4623.      add   dx,1
  4624.      mov   al,1
  4625.      out   dx,al
  4626.  
  4627.  
  4628.      ; com2 mouse enable
  4629.  
  4630.      mov   bx,0x2f8 ; combase
  4631.  
  4632.      mov   dx,bx
  4633.      add   dx,3
  4634.      mov   al,0x80
  4635.      out   dx,al
  4636.  
  4637.      mov   dx,bx
  4638.      add   dx,1
  4639.      mov   al,0
  4640.      out   dx,al
  4641.  
  4642.      mov   dx,bx
  4643.      add   dx,0
  4644.      mov   al,0x30*2
  4645.      out   dx,al
  4646.  
  4647.      mov   dx,bx
  4648.      add   dx,3
  4649.      mov   al,2
  4650.      out   dx,al
  4651.  
  4652.      mov   dx,bx
  4653.      add   dx,4
  4654.      mov   al,0xb
  4655.      out   dx,al
  4656.  
  4657.      mov   dx,bx
  4658.      add   dx,1
  4659.      mov   al,1
  4660.      out   dx,al
  4661.  
  4662.      ret
  4663.  
  4664.  
  4665. _rdtsc:
  4666.      bt [cpu_caps], CAPS_TSC
  4667.      jnc ret_rdtsc
  4668.      rdtsc
  4669.      ret
  4670.    ret_rdtsc:
  4671.      mov   edx,0xffffffff
  4672.      mov   eax,0xffffffff
  4673.      ret
  4674.  
  4675. rerouteirqs:
  4676.  
  4677.         cli
  4678.  
  4679.         mov     al,0x11         ;  icw4, edge triggered
  4680.         out     0x20,al
  4681.         call    pic_delay
  4682.         out     0xA0,al
  4683.         call    pic_delay
  4684.  
  4685.         mov     al,0x20         ;  generate 0x20 +
  4686.         out     0x21,al
  4687.         call    pic_delay
  4688.         mov     al,0x28         ;  generate 0x28 +
  4689.         out     0xA1,al
  4690.         call    pic_delay
  4691.  
  4692.         mov     al,0x04         ;  slave at irq2
  4693.         out     0x21,al
  4694.         call    pic_delay
  4695.         mov     al,0x02         ;  at irq9
  4696.         out     0xA1,al
  4697.         call    pic_delay
  4698.  
  4699.         mov     al,0x01         ;  8086 mode
  4700.         out     0x21,al
  4701.         call    pic_delay
  4702.         out     0xA1,al
  4703.         call    pic_delay
  4704.  
  4705.         mov     al,255          ; mask all irq's
  4706.         out     0xA1,al
  4707.         call    pic_delay
  4708.         out     0x21,al
  4709.         call    pic_delay
  4710.  
  4711.         mov     ecx,0x1000
  4712.         cld
  4713. picl1:  call    pic_delay
  4714.         loop    picl1
  4715.  
  4716.         mov     al,255          ; mask all irq's
  4717.         out     0xA1,al
  4718.         call    pic_delay
  4719.         out     0x21,al
  4720.         call    pic_delay
  4721.  
  4722.         cli
  4723.  
  4724.         ret
  4725.  
  4726.  
  4727. pic_delay:
  4728.  
  4729.         jmp     pdl1
  4730. pdl1:   ret
  4731.  
  4732.  
  4733. sys_msg_board_str:
  4734.  
  4735.      pushad
  4736.    @@:
  4737.      cmp    [esi],byte 0
  4738.      je     @f
  4739.      mov    eax,1
  4740.      movzx  ebx,byte [esi]
  4741.      call   sys_msg_board
  4742.      inc    esi
  4743.      jmp    @b
  4744.    @@:
  4745.      popad
  4746.      ret
  4747.  
  4748. uglobal
  4749.   msg_board_data: times 4096 db 0
  4750.   msg_board_count dd 0x0
  4751. endg
  4752.  
  4753. sys_msg_board:
  4754.  
  4755. ; eax=1 : write :  bl byte to write
  4756. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4757.  
  4758.      mov  ecx,[msg_board_count]
  4759.      cmp  eax, 1
  4760.      jne  smbl1
  4761.  
  4762.  
  4763.      mov  [msg_board_data+ecx],bl
  4764.      inc  ecx
  4765.      and  ecx, 4095
  4766.      mov  [msg_board_count], ecx
  4767.      mov  [check_idle_semaphore], 5
  4768.      ret
  4769.    smbl1:
  4770.  
  4771.      cmp   eax, 2
  4772.      jne   smbl2
  4773.      test  ecx, ecx
  4774.      jz    smbl21
  4775. ;     mov   edi, msg_board_data
  4776. ;     mov   esi, msg_board_data+1
  4777. ;     movzx eax, byte [edi]
  4778.      mov   eax, msg_board_data+1
  4779.      mov   ebx, msg_board_data
  4780.      movzx edx, byte [ebx]
  4781.      call  memmove
  4782. ;     push  ecx
  4783. ;     shr   ecx, 2
  4784. ;     cld
  4785. ;     rep   movsd
  4786. ;     pop   ecx
  4787. ;     and   ecx, 3
  4788. ;     rep   movsb
  4789.      dec   [msg_board_count]
  4790.      mov   [esp+36], edx ;eax
  4791.      mov   [esp+24], dword 1
  4792.      ret
  4793.    smbl21:
  4794.      mov   [esp+36], ecx
  4795.      mov   [esp+24], ecx
  4796.  
  4797.    smbl2:
  4798.      ret
  4799.  
  4800.  
  4801.  
  4802. sys_process_def:
  4803.         mov     edi, [CURRENT_TASK]
  4804.  
  4805.         dec     eax             ; 1 = set keyboard mode
  4806.      jne   no_set_keyboard_setup
  4807.  
  4808.      shl   edi,8
  4809.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4810.  
  4811.      ret
  4812.  
  4813.    no_set_keyboard_setup:
  4814.  
  4815.         dec     eax             ; 2 = get keyboard mode
  4816.      jne   no_get_keyboard_setup
  4817.  
  4818.      shl   edi,8
  4819.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4820.  
  4821.      mov   [esp+36],eax
  4822.  
  4823.      ret
  4824.  
  4825.    no_get_keyboard_setup:
  4826.  
  4827.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4828.      jne   no_get_keyboard_cas
  4829.  
  4830. ;     xor   eax,eax
  4831. ;     movzx eax,byte [shift]
  4832. ;     movzx ebx,byte [ctrl]
  4833. ;     shl   ebx,2
  4834. ;     add   eax,ebx
  4835. ;     movzx ebx,byte [alt]
  4836. ;     shl   ebx,3
  4837. ;     add   eax,ebx
  4838.  
  4839.  ;// mike.dld [
  4840.      mov   eax, [kb_state]
  4841.  ;// mike.dld ]
  4842.  
  4843.      mov   [esp+36],eax
  4844.  
  4845.      ret
  4846.  
  4847.    no_get_keyboard_cas:
  4848.  
  4849.         dec     eax
  4850.         jnz     no_add_keyboard_hotkey
  4851.  
  4852.         mov     eax, hotkey_list
  4853. @@:
  4854.         cmp     dword [eax+8], 0
  4855.         jz      .found_free
  4856.         add     eax, 16
  4857.         cmp     eax, hotkey_list+16*256
  4858.         jb      @b
  4859.         mov     dword [esp+36], 1
  4860.         ret
  4861. .found_free:
  4862.         mov     [eax+8], edi
  4863.         mov     [eax+4], ecx
  4864.         movzx   ebx, bl
  4865.         lea     ebx, [hotkey_scancodes+ebx*4]
  4866.         mov     ecx, [ebx]
  4867.         mov     [eax], ecx
  4868.         mov     [ebx], eax
  4869.         mov     [eax+12], ebx
  4870.         jecxz   @f
  4871.         mov     [ecx+12], eax
  4872. @@:
  4873.         and     dword [esp+36], 0
  4874.         ret
  4875.  
  4876. no_add_keyboard_hotkey:
  4877.  
  4878.         dec     eax
  4879.         jnz     no_del_keyboard_hotkey
  4880.  
  4881.         movzx   ebx, bl
  4882.         lea     ebx, [hotkey_scancodes+ebx*4]
  4883.         mov     eax, [ebx]
  4884. .scan:
  4885.         test    eax, eax
  4886.         jz      .notfound
  4887.         cmp     [eax+8], edi
  4888.         jnz     .next
  4889.         cmp     [eax+4], ecx
  4890.         jz      .found
  4891. .next:
  4892.         mov     eax, [eax]
  4893.         jmp     .scan
  4894. .notfound:
  4895.         mov     dword [esp+36], 1
  4896.         ret
  4897. .found:
  4898.         mov     ecx, [eax]
  4899.         jecxz   @f
  4900.         mov     edx, [eax+12]
  4901.         mov     [ecx+12], edx
  4902. @@:
  4903.         mov     ecx, [eax+12]
  4904.         mov     edx, [eax]
  4905.         mov     [ecx], edx
  4906.         xor     edx, edx
  4907.         mov     [eax+4], edx
  4908.         mov     [eax+8], edx
  4909.         mov     [eax+12], edx
  4910.         mov     [eax], edx
  4911.         mov     [esp+36], edx
  4912.         ret
  4913.  
  4914. no_del_keyboard_hotkey:
  4915.      ret
  4916.  
  4917.  
  4918. align 4
  4919.  
  4920. sys_gs:                         ; direct screen access
  4921.  
  4922.      cmp  eax,1                 ; resolution
  4923.      jne  no_gs1
  4924.      mov  eax,[ScreenWidth]
  4925.      shl  eax,16
  4926.      mov  ax,[ScreenHeight]
  4927.      add  eax,0x00010001
  4928.      mov  [esp+36],eax
  4929.      ret
  4930.    no_gs1:
  4931.  
  4932.      cmp   eax,2                ; bits per pixel
  4933.      jne   no_gs2
  4934.      movzx eax,byte [ScreenBPP]
  4935.      mov   [esp+36],eax
  4936.      ret
  4937.    no_gs2:
  4938.  
  4939.      cmp   eax,3                ; bytes per scanline
  4940.      jne   no_gs3
  4941.      mov   eax,[BytesPerScanLine]
  4942.      mov   [esp+36],eax
  4943.      ret
  4944.    no_gs3:
  4945.  
  4946.      mov  [esp+36],dword -1
  4947.      ret
  4948.  
  4949.  
  4950. align 4 ; PCI functions
  4951.  
  4952. sys_pci:
  4953.  
  4954.      call  pci_api
  4955.      mov   [esp+36],eax
  4956.      ret
  4957.  
  4958.  
  4959. align 4  ;  system functions
  4960.  
  4961. syscall_setpixel:                       ; SetPixel
  4962.  
  4963.  
  4964.      mov   edx,[TASK_BASE]
  4965.      add   eax,[edx-twdw+WDATA.box.left]
  4966.      add   ebx,[edx-twdw+WDATA.box.top]
  4967.         mov     edi,[current_slot]
  4968.         add     eax,[edi+APPDATA.wnd_clientbox.left]
  4969.         add     ebx,[edi+APPDATA.wnd_clientbox.top]
  4970.      xor   edi,edi ; no force
  4971. ;     mov   edi,1
  4972.      call  [disable_mouse]
  4973.      jmp   [putpixel]
  4974.  
  4975. align 4
  4976.  
  4977. syscall_writetext:                      ; WriteText
  4978.  
  4979.         mov   edi,[TASK_BASE]
  4980.         mov   ebp,[edi-twdw+WDATA.box.left]
  4981.         push  esi
  4982.         mov   esi,[current_slot]
  4983.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4984.         shl   ebp,16
  4985.         add   ebp,[edi-twdw+WDATA.box.top]
  4986.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4987.         pop   esi
  4988.         add   ecx,[edi+TASKDATA.mem_start]
  4989.         add   eax,ebp
  4990.         xor   edi,edi
  4991.         jmp   dtext
  4992.  
  4993. align 4
  4994.  
  4995. syscall_openramdiskfile:                ; OpenRamdiskFile
  4996.  
  4997.  
  4998.      mov   edi,[TASK_BASE]
  4999.      add   edi,TASKDATA.mem_start
  5000.      add   eax,[edi]
  5001.      add   edx,[edi]
  5002.      mov   esi,12
  5003.      call  fileread
  5004.      mov   [esp+36],ebx
  5005.      ret
  5006.  
  5007. align 4
  5008.  
  5009. syscall_drawrect:                       ; DrawRect
  5010.  
  5011.      mov   edi,ecx
  5012.      and   edi,0x80FFFFFF
  5013.      test  ax,ax
  5014.      je    drectr
  5015.      test  bx,bx
  5016.      je    drectr
  5017.      movzx ecx,ax
  5018.      shr   eax,16
  5019.      movzx edx,bx
  5020.      shr   ebx,16
  5021.         mov     esi,[current_slot]
  5022.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  5023.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  5024.      add   ecx,eax
  5025.      add   edx,ebx
  5026.      jmp   [drawbar]
  5027.     drectr:
  5028.      ret
  5029.  
  5030. align 4
  5031.  
  5032. syscall_getscreensize:                  ; GetScreenSize
  5033.  
  5034.      movzx eax,word[ScreenWidth]
  5035.      shl   eax,16
  5036.      mov   ax,[ScreenHeight]
  5037.      mov   [esp+36],eax
  5038.      ret
  5039.  
  5040. align 4
  5041.  
  5042. syscall_cdaudio:                        ; CD
  5043.  
  5044.         cmp     eax, 4
  5045.         jb      .audio
  5046.         jz      .eject
  5047.         cmp     eax, 5
  5048.         jnz     .ret
  5049. .load:
  5050.         call    .reserve
  5051.         call    LoadMedium
  5052.         call    .free
  5053.         ret
  5054. .eject:
  5055.         call    .reserve
  5056.         call    clear_CD_cache
  5057.         call    allow_medium_removal
  5058.         call    EjectMedium
  5059.         call    .free
  5060.         ret
  5061. .audio:
  5062.      call  sys_cd_audio
  5063.      mov   [esp+36],eax
  5064. .ret:
  5065.      ret
  5066.  
  5067. .reserve:
  5068.         call    reserve_cd
  5069.         mov     eax, ebx
  5070.         shr     eax, 1
  5071.         and     eax, 1
  5072.         inc     eax
  5073.         mov     [ChannelNumber], ax
  5074.         mov     eax, ebx
  5075.         and     eax, 1
  5076.         mov     [DiskNumber], al
  5077.         call    reserve_cd_channel
  5078.         and     ebx, 3
  5079.         inc     ebx
  5080.         mov     [cdpos], ebx
  5081.         add     ebx, ebx
  5082.         mov     cl, 8
  5083.         sub     cl, bl
  5084.         mov     al, [DRIVE_DATA+1]
  5085.         shr     al, cl
  5086.         test    al, 2
  5087.         jz      .err
  5088.         ret
  5089. .free:
  5090.         call    free_cd_channel
  5091.         and     [cd_status], 0
  5092.         ret
  5093. .err:
  5094.         call    .free
  5095.         pop     eax
  5096.         ret
  5097.  
  5098. align 4
  5099.  
  5100. syscall_delramdiskfile:                 ; DelRamdiskFile
  5101.  
  5102.      mov   edi,[TASK_BASE]
  5103.      add   edi,TASKDATA.mem_start
  5104.      add   eax,[edi]
  5105.      call  filedelete
  5106.      mov   [esp+36],eax
  5107.      ret
  5108.  
  5109. align 4
  5110.  
  5111. syscall_writeramdiskfile:               ; WriteRamdiskFile
  5112.  
  5113.      mov   edi,[TASK_BASE]
  5114.      add   edi,TASKDATA.mem_start
  5115.      add   eax,[edi]
  5116.      add   ebx,[edi]
  5117.      call  filesave
  5118.      mov   [esp+36],eax
  5119.      ret
  5120.  
  5121. align 4
  5122.  
  5123. syscall_getpixel:                       ; GetPixel
  5124.      mov   ecx,[ScreenWidth]
  5125.      inc   ecx
  5126.      xor   edx,edx
  5127.      div   ecx
  5128.      mov   ebx,edx
  5129.      xchg  eax,ebx
  5130.      call  dword [GETPIXEL]
  5131.      mov   [esp+36],ecx
  5132.      ret
  5133.  
  5134. align 4
  5135.  
  5136. syscall_readstring:                     ; ReadString
  5137.  
  5138.      mov   edi,[TASK_BASE]
  5139.      add   edi,TASKDATA.mem_start
  5140.      add   eax,[edi]
  5141.      call  read_string
  5142.      mov   [esp+36],eax
  5143.      ret
  5144.  
  5145. align 4
  5146.  
  5147. syscall_drawline:                       ; DrawLine
  5148.  
  5149.      mov   edi,[TASK_BASE]
  5150.      movzx edx,word[edi-twdw+WDATA.box.left]
  5151.      mov   ebp,edx
  5152.         mov     esi,[current_slot]
  5153.         add     ebp,[esi+APPDATA.wnd_clientbox.left]
  5154.         add     dx,word[esi+APPDATA.wnd_clientbox.left]
  5155.      shl   edx,16
  5156.      add   ebp,edx
  5157.      movzx edx,word[edi-twdw+WDATA.box.top]
  5158.      add   eax,ebp
  5159.      mov   ebp,edx
  5160.         add     ebp,[esi+APPDATA.wnd_clientbox.top]
  5161.         add     dx,word[esi+APPDATA.wnd_clientbox.top]
  5162.      shl   edx,16
  5163.      xor   edi,edi
  5164.      add   edx,ebp
  5165.      add   ebx,edx
  5166.      jmp   [draw_line]
  5167.  
  5168. align 4
  5169.  
  5170. syscall_getirqowner:                    ; GetIrqOwner
  5171.      cmp   eax,16
  5172.      jae   .err
  5173.      shl   eax,2
  5174.      add   eax,irq_owner
  5175.      mov   eax,[eax]
  5176.      mov   [esp+36],eax
  5177.      ret
  5178. .err:
  5179.      or    dword [esp+36], -1
  5180.      ret
  5181.  
  5182. align 4
  5183.  
  5184. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5185.  
  5186.      call  r_f_port_area
  5187.      mov   [esp+36],eax
  5188.      ret
  5189.  
  5190. align 4
  5191.  
  5192. syscall_threads:                        ; CreateThreads
  5193.  
  5194.      call  sys_threads
  5195.      mov   [esp+36],eax
  5196.      ret
  5197.  
  5198. align 4
  5199.  
  5200. stack_driver_stat:
  5201.  
  5202.      call  app_stack_handler            ; Stack status
  5203.  
  5204. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5205. ;     call  change_task                 ; between sent packet
  5206.  
  5207.      mov   [esp+36],eax
  5208.      ret
  5209.  
  5210. align 4
  5211.  
  5212. socket:                                 ; Socket interface
  5213.      call  app_socket_handler
  5214.  
  5215. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5216. ;     call  change_task                 ; between sent packet
  5217.  
  5218.      mov   [esp+36],eax
  5219.      mov   [esp+24],ebx
  5220.      ret
  5221.  
  5222. align 4
  5223.  
  5224. user_events:                            ; User event times
  5225.  
  5226.      mov   eax,0x12345678
  5227.      mov   [esp+36],eax
  5228.  
  5229.      ret
  5230.  
  5231. align 4
  5232.  
  5233. read_from_hd:                           ; Read from hd - fn not in use
  5234.  
  5235.      mov   edi,[TASK_BASE]
  5236.      add   edi,TASKDATA.mem_start
  5237.      add   eax,[edi]
  5238.      add   ecx,[edi]
  5239.      add   edx,[edi]
  5240.      call  file_read
  5241.  
  5242.      mov   [esp+36],eax
  5243.      mov   [esp+24],ebx
  5244.  
  5245.      ret
  5246.  
  5247. align 4
  5248. paleholder:
  5249.         ret
  5250.  
  5251. ; --------------- APM ---------------------
  5252. apm_entry    dp    0
  5253. apm_vf        dd    0
  5254. align 4
  5255. sys_apm:
  5256.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5257.     jne    @f
  5258.     or    [esp + 56], byte 1    ; error
  5259.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5260.     ret
  5261.  
  5262. @@:
  5263.     xchg    eax, ecx
  5264.     xchg    ebx, ecx
  5265.  
  5266.     cmp    al, 3
  5267.     ja    @f
  5268.     and    [esp + 56], byte 0xfe    ; emulate func 0..3 as func 0
  5269.     mov    eax, [apm_vf]
  5270.     mov    [esp + 36], eax
  5271.     shr    eax, 16
  5272.     mov    [esp + 32], eax
  5273.     ret
  5274.  
  5275. @@:
  5276.  
  5277.     mov esi, [master_tab+(OS_BASE shr 20)]
  5278.     xchg [master_tab], esi
  5279.     push esi
  5280.     mov edi, cr3
  5281.     mov cr3, edi                 ;flush TLB
  5282.  
  5283.     call    pword [apm_entry]    ; call APM BIOS
  5284.  
  5285.     xchg eax, [esp]
  5286.     mov [master_tab], eax
  5287.     mov eax, cr3
  5288.     mov cr3, eax
  5289.     pop eax
  5290.  
  5291.     mov    [esp + 8 ], edi
  5292.     mov    [esp + 12], esi
  5293.     mov    [esp + 24], ebx
  5294.     mov    [esp + 28], edx
  5295.     mov    [esp + 32], ecx
  5296.     mov    [esp + 36], eax
  5297.     setc    al
  5298.     and    [esp + 56], byte 0xfe
  5299.     or    [esp + 56], al
  5300.  
  5301.  
  5302.     ret
  5303. ; -----------------------------------------
  5304.  
  5305. align 4
  5306.  
  5307. undefined_syscall:                      ; Undefined system call
  5308.  
  5309.      mov   [esp+36],dword -1
  5310.      ret
  5311.  
  5312. align 4
  5313. system_shutdown:          ; shut down the system
  5314.  
  5315.            cmp byte [BOOT_VAR+0x9030], 1
  5316.            jne @F
  5317.            ret
  5318. @@:
  5319.            call stop_all_services
  5320.            push 3                ; stop playing cd
  5321.            pop  eax
  5322.            call sys_cd_audio
  5323.  
  5324. yes_shutdown_param:
  5325.            cli
  5326.  
  5327.            mov  eax, kernel_file ; load kernel.mnt to 0x8000:0
  5328.            push 12
  5329.            pop  esi
  5330.            xor  ebx,ebx
  5331.            or   ecx,-1
  5332.            mov  edx, OS_BASE+0x80000
  5333.            call fileread
  5334.  
  5335.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5336.            mov  edi,OS_BASE+0x40000
  5337.            mov  ecx,1000
  5338.            rep  movsb
  5339.  
  5340.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5341.            mov  edi, OS_BASE
  5342.            mov  ecx,0x10000/4
  5343.            cld
  5344.            rep movsd
  5345.  
  5346.            call restorefatchain
  5347.  
  5348.            mov al, 0xFF
  5349.            out 0x21, al
  5350.            out 0xA1, al
  5351.  
  5352.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5353.            mov  word [OS_BASE+0x467+2],0x1000
  5354.  
  5355.            mov  al,0x0F
  5356.            out  0x70,al
  5357.            mov  al,0x05
  5358.            out  0x71,al
  5359.  
  5360.            mov  al,0xFE
  5361.            out  0x64,al
  5362.            hlt
  5363.  
  5364.  
  5365. include "data32.inc"
  5366.  
  5367. __REV__ = __REV
  5368.  
  5369. uglobals_size = $ - endofcode
  5370. diff16 "end of kernel code",0,$
  5371.  
  5372.