Subversion Repositories Kolibri OS

Rev

Rev 1380 | Rev 1392 | 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: 1391 $
  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,ebx
  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.     call  force_redraw_background
  2440.     mov    [REDRAW_BACKGROUND], byte 2
  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],dword 0
  2561.     and   [draw_data+32 + RECT.top],dword 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.     mov   byte [REDRAW_BACKGROUND], 1
  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   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3087.     jz    nobackgr
  3088.     cmp    [background_defined], 0
  3089.     jz    nobackgr
  3090.     cmp   [REDRAW_BACKGROUND], byte 2
  3091.     jnz   no_set_bgr_event
  3092.     xor   edi, edi
  3093.     mov   ecx,  [TASK_COUNT]
  3094. set_bgr_event:
  3095.     add   edi, 256
  3096.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3097.     loop  set_bgr_event
  3098. no_set_bgr_event:
  3099. ;    mov   [draw_data+32 + RECT.left],dword 0
  3100. ;    mov   [draw_data+32 + RECT.top],dword 0
  3101. ;    mov   eax,[Screen_Max_X]
  3102. ;    mov   ebx,[Screen_Max_Y]
  3103. ;    mov   [draw_data+32 + RECT.right],eax
  3104. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3105.     call  drawbackground
  3106.     mov   [REDRAW_BACKGROUND],byte 0
  3107.     mov   [MOUSE_BACKGROUND],byte 0
  3108.  
  3109. nobackgr:
  3110.  
  3111.     ; system shutdown request
  3112.  
  3113.     cmp  [SYS_SHUTDOWN],byte 0
  3114.     je   noshutdown
  3115.  
  3116.     mov  edx,[shutdown_processes]
  3117.  
  3118.     cmp  [SYS_SHUTDOWN],dl
  3119.     jne  no_mark_system_shutdown
  3120.  
  3121.     lea   ecx,[edx-1]
  3122.     mov   edx,OS_BASE+0x3040
  3123.     jecxz @f
  3124. markz:
  3125.     mov   [edx+TASKDATA.state],byte 3
  3126.     add   edx,0x20
  3127.     loop  markz
  3128. @@:
  3129.  
  3130.   no_mark_system_shutdown:
  3131.  
  3132.     call [_display.disable_mouse]
  3133.  
  3134.     dec  byte [SYS_SHUTDOWN]
  3135.     je   system_shutdown
  3136.  
  3137. noshutdown:
  3138.  
  3139.  
  3140.     mov   eax,[TASK_COUNT]                  ; termination
  3141.     mov   ebx,TASK_DATA+TASKDATA.state
  3142.     mov   esi,1
  3143.  
  3144. newct:
  3145.     mov   cl,[ebx]
  3146.     cmp   cl,byte 3
  3147.     jz    terminate
  3148.     cmp   cl,byte 4
  3149.     jz    terminate
  3150.  
  3151.     add   ebx,0x20
  3152.     inc   esi
  3153.     dec   eax
  3154.     jnz   newct
  3155.     ret
  3156.  
  3157. ; redraw screen
  3158.  
  3159. redrawscreen:
  3160.  
  3161. ; eax , if process window_data base is eax, do not set flag/limits
  3162.  
  3163.          pushad
  3164.          push  eax
  3165.  
  3166. ;;;         mov   ebx,2
  3167. ;;;         call  delay_hs
  3168.  
  3169.          ;mov   ecx,0               ; redraw flags for apps
  3170.          xor   ecx,ecx
  3171.        newdw2:
  3172.  
  3173.          inc   ecx
  3174.          push  ecx
  3175.  
  3176.          mov   eax,ecx
  3177.          shl   eax,5
  3178.          add   eax,window_data
  3179.  
  3180.          cmp   eax,[esp+4]
  3181.          je    not_this_task
  3182.                                    ; check if window in redraw area
  3183.          mov   edi,eax
  3184.  
  3185.          cmp   ecx,1               ; limit for background
  3186.          jz    bgli
  3187.  
  3188.          mov   eax, [edi + WDATA.box.left]
  3189.          mov   ebx, [edi + WDATA.box.top]
  3190.          mov   ecx, [edi + WDATA.box.width]
  3191.          mov   edx, [edi + WDATA.box.height]
  3192.          add   ecx,eax
  3193.          add   edx,ebx
  3194.  
  3195.          mov   ecx,[draw_limits.bottom]   ; ecx = area y end     ebx = window y start
  3196.          cmp   ecx,ebx
  3197.          jb    ricino
  3198.  
  3199.          mov   ecx,[draw_limits.right]   ; ecx = area x end     eax = window x start
  3200.          cmp   ecx,eax
  3201.          jb    ricino
  3202.  
  3203.          mov   eax, [edi + WDATA.box.left]
  3204.          mov   ebx, [edi + WDATA.box.top]
  3205.          mov   ecx, [edi + WDATA.box.width]
  3206.          mov   edx, [edi + WDATA.box.height]
  3207.          add   ecx, eax
  3208.          add   edx, ebx
  3209.  
  3210.          mov   eax,[draw_limits.top]    ; eax = area y start     edx = window y end
  3211.          cmp   edx,eax
  3212.          jb    ricino
  3213.  
  3214.          mov   eax,[draw_limits.left]    ; eax = area x start     ecx = window x end
  3215.          cmp   ecx,eax
  3216.          jb    ricino
  3217.  
  3218.         bgli:
  3219.  
  3220.          cmp   ecx,1
  3221.          jnz   .az
  3222.          mov   al,[REDRAW_BACKGROUND]
  3223.          cmp   al,2
  3224.          jz    newdw8
  3225.          test  al,al
  3226.          jz    .az
  3227.          lea   eax,[edi+draw_data-window_data]
  3228.          mov   ebx,[draw_limits.left]
  3229.          cmp   ebx,[eax+RECT.left]
  3230.          jae   @f
  3231.          mov   [eax+RECT.left],ebx
  3232.         @@:
  3233.          mov   ebx,[draw_limits.top]
  3234.          cmp   ebx,[eax+RECT.top]
  3235.          jae   @f
  3236.          mov   [eax+RECT.top],ebx
  3237.         @@:
  3238.          mov   ebx,[draw_limits.right]
  3239.          cmp   ebx,[eax+RECT.right]
  3240.          jbe   @f
  3241.          mov   [eax+RECT.right],ebx
  3242.         @@:
  3243.          mov   ebx,[draw_limits.bottom]
  3244.          cmp   ebx,[eax+RECT.bottom]
  3245.          jbe   @f
  3246.          mov   [eax+RECT.bottom],ebx
  3247.         @@:
  3248.          jmp   newdw8
  3249.         .az:
  3250.  
  3251.          mov   eax,edi
  3252.          add   eax,draw_data-window_data
  3253.  
  3254.          mov   ebx,[draw_limits.left]          ; set limits
  3255.          mov   [eax + RECT.left], ebx
  3256.          mov   ebx,[draw_limits.top]
  3257.          mov   [eax + RECT.top], ebx
  3258.          mov   ebx,[draw_limits.right]
  3259.          mov   [eax + RECT.right], ebx
  3260.          mov   ebx,[draw_limits.bottom]
  3261.          mov   [eax + RECT.bottom], ebx
  3262.  
  3263.          sub   eax,draw_data-window_data
  3264.  
  3265.          cmp   dword [esp],1
  3266.          jne   nobgrd
  3267.          mov   byte [REDRAW_BACKGROUND], 1
  3268.  
  3269.        newdw8:
  3270.        nobgrd:
  3271.  
  3272.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3273.  
  3274.        ricino:
  3275.  
  3276.        not_this_task:
  3277.  
  3278.          pop   ecx
  3279.  
  3280.          cmp   ecx,[TASK_COUNT]
  3281.          jle   newdw2
  3282.  
  3283.          pop  eax
  3284.          popad
  3285.  
  3286.          ret
  3287.  
  3288. calculatebackground:   ; background
  3289.  
  3290.         mov   edi, [_WinMapAddress]                ; set os to use all pixels
  3291.         mov   eax,0x01010101
  3292.         mov   ecx, [_WinMapSize]
  3293.         shr   ecx, 2
  3294.         rep   stosd
  3295.  
  3296.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3297.  
  3298.         ret
  3299.  
  3300. uglobal
  3301.   imax    dd 0x0
  3302. endg
  3303.  
  3304.  
  3305.  
  3306. delay_ms:     ; delay in 1/1000 sec
  3307.  
  3308.  
  3309.         push  eax
  3310.         push  ecx
  3311.  
  3312.         mov   ecx,esi
  3313.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3314.         imul  ecx, 33941
  3315.         shr   ecx, 9
  3316.         ; </CPU clock fix>
  3317.  
  3318.         in    al,0x61
  3319.         and   al,0x10
  3320.         mov   ah,al
  3321.         cld
  3322.  
  3323.  cnt1:  in    al,0x61
  3324.         and   al,0x10
  3325.         cmp   al,ah
  3326.         jz    cnt1
  3327.  
  3328.         mov   ah,al
  3329.         loop  cnt1
  3330.  
  3331.         pop   ecx
  3332.         pop   eax
  3333.  
  3334.         ret
  3335.  
  3336.  
  3337. set_app_param:
  3338.         mov     edi, [TASK_BASE]
  3339.         mov     eax, [edi + TASKDATA.event_mask]
  3340.         mov     [edi + TASKDATA.event_mask], ebx
  3341.         mov     [esp+32], eax
  3342.         ret
  3343.  
  3344.  
  3345.  
  3346. delay_hs:     ; delay in 1/100 secs
  3347. ; ebx = delay time
  3348.         push  ecx
  3349.         push  edx
  3350.  
  3351.         mov   edx,[timer_ticks]
  3352.  
  3353.       newtic:
  3354.         mov   ecx,[timer_ticks]
  3355.         sub   ecx,edx
  3356.         cmp   ecx,ebx
  3357.         jae   zerodelay
  3358.  
  3359.         call  change_task
  3360.  
  3361.         jmp   newtic
  3362.  
  3363.       zerodelay:
  3364.         pop   edx
  3365.         pop   ecx
  3366.  
  3367.         ret
  3368.  
  3369. align 16        ;very often call this subrutine
  3370. memmove:       ; memory move in bytes
  3371.  
  3372. ; eax = from
  3373. ; ebx = to
  3374. ; ecx = no of bytes
  3375.     test ecx, ecx
  3376.     jle  .ret
  3377.  
  3378.     push esi edi ecx
  3379.  
  3380.     mov  edi, ebx
  3381.     mov  esi, eax
  3382.  
  3383.     test ecx, not 11b
  3384.     jz   @f
  3385.  
  3386.     push ecx
  3387.     shr  ecx, 2
  3388.     rep  movsd
  3389.     pop  ecx
  3390.     and  ecx, 11b
  3391.     jz   .finish
  3392.   @@:
  3393.     rep  movsb
  3394.  
  3395.   .finish:
  3396.     pop  ecx edi esi
  3397.   .ret:
  3398.     ret
  3399.  
  3400.  
  3401. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3402. ;align 4
  3403. ;
  3404. ;read_floppy_file:
  3405. ;
  3406. ;; as input
  3407. ;;
  3408. ;; eax pointer to file
  3409. ;; ebx file lenght
  3410. ;; ecx start 512 byte block number
  3411. ;; edx number of blocks to read
  3412. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3413. ;;
  3414. ;;
  3415. ;; on return
  3416. ;;
  3417. ;; eax = 0 command succesful
  3418. ;;       1 no fd base and/or partition defined
  3419. ;;       2 yet unsupported FS
  3420. ;;       3 unknown FS
  3421. ;;       4 partition not defined at hd
  3422. ;;       5 file not found
  3423. ;; ebx = size of file
  3424. ;
  3425. ;     mov   edi,[TASK_BASE]
  3426. ;     add   edi,0x10
  3427. ;     add   esi,[edi]
  3428. ;     add   eax,[edi]
  3429. ;
  3430. ;     pushad
  3431. ;     mov  edi,esi
  3432. ;     add  edi,1024
  3433. ;     mov  esi,0x100000+19*512
  3434. ;     sub  ecx,1
  3435. ;     shl  ecx,9
  3436. ;     add  esi,ecx
  3437. ;     shl  edx,9
  3438. ;     mov  ecx,edx
  3439. ;     cld
  3440. ;     rep  movsb
  3441. ;     popad
  3442. ;
  3443. ;     mov   [esp+36],eax
  3444. ;     mov   [esp+24],ebx
  3445. ;     ret
  3446.  
  3447.  
  3448.  
  3449. align 4
  3450.  
  3451. sys_programirq:
  3452.  
  3453.     mov   eax, [TASK_BASE]
  3454.     add   ebx, [eax + TASKDATA.mem_start]
  3455.  
  3456.     cmp   ecx, 16
  3457.     jae   .not_owner
  3458.     mov   edi, [eax + TASKDATA.pid]
  3459.     cmp   edi, [irq_owner + 4 * ecx]
  3460.     je    .spril1
  3461. .not_owner:
  3462.     xor   ecx, ecx
  3463.     inc   ecx
  3464.     jmp   .end
  3465.   .spril1:
  3466.  
  3467.     shl   ecx, 6
  3468.     mov   esi, ebx
  3469.     lea   edi, [irq00read + ecx]
  3470.     push  16
  3471.     pop   ecx
  3472.  
  3473.     cld
  3474.     rep   movsd
  3475.   .end:
  3476.     mov   [esp+32], ecx
  3477.     ret
  3478.  
  3479.  
  3480. align 4
  3481.  
  3482. get_irq_data:
  3483.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3484.      xor   bh, bh
  3485.      cmp   ebx, 16
  3486.      jae   .not_owner
  3487.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3488.  
  3489.      mov   eax,[TASK_BASE]
  3490.  
  3491.      cmp   edx,[eax+TASKDATA.pid]
  3492.      je    gidril1
  3493. .not_owner:
  3494.      xor   edx, edx
  3495.      dec   edx
  3496.      jmp   gid1
  3497.  
  3498.   gidril1:
  3499.  
  3500.      shl   ebx, 12
  3501.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3502.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3503.      dec   esi
  3504.      jz    gid1
  3505.      test  edx, edx                      ; check if buffer is empty
  3506.      jz    gid1
  3507.  
  3508.      mov   ebx, [eax + 0x4]
  3509.      mov   edi, ecx
  3510.  
  3511.      mov   ecx, 4000                     ; buffer size, used frequently
  3512.  
  3513.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3514.      jb    @f
  3515.  
  3516.      xor   ebx, ebx
  3517.  
  3518.    @@:
  3519.  
  3520.      lea   esi, [ebx + edx]              ; calculate data size and offset
  3521.      cld
  3522.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  3523.      jbe   @f
  3524.  
  3525.      sub   ecx, ebx
  3526.      sub   edx, ecx
  3527.  
  3528.      lea   esi, [eax + ebx + 0x10]
  3529.      rep   movsb
  3530.  
  3531.      xor   ebx, ebx
  3532.    @@:
  3533.      lea   esi, [eax + ebx + 0x10]
  3534.      mov   ecx, edx
  3535.      add   ebx, edx
  3536.  
  3537.      rep   movsb
  3538.      mov   edx, [eax]
  3539.      mov   [eax], ecx                    ; set data size to zero
  3540.      mov   [eax + 0x4], ebx              ; set data offset
  3541.  
  3542.    gid1:
  3543.      mov   [esp+32], edx                 ; eax
  3544.      ret
  3545.  
  3546.  
  3547. set_io_access_rights:
  3548.       push edi eax
  3549.       mov edi, tss._io_map_0
  3550. ;     mov   ecx,eax
  3551. ;     and   ecx,7    ; offset in byte
  3552. ;     shr   eax,3    ; number of byte
  3553. ;     add   edi,eax
  3554. ;     mov   ebx,1
  3555. ;     shl   ebx,cl
  3556.      test  ebp,ebp
  3557. ;     cmp   ebp,0                ; enable access - ebp = 0
  3558.      jnz   siar1
  3559. ;     not   ebx
  3560. ;     and   [edi],byte bl
  3561.      btr [edi], eax
  3562.      pop eax edi
  3563.      ret
  3564. siar1:
  3565.      bts [edi], eax
  3566.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3567.      pop eax edi
  3568.      ret
  3569. ;reserve/free group of ports
  3570. ;  * eax = 46 - number function
  3571. ;  * ebx = 0 - reserve, 1 - free
  3572. ;  * ecx = number start arrea of ports
  3573. ;  * edx = number end arrea of ports (include last number of port)
  3574. ;Return value:
  3575. ;  * eax = 0 - succesful
  3576. ;  * eax = 1 - error
  3577. ;  * The system has reserve this ports:
  3578. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3579. ;destroys eax,ebx, ebp
  3580. r_f_port_area:
  3581.  
  3582.      test  ebx, ebx
  3583.      jnz   free_port_area
  3584. ;     je    r_port_area
  3585. ;     jmp   free_port_area
  3586.  
  3587. ;   r_port_area:
  3588.  
  3589. ;     pushad
  3590.  
  3591.      cmp   ecx,edx            ; beginning > end ?
  3592.      ja    rpal1
  3593.      cmp   edx,65536
  3594.      jae   rpal1
  3595.      mov   eax,[RESERVED_PORTS]
  3596.      test  eax,eax            ; no reserved areas ?
  3597.      je    rpal2
  3598.      cmp   eax,255            ; max reserved
  3599.      jae   rpal1
  3600.  rpal3:
  3601.      mov   ebx,eax
  3602.      shl   ebx,4
  3603.      add   ebx,RESERVED_PORTS
  3604.      cmp   ecx,[ebx+8]
  3605.      ja    rpal4
  3606.      cmp   edx,[ebx+4]
  3607.      jae   rpal1
  3608. ;     jb    rpal4
  3609. ;     jmp   rpal1
  3610.  rpal4:
  3611.      dec   eax
  3612.      jnz   rpal3
  3613.      jmp   rpal2
  3614.    rpal1:
  3615. ;     popad
  3616. ;     mov   eax,1
  3617.      xor    eax,eax
  3618.      inc    eax
  3619.      ret
  3620.    rpal2:
  3621. ;     popad
  3622.      ; enable port access at port IO map
  3623.         cli
  3624.         pushad                        ; start enable io map
  3625.  
  3626.         cmp   edx,65536 ;16384
  3627.         jae   no_unmask_io ; jge
  3628.         mov   eax,ecx
  3629. ;       push    ebp
  3630.         xor     ebp,ebp                ; enable - eax = port
  3631. new_port_access:
  3632. ;     pushad
  3633.         call    set_io_access_rights
  3634. ;     popad
  3635.      inc   eax
  3636.      cmp   eax,edx
  3637.      jbe   new_port_access
  3638. ;       pop     ebp
  3639. no_unmask_io:
  3640.         popad                         ; end enable io map
  3641.      sti
  3642.  
  3643.      mov   eax,[RESERVED_PORTS]
  3644.      add   eax,1
  3645.      mov   [RESERVED_PORTS],eax
  3646.      shl   eax,4
  3647.      add   eax,RESERVED_PORTS
  3648.      mov   ebx,[TASK_BASE]
  3649.      mov   ebx,[ebx+TASKDATA.pid]
  3650.      mov   [eax],ebx
  3651.      mov   [eax+4],ecx
  3652.      mov   [eax+8],edx
  3653.  
  3654.      xor   eax, eax
  3655.      ret
  3656.  
  3657. free_port_area:
  3658.  
  3659. ;     pushad
  3660.      mov   eax,[RESERVED_PORTS]     ; no reserved areas ?
  3661.      test  eax,eax
  3662.      jz    frpal2
  3663.      mov   ebx,[TASK_BASE]
  3664.      mov   ebx,[ebx+TASKDATA.pid]
  3665.    frpal3:
  3666.      mov   edi,eax
  3667.      shl   edi,4
  3668.      add   edi,RESERVED_PORTS
  3669.      cmp   ebx,[edi]
  3670.      jne   frpal4
  3671.      cmp   ecx,[edi+4]
  3672.      jne   frpal4
  3673.      cmp   edx,[edi+8]
  3674.      jne   frpal4
  3675.      jmp   frpal1
  3676.    frpal4:
  3677.      dec   eax
  3678.      jnz   frpal3
  3679.    frpal2:
  3680. ;     popad
  3681.      inc   eax
  3682.      ret
  3683.    frpal1:
  3684.         push    ecx
  3685.         mov   ecx,256
  3686.         sub   ecx,eax
  3687.         shl   ecx,4
  3688.         mov   esi,edi
  3689.         add   esi,16
  3690.         cld
  3691.         rep   movsb
  3692.  
  3693.         dec   dword [RESERVED_PORTS]
  3694. ;popad
  3695. ;disable port access at port IO map
  3696.  
  3697. ;     pushad                        ; start disable io map
  3698.      pop        eax     ;start port
  3699.      cmp   edx,65536 ;16384
  3700.      jge   no_mask_io
  3701.  
  3702. ;     mov   eax,ecx
  3703.         xor     ebp,ebp
  3704.         inc     ebp
  3705. new_port_access_disable:
  3706. ;     pushad
  3707. ;     mov   ebp,1                  ; disable - eax = port
  3708.      call  set_io_access_rights
  3709. ;     popad
  3710.      inc   eax
  3711.      cmp   eax,edx
  3712.      jbe   new_port_access_disable
  3713. no_mask_io:
  3714. ;     popad                         ; end disable io map
  3715.      xor   eax, eax
  3716.      ret
  3717.  
  3718.  
  3719. reserve_free_irq:
  3720.  
  3721.      xor   esi, esi
  3722.      inc   esi
  3723.      cmp   ecx, 16
  3724.      jae   ril1
  3725.  
  3726.      push  ecx
  3727.      lea   ecx, [irq_owner + 4 * ecx]
  3728.      mov   edx, [ecx]
  3729.      mov   eax, [TASK_BASE]
  3730.      mov   edi, [eax + TASKDATA.pid]
  3731.      pop   eax
  3732.      dec   ebx
  3733.      jnz   reserve_irq
  3734.  
  3735.      cmp   edx, edi
  3736.      jne   ril1
  3737.      dec   esi
  3738.      mov   [ecx], esi
  3739.  
  3740.      jmp   ril1
  3741.  
  3742.   reserve_irq:
  3743.  
  3744.      cmp   dword [ecx], 0
  3745.      jne   ril1
  3746.  
  3747.      mov   ebx, [f_irqs + 4 * eax]
  3748.  
  3749.      stdcall attach_int_handler, eax, ebx, dword 0
  3750.  
  3751.      mov   [ecx], edi
  3752.  
  3753.      dec   esi
  3754.    ril1:
  3755.      mov   [esp+32], esi ; return in eax
  3756.      ret
  3757.  
  3758. iglobal
  3759. f_irqs:
  3760.      dd 0x0
  3761.      dd 0x0
  3762.      dd p_irq2
  3763.      dd p_irq3
  3764.      dd p_irq4
  3765.      dd p_irq5
  3766.      dd p_irq6
  3767.      dd p_irq7
  3768.      dd p_irq8
  3769.      dd p_irq9
  3770.      dd p_irq10
  3771.      dd p_irq11
  3772.      dd 0x0
  3773.      dd 0x0
  3774.      dd p_irq14
  3775.      dd p_irq15
  3776.  
  3777. endg
  3778.  
  3779. drawbackground:
  3780.        inc   [mouse_pause]
  3781.        cmp   [SCR_MODE],word 0x12
  3782.        je   dbrv20
  3783.      dbrv12:
  3784.        cmp  [SCR_MODE],word 0100000000000000b
  3785.        jge  dbrv20
  3786.        cmp  [SCR_MODE],word 0x13
  3787.        je   dbrv20
  3788.        call  vesa12_drawbackground
  3789.        dec   [mouse_pause]
  3790.        call   [draw_pointer]
  3791.        ret
  3792.      dbrv20:
  3793.        cmp   [BgrDrawMode],dword 1
  3794.        jne   bgrstr
  3795.        call  vesa20_drawbackground_tiled
  3796.        dec   [mouse_pause]
  3797.        call   [draw_pointer]
  3798.        ret
  3799.      bgrstr:
  3800.        call  vesa20_drawbackground_stretch
  3801.        dec   [mouse_pause]
  3802.        call   [draw_pointer]
  3803.        ret
  3804.  
  3805. align 4
  3806.  
  3807. syscall_putimage:                       ; PutImage
  3808. sys_putimage:
  3809.      test  ecx,0x80008000
  3810.      jnz   .exit
  3811.      test  ecx,0x0000FFFF
  3812.      jz    .exit
  3813.      test  ecx,0xFFFF0000
  3814.      jnz   @f
  3815.   .exit:
  3816.      ret
  3817.  @@:
  3818.         mov     edi,[current_slot]
  3819.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  3820.         rol     edx,16
  3821.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  3822.         rol     edx,16
  3823.   .forced:
  3824.         push    ebp esi 0
  3825.         mov     ebp, putimage_get24bpp
  3826.         mov     esi, putimage_init24bpp
  3827. sys_putimage_bpp:
  3828. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  3829. ;        mov     eax, vga_putimage
  3830.         cmp     [SCR_MODE], word 0x12
  3831.         jz      @f   ;.doit
  3832.         mov     eax, vesa12_putimage
  3833.         cmp     [SCR_MODE], word 0100000000000000b
  3834.         jae     @f
  3835.         cmp     [SCR_MODE], word 0x13
  3836.         jnz     .doit
  3837. @@:
  3838.         mov     eax, vesa20_putimage
  3839. .doit:
  3840.         inc     [mouse_pause]
  3841.         call    eax
  3842.         dec     [mouse_pause]
  3843.         pop     ebp esi ebp
  3844.         jmp     [draw_pointer]
  3845.  
  3846. syscall_putimage_palette:
  3847.         mov     edi, esi
  3848.         mov     esi, edx
  3849.         mov     edx, ecx
  3850.         mov     ecx, ebx
  3851.         mov     ebx, eax
  3852. sys_putimage_palette:
  3853. ; ebx = pointer to image
  3854. ; ecx = [xsize]*65536 + [ysize]
  3855. ; edx = [xstart]*65536 + [ystart]
  3856. ; esi = number of bits per pixel, must be 8, 24 or 32
  3857. ; edi = pointer to palette
  3858. ; ebp = row delta
  3859.         mov     eax, [CURRENT_TASK]
  3860.         shl     eax, 8
  3861.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3862.         rol     edx, 16
  3863.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3864.         rol     edx, 16
  3865. .forced:
  3866.         cmp     esi, 1
  3867.         jnz     @f
  3868.         push    edi
  3869.         mov     eax, [edi+4]
  3870.         sub     eax, [edi]
  3871.         push    eax
  3872.         push    dword [edi]
  3873.         push    0ffffff80h
  3874.         mov     edi, esp
  3875.         call    put_mono_image
  3876.         add     esp, 12
  3877.         pop     edi
  3878.         ret
  3879. @@:
  3880.         cmp     esi, 2
  3881.         jnz     @f
  3882.         push    edi
  3883.         push    0ffffff80h
  3884.         mov     edi, esp
  3885.         call    put_2bit_image
  3886.         pop     eax
  3887.         pop     edi
  3888.         ret
  3889. @@:
  3890.         cmp     esi, 4
  3891.         jnz     @f
  3892.         push    edi
  3893.         push    0ffffff80h
  3894.         mov     edi, esp
  3895.         call    put_4bit_image
  3896.         pop     eax
  3897.         pop     edi
  3898.         ret
  3899. @@:
  3900.         push    ebp esi ebp
  3901.         cmp     esi, 8
  3902.         jnz     @f
  3903.         mov     ebp, putimage_get8bpp
  3904.         mov     esi, putimage_init8bpp
  3905.         jmp     sys_putimage_bpp
  3906. @@:
  3907.         cmp     esi, 15
  3908.         jnz     @f
  3909.         mov     ebp, putimage_get15bpp
  3910.         mov     esi, putimage_init15bpp
  3911.         jmp     sys_putimage_bpp
  3912. @@:
  3913.         cmp     esi, 16
  3914.         jnz     @f
  3915.         mov     ebp, putimage_get16bpp
  3916.         mov     esi, putimage_init16bpp
  3917.         jmp     sys_putimage_bpp
  3918. @@:
  3919.         cmp     esi, 24
  3920.         jnz     @f
  3921.         mov     ebp, putimage_get24bpp
  3922.         mov     esi, putimage_init24bpp
  3923.         jmp     sys_putimage_bpp
  3924. @@:
  3925.         cmp     esi, 32
  3926.         jnz     @f
  3927.         mov     ebp, putimage_get32bpp
  3928.         mov     esi, putimage_init32bpp
  3929.         jmp     sys_putimage_bpp
  3930. @@:
  3931.         pop     ebp esi ebp
  3932.         ret
  3933.  
  3934. put_mono_image:
  3935.         push    ebp esi ebp
  3936.         mov     ebp, putimage_get1bpp
  3937.         mov     esi, putimage_init1bpp
  3938.         jmp     sys_putimage_bpp
  3939. put_2bit_image:
  3940.         push    ebp esi ebp
  3941.         mov     ebp, putimage_get2bpp
  3942.         mov     esi, putimage_init2bpp
  3943.         jmp     sys_putimage_bpp
  3944. put_4bit_image:
  3945.         push    ebp esi ebp
  3946.         mov     ebp, putimage_get4bpp
  3947.         mov     esi, putimage_init4bpp
  3948.         jmp     sys_putimage_bpp
  3949.  
  3950. putimage_init24bpp:
  3951.         lea     eax, [eax*3]
  3952. putimage_init8bpp:
  3953.         ret
  3954.  
  3955. align 16
  3956. putimage_get24bpp:
  3957.         movzx   eax, byte [esi+2]
  3958.         shl     eax, 16
  3959.         mov     ax, [esi]
  3960.         add     esi, 3
  3961.         ret     4
  3962. align 16
  3963. putimage_get8bpp:
  3964.         movzx   eax, byte [esi]
  3965.         push    edx
  3966.         mov     edx, [esp+8]
  3967.         mov     eax, [edx+eax*4]
  3968.         pop     edx
  3969.         inc     esi
  3970.         ret     4
  3971.  
  3972. putimage_init1bpp:
  3973.         add     eax, ecx
  3974.         push    ecx
  3975.         add     eax, 7
  3976.         add     ecx, 7
  3977.         shr     eax, 3
  3978.         shr     ecx, 3
  3979.         sub     eax, ecx
  3980.         pop     ecx
  3981.         ret
  3982. align 16
  3983. putimage_get1bpp:
  3984.         push    edx
  3985.         mov     edx, [esp+8]
  3986.         mov     al, [edx]
  3987.         add     al, al
  3988.         jnz     @f
  3989.         lodsb
  3990.         adc     al, al
  3991. @@:
  3992.         mov     [edx], al
  3993.         sbb     eax, eax
  3994.         and     eax, [edx+8]
  3995.         add     eax, [edx+4]
  3996.         pop     edx
  3997.         ret     4
  3998.  
  3999. putimage_init2bpp:
  4000.         add     eax, ecx
  4001.         push    ecx
  4002.         add     ecx, 3
  4003.         add     eax, 3
  4004.         shr     ecx, 2
  4005.         shr     eax, 2
  4006.         sub     eax, ecx
  4007.         pop     ecx
  4008.         ret
  4009. align 16
  4010. putimage_get2bpp:
  4011.         push    edx
  4012.         mov     edx, [esp+8]
  4013.         mov     al, [edx]
  4014.         mov     ah, al
  4015.         shr     al, 6
  4016.         shl     ah, 2
  4017.         jnz     .nonewbyte
  4018.         lodsb
  4019.         mov     ah, al
  4020.         shr     al, 6
  4021.         shl     ah, 2
  4022.         add     ah, 1
  4023. .nonewbyte:
  4024.         mov     [edx], ah
  4025.         mov     edx, [edx+4]
  4026.         movzx   eax, al
  4027.         mov     eax, [edx+eax*4]
  4028.         pop     edx
  4029.         ret     4
  4030.  
  4031. putimage_init4bpp:
  4032.         add     eax, ecx
  4033.         push    ecx
  4034.         add     ecx, 1
  4035.         add     eax, 1
  4036.         shr     ecx, 1
  4037.         shr     eax, 1
  4038.         sub     eax, ecx
  4039.         pop     ecx
  4040.         ret
  4041. align 16
  4042. putimage_get4bpp:
  4043.         push    edx
  4044.         mov     edx, [esp+8]
  4045.         add     byte [edx], 80h
  4046.         jc      @f
  4047.         movzx   eax, byte [edx+1]
  4048.         mov     edx, [edx+4]
  4049.         and     eax, 0x0F
  4050.         mov     eax, [edx+eax*4]
  4051.         pop     edx
  4052.         ret     4
  4053. @@:
  4054.         movzx   eax, byte [esi]
  4055.         add     esi, 1
  4056.         mov     [edx+1], al
  4057.         shr     eax, 4
  4058.         mov     edx, [edx+4]
  4059.         mov     eax, [edx+eax*4]
  4060.         pop     edx
  4061.         ret     4
  4062.  
  4063. putimage_init32bpp:
  4064.         shl     eax, 2
  4065.         ret
  4066. align 16
  4067. putimage_get32bpp:
  4068.         lodsd
  4069.         ret     4
  4070.  
  4071. putimage_init15bpp:
  4072. putimage_init16bpp:
  4073.         add     eax, eax
  4074.         ret
  4075. align 16
  4076. putimage_get15bpp:
  4077. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4078.         push    ecx edx
  4079.         movzx   eax, word [esi]
  4080.         add     esi, 2
  4081.         mov     ecx, eax
  4082.         mov     edx, eax
  4083.         and     eax, 0x1F
  4084.         and     ecx, 0x1F shl 5
  4085.         and     edx, 0x1F shl 10
  4086.         shl     eax, 3
  4087.         shl     ecx, 6
  4088.         shl     edx, 9
  4089.         or      eax, ecx
  4090.         or      eax, edx
  4091.         pop     edx ecx
  4092.         ret     4
  4093.  
  4094. align 16
  4095. putimage_get16bpp:
  4096. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4097.         push    ecx edx
  4098.         movzx   eax, word [esi]
  4099.         add     esi, 2
  4100.         mov     ecx, eax
  4101.         mov     edx, eax
  4102.         and     eax, 0x1F
  4103.         and     ecx, 0x3F shl 5
  4104.         and     edx, 0x1F shl 11
  4105.         shl     eax, 3
  4106.         shl     ecx, 5
  4107.         shl     edx, 8
  4108.         or      eax, ecx
  4109.         or      eax, edx
  4110.         pop     edx ecx
  4111.         ret     4
  4112.  
  4113. ; eax x beginning
  4114. ; ebx y beginning
  4115. ; ecx x end
  4116.         ; edx y end
  4117. ; edi color
  4118.  
  4119. __sys_drawbar:
  4120.         mov     esi,[current_slot]
  4121.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4122.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4123.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4124.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4125.   .forced:
  4126.     inc   [mouse_pause]
  4127. ;        call    [disable_mouse]
  4128.     cmp   [SCR_MODE],word 0x12
  4129.     je   dbv20
  4130.    sdbv20:
  4131.     cmp  [SCR_MODE],word 0100000000000000b
  4132.     jge  dbv20
  4133.     cmp  [SCR_MODE],word 0x13
  4134.     je   dbv20
  4135.     call vesa12_drawbar
  4136.     dec   [mouse_pause]
  4137.     call   [draw_pointer]
  4138.     ret
  4139.   dbv20:
  4140.     call vesa20_drawbar
  4141.     dec   [mouse_pause]
  4142.     call   [draw_pointer]
  4143.     ret
  4144.  
  4145.  
  4146.  
  4147. kb_read:
  4148.  
  4149.         push    ecx edx
  4150.  
  4151.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4152.       kr_loop:
  4153.         in      al,0x64
  4154.         test    al,1
  4155.         jnz     kr_ready
  4156.         loop    kr_loop
  4157.         mov     ah,1
  4158.         jmp     kr_exit
  4159.       kr_ready:
  4160.         push    ecx
  4161.         mov     ecx,32
  4162.       kr_delay:
  4163.         loop    kr_delay
  4164.         pop     ecx
  4165.         in      al,0x60
  4166.         xor     ah,ah
  4167.       kr_exit:
  4168.  
  4169.         pop     edx ecx
  4170.  
  4171.         ret
  4172.  
  4173.  
  4174. kb_write:
  4175.  
  4176.         push    ecx edx
  4177.  
  4178.         mov     dl,al
  4179. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4180. ;      kw_loop1:
  4181. ;        in      al,0x64
  4182. ;        test    al,0x20
  4183. ;        jz      kw_ok1
  4184. ;        loop    kw_loop1
  4185. ;        mov     ah,1
  4186. ;        jmp     kw_exit
  4187. ;      kw_ok1:
  4188.         in      al,0x60
  4189.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4190.       kw_loop:
  4191.         in      al,0x64
  4192.         test    al,2
  4193.         jz      kw_ok
  4194.         loop    kw_loop
  4195.         mov     ah,1
  4196.         jmp     kw_exit
  4197.       kw_ok:
  4198.         mov     al,dl
  4199.         out     0x60,al
  4200.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4201.       kw_loop3:
  4202.         in      al,0x64
  4203.         test    al,2
  4204.         jz      kw_ok3
  4205.         loop    kw_loop3
  4206.         mov     ah,1
  4207.         jmp     kw_exit
  4208.       kw_ok3:
  4209.         mov     ah,8
  4210.       kw_loop4:
  4211.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4212.       kw_loop5:
  4213.         in      al,0x64
  4214.         test    al,1
  4215.         jnz     kw_ok4
  4216.         loop    kw_loop5
  4217.         dec     ah
  4218.         jnz     kw_loop4
  4219.       kw_ok4:
  4220.         xor     ah,ah
  4221.       kw_exit:
  4222.  
  4223.         pop     edx ecx
  4224.  
  4225.         ret
  4226.  
  4227.  
  4228. kb_cmd:
  4229.  
  4230.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4231.       c_wait:
  4232.         in      al,0x64
  4233.         test    al,2
  4234.         jz      c_send
  4235.         loop    c_wait
  4236.         jmp     c_error
  4237.       c_send:
  4238.         mov     al,bl
  4239.         out     0x64,al
  4240.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4241.       c_accept:
  4242.         in      al,0x64
  4243.         test    al,2
  4244.         jz      c_ok
  4245.         loop    c_accept
  4246.       c_error:
  4247.         mov     ah,1
  4248.         jmp     c_exit
  4249.       c_ok:
  4250.         xor     ah,ah
  4251.       c_exit:
  4252.         ret
  4253.  
  4254.  
  4255. setmouse:  ; set mousepicture -pointer
  4256.            ; ps2 mouse enable
  4257.  
  4258.      mov     [MOUSE_PICTURE],dword mousepointer
  4259.  
  4260.      cli
  4261.  
  4262.      ret
  4263.  
  4264. if used _rdtsc
  4265. _rdtsc:
  4266.      bt [cpu_caps], CAPS_TSC
  4267.      jnc ret_rdtsc
  4268.      rdtsc
  4269.      ret
  4270.    ret_rdtsc:
  4271.      mov   edx,0xffffffff
  4272.      mov   eax,0xffffffff
  4273.      ret
  4274. end if
  4275.  
  4276. rerouteirqs:
  4277.  
  4278.         cli
  4279.  
  4280.         mov     al,0x11         ;  icw4, edge triggered
  4281.         out     0x20,al
  4282.         call    pic_delay
  4283.         out     0xA0,al
  4284.         call    pic_delay
  4285.  
  4286.         mov     al,0x20         ;  generate 0x20 +
  4287.         out     0x21,al
  4288.         call    pic_delay
  4289.         mov     al,0x28         ;  generate 0x28 +
  4290.         out     0xA1,al
  4291.         call    pic_delay
  4292.  
  4293.         mov     al,0x04         ;  slave at irq2
  4294.         out     0x21,al
  4295.         call    pic_delay
  4296.         mov     al,0x02         ;  at irq9
  4297.         out     0xA1,al
  4298.         call    pic_delay
  4299.  
  4300.         mov     al,0x01         ;  8086 mode
  4301.         out     0x21,al
  4302.         call    pic_delay
  4303.         out     0xA1,al
  4304.         call    pic_delay
  4305.  
  4306.         mov     al,255          ; mask all irq's
  4307.         out     0xA1,al
  4308.         call    pic_delay
  4309.         out     0x21,al
  4310.         call    pic_delay
  4311.  
  4312.         mov     ecx,0x1000
  4313.         cld
  4314. picl1:  call    pic_delay
  4315.         loop    picl1
  4316.  
  4317.         mov     al,255          ; mask all irq's
  4318.         out     0xA1,al
  4319.         call    pic_delay
  4320.         out     0x21,al
  4321.         call    pic_delay
  4322.  
  4323.         cli
  4324.  
  4325.         ret
  4326.  
  4327.  
  4328. pic_delay:
  4329.  
  4330.         jmp     pdl1
  4331. pdl1:   ret
  4332.  
  4333.  
  4334. sys_msg_board_str:
  4335.  
  4336.      pushad
  4337.    @@:
  4338.      cmp    [esi],byte 0
  4339.      je     @f
  4340.      mov    eax,1
  4341.      movzx  ebx,byte [esi]
  4342.      call   sys_msg_board
  4343.      inc    esi
  4344.      jmp    @b
  4345.    @@:
  4346.      popad
  4347.      ret
  4348.  
  4349. sys_msg_board_byte:
  4350. ; in: al = byte to display
  4351. ; out: nothing
  4352. ; destroys: nothing
  4353.         pushad
  4354.         mov     ecx, 2
  4355.         shl     eax, 24
  4356.         jmp     @f
  4357.  
  4358. sys_msg_board_word:
  4359. ; in: ax = word to display
  4360. ; out: nothing
  4361. ; destroys: nothing
  4362.         pushad
  4363.         mov     ecx, 4
  4364.         shl     eax, 16
  4365.         jmp     @f
  4366.  
  4367. sys_msg_board_dword:
  4368. ; in: eax = dword to display
  4369. ; out: nothing
  4370. ; destroys: nothing
  4371.         pushad
  4372.         mov     ecx, 8
  4373. @@:
  4374.         push    ecx
  4375.         rol     eax, 4
  4376.         push    eax
  4377.         and     al, 0xF
  4378.         cmp     al, 10
  4379.         sbb     al, 69h
  4380.         das
  4381.         mov     bl, al
  4382.         xor     eax, eax
  4383.         inc     eax
  4384.         call    sys_msg_board
  4385.         pop     eax
  4386.         pop     ecx
  4387.         loop    @b
  4388.         popad
  4389.         ret
  4390.  
  4391. uglobal
  4392.   msg_board_data: times 4096 db 0
  4393.   msg_board_count dd 0x0
  4394. endg
  4395.  
  4396. sys_msg_board:
  4397.  
  4398. ; eax=1 : write :  bl byte to write
  4399. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4400.  
  4401.         mov     ecx, [msg_board_count]
  4402.         cmp     eax, 1
  4403.         jne     .smbl1
  4404.  
  4405. if defined debug_com_base
  4406.  
  4407.         push    dx ax
  4408.  
  4409.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4410.         mov     dx, debug_com_base+5
  4411.         in      al, dx
  4412.         test    al, 1 shl 5
  4413.         jz      @r
  4414.  
  4415.         mov     dx, debug_com_base      ; Output the byte
  4416.         mov     al, bl
  4417.         out     dx, al
  4418.  
  4419.         pop     ax dx
  4420.  
  4421. end if
  4422.  
  4423.         mov     [msg_board_data+ecx],bl
  4424.         inc     ecx
  4425.         and     ecx, 4095
  4426.         mov     [msg_board_count], ecx
  4427.         mov     [check_idle_semaphore], 5
  4428.         ret
  4429. .smbl1:
  4430.         cmp     eax, 2
  4431.         jne     .smbl2
  4432.         test    ecx, ecx
  4433.         jz      .smbl21
  4434.         mov     eax, msg_board_data+1
  4435.         mov     ebx, msg_board_data
  4436.         movzx   edx, byte [ebx]
  4437.         call    memmove
  4438.         dec     [msg_board_count]
  4439.         mov     [esp + 36], edx ;eax
  4440.         mov     [esp + 24], dword 1
  4441.         ret
  4442. .smbl21:
  4443.         mov     [esp+36], ecx
  4444.         mov     [esp+24], ecx
  4445. .smbl2:
  4446.         ret
  4447.  
  4448.  
  4449.  
  4450. sys_process_def:
  4451.         mov     edi, [CURRENT_TASK]
  4452.  
  4453.         dec     eax             ; 1 = set keyboard mode
  4454.      jne   no_set_keyboard_setup
  4455.  
  4456.      shl   edi,8
  4457.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4458.  
  4459.      ret
  4460.  
  4461.    no_set_keyboard_setup:
  4462.  
  4463.         dec     eax             ; 2 = get keyboard mode
  4464.      jne   no_get_keyboard_setup
  4465.  
  4466.      shl   edi,8
  4467.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4468.  
  4469.      mov   [esp+36],eax
  4470.  
  4471.      ret
  4472.  
  4473.    no_get_keyboard_setup:
  4474.  
  4475.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4476.      jne   no_get_keyboard_cas
  4477.  
  4478. ;     xor   eax,eax
  4479. ;     movzx eax,byte [shift]
  4480. ;     movzx ebx,byte [ctrl]
  4481. ;     shl   ebx,2
  4482. ;     add   eax,ebx
  4483. ;     movzx ebx,byte [alt]
  4484. ;     shl   ebx,3
  4485. ;     add   eax,ebx
  4486.  
  4487.  ;// mike.dld [
  4488.      mov   eax, [kb_state]
  4489.  ;// mike.dld ]
  4490.  
  4491.      mov   [esp+36],eax
  4492.  
  4493.      ret
  4494.  
  4495.    no_get_keyboard_cas:
  4496.  
  4497.         dec     eax
  4498.         jnz     no_add_keyboard_hotkey
  4499.  
  4500.         mov     eax, hotkey_list
  4501. @@:
  4502.         cmp     dword [eax+8], 0
  4503.         jz      .found_free
  4504.         add     eax, 16
  4505.         cmp     eax, hotkey_list+16*256
  4506.         jb      @b
  4507.         mov     dword [esp+36], 1
  4508.         ret
  4509. .found_free:
  4510.         mov     [eax+8], edi
  4511.         mov     [eax+4], ecx
  4512.         movzx   ebx, bl
  4513.         lea     ebx, [hotkey_scancodes+ebx*4]
  4514.         mov     ecx, [ebx]
  4515.         mov     [eax], ecx
  4516.         mov     [ebx], eax
  4517.         mov     [eax+12], ebx
  4518.         jecxz   @f
  4519.         mov     [ecx+12], eax
  4520. @@:
  4521.         and     dword [esp+36], 0
  4522.         ret
  4523.  
  4524. no_add_keyboard_hotkey:
  4525.  
  4526.         dec     eax
  4527.         jnz     no_del_keyboard_hotkey
  4528.  
  4529.         movzx   ebx, bl
  4530.         lea     ebx, [hotkey_scancodes+ebx*4]
  4531.         mov     eax, [ebx]
  4532. .scan:
  4533.         test    eax, eax
  4534.         jz      .notfound
  4535.         cmp     [eax+8], edi
  4536.         jnz     .next
  4537.         cmp     [eax+4], ecx
  4538.         jz      .found
  4539. .next:
  4540.         mov     eax, [eax]
  4541.         jmp     .scan
  4542. .notfound:
  4543.         mov     dword [esp+36], 1
  4544.         ret
  4545. .found:
  4546.         mov     ecx, [eax]
  4547.         jecxz   @f
  4548.         mov     edx, [eax+12]
  4549.         mov     [ecx+12], edx
  4550. @@:
  4551.         mov     ecx, [eax+12]
  4552.         mov     edx, [eax]
  4553.         mov     [ecx], edx
  4554.         xor     edx, edx
  4555.         mov     [eax+4], edx
  4556.         mov     [eax+8], edx
  4557.         mov     [eax+12], edx
  4558.         mov     [eax], edx
  4559.         mov     [esp+36], edx
  4560.         ret
  4561.  
  4562. no_del_keyboard_hotkey:
  4563.      ret
  4564.  
  4565.  
  4566. align 4
  4567.  
  4568. sys_gs:                         ; direct screen access
  4569.  
  4570.      cmp  eax,1                 ; resolution
  4571.      jne  no_gs1
  4572.      mov  eax,[Screen_Max_X]
  4573.      shl  eax,16
  4574.      mov  ax,[Screen_Max_Y]
  4575.      add  eax,0x00010001
  4576.      mov  [esp+36],eax
  4577.      ret
  4578.    no_gs1:
  4579.  
  4580.      cmp   eax,2                ; bits per pixel
  4581.      jne   no_gs2
  4582.      movzx eax,byte [ScreenBPP]
  4583.      mov   [esp+36],eax
  4584.      ret
  4585.    no_gs2:
  4586.  
  4587.      cmp   eax,3                ; bytes per scanline
  4588.      jne   no_gs3
  4589.      mov   eax,[BytesPerScanLine]
  4590.      mov   [esp+36],eax
  4591.      ret
  4592.    no_gs3:
  4593.  
  4594.      or  [esp+36],dword -1
  4595.      ret
  4596.  
  4597.  
  4598. align 4 ; PCI functions
  4599.  
  4600. sys_pci:
  4601.  
  4602.      call  pci_api
  4603.      mov   [esp+36],eax
  4604.      ret
  4605.  
  4606.  
  4607. align 4  ;  system functions
  4608.  
  4609. syscall_setpixel:                       ; SetPixel
  4610.  
  4611.         mov     eax, ebx
  4612.         mov     ebx, ecx
  4613.         mov     ecx, edx
  4614.         mov     edx, [TASK_BASE]
  4615.         add     eax, [edx-twdw+WDATA.box.left]
  4616.         add     ebx, [edx-twdw+WDATA.box.top]
  4617.         mov     edi, [current_slot]
  4618.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4619.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4620.         xor     edi, edi ; no force
  4621. ;       mov     edi, 1
  4622.         call    [_display.disable_mouse]
  4623.         jmp     [putpixel]
  4624.  
  4625. align 4
  4626.  
  4627. syscall_writetext:                      ; WriteText
  4628.  
  4629.         mov   eax,[TASK_BASE]
  4630.         mov   ebp,[eax-twdw+WDATA.box.left]
  4631.         push  esi
  4632.         mov   esi,[current_slot]
  4633.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4634.         shl   ebp,16
  4635.         add   ebp,[eax-twdw+WDATA.box.top]
  4636.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4637.         pop   esi
  4638.         add   ebx,ebp
  4639.         mov   eax,edi
  4640.         xor   edi,edi
  4641.         jmp   dtext
  4642.  
  4643. align 4
  4644.  
  4645. syscall_openramdiskfile:                ; OpenRamdiskFile
  4646.  
  4647.         mov     eax, ebx
  4648.         mov     ebx, ecx
  4649.         mov     ecx, edx
  4650.         mov     edx, esi
  4651.         mov     esi, 12
  4652.         call    fileread
  4653.         mov     [esp+32], eax
  4654.         ret
  4655.  
  4656. align 4
  4657.  
  4658. syscall_drawrect:                       ; DrawRect
  4659.  
  4660.         mov     edi, edx ; color + gradient
  4661.         and     edi, 0x80FFFFFF
  4662.         test    bx, bx  ; x.size
  4663.         je      .drectr
  4664.         test    cx, cx ; y.size
  4665.         je      .drectr
  4666.  
  4667.         mov     eax, ebx ; bad idea
  4668.         mov     ebx, ecx
  4669.  
  4670.         movzx   ecx, ax ; ecx - x.size
  4671.         shr     eax, 16 ; eax - x.coord
  4672.         movzx   edx, bx ; edx - y.size
  4673.         shr     ebx, 16 ; ebx - y.coord
  4674.         mov     esi, [current_slot]
  4675.  
  4676.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4677.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4678.         add     ecx, eax
  4679.         add     edx, ebx
  4680.         jmp     [drawbar]
  4681. .drectr:
  4682.         ret
  4683.  
  4684. align 4
  4685. syscall_getscreensize:                  ; GetScreenSize
  4686.         mov     ax, [Screen_Max_X]
  4687.         shl     eax, 16
  4688.         mov     ax, [Screen_Max_Y]
  4689.         mov     [esp + 32], eax
  4690.         ret
  4691.  
  4692. align 4
  4693.  
  4694. syscall_cdaudio:                        ; CD
  4695.  
  4696.         cmp     ebx, 4
  4697.         jb      .audio
  4698.         jz      .eject
  4699.         cmp     ebx, 5
  4700.         jnz     .ret
  4701. .load:
  4702.         call    .reserve
  4703.         call    LoadMedium
  4704.         ;call    .free
  4705.                 jmp             .free
  4706. ;        ret
  4707. .eject:
  4708.         call    .reserve
  4709.         call    clear_CD_cache
  4710.         call    allow_medium_removal
  4711.         call    EjectMedium
  4712. ;        call    .free
  4713.                 jmp             .free
  4714. ;        ret
  4715. .audio:
  4716.      call  sys_cd_audio
  4717.      mov   [esp+36-4],eax
  4718. .ret:
  4719.      ret
  4720.  
  4721. .reserve:
  4722.         call    reserve_cd
  4723.         mov     eax, ecx
  4724.         shr     eax, 1
  4725.         and     eax, 1
  4726.         inc     eax
  4727.         mov     [ChannelNumber], ax
  4728.         mov     eax, ecx
  4729.         and     eax, 1
  4730.         mov     [DiskNumber], al
  4731.         call    reserve_cd_channel
  4732.         and     ebx, 3
  4733.         inc     ebx
  4734.         mov     [cdpos], ebx
  4735.         add     ebx, ebx
  4736.         mov     cl, 8
  4737.         sub     cl, bl
  4738.         mov     al, [DRIVE_DATA+1]
  4739.         shr     al, cl
  4740.         test    al, 2
  4741.         jz      .free;.err
  4742.         ret
  4743. .free:
  4744.         call    free_cd_channel
  4745.         and     [cd_status], 0
  4746.         ret
  4747. .err:
  4748.         call    .free
  4749. ;        pop     eax
  4750.         ret
  4751.  
  4752. align 4
  4753.  
  4754. syscall_getpixel:                       ; GetPixel
  4755.      mov   ecx, [Screen_Max_X]
  4756.      inc   ecx
  4757.      xor   edx, edx
  4758.      mov   eax, ebx
  4759.      div   ecx
  4760.      mov   ebx, edx
  4761.      xchg  eax, ebx
  4762.      call  dword [GETPIXEL] ; eax - x, ebx - y
  4763.      mov   [esp + 32], ecx
  4764.      ret
  4765.  
  4766. align 4
  4767.  
  4768. syscall_getarea:
  4769. ;eax = 36
  4770. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4771. ;ecx = [size x]*65536 + [size y]
  4772. ;edx = [start x]*65536 + [start y]
  4773.      pushad
  4774.          inc   [mouse_pause]
  4775. ; Check of use of the hardware cursor.
  4776.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4777.           jne  @f
  4778. ; Since the test for the coordinates of the mouse should not be used,
  4779. ; then use the call [disable_mouse] is not possible!
  4780.       cmp  dword [MOUSE_VISIBLE],dword 0
  4781.       jne  @f
  4782.       pushf
  4783.       cli
  4784.       call draw_mouse_under
  4785.       popf
  4786.       mov  [MOUSE_VISIBLE],dword 1
  4787. @@:
  4788.      mov   edi,ebx
  4789.      mov   eax,edx
  4790.      shr   eax,16
  4791.      mov   ebx,edx
  4792.      and   ebx,0xffff
  4793.      dec   eax
  4794.            dec   ebx
  4795.      ; eax - x, ebx - y
  4796.      mov   edx,ecx
  4797.      
  4798.      shr   ecx,16
  4799.      and   edx,0xffff
  4800.      mov   esi,ecx
  4801.      ; ecx - size x, edx - size y
  4802.          
  4803.          mov   ebp,edx
  4804.          dec   ebp
  4805.      lea   ebp,[ebp*3]
  4806.          
  4807.          imul  ebp,esi
  4808.          
  4809.          mov   esi,ecx
  4810.          dec   esi
  4811.          lea   esi,[esi*3]
  4812.          
  4813.      add   ebp,esi
  4814.      add   ebp,edi
  4815.  
  4816.      add   ebx,edx
  4817.          
  4818. .start_y:
  4819.      push  ecx edx
  4820. .start_x:
  4821.      push  eax ebx ecx
  4822.      add   eax,ecx
  4823.  
  4824.      call  dword [GETPIXEL] ; eax - x, ebx - y
  4825.      
  4826.      mov   [ebp],cx
  4827.      shr   ecx,16
  4828.      mov   [ebp+2],cl
  4829.  
  4830.      pop   ecx ebx eax
  4831.      sub   ebp,3
  4832.      dec   ecx
  4833.      jnz   .start_x
  4834.          pop   edx ecx
  4835.          dec   ebx
  4836.      dec   edx
  4837.      jnz   .start_y
  4838.      dec        [mouse_pause]
  4839. ; Check of use of the hardware cursor.
  4840.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4841.           jne  @f
  4842.          call  [draw_pointer]
  4843. @@:
  4844.      popad
  4845.      ret
  4846.  
  4847. align 4
  4848.  
  4849. syscall_drawline:                       ; DrawLine
  4850.  
  4851.         mov     edi, [TASK_BASE]
  4852.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4853.         mov     ebp, eax
  4854.         mov     esi, [current_slot]
  4855.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4856.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4857.         add     ebp,ebx
  4858.         shl     eax, 16
  4859.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4860.         add     eax, ebp
  4861.         mov     ebp, ebx
  4862.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4863.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4864.         add     ebp, ecx
  4865.         shl     ebx, 16
  4866.         xor     edi, edi
  4867.         add     ebx, ebp
  4868.         mov     ecx, edx
  4869.         jmp     [draw_line]
  4870.  
  4871. align 4
  4872.  
  4873. syscall_getirqowner:                    ; GetIrqOwner
  4874.  
  4875.      cmp   ebx,16
  4876.      jae   .err
  4877.  
  4878.      cmp   [irq_rights + 4 * ebx], dword 2
  4879.      je    .err
  4880.  
  4881.      mov   eax,[4 * ebx + irq_owner]
  4882.      mov   [esp+32],eax
  4883.  
  4884.      ret
  4885. .err:
  4886.      or    dword [esp+32], -1
  4887.      ret
  4888.  
  4889. align 4
  4890.  
  4891. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4892.  
  4893.      call  r_f_port_area
  4894.      mov   [esp+32],eax
  4895.      ret
  4896.  
  4897. align 4
  4898.  
  4899. syscall_threads:                        ; CreateThreads
  4900. ; eax=1 create thread
  4901. ;
  4902. ;   ebx=thread start
  4903. ;   ecx=thread stack value
  4904. ;
  4905. ; on return : eax = pid
  4906.  
  4907.      call  new_sys_threads
  4908.  
  4909.      mov   [esp+32],eax
  4910.      ret
  4911.  
  4912. align 4
  4913.  
  4914. stack_driver_stat:
  4915.  
  4916.      call  app_stack_handler            ; Stack status
  4917.  
  4918. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4919. ;     call  change_task                 ; between sent packet
  4920.  
  4921.      mov   [esp+32],eax
  4922.      ret
  4923.  
  4924. align 4
  4925.  
  4926. socket:                                 ; Socket interface
  4927.      call  app_socket_handler
  4928.  
  4929. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4930. ;     call  change_task                 ; between sent packet
  4931.  
  4932.      mov   [esp+36],eax
  4933.      mov   [esp+24],ebx
  4934.      ret
  4935.  
  4936. align 4
  4937.  
  4938. read_from_hd:                           ; Read from hd - fn not in use
  4939.  
  4940.      mov   edi,[TASK_BASE]
  4941.      add   edi,TASKDATA.mem_start
  4942.      add   eax,[edi]
  4943.      add   ecx,[edi]
  4944.      add   edx,[edi]
  4945.      call  file_read
  4946.  
  4947.      mov   [esp+36],eax
  4948.      mov   [esp+24],ebx
  4949.  
  4950.      ret
  4951.  
  4952. paleholder:
  4953.         ret
  4954.  
  4955. align 4
  4956. set_screen:
  4957.         cmp eax, [Screen_Max_X]
  4958.         jne .set
  4959.  
  4960.         cmp edx, [Screen_Max_Y]
  4961.         jne .set
  4962.         ret
  4963. .set:
  4964.         pushfd
  4965.         cli
  4966.  
  4967.         mov [Screen_Max_X], eax
  4968.         mov [Screen_Max_Y], edx
  4969.         mov [BytesPerScanLine], ecx
  4970.  
  4971.         mov [screen_workarea.right],eax
  4972.         mov [screen_workarea.bottom], edx
  4973.  
  4974.         push ebx
  4975.         push esi
  4976.         push edi
  4977.  
  4978.         pushad
  4979.  
  4980.         stdcall kernel_free, [_WinMapAddress]
  4981.  
  4982.         mov eax, [_display.width]
  4983.         mul [_display.height]
  4984.         mov [_WinMapSize], eax
  4985.  
  4986.         stdcall kernel_alloc, eax
  4987.         mov [_WinMapAddress], eax
  4988.         test eax, eax
  4989.         jz .epic_fail
  4990.  
  4991.         popad
  4992.  
  4993.         call    repos_windows
  4994.         xor eax, eax
  4995.         xor ebx, ebx
  4996.         mov     ecx, [Screen_Max_X]
  4997.         mov     edx, [Screen_Max_Y]
  4998.         call    calculatescreen
  4999.         pop edi
  5000.         pop esi
  5001.         pop ebx
  5002.  
  5003.         popfd
  5004.         ret
  5005.  
  5006. .epic_fail:
  5007.         hlt                     ; Houston, we've had a problem
  5008.  
  5009. ; --------------- APM ---------------------
  5010. uglobal
  5011. apm_entry       dp      0
  5012. apm_vf          dd      0
  5013. endg
  5014.  
  5015. align 4
  5016. sys_apm:
  5017.         xor     eax,eax
  5018.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5019.         jne     @f
  5020.         inc     eax
  5021.         or      dword [esp + 44], eax   ; error
  5022.         add     eax,7
  5023.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5024.         ret
  5025.  
  5026. @@:
  5027. ;       xchg    eax, ecx
  5028. ;       xchg    ebx, ecx
  5029.  
  5030.         cmp     dx, 3
  5031.         ja      @f
  5032.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5033.         mov     eax,[apm_vf]
  5034.         mov     [esp + 32], eax
  5035.         shr     eax, 16
  5036.         mov     [esp + 28], eax
  5037.         ret
  5038.  
  5039. @@:
  5040.  
  5041.         mov     esi,[master_tab+(OS_BASE shr 20)]
  5042.         xchg    [master_tab], esi
  5043.         push    esi
  5044.         mov     edi, cr3
  5045.         mov     cr3, edi                ;flush TLB
  5046.  
  5047.         call    pword [apm_entry]       ;call APM BIOS
  5048.  
  5049.         xchg    eax, [esp]
  5050.         mov     [master_tab], eax
  5051.         mov     eax, cr3
  5052.         mov     cr3, eax
  5053.         pop eax
  5054.  
  5055.         mov     [esp + 4 ], edi
  5056.         mov     [esp + 8], esi
  5057.         mov     [esp + 20], ebx
  5058.         mov     [esp + 24], edx
  5059.         mov     [esp + 28], ecx
  5060.         mov     [esp + 32], eax
  5061.         setc    al
  5062.         and     [esp + 44], byte 0xfe
  5063.         or      [esp + 44], al
  5064.         ret
  5065. ; -----------------------------------------
  5066.  
  5067. align 4
  5068.  
  5069. undefined_syscall:                      ; Undefined system call
  5070.      mov   [esp + 32], dword -1
  5071.      ret
  5072.  
  5073. align 4
  5074. system_shutdown:          ; shut down the system
  5075.  
  5076.            cmp byte [BOOT_VAR+0x9030], 1
  5077.            jne @F
  5078.            ret
  5079. @@:
  5080.            call stop_all_services
  5081.            push 3                ; stop playing cd
  5082.            pop  eax
  5083.            call sys_cd_audio
  5084.  
  5085. yes_shutdown_param:
  5086.            cli
  5087.  
  5088.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  5089.            push 12
  5090.            pop  esi
  5091.            xor  ebx,ebx
  5092.            or   ecx,-1
  5093.            mov  edx, OS_BASE+0x70000
  5094.            call fileread
  5095.  
  5096.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5097.            mov  edi,OS_BASE+0x40000
  5098.            mov  ecx,1000
  5099.            rep  movsb
  5100.  
  5101.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5102.            mov  edi, OS_BASE
  5103.            mov  ecx,0x10000/4
  5104.            cld
  5105.            rep movsd
  5106.  
  5107.            call restorefatchain
  5108.  
  5109.            mov al, 0xFF
  5110.            out 0x21, al
  5111.            out 0xA1, al
  5112.  
  5113. if 0
  5114.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5115.            mov  word [OS_BASE+0x467+2],0x1000
  5116.  
  5117.            mov  al,0x0F
  5118.            out  0x70,al
  5119.            mov  al,0x05
  5120.            out  0x71,al
  5121.  
  5122.            mov  al,0xFE
  5123.            out  0x64,al
  5124.  
  5125.            hlt
  5126.            jmp $-1
  5127.  
  5128. else
  5129.         cmp     byte [OS_BASE + 0x9030], 2
  5130.         jnz     no_acpi_power_off
  5131.  
  5132. ; scan for RSDP
  5133. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5134.         movzx   eax, word [OS_BASE + 0x40E]
  5135.         shl     eax, 4
  5136.         jz      @f
  5137.         mov     ecx, 1024/16
  5138.         call    scan_rsdp
  5139.         jnc     .rsdp_found
  5140. @@:
  5141. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5142.         mov     eax, 0xE0000
  5143.         mov     ecx, 0x2000
  5144.         call    scan_rsdp
  5145.         jc      no_acpi_power_off
  5146. .rsdp_found:
  5147.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5148.         mov     ebp, [ipc_tmp]
  5149.         stdcall map_page, ebp, esi, PG_MAP
  5150.         lea     eax, [esi+1000h]
  5151.         lea     edx, [ebp+1000h]
  5152.         stdcall map_page, edx, eax, PG_MAP
  5153.         and     esi, 0xFFF
  5154.         add     esi, ebp
  5155.         cmp     dword [esi], 'RSDT'
  5156.         jnz     no_acpi_power_off
  5157.         mov     ecx, [esi+4]
  5158.         sub     ecx, 24h
  5159.         jbe     no_acpi_power_off
  5160.         shr     ecx, 2
  5161.         add     esi, 24h
  5162. .scan_fadt:
  5163.         lodsd
  5164.         mov     ebx, eax
  5165.         lea     eax, [ebp+2000h]
  5166.         stdcall map_page, eax, ebx, PG_MAP
  5167.         lea     eax, [ebp+3000h]
  5168.         add     ebx, 0x1000
  5169.         stdcall map_page, eax, ebx, PG_MAP
  5170.         and     ebx, 0xFFF
  5171.         lea     ebx, [ebx+ebp+2000h]
  5172.         cmp     dword [ebx], 'FACP'
  5173.         jz      .fadt_found
  5174.         loop    .scan_fadt
  5175.         jmp     no_acpi_power_off
  5176. .fadt_found:
  5177. ; ebx is linear address of FADT
  5178.         mov     edi, [ebx+40] ; physical address of the DSDT
  5179.         lea     eax, [ebp+4000h]
  5180.         stdcall map_page, eax, edi, PG_MAP
  5181.         lea     eax, [ebp+5000h]
  5182.         lea     esi, [edi+0x1000]
  5183.         stdcall map_page, eax, esi, PG_MAP
  5184.         and     esi, 0xFFF
  5185.         sub     edi, esi
  5186.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5187.         jnz     no_acpi_power_off
  5188.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5189.         sub     eax, 36+4
  5190.         jbe     no_acpi_power_off
  5191.         add     esi, 36
  5192. .scan_dsdt:
  5193.         cmp     dword [esi+ebp+4000h], '_S5_'
  5194.         jnz     .scan_dsdt_cont
  5195.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5196.         jnz     .scan_dsdt_cont
  5197.         mov     dl, [esi+ebp+4000h+6]
  5198.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5199.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5200.         ja      .scan_dsdt_cont
  5201.         cmp     dl, 1
  5202.         jb      .scan_dsdt_cont
  5203.         lea     esi, [esi+ebp+4000h+7]
  5204.         xor     ecx, ecx
  5205.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5206.         jz      @f
  5207.         cmp     byte [esi], 0xA
  5208.         jnz     no_acpi_power_off
  5209.         inc     esi
  5210.         mov     cl, [esi]
  5211. @@:
  5212.         inc     esi
  5213.         cmp     dl, 2
  5214.         jb      @f
  5215.         cmp     byte [esi], 0
  5216.         jz      @f
  5217.         cmp     byte [esi], 0xA
  5218.         jnz     no_acpi_power_off
  5219.         inc     esi
  5220.         mov     ch, [esi]
  5221. @@:
  5222.         jmp     do_acpi_power_off
  5223. .scan_dsdt_cont:
  5224.         inc     esi
  5225.         cmp     esi, 0x1000
  5226.         jb      @f
  5227.         sub     esi, 0x1000
  5228.         add     edi, 0x1000
  5229.         push    eax
  5230.         lea     eax, [ebp+4000h]
  5231.         stdcall map_page, eax, edi, PG_MAP
  5232.         push    PG_MAP
  5233.         lea     eax, [edi+1000h]
  5234.         push    eax
  5235.         lea     eax, [ebp+5000h]
  5236.         push    eax
  5237.         stdcall map_page
  5238.         pop     eax
  5239. @@:
  5240.         dec     eax
  5241.         jnz     .scan_dsdt
  5242.         jmp     no_acpi_power_off
  5243. do_acpi_power_off:
  5244.         mov     edx, [ebx+48]
  5245.         test    edx, edx
  5246.         jz      .nosmi
  5247.         mov     al, [ebx+52]
  5248.         out     dx, al
  5249.         mov     edx, [ebx+64]
  5250. @@:
  5251.         in      ax, dx
  5252.         test    al, 1
  5253.         jz      @b
  5254. .nosmi:
  5255.         and     cx, 0x0707
  5256.         shl     cx, 2
  5257.         or      cx, 0x2020
  5258.         mov     edx, [ebx+64]
  5259.         in      ax, dx
  5260.         and     ax, 203h
  5261.         or      ah, cl
  5262.         out     dx, ax
  5263.         mov     edx, [ebx+68]
  5264.         test    edx, edx
  5265.         jz      @f
  5266.         in      ax, dx
  5267.         and     ax, 203h
  5268.         or      ah, ch
  5269.         out     dx, ax
  5270. @@:
  5271.         jmp     $
  5272.  
  5273.  
  5274. no_acpi_power_off:
  5275.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5276.            mov  word [OS_BASE+0x467+2],0x1000
  5277.  
  5278.            mov  al,0x0F
  5279.            out  0x70,al
  5280.            mov  al,0x05
  5281.            out  0x71,al
  5282.  
  5283.            mov  al,0xFE
  5284.            out  0x64,al
  5285.  
  5286.            hlt
  5287.            jmp $-1
  5288.  
  5289. scan_rsdp:
  5290.         add     eax, OS_BASE
  5291. .s:
  5292.         cmp     dword [eax], 'RSD '
  5293.         jnz     .n
  5294.         cmp     dword [eax+4], 'PTR '
  5295.         jnz     .n
  5296.         xor     edx, edx
  5297.         xor     esi, esi
  5298. @@:
  5299.         add     dl, [eax+esi]
  5300.         inc     esi
  5301.         cmp     esi, 20
  5302.         jnz     @b
  5303.         test    dl, dl
  5304.         jz      .ok
  5305. .n:
  5306.         add     eax, 10h
  5307.         loop    .s
  5308.         stc
  5309. .ok:
  5310.         ret
  5311. end if
  5312.  
  5313. include "data32.inc"
  5314.  
  5315. __REV__ = __REV
  5316.  
  5317. uglobals_size = $ - endofcode
  5318. diff16 "end of kernel code",0,$
  5319.