aboutsummaryrefslogtreecommitdiff
path: root/tests/cart_test.go
blob: 5b41007b5b89c33381e3ac92f0f01cb20c17a0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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)
	}
}