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