Subversion Repositories Kolibri OS

Rev

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