Subversion Repositories Kolibri OS

Rev

Rev 2244 | Rev 2267 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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