Subversion Repositories Kolibri OS

Rev

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