Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Implement IRQ (Hardware Interrupts) into ARM core and also add CPU ty…
Browse files Browse the repository at this point in the history
…pe into IO data.

Thanks Wolfvak, Normmatt for help with IRQ!
  • Loading branch information
Starlet committed Aug 25, 2018
1 parent ae62a10 commit 57eea36
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
20 changes: 20 additions & 0 deletions LemonLime.ARM/Interpreter.cs
Expand Up @@ -39,6 +39,11 @@ public Interpreter(IBus Bus, bool HighVectors = false)
/// </summary>
public event EventHandler<SoftwareInterruptEventArgs> OnSoftwareInterrupt;

/// <summary>
/// Whether to do a Hardware Interrupt or not
/// </summary>
public bool IRQ = false;

/// <summary>
/// Resets the CPU, and sets the Registers to the initial state.
/// </summary>
Expand Down Expand Up @@ -80,6 +85,21 @@ public void ReloadPipeline()
/// </summary>
public void Execute()
{
if (IRQ)
{
uint SPSR = Registers.CPSR;
Registers.Mode = ARMMode.IRQ;
Registers[14] = Registers[15] - (uint)(Registers.IsFlagSet(ARMFlag.Thumb) ? 0 : 4);
Registers.SPSR = SPSR;
Registers.SetFlag(ARMFlag.Thumb, false);
Registers.SetFlag(ARMFlag.IRQDisable, true);
Registers.SetFlag(ARMFlag.Endianness, false);

Registers[15] = HighVectors ? 0xffff0000 + 24 : 24;

IRQ = false;
}

uint TruePC = Registers[15] - 4;

if (Registers.IsFlagSet(ARMFlag.Thumb))
Expand Down
18 changes: 18 additions & 0 deletions LemonLime.CTR/CPUHandler.cs
Expand Up @@ -62,6 +62,24 @@ public void EnableCPU(CPUType Type, bool Enabled)
}
}

public void EnableIRQ(CPUType Type)
{
switch (Type)
{
case CPUType.ARM9:
if (ARM9_Enabled != true) throw new Exception("ARM9 is not enabled.");
Logger.WriteInfo("Enabling ARM9 IRQ.");
ARM9.IRQ = true;
break;

case CPUType.ARM11:
if (ARM11_Enabled != true) throw new Exception("ARM11 is not enabled.");
Logger.WriteInfo("Enabling ARM11 IRQ.");
ARM11.IRQ = true;
break;
}
}

public void Start()
{
ARM9_Thread.Start();
Expand Down
10 changes: 6 additions & 4 deletions LemonLime.CTR/IO/IOData.cs
Expand Up @@ -3,6 +3,7 @@
class IOData
{
public CPUHandler CPU;
public CPUType CPUType;
public uint Address;
public IOType Type;
public IOWidth Width;
Expand All @@ -13,12 +14,13 @@ class IOData
public ushort Read16;
public uint Read32;

public IOData(CPUHandler CPU, uint Address,
IOType Type, IOWidth Width,
byte Write8 = 0, ushort Write16 = 0,
uint Write32 = 0)
public IOData(CPUHandler CPU, CPUType CPUType,
uint Address, IOType Type,
IOWidth Width, byte Write8 = 0,
ushort Write16 = 0, uint Write32 = 0)
{
this.CPU = CPU;
this.CPUType = CPUType;
this.Address = Address;
this.Type = Type;
this.Width = Width;
Expand Down
2 changes: 0 additions & 2 deletions LemonLime.CTR/IO/IOHandler.cs
Expand Up @@ -8,12 +8,10 @@ namespace LemonLime.CTR.IO
{
class IOHandler
{
// IO Entries
private List<IOEntry> Entries;

public IOHandler()
{
// Our IO entries
Entries = new List<IOEntry>
{
// CFG9
Expand Down
12 changes: 6 additions & 6 deletions LemonLime.CTR/Memory.cs
Expand Up @@ -80,7 +80,7 @@ public byte ReadUInt8(uint Address)
}
}

IOData IOInfo = new IOData(CPU, Address, IOType.Read, IOWidth.Width1);
IOData IOInfo = new IOData(CPU, Type, Address, IOType.Read, IOWidth.Width1);
IO.Call(IOInfo);
return IOInfo.Read8;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public ushort ReadUInt16(uint Address)
}
}

IOData IOInfo = new IOData(CPU, Address, IOType.Read, IOWidth.Width2);
IOData IOInfo = new IOData(CPU, Type, Address, IOType.Read, IOWidth.Width2);
IO.Call(IOInfo);
return IOInfo.Read16;
}
Expand All @@ -154,7 +154,7 @@ public uint ReadUInt32(uint Address)
}
}

IOData IOInfo = new IOData(CPU, Address, IOType.Read, IOWidth.Width4);
IOData IOInfo = new IOData(CPU, Type, Address, IOType.Read, IOWidth.Width4);
IO.Call(IOInfo);
return IOInfo.Read32;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public void WriteUInt8(uint Address, byte Value)
}
}

IO.Call(new IOData(CPU, Address, IOType.Write, IOWidth.Width1, Value));
IO.Call(new IOData(CPU, Type, Address, IOType.Write, IOWidth.Width1, Value));
return;
}
else if (Address >= 0x1FF80000 && Address < 0x1FF80000 + 0x00080000)
Expand Down Expand Up @@ -229,7 +229,7 @@ public void WriteUInt16(uint Address, ushort Value)
}
}

IO.Call(new IOData(CPU, Address, IOType.Write, IOWidth.Width2, 0, Value));
IO.Call(new IOData(CPU, Type, Address, IOType.Write, IOWidth.Width2, 0, Value));
return;
}

Expand All @@ -249,7 +249,7 @@ public void WriteUInt32(uint Address, uint Value)
}
}

IO.Call(new IOData(CPU, Address, IOType.Write, IOWidth.Width4, 0, 0, Value));
IO.Call(new IOData(CPU, Type, Address, IOType.Write, IOWidth.Width4, 0, 0, Value));
return;
}

Expand Down

0 comments on commit 57eea36

Please sign in to comment.