Subversion Repositories Kolibri OS

Rev

Rev 1376 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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