Subversion Repositories Kolibri OS

Rev

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

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