Subversion Repositories Kolibri OS

Rev

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