Subversion Repositories Kolibri OS

Rev

Rev 1675 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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