Subversion Repositories Kolibri OS

Rev

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