Subversion Repositories Kolibri OS

Rev

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