diff --git a/tests/GameBoy.Core.Tests/BlarggTests.cs b/tests/GameBoy.Core.Tests/BlarggTests.cs new file mode 100644 index 0000000..ad42ef0 --- /dev/null +++ b/tests/GameBoy.Core.Tests/BlarggTests.cs @@ -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; + } + } +} diff --git a/tests/GameBoy.Core.Tests/TestRoms.cs b/tests/GameBoy.Core.Tests/TestRoms.cs new file mode 100644 index 0000000..67b4b8f --- /dev/null +++ b/tests/GameBoy.Core.Tests/TestRoms.cs @@ -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; + } + } +}