Subversion Repositories Kolibri OS

Rev

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