Subversion Repositories Kolibri OS

Rev

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