Subversion Repositories Kolibri OS

Rev

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

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