all repos — mgba @ f6a4a13b60ae30fa74ee33422e2d951b6c0a3348

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