Subversion Repositories Kolibri OS

Rev

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