Subversion Repositories Kolibri OS

Rev

Rev 973 | Rev 1025 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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