Subversion Repositories Kolibri OS

Rev

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