Subversion Repositories Kolibri OS

Rev

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