Subversion Repositories Kolibri OS

Rev

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

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