Subversion Repositories Kolibri OS

Rev

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