Skip to content

Commit

Permalink
Better debugging support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwhitney committed Apr 16, 2020
1 parent 2a3e3d5 commit 9a599c5
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 37 deletions.
2 changes: 1 addition & 1 deletion CoreBoy.Test.Integration/Blargg/Individual/CgbSoundTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CoreBoy.Test.Integration.Blargg.Individual
{
[TestFixture, Timeout(1000 * 60)]
[TestFixture, Timeout(1000 * 60 * 5)]
public class CgbSoundTest
{
public static object[] RomsFrom => ParametersProvider.getParameters("blargg\\cgb_sound");
Expand Down
2 changes: 1 addition & 1 deletion CoreBoy.Test.Integration/Blargg/Individual/OamBug2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CoreBoy.Test.Integration.Blargg.Individual
{
[TestFixture, Timeout(1000 * 60 * 1)]
[TestFixture, Timeout(1000 * 60 * 3)]
public class OamBug2Test
{
public static object[] RomsFrom => ParametersProvider.getParameters("blargg/oam_bug-2");
Expand Down
2 changes: 2 additions & 0 deletions CoreBoy.Test.Integration/Mooneye/GeneralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class GeneralTest
[TestCaseSource(nameof(RomsFrom))]
public void Execute(string filePath)
{
// 9 JVM failures.

var rom = new FileInfo(filePath);
RomTestUtils.testMooneyeRom(rom);
}
Expand Down
1 change: 1 addition & 0 deletions CoreBoy.Test.Integration/Mooneye/InterruptsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CoreBoy.Test.Integration.Mooneye
{
[Ignore("JVMFailed")]
[TestFixture, Timeout(1000 * 60)]
public class InterruptsTest
{
Expand Down
3 changes: 3 additions & 0 deletions CoreBoy.Test.Integration/Mooneye/MiscTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CoreBoy.Test.Integration.Mooneye
{
[Ignore("JVMFailed")]
[TestFixture, Timeout(1000 * 60)]
public class MiscTest
{
Expand All @@ -13,6 +14,8 @@ public class MiscTest
[TestCaseSource(nameof(RomsFrom))]
public void Execute(string filePath)
{
// Three of these tests fail in the JVM original.

var rom = new FileInfo(filePath);
RomTestUtils.testMooneyeRom(rom);
}
Expand Down
3 changes: 3 additions & 0 deletions CoreBoy.Test.Integration/Mooneye/PpuTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CoreBoy.Test.Integration.Mooneye
{
[Ignore("JVMFailed")]
[TestFixture, Timeout(1000 * 60)]
public class PpuTest
{
Expand All @@ -13,6 +14,8 @@ public class PpuTest
[TestCaseSource(nameof(RomsFrom))]
public void Execute(string filePath)
{
// All but the final test fails in the JVM

var rom = new FileInfo(filePath);
RomTestUtils.testMooneyeRom(rom);
}
Expand Down
1 change: 1 addition & 0 deletions CoreBoy.Test.Integration/Mooneye/SerialTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CoreBoy.Test.Integration.Mooneye
{
[Ignore("JVMFailed")]
[TestFixture, Timeout(1000 * 60)]
public class SerialTest
{
Expand Down
63 changes: 36 additions & 27 deletions CoreBoy.Test.Integration/Support/MemoryTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,51 @@ namespace CoreBoy.Test.Integration.Support
{
public class MemoryTestRunner
{
private readonly Gameboy gb;
private readonly StringBuilder text;
private readonly TextWriter os;
private bool testStarted;
private readonly Gameboy _gb;
private readonly StringBuilder _text;
private readonly TextWriter _os;
private bool _testStarted;
private ITracer _tracer;

public MemoryTestRunner(FileInfo romFileInfo, TextWriter os)
public MemoryTestRunner(FileInfo romFileInfo, TextWriter os, bool trace = false)
{
_tracer = trace ? (ITracer)new Tracer(romFileInfo.Name) : new NullTracer();

var options = new GameboyOptions(romFileInfo);
var cart = new Cartridge(options);
gb = new Gameboy(options, cart, new NullDisplay(), new NullController(), new NullSoundOutput(),
_gb = new Gameboy(options, cart, new NullDisplay(), new NullController(), new NullSoundOutput(),
new NullSerialEndpoint());
text = new StringBuilder();
this.os = os;
_text = new StringBuilder();
this._os = os;
}

public TestResult runTest()
public TestResult RunTest()
{
_tracer.Collect(_gb.Cpu.Registers);

int status = 0x80;
int divider = 0;
while (status == 0x80 && !SerialTestRunner.IsInfiniteLoop(gb))
while (status == 0x80 && !SerialTestRunner.IsInfiniteLoop(_gb))
{
gb.Tick();
if (++divider >= (gb.SpeedMode.GetSpeedMode() == 2 ? 1 : 4))
_gb.Tick();
if (++divider >= (_gb.SpeedMode.GetSpeedMode() == 2 ? 1 : 4))
{
status = getTestResult(gb);
status = GetTestResult(_gb);
divider = 0;
}

_tracer.Collect(_gb.Cpu.Registers);
}

return new TestResult(status, text.ToString());
_tracer.Save();

return new TestResult(status, _text.ToString());
}

private int getTestResult(Gameboy gb)
private int GetTestResult(Gameboy gb)
{
IAddressSpace mem = gb.Mmu;
if (!testStarted)
if (!_testStarted)
{
var i = 0xa000;
foreach (var v in new[] {0x80, 0xde, 0xb0, 0x61})
Expand All @@ -57,7 +66,7 @@ private int getTestResult(Gameboy gb)
}
}

testStarted = true;
_testStarted = true;
}

int status = mem.GetByte(0xa000);
Expand All @@ -79,8 +88,8 @@ private int getTestResult(Gameboy gb)
}

var c = (char) reg.A;
text.Append(c);
os?.Write(c);
_text.Append(c);
_os?.Write(c);

reg.PC += 0x19;
return status;
Expand All @@ -89,24 +98,24 @@ private int getTestResult(Gameboy gb)
public class TestResult
{

private readonly int status;
private readonly int _status;

private readonly string text;
private readonly string _text;

public TestResult(int status, string text)
{
this.status = status;
this.text = text;
this._status = status;
this._text = text;
}

public int getStatus()
public int GetStatus()
{
return status;
return _status;
}

public string getText()
public string GetText()
{
return text;
return _text;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions CoreBoy.Test.Integration/Support/RomTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace CoreBoy.Test.Integration.Support
{
public class RomTestUtils
{
public static void testRomWithMemory(FileInfo romFileInfoInfo)
public static void testRomWithMemory(FileInfo romFileInfoInfo, bool trace = false)
{
Console.WriteLine($"\n### Running test rom {romFileInfoInfo.FullName} ###");
var runner = new MemoryTestRunner(romFileInfoInfo, Console.Out);
var runner = new MemoryTestRunner(romFileInfoInfo, Console.Out, trace);

var result = runner.runTest();
var result = runner.RunTest();

Assert.AreEqual(0, result.getStatus(), "Non-zero return value");
Assert.AreEqual(0, result.GetStatus(), "Non-zero return value");
}

public static void testRomWithSerial(FileInfo romFileInfoInfo, bool trace = false)
Expand Down
14 changes: 10 additions & 4 deletions CoreBoy.Test.Integration/Support/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ namespace CoreBoy.Test.Integration.Support
public class Tracer : ITracer
{
private int Counter { set; get; }
private readonly string _filename;
private readonly StringBuilder _log;
private readonly StreamWriter _outputFile;

public Tracer(string filename)
{
_filename = filename;
_log = new StringBuilder();
_outputFile = new StreamWriter($"{filename}.csharp.log") {AutoFlush = true};
}

public void Collect(Registers state)
{
_outputFile.WriteLine(state.ToString());

if (Counter % 10000 == 0)
{
_outputFile.Flush();
}

Counter++;
_log.AppendLine(state.ToString());
}

public void Save() => File.WriteAllText(_filename + ".csharp.log", _log.ToString());
public void Save() => _outputFile.Flush();
}
}

0 comments on commit 9a599c5

Please sign in to comment.