Subversion Repositories Kolibri OS

Rev

Rev 1312 | Rev 1316 | 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: 1314 $
  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.5.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  setwindowdefaults
  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.  
  755.         call init_cursors
  756.         mov eax, [def_cursor]
  757.         mov [SLOT_BASE+APPDATA.cursor],eax
  758.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  759.  
  760.   ; READ TSC / SECOND
  761.  
  762.         mov   esi,boot_tsc
  763.         call  boot_log
  764.         cli
  765.         rdtsc ;call  _rdtsc
  766.         mov   ecx,eax
  767.         mov   esi,250               ; wait 1/4 a second
  768.         call  delay_ms
  769.         rdtsc ;call  _rdtsc
  770.         sti
  771.         sub   eax,ecx
  772.         shl   eax,2
  773.         mov   [CPU_FREQ],eax          ; save tsc / sec
  774. ;       mov ebx, 1000000
  775. ;       div ebx
  776. ; ¢®®¡é¥-â® ¯à®¨§¢®¤¨â¥«ì­®áâì ¢ ¤ ­­®¬ ª®­ªà¥â­®¬ ¬¥áâ¥
  777. ; ᮢ¥à襭­® ­¥ªà¨â¨ç­ , ­® çâ®¡ë § âª­ãâì «î¡¨â¥«¥©
  778. ; ®¯â¨¬¨§¨àãîé¨å ª®¬¯¨«ïâ®à®¢ Ÿ‚“...
  779.         mov     edx, 2251799814
  780.         mul     edx
  781.         shr     edx, 19
  782.         mov [stall_mcs], edx
  783. ; PRINT CPU FREQUENCY
  784.         mov     esi, boot_cpufreq
  785.         call    boot_log
  786.  
  787.         mov     ebx, edx
  788.         movzx   ecx, word [boot_y]
  789.         add     ecx, (10+17*6) shl 16 - 10 ; 'CPU frequency is '
  790.         mov     edx, 0xFFFFFF
  791.         xor     edi,edi
  792.         mov     eax, 0x00040000
  793.                 inc             edi
  794.         call    display_number_force
  795.  
  796. ; SET VARIABLES
  797.  
  798.         call  set_variables
  799.  
  800. ; SET MOUSE
  801.  
  802.         ;call   detect_devices
  803.         stdcall load_driver, szPS2MDriver
  804. ;        stdcall load_driver, szCOM_MDriver
  805.  
  806.         mov   esi,boot_setmouse
  807.         call  boot_log
  808.         call  setmouse
  809.  
  810.  
  811. ; STACK AND FDC
  812.  
  813.         call  stack_init
  814.         call  fdc_init
  815.  
  816. ; PALETTE FOR 320x200 and 640x480 16 col
  817.  
  818.         cmp   [SCR_MODE],word 0x12
  819.         jne   no_pal_vga
  820.         mov   esi,boot_pal_vga
  821.         call  boot_log
  822.         call  paletteVGA
  823.       no_pal_vga:
  824.  
  825.         cmp   [SCR_MODE],word 0x13
  826.         jne   no_pal_ega
  827.         mov   esi,boot_pal_ega
  828.         call  boot_log
  829.         call  palette320x200
  830.       no_pal_ega:
  831.  
  832. ; LOAD DEFAULT SKIN
  833.  
  834.         call    load_default_skin
  835.  
  836. ;protect io permission map
  837.  
  838.            mov esi, [default_io_map]
  839.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map], PG_MAP
  840.            add esi, 0x1000
  841.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  842.  
  843.            stdcall map_page,tss._io_map_0,\
  844.                    [SLOT_BASE+256+APPDATA.io_map], PG_MAP
  845.            stdcall map_page,tss._io_map_1,\
  846.                    [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  847.  
  848.   mov ax,[OS_BASE+0x10000+bx_from_load]
  849.   cmp ax,'r1'           ; if not rused ram disk - load network configuration from files {SPraid.simba}
  850.   je  no_st_network
  851.         call set_network_conf
  852.   no_st_network:
  853.  
  854. ; LOAD FIRST APPLICATION
  855.         cli
  856.  
  857.         cmp   byte [BOOT_VAR+0x9030],1
  858.         jne   no_load_vrr_m
  859.  
  860.         mov     ebp, vrr_m
  861.         call    fs_execute_from_sysdir
  862.  
  863. ;        cmp   eax,2                  ; if vrr_m app found (PID=2)
  864.         sub   eax,2
  865.         jz    first_app_found
  866.  
  867. no_load_vrr_m:
  868.  
  869.         mov     ebp, firstapp
  870.         call    fs_execute_from_sysdir
  871.  
  872. ;        cmp   eax,2                  ; continue if a process has been loaded
  873.         sub   eax,2
  874.         jz    first_app_found
  875.  
  876.         mov     esi, boot_failed
  877.         call    boot_log
  878.  
  879.         mov   eax, 0xDEADBEEF        ; otherwise halt
  880.         hlt
  881.  
  882. first_app_found:
  883.  
  884.         cli
  885.  
  886.         ;mov   [TASK_COUNT],dword 2
  887.         push  1
  888.         pop   dword [CURRENT_TASK]      ; set OS task fisrt
  889.  
  890. ; SET KEYBOARD PARAMETERS
  891.         mov   al, 0xf6         ; reset keyboard, scan enabled
  892.         call  kb_write
  893.  
  894.         ; wait until 8042 is ready
  895.         xor ecx,ecx
  896.       @@:
  897.         in     al,64h
  898.         and    al,00000010b
  899.         loopnz @b
  900.  
  901.        ; mov   al, 0xED       ; svetodiody - only for testing!
  902.        ; call  kb_write
  903.        ; call  kb_read
  904.        ; mov   al, 111b
  905.        ; call  kb_write
  906.        ; call  kb_read
  907.  
  908.         mov   al, 0xF3       ; set repeat rate & delay
  909.         call  kb_write
  910. ;        call  kb_read
  911.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  912.         call  kb_write
  913. ;        call  kb_read
  914.      ;// mike.dld [
  915.         call  set_lights
  916.      ;// mike.dld ]
  917.  
  918.  
  919. ; Setup serial output console (if enabled)
  920.  
  921. if defined debug_com_base
  922.  
  923.         ; enable Divisor latch
  924.  
  925.         mov     dx, debug_com_base+3
  926.         mov     al, 1 shl 7
  927.         out     dx, al
  928.  
  929.         ; Set speed to 115200 baud (max speed)
  930.  
  931.         mov     dx, debug_com_base
  932.         mov     al, 0x01
  933.         out     dx, al
  934.  
  935.         mov     dx, debug_com_base+1
  936.         mov     al, 0x00
  937.         out     dx, al
  938.  
  939.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  940.  
  941.         mov     dx, debug_com_base+3
  942.         mov     al, 3
  943.         out     dx, al
  944.  
  945.         ; disable interrupts
  946.  
  947.         mov     dx, debug_com_base+1
  948.         mov     al, 0
  949.         out     dx, al
  950.  
  951.         ; clear +  enable fifo (64 bits)
  952.  
  953.         mov     dx, debug_com_base+2
  954.         mov     al, 0x7 + 1 shl 5
  955.         out     dx, al
  956.  
  957.  
  958. end if
  959.  
  960. ; START MULTITASKING
  961.  
  962. if preboot_blogesc
  963.         mov     esi, boot_tasking
  964.         call    boot_log
  965. .bll1:  in      al, 0x60        ; wait for ESC key press
  966.         cmp     al, 129
  967.         jne     .bll1
  968. end if
  969.  
  970. ;       mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  971.  
  972. ; UNMASK ALL IRQ'S
  973.  
  974.         mov   esi,boot_allirqs
  975.         call  boot_log
  976.  
  977.         cli                          ;guarantee forbidance of interrupts.
  978.         mov   al,0                   ; unmask all irq's
  979.         out   0xA1,al
  980.         out   0x21,al
  981.  
  982.         mov   ecx,32
  983.  
  984.      ready_for_irqs:
  985.  
  986.         mov   al,0x20                ; ready for irqs
  987.         out   0x20,al
  988.         out   0xa0,al
  989.  
  990.         loop  ready_for_irqs         ; flush the queue
  991.  
  992.         stdcall attach_int_handler, dword 1, irq1, dword 0
  993.  
  994. ;        mov    [dma_hdd],1
  995.         cmp     [IDEContrRegsBaseAddr], 0
  996.         setnz   [dma_hdd]
  997.         mov [timer_ticks_enable],1              ; for cd driver
  998.  
  999.         sti
  1000.         call change_task
  1001.  
  1002.         jmp osloop
  1003.  
  1004. ;        jmp   $                      ; wait here for timer to take control
  1005.  
  1006.         ; Fly :)
  1007.  
  1008. include 'unpacker.inc'
  1009. include 'fdo.inc'
  1010.  
  1011. align 4
  1012. boot_log:
  1013.          pushad
  1014.  
  1015.         mov   ebx,10*65536
  1016.         mov   bx,word [boot_y]
  1017.         add   [boot_y],dword 10
  1018.         mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  1019.                 xor       edi,edi
  1020.         mov   edx,esi
  1021.                 inc       edi
  1022.         call  dtext
  1023.  
  1024.         mov   [novesachecksum],1000
  1025.         call  checkVga_N13
  1026.  
  1027.         popad
  1028.  
  1029.         ret
  1030.  
  1031. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1032. ;                                                                    ;
  1033. ;                    MAIN OS LOOP START                              ;
  1034. ;                                                                    ;
  1035. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1036. align 32
  1037. osloop:
  1038.         call   [draw_pointer]
  1039.         call   checkbuttons
  1040.         call   checkwindows
  1041. ;       call   check_window_move_request
  1042.         call   checkmisc
  1043.         call   checkVga_N13
  1044.         call   stack_handler
  1045.         call   checkidle
  1046.         call   check_fdd_motor_status
  1047.         call   check_ATAPI_device_event
  1048.         jmp    osloop
  1049. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1050. ;                                                                    ;
  1051. ;                      MAIN OS LOOP END                              ;
  1052. ;                                                                    ;
  1053. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1054. align 4
  1055. checkidle:
  1056.         pushad
  1057.         call    change_task
  1058.         jmp     idle_loop_entry
  1059.   idle_loop:
  1060.         cmp     eax,[idlemem]     ; eax == [timer_ticks]
  1061.         jne     idle_exit
  1062.         rdtsc   ;call _rdtsc
  1063.         mov     ecx,eax
  1064.         hlt
  1065.         rdtsc   ;call _rdtsc
  1066.         sub     eax,ecx
  1067.         add     [idleuse],eax
  1068.   idle_loop_entry:
  1069.         mov     eax,[timer_ticks] ; eax =  [timer_ticks]
  1070.         cmp     [check_idle_semaphore],0
  1071.         je      idle_loop
  1072.         dec     [check_idle_semaphore]
  1073.   idle_exit:
  1074.         mov     [idlemem],eax     ; eax == [timer_ticks]
  1075.         popad
  1076.         ret
  1077.  
  1078. uglobal
  1079.   idlemem               dd   0x0
  1080.   idleuse               dd   0x0
  1081.   idleusesec            dd   0x0
  1082.   check_idle_semaphore  dd   0x0
  1083. endg
  1084.  
  1085.  
  1086.  
  1087. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1088. ;                                                                      ;
  1089. ;                   INCLUDED SYSTEM FILES                              ;
  1090. ;                                                                      ;
  1091. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1092.  
  1093.  
  1094. include "kernel32.inc"
  1095.  
  1096.  
  1097. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1098. ;                                                                      ;
  1099. ;                       KERNEL FUNCTIONS                               ;
  1100. ;                                                                      ;
  1101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1102.  
  1103. reserve_irqs_ports:
  1104.  
  1105.         push eax
  1106.         xor     eax,eax
  1107.                 inc     eax
  1108.         mov  byte [irq_owner+4*0],al            ;1    ; timer
  1109.         ;mov  [irq_owner+4*1], 1    ; keyboard
  1110.         mov  byte [irq_owner+4*6],al            ;1    ; floppy diskette
  1111.         mov  byte [irq_owner+4*13],al   ;1   ; math co-pros
  1112.         mov  byte [irq_owner+4*14],al   ;1   ; ide I
  1113.         mov  byte [irq_owner+4*15],al   ;1   ; ide II
  1114.         pop  eax
  1115.  
  1116. ; RESERVE PORTS
  1117.         push  4
  1118.         pop   dword [RESERVED_PORTS]    ;,edi
  1119.  
  1120.         push  1
  1121.         pop   dword [RESERVED_PORTS+16+0]       ;,dword 1
  1122.         and   dword [RESERVED_PORTS+16+4],0     ;,dword 0x0
  1123.         mov   dword [RESERVED_PORTS+16+8],0x2d  ;,dword 0x2d
  1124.  
  1125.         push  1
  1126.         pop   dword [RESERVED_PORTS+32+0]       ;,dword 1
  1127.         push  0x30
  1128.         pop   dword [RESERVED_PORTS+32+4]       ;,dword 0x30
  1129.         push  0x4d
  1130.         pop   dword [RESERVED_PORTS+32+8]       ;,dword 0x4d
  1131.  
  1132.         push  1
  1133.         pop   dword [RESERVED_PORTS+48+0]       ;,dword 1
  1134.         push  0x50
  1135.         pop   dword [RESERVED_PORTS+48+4]       ;,dword 0x50
  1136.         mov   dword [RESERVED_PORTS+48+8],0xdf  ;,dword 0xdf
  1137.  
  1138.         push  1
  1139.         pop   dword [RESERVED_PORTS+64+0]       ;,dword 1
  1140.  
  1141.         mov   dword [RESERVED_PORTS+64+4],0xe5  ;,dword 0xe5
  1142.         mov   dword [RESERVED_PORTS+64+8],0xff  ;,dword 0xff
  1143.  
  1144.         ret
  1145.  
  1146. setirqreadports:
  1147.  
  1148.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1149.             and   dword [irq12read+4],0                   ; end of port list
  1150. ;        mov   [irq12read+4],dword 0                  ; end of port list
  1151.         ;mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1152.         ;mov   [irq04read+4],dword 0                  ; end of port list
  1153.         ;mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1154.         ;mov   [irq03read+4],dword 0                  ; end of port list
  1155.  
  1156.         ret
  1157.  
  1158. iglobal
  1159.   process_number dd 0x1
  1160. endg
  1161.  
  1162. set_variables:
  1163.  
  1164.         mov   ecx,0x100                       ; flush port 0x60
  1165. .fl60:  in    al,0x60
  1166.         loop  .fl60
  1167.         push  eax
  1168.  
  1169.         mov   ax,[BOOT_VAR+0x900c]
  1170.         shr   ax,1
  1171.         shl   eax,16
  1172.         mov   ax,[BOOT_VAR+0x900A]
  1173.         shr   ax,1
  1174.         mov   [MOUSE_X],eax
  1175.  
  1176.         xor   eax,eax
  1177.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  1178.  
  1179.         mov   byte [MOUSE_BUFF_COUNT],al                 ; mouse buffer
  1180.         mov   byte [KEY_COUNT],al                 ; keyboard buffer
  1181.         mov   byte [BTN_COUNT],al                 ; button buffer
  1182. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1183.  
  1184.      ;!! IP 04.02.2005:
  1185.         mov   byte [DONT_SWITCH],al ; change task if possible
  1186.         pop   eax
  1187.         ret
  1188.  
  1189. align 4
  1190. ;input  eax=43,bl-byte of output, ecx - number of port
  1191. sys_outport:
  1192.  
  1193.     mov   edi,ecx          ; separate flag for read / write
  1194.     and   ecx,65535
  1195.  
  1196.     mov   eax,[RESERVED_PORTS]
  1197.     test  eax,eax
  1198.     jnz   .sopl8
  1199.     inc   eax
  1200.         mov   [esp+32],eax
  1201.     ret
  1202.  
  1203.   .sopl8:
  1204.     mov   edx,[TASK_BASE]
  1205.     mov   edx,[edx+0x4]
  1206.     ;and   ecx,65535
  1207.     ;cld - set on interrupt 0x40
  1208.   .sopl1:
  1209.  
  1210.     mov   esi,eax
  1211.     shl   esi,4
  1212.     add   esi,RESERVED_PORTS
  1213.     cmp   edx,[esi+0]
  1214.     jne   .sopl2
  1215.     cmp   ecx,[esi+4]
  1216.     jb    .sopl2
  1217.     cmp   ecx,[esi+8]
  1218.     jg    .sopl2
  1219. .sopl3:
  1220.  
  1221.     test  edi,0x80000000 ; read ?
  1222.     jnz   .sopl4
  1223.  
  1224.         mov       eax,ebx
  1225.     mov   dx,cx          ; write
  1226.     out   dx,al
  1227.     and   [esp+32],dword 0
  1228.     ret
  1229.  
  1230.         .sopl2:
  1231.  
  1232.     dec   eax
  1233.     jnz   .sopl1
  1234.         inc       eax
  1235.     mov   [esp+32],eax
  1236.     ret
  1237.  
  1238.  
  1239.   .sopl4:
  1240.  
  1241.     mov   dx,cx          ; read
  1242.     in    al,dx
  1243.     and   eax,0xff
  1244.     and   [esp+32],dword 0
  1245.     mov   [esp+20],eax
  1246.     ret
  1247.  
  1248. display_number:
  1249.  
  1250. ; eax = print type, al=0 -> ebx is number
  1251. ;                   al=1 -> ebx is pointer
  1252. ;                   ah=0 -> display decimal
  1253. ;                   ah=1 -> display hexadecimal
  1254. ;                   ah=2 -> display binary
  1255. ;                   eax bits 16-21 = number of digits to display (0-32)
  1256. ;                   eax bits 22-31 = reserved
  1257. ;
  1258. ; ebx = number or pointer
  1259. ; ecx = x shl 16 + y
  1260. ; edx = color
  1261.     xor     edi, edi
  1262. display_number_force:
  1263.     push  eax
  1264.     and   eax,0x3fffffff
  1265.     cmp   eax,0xffff            ; length > 0 ?
  1266.     pop   eax
  1267.     jge   cont_displ
  1268.     ret
  1269.    cont_displ:
  1270.      push  eax
  1271.      and   eax,0x3fffffff
  1272.      cmp   eax,61*0x10000        ; length <= 60 ?
  1273.      pop   eax
  1274.      jb    cont_displ2
  1275.      ret
  1276.    cont_displ2:
  1277.  
  1278.      pushad
  1279.  
  1280.      cmp   al,1                  ; ecx is a pointer ?
  1281.      jne   displnl1
  1282.      mov   ebp,ebx
  1283.      add   ebp,4
  1284.      mov   ebp,[ebp+std_application_base_address]
  1285.      mov   ebx,[ebx+std_application_base_address]
  1286.  displnl1:
  1287.      sub   esp,64
  1288.  
  1289.     test   ah,ah                  ; DECIMAL
  1290.     jnz   no_display_desnum
  1291.     shr   eax,16
  1292.     and   eax,0xC03f
  1293. ;     and   eax,0x3f
  1294.     push  eax
  1295.     and   eax,0x3f
  1296.     mov   edi,esp
  1297.     add   edi,4+64-1
  1298.     mov   ecx,eax
  1299.     mov   eax,ebx
  1300.     mov   ebx,10
  1301.  d_desnum:
  1302.      xor   edx,edx
  1303.      call  division_64_bits
  1304.      div   ebx
  1305.      add   dl,48
  1306.      mov   [edi],dl
  1307.      dec   edi
  1308.      loop  d_desnum
  1309.      pop   eax
  1310.      call  normalize_number
  1311.      call  draw_num_text
  1312.      add   esp,64
  1313.      popad
  1314.      ret
  1315.    no_display_desnum:
  1316.  
  1317.      cmp   ah,0x01               ; HEXADECIMAL
  1318.      jne   no_display_hexnum
  1319.      shr   eax,16
  1320.      and   eax,0xC03f
  1321. ;     and   eax,0x3f
  1322.      push  eax
  1323.      and   eax,0x3f
  1324.      mov   edi,esp
  1325.      add   edi,4+64-1
  1326.      mov   ecx,eax
  1327.      mov   eax,ebx
  1328.      mov   ebx,16
  1329.    d_hexnum:
  1330.      xor   edx,edx
  1331.      call  division_64_bits
  1332.      div   ebx
  1333.    hexletters = __fdo_hexdigits
  1334.      add   edx,hexletters
  1335.      mov   dl,[edx]
  1336.      mov   [edi],dl
  1337.      dec   edi
  1338.      loop  d_hexnum
  1339.      pop   eax
  1340.      call  normalize_number
  1341.      call  draw_num_text
  1342.      add   esp,64
  1343.      popad
  1344.      ret
  1345.    no_display_hexnum:
  1346.  
  1347.      cmp   ah,0x02               ; BINARY
  1348.      jne   no_display_binnum
  1349.      shr   eax,16
  1350.      and   eax,0xC03f
  1351. ;     and   eax,0x3f
  1352.      push  eax
  1353.      and   eax,0x3f
  1354.      mov   edi,esp
  1355.      add   edi,4+64-1
  1356.      mov   ecx,eax
  1357.      mov   eax,ebx
  1358.      mov   ebx,2
  1359.    d_binnum:
  1360.      xor   edx,edx
  1361.      call  division_64_bits
  1362.      div   ebx
  1363.      add   dl,48
  1364.      mov   [edi],dl
  1365.      dec   edi
  1366.      loop  d_binnum
  1367.      pop   eax
  1368.      call  normalize_number
  1369.      call  draw_num_text
  1370.      add   esp,64
  1371.      popad
  1372.      ret
  1373.    no_display_binnum:
  1374.  
  1375.      add   esp,64
  1376.      popad
  1377.      ret
  1378.  
  1379. normalize_number:
  1380.      test  ah,0x80
  1381.      jz   .continue
  1382.      mov  ecx,48
  1383.      and   eax,0x3f
  1384. @@:
  1385.      inc   edi
  1386.      cmp   [edi],cl
  1387.      jne   .continue
  1388.      dec   eax
  1389.      cmp   eax,1
  1390.      ja    @r
  1391.      mov   al,1
  1392. .continue:
  1393.      and   eax,0x3f
  1394.      ret
  1395.  
  1396. division_64_bits:
  1397.      test  [esp+1+4],byte 0x40
  1398.      jz   .continue
  1399.      push  eax
  1400.      mov   eax,ebp
  1401.      div   ebx
  1402.      mov   ebp,eax
  1403.      pop   eax
  1404. .continue:
  1405.      ret
  1406.  
  1407. draw_num_text:
  1408.      mov   esi,eax
  1409.      mov   edx,64+4
  1410.      sub   edx,eax
  1411.      add   edx,esp
  1412.      mov   ebx,[esp+64+32-8+4]
  1413. ; add window start x & y
  1414.      mov   ecx,[TASK_BASE]
  1415.  
  1416.      mov   edi,[CURRENT_TASK]
  1417.      shl   edi,8
  1418.  
  1419.      mov   eax,[ecx-twdw+WDATA.box.left]
  1420.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1421.      shl   eax,16
  1422.      add   eax,[ecx-twdw+WDATA.box.top]
  1423.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1424.      add   ebx,eax
  1425.      mov   ecx,[esp+64+32-12+4]
  1426.         and     ecx, not 0x80000000     ; force counted string
  1427.         mov     eax, [esp+64+8]         ; background color (if given)
  1428.         mov     edi, [esp+64+4]
  1429.      jmp   dtext
  1430.  
  1431. align 4
  1432.  
  1433. sys_setup:
  1434.  
  1435. ; 1=roland mpu midi base , base io address
  1436. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1437. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1438. ; 5=system language, 1eng 2fi 3ger 4rus
  1439. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1440. ; 8=fat32 partition in hd
  1441. ; 9
  1442. ; 10 = sound dma channel
  1443. ; 11 = enable lba read
  1444. ; 12 = enable pci access
  1445.  
  1446.  
  1447.         and  [esp+32],dword 0
  1448.         dec     ebx                             ; MIDI
  1449.         jnz  nsyse1
  1450.         cmp  ecx,0x100
  1451.  
  1452.         jb   nsyse1
  1453.         mov     esi,65535
  1454.         cmp     esi,ecx
  1455.  
  1456.         jb   nsyse1
  1457.         mov  [midi_base],cx     ;bx
  1458.         mov  word [mididp],cx   ;bx
  1459.         inc  cx                 ;bx
  1460.         mov  word [midisp],cx   ;bx
  1461.         ret
  1462.  
  1463. iglobal
  1464. midi_base dw 0
  1465. endg
  1466.  
  1467.    nsyse1:
  1468.         dec     ebx                              ; KEYBOARD
  1469.         jnz  nsyse2
  1470.         mov  edi,[TASK_BASE]
  1471.         mov  eax,[edi+TASKDATA.mem_start]
  1472.         add  eax,edx
  1473.  
  1474.         dec     ecx
  1475.         jnz  kbnobase
  1476.         mov  ebx,keymap
  1477.         mov  ecx,128
  1478.         call memmove
  1479.         ret
  1480.    kbnobase:
  1481.         dec  ecx
  1482.         jnz  kbnoshift
  1483.  
  1484.         mov  ebx,keymap_shift
  1485.         mov  ecx,128
  1486.         call memmove
  1487.         ret
  1488.    kbnoshift:
  1489.         dec  ecx
  1490.         jnz  kbnoalt
  1491.         mov  ebx,keymap_alt
  1492.         mov  ecx,128
  1493.         call memmove
  1494.         ret
  1495.    kbnoalt:
  1496.         sub  ecx,6
  1497.         jnz  kbnocountry
  1498.         mov  word [keyboard],dx
  1499.         ret
  1500.    kbnocountry:
  1501.         mov  [esp+32],dword 1
  1502.         ret
  1503.    nsyse2:
  1504.         dec  ebx                            ; CD
  1505.         jnz  nsyse4
  1506.  
  1507.         test ecx,ecx
  1508.         jz   nosesl
  1509.  
  1510.         cmp  ecx, 4
  1511.         ja   nosesl
  1512.         mov  [cd_base],cl
  1513.  
  1514.         dec     ecx
  1515.         jnz  noprma
  1516.         mov  [cdbase],0x1f0
  1517.         mov  [cdid],0xa0
  1518.    noprma:
  1519.  
  1520.         dec     ecx
  1521.         jnz  noprsl
  1522.         mov  [cdbase],0x1f0
  1523.         mov  [cdid],0xb0
  1524.    noprsl:
  1525.         dec     ecx
  1526.         jnz  nosema
  1527.         mov  [cdbase],0x170
  1528.         mov  [cdid],0xa0
  1529.    nosema:
  1530.         dec     ecx
  1531.         jnz  nosesl
  1532.         mov  [cdbase],0x170
  1533.         mov  [cdid],0xb0
  1534.    nosesl:
  1535.         ret
  1536.  
  1537. iglobal
  1538. cd_base db 0
  1539.  
  1540. endg
  1541.    nsyse4:
  1542.                  
  1543.         sub  ebx,2               ; SYSTEM LANGUAGE
  1544.         jnz  nsyse5
  1545.         mov  [syslang],ecx
  1546.         ret
  1547.    nsyse5:
  1548.          
  1549.         sub  ebx,2              ; HD BASE
  1550.         jnz  nsyse7
  1551.  
  1552.         test ecx,ecx
  1553.         jz   nosethd
  1554.  
  1555.         cmp  ecx,4
  1556.         ja   nosethd
  1557.         mov  [hd_base],cl
  1558.  
  1559.         cmp  ecx,1
  1560.         jnz  noprmahd
  1561.         mov  [hdbase],0x1f0
  1562.         and  dword [hdid],0x0
  1563.         mov  dword [hdpos],ecx
  1564. ;     call set_FAT32_variables
  1565.    noprmahd:
  1566.  
  1567.         cmp  ecx,2
  1568.         jnz  noprslhd
  1569.         mov  [hdbase],0x1f0
  1570.         mov  [hdid],0x10
  1571.         mov  dword [hdpos],ecx
  1572. ;     call set_FAT32_variables
  1573.    noprslhd:
  1574.  
  1575.         cmp  ecx,3
  1576.         jnz  nosemahd
  1577.         mov  [hdbase],0x170
  1578.         and  dword [hdid],0x0
  1579.         mov  dword [hdpos],ecx
  1580. ;     call set_FAT32_variables
  1581.    nosemahd:
  1582.  
  1583.         cmp  ecx,4
  1584.         jnz  noseslhd
  1585.         mov  [hdbase],0x170
  1586.         mov  [hdid],0x10
  1587.         mov  dword [hdpos],ecx
  1588. ;     call set_FAT32_variables
  1589.    noseslhd:
  1590.         call  reserve_hd1
  1591.         call  reserve_hd_channel
  1592.         call  free_hd_channel
  1593.         and   dword [hd1_status],0        ; free
  1594.    nosethd:
  1595.         ret
  1596.  
  1597. iglobal
  1598. hd_base db 0
  1599. endg
  1600.  
  1601. nsyse7:
  1602.  
  1603. ;     cmp  eax,8                      ; HD PARTITION
  1604.         dec  ebx
  1605.         jnz  nsyse8
  1606.         mov  [fat32part],ecx
  1607. ;     call set_FAT32_variables
  1608.         call  reserve_hd1
  1609.         call  reserve_hd_channel
  1610.         call  free_hd_channel
  1611. ;       pusha
  1612.         call  choice_necessity_partition_1
  1613. ;       popa
  1614.         and dword [hd1_status],0        ; free
  1615.         ret
  1616.  
  1617. nsyse8:
  1618. ;     cmp  eax,11                     ; ENABLE LBA READ
  1619.         and  ecx,1
  1620.         sub  ebx,3
  1621.         jnz  no_set_lba_read
  1622.         mov  [lba_read_enabled],ecx
  1623.         ret
  1624.  
  1625. no_set_lba_read:
  1626. ;     cmp  eax,12                     ; ENABLE PCI ACCESS
  1627.         dec  ebx
  1628.         jnz  no_set_pci_access
  1629.         mov  [pci_access_enabled],ecx
  1630.         ret
  1631. no_set_pci_access:
  1632.  
  1633. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1634. include 'vmodeint.inc'
  1635. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1636.  
  1637. sys_setup_err:
  1638.         or  [esp+32],dword -1
  1639.         ret
  1640.  
  1641. align 4
  1642.  
  1643. sys_getsetup:
  1644.  
  1645. ; 1=roland mpu midi base , base io address
  1646. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1647. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1648. ; 5=system language, 1eng 2fi 3ger 4rus
  1649. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1650. ; 8=fat32 partition in hd
  1651. ; 9=get hs timer tic
  1652.  
  1653. ;     cmp  eax,1
  1654.         dec     ebx
  1655.         jnz  ngsyse1
  1656.         movzx eax,[midi_base]
  1657.         mov  [esp+32],eax
  1658.         ret
  1659. ngsyse1:
  1660. ;     cmp  eax,2
  1661.         dec     ebx
  1662.         jnz  ngsyse2
  1663.  
  1664.         mov  edi,[TASK_BASE]
  1665.         mov  ebx,[edi+TASKDATA.mem_start]
  1666.         add  ebx,edx
  1667.  
  1668. ;     cmp  ebx,1
  1669.         dec     ecx
  1670.         jnz  kbnobaseret
  1671.         mov  eax,keymap
  1672.         mov  ecx,128
  1673.         call memmove
  1674.         ret
  1675. kbnobaseret:
  1676. ;     cmp  ebx,2
  1677.         dec     ecx
  1678.         jnz  kbnoshiftret
  1679.  
  1680.         mov  eax,keymap_shift
  1681.         mov  ecx,128
  1682.         call memmove
  1683.         ret
  1684. kbnoshiftret:
  1685. ;     cmp  ebx,3
  1686.         dec     ecx
  1687.         jne  kbnoaltret
  1688.  
  1689.         mov  eax,keymap_alt
  1690.         mov  ecx,128
  1691.         call memmove
  1692.         ret
  1693. kbnoaltret:
  1694. ;     cmp  ebx,9
  1695.         sub     ecx,6
  1696.         jnz  ngsyse2
  1697.         movzx eax,word [keyboard]
  1698.         mov  [esp+32],eax
  1699.         ret
  1700.  
  1701.  
  1702. ngsyse2:
  1703. ;         cmp  eax,3
  1704.         dec     ebx
  1705.         jnz  ngsyse3
  1706.         movzx eax,[cd_base]
  1707.         mov  [esp+32],eax
  1708.         ret
  1709. ngsyse3:
  1710. ;         cmp  eax,5
  1711.         sub     ebx,2
  1712.         jnz  ngsyse5
  1713.         mov  eax,[syslang]
  1714.         mov  [esp+32],eax
  1715.         ret
  1716. ngsyse5:
  1717. ;     cmp  eax,7
  1718.         sub     ebx,2
  1719.         jnz  ngsyse7
  1720.         movzx eax,[hd_base]
  1721.         mov  [esp+32],eax
  1722.         ret
  1723. ngsyse7:
  1724. ;     cmp  eax,8
  1725.         dec     ebx
  1726.         jnz  ngsyse8
  1727.         mov eax,[fat32part]
  1728.         mov  [esp+32],eax
  1729.         ret
  1730. ngsyse8:
  1731. ;     cmp  eax,9
  1732.         dec     ebx
  1733.         jnz  ngsyse9
  1734.         mov  eax,[timer_ticks] ;[0xfdf0]
  1735.         mov  [esp+32],eax
  1736.         ret
  1737. ngsyse9:
  1738. ;     cmp  eax,11
  1739.         sub     ebx,2
  1740.         jnz  ngsyse11
  1741.         mov eax,[lba_read_enabled]
  1742.         mov  [esp+32],eax
  1743.         ret
  1744. ngsyse11:
  1745. ;     cmp  eax,12
  1746.         dec     ebx
  1747.         jnz  ngsyse12
  1748.         mov eax,[pci_access_enabled]
  1749.         mov  [esp+32],eax
  1750.         ret
  1751. ngsyse12:
  1752.         mov  [esp+32],dword 1
  1753.         ret
  1754.  
  1755.        
  1756. get_timer_ticks:
  1757.         mov eax,[timer_ticks]
  1758.         ret
  1759.  
  1760. iglobal
  1761. align 4
  1762. mousefn dd msscreen, mswin, msbutton, msset
  1763.         dd app_load_cursor
  1764.         dd app_set_cursor
  1765.         dd app_delete_cursor
  1766.         dd msz
  1767. endg
  1768.  
  1769. readmousepos:
  1770.  
  1771. ; eax=0 screen relative
  1772. ; eax=1 window relative
  1773. ; eax=2 buttons pressed
  1774. ; eax=3 set mouse pos   ; reserved
  1775. ; eax=4 load cursor
  1776. ; eax=5 set cursor
  1777. ; eax=6 delete cursor   ; reserved
  1778. ; eax=7 get mouse_z
  1779.  
  1780.            cmp ebx, 7
  1781.            ja msset
  1782.            jmp [mousefn+ebx*4]
  1783. msscreen:
  1784.            mov  eax,[MOUSE_X]
  1785.            shl  eax,16
  1786.            mov  ax,[MOUSE_Y]
  1787.            mov  [esp+36-4],eax
  1788.            ret
  1789. mswin:
  1790.            mov  eax,[MOUSE_X]
  1791.            shl  eax,16
  1792.            mov  ax,[MOUSE_Y]
  1793.            mov  esi,[TASK_BASE]
  1794.            mov  bx, word [esi-twdw+WDATA.box.left]
  1795.            shl  ebx,16
  1796.            mov  bx, word [esi-twdw+WDATA.box.top]
  1797.            sub  eax,ebx
  1798.  
  1799.            mov  edi,[CURRENT_TASK]
  1800.            shl  edi,8
  1801.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1802.            rol  eax,16
  1803.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1804.            rol  eax,16
  1805.            mov  [esp+36-4],eax
  1806.            ret
  1807. msbutton:
  1808.            movzx eax,byte [BTN_DOWN]
  1809.            mov  [esp+36-4],eax
  1810.            ret
  1811. msz:
  1812.            mov   edi, [TASK_COUNT]
  1813.            movzx edi, word [WIN_POS + edi*2]
  1814.            cmp   edi, [CURRENT_TASK]
  1815.            jne   @f
  1816.            mov   ax,[MOUSE_SCROLL_H]
  1817.            shl   eax,16
  1818.            mov   ax,[MOUSE_SCROLL_V]
  1819.            mov   [esp+36-4],eax
  1820.            and   [MOUSE_SCROLL_H],word 0
  1821.            and   [MOUSE_SCROLL_V],word 0
  1822.            ret
  1823.        @@:
  1824.            and  [esp+36-4],dword 0
  1825. ;           ret
  1826. msset:
  1827.            ret
  1828.  
  1829. app_load_cursor:
  1830.            cmp ecx, OS_BASE
  1831.            jae msset
  1832.            stdcall load_cursor, ecx, edx
  1833.            mov [esp+36-4], eax
  1834.            ret
  1835.  
  1836. app_set_cursor:
  1837.            stdcall set_cursor, ecx
  1838.            mov [esp+36-4], eax
  1839.            ret
  1840.  
  1841. app_delete_cursor:
  1842.            stdcall delete_cursor, ecx
  1843.            mov [esp+36-4], eax
  1844.            ret
  1845.  
  1846. is_input:
  1847.  
  1848.    push edx
  1849.    mov  dx,word [midisp]
  1850.    in   al,dx
  1851.    and  al,0x80
  1852.    pop  edx
  1853.    ret
  1854.  
  1855. is_output:
  1856.  
  1857.    push edx
  1858.    mov  dx,word [midisp]
  1859.    in   al,dx
  1860.    and  al,0x40
  1861.    pop  edx
  1862.    ret
  1863.  
  1864.  
  1865. get_mpu_in:
  1866.  
  1867.    push edx
  1868.    mov  dx,word [mididp]
  1869.    in   al,dx
  1870.    pop  edx
  1871.    ret
  1872.  
  1873.  
  1874. put_mpu_out:
  1875.  
  1876.    push edx
  1877.    mov  dx,word [mididp]
  1878.    out  dx,al
  1879.    pop  edx
  1880.    ret
  1881.  
  1882.  
  1883.  
  1884. align 4
  1885.  
  1886. sys_midi:
  1887.         cmp  [mididp],0
  1888.         jnz  sm0
  1889.         mov  [esp+36],dword 1
  1890.         ret
  1891. sm0:
  1892.         and  [esp+36],dword 0
  1893.         dec  ebx
  1894.         jnz  smn1
  1895.  ;    call setuart
  1896. su1:
  1897.         call is_output
  1898.         test al,al
  1899.         jnz  su1
  1900.         mov  dx,word [midisp]
  1901.         mov  al,0xff
  1902.         out  dx,al
  1903. su2:
  1904.         mov  dx,word [midisp]
  1905.         mov  al,0xff
  1906.         out  dx,al
  1907.         call is_input
  1908.         test al,al
  1909.         jnz  su2
  1910.         call get_mpu_in
  1911.         cmp  al,0xfe
  1912.         jnz  su2
  1913. su3:
  1914.         call is_output
  1915.         test  al,al
  1916.         jnz  su3
  1917.         mov  dx,word [midisp]
  1918.         mov  al,0x3f
  1919.         out  dx,al
  1920.         ret
  1921. smn1:
  1922.         dec  ebx
  1923.         jnz  smn2
  1924. sm10:
  1925.         call get_mpu_in
  1926.         call is_output
  1927.         test al,al
  1928.         jnz  sm10
  1929.         mov  al,bl
  1930.         call put_mpu_out
  1931.         smn2:
  1932.         ret
  1933.  
  1934. detect_devices:
  1935. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1936. ;include 'detect/commouse.inc'
  1937. ;include 'detect/ps2mouse.inc'
  1938. ;include 'detect/dev_fd.inc'
  1939. ;include 'detect/dev_hdcd.inc'
  1940. ;include 'detect/sear_par.inc'
  1941. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1942.     ret
  1943.  
  1944. sys_end:
  1945.  
  1946.      mov ecx, [current_slot]
  1947.      mov eax, [ecx+APPDATA.tls_base]
  1948.      test eax, eax
  1949.      jz @F
  1950.  
  1951.      stdcall user_free, eax
  1952. @@:
  1953.  
  1954.      mov   eax,[TASK_BASE]
  1955.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1956.  
  1957.     waitterm:            ; wait here for termination
  1958.      mov   ebx,100
  1959.      call  delay_hs
  1960.      jmp   waitterm
  1961.  
  1962. iglobal
  1963. align 4
  1964. sys_system_table:
  1965.         dd      exit_for_anyone         ; 1 = obsolete
  1966.         dd      sysfn_terminate         ; 2 = terminate thread
  1967.         dd      sysfn_activate          ; 3 = activate window
  1968.         dd      sysfn_getidletime       ; 4 = get idle time
  1969.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1970.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1971.         dd      sysfn_getactive         ; 7 = get active window
  1972.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1973.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1974.         dd      sysfn_minimize          ; 10 = minimize window
  1975.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1976.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1977.         dd      sysfn_getversion        ; 13 = get kernel version
  1978.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1979.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1980.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1981.         dd      sysfn_getallmem         ; 17 = get total memory size
  1982.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1983.                                         ;                 instead of slot
  1984.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1985.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1986.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1987.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1988. sysfn_num = ($ - sys_system_table)/4
  1989. endg
  1990.  
  1991. sys_system:
  1992.         dec     ebx
  1993.         cmp     ebx, sysfn_num
  1994.         jae     @f
  1995.         jmp     dword [sys_system_table + ebx*4]
  1996. @@:
  1997.         ret
  1998.  
  1999.  
  2000. sysfn_shutdown:          ; 18.9 = system shutdown
  2001.      cmp  ecx,1
  2002.      jl   exit_for_anyone
  2003.      cmp  ecx,4
  2004.      jg   exit_for_anyone
  2005.      mov  [BOOT_VAR+0x9030],cl
  2006.  
  2007.      mov  eax,[TASK_COUNT]
  2008.      mov  [SYS_SHUTDOWN],al
  2009.      mov  [shutdown_processes],eax
  2010.      and  dword [esp+32], 0
  2011.  exit_for_anyone:
  2012.      ret
  2013.   uglobal
  2014.    shutdown_processes: dd 0x0
  2015.   endg
  2016.  
  2017. sysfn_terminate:        ; 18.2 = TERMINATE
  2018.      cmp  ecx,2
  2019.      jb   noprocessterminate
  2020.      mov  edx,[TASK_COUNT]
  2021.      cmp  ecx,edx
  2022.      ja   noprocessterminate
  2023.      mov  eax,[TASK_COUNT]
  2024.      shl  ecx,5
  2025.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  2026.      add  ecx,CURRENT_TASK+TASKDATA.state
  2027.      cmp  byte [ecx], 9
  2028.      jz   noprocessterminate
  2029.  
  2030.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2031.      mov  [ecx],byte 3       ; clear possible i40's
  2032.      ;call MEM_Heap_UnLock
  2033.  
  2034.      cmp  edx,[application_table_status]    ; clear app table stat
  2035.      jne  noatsc
  2036.      mov  [application_table_status],0
  2037.    noatsc:
  2038.    noprocessterminate:
  2039.      ret
  2040.  
  2041. sysfn_terminate2:
  2042. ;lock application_table_status mutex
  2043. .table_status:
  2044.     cli
  2045.     cmp    [application_table_status],0
  2046.     je     .stf
  2047.     sti
  2048.     call   change_task
  2049.     jmp    .table_status
  2050. .stf:
  2051.     call   set_application_table_status
  2052.     mov    eax,ecx
  2053.     call   pid_to_slot
  2054.     test   eax,eax
  2055.     jz     .not_found
  2056.     mov    ecx,eax
  2057.     cli
  2058.     call   sysfn_terminate
  2059.     mov    [application_table_status],0
  2060.     sti
  2061.     and    dword [esp+32],0
  2062.     ret
  2063. .not_found:
  2064.     mov    [application_table_status],0
  2065.     or     dword [esp+32],-1
  2066.     ret
  2067.  
  2068. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2069.      cmp  ecx,2
  2070.      jb   .nowindowactivate
  2071.      cmp  ecx,[TASK_COUNT]
  2072.      ja   .nowindowactivate
  2073.  
  2074.      mov   [window_minimize], 2   ; restore window if minimized
  2075.  
  2076.      movzx esi, word [WIN_STACK + ecx*2]
  2077.      cmp   esi, [TASK_COUNT]
  2078.      je    .nowindowactivate ; already active
  2079.  
  2080.      mov   edi, ecx
  2081.      shl   edi, 5
  2082.      add   edi, window_data
  2083.      movzx esi, word [WIN_STACK + ecx * 2]
  2084.      lea   esi, [WIN_POS + esi * 2]
  2085.      call  waredraw
  2086. .nowindowactivate:
  2087.      ret
  2088.  
  2089. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2090.      mov  eax,[idleusesec]
  2091.      mov  [esp+32], eax
  2092.      ret
  2093.  
  2094. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2095.      mov  eax,[CPU_FREQ]
  2096.      mov  [esp+32], eax
  2097.      ret
  2098.  
  2099. ;  SAVE ramdisk to /hd/1/menuet.img
  2100. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2101.    include 'blkdev/rdsave.inc'
  2102. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2103. align 4
  2104. sysfn_getactive:        ; 18.7 = get active window
  2105.      mov  eax, [TASK_COUNT]
  2106.    movzx  eax, word [WIN_POS + eax*2]
  2107.      mov  [esp+32],eax
  2108.      ret
  2109.  
  2110. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2111. ;     cmp  ecx,1
  2112.      dec  ecx
  2113.      jnz  nogetsoundflag
  2114.      movzx  eax,byte [sound_flag] ; get sound_flag
  2115.      mov  [esp+32],eax
  2116.      ret
  2117.  nogetsoundflag:
  2118. ;     cmp  ecx,2
  2119.      dec  ecx
  2120.      jnz  nosoundflag
  2121.      xor  byte [sound_flag], 1
  2122.  nosoundflag:
  2123.      ret
  2124.  
  2125. sysfn_minimize:         ; 18.10 = minimize window
  2126.      mov   [window_minimize],1
  2127.      ret
  2128. align 4
  2129. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2130. ;     cmp  ecx,1
  2131.      dec  ecx
  2132.      jnz  full_table
  2133.   small_table:
  2134.      call for_all_tables
  2135.      mov ecx,10
  2136.      cld
  2137.      rep movsb
  2138.      ret
  2139.    for_all_tables:
  2140.      mov edi,edx
  2141.      mov esi,DRIVE_DATA
  2142.      ret
  2143.   full_table:
  2144. ;     cmp  ecx,2
  2145.      dec  ecx
  2146.      jnz  exit_for_anyone
  2147.      call for_all_tables
  2148.      mov ecx,16384
  2149.      cld
  2150.      rep movsd
  2151.      ret
  2152.  
  2153. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2154.         and     dword [esp+32], 0
  2155.         ret
  2156.  
  2157. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2158.      mov edi,ebx
  2159.      mov esi,version_inf
  2160.      mov ecx,version_end-version_inf
  2161.      rep movsb
  2162.      ret
  2163.  
  2164. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2165.      ;wait retrace functions
  2166.  sys_wait_retrace:
  2167.      mov edx,0x3da
  2168.  WaitRetrace_loop:
  2169.      in al,dx
  2170.      test al,1000b
  2171.      jz WaitRetrace_loop
  2172.      and [esp+32],dword 0
  2173.      ret
  2174.  
  2175. align 4
  2176. sysfn_centermouse:      ; 18.15 = mouse centered
  2177. ; removed here by <Lrz>
  2178. ;     call  mouse_centered
  2179. ;* mouse centered - start code- Mario79
  2180. ;mouse_centered:
  2181. ;        push  eax
  2182.         mov   eax,[Screen_Max_X]
  2183.         shr   eax,1
  2184.         mov   [MOUSE_X],ax
  2185.         mov   eax,[Screen_Max_Y]
  2186.         shr   eax,1
  2187.         mov   [MOUSE_Y],ax
  2188. ;        ret
  2189. ;* mouse centered - end code- Mario79
  2190.         xor   eax,eax
  2191.         and   [esp+32],eax
  2192. ;        pop   eax
  2193.  
  2194.      ret
  2195. align 4
  2196. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2197.      test ecx,ecx  ; get mouse speed factor
  2198.      jnz  .set_mouse_acceleration
  2199.      xor  eax,eax
  2200.      mov  ax,[mouse_speed_factor]
  2201.      mov  [esp+32],eax
  2202.      ret
  2203.  .set_mouse_acceleration:
  2204. ;     cmp  ecx,1  ; set mouse speed factor
  2205.      dec  ecx
  2206.      jnz  .get_mouse_delay
  2207.      mov  [mouse_speed_factor],dx
  2208.      ret
  2209.  .get_mouse_delay:
  2210. ;     cmp  ecx,2  ; get mouse delay
  2211.      dec  ecx
  2212.      jnz  .set_mouse_delay
  2213.      mov  eax,[mouse_delay]
  2214.      mov  [esp+32],eax
  2215.      ret
  2216.  .set_mouse_delay:
  2217. ;     cmp  ecx,3  ; set mouse delay
  2218.      dec  ecx
  2219.      jnz  .set_pointer_position
  2220.      mov  [mouse_delay],edx
  2221.      ret
  2222.  .set_pointer_position:
  2223. ;     cmp  ecx,4  ; set mouse pointer position
  2224.      dec   ecx
  2225.      jnz  .set_mouse_button
  2226.      mov   [MOUSE_Y],dx    ;y
  2227.      ror   edx,16
  2228.      mov   [MOUSE_X],dx    ;x
  2229.      rol   edx,16
  2230.      ret
  2231.  .set_mouse_button:
  2232. ;     cmp   ecx,5  ; set mouse button features
  2233.      dec   ecx
  2234.      jnz  .end
  2235.      mov   [BTN_DOWN],dl
  2236.      mov   [mouse_active],1
  2237.  .end:
  2238.      ret
  2239.  
  2240. sysfn_getfreemem:
  2241.      mov eax, [pg_data.pages_free]
  2242.      shl eax, 2
  2243.      mov [esp+32],eax
  2244.      ret
  2245.  
  2246. sysfn_getallmem:
  2247.      mov  eax,[MEM_AMOUNT]
  2248.      shr eax, 10
  2249.      mov  [esp+32],eax
  2250.      ret
  2251.  
  2252. ; // Alver, 2007-22-08 // {
  2253. sysfn_pid_to_slot:
  2254.      mov   eax, ecx
  2255.      call  pid_to_slot
  2256.      mov   [esp+32], eax
  2257.      ret
  2258.  
  2259. sysfn_min_rest_window:
  2260.      pushad
  2261.      mov   eax, edx      ; ebx - operating
  2262.      shr   ecx, 1
  2263.      jnc    @f
  2264.      call  pid_to_slot
  2265. @@:
  2266.      or    eax, eax      ; eax - number of slot
  2267.      jz    .error
  2268.      cmp   eax, 255         ; varify maximal slot number
  2269.      ja    .error
  2270.      movzx eax, word [WIN_STACK + eax*2]
  2271.      shr   ecx, 1
  2272.      jc    .restore
  2273.  ; .minimize:
  2274.      call  minimize_window
  2275.      jmp   .exit
  2276. .restore:
  2277.      call  restore_minimized_window
  2278. .exit:
  2279.      popad
  2280.      xor   eax, eax
  2281.      mov   [esp+32], eax
  2282.      ret
  2283. .error:
  2284.      popad
  2285.      xor   eax, eax
  2286.      dec   eax
  2287.      mov   [esp+32], eax
  2288.      ret
  2289. ; } \\ Alver, 2007-22-08 \\
  2290.  
  2291. uglobal
  2292. ;// mike.dld, 2006-29-01 [
  2293. screen_workarea RECT
  2294. ;// mike.dld, 2006-29-01 ]
  2295. window_minimize db 0
  2296. sound_flag      db 0
  2297. endg
  2298.  
  2299. iglobal
  2300. version_inf:
  2301.   db 0,7,5,0  ; version 0.7.5.0
  2302.   db UID_KOLIBRI
  2303.   dd __REV__
  2304. version_end:
  2305. endg
  2306.  
  2307. UID_NONE=0
  2308. UID_MENUETOS=1   ;official
  2309. UID_KOLIBRI=2    ;russian
  2310.  
  2311. sys_cachetodiskette:
  2312.         cmp     ebx, 1
  2313.         jne     .no_floppy_a_save
  2314.         mov     [flp_number], 1
  2315.         jmp     .save_image_on_floppy
  2316. .no_floppy_a_save:
  2317.         cmp     ebx, 2
  2318.         jne     .no_floppy_b_save
  2319.         mov     [flp_number], 2
  2320. .save_image_on_floppy:
  2321.         call    save_image
  2322.         mov     [esp + 32], dword 0
  2323.         cmp     [FDC_Status], 0
  2324.         je      .yes_floppy_save
  2325. .no_floppy_b_save:
  2326.         mov     [esp + 32], dword 1
  2327. .yes_floppy_save:
  2328.         ret
  2329.  
  2330. uglobal
  2331. ;  bgrchanged  dd  0x0
  2332. align 4
  2333. bgrlockpid dd 0
  2334. bgrlock db 0
  2335. endg
  2336.  
  2337. sys_background:
  2338.  
  2339.     cmp   ebx,1                            ; BACKGROUND SIZE
  2340.     jnz   nosb1
  2341.     test  ecx,ecx
  2342. ;    cmp   ecx,0
  2343.     jz    sbgrr
  2344.     test  edx,edx
  2345. ;    cmp   edx,0
  2346.     jz    sbgrr
  2347. @@:
  2348. ;;Maxis use atomic bts for mutexes  4.4.2009
  2349.         bts     dword [bgrlock], 0
  2350.         jnc     @f
  2351.         call    change_task
  2352.         jmp     @b
  2353. @@:
  2354.     mov   [BgrDataWidth],ecx
  2355.     mov   [BgrDataHeight],edx
  2356. ;    mov   [bgrchanged],1
  2357.  
  2358.     pushad
  2359. ; return memory for old background
  2360.         mov     eax, [img_background]
  2361.         cmp     eax, static_background_data
  2362.         jz      @f
  2363.         stdcall kernel_free, eax
  2364. @@:
  2365. ; calculate RAW size
  2366.     xor  eax,eax
  2367.     inc  eax
  2368.     cmp  [BgrDataWidth],eax
  2369.     jae   @f
  2370.     mov [BgrDataWidth],eax
  2371. @@:
  2372.     cmp  [BgrDataHeight],eax
  2373.     jae   @f
  2374.     mov [BgrDataHeight],eax
  2375. @@:
  2376.     mov  eax,[BgrDataWidth]
  2377.     imul eax,[BgrDataHeight]
  2378.     lea  eax,[eax*3]
  2379.     mov  [mem_BACKGROUND],eax
  2380. ; get memory for new background
  2381.     stdcall kernel_alloc, eax
  2382.     test eax, eax
  2383.     jz .memfailed
  2384.     mov [img_background], eax
  2385.     jmp .exit
  2386. .memfailed:
  2387. ; revert to static monotone data
  2388.         mov     [img_background], static_background_data
  2389.         xor     eax, eax
  2390.         inc     eax
  2391.         mov     [BgrDataWidth], eax
  2392.         mov     [BgrDataHeight], eax
  2393.         mov     [mem_BACKGROUND], 4
  2394. .exit:
  2395.     popad
  2396.         mov     [bgrlock], 0
  2397.  
  2398.   sbgrr:
  2399.     ret
  2400.  
  2401. nosb1:
  2402.  
  2403.     cmp   ebx,2                            ; SET PIXEL
  2404.     jnz   nosb2
  2405.  
  2406.     mov   eax, [img_background]
  2407.     test  ecx, ecx
  2408.     jz    @f
  2409.     cmp   eax, static_background_data
  2410.     jz    .ret
  2411. @@:
  2412.     mov ebx, [mem_BACKGROUND]
  2413.     add ebx, 4095
  2414.     and ebx, -4096
  2415.     sub ebx, 4
  2416.     cmp   ecx, ebx
  2417.     ja   .ret
  2418.  
  2419.     mov   ebx,[eax+ecx]
  2420.     and   ebx,0xFF000000 ;255*256*256*256
  2421.     and   edx,0x00FFFFFF ;255*256*256+255*256+255
  2422.     add   edx,ebx
  2423.     mov   [eax+ecx],edx
  2424. .ret:
  2425.     ret
  2426. nosb2:
  2427.  
  2428.     cmp   ebx,3                            ; DRAW BACKGROUND
  2429.     jnz   nosb3
  2430. draw_background_temp:
  2431. ;    cmp   [bgrchanged],1 ;0
  2432. ;    je    nosb31
  2433. ;draw_background_temp:
  2434. ;    mov   [bgrchanged],1 ;0
  2435.     mov    [background_defined], 1
  2436.     call  force_redraw_background
  2437.     mov    [REDRAW_BACKGROUND], byte 2
  2438.    nosb31:
  2439.     ret
  2440.   nosb3:
  2441.  
  2442.     cmp   ebx,4                            ; TILED / STRETCHED
  2443.     jnz   nosb4
  2444.     cmp   ecx,[BgrDrawMode]
  2445.     je    nosb41
  2446.     mov   [BgrDrawMode],ecx
  2447. ;    mov   [bgrchanged],1
  2448.    nosb41:
  2449.     ret
  2450.   nosb4:
  2451.  
  2452.     cmp   ebx,5                            ; BLOCK MOVE TO BGR
  2453.     jnz   nosb5
  2454.     cmp   [img_background], static_background_data
  2455.     jnz   @f
  2456.     test  edx, edx
  2457.     jnz   .fin
  2458.     cmp   esi, 4
  2459.     ja    .fin
  2460.   @@:
  2461.   ; bughere
  2462.     mov   eax, ecx
  2463.     mov   ebx, edx
  2464.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2465.     mov   ecx, esi
  2466.     call  memmove
  2467.   .fin:
  2468.     ret
  2469.   nosb5:
  2470.  
  2471.         cmp     ebx, 6
  2472.         jnz     nosb6
  2473. ;;Maxis use atomic bts for mutex 4.4.2009
  2474. @@:
  2475.         bts     dword [bgrlock], 0
  2476.         jnc     @f
  2477.         call    change_task
  2478.         jmp     @b
  2479. @@:
  2480.         mov     eax, [CURRENT_TASK]
  2481.         mov     [bgrlockpid], eax
  2482.         cmp     [img_background], static_background_data
  2483.         jz      .nomem
  2484.         stdcall user_alloc, [mem_BACKGROUND]
  2485.         mov     [esp+32], eax
  2486.         test    eax, eax
  2487.         jz      .nomem
  2488.         mov     ebx, eax
  2489.         shr     ebx, 12
  2490.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2491.         mov     esi, [img_background]
  2492.         shr     esi, 12
  2493.         mov     ecx, [mem_BACKGROUND]
  2494.         add     ecx, 0xFFF
  2495.         shr     ecx, 12
  2496. .z:
  2497.         mov     eax, [page_tabs+ebx*4]
  2498.         test    al, 1
  2499.         jz      @f
  2500.         call    free_page
  2501. @@:
  2502.         mov     eax, [page_tabs+esi*4]
  2503.         or      al, PG_UW
  2504.         mov     [page_tabs+ebx*4], eax
  2505.         mov     eax, ebx
  2506.         shl     eax, 12
  2507.         invlpg  [eax]
  2508.         inc     ebx
  2509.         inc     esi
  2510.         loop    .z
  2511.         ret
  2512. .nomem:
  2513.         and     [bgrlockpid], 0
  2514.         mov     [bgrlock], 0
  2515. nosb6:
  2516.         cmp     ebx, 7
  2517.         jnz     nosb7
  2518.         cmp     [bgrlock], 0
  2519.         jz      .err
  2520.         mov     eax, [CURRENT_TASK]
  2521.         cmp     [bgrlockpid], eax
  2522.         jnz     .err
  2523.         mov     eax, ecx
  2524.         mov     ebx, ecx
  2525.         shr     eax, 12
  2526.         mov     ecx, [page_tabs+(eax-1)*4]
  2527.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2528.         jz      .err
  2529.         jnp     .err
  2530.         push    eax
  2531.         shr     ecx, 12
  2532.         dec     ecx
  2533. @@:
  2534.         and     dword [page_tabs+eax*4], 0
  2535.         mov     edx, eax
  2536.         shl     edx, 12
  2537.         push eax
  2538.         invlpg  [edx]
  2539.         pop eax
  2540.         inc     eax
  2541.         loop    @b
  2542.         pop     eax
  2543.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2544.         stdcall user_free, ebx
  2545.         mov     [esp+32], eax
  2546.         and     [bgrlockpid], 0
  2547.         mov     [bgrlock], 0
  2548.         ret
  2549. .err:
  2550.         and     dword [esp+32], 0
  2551.         ret
  2552.  
  2553. nosb7:
  2554.     ret
  2555.  
  2556. force_redraw_background:
  2557.     and   [draw_data+32 + RECT.left],dword 0
  2558.     and   [draw_data+32 + RECT.top],dword 0
  2559.     push  eax ebx
  2560.     mov   eax,[Screen_Max_X]
  2561.     mov   ebx,[Screen_Max_Y]
  2562.     mov   [draw_data+32 + RECT.right],eax
  2563.     mov   [draw_data+32 + RECT.bottom],ebx
  2564.     pop   ebx eax
  2565.     mov   byte [REDRAW_BACKGROUND], 1
  2566.     ret
  2567.  
  2568. align 4
  2569.  
  2570. sys_getbackground:
  2571. ;    cmp   eax,1                                  ; SIZE
  2572.     dec   ebx
  2573.     jnz   nogb1
  2574.     mov   eax,[BgrDataWidth]
  2575.     shl   eax,16
  2576.     mov   ax,[BgrDataHeight]
  2577.     mov   [esp+32],eax
  2578.     ret
  2579.  
  2580. nogb1:
  2581. ;    cmp   eax,2                                  ; PIXEL
  2582.     dec   ebx
  2583.     jnz   nogb2
  2584.  
  2585.         mov     eax, [img_background]
  2586.         test    ecx, ecx
  2587.         jz      @f
  2588.         cmp     eax, static_background_data
  2589.         jz      .ret
  2590. @@:
  2591.     mov ebx, [mem_BACKGROUND]
  2592.     add ebx, 4095
  2593.     and ebx, -4096
  2594.     sub ebx, 4
  2595.     cmp ecx, ebx
  2596.     ja  .ret
  2597.  
  2598.     mov   eax,[ecx+eax]
  2599.  
  2600.     and   eax, 0xFFFFFF
  2601.     mov   [esp+32],eax
  2602. .ret:
  2603.     ret
  2604.   nogb2:
  2605.  
  2606. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2607.     dec   ebx
  2608.     dec   ebx
  2609.     jnz   nogb4
  2610.     mov   eax,[BgrDrawMode]
  2611.   nogb4:
  2612.     mov   [esp+32],eax
  2613.     ret
  2614.  
  2615. align 4
  2616.  
  2617. sys_getkey:
  2618.         mov     [esp + 32],dword 1
  2619.         ; test main buffer
  2620.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2621.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2622.         mov     edx, [TASK_COUNT]
  2623.         cmp     ecx, edx
  2624.         jne     .finish
  2625.         cmp     [KEY_COUNT], byte 0
  2626.         je      .finish
  2627.         movzx   eax, byte [KEY_BUFF]
  2628.         shl     eax, 8
  2629.         push    eax
  2630.         dec     byte [KEY_COUNT]
  2631.         and     byte [KEY_COUNT], 127
  2632.         movzx   ecx, byte [KEY_COUNT]
  2633.         add     ecx, 2
  2634.         mov     eax, KEY_BUFF + 1
  2635.         mov     ebx, KEY_BUFF
  2636.         call    memmove
  2637.         pop     eax
  2638. .ret_eax:
  2639.         mov     [esp + 32], eax
  2640.         ret
  2641. .finish:
  2642. ; test hotkeys buffer
  2643.         mov     ecx, hotkey_buffer
  2644. @@:
  2645.         cmp     [ecx], ebx
  2646.         jz      .found
  2647.         add     ecx, 8
  2648.         cmp     ecx, hotkey_buffer + 120 * 8
  2649.         jb      @b
  2650.         ret
  2651. .found:
  2652.         mov     ax, [ecx + 6]
  2653.         shl     eax, 16
  2654.         mov     ah, [ecx + 4]
  2655.         mov     al, 2
  2656.         and     dword [ecx + 4], 0
  2657.         and     dword [ecx], 0
  2658.         jmp     .ret_eax
  2659.  
  2660. align 4
  2661.  
  2662. sys_getbutton:
  2663.  
  2664.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2665.         mov     [esp + 32], dword 1
  2666.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2667.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2668.         cmp     ecx, edx
  2669.         jne     .exit
  2670.         movzx   eax, byte [BTN_COUNT]
  2671.         test    eax, eax
  2672.         jz      .exit
  2673.         mov     eax, [BTN_BUFF]
  2674.         shl     eax, 8
  2675. ; // Alver 22.06.2008 // {
  2676.         mov       al, byte [btn_down_determ]
  2677.         and       al,0xFE                                       ; delete left button bit
  2678. ; } \\ Alver \\
  2679.         mov     [BTN_COUNT], byte 0
  2680.         mov     [esp + 32], eax
  2681. .exit:
  2682.         ret
  2683.  
  2684.  
  2685. align 4
  2686.  
  2687. sys_cpuusage:
  2688.  
  2689. ;  RETURN:
  2690. ;
  2691. ;  +00 dword     process cpu usage
  2692. ;  +04  word     position in windowing stack
  2693. ;  +06  word     windowing stack value at current position (cpu nro)
  2694. ;  +10 12 bytes  name
  2695. ;  +22 dword     start in mem
  2696. ;  +26 dword     used mem
  2697. ;  +30 dword     PID , process idenfification number
  2698. ;
  2699.  
  2700.     cmp  ecx,-1         ; who am I ?
  2701.     jne  .no_who_am_i
  2702.     mov  ecx,[CURRENT_TASK]
  2703.   .no_who_am_i:
  2704.         cmp     ecx, max_processes
  2705.         ja      .nofillbuf
  2706.  
  2707. ; +4: word: position of the window of thread in the window stack
  2708.         mov     ax, [WIN_STACK + ecx * 2]
  2709.         mov     [ebx+4], ax
  2710. ; +6: word: number of the thread slot, which window has in the window stack
  2711. ;           position ecx (has no relation to the specific thread)
  2712.         mov     ax, [WIN_POS + ecx * 2]
  2713.         mov     [ebx+6], ax
  2714.  
  2715.         shl     ecx, 5
  2716.  
  2717. ; +0: dword: memory usage
  2718.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2719.         mov     [ebx], eax
  2720. ; +10: 11 bytes: name of the process
  2721.         push    ecx
  2722.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2723.         add     ebx, 10
  2724.         mov     ecx, 11
  2725.         call    memmove
  2726.         pop     ecx
  2727.  
  2728. ; +22: address of the process in memory
  2729. ; +26: size of used memory - 1
  2730.         push    edi
  2731.         lea     edi, [ebx+12]
  2732.         xor     eax, eax
  2733.         mov     edx, 0x100000*16
  2734.         cmp     ecx, 1 shl 5
  2735.         je      .os_mem
  2736.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2737.         mov     eax, std_application_base_address
  2738. .os_mem:
  2739.         stosd
  2740.         lea     eax, [edx-1]
  2741.         stosd
  2742.  
  2743. ; +30: PID/TID
  2744.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2745.         stosd
  2746.  
  2747.     ; window position and size
  2748.         push    esi
  2749.         lea     esi, [ecx + window_data + WDATA.box]
  2750.         movsd
  2751.         movsd
  2752.         movsd
  2753.         movsd
  2754.  
  2755.     ; Process state (+50)
  2756.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2757.         stosd
  2758.  
  2759.     ; Window client area box
  2760.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2761.         movsd
  2762.         movsd
  2763.         movsd
  2764.         movsd
  2765.  
  2766.     ; Window state
  2767.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2768.         stosb
  2769.  
  2770.         pop     esi
  2771.         pop     edi
  2772.  
  2773. .nofillbuf:
  2774.     ; return number of processes
  2775.  
  2776.     mov    eax,[TASK_COUNT]
  2777.     mov    [esp+32],eax
  2778.     ret
  2779.  
  2780. align 4
  2781. sys_clock:
  2782.         cli
  2783.   ; Mikhail Lisovin  xx Jan 2005
  2784.   @@:   mov   al, 10
  2785.         out   0x70, al
  2786.         in    al, 0x71
  2787.         test  al, al
  2788.         jns   @f
  2789.         mov   esi, 1
  2790.         call  delay_ms
  2791.         jmp   @b
  2792.   @@:
  2793.   ; end Lisovin's fix
  2794.  
  2795.         xor   al,al           ; seconds
  2796.         out   0x70,al
  2797.         in    al,0x71
  2798.         movzx ecx,al
  2799.         mov   al,02           ; minutes
  2800.         shl   ecx,16
  2801.         out   0x70,al
  2802.         in    al,0x71
  2803.         movzx edx,al
  2804.         mov   al,04           ; hours
  2805.         shl   edx,8
  2806.         out   0x70,al
  2807.         in    al,0x71
  2808.         add   ecx,edx
  2809.         movzx edx,al
  2810.         add   ecx,edx
  2811.         sti
  2812.         mov     [esp + 32], ecx
  2813.         ret
  2814.  
  2815.  
  2816. align 4
  2817.  
  2818. sys_date:
  2819.  
  2820.         cli
  2821.   @@:   mov   al, 10
  2822.         out   0x70, al
  2823.         in    al, 0x71
  2824.         test  al, al
  2825.         jns   @f
  2826.         mov   esi, 1
  2827.         call  delay_ms
  2828.         jmp   @b
  2829.   @@:
  2830.  
  2831.         mov     ch,0
  2832.         mov     al,7            ; date
  2833.         out     0x70,al
  2834.         in      al,0x71
  2835.         mov     cl,al
  2836.         mov     al,8            ; month
  2837.         shl     ecx,16
  2838.         out     0x70,al
  2839.         in      al,0x71
  2840.         mov     ch,al
  2841.         mov     al,9            ; year
  2842.         out     0x70,al
  2843.         in      al,0x71
  2844.         mov     cl,al
  2845.         sti
  2846.         mov     [esp+32], ecx
  2847.         ret
  2848.  
  2849.  
  2850. ; redraw status
  2851.  
  2852. sys_redrawstat:
  2853.         cmp     ebx, 1
  2854.         jne     no_widgets_away
  2855.         ; buttons away
  2856.         mov     ecx,[CURRENT_TASK]
  2857.   sys_newba2:
  2858.         mov     edi,[BTN_ADDR]
  2859.         cmp     [edi], dword 0  ; empty button list ?
  2860.         je      end_of_buttons_away
  2861.         movzx   ebx, word [edi]
  2862.         inc     ebx
  2863.         mov     eax,edi
  2864.   sys_newba:
  2865.         dec     ebx
  2866.         jz      end_of_buttons_away
  2867.  
  2868.         add     eax, 0x10
  2869.         cmp     cx, [eax]
  2870.         jnz     sys_newba
  2871.  
  2872.         push    eax ebx ecx
  2873.         mov     ecx,ebx
  2874.         inc     ecx
  2875.         shl     ecx, 4
  2876.         mov     ebx, eax
  2877.         add     eax, 0x10
  2878.         call    memmove
  2879.         dec     dword [edi]
  2880.         pop     ecx ebx eax
  2881.  
  2882.         jmp     sys_newba2
  2883.  
  2884.   end_of_buttons_away:
  2885.  
  2886.         ret
  2887.  
  2888.   no_widgets_away:
  2889.  
  2890.         cmp     ebx, 2
  2891.         jnz     srl1
  2892.  
  2893.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2894.         add     edx, draw_data - CURRENT_TASK
  2895.         mov     [edx + RECT.left], 0
  2896.         mov     [edx + RECT.top], 0
  2897.         mov     eax, [Screen_Max_X]
  2898.         mov     [edx + RECT.right], eax
  2899.         mov     eax, [Screen_Max_Y]
  2900.         mov     [edx + RECT.bottom], eax
  2901.  
  2902.         mov     edi, [TASK_BASE]
  2903.         or      [edi - twdw + WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2904.         call    sys_window_mouse
  2905.         ret
  2906.  
  2907.   srl1:
  2908.         ret
  2909.  
  2910.  
  2911. sys_drawwindow:
  2912.  
  2913.     mov   eax,edx
  2914.     shr   eax,16+8
  2915.     and   eax,15
  2916.  
  2917. ;    cmp   eax,0   ; type I    - original style
  2918.     jne   nosyswI
  2919.     inc   [mouse_pause]
  2920.     call  [_display.disable_mouse]
  2921.     call  sys_set_window
  2922.     call  [_display.disable_mouse]
  2923.     call  drawwindow_I
  2924.     ;dec   [mouse_pause]
  2925.     ;call   [draw_pointer]
  2926.     ;ret
  2927.     jmp   draw_window_caption.2
  2928.   nosyswI:
  2929.  
  2930.     cmp   al,1    ; type II   - only reserve area, no draw
  2931.     jne   nosyswII
  2932.     inc   [mouse_pause]
  2933.     call  [_display.disable_mouse]
  2934.     call  sys_set_window
  2935.     call  [_display.disable_mouse]
  2936.     call  sys_window_mouse
  2937.     dec   [mouse_pause]
  2938.     call   [draw_pointer]
  2939.     ret
  2940.   nosyswII:
  2941.  
  2942.     cmp   al,2    ; type III  - new style
  2943.     jne   nosyswIII
  2944.     inc   [mouse_pause]
  2945.     call  [_display.disable_mouse]
  2946.     call  sys_set_window
  2947.     call  [_display.disable_mouse]
  2948.     call  drawwindow_III
  2949.     ;dec   [mouse_pause]
  2950.     ;call   [draw_pointer]
  2951.     ;ret
  2952.     jmp   draw_window_caption.2
  2953.   nosyswIII:
  2954.  
  2955.     cmp   al,3    ; type IV - skinned window
  2956.     je    draw_skin_window
  2957.     cmp   al,4    ; type V - skinned window not sized! {not_sized_skin_window}
  2958.     jne   nosyswV
  2959.   draw_skin_window:
  2960.  
  2961.     inc   [mouse_pause]
  2962.     call  [_display.disable_mouse]
  2963.     call  sys_set_window
  2964.     call  [_display.disable_mouse]
  2965.     mov   eax, [TASK_COUNT]
  2966.     movzx eax, word [WIN_POS + eax*2]
  2967.     cmp   eax, [CURRENT_TASK]
  2968.     setz  al
  2969.     movzx eax, al
  2970.     push  eax
  2971.     call  drawwindow_IV
  2972.     ;dec   [mouse_pause]
  2973.     ;call   [draw_pointer]
  2974.     ;ret
  2975.     jmp   draw_window_caption.2
  2976.   nosyswV:
  2977.  
  2978.     ret
  2979.  
  2980.  
  2981. draw_window_caption:
  2982.         inc     [mouse_pause]
  2983.         call    [_display.disable_mouse]
  2984.  
  2985.         xor     eax,eax
  2986.         mov     edx,[TASK_COUNT]
  2987.         movzx   edx,word[WIN_POS+edx*2]
  2988.         cmp     edx,[CURRENT_TASK]
  2989.         jne     @f
  2990.         inc     eax
  2991.     @@: mov     edx,[CURRENT_TASK]
  2992.         shl     edx,5
  2993.         add     edx,window_data
  2994.         movzx   ebx,[edx+WDATA.fl_wstyle]
  2995.         and     bl,0x0F
  2996.         cmp     bl,3
  2997.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  2998.         cmp     bl,4
  2999.         je      .draw_caption_style_3
  3000.  
  3001.         jmp     .not_style_3
  3002.   .draw_caption_style_3:
  3003.  
  3004.         push    edx
  3005.         call    drawwindow_IV_caption
  3006.         add     esp,4
  3007.         jmp     .2
  3008.  
  3009.   .not_style_3:
  3010.         cmp     bl,2
  3011.         jne     .not_style_2
  3012.  
  3013.         call    drawwindow_III_caption
  3014.         jmp     .2
  3015.  
  3016.   .not_style_2:
  3017.         cmp     bl,0
  3018.         jne     .2
  3019.  
  3020.         call    drawwindow_I_caption
  3021.  
  3022. ;--------------------------------------------------------------
  3023.   .2:   ;jmp     @f
  3024.         mov     edi,[CURRENT_TASK]
  3025.         shl     edi,5
  3026.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3027.         jz      @f
  3028.         mov     edx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  3029.         or      edx,edx
  3030.         jz      @f
  3031.  
  3032.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  3033.         and     al,0x0F
  3034.         cmp     al,3
  3035.         je      .skinned
  3036.         cmp     al,4
  3037.         je      .skinned
  3038.  
  3039.         jmp     .not_skinned
  3040.   .skinned:
  3041.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3042.         mov     bp,word[edi+window_data+WDATA.box.top]
  3043.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3044.         sub     ax,[_skinmargins.left]
  3045.         sub     ax,[_skinmargins.right]
  3046.         push    edx
  3047.         cwde
  3048.         cdq
  3049.         mov     ebx,6
  3050.         idiv    ebx
  3051.         pop     edx
  3052.         or      eax,eax
  3053.         js      @f
  3054.         mov     esi,eax
  3055.         mov     ebx,dword[_skinmargins.left-2]
  3056.         mov     bx,word[_skinh]
  3057.         sub     bx,[_skinmargins.bottom]
  3058.         sub     bx,[_skinmargins.top]
  3059.         sar     bx,1
  3060.         adc     bx,0
  3061.         add     bx,[_skinmargins.top]
  3062.         add     bx,-3
  3063.         add     ebx,ebp
  3064.         jmp     .dodraw
  3065.  
  3066.   .not_skinned:
  3067.         cmp     al,1
  3068.         je      @f
  3069.  
  3070.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3071.         mov     bp,word[edi+window_data+WDATA.box.top]
  3072.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3073.         sub     eax,16
  3074.         push    edx
  3075.         cwde
  3076.         cdq
  3077.         mov     ebx,6
  3078.         idiv    ebx
  3079.         pop     edx
  3080.         or      eax,eax
  3081.         js      @f
  3082.         mov     esi,eax
  3083.         mov     ebx,0x00080007
  3084.         add     ebx,ebp
  3085. .dodraw:
  3086.         mov     ecx,[common_colours+16];0x00FFFFFF
  3087.         or      ecx, 0x80000000
  3088.         xor     edi,edi
  3089. ; // Alver 22.06.2008 // {
  3090. ;       call    dtext
  3091.         call dtext_asciiz_esi
  3092. ; } \\ Alver \\
  3093.  
  3094.     @@:
  3095. ;--------------------------------------------------------------
  3096.         dec     [mouse_pause]
  3097.         call    [draw_pointer]
  3098.         ret
  3099.  
  3100. iglobal
  3101. align 4
  3102. window_topleft dd \
  3103.   1, 21,\               ;type 0
  3104.   0,  0,\       ;type 1
  3105.   5, 20,\       ;type 2
  3106.   5,  ?,\       ;type 3 {set by skin}
  3107.   5,  ?         ;type 4 {set by skin}
  3108. endg
  3109.  
  3110. set_window_clientbox:
  3111.         push    eax ecx edi
  3112.  
  3113.         mov     eax,[_skinh]
  3114.         mov     [window_topleft+4*7],eax
  3115.         mov     [window_topleft+4*9],eax
  3116.  
  3117.         mov     ecx,edi
  3118.         sub     edi,window_data
  3119.         shl     edi,3
  3120.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3121.         jz      @f
  3122.  
  3123.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3124.         and     eax,0x0F
  3125.         mov     eax,[eax*8+window_topleft+0]
  3126.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3127.         shl     eax,1
  3128.         neg     eax
  3129.         add     eax,[ecx+WDATA.box.width]
  3130.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3131.  
  3132.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3133.         and     eax,0x0F
  3134.         push    [eax*8+window_topleft+0]
  3135.         mov     eax,[eax*8+window_topleft+4]
  3136.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3137.         neg     eax
  3138.         sub     eax,[esp]
  3139.         add     eax,[ecx+WDATA.box.height]
  3140.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3141.         add     esp,4
  3142.  
  3143.         pop     edi ecx eax
  3144.         ret
  3145.     @@:
  3146.         xor     eax,eax
  3147.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3148.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3149.         mov     eax,[ecx+WDATA.box.width]
  3150.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3151.         mov     eax,[ecx+WDATA.box.height]
  3152.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3153.  
  3154.         pop     edi ecx eax
  3155.         ret
  3156.  
  3157. sys_set_window:
  3158.  
  3159.     mov   eax,[CURRENT_TASK]
  3160.     shl   eax,5
  3161.     add   eax,window_data
  3162.  
  3163.     ; colors
  3164.     mov   [eax+WDATA.cl_workarea],edx
  3165.     mov   [eax+WDATA.cl_titlebar],esi
  3166.     mov   [eax+WDATA.cl_frames],edi
  3167.  
  3168.     mov   edi, eax
  3169.  
  3170.     ; check flag (?)
  3171.     test  [edi+WDATA.fl_wdrawn],1
  3172.     jnz   newd
  3173.  
  3174.     mov   eax,[timer_ticks] ;[0xfdf0]
  3175.     add   eax,100
  3176.     mov   [new_window_starting],eax
  3177.  
  3178.     movsx eax,bx
  3179.     mov   [edi+WDATA.box.width],eax
  3180.     movsx eax,cx
  3181.     mov   [edi+WDATA.box.height],eax
  3182.     sar   ebx,16
  3183.     sar   ecx,16
  3184.     mov   [edi+WDATA.box.left],ebx
  3185.     mov   [edi+WDATA.box.top],ecx
  3186.  
  3187.     call  check_window_position
  3188.  
  3189.     push  ecx esi edi               ; save for window fullscreen/resize
  3190.     ;mov   esi,edi
  3191.  
  3192.         mov     cl, [edi+WDATA.fl_wstyle]
  3193.         mov     eax, [edi+WDATA.cl_frames]
  3194.  
  3195.     sub   edi,window_data
  3196.     shl   edi,3
  3197.     add   edi,SLOT_BASE
  3198.  
  3199.         and     cl,0x0F
  3200.         mov     [edi+APPDATA.wnd_caption],0
  3201.         cmp     cl,3
  3202.         je      set_APPDATA_wnd_caption
  3203.         cmp     cl,4                                                            ; {SPraid.simba}
  3204.         je      set_APPDATA_wnd_caption
  3205.  
  3206.         jmp     @f
  3207.     set_APPDATA_wnd_caption:
  3208.         mov     [edi+APPDATA.wnd_caption],eax
  3209.     @@: mov     esi,[esp+0]
  3210.  
  3211.     add   edi, APPDATA.saved_box
  3212.         movsd
  3213.         movsd
  3214.         movsd
  3215.         movsd
  3216.     pop   edi esi ecx
  3217.  
  3218.         mov     esi, [CURRENT_TASK]
  3219.         movzx   esi, word [WIN_STACK+esi*2]
  3220.         lea     esi, [WIN_POS+esi*2]
  3221.         call    waredraw
  3222.  
  3223. ;;;    mov   ebx, 1
  3224. ;;;    call  delay_hs
  3225.     mov   eax, [edi+WDATA.box.left]
  3226.     mov   ebx, [edi+WDATA.box.top]
  3227.     mov   ecx, [edi+WDATA.box.width]
  3228.     mov   edx, [edi+WDATA.box.height]
  3229.     add   ecx, eax
  3230.     add   edx, ebx
  3231.     call  calculatescreen
  3232.  
  3233.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3234.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3235.  
  3236.   newd:
  3237.     call  set_window_clientbox
  3238.  
  3239.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3240.     mov   edx,edi
  3241.  
  3242.     ret
  3243.  
  3244. syscall_windowsettings:
  3245.  
  3246.   .set_window_caption:
  3247.         dec     eax     ; subfunction #1 - set window caption
  3248.         jnz     .get_window_caption
  3249.  
  3250.         ; NOTE: only window owner thread can set its caption,
  3251.         ;       so there's no parameter for PID/TID
  3252.  
  3253.         mov     edi,[CURRENT_TASK]
  3254.         shl     edi,5
  3255.  
  3256.         ; have to check if caption is within application memory limit
  3257.         ; check is trivial, and if application resizes its memory,
  3258.         ;   caption still can become over bounds
  3259. ; diamond, 31.10.2006: check removed because with new memory manager
  3260. ; there can be valid data after APPDATA.mem_size bound
  3261. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3262. ;        add     ecx,255 ; max caption length
  3263. ;        cmp     ebx,ecx
  3264. ;        ja      .exit_fail
  3265.  
  3266.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3267.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3268.  
  3269.         call    draw_window_caption
  3270.  
  3271.         xor     eax,eax ; eax = 0 (success)
  3272.         ret
  3273.  
  3274.   .get_window_caption:
  3275.         dec     eax     ; subfunction #2 - get window caption
  3276.         jnz     .exit_fail
  3277.  
  3278.         ; not implemented yet
  3279.  
  3280.   .exit_fail:
  3281.         xor     eax,eax
  3282.         inc     eax     ; eax = 1 (fail)
  3283.         ret
  3284.  
  3285.  
  3286. sys_window_move:
  3287.  
  3288.         mov     edi,[CURRENT_TASK]
  3289.         shl     edi,5
  3290.         add     edi,window_data
  3291.  
  3292.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3293.         jnz     .window_move_return
  3294.  
  3295.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3296.         push    dword [edi + WDATA.box.top]
  3297.         push    dword [edi + WDATA.box.width]
  3298.         push    dword [edi + WDATA.box.height]
  3299.  
  3300.         cmp   eax,-1                  ; set new position and size
  3301.         je    .no_x_reposition
  3302.         mov     [edi + WDATA.box.left], eax
  3303.       .no_x_reposition:
  3304.         cmp   ebx,-1
  3305.         je    .no_y_reposition
  3306.         mov     [edi + WDATA.box.top], ebx
  3307.       .no_y_reposition:
  3308.  
  3309.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3310.         jnz     .no_y_resizing
  3311.  
  3312.         cmp   ecx,-1
  3313.         je    .no_x_resizing
  3314.         mov     [edi + WDATA.box.width], ecx
  3315.       .no_x_resizing:
  3316.         cmp   edx,-1
  3317.         je    .no_y_resizing
  3318.         mov     [edi + WDATA.box.height], edx
  3319.       .no_y_resizing:
  3320.  
  3321.         call  check_window_position
  3322.         call  set_window_clientbox
  3323.  
  3324.         pushad                       ; save for window fullscreen/resize
  3325.         mov   esi,edi
  3326.         sub   edi,window_data
  3327.         shr   edi,5
  3328.         shl   edi,8
  3329.         add   edi, SLOT_BASE + APPDATA.saved_box
  3330.         mov   ecx,4
  3331.         cld
  3332.         rep   movsd
  3333.         popad
  3334.  
  3335.         pushad                       ; calculcate screen at new position
  3336.         mov   eax, [edi + WDATA.box.left]
  3337.         mov   ebx, [edi + WDATA.box.top]
  3338.         mov   ecx, [edi + WDATA.box.width]
  3339.         mov   edx, [edi + WDATA.box.height]
  3340.         add   ecx,eax
  3341.         add   edx,ebx
  3342.  
  3343.         call  calculatescreen
  3344.         popad
  3345.  
  3346.         pop   edx                   ; calculcate screen at old position
  3347.         pop   ecx
  3348.         pop   ebx
  3349.         pop   eax
  3350.         add   ecx,eax
  3351.         add   edx,ebx
  3352.         mov   [dlx],eax             ; save for drawlimits
  3353.         mov   [dly],ebx
  3354.         mov   [dlxe],ecx
  3355.         mov   [dlye],edx
  3356.         call  calculatescreen
  3357.  
  3358.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3359.  
  3360.         mov   eax,edi               ; redraw screen at old position
  3361.         xor   esi,esi
  3362.         call  redrawscreen
  3363.  
  3364.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3365.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3366.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3367.  
  3368.         call  [draw_pointer]
  3369.  
  3370.         mov   [window_move_pr],0
  3371.  
  3372.       .window_move_return:
  3373.  
  3374.         ret
  3375.  
  3376. uglobal
  3377.   window_move_pr   dd  0x0
  3378.   window_move_eax  dd  0x0
  3379.   window_move_ebx  dd  0x0
  3380.   window_move_ecx  dd  0x0
  3381.   window_move_edx  dd  0x0
  3382. endg
  3383.  
  3384. ;ok - 100% work
  3385. ;nt - not tested
  3386. ;---------------------------------------------------------------------------------------------
  3387. ;eax
  3388. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3389. ;1 - change task. Ret nothing. Block. ok.
  3390. ;2 - performance control
  3391. ; ebx
  3392. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3393. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3394. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3395. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3396. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3397. ;eax
  3398. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3399. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3400. ;---------------------------------------------------------------------------------------------
  3401. sys_sheduler:  
  3402. ; old sys_sheduler ;noname & halyavin
  3403. ;    cmp eax,0
  3404. ;    je shed_counter
  3405. ;    cmp eax,2
  3406. ;    je perf_control
  3407. ;    cmp eax,3
  3408. ;    je rdmsr_instr
  3409. ;    cmp eax,4
  3410. ;    je wrmsr_instr
  3411. ;    cmp eax,1
  3412. ;    jne not_supported
  3413. ;    call change_task ;delay,0
  3414.  
  3415. ;rewritten by <Lrz>  15.11.2009
  3416.      test       eax,eax        
  3417.      jz         .shed_counter    ;eax=0
  3418.      dec        eax            
  3419.      jz         change_task      ;eax=1
  3420.      dec        eax
  3421.      jz         .perf_control    ;eax=2
  3422.      dec        eax
  3423.      jz         .rdmsr_instr     ;eax=3
  3424.      dec        eax
  3425.      jnz        @f
  3426. ;wrmsr_instr     ;eax=4
  3427. ;.wrmsr_instr:
  3428. ;now counter in ecx
  3429. ;(edx:eax) esi:edi => edx:esi
  3430.         ; Fast Call MSR can't be destroy
  3431.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  3432.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  3433.         cmp     ecx, MSR_SYSENTER_CS
  3434.         je      @f
  3435.         cmp     ecx, MSR_SYSENTER_ESP
  3436.         je      @f
  3437.         cmp     ecx, MSR_SYSENTER_EIP
  3438.         je      @f
  3439.         cmp     ecx, MSR_AMD_STAR
  3440.         je      @f
  3441.  
  3442.         mov     eax, esi
  3443.         wrmsr
  3444.         ; mov   [esp + 36], eax
  3445.         ; mov   [esp + 24], edx ;ret in ebx?
  3446. @@:
  3447. ret
  3448.  
  3449. .shed_counter:
  3450.     mov eax,[context_counter]
  3451.     mov [esp+36],eax
  3452. ret
  3453. .perf_control:
  3454. ;    inc eax ;now eax=3
  3455.     add eax,3           ;restore eax=3
  3456.     cmp ebx,eax
  3457.     je cache_disable
  3458.     dec eax
  3459.     cmp ebx,eax
  3460.     je cache_enable
  3461.     dec eax
  3462.     cmp ebx,eax
  3463.     je is_cache_enabled
  3464.     dec eax
  3465.     cmp ebx,eax
  3466.     je modify_pce
  3467. ret
  3468.  
  3469. .rdmsr_instr:
  3470. ;now counter in ecx
  3471. ;(edx:eax) esi:edi => edx:esi
  3472. mov eax,esi
  3473. rdmsr
  3474. mov [esp+36],eax
  3475. mov [esp+24],edx ;ret in ebx?
  3476. ret
  3477.  
  3478. cache_disable:
  3479.        mov eax,cr0
  3480.        or  eax,01100000000000000000000000000000b
  3481.        mov cr0,eax
  3482.        wbinvd ;set MESI
  3483. ret
  3484.  
  3485. cache_enable:
  3486.        mov eax,cr0
  3487.        and eax,10011111111111111111111111111111b
  3488.        mov cr0,eax
  3489. ret
  3490.  
  3491. is_cache_enabled:
  3492.        mov eax,cr0
  3493.        mov ebx,eax
  3494.        and eax,01100000000000000000000000000000b
  3495.        jz cache_disabled
  3496.        mov [esp+36],ebx
  3497. cache_disabled:
  3498.        mov dword [esp+36],eax ;0
  3499. ret
  3500.  
  3501. modify_pce:
  3502.        mov eax,cr4
  3503. ;       mov ebx,0
  3504. ;       or  bx,100000000b ;pce
  3505. ;       xor eax,ebx ;invert pce
  3506.        bts eax,8 ;pce=cr4[8]
  3507.        mov cr4,eax
  3508.        mov [esp+36],eax
  3509. ret
  3510. ;---------------------------------------------------------------------------------------------
  3511.  
  3512.  
  3513. ; check if pixel is allowed to be drawn
  3514.  
  3515. checkpixel:
  3516.         push eax edx
  3517.  
  3518.         mov  edx,[Screen_Max_X]     ; screen x size
  3519.         inc  edx
  3520.         imul edx, ebx
  3521.         add  eax, [_WinMapAddress]
  3522.         mov  dl, [eax+edx] ; lea eax, [...]
  3523.  
  3524.         xor  ecx, ecx
  3525.         mov  eax, [CURRENT_TASK]
  3526.         cmp  al, dl
  3527.         setne cl
  3528.  
  3529.         pop  edx eax
  3530.         ret
  3531.  
  3532. iglobal
  3533.   cpustring db 'CPU',0
  3534. endg
  3535.  
  3536. uglobal
  3537. background_defined    db    0    ; diamond, 11.04.2006
  3538. endg
  3539.  
  3540. align 4
  3541. ; check misc
  3542.  
  3543. checkmisc:
  3544.  
  3545.     cmp   [ctrl_alt_del], 1
  3546.     jne   nocpustart
  3547.  
  3548.         mov     ebp, cpustring
  3549.         call    fs_execute_from_sysdir
  3550.  
  3551.     mov   [ctrl_alt_del], 0
  3552.  
  3553. nocpustart:
  3554.     cmp   [mouse_active], 1
  3555.     jne   mouse_not_active
  3556.     mov   [mouse_active], 0
  3557.     xor   edi, edi
  3558.     mov   ecx,  [TASK_COUNT]
  3559. set_mouse_event:
  3560.     add   edi, 256
  3561.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3562.     loop  set_mouse_event
  3563.  
  3564. mouse_not_active:
  3565.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3566.     jz    nobackgr
  3567.     cmp    [background_defined], 0
  3568.     jz    nobackgr
  3569.     cmp   [REDRAW_BACKGROUND], byte 2
  3570.     jnz   no_set_bgr_event
  3571.     xor   edi, edi
  3572.     mov   ecx,  [TASK_COUNT]
  3573. set_bgr_event:
  3574.     add   edi, 256
  3575.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3576.     loop  set_bgr_event
  3577. no_set_bgr_event:
  3578. ;    mov   [draw_data+32 + RECT.left],dword 0
  3579. ;    mov   [draw_data+32 + RECT.top],dword 0
  3580. ;    mov   eax,[Screen_Max_X]
  3581. ;    mov   ebx,[Screen_Max_Y]
  3582. ;    mov   [draw_data+32 + RECT.right],eax
  3583. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3584.     call  drawbackground
  3585.     mov   [REDRAW_BACKGROUND],byte 0
  3586.     mov   [MOUSE_BACKGROUND],byte 0
  3587.  
  3588. nobackgr:
  3589.  
  3590.     ; system shutdown request
  3591.  
  3592.     cmp  [SYS_SHUTDOWN],byte 0
  3593.     je   noshutdown
  3594.  
  3595.     mov  edx,[shutdown_processes]
  3596.  
  3597.     cmp  [SYS_SHUTDOWN],dl
  3598.     jne  no_mark_system_shutdown
  3599.  
  3600.     lea   ecx,[edx-1]
  3601.     mov   edx,OS_BASE+0x3040
  3602.     jecxz @f
  3603. markz:
  3604.     mov   [edx+TASKDATA.state],byte 3
  3605.     add   edx,0x20
  3606.     loop  markz
  3607. @@:
  3608.  
  3609.   no_mark_system_shutdown:
  3610.  
  3611.     call [_display.disable_mouse]
  3612.  
  3613.     dec  byte [SYS_SHUTDOWN]
  3614.     je   system_shutdown
  3615.  
  3616. noshutdown:
  3617.  
  3618.  
  3619.     mov   eax,[TASK_COUNT]                  ; termination
  3620.     mov   ebx,TASK_DATA+TASKDATA.state
  3621.     mov   esi,1
  3622.  
  3623. newct:
  3624.     mov   cl,[ebx]
  3625.     cmp   cl,byte 3
  3626.     jz    terminate
  3627.     cmp   cl,byte 4
  3628.     jz    terminate
  3629.  
  3630.     add   ebx,0x20
  3631.     inc   esi
  3632.     dec   eax
  3633.     jnz   newct
  3634.     ret
  3635.  
  3636. ; redraw screen
  3637.  
  3638. redrawscreen:
  3639.  
  3640. ; eax , if process window_data base is eax, do not set flag/limits
  3641.  
  3642.          pushad
  3643.          push  eax
  3644.  
  3645. ;;;         mov   ebx,2
  3646. ;;;         call  delay_hs
  3647.  
  3648.          ;mov   ecx,0               ; redraw flags for apps
  3649.          xor   ecx,ecx
  3650.        newdw2:
  3651.  
  3652.          inc   ecx
  3653.          push  ecx
  3654.  
  3655.          mov   eax,ecx
  3656.          shl   eax,5
  3657.          add   eax,window_data
  3658.  
  3659.          cmp   eax,[esp+4]
  3660.          je    not_this_task
  3661.                                    ; check if window in redraw area
  3662.          mov   edi,eax
  3663.  
  3664.          cmp   ecx,1               ; limit for background
  3665.          jz    bgli
  3666.  
  3667.          mov   eax, [edi + WDATA.box.left]
  3668.          mov   ebx, [edi + WDATA.box.top]
  3669.          mov   ecx, [edi + WDATA.box.width]
  3670.          mov   edx, [edi + WDATA.box.height]
  3671.          add   ecx,eax
  3672.          add   edx,ebx
  3673.  
  3674.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3675.          cmp   ecx,ebx
  3676.          jb    ricino
  3677.  
  3678.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3679.          cmp   ecx,eax
  3680.          jb    ricino
  3681.  
  3682.          mov   eax, [edi + WDATA.box.left]
  3683.          mov   ebx, [edi + WDATA.box.top]
  3684.          mov   ecx, [edi + WDATA.box.width]
  3685.          mov   edx, [edi + WDATA.box.height]
  3686.          add   ecx, eax
  3687.          add   edx, ebx
  3688.  
  3689.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3690.          cmp   edx,eax
  3691.          jb    ricino
  3692.  
  3693.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3694.          cmp   ecx,eax
  3695.          jb    ricino
  3696.  
  3697.         bgli:
  3698.  
  3699.          cmp   ecx,1
  3700.          jnz   .az
  3701.          mov   al,[REDRAW_BACKGROUND]
  3702.          cmp   al,2
  3703.          jz    newdw8
  3704.          test  al,al
  3705.          jz    .az
  3706.          lea   eax,[edi+draw_data-window_data]
  3707.          mov   ebx,[dlx]
  3708.          cmp   ebx,[eax+RECT.left]
  3709.          jae   @f
  3710.          mov   [eax+RECT.left],ebx
  3711.         @@:
  3712.          mov   ebx,[dly]
  3713.          cmp   ebx,[eax+RECT.top]
  3714.          jae   @f
  3715.          mov   [eax+RECT.top],ebx
  3716.         @@:
  3717.          mov   ebx,[dlxe]
  3718.          cmp   ebx,[eax+RECT.right]
  3719.          jbe   @f
  3720.          mov   [eax+RECT.right],ebx
  3721.         @@:
  3722.          mov   ebx,[dlye]
  3723.          cmp   ebx,[eax+RECT.bottom]
  3724.          jbe   @f
  3725.          mov   [eax+RECT.bottom],ebx
  3726.         @@:
  3727.          jmp   newdw8
  3728.         .az:
  3729.  
  3730.          mov   eax,edi
  3731.          add   eax,draw_data-window_data
  3732.  
  3733.          mov   ebx,[dlx]          ; set limits
  3734.          mov   [eax + RECT.left], ebx
  3735.          mov   ebx,[dly]
  3736.          mov   [eax + RECT.top], ebx
  3737.          mov   ebx,[dlxe]
  3738.          mov   [eax + RECT.right], ebx
  3739.          mov   ebx,[dlye]
  3740.          mov   [eax + RECT.bottom], ebx
  3741.  
  3742.          sub   eax,draw_data-window_data
  3743.  
  3744.          cmp   dword [esp],1
  3745.          jne   nobgrd
  3746.          mov   byte [REDRAW_BACKGROUND], 1
  3747.  
  3748.        newdw8:
  3749.        nobgrd:
  3750.  
  3751.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3752.  
  3753.        ricino:
  3754.  
  3755.        not_this_task:
  3756.  
  3757.          pop   ecx
  3758.  
  3759.          cmp   ecx,[TASK_COUNT]
  3760.          jle   newdw2
  3761.  
  3762.          pop  eax
  3763.          popad
  3764.  
  3765.          ret
  3766.  
  3767. calculatebackground:   ; background
  3768.  
  3769.         mov   edi, [_WinMapAddress]                ; set os to use all pixels
  3770.         mov   eax,0x01010101
  3771.         mov   ecx, [_WinMapSize]
  3772.         shr   ecx, 2
  3773.         rep   stosd
  3774.  
  3775.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3776.  
  3777.         ret
  3778.  
  3779. uglobal
  3780.   imax    dd 0x0
  3781. endg
  3782.  
  3783.  
  3784.  
  3785. delay_ms:     ; delay in 1/1000 sec
  3786.  
  3787.  
  3788.         push  eax
  3789.         push  ecx
  3790.  
  3791.         mov   ecx,esi
  3792.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3793.         imul  ecx, 33941
  3794.         shr   ecx, 9
  3795.         ; </CPU clock fix>
  3796.  
  3797.         in    al,0x61
  3798.         and   al,0x10
  3799.         mov   ah,al
  3800.         cld
  3801.  
  3802.  cnt1:  in    al,0x61
  3803.         and   al,0x10
  3804.         cmp   al,ah
  3805.         jz    cnt1
  3806.  
  3807.         mov   ah,al
  3808.         loop  cnt1
  3809.  
  3810.         pop   ecx
  3811.         pop   eax
  3812.  
  3813.         ret
  3814.  
  3815.  
  3816. set_app_param:
  3817.         mov     edi, [TASK_BASE]
  3818.         mov     eax, [edi + TASKDATA.event_mask]
  3819.         mov     [edi + TASKDATA.event_mask], ebx
  3820.         mov     [esp+32], eax
  3821.         ret
  3822.  
  3823.  
  3824.  
  3825. delay_hs:     ; delay in 1/100 secs
  3826. ; ebx = delay time
  3827.         push  ecx
  3828.         push  edx
  3829.  
  3830.         mov   edx,[timer_ticks]
  3831.  
  3832.       newtic:
  3833.         mov   ecx,[timer_ticks]
  3834.         sub   ecx,edx
  3835.         cmp   ecx,ebx
  3836.         jae   zerodelay
  3837.  
  3838.         call  change_task
  3839.  
  3840.         jmp   newtic
  3841.  
  3842.       zerodelay:
  3843.         pop   edx
  3844.         pop   ecx
  3845.  
  3846.         ret
  3847.  
  3848. align 16        ;very often call this subrutine
  3849. memmove:       ; memory move in bytes
  3850.  
  3851. ; eax = from
  3852. ; ebx = to
  3853. ; ecx = no of bytes
  3854.     test ecx, ecx
  3855.     jle  .ret
  3856.  
  3857.     push esi edi ecx
  3858.  
  3859.     mov  edi, ebx
  3860.     mov  esi, eax
  3861.  
  3862.     test ecx, not 11b
  3863.     jz   @f
  3864.  
  3865.     push ecx
  3866.     shr  ecx, 2
  3867.     rep  movsd
  3868.     pop  ecx
  3869.     and  ecx, 11b
  3870.     jz   .finish
  3871.   @@:
  3872.     rep  movsb
  3873.  
  3874.   .finish:
  3875.     pop  ecx edi esi
  3876.   .ret:
  3877.     ret
  3878.  
  3879.  
  3880. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3881. ;align 4
  3882. ;
  3883. ;read_floppy_file:
  3884. ;
  3885. ;; as input
  3886. ;;
  3887. ;; eax pointer to file
  3888. ;; ebx file lenght
  3889. ;; ecx start 512 byte block number
  3890. ;; edx number of blocks to read
  3891. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3892. ;;
  3893. ;;
  3894. ;; on return
  3895. ;;
  3896. ;; eax = 0 command succesful
  3897. ;;       1 no fd base and/or partition defined
  3898. ;;       2 yet unsupported FS
  3899. ;;       3 unknown FS
  3900. ;;       4 partition not defined at hd
  3901. ;;       5 file not found
  3902. ;; ebx = size of file
  3903. ;
  3904. ;     mov   edi,[TASK_BASE]
  3905. ;     add   edi,0x10
  3906. ;     add   esi,[edi]
  3907. ;     add   eax,[edi]
  3908. ;
  3909. ;     pushad
  3910. ;     mov  edi,esi
  3911. ;     add  edi,1024
  3912. ;     mov  esi,0x100000+19*512
  3913. ;     sub  ecx,1
  3914. ;     shl  ecx,9
  3915. ;     add  esi,ecx
  3916. ;     shl  edx,9
  3917. ;     mov  ecx,edx
  3918. ;     cld
  3919. ;     rep  movsb
  3920. ;     popad
  3921. ;
  3922. ;     mov   [esp+36],eax
  3923. ;     mov   [esp+24],ebx
  3924. ;     ret
  3925.  
  3926.  
  3927.  
  3928. align 4
  3929.  
  3930. sys_programirq:
  3931.  
  3932.     mov   eax, [TASK_BASE]
  3933.     add   ebx, [eax + TASKDATA.mem_start]
  3934.  
  3935.     cmp   ecx, 16
  3936.     jae   .not_owner
  3937.     mov   edi, [eax + TASKDATA.pid]
  3938.     cmp   edi, [irq_owner + 4 * ecx]
  3939.     je    .spril1
  3940. .not_owner:
  3941.     xor   ecx, ecx
  3942.     inc   ecx
  3943.     jmp   .end
  3944.   .spril1:
  3945.  
  3946.     shl   ecx, 6
  3947.     mov   esi, ebx
  3948.     lea   edi, [irq00read + ecx]
  3949.     push  16
  3950.     pop   ecx
  3951.  
  3952.     cld
  3953.     rep   movsd
  3954.   .end:
  3955.     mov   [esp+32], ecx
  3956.     ret
  3957.  
  3958.  
  3959. align 4
  3960.  
  3961. get_irq_data:
  3962.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3963.      xor   bh, bh
  3964.      cmp   ebx, 16
  3965.      jae   .not_owner
  3966.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3967.  
  3968.      mov   eax,[TASK_BASE]
  3969.  
  3970.      cmp   edx,[eax+TASKDATA.pid]
  3971.      je    gidril1
  3972. .not_owner:
  3973.      xor   edx, edx
  3974.      dec   edx
  3975.      jmp   gid1
  3976.  
  3977.   gidril1:
  3978.  
  3979.      shl   ebx, 12
  3980.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3981.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3982.      dec   esi
  3983.      jz    gid1
  3984.      test  edx, edx                      ; check if buffer is empty
  3985.      jz    gid1
  3986.  
  3987.      mov   ebx, [eax + 0x4]
  3988.      mov   edi, ecx
  3989.  
  3990.      mov   ecx, 4000                     ; buffer size, used frequently
  3991.  
  3992.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3993.      jb    @f
  3994.  
  3995.      xor   ebx, ebx
  3996.  
  3997.    @@:
  3998.  
  3999.      lea   esi, [ebx + edx]              ; calculate data size and offset
  4000.      cld
  4001.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  4002.      jbe   @f
  4003.  
  4004.      sub   ecx, ebx
  4005.      sub   edx, ecx
  4006.  
  4007.      lea   esi, [eax + ebx + 0x10]
  4008.      rep   movsb
  4009.  
  4010.      xor   ebx, ebx
  4011.    @@:
  4012.      lea   esi, [eax + ebx + 0x10]
  4013.      mov   ecx, edx
  4014.      add   ebx, edx
  4015.  
  4016.      rep   movsb
  4017.      mov   edx, [eax]
  4018.      mov   [eax], ecx                    ; set data size to zero
  4019.      mov   [eax + 0x4], ebx              ; set data offset
  4020.  
  4021.    gid1:
  4022.      mov   [esp+32], edx                 ; eax
  4023.      ret
  4024.  
  4025.  
  4026. set_io_access_rights:
  4027.       push edi eax
  4028.       mov edi, tss._io_map_0
  4029. ;     mov   ecx,eax
  4030. ;     and   ecx,7    ; offset in byte
  4031. ;     shr   eax,3    ; number of byte
  4032. ;     add   edi,eax
  4033. ;     mov   ebx,1
  4034. ;     shl   ebx,cl
  4035.      test  ebp,ebp
  4036. ;     cmp   ebp,0                ; enable access - ebp = 0
  4037.      jnz   siar1
  4038. ;     not   ebx
  4039. ;     and   [edi],byte bl
  4040.      btr [edi], eax
  4041.      pop eax edi
  4042.      ret
  4043. siar1:
  4044.      bts [edi], eax
  4045.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4046.      pop eax edi
  4047.      ret
  4048. ;reserve/free group of ports
  4049. ;  * eax = 46 - number function
  4050. ;  * ebx = 0 - reserve, 1 - free
  4051. ;  * ecx = number start arrea of ports
  4052. ;  * edx = number end arrea of ports (include last number of port)
  4053. ;Return value:
  4054. ;  * eax = 0 - succesful
  4055. ;  * eax = 1 - error
  4056. ;  * The system has reserve this ports:
  4057. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4058. ;destroys eax,ebx, ebp
  4059. r_f_port_area:
  4060.  
  4061.      test  ebx, ebx
  4062.      jnz   free_port_area
  4063. ;     je    r_port_area
  4064. ;     jmp   free_port_area
  4065.  
  4066. ;   r_port_area:
  4067.  
  4068. ;     pushad
  4069.  
  4070.      cmp   ecx,edx            ; beginning > end ?
  4071.      ja    rpal1
  4072.      cmp   edx,65536
  4073.      jae   rpal1
  4074.      mov   eax,[RESERVED_PORTS]
  4075.      test  eax,eax            ; no reserved areas ?
  4076.      je    rpal2
  4077.      cmp   eax,255            ; max reserved
  4078.      jae   rpal1
  4079.  rpal3:
  4080.      mov   ebx,eax
  4081.      shl   ebx,4
  4082.      add   ebx,RESERVED_PORTS
  4083.      cmp   ecx,[ebx+8]
  4084.      ja    rpal4
  4085.      cmp   edx,[ebx+4]
  4086.      jae   rpal1
  4087. ;     jb    rpal4
  4088. ;     jmp   rpal1
  4089.  rpal4:
  4090.      dec   eax
  4091.      jnz   rpal3
  4092.      jmp   rpal2
  4093.    rpal1:
  4094. ;     popad
  4095. ;     mov   eax,1
  4096.      xor    eax,eax
  4097.      inc    eax
  4098.      ret
  4099.    rpal2:
  4100. ;     popad
  4101.      ; enable port access at port IO map
  4102.         cli
  4103.         pushad                        ; start enable io map
  4104.  
  4105.         cmp   edx,65536 ;16384
  4106.         jae   no_unmask_io ; jge
  4107.         mov   eax,ecx
  4108. ;       push    ebp
  4109.         xor     ebp,ebp                ; enable - eax = port
  4110. new_port_access:
  4111. ;     pushad
  4112.         call    set_io_access_rights
  4113. ;     popad
  4114.      inc   eax
  4115.      cmp   eax,edx
  4116.      jbe   new_port_access
  4117. ;       pop     ebp
  4118. no_unmask_io:
  4119.         popad                         ; end enable io map
  4120.      sti
  4121.  
  4122.      mov   eax,[RESERVED_PORTS]
  4123.      add   eax,1
  4124.      mov   [RESERVED_PORTS],eax
  4125.      shl   eax,4
  4126.      add   eax,RESERVED_PORTS
  4127.      mov   ebx,[TASK_BASE]
  4128.      mov   ebx,[ebx+TASKDATA.pid]
  4129.      mov   [eax],ebx
  4130.      mov   [eax+4],ecx
  4131.      mov   [eax+8],edx
  4132.  
  4133.      xor   eax, eax
  4134.      ret
  4135.  
  4136. free_port_area:
  4137.  
  4138. ;     pushad
  4139.      mov   eax,[RESERVED_PORTS]     ; no reserved areas ?
  4140.      test  eax,eax
  4141.      jz    frpal2
  4142.      mov   ebx,[TASK_BASE]
  4143.      mov   ebx,[ebx+TASKDATA.pid]
  4144.    frpal3:
  4145.      mov   edi,eax
  4146.      shl   edi,4
  4147.      add   edi,RESERVED_PORTS
  4148.      cmp   ebx,[edi]
  4149.      jne   frpal4
  4150.      cmp   ecx,[edi+4]
  4151.      jne   frpal4
  4152.      cmp   edx,[edi+8]
  4153.      jne   frpal4
  4154.      jmp   frpal1
  4155.    frpal4:
  4156.      dec   eax
  4157.      jnz   frpal3
  4158.    frpal2:
  4159. ;     popad
  4160.      inc   eax
  4161.      ret
  4162.    frpal1:
  4163.         push    ecx
  4164.         mov   ecx,256
  4165.         sub   ecx,eax
  4166.         shl   ecx,4
  4167.         mov   esi,edi
  4168.         add   esi,16
  4169.         cld
  4170.         rep   movsb
  4171.  
  4172.         dec   dword [RESERVED_PORTS]
  4173. ;popad
  4174. ;disable port access at port IO map
  4175.  
  4176. ;     pushad                        ; start disable io map
  4177.      pop        eax     ;start port
  4178.      cmp   edx,65536 ;16384
  4179.      jge   no_mask_io
  4180.  
  4181. ;     mov   eax,ecx
  4182.         xor     ebp,ebp
  4183.         inc     ebp
  4184. new_port_access_disable:
  4185. ;     pushad
  4186. ;     mov   ebp,1                  ; disable - eax = port
  4187.      call  set_io_access_rights
  4188. ;     popad
  4189.      inc   eax
  4190.      cmp   eax,edx
  4191.      jbe   new_port_access_disable
  4192. no_mask_io:
  4193. ;     popad                         ; end disable io map
  4194.      xor   eax, eax
  4195.      ret
  4196.  
  4197.  
  4198. reserve_free_irq:
  4199.  
  4200.      xor   esi, esi
  4201.      inc   esi
  4202.      cmp   ecx, 16
  4203.      jae   ril1
  4204.  
  4205.      push  ecx
  4206.      lea   ecx, [irq_owner + 4 * ecx]
  4207.      mov   edx, [ecx]
  4208.      mov   eax, [TASK_BASE]
  4209.      mov   edi, [eax + TASKDATA.pid]
  4210.      pop   eax
  4211.      dec   ebx
  4212.      jnz   reserve_irq
  4213.  
  4214.      cmp   edx, edi
  4215.      jne   ril1
  4216.      dec   esi
  4217.      mov   [ecx], esi
  4218.  
  4219.      jmp   ril1
  4220.  
  4221.   reserve_irq:
  4222.  
  4223.      cmp   dword [ecx], 0
  4224.      jne   ril1
  4225.  
  4226.      mov   ebx, [f_irqs + 4 * eax]
  4227.  
  4228.      stdcall attach_int_handler, eax, ebx, dword 0
  4229.  
  4230.      mov   [ecx], edi
  4231.  
  4232.      dec   esi
  4233.    ril1:
  4234.      mov   [esp+32], esi ; return in eax
  4235.      ret
  4236.  
  4237. iglobal
  4238. f_irqs:
  4239.      dd 0x0
  4240.      dd 0x0
  4241.      dd p_irq2
  4242.      dd p_irq3
  4243.      dd p_irq4
  4244.      dd p_irq5
  4245.      dd p_irq6
  4246.      dd p_irq7
  4247.      dd p_irq8
  4248.      dd p_irq9
  4249.      dd p_irq10
  4250.      dd p_irq11
  4251.      dd 0x0
  4252.      dd 0x0
  4253.      dd p_irq14
  4254.      dd p_irq15
  4255.  
  4256. endg
  4257.  
  4258. drawbackground:
  4259.        inc   [mouse_pause]
  4260.        cmp   [SCR_MODE],word 0x12
  4261.        je   dbrv20
  4262.      dbrv12:
  4263.        cmp  [SCR_MODE],word 0100000000000000b
  4264.        jge  dbrv20
  4265.        cmp  [SCR_MODE],word 0x13
  4266.        je   dbrv20
  4267.        call  vesa12_drawbackground
  4268.        dec   [mouse_pause]
  4269.        call   [draw_pointer]
  4270.        ret
  4271.      dbrv20:
  4272.        cmp   [BgrDrawMode],dword 1
  4273.        jne   bgrstr
  4274.        call  vesa20_drawbackground_tiled
  4275.        dec   [mouse_pause]
  4276.        call   [draw_pointer]
  4277.        ret
  4278.      bgrstr:
  4279.        call  vesa20_drawbackground_stretch
  4280.        dec   [mouse_pause]
  4281.        call   [draw_pointer]
  4282.        ret
  4283.  
  4284. align 4
  4285.  
  4286. syscall_putimage:                       ; PutImage
  4287. sys_putimage:
  4288.      test  ecx,0x80008000
  4289.      jnz   .exit
  4290.      test  ecx,0x0000FFFF
  4291.      jz    .exit
  4292.      test  ecx,0xFFFF0000
  4293.      jnz   @f
  4294.   .exit:
  4295.      ret
  4296.  @@:
  4297.         mov     edi,[current_slot]
  4298.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4299.         rol     edx,16
  4300.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4301.         rol     edx,16
  4302.   .forced:
  4303.         push    ebp esi 0
  4304.         mov     ebp, putimage_get24bpp
  4305.         mov     esi, putimage_init24bpp
  4306. sys_putimage_bpp:
  4307. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4308. ;        mov     eax, vga_putimage
  4309.         cmp     [SCR_MODE], word 0x12
  4310.         jz      @f   ;.doit
  4311.         mov     eax, vesa12_putimage
  4312.         cmp     [SCR_MODE], word 0100000000000000b
  4313.         jae     @f
  4314.         cmp     [SCR_MODE], word 0x13
  4315.         jnz     .doit
  4316. @@:
  4317.         mov     eax, vesa20_putimage
  4318. .doit:
  4319.         inc     [mouse_pause]
  4320.         call    eax
  4321.         dec     [mouse_pause]
  4322.         pop     ebp esi ebp
  4323.         jmp     [draw_pointer]
  4324.  
  4325. syscall_putimage_palette:
  4326.         mov     edi, esi
  4327.         mov     esi, edx
  4328.         mov     edx, ecx
  4329.         mov     ecx, ebx
  4330.         mov     ebx, eax
  4331. sys_putimage_palette:
  4332. ; ebx = pointer to image
  4333. ; ecx = [xsize]*65536 + [ysize]
  4334. ; edx = [xstart]*65536 + [ystart]
  4335. ; esi = number of bits per pixel, must be 8, 24 or 32
  4336. ; edi = pointer to palette
  4337. ; ebp = row delta
  4338.         mov     eax, [CURRENT_TASK]
  4339.         shl     eax, 8
  4340.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4341.         rol     edx, 16
  4342.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4343.         rol     edx, 16
  4344. .forced:
  4345.         cmp     esi, 1
  4346.         jnz     @f
  4347.         push    edi
  4348.         mov     eax, [edi+4]
  4349.         sub     eax, [edi]
  4350.         push    eax
  4351.         push    dword [edi]
  4352.         push    0ffffff80h
  4353.         mov     edi, esp
  4354.         call    put_mono_image
  4355.         add     esp, 12
  4356.         pop     edi
  4357.         ret
  4358. @@:
  4359.         cmp     esi, 2
  4360.         jnz     @f
  4361.         push    edi
  4362.         push    0ffffff80h
  4363.         mov     edi, esp
  4364.         call    put_2bit_image
  4365.         pop     eax
  4366.         pop     edi
  4367.         ret
  4368. @@:
  4369.         cmp     esi, 4
  4370.         jnz     @f
  4371.         push    edi
  4372.         push    0ffffff80h
  4373.         mov     edi, esp
  4374.         call    put_4bit_image
  4375.         pop     eax
  4376.         pop     edi
  4377.         ret
  4378. @@:
  4379.         push    ebp esi ebp
  4380.         cmp     esi, 8
  4381.         jnz     @f
  4382.         mov     ebp, putimage_get8bpp
  4383.         mov     esi, putimage_init8bpp
  4384.         jmp     sys_putimage_bpp
  4385. @@:
  4386.         cmp     esi, 15
  4387.         jnz     @f
  4388.         mov     ebp, putimage_get15bpp
  4389.         mov     esi, putimage_init15bpp
  4390.         jmp     sys_putimage_bpp
  4391. @@:
  4392.         cmp     esi, 16
  4393.         jnz     @f
  4394.         mov     ebp, putimage_get16bpp
  4395.         mov     esi, putimage_init16bpp
  4396.         jmp     sys_putimage_bpp
  4397. @@:
  4398.         cmp     esi, 24
  4399.         jnz     @f
  4400.         mov     ebp, putimage_get24bpp
  4401.         mov     esi, putimage_init24bpp
  4402.         jmp     sys_putimage_bpp
  4403. @@:
  4404.         cmp     esi, 32
  4405.         jnz     @f
  4406.         mov     ebp, putimage_get32bpp
  4407.         mov     esi, putimage_init32bpp
  4408.         jmp     sys_putimage_bpp
  4409. @@:
  4410.         pop     ebp esi ebp
  4411.         ret
  4412.  
  4413. put_mono_image:
  4414.         push    ebp esi ebp
  4415.         mov     ebp, putimage_get1bpp
  4416.         mov     esi, putimage_init1bpp
  4417.         jmp     sys_putimage_bpp
  4418. put_2bit_image:
  4419.         push    ebp esi ebp
  4420.         mov     ebp, putimage_get2bpp
  4421.         mov     esi, putimage_init2bpp
  4422.         jmp     sys_putimage_bpp
  4423. put_4bit_image:
  4424.         push    ebp esi ebp
  4425.         mov     ebp, putimage_get4bpp
  4426.         mov     esi, putimage_init4bpp
  4427.         jmp     sys_putimage_bpp
  4428.  
  4429. putimage_init24bpp:
  4430.         lea     eax, [eax*3]
  4431. putimage_init8bpp:
  4432.         ret
  4433.  
  4434. align 16
  4435. putimage_get24bpp:
  4436.         movzx   eax, byte [esi+2]
  4437.         shl     eax, 16
  4438.         mov     ax, [esi]
  4439.         add     esi, 3
  4440.         ret     4
  4441. align 16
  4442. putimage_get8bpp:
  4443.         movzx   eax, byte [esi]
  4444.         push    edx
  4445.         mov     edx, [esp+8]
  4446.         mov     eax, [edx+eax*4]
  4447.         pop     edx
  4448.         inc     esi
  4449.         ret     4
  4450.  
  4451. putimage_init1bpp:
  4452.         add     eax, ecx
  4453.         push    ecx
  4454.         add     eax, 7
  4455.         add     ecx, 7
  4456.         shr     eax, 3
  4457.         shr     ecx, 3
  4458.         sub     eax, ecx
  4459.         pop     ecx
  4460.         ret
  4461. align 16
  4462. putimage_get1bpp:
  4463.         push    edx
  4464.         mov     edx, [esp+8]
  4465.         mov     al, [edx]
  4466.         add     al, al
  4467.         jnz     @f
  4468.         lodsb
  4469.         adc     al, al
  4470. @@:
  4471.         mov     [edx], al
  4472.         sbb     eax, eax
  4473.         and     eax, [edx+8]
  4474.         add     eax, [edx+4]
  4475.         pop     edx
  4476.         ret     4
  4477.  
  4478. putimage_init2bpp:
  4479.         add     eax, ecx
  4480.         push    ecx
  4481.         add     ecx, 3
  4482.         add     eax, 3
  4483.         shr     ecx, 2
  4484.         shr     eax, 2
  4485.         sub     eax, ecx
  4486.         pop     ecx
  4487.         ret
  4488. align 16
  4489. putimage_get2bpp:
  4490.         push    edx
  4491.         mov     edx, [esp+8]
  4492.         mov     al, [edx]
  4493.         mov     ah, al
  4494.         shr     al, 6
  4495.         shl     ah, 2
  4496.         jnz     .nonewbyte
  4497.         lodsb
  4498.         mov     ah, al
  4499.         shr     al, 6
  4500.         shl     ah, 2
  4501.         add     ah, 1
  4502. .nonewbyte:
  4503.         mov     [edx], ah
  4504.         mov     edx, [edx+4]
  4505.         movzx   eax, al
  4506.         mov     eax, [edx+eax*4]
  4507.         pop     edx
  4508.         ret     4
  4509.  
  4510. putimage_init4bpp:
  4511.         add     eax, ecx
  4512.         push    ecx
  4513.         add     ecx, 1
  4514.         add     eax, 1
  4515.         shr     ecx, 1
  4516.         shr     eax, 1
  4517.         sub     eax, ecx
  4518.         pop     ecx
  4519.         ret
  4520. align 16
  4521. putimage_get4bpp:
  4522.         push    edx
  4523.         mov     edx, [esp+8]
  4524.         add     byte [edx], 80h
  4525.         jc      @f
  4526.         movzx   eax, byte [edx+1]
  4527.         mov     edx, [edx+4]
  4528.         and     eax, 0x0F
  4529.         mov     eax, [edx+eax*4]
  4530.         pop     edx
  4531.         ret     4
  4532. @@:
  4533.         movzx   eax, byte [esi]
  4534.         add     esi, 1
  4535.         mov     [edx+1], al
  4536.         shr     eax, 4
  4537.         mov     edx, [edx+4]
  4538.         mov     eax, [edx+eax*4]
  4539.         pop     edx
  4540.         ret     4
  4541.  
  4542. putimage_init32bpp:
  4543.         shl     eax, 2
  4544.         ret
  4545. align 16
  4546. putimage_get32bpp:
  4547.         lodsd
  4548.         ret     4
  4549.  
  4550. putimage_init15bpp:
  4551. putimage_init16bpp:
  4552.         add     eax, eax
  4553.         ret
  4554. align 16
  4555. putimage_get15bpp:
  4556. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4557.         push    ecx edx
  4558.         movzx   eax, word [esi]
  4559.         add     esi, 2
  4560.         mov     ecx, eax
  4561.         mov     edx, eax
  4562.         and     eax, 0x1F
  4563.         and     ecx, 0x1F shl 5
  4564.         and     edx, 0x1F shl 10
  4565.         shl     eax, 3
  4566.         shl     ecx, 6
  4567.         shl     edx, 9
  4568.         or      eax, ecx
  4569.         or      eax, edx
  4570.         pop     edx ecx
  4571.         ret     4
  4572.  
  4573. align 16
  4574. putimage_get16bpp:
  4575. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4576.         push    ecx edx
  4577.         movzx   eax, word [esi]
  4578.         add     esi, 2
  4579.         mov     ecx, eax
  4580.         mov     edx, eax
  4581.         and     eax, 0x1F
  4582.         and     ecx, 0x3F shl 5
  4583.         and     edx, 0x1F shl 11
  4584.         shl     eax, 3
  4585.         shl     ecx, 5
  4586.         shl     edx, 8
  4587.         or      eax, ecx
  4588.         or      eax, edx
  4589.         pop     edx ecx
  4590.         ret     4
  4591.  
  4592. ; eax x beginning
  4593. ; ebx y beginning
  4594. ; ecx x end
  4595.         ; edx y end
  4596. ; edi color
  4597.  
  4598. __sys_drawbar:
  4599.         mov     esi,[current_slot]
  4600.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4601.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4602.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4603.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4604.   .forced:
  4605.     inc   [mouse_pause]
  4606. ;        call    [disable_mouse]
  4607.     cmp   [SCR_MODE],word 0x12
  4608.     je   dbv20
  4609.    sdbv20:
  4610.     cmp  [SCR_MODE],word 0100000000000000b
  4611.     jge  dbv20
  4612.     cmp  [SCR_MODE],word 0x13
  4613.     je   dbv20
  4614.     call vesa12_drawbar
  4615.     dec   [mouse_pause]
  4616.     call   [draw_pointer]
  4617.     ret
  4618.   dbv20:
  4619.     call vesa20_drawbar
  4620.     dec   [mouse_pause]
  4621.     call   [draw_pointer]
  4622.     ret
  4623.  
  4624.  
  4625.  
  4626. kb_read:
  4627.  
  4628.         push    ecx edx
  4629.  
  4630.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4631.       kr_loop:
  4632.         in      al,0x64
  4633.         test    al,1
  4634.         jnz     kr_ready
  4635.         loop    kr_loop
  4636.         mov     ah,1
  4637.         jmp     kr_exit
  4638.       kr_ready:
  4639.         push    ecx
  4640.         mov     ecx,32
  4641.       kr_delay:
  4642.         loop    kr_delay
  4643.         pop     ecx
  4644.         in      al,0x60
  4645.         xor     ah,ah
  4646.       kr_exit:
  4647.  
  4648.         pop     edx ecx
  4649.  
  4650.         ret
  4651.  
  4652.  
  4653. kb_write:
  4654.  
  4655.         push    ecx edx
  4656.  
  4657.         mov     dl,al
  4658. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4659. ;      kw_loop1:
  4660. ;        in      al,0x64
  4661. ;        test    al,0x20
  4662. ;        jz      kw_ok1
  4663. ;        loop    kw_loop1
  4664. ;        mov     ah,1
  4665. ;        jmp     kw_exit
  4666. ;      kw_ok1:
  4667.         in      al,0x60
  4668.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4669.       kw_loop:
  4670.         in      al,0x64
  4671.         test    al,2
  4672.         jz      kw_ok
  4673.         loop    kw_loop
  4674.         mov     ah,1
  4675.         jmp     kw_exit
  4676.       kw_ok:
  4677.         mov     al,dl
  4678.         out     0x60,al
  4679.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4680.       kw_loop3:
  4681.         in      al,0x64
  4682.         test    al,2
  4683.         jz      kw_ok3
  4684.         loop    kw_loop3
  4685.         mov     ah,1
  4686.         jmp     kw_exit
  4687.       kw_ok3:
  4688.         mov     ah,8
  4689.       kw_loop4:
  4690.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4691.       kw_loop5:
  4692.         in      al,0x64
  4693.         test    al,1
  4694.         jnz     kw_ok4
  4695.         loop    kw_loop5
  4696.         dec     ah
  4697.         jnz     kw_loop4
  4698.       kw_ok4:
  4699.         xor     ah,ah
  4700.       kw_exit:
  4701.  
  4702.         pop     edx ecx
  4703.  
  4704.         ret
  4705.  
  4706.  
  4707. kb_cmd:
  4708.  
  4709.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4710.       c_wait:
  4711.         in      al,0x64
  4712.         test    al,2
  4713.         jz      c_send
  4714.         loop    c_wait
  4715.         jmp     c_error
  4716.       c_send:
  4717.         mov     al,bl
  4718.         out     0x64,al
  4719.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4720.       c_accept:
  4721.         in      al,0x64
  4722.         test    al,2
  4723.         jz      c_ok
  4724.         loop    c_accept
  4725.       c_error:
  4726.         mov     ah,1
  4727.         jmp     c_exit
  4728.       c_ok:
  4729.         xor     ah,ah
  4730.       c_exit:
  4731.         ret
  4732.  
  4733.  
  4734. setmouse:  ; set mousepicture -pointer
  4735.            ; ps2 mouse enable
  4736.  
  4737.      mov     [MOUSE_PICTURE],dword mousepointer
  4738.  
  4739.      cli
  4740.  
  4741.      ret
  4742.  
  4743. if used _rdtsc
  4744. _rdtsc:
  4745.      bt [cpu_caps], CAPS_TSC
  4746.      jnc ret_rdtsc
  4747.      rdtsc
  4748.      ret
  4749.    ret_rdtsc:
  4750.      mov   edx,0xffffffff
  4751.      mov   eax,0xffffffff
  4752.      ret
  4753. end if
  4754.  
  4755. rerouteirqs:
  4756.  
  4757.         cli
  4758.  
  4759.         mov     al,0x11         ;  icw4, edge triggered
  4760.         out     0x20,al
  4761.         call    pic_delay
  4762.         out     0xA0,al
  4763.         call    pic_delay
  4764.  
  4765.         mov     al,0x20         ;  generate 0x20 +
  4766.         out     0x21,al
  4767.         call    pic_delay
  4768.         mov     al,0x28         ;  generate 0x28 +
  4769.         out     0xA1,al
  4770.         call    pic_delay
  4771.  
  4772.         mov     al,0x04         ;  slave at irq2
  4773.         out     0x21,al
  4774.         call    pic_delay
  4775.         mov     al,0x02         ;  at irq9
  4776.         out     0xA1,al
  4777.         call    pic_delay
  4778.  
  4779.         mov     al,0x01         ;  8086 mode
  4780.         out     0x21,al
  4781.         call    pic_delay
  4782.         out     0xA1,al
  4783.         call    pic_delay
  4784.  
  4785.         mov     al,255          ; mask all irq's
  4786.         out     0xA1,al
  4787.         call    pic_delay
  4788.         out     0x21,al
  4789.         call    pic_delay
  4790.  
  4791.         mov     ecx,0x1000
  4792.         cld
  4793. picl1:  call    pic_delay
  4794.         loop    picl1
  4795.  
  4796.         mov     al,255          ; mask all irq's
  4797.         out     0xA1,al
  4798.         call    pic_delay
  4799.         out     0x21,al
  4800.         call    pic_delay
  4801.  
  4802.         cli
  4803.  
  4804.         ret
  4805.  
  4806.  
  4807. pic_delay:
  4808.  
  4809.         jmp     pdl1
  4810. pdl1:   ret
  4811.  
  4812.  
  4813. sys_msg_board_str:
  4814.  
  4815.      pushad
  4816.    @@:
  4817.      cmp    [esi],byte 0
  4818.      je     @f
  4819.      mov    eax,1
  4820.      movzx  ebx,byte [esi]
  4821.      call   sys_msg_board
  4822.      inc    esi
  4823.      jmp    @b
  4824.    @@:
  4825.      popad
  4826.      ret
  4827.  
  4828. sys_msg_board_byte:
  4829. ; in: al = byte to display
  4830. ; out: nothing
  4831. ; destroys: nothing
  4832.         pushad
  4833.         mov     ecx, 2
  4834.         shl     eax, 24
  4835.         jmp     @f
  4836.  
  4837. sys_msg_board_word:
  4838. ; in: ax = word to display
  4839. ; out: nothing
  4840. ; destroys: nothing
  4841.         pushad
  4842.         mov     ecx, 4
  4843.         shl     eax, 16
  4844.         jmp     @f
  4845.  
  4846. sys_msg_board_dword:
  4847. ; in: eax = dword to display
  4848. ; out: nothing
  4849. ; destroys: nothing
  4850.         pushad
  4851.         mov     ecx, 8
  4852. @@:
  4853.         push    ecx
  4854.         rol     eax, 4
  4855.         push    eax
  4856.         and     al, 0xF
  4857.         cmp     al, 10
  4858.         sbb     al, 69h
  4859.         das
  4860.         mov     bl, al
  4861.         xor     eax, eax
  4862.         inc     eax
  4863.         call    sys_msg_board
  4864.         pop     eax
  4865.         pop     ecx
  4866.         loop    @b
  4867.         popad
  4868.         ret
  4869.  
  4870. uglobal
  4871.   msg_board_data: times 4096 db 0
  4872.   msg_board_count dd 0x0
  4873. endg
  4874.  
  4875. sys_msg_board:
  4876.  
  4877. ; eax=1 : write :  bl byte to write
  4878. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4879.  
  4880.         mov     ecx, [msg_board_count]
  4881.         cmp     eax, 1
  4882.         jne     .smbl1
  4883.  
  4884. if defined debug_com_base
  4885.  
  4886.         push    dx ax
  4887.  
  4888.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4889.         mov     dx, debug_com_base+5
  4890.         in      al, dx
  4891.         test    al, 1 shl 5
  4892.         jz      @r
  4893.  
  4894.         mov     dx, debug_com_base      ; Output the byte
  4895.         mov     al, bl
  4896.         out     dx, al
  4897.  
  4898.         pop     ax dx
  4899.  
  4900. end if
  4901.  
  4902.         mov     [msg_board_data+ecx],bl
  4903.         inc     ecx
  4904.         and     ecx, 4095
  4905.         mov     [msg_board_count], ecx
  4906.         mov     [check_idle_semaphore], 5
  4907.         ret
  4908. .smbl1:
  4909.         cmp     eax, 2
  4910.         jne     .smbl2
  4911.         test    ecx, ecx
  4912.         jz      .smbl21
  4913.         mov     eax, msg_board_data+1
  4914.         mov     ebx, msg_board_data
  4915.         movzx   edx, byte [ebx]
  4916.         call    memmove
  4917.         dec     [msg_board_count]
  4918.         mov     [esp + 36], edx ;eax
  4919.         mov     [esp + 24], dword 1
  4920.         ret
  4921. .smbl21:
  4922.         mov     [esp+36], ecx
  4923.         mov     [esp+24], ecx
  4924. .smbl2:
  4925.         ret
  4926.  
  4927.  
  4928.  
  4929. sys_process_def:
  4930.         mov     edi, [CURRENT_TASK]
  4931.  
  4932.         dec     eax             ; 1 = set keyboard mode
  4933.      jne   no_set_keyboard_setup
  4934.  
  4935.      shl   edi,8
  4936.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4937.  
  4938.      ret
  4939.  
  4940.    no_set_keyboard_setup:
  4941.  
  4942.         dec     eax             ; 2 = get keyboard mode
  4943.      jne   no_get_keyboard_setup
  4944.  
  4945.      shl   edi,8
  4946.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4947.  
  4948.      mov   [esp+36],eax
  4949.  
  4950.      ret
  4951.  
  4952.    no_get_keyboard_setup:
  4953.  
  4954.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4955.      jne   no_get_keyboard_cas
  4956.  
  4957. ;     xor   eax,eax
  4958. ;     movzx eax,byte [shift]
  4959. ;     movzx ebx,byte [ctrl]
  4960. ;     shl   ebx,2
  4961. ;     add   eax,ebx
  4962. ;     movzx ebx,byte [alt]
  4963. ;     shl   ebx,3
  4964. ;     add   eax,ebx
  4965.  
  4966.  ;// mike.dld [
  4967.      mov   eax, [kb_state]
  4968.  ;// mike.dld ]
  4969.  
  4970.      mov   [esp+36],eax
  4971.  
  4972.      ret
  4973.  
  4974.    no_get_keyboard_cas:
  4975.  
  4976.         dec     eax
  4977.         jnz     no_add_keyboard_hotkey
  4978.  
  4979.         mov     eax, hotkey_list
  4980. @@:
  4981.         cmp     dword [eax+8], 0
  4982.         jz      .found_free
  4983.         add     eax, 16
  4984.         cmp     eax, hotkey_list+16*256
  4985.         jb      @b
  4986.         mov     dword [esp+36], 1
  4987.         ret
  4988. .found_free:
  4989.         mov     [eax+8], edi
  4990.         mov     [eax+4], ecx
  4991.         movzx   ebx, bl
  4992.         lea     ebx, [hotkey_scancodes+ebx*4]
  4993.         mov     ecx, [ebx]
  4994.         mov     [eax], ecx
  4995.         mov     [ebx], eax
  4996.         mov     [eax+12], ebx
  4997.         jecxz   @f
  4998.         mov     [ecx+12], eax
  4999. @@:
  5000.         and     dword [esp+36], 0
  5001.         ret
  5002.  
  5003. no_add_keyboard_hotkey:
  5004.  
  5005.         dec     eax
  5006.         jnz     no_del_keyboard_hotkey
  5007.  
  5008.         movzx   ebx, bl
  5009.         lea     ebx, [hotkey_scancodes+ebx*4]
  5010.         mov     eax, [ebx]
  5011. .scan:
  5012.         test    eax, eax
  5013.         jz      .notfound
  5014.         cmp     [eax+8], edi
  5015.         jnz     .next
  5016.         cmp     [eax+4], ecx
  5017.         jz      .found
  5018. .next:
  5019.         mov     eax, [eax]
  5020.         jmp     .scan
  5021. .notfound:
  5022.         mov     dword [esp+36], 1
  5023.         ret
  5024. .found:
  5025.         mov     ecx, [eax]
  5026.         jecxz   @f
  5027.         mov     edx, [eax+12]
  5028.         mov     [ecx+12], edx
  5029. @@:
  5030.         mov     ecx, [eax+12]
  5031.         mov     edx, [eax]
  5032.         mov     [ecx], edx
  5033.         xor     edx, edx
  5034.         mov     [eax+4], edx
  5035.         mov     [eax+8], edx
  5036.         mov     [eax+12], edx
  5037.         mov     [eax], edx
  5038.         mov     [esp+36], edx
  5039.         ret
  5040.  
  5041. no_del_keyboard_hotkey:
  5042.      ret
  5043.  
  5044.  
  5045. align 4
  5046.  
  5047. sys_gs:                         ; direct screen access
  5048.  
  5049.      cmp  eax,1                 ; resolution
  5050.      jne  no_gs1
  5051.      mov  eax,[Screen_Max_X]
  5052.      shl  eax,16
  5053.      mov  ax,[Screen_Max_Y]
  5054.      add  eax,0x00010001
  5055.      mov  [esp+36],eax
  5056.      ret
  5057.    no_gs1:
  5058.  
  5059.      cmp   eax,2                ; bits per pixel
  5060.      jne   no_gs2
  5061.      movzx eax,byte [ScreenBPP]
  5062.      mov   [esp+36],eax
  5063.      ret
  5064.    no_gs2:
  5065.  
  5066.      cmp   eax,3                ; bytes per scanline
  5067.      jne   no_gs3
  5068.      mov   eax,[BytesPerScanLine]
  5069.      mov   [esp+36],eax
  5070.      ret
  5071.    no_gs3:
  5072.  
  5073.      or  [esp+36],dword -1
  5074.      ret
  5075.  
  5076.  
  5077. align 4 ; PCI functions
  5078.  
  5079. sys_pci:
  5080.  
  5081.      call  pci_api
  5082.      mov   [esp+36],eax
  5083.      ret
  5084.  
  5085.  
  5086. align 4  ;  system functions
  5087.  
  5088. syscall_setpixel:                       ; SetPixel
  5089.  
  5090.         mov     eax, ebx
  5091.         mov     ebx, ecx
  5092.         mov     ecx, edx
  5093.         mov     edx, [TASK_BASE]
  5094.         add     eax, [edx-twdw+WDATA.box.left]
  5095.         add     ebx, [edx-twdw+WDATA.box.top]
  5096.         mov     edi, [current_slot]
  5097.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5098.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5099.         xor     edi, edi ; no force
  5100. ;       mov     edi, 1
  5101.         call    [_display.disable_mouse]
  5102.         jmp     [putpixel]
  5103.  
  5104. align 4
  5105.  
  5106. syscall_writetext:                      ; WriteText
  5107.  
  5108.         mov   eax,[TASK_BASE]
  5109.         mov   ebp,[eax-twdw+WDATA.box.left]
  5110.         push  esi
  5111.         mov   esi,[current_slot]
  5112.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  5113.         shl   ebp,16
  5114.         add   ebp,[eax-twdw+WDATA.box.top]
  5115.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  5116.         pop   esi
  5117.         add   ebx,ebp
  5118.         mov   eax,edi
  5119.         xor   edi,edi
  5120.         jmp   dtext
  5121.  
  5122. align 4
  5123.  
  5124. syscall_openramdiskfile:                ; OpenRamdiskFile
  5125.  
  5126.         mov     eax, ebx
  5127.         mov     ebx, ecx
  5128.         mov     ecx, edx
  5129.         mov     edx, esi
  5130.         mov     esi, 12
  5131.         call    fileread
  5132.         mov     [esp+32], eax
  5133.         ret
  5134.  
  5135. align 4
  5136.  
  5137. syscall_drawrect:                       ; DrawRect
  5138.  
  5139.         mov     edi, edx ; color + gradient
  5140.         and     edi, 0x80FFFFFF
  5141.         test    bx, bx  ; x.size
  5142.         je      .drectr
  5143.         test    cx, cx ; y.size
  5144.         je      .drectr
  5145.  
  5146.         mov     eax, ebx ; bad idea
  5147.         mov     ebx, ecx
  5148.  
  5149.         movzx   ecx, ax ; ecx - x.size
  5150.         shr     eax, 16 ; eax - x.coord
  5151.         movzx   edx, bx ; edx - y.size
  5152.         shr     ebx, 16 ; ebx - y.coord
  5153.         mov     esi, [current_slot]
  5154.  
  5155.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5156.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5157.         add     ecx, eax
  5158.         add     edx, ebx
  5159.         jmp     [drawbar]
  5160. .drectr:
  5161.         ret
  5162.  
  5163. align 4
  5164. syscall_getscreensize:                  ; GetScreenSize
  5165.         mov     ax, [Screen_Max_X]
  5166.         shl     eax, 16
  5167.         mov     ax, [Screen_Max_Y]
  5168.         mov     [esp + 32], eax
  5169.         ret
  5170.  
  5171. align 4
  5172.  
  5173. syscall_cdaudio:                        ; CD
  5174.  
  5175.         cmp     ebx, 4
  5176.         jb      .audio
  5177.         jz      .eject
  5178.         cmp     ebx, 5
  5179.         jnz     .ret
  5180. .load:
  5181.         call    .reserve
  5182.         call    LoadMedium
  5183.         ;call    .free
  5184.                 jmp             .free
  5185. ;        ret
  5186. .eject:
  5187.         call    .reserve
  5188.         call    clear_CD_cache
  5189.         call    allow_medium_removal
  5190.         call    EjectMedium
  5191. ;        call    .free
  5192.                 jmp             .free
  5193. ;        ret
  5194. .audio:
  5195.      call  sys_cd_audio
  5196.      mov   [esp+36-4],eax
  5197. .ret:
  5198.      ret
  5199.  
  5200. .reserve:
  5201.         call    reserve_cd
  5202.         mov     eax, ecx
  5203.         shr     eax, 1
  5204.         and     eax, 1
  5205.         inc     eax
  5206.         mov     [ChannelNumber], ax
  5207.         mov     eax, ecx
  5208.         and     eax, 1
  5209.         mov     [DiskNumber], al
  5210.         call    reserve_cd_channel
  5211.         and     ebx, 3
  5212.         inc     ebx
  5213.         mov     [cdpos], ebx
  5214.         add     ebx, ebx
  5215.         mov     cl, 8
  5216.         sub     cl, bl
  5217.         mov     al, [DRIVE_DATA+1]
  5218.         shr     al, cl
  5219.         test    al, 2
  5220.         jz      .free;.err
  5221.         ret
  5222. .free:
  5223.         call    free_cd_channel
  5224.         and     [cd_status], 0
  5225.         ret
  5226. .err:
  5227.         call    .free
  5228. ;        pop     eax
  5229.         ret
  5230.  
  5231. align 4
  5232.  
  5233. syscall_getpixel:                       ; GetPixel
  5234.      mov   ecx, [Screen_Max_X]
  5235.      inc   ecx
  5236.      xor   edx, edx
  5237.      mov   eax, ebx
  5238.      div   ecx
  5239.      mov   ebx, edx
  5240.      xchg  eax, ebx
  5241.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5242.      mov   [esp + 32], ecx
  5243.      ret
  5244.  
  5245. align 4
  5246.  
  5247. syscall_getarea:
  5248. ;eax = 36
  5249. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5250. ;ecx = [size x]*65536 + [size y]
  5251. ;edx = [start x]*65536 + [start y]
  5252.      pushad
  5253.          inc   [mouse_pause]
  5254. ; Check of use of the hardware cursor.
  5255.       cmp  [_display.disable_mouse],__sys_disable_mouse
  5256.           jne  @f
  5257. ; Since the test for the coordinates of the mouse should not be used,
  5258. ; then use the call [disable_mouse] is not possible!
  5259.       cmp  dword [MOUSE_VISIBLE],dword 0
  5260.       jne  @f
  5261.       pushf
  5262.       cli
  5263.       call draw_mouse_under
  5264.       popf
  5265.       mov  [MOUSE_VISIBLE],dword 1
  5266. @@:
  5267.      mov   edi,ebx
  5268.      mov   eax,edx
  5269.      shr   eax,16
  5270.      mov   ebx,edx
  5271.      and   ebx,0xffff
  5272.      dec   eax
  5273.            dec   ebx
  5274.      ; eax - x, ebx - y
  5275.      mov   edx,ecx
  5276.      
  5277.      shr   ecx,16
  5278.      and   edx,0xffff
  5279.      mov   esi,ecx
  5280.      ; ecx - size x, edx - size y
  5281.          
  5282.          mov   ebp,edx
  5283.          dec   ebp
  5284.      lea   ebp,[ebp*3]
  5285.          
  5286.          imul  ebp,esi
  5287.          
  5288.          mov   esi,ecx
  5289.          dec   esi
  5290.          lea   esi,[esi*3]
  5291.          
  5292.      add   ebp,esi
  5293.      add   ebp,edi
  5294.  
  5295.      add   ebx,edx
  5296.          
  5297. .start_y:
  5298.      push  ecx edx
  5299. .start_x:
  5300.      push  eax ebx ecx
  5301.      add   eax,ecx
  5302.  
  5303.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5304.      
  5305.      mov   [ebp],cx
  5306.      shr   ecx,16
  5307.      mov   [ebp+2],cl
  5308.  
  5309.      pop   ecx ebx eax
  5310.      sub   ebp,3
  5311.      dec   ecx
  5312.      jnz   .start_x
  5313.          pop   edx ecx
  5314.          dec   ebx
  5315.      dec   edx
  5316.      jnz   .start_y
  5317.      dec        [mouse_pause]
  5318. ; Check of use of the hardware cursor.
  5319.       cmp  [_display.disable_mouse],__sys_disable_mouse
  5320.           jne  @f
  5321.          call  [draw_pointer]
  5322. @@:
  5323.      popad
  5324.      ret
  5325.  
  5326. align 4
  5327.  
  5328. syscall_drawline:                       ; DrawLine
  5329.  
  5330.         mov     edi, [TASK_BASE]
  5331.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5332.         mov     ebp, eax
  5333.         mov     esi, [current_slot]
  5334.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5335.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5336.         add     ebp,ebx
  5337.         shl     eax, 16
  5338.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5339.         add     eax, ebp
  5340.         mov     ebp, ebx
  5341.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5342.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5343.         add     ebp, ecx
  5344.         shl     ebx, 16
  5345.         xor     edi, edi
  5346.         add     ebx, ebp
  5347.         mov     ecx, edx
  5348.         jmp     [draw_line]
  5349.  
  5350. align 4
  5351.  
  5352. syscall_getirqowner:                    ; GetIrqOwner
  5353.  
  5354.      cmp   ebx,16
  5355.      jae   .err
  5356.  
  5357.      cmp   [irq_rights + 4 * ebx], dword 2
  5358.      je    .err
  5359.  
  5360.      mov   eax,[4 * ebx + irq_owner]
  5361.      mov   [esp+32],eax
  5362.  
  5363.      ret
  5364. .err:
  5365.      or    dword [esp+32], -1
  5366.      ret
  5367.  
  5368. align 4
  5369.  
  5370. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5371.  
  5372.      call  r_f_port_area
  5373.      mov   [esp+32],eax
  5374.      ret
  5375.  
  5376. align 4
  5377.  
  5378. syscall_threads:                        ; CreateThreads
  5379.  
  5380.      call  sys_threads
  5381.      mov   [esp+36],eax
  5382.      ret
  5383.  
  5384. align 4
  5385.  
  5386. stack_driver_stat:
  5387.  
  5388.      call  app_stack_handler            ; Stack status
  5389.  
  5390. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5391. ;     call  change_task                 ; between sent packet
  5392.  
  5393.      mov   [esp+36],eax
  5394.      ret
  5395.  
  5396. align 4
  5397.  
  5398. socket:                                 ; Socket interface
  5399.      call  app_socket_handler
  5400.  
  5401. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5402. ;     call  change_task                 ; between sent packet
  5403.  
  5404.      mov   [esp+36],eax
  5405.      mov   [esp+24],ebx
  5406.      ret
  5407.  
  5408. align 4
  5409.  
  5410. read_from_hd:                           ; Read from hd - fn not in use
  5411.  
  5412.      mov   edi,[TASK_BASE]
  5413.      add   edi,TASKDATA.mem_start
  5414.      add   eax,[edi]
  5415.      add   ecx,[edi]
  5416.      add   edx,[edi]
  5417.      call  file_read
  5418.  
  5419.      mov   [esp+36],eax
  5420.      mov   [esp+24],ebx
  5421.  
  5422.      ret
  5423.  
  5424. paleholder:
  5425.         ret
  5426.  
  5427. align 4
  5428. set_screen:
  5429.         cmp eax, [Screen_Max_X]
  5430.         jne .set
  5431.  
  5432.         cmp edx, [Screen_Max_Y]
  5433.         jne .set
  5434.         ret
  5435. .set:
  5436.         pushfd
  5437.         cli
  5438.  
  5439.         mov [Screen_Max_X], eax
  5440.         mov [Screen_Max_Y], edx
  5441.         mov [BytesPerScanLine], ecx
  5442.  
  5443.         mov [screen_workarea.right],eax
  5444.         mov [screen_workarea.bottom], edx
  5445.  
  5446.         push ebx
  5447.         push esi
  5448.         push edi
  5449.  
  5450.         pushad
  5451.  
  5452.         stdcall kernel_free, [_WinMapAddress]
  5453.  
  5454.         mov eax, [_display.width]
  5455.         mul [_display.height]
  5456.         mov [_WinMapSize], eax
  5457.  
  5458.         stdcall kernel_alloc, eax
  5459.         mov [_WinMapAddress], eax
  5460.         test eax, eax
  5461.         jz .epic_fail
  5462.  
  5463.         popad
  5464.  
  5465.         call    repos_windows
  5466.         xor eax, eax
  5467.         xor ebx, ebx
  5468.         mov     ecx, [Screen_Max_X]
  5469.         mov     edx, [Screen_Max_Y]
  5470.         call    calculatescreen
  5471.         pop edi
  5472.         pop esi
  5473.         pop ebx
  5474.  
  5475.         popfd
  5476.         ret
  5477.  
  5478. .epic_fail:
  5479.         hlt                     ; Houston, we've had a problem
  5480.  
  5481. ; --------------- APM ---------------------
  5482. apm_entry    dp    0
  5483. apm_vf        dd    0
  5484. align 4
  5485. sys_apm:
  5486.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5487.     jne    @f
  5488.     or     [esp + 48], byte 1    ; error
  5489.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5490.     ret
  5491.  
  5492. @@:
  5493.     xchg    eax, ecx
  5494.     xchg    ebx, ecx
  5495.  
  5496.     cmp    al, 3
  5497.     ja    @f
  5498.     and    [esp + 48], byte 0xfe    ; emulate func 0..3 as func 0
  5499.     mov    eax, [apm_vf]
  5500.     mov    [esp + 36], eax
  5501.     shr    eax, 16
  5502.     mov    [esp + 32], eax
  5503.     ret
  5504.  
  5505. @@:
  5506.  
  5507.     mov esi, [master_tab+(OS_BASE shr 20)]
  5508.     xchg [master_tab], esi
  5509.     push esi
  5510.     mov edi, cr3
  5511.     mov cr3, edi                 ;flush TLB
  5512.  
  5513.     call    pword [apm_entry]    ; call APM BIOS
  5514.  
  5515.     xchg eax, [esp]
  5516.     mov [master_tab], eax
  5517.     mov eax, cr3
  5518.     mov cr3, eax
  5519.     pop eax
  5520.  
  5521.     mov    [esp + 8 ], edi
  5522.     mov    [esp + 12], esi
  5523.     mov    [esp + 24], ebx
  5524.     mov    [esp + 28], edx
  5525.     mov    [esp + 32], ecx
  5526.     mov    [esp + 36], eax
  5527.     setc   al
  5528.     and    [esp + 48], byte 0xfe
  5529.     or     [esp + 48], al
  5530.  
  5531.  
  5532.     ret
  5533. ; -----------------------------------------
  5534.  
  5535. align 4
  5536.  
  5537. undefined_syscall:                      ; Undefined system call
  5538.      mov   [esp + 32], dword -1
  5539.      ret
  5540.  
  5541. align 4
  5542. system_shutdown:          ; shut down the system
  5543.  
  5544.            cmp byte [BOOT_VAR+0x9030], 1
  5545.            jne @F
  5546.            ret
  5547. @@:
  5548.            call stop_all_services
  5549.            push 3                ; stop playing cd
  5550.            pop  eax
  5551.            call sys_cd_audio
  5552.  
  5553. yes_shutdown_param:
  5554.            cli
  5555.  
  5556.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  5557.            push 12
  5558.            pop  esi
  5559.            xor  ebx,ebx
  5560.            or   ecx,-1
  5561.            mov  edx, OS_BASE+0x70000
  5562.            call fileread
  5563.  
  5564.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5565.            mov  edi,OS_BASE+0x40000
  5566.            mov  ecx,1000
  5567.            rep  movsb
  5568.  
  5569.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5570.            mov  edi, OS_BASE
  5571.            mov  ecx,0x10000/4
  5572.            cld
  5573.            rep movsd
  5574.  
  5575.            call restorefatchain
  5576.  
  5577.            mov al, 0xFF
  5578.            out 0x21, al
  5579.            out 0xA1, al
  5580.  
  5581. if 0
  5582.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5583.            mov  word [OS_BASE+0x467+2],0x1000
  5584.  
  5585.            mov  al,0x0F
  5586.            out  0x70,al
  5587.            mov  al,0x05
  5588.            out  0x71,al
  5589.  
  5590.            mov  al,0xFE
  5591.            out  0x64,al
  5592.  
  5593.            hlt
  5594.            jmp $-1
  5595.  
  5596. else
  5597.         cmp     byte [OS_BASE + 0x9030], 2
  5598.         jnz     no_acpi_power_off
  5599.  
  5600. ; scan for RSDP
  5601. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5602.         movzx   eax, word [OS_BASE + 0x40E]
  5603.         shl     eax, 4
  5604.         jz      @f
  5605.         mov     ecx, 1024/16
  5606.         call    scan_rsdp
  5607.         jnc     .rsdp_found
  5608. @@:
  5609. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5610.         mov     eax, 0xE0000
  5611.         mov     ecx, 0x2000
  5612.         call    scan_rsdp
  5613.         jc      no_acpi_power_off
  5614. .rsdp_found:
  5615.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5616.         mov     ebp, [ipc_tmp]
  5617.         stdcall map_page, ebp, esi, PG_MAP
  5618.         lea     eax, [esi+1000h]
  5619.         lea     edx, [ebp+1000h]
  5620.         stdcall map_page, edx, eax, PG_MAP
  5621.         and     esi, 0xFFF
  5622.         add     esi, ebp
  5623.         cmp     dword [esi], 'RSDT'
  5624.         jnz     no_acpi_power_off
  5625.         mov     ecx, [esi+4]
  5626.         sub     ecx, 24h
  5627.         jbe     no_acpi_power_off
  5628.         shr     ecx, 2
  5629.         add     esi, 24h
  5630. .scan_fadt:
  5631.         lodsd
  5632.         mov     ebx, eax
  5633.         lea     eax, [ebp+2000h]
  5634.         stdcall map_page, eax, ebx, PG_MAP
  5635.         lea     eax, [ebp+3000h]
  5636.         add     ebx, 0x1000
  5637.         stdcall map_page, eax, ebx, PG_MAP
  5638.         and     ebx, 0xFFF
  5639.         lea     ebx, [ebx+ebp+2000h]
  5640.         cmp     dword [ebx], 'FACP'
  5641.         jz      .fadt_found
  5642.         loop    .scan_fadt
  5643.         jmp     no_acpi_power_off
  5644. .fadt_found:
  5645. ; ebx is linear address of FADT
  5646.         mov     edi, [ebx+40] ; physical address of the DSDT
  5647.         lea     eax, [ebp+4000h]
  5648.         stdcall map_page, eax, edi, PG_MAP
  5649.         lea     eax, [ebp+5000h]
  5650.         lea     esi, [edi+0x1000]
  5651.         stdcall map_page, eax, esi, PG_MAP
  5652.         and     esi, 0xFFF
  5653.         sub     edi, esi
  5654.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5655.         jnz     no_acpi_power_off
  5656.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5657.         sub     eax, 36+4
  5658.         jbe     no_acpi_power_off
  5659.         add     esi, 36
  5660. .scan_dsdt:
  5661.         cmp     dword [esi+ebp+4000h], '_S5_'
  5662.         jnz     .scan_dsdt_cont
  5663.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5664.         jnz     .scan_dsdt_cont
  5665.         mov     dl, [esi+ebp+4000h+6]
  5666.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5667.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5668.         ja      .scan_dsdt_cont
  5669.         cmp     dl, 1
  5670.         jb      .scan_dsdt_cont
  5671.         lea     esi, [esi+ebp+4000h+7]
  5672.         xor     ecx, ecx
  5673.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5674.         jz      @f
  5675.         cmp     byte [esi], 0xA
  5676.         jnz     no_acpi_power_off
  5677.         inc     esi
  5678.         mov     cl, [esi]
  5679. @@:
  5680.         inc     esi
  5681.         cmp     dl, 2
  5682.         jb      @f
  5683.         cmp     byte [esi], 0
  5684.         jz      @f
  5685.         cmp     byte [esi], 0xA
  5686.         jnz     no_acpi_power_off
  5687.         inc     esi
  5688.         mov     ch, [esi]
  5689. @@:
  5690.         jmp     do_acpi_power_off
  5691. .scan_dsdt_cont:
  5692.         inc     esi
  5693.         cmp     esi, 0x1000
  5694.         jb      @f
  5695.         sub     esi, 0x1000
  5696.         add     edi, 0x1000
  5697.         push    eax
  5698.         lea     eax, [ebp+4000h]
  5699.         stdcall map_page, eax, edi, PG_MAP
  5700.         push    PG_MAP
  5701.         lea     eax, [edi+1000h]
  5702.         push    eax
  5703.         lea     eax, [ebp+5000h]
  5704.         push    eax
  5705.         stdcall map_page
  5706.         pop     eax
  5707. @@:
  5708.         dec     eax
  5709.         jnz     .scan_dsdt
  5710.         jmp     no_acpi_power_off
  5711. do_acpi_power_off:
  5712.         mov     edx, [ebx+48]
  5713.         test    edx, edx
  5714.         jz      .nosmi
  5715.         mov     al, [ebx+52]
  5716.         out     dx, al
  5717.         mov     edx, [ebx+64]
  5718. @@:
  5719.         in      ax, dx
  5720.         test    al, 1
  5721.         jz      @b
  5722. .nosmi:
  5723.         and     cx, 0x0707
  5724.         shl     cx, 2
  5725.         or      cx, 0x2020
  5726.         mov     edx, [ebx+64]
  5727.         in      ax, dx
  5728.         and     ax, 203h
  5729.         or      ah, cl
  5730.         out     dx, ax
  5731.         mov     edx, [ebx+68]
  5732.         test    edx, edx
  5733.         jz      @f
  5734.         in      ax, dx
  5735.         and     ax, 203h
  5736.         or      ah, ch
  5737.         out     dx, ax
  5738. @@:
  5739.         jmp     $
  5740.  
  5741.  
  5742. no_acpi_power_off:
  5743.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5744.            mov  word [OS_BASE+0x467+2],0x1000
  5745.  
  5746.            mov  al,0x0F
  5747.            out  0x70,al
  5748.            mov  al,0x05
  5749.            out  0x71,al
  5750.  
  5751.            mov  al,0xFE
  5752.            out  0x64,al
  5753.  
  5754.            hlt
  5755.            jmp $-1
  5756.  
  5757. scan_rsdp:
  5758.         add     eax, OS_BASE
  5759. .s:
  5760.         cmp     dword [eax], 'RSD '
  5761.         jnz     .n
  5762.         cmp     dword [eax+4], 'PTR '
  5763.         jnz     .n
  5764.         xor     edx, edx
  5765.         xor     esi, esi
  5766. @@:
  5767.         add     dl, [eax+esi]
  5768.         inc     esi
  5769.         cmp     esi, 20
  5770.         jnz     @b
  5771.         test    dl, dl
  5772.         jz      .ok
  5773. .n:
  5774.         add     eax, 10h
  5775.         loop    .s
  5776.         stc
  5777. .ok:
  5778.         ret
  5779. end if
  5780.  
  5781. include "data32.inc"
  5782.  
  5783. __REV__ = __REV
  5784.  
  5785. uglobals_size = $ - endofcode
  5786. diff16 "end of kernel code",0,$
  5787.