Subversion Repositories Kolibri OS

Rev

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