all repos — mgba @ fa884d071ecaa3e05ff20b45a67bf9500dd3d6b6

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