all repos — mgba @ 74af414b966950fc872cbddc05b8083fe29fef95

mGBA Game Boy Advance Emulator

src/gb/test/memory.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(GBMemory) {
15	struct VFile* vf = VFileMemChunk(NULL, GB_SIZE_CART_BANK0 * 4);
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(GBMemory) {
26	if (!*state) {
27		return 0;
28	}
29	struct mCore* core = *state;
30	mCoreConfigDeinit(&core->config);
31	core->deinit(core);
32	return 0;
33}
34
35M_TEST_DEFINE(patchROMBank0) {
36	struct mCore* core = *state;
37	struct GB* gb = core->board;
38	int8_t oldExpected = gb->memory.rom[0];
39	int8_t old;
40	int8_t newExpected = 0x40;
41
42	core->reset(core);
43	GBPatch8(gb->cpu, 0, newExpected, &old, 0);
44	assert_int_equal(old, oldExpected);
45	assert_int_equal(gb->memory.rom[0], newExpected);
46	assert_int_equal(GBView8(gb->cpu, 0, 0), newExpected);
47}
48
49M_TEST_DEFINE(patchROMBank1) {
50	struct mCore* core = *state;
51	struct GB* gb = core->board;
52	int8_t oldExpected = gb->memory.rom[GB_SIZE_CART_BANK0];
53	int8_t old;
54	int8_t newExpected = 0x41;
55
56	core->reset(core);
57	GBPatch8(gb->cpu, GB_SIZE_CART_BANK0, newExpected, &old, 1);
58	assert_int_equal(old, oldExpected);
59	assert_int_equal(gb->memory.rom[GB_SIZE_CART_BANK0], newExpected);
60	assert_int_equal(GBView8(gb->cpu, GB_SIZE_CART_BANK0, 1), newExpected);
61}
62
63M_TEST_DEFINE(patchROMBank2) {
64	struct mCore* core = *state;
65	struct GB* gb = core->board;
66	int8_t oldExpected = gb->memory.rom[GB_SIZE_CART_BANK0 * 2];
67	int8_t old;
68	int8_t newExpected = 0x42;
69
70	core->reset(core);
71	GBPatch8(gb->cpu, GB_SIZE_CART_BANK0, newExpected, &old, 2);
72	assert_int_equal(old, oldExpected);
73	assert_int_equal(gb->memory.rom[GB_SIZE_CART_BANK0 * 2], newExpected);
74	assert_int_equal(GBView8(gb->cpu, GB_SIZE_CART_BANK0, 2), newExpected);
75}
76
77M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(GBMemory,
78	cmocka_unit_test(patchROMBank0),
79	cmocka_unit_test(patchROMBank1),
80	cmocka_unit_test(patchROMBank2))