Subversion Repositories Kolibri OS

Rev

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

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