Subversion Repositories Kolibri OS

Rev

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