src/gb/test/mbc.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/test/suite.h"
7
8#include "core/core.h"
9#include "gb/core.h"
10#include "gb/gb.h"
11#include "gb/mbc.h"
12#include "util/vfs.h"
13
14M_TEST_SUITE_SETUP(GBMBC) {
15 struct VFile* vf = VFileMemChunk(NULL, 2048);
16 GBSynthesizeROM(vf);
17 struct mCore* core = GBCoreCreate();
18 core->init(core);
19 core->loadROM(core, vf);
20 *state = core;
21 return 0;
22}
23
24M_TEST_SUITE_TEARDOWN(GBMBC) {
25 if (!*state) {
26 return 0;
27 }
28 struct mCore* core = *state;
29 core->deinit(core);
30 return 0;
31}
32M_TEST_DEFINE(detectNone) {
33 struct mCore* core = *state;
34 struct GB* gb = core->board;
35 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
36
37 gb->memory.mbcType = GB_MBC_AUTODETECT;
38 cart->type = 0x00;
39 core->reset(core);
40 assert_int_equal(gb->memory.mbcType, GB_MBC_NONE);
41
42 gb->memory.mbcType = GB_MBC_AUTODETECT;
43 cart->type = 0x08;
44 core->reset(core);
45 assert_int_equal(gb->memory.mbcType, GB_MBC_NONE);
46
47
48 gb->memory.mbcType = GB_MBC_AUTODETECT;
49 cart->type = 0x09;
50 core->reset(core);
51 assert_int_equal(gb->memory.mbcType, GB_MBC_NONE);
52
53
54 gb->memory.mbcType = GB_MBC_AUTODETECT;
55 cart->type = 0x0A;
56 core->reset(core);
57 assert_int_not_equal(gb->memory.mbcType, GB_MBC_NONE);
58}
59
60M_TEST_DEFINE(detect1) {
61 struct mCore* core = *state;
62 struct GB* gb = core->board;
63 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
64
65 gb->memory.mbcType = GB_MBC_AUTODETECT;
66 cart->type = 0x00;
67 core->reset(core);
68 assert_int_not_equal(gb->memory.mbcType, GB_MBC1);
69
70 gb->memory.mbcType = GB_MBC_AUTODETECT;
71 cart->type = 0x01;
72 core->reset(core);
73 assert_int_equal(gb->memory.mbcType, GB_MBC1);
74
75 gb->memory.mbcType = GB_MBC_AUTODETECT;
76 cart->type = 0x02;
77 core->reset(core);
78 assert_int_equal(gb->memory.mbcType, GB_MBC1);
79
80 gb->memory.mbcType = GB_MBC_AUTODETECT;
81 cart->type = 0x03;
82 core->reset(core);
83 assert_int_equal(gb->memory.mbcType, GB_MBC1);
84
85 gb->memory.mbcType = GB_MBC_AUTODETECT;
86 cart->type = 0x04;
87 core->reset(core);
88 assert_int_not_equal(gb->memory.mbcType, GB_MBC1);
89}
90
91M_TEST_DEFINE(detect2) {
92 struct mCore* core = *state;
93 struct GB* gb = core->board;
94 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
95
96 gb->memory.mbcType = GB_MBC_AUTODETECT;
97 cart->type = 0x04;
98 core->reset(core);
99 assert_int_not_equal(gb->memory.mbcType, GB_MBC2);
100
101 gb->memory.mbcType = GB_MBC_AUTODETECT;
102 cart->type = 0x05;
103 core->reset(core);
104 assert_int_equal(gb->memory.mbcType, GB_MBC2);
105
106 gb->memory.mbcType = GB_MBC_AUTODETECT;
107 cart->type = 0x06;
108 core->reset(core);
109 assert_int_equal(gb->memory.mbcType, GB_MBC2);
110
111 gb->memory.mbcType = GB_MBC_AUTODETECT;
112 cart->type = 0x07;
113 core->reset(core);
114 assert_int_not_equal(gb->memory.mbcType, GB_MBC2);
115}
116
117M_TEST_DEFINE(detect3) {
118 struct mCore* core = *state;
119 struct GB* gb = core->board;
120 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
121
122 gb->memory.mbcType = GB_MBC_AUTODETECT;
123 cart->type = 0x0E;
124 core->reset(core);
125 assert_int_not_equal(gb->memory.mbcType, GB_MBC3);
126 assert_int_not_equal(gb->memory.mbcType, GB_MBC3_RTC);
127
128 gb->memory.mbcType = GB_MBC_AUTODETECT;
129 cart->type = 0x0F;
130 core->reset(core);
131 assert_int_equal(gb->memory.mbcType, GB_MBC3_RTC);
132
133 gb->memory.mbcType = GB_MBC_AUTODETECT;
134 cart->type = 0x10;
135 core->reset(core);
136 assert_int_equal(gb->memory.mbcType, GB_MBC3_RTC);
137
138 gb->memory.mbcType = GB_MBC_AUTODETECT;
139 cart->type = 0x11;
140 core->reset(core);
141 assert_int_equal(gb->memory.mbcType, GB_MBC3);
142
143 gb->memory.mbcType = GB_MBC_AUTODETECT;
144 cart->type = 0x12;
145 core->reset(core);
146 assert_int_equal(gb->memory.mbcType, GB_MBC3);
147
148 gb->memory.mbcType = GB_MBC_AUTODETECT;
149 cart->type = 0x13;
150 core->reset(core);
151 assert_int_equal(gb->memory.mbcType, GB_MBC3);
152
153 gb->memory.mbcType = GB_MBC_AUTODETECT;
154 cart->type = 0x14;
155 core->reset(core);
156 assert_int_not_equal(gb->memory.mbcType, GB_MBC3);
157 assert_int_not_equal(gb->memory.mbcType, GB_MBC3_RTC);
158}
159
160M_TEST_DEFINE(detect5) {
161 struct mCore* core = *state;
162 struct GB* gb = core->board;
163 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
164
165 gb->memory.mbcType = GB_MBC_AUTODETECT;
166 cart->type = 0x19;
167 core->reset(core);
168 assert_int_equal(gb->memory.mbcType, GB_MBC5);
169
170 gb->memory.mbcType = GB_MBC_AUTODETECT;
171 cart->type = 0x1A;
172 core->reset(core);
173 assert_int_equal(gb->memory.mbcType, GB_MBC5);
174
175 gb->memory.mbcType = GB_MBC_AUTODETECT;
176 cart->type = 0x1B;
177 core->reset(core);
178 assert_int_equal(gb->memory.mbcType, GB_MBC5);
179
180 gb->memory.mbcType = GB_MBC_AUTODETECT;
181 cart->type = 0x1C;
182 core->reset(core);
183 assert_int_equal(gb->memory.mbcType, GB_MBC5_RUMBLE);
184
185 gb->memory.mbcType = GB_MBC_AUTODETECT;
186 cart->type = 0x1D;
187 core->reset(core);
188 assert_int_equal(gb->memory.mbcType, GB_MBC5_RUMBLE);
189
190 gb->memory.mbcType = GB_MBC_AUTODETECT;
191 cart->type = 0x1E;
192 core->reset(core);
193 assert_int_equal(gb->memory.mbcType, GB_MBC5_RUMBLE);
194}
195
196M_TEST_DEFINE(detect6) {
197 struct mCore* core = *state;
198 struct GB* gb = core->board;
199 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
200
201 gb->memory.mbcType = GB_MBC_AUTODETECT;
202 cart->type = 0x1F;
203 core->reset(core);
204 assert_int_not_equal(gb->memory.mbcType, GB_MBC6);
205
206 gb->memory.mbcType = GB_MBC_AUTODETECT;
207 cart->type = 0x20;
208 core->reset(core);
209 assert_int_equal(gb->memory.mbcType, GB_MBC6);
210
211 gb->memory.mbcType = GB_MBC_AUTODETECT;
212 cart->type = 0x21;
213 core->reset(core);
214 assert_int_not_equal(gb->memory.mbcType, GB_MBC6);
215}
216
217M_TEST_DEFINE(detect7) {
218 struct mCore* core = *state;
219 struct GB* gb = core->board;
220 struct GBCartridge* cart = (struct GBCartridge*) &gb->memory.rom[0x100];
221
222 gb->memory.mbcType = GB_MBC_AUTODETECT;
223 cart->type = 0x21;
224 core->reset(core);
225 assert_int_not_equal(gb->memory.mbcType, GB_MBC7);
226
227 gb->memory.mbcType = GB_MBC_AUTODETECT;
228 cart->type = 0x22;
229 core->reset(core);
230 assert_int_equal(gb->memory.mbcType, GB_MBC7);
231
232 gb->memory.mbcType = GB_MBC_AUTODETECT;
233 cart->type = 0x23;
234 core->reset(core);
235 assert_int_not_equal(gb->memory.mbcType, GB_MBC7);
236}
237
238M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(GBMBC,
239 cmocka_unit_test(detectNone),
240 cmocka_unit_test(detect1),
241 cmocka_unit_test(detect2),
242 cmocka_unit_test(detect3),
243 cmocka_unit_test(detect5),
244 cmocka_unit_test(detect6),
245 cmocka_unit_test(detect7))