Page 1 of 1

PS/2 Mouse being buggy on real hardware

Posted: Mon Dec 23, 2013 4:28 am
by PearOs
Hey guys! So I attempted a "PS/2 Mouse Driver" today and it works in Bochs and VMWare, but fails to work on Real Hardware! I tried to boot it on two laptops that have touch pads. In the past a Standard PS/2 driver used to work with those but my driver isn't.. oddly enough after a few reboots it started working? So I am guessing maybe there's some timing off or something. I read the wiki, but I can't really find any real problems in my driver. I would appreciate some help on this issue.

Code:

Code: Select all

private static byte Cycle = 0;
        private static Array MouseData = new Array(3, Array.ArrayType.Byte);
        public static uint MouseX = 0;
        public static uint MouseY = 0;
        /// <summary>
        /// Setups the Mouse.
        /// </summary>
        public static void Setup()
        {
            byte status = 0;
            //Enable the auxiliary mouse device
            WaitSignal();
            IOPort.Write_8(0x64, 0xA8);
            //Now disable automatic packet streaming
            Write(0xF5);
            //Lets see if the mouse is alive.
            Write(0xEB);
            WaitData();
            if (IOPort.Read_8(0x60) != 0xFA)
            {
                Console.WriteLine("Mouse may be disabled!");
            }
            //Enable the interrupts
            WaitSignal();
            IOPort.Write_8(0x64, 0x20);

            WaitData();
            status = (byte)(IOPort.Read_8(0x60) | 2);


            WaitSignal();
            IOPort.Write_8(0x64, 0x60);

            WaitSignal();
            IOPort.Write_8(0x60, status);
            //Tell the mouse to use default settings
            Write(0xF6);
            //Enable the mouse
            Write(0xF4);
            //Setup the IRQ Handler.
            IRQ.Mask(12, HandleMouse);
            Console.WriteLine("Cursor Setup!");
        }
        /// <summary>
        /// Writes a byte to the Cursor.
        /// </summary>
        /// <param name="b"></param>
        private static void Write(byte b)
        {
            //Wait to be able to send a command.
            WaitSignal();
            //Tell the mouse we are sending a command.
            IOPort.Write_8(0x64, 0xD4);
            //Wait for the final part.
            WaitSignal();
            //Finally write
            IOPort.Write_8(0x60, b);
            //Now wait for a acknowledgement.
            while (IOPort.Read_8(0x60) != 0xFA) { }
        }
        /// <summary>
        /// Reads a byte from the Cursor
        /// </summary>
        /// <returns></returns>
        private static byte Read()
        {
            WaitData();
            return IOPort.Read_8(0x60);
        }
        /// <summary>
        /// Waits until the Mouse is ready for Data.
        /// </summary>
        private static void WaitData()
        {
            for (int i = 0; i < 1000 & ((IOPort.Read_8(0x64) & 1) == 1); i++)
            {
            }
        }
        /// <summary>
        /// Waits until the Mouse Signal is ready.
        /// </summary>
        private static void WaitSignal()
        {
            for (int i = 0; i < 1000 & ((IOPort.Read_8(0x64 & 2)) != 0); i++)
            {
            }
        }
        /// <summary>
        /// Handles the Mouse on interrupt.
        /// </summary>
        private static void HandleMouse()
        {
            Console.Print('M');
            switch (Cycle)
            {
                case 0:
                    MouseData.Set(0, (uint)Read());
                    Cycle++;
                    break;
                case 1:
                    MouseData.Set(1, (uint)Read());
                    Cycle++;
                    break;
                case 2:
                    MouseData.Set(2, (uint)Read());
                    Cycle = 0;
                    if (((byte)MouseData.Get((uint)0) & 0x10) == 0x10)
                    {
                        MouseX -= (uint)((byte)MouseData.Get(1) ^ 0xFF);
                    }
                    else
                    {
                        MouseX += (uint)MouseData.Get(1);
                    }

                    if (((byte)MouseData.Get((uint)0) & 0x20) == 0x20)
                    {
                        MouseY += (uint)((byte)MouseData.Get((uint)2) ^ 0xFF);
                    }
                    else
                    {
                        MouseY -= (uint)MouseData.Get(2);
                    }
                    break;
            }
        }
Thanks guys, Matt

Re: PS/2 Mouse being buggy on real hardware

Posted: Mon Dec 23, 2013 8:23 pm
by blanham
I think it might be a problem here:

[quote]

Code: Select all

 /// <summary>
        /// Waits until the Mouse is ready for Data.
        /// </summary>
        private static void WaitData()
        {
            for (int i = 0; i < 1000 & ((IOPort.Read_8(0x64) & 1) == 1); i++)
            {
            }
        }
        /// <summary>
        /// Waits until the Mouse Signal is ready.
        /// </summary>
        private static void WaitSignal()
        {
            for (int i = 0; i < 1000 & ((IOPort.Read_8(0x64 & 2)) != 0); i++)
            {
            }
        }
[/quote
You're using bitwise AND (&) instead of logical AND (&&) in the for loop comparisons. Add parens around i < 1000 and change & to && and it might take care of your problem, since it's likely because of this that these two functions are returning immediately instead of waiting.