Subversion Repositories Kolibri OS

Rev

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