Subversion Repositories Kolibri OS

Rev

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