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;
}
}