This project is read-only.

How to enable the mouse?

Oct 13, 2010 at 2:47 AM

Does anyone know how to enable the mouse? it doesn't have to do anything I just want to get it working.

Oct 13, 2010 at 12:58 PM
Edited Oct 13, 2010 at 1:39 PM

 

FIRST CREATE A FILE CALLED mouse1.cs
then paste this:



using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.Kernel;

namespace Cosmos.Hardware
{ public static class mouse1 { public static int X, Y; public static MouseState Buttons; public static void Initialize() { //enable mouse WaitSignal(); CPUBus.Write8(0x64, 0xA8); // enable interrupt WaitSignal(); CPUBus.Write8(0x64, 0x20); WaitData(); byte status = (byte)(CPUBus.Read8(0x60) | 2); WaitSignal(); CPUBus.Write8(0x64, 0x60); WaitSignal(); CPUBus.Write8(0x60, status); //default Write(0xF6); Read(); //Acknowledge //Enable the mouse Write(0xF4); Read(); //Acknowledge Interrupts.AddIRQHandler(12, new Interrupts.InterruptDelegate(HandleMouse)); } private static byte Read() { WaitData(); return CPUBus.Read8(0x60); } private static void Write(byte b) { //Wait to be able to send a command WaitSignal(); //Tell the mouse we are sending a command CPUBus.Write8(0x64, 0xD4); //Wait for the final part WaitSignal(); //Finally write CPUBus.Write8(0x60, b); } private static void WaitData() { for (int i = 0; i < 1000 & ((CPUBus.Read8(0x64) & 1) == 1); i++) ; } private static void WaitSignal() { for (int i = 0; i < 1000 & ((CPUBus.Read8(0x64) & 2) != 0); i++) ; } public enum MouseState { None=0, Left=1, Right =2, Middle =4 } private static byte mouse_cycle =0; private static int[] mouse_byte = new int[4]; public static void HandleMouse(ref Interrupts.InterruptContext context) { switch (mouse_cycle) { case 0: mouse_byte[0] = CPUBus.Read8(0x60); if ((mouse_byte[0] & 0x8) == 0x8) mouse_cycle++; break; case 1: mouse_byte[1] = CPUBus.Read8(0x60); mouse_cycle++; break; case 2: mouse_byte[2] = CPUBus.Read8(0x60); mouse_cycle = 0; if ((mouse_byte[0] & 0x10) == 0x10) X -= mouse_byte[1] ^ 0xff; else X += mouse_byte[1]; if ((mouse_byte[0] & 0x20) == 0x20) Y += mouse_byte[2] ^ 0xff; else Y -= mouse_byte[2]; if (X < 0) X = 0; else if (X > 319) X = 319; if (Y < 0) Y = 0; else if (Y > 199) Y = 199; Buttons = (MouseState)(mouse_byte[0] & 0x7); break; } } } }
next
goto program.cs
and write follow:

 public static void Init()        
{            
             var xBoot = new Cosmos.Sys.Boot();            
             xBoot.Execute();
            mouse1.Initialize();
            VGAScreen.SetMode320x200x8();
            VGAScreen.SetPaletteEntry(0, 0, 0, 0);
            VGAScreen.SetPaletteEntry(1, 63, 0, 0);
            VGAScreen.SetPaletteEntry(2, 63, 63, 63);
            VGAScreen.SetPaletteEntry(3, 63, 63, 0);
            VGAScreen.SetPaletteEntry(4, 63, 0, 63);
            VGAScreen.Clear(0);
            uint x = (uint)mouse1.X;
            uint y = (uint)mouse1.Y;
            uint oc = 0;

 

while (true)
{

 


#region Mouse Data
                uint mx = (uint)mouse1.X;
                uint my = (uint)mouse1.Y;

                if (mx != x || my != y)
                {

                    if (mouse1.Buttons == mouse1.MouseState.Left)
                    {
                        VGAScreen.SetPixel(mx, my, 1);
                    }
                    else
                    {
                        VGAScreen.SetPixel320x200x8(x, y, oc);
                        VGAScreen.SetPixel320x200x8(x + 1, y, oc);
                        VGAScreen.SetPixel320x200x8(x + 1, y, oc);
                        VGAScreen.SetPixel320x200x8(x + 2, y, oc);
                        VGAScreen.SetPixel320x200x8(x, y + 1, oc);
                        VGAScreen.SetPixel320x200x8(x, y + 2, oc);
                        VGAScreen.SetPixel320x200x8(x + 1, y + 1, oc);
                        VGAScreen.SetPixel320x200x8(x + 2, y + 2, oc);
                        VGAScreen.SetPixel320x200x8(x + 3, y + 3, oc);
                        x = mx;
                        y = my;
                        oc = VGAScreen.GetPixel320x200x8(x, y);
                    }
                }
                VGAScreen.SetPixel320x200x8(mx, my, 4);
                VGAScreen.SetPixel320x200x8(mx + 1, my, 4);
                VGAScreen.SetPixel320x200x8(mx + 1, my, 4);
                VGAScreen.SetPixel320x200x8(mx + 2, my, 4);
                VGAScreen.SetPixel320x200x8(mx, my + 1, 4);
                VGAScreen.SetPixel320x200x8(mx, my + 2, 4);
                VGAScreen.SetPixel320x200x8(mx + 1, my + 1, 4);
                VGAScreen.SetPixel320x200x8(mx + 2, my + 2, 4);
                VGAScreen.SetPixel320x200x8(mx + 3, my + 3, 4);
                #endregion
}

 

 

Oct 13, 2010 at 10:29 PM

the only problem with you code is that if it changes color on the right side it will draw a line or the prevoius color

Oct 14, 2010 at 8:37 AM
How do I fix it?
Jan 2, 2011 at 2:39 PM

You need to keep track of all the old colors (previous colors) of the cursor. And replace them on the same way. Like this:

while (true)
            {
                uint mx = (uint)Mouse.X;
                uint my = (uint)Mouse.Y;

                if (mx != x || my != y)
                {
                    VGAScreen.SetPixel320x200x8(x, y, oc1);
                    VGAScreen.SetPixel320x200x8(x + 1, y, oc2);
                    VGAScreen.SetPixel320x200x8(x + 2, y, oc3);
                    VGAScreen.SetPixel320x200x8(x, y + 1, oc4);
                    VGAScreen.SetPixel320x200x8(x, y + 2, oc5);
                    VGAScreen.SetPixel320x200x8(x + 1, y + 1, oc6);
                    VGAScreen.SetPixel320x200x8(x + 2, y + 2, oc7);
                    VGAScreen.SetPixel320x200x8(x + 3, y + 3, oc8);
                    x = mx;
                    y = my;
                    oc1 = VGAScreen.GetPixel320x200x8(x, y);
                    oc2 = VGAScreen.GetPixel320x200x8(x + 1, y);
                    oc3 = VGAScreen.GetPixel320x200x8(x + 2, y);
                    oc4 = VGAScreen.GetPixel320x200x8(x, y + 1);
                    oc5 = VGAScreen.GetPixel320x200x8(x, y + 2);
                    oc6 = VGAScreen.GetPixel320x200x8(x + 1, y + 1);
                    oc7 = VGAScreen.GetPixel320x200x8(x + 2, y + 2);
                    oc8 = VGAScreen.GetPixel320x200x8(x + 3, y + 3);
                }
                VGAScreen.SetPixel320x200x8(mx, my, 6);
                VGAScreen.SetPixel320x200x8(mx + 1, my, 6);
                VGAScreen.SetPixel320x200x8(mx + 2, my, 6);
                VGAScreen.SetPixel320x200x8(mx, my + 1, 6);
                VGAScreen.SetPixel320x200x8(mx, my + 2, 6);
                VGAScreen.SetPixel320x200x8(mx + 1, my + 1, 6);
                VGAScreen.SetPixel320x200x8(mx + 2, my + 2, 6);
                VGAScreen.SetPixel320x200x8(mx + 3, my + 3, 6);
            }
}
Jun 1, 2011 at 8:33 PM

i am using the latest cosmos kit and i am getting the problem in the line ....

 

"

 public static void HandleMouse(ref Interrupts.InterruptContext context)

"
the error is
"
D:\Misc 2\CosmosKernel3\CosmosKernel3\mouse1.cs(85,44): error CS0246: The type or namespace name 'Interrupts' could not be found (are you missing a using directive or an assembly reference?)
"

help please ...!!!!
Jun 1, 2011 at 9:49 PM
Edited Jun 1, 2011 at 9:50 PM

Well, currently the mouse doesn't work in the trunk version, but here is my current copy:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPUBus = Cosmos.Core.Plugs.IOPortImpl;
using Cosmos.Core;
using GuessKernel;
//using Cosmos.Kernel;

namespace Cosmos.Hardware
{
    /// <summary>
    /// This class describes the mouse.
    /// </summary>
    public class Mouse
    {
        private IOPort p60 = new IOPort(0x60);
        private IOPort p64 = new IOPort(0x64);
        /// <summary>
        /// The X location of the mouse.
        /// </summary>
        public int X;
        /// <summary>
        /// The Y location of the mouse.
        /// </summary>
        public int Y;
        /// <summary>
        /// The state the mouse is currently in.
        /// </summary>
        public MouseState Buttons;

        /// <summary>
        /// This is the required call to start
        /// the mouse receiving interrupts.
        /// </summary>
        public void Initialize()
        {
            WaitSignal();
            p64.Byte = 0xA8;

            //Enable the interrupts
            WaitSignal();
            p64.Byte =  0x20;
            WaitData();
            byte status = (byte)(p60.Byte | 2);
            WaitSignal();
            p64.Byte = 0x60;
            WaitSignal();
            p60.Byte =  status;

            //Tell the mouse to use default settings 
            Write(0xF6);
            Read();  //Acknowledge

            //Enable the mouse
            Write(0xF4);
            Read();  //Acknowledge

            Cosmos.Core.INTs.SetIrqHandler(9, new INTs.InterruptDelegate(HandleMouse));
            Cosmos.Core.INTs.SetIrqHandler(11, new INTs.InterruptDelegate(HandleMouse));
            Cosmos.Core.INTs.SetIrqHandler(12, new INTs.InterruptDelegate(HandleMouse));
            Cosmos.Core.INTs.SetIrqHandler(14, new INTs.InterruptDelegate(HandleMouse));
            Cosmos.Core.INTs.SetIrqHandler(15, new INTs.InterruptDelegate(HandleMouse));


        }

        private byte Read()
        {
            WaitData();
            return p60.Byte;
        }

        private void Write(byte b)
        {
            //Wait to be able to send a command
            WaitSignal();
            //Tell the mouse we are sending a command
            p64.Byte = 0xD4;
            //Wait for the final part
            WaitSignal();
            //Finally write
            p60.Byte =  b;
        }

        public bool TimedOut = false;
        public bool TimedOut2 = false;

        private void WaitData()
        {
            for (int i = 0; i < 100000 && ((p64.Byte & 1) != 1); i++)
            {
                if (i == 99999)
                    TimedOut = true;
            }
        }

        private void WaitSignal()
        {
            for (int i = 0; i < 100000 && ((p64.Byte & 2) != 0); i++)
            {
                if (i == 99999)
                    TimedOut2 = true;
            }
        }

        /// <summary>
        /// The possible states of a mouse.
        /// </summary>
        public enum MouseState
        {
            /// <summary>
            /// No button is pressed.
            /// </summary>
            None=0,
            /// <summary>
            /// The left mouse button is pressed.
            /// </summary>
            Left=1,
            /// <summary>
            /// The right mouse button is pressed.
            /// </summary>
            Right =2,
            /// <summary>
            /// The middle mouse button is pressed.
            /// </summary>
            Middle =4
        }

        private byte mouse_cycle =0;
        private int[] mouse_byte = new int[4];

        /// <summary>
        /// This is the default mouse handling code.
        /// </summary>
        /// <param name="context"></param>
        public void HandleMouse(ref INTs.IRQContext context)
        {
            switch (mouse_cycle)
            {
                case 0:
                    mouse_byte[0] = p60.Byte;

                    if ((mouse_byte[0] & 0x8) == 0x8)
                        mouse_cycle++;

                    break;
                case 1:
                    mouse_byte[1] = p60.Byte;
                    mouse_cycle++;
                    break;
                case 2:
                    mouse_byte[2] = p60.Byte;
                    mouse_cycle = 0;

                    if ((mouse_byte[0] & 0x10) == 0x10)
                        X -= mouse_byte[1] ^ 0xff;
                    else
                        X += mouse_byte[1];

                    if ((mouse_byte[0] & 0x20) == 0x20)
                        Y += mouse_byte[2] ^ 0xff;
                    else
                        Y -= mouse_byte[2];

                    if (X < 0)
                        X = 0;
                    else if (X > 319)
                        X = 319;

                    if (Y < 0)
                        Y = 0;
                    else if (Y > 199)
                        Y = 199;

                    Buttons = (MouseState)(mouse_byte[0] & 0x7);

                    break;
            }
        }
    }
}

That will build, however it doesn't work. I'm currently in the process of getting it working. I'll post the working version when I actually have it working.
Jun 1, 2011 at 10:16 PM
On 6/1/2011 4:49 PM, blah38621 wrote:
> From: blah38621

blah38621 can you contact me offlist or on skype?
Jun 1, 2011 at 11:29 PM
Edited Jun 2, 2011 at 12:37 AM

 

Kudzu: Email sent.

 

btw, currently trying a version of this code based in X#.

Also, don't get your hopes up on it as I've never coded in ASM before XD so I have no idea what (some) of the instructions mean :P made harder due to the quirks of X# :P

 

and one last thing, this time a question. "push ecx edx" is the same thing as "push ecx" then on the next line "push edx" right? (I'm having to work around X# limitations :P)

Jun 2, 2011 at 4:00 PM
On 6/1/2011 6:29 PM, blah38621 wrote:
> From: blah38621
>
> Email sent.

Didnt get it.... You can use the message feature at CodePlex to send me
your direct email.