Subversion Repositories Kolibri OS

Rev

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