Subversion Repositories Kolibri OS

Rev

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

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