Subversion Repositories Kolibri OS

Rev

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

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