Subversion Repositories Kolibri OS

Rev

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