Subversion Repositories Kolibri OS

Rev

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