Subversion Repositories Kolibri OS

Rev

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