Subversion Repositories Kolibri OS

Rev

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