Subversion Repositories Kolibri OS

Rev

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

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