test(cpu): add Blargg hardware test ROM harness

This commit is contained in:
dualfroz committed 2026-08-27 16:35:18 +02:00
1 parent 0c11a649e5
commit 62e3d5df9f
2 files changed
+84

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
using Xunit;
using Xunit.Abstractions;
namespace GameBoy.Tests
{
public sealed class BlarggTests
{
private const int MaxFrames = 4000;
private readonly ITestOutputHelper output;
public BlarggTests(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void CpuInstrs_ReportsPassedOverSerial()
{
string serial = RunUntilSerialResult(TestRoms.Load("cpu_instrs.gb"));
output.WriteLine(serial);
Assert.Contains("Passed", serial);
Assert.DoesNotContain("Failed", serial);
}
[Fact]
public void InstrTiming_ReportsPassedOverSerial()
{
string serial = RunUntilSerialResult(TestRoms.Load("instr_timing.gb"));
output.WriteLine(serial);
Assert.Contains("Passed", serial);
Assert.DoesNotContain("Failed", serial);
}
private static string RunUntilSerialResult(byte[] rom)
{
var emulator = new Emulator(rom);
for (int frame = 0; frame < MaxFrames; frame++)
{
emulator.RunFrame();
string serial = emulator.SerialOutput;
if (serial.Contains("Passed") || serial.Contains("Failed"))
return serial;
}
return emulator.SerialOutput;
}
}
}
+37
View File
@@ -0,0 +1,37 @@
using System;
using System.IO;
namespace GameBoy.Tests
{
internal static class TestRoms
{
public static string Path(string name)
{
string path = System.IO.Path.Combine(AppContext.BaseDirectory, "roms", name);
if (!File.Exists(path))
throw new FileNotFoundException($"test rom not found: {path}");
return path;
}
public static byte[] Load(string name) => File.ReadAllBytes(Path(name));
public static byte[] BuildProgram(params byte[] program)
{
var rom = new byte[0x8000];
rom[0x0147] = 0x00;
rom[0x0148] = 0x00;
rom[0x0149] = 0x00;
Array.Copy(program, 0, rom, 0x0100, program.Length);
return rom;
}
public static byte[] BuildCartridge(byte cartridgeType, byte ramSizeCode, int romBanks = 2)
{
var rom = new byte[0x4000 * romBanks];
rom[0x0147] = cartridgeType;
rom[0x0148] = 0x00;
rom[0x0149] = ramSizeCode;
return rom;
}
}
}