diff options
Diffstat (limited to 'tests/cart_test.go')
| -rw-r--r-- | tests/cart_test.go | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/cart_test.go b/tests/cart_test.go new file mode 100644 index 0000000..5b41007 --- /dev/null +++ b/tests/cart_test.go @@ -0,0 +1,115 @@ +package tests + +import ( + "crecher.net/gones/v2/cart" + "os" + "testing" +) + +const nestestPath = "testdata/nestest.nes" + +func header(prgBanks, chrBanks, flags6, flags7 byte) []byte { + h := make([]byte, 16, 16+int(prgBanks)*16*1024+int(chrBanks)*8*1024) + copy(h, "NES\x1A") + h[4], h[5], h[6], h[7] = prgBanks, chrBanks, flags6, flags7 + h = append(h, make([]byte, int(prgBanks)*16*1024)...) + h = append(h, make([]byte, int(chrBanks)*8*1024)...) + return h +} + +func TestNestestHeader(t *testing.T) { + raw, err := os.ReadFile(nestestPath) + if err != nil { + t.Fatal(err) + } + t.Logf("header: % x", raw[:16]) + + c, err := cart.Parse(raw) + if err != nil { + t.Fatal(err) + } + + if want := (raw[6] >> 4) | (raw[7] & 0xF0); c.MapperID != want { + t.Errorf("MapperID = %d, want %d", c.MapperID, want) + } + if c.MapperID != 0 { + t.Errorf("nestest should be mapper 0, got %d", c.MapperID) + } + if want := int(raw[4]) * 16 * 1024; len(c.PRG) != want { + t.Errorf("len(PRG) = %d, want %d", len(c.PRG), want) + } + if want := int(raw[5]) * 8 * 1024; len(c.CHR) != want { + t.Errorf("len(CHR) = %d, want %d", len(c.CHR), want) + } + wantMirror := cart.Horizontal + if raw[6]&0x01 != 0 { + wantMirror = cart.Vertical + } + if c.Mirroring != wantMirror { + t.Errorf("Mirroring = %v, want %v", c.Mirroring, wantMirror) + } +} + +func TestLoadNestestResetVector(t *testing.T) { + c, err := cart.Load(nestestPath) + if err != nil { + t.Fatal(err) + } + lo := uint16(c.Mapper.CPURead(0xFFFC)) + hi := uint16(c.Mapper.CPURead(0xFFFD)) + if got := hi<<8 | lo; got != 0xC004 { + t.Errorf("reset vector = %#04x, want 0xC004", got) + } +} + +func TestParseErrors(t *testing.T) { + tests := []struct { + name string + data []byte + }{ + {"empty", nil}, + {"shorter than header", []byte("NES\x1A")}, + {"bad magic", append([]byte("XXXX"), make([]byte, 12)...)}, + {"truncated PRG", header(2, 1, 0, 0)[:16+1024]}, + {"unsupported mapper 1", header(1, 1, 0x10, 0)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if _, err := cart.Parse(tt.data); err == nil { + t.Error("expected error, got nil") + } + }) + } +} + +func TestParseCHRRAM(t *testing.T) { + c, err := cart.Parse(header(1, 0, 0, 0)) + if err != nil { + t.Fatal(err) + } + if !c.CHRIsRAM || len(c.CHR) != 8*1024 { + t.Errorf("CHRIsRAM = %v, len(CHR) = %d; want true, 8192", c.CHRIsRAM, len(c.CHR)) + } + c.Mapper.PPUWrite(0x0010, 0xAB) + if got := c.Mapper.PPURead(0x0010); got != 0xAB { + t.Errorf("CHR-RAM readback = %#02x, want 0xAB", got) + } +} + +func TestParseFlags(t *testing.T) { + c, err := cart.Parse(header(1, 1, 0x03, 0)) + if err != nil { + t.Fatal(err) + } + if c.Mirroring != cart.Vertical || !c.Battery { + t.Errorf("Mirroring = %v, Battery = %v; want cart.Vertical, true", c.Mirroring, c.Battery) + } + + c, err = cart.Parse(header(1, 1, 0x09, 0)) + if err != nil { + t.Fatal(err) + } + if c.Mirroring != cart.FourScreen { + t.Errorf("Mirroring = %v, want cart.FourScreen", c.Mirroring) + } +} |