all repos — mgba @ 47062deb9fdb1e32b2f94429cd07e7cbca09cbea

mGBA Game Boy Advance Emulator

src/gb/core.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 "core.h"
  7
  8#include "core/core.h"
  9#include "gb/gb.h"
 10#include "gb/renderers/software.h"
 11#include "util/memory.h"
 12#include "util/patch.h"
 13
 14struct GBCore {
 15	struct mCore d;
 16	struct GBVideoSoftwareRenderer renderer;
 17	uint8_t keys;
 18};
 19
 20static bool _GBCoreInit(struct mCore* core) {
 21	struct GBCore* gbcore = (struct GBCore*) core;
 22
 23	struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
 24	struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
 25	if (!cpu || !gb) {
 26		free(cpu);
 27		free(gb);
 28		return false;
 29	}
 30	core->cpu = cpu;
 31	core->board = gb;
 32
 33	GBCreate(gb);
 34	LR35902SetComponents(cpu, &gb->d, 0, 0);
 35	LR35902Init(cpu);
 36
 37	GBVideoSoftwareRendererCreate(&gbcore->renderer);
 38
 39	gbcore->keys = 0;
 40	gb->keySource = &gbcore->keys;
 41
 42#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 43	mDirectorySetInit(&core->dirs);
 44#endif
 45	
 46	return true;
 47}
 48
 49static void _GBCoreDeinit(struct mCore* core) {
 50	LR35902Deinit(core->cpu);
 51	GBDestroy(core->board);
 52	mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
 53	mappedMemoryFree(core->board, sizeof(struct GB));
 54#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 55	mDirectorySetDeinit(&core->dirs);
 56#endif
 57}
 58
 59static enum mPlatform _GBCorePlatform(struct mCore* core) {
 60	UNUSED(core);
 61	return PLATFORM_GB;
 62}
 63
 64static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 65	struct GB* gb = core->board;
 66	gb->sync = sync;
 67}
 68
 69static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 70	UNUSED(config);
 71
 72	struct GB* gb = core->board;
 73	gb->audio.masterVolume = core->opts.volume;
 74}
 75
 76static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 77	UNUSED(core);
 78	*width = GB_VIDEO_HORIZONTAL_PIXELS;
 79	*height = GB_VIDEO_VERTICAL_PIXELS;
 80}
 81
 82static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 83	struct GBCore* gbcore = (struct GBCore*) core;
 84	gbcore->renderer.outputBuffer = buffer;
 85	gbcore->renderer.outputBufferStride = stride;
 86}
 87
 88static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
 89	struct GBCore* gbcore = (struct GBCore*) core;
 90	*buffer = gbcore->renderer.outputBuffer;
 91	*stride = gbcore->renderer.outputBufferStride;
 92}
 93
 94static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
 95	struct GB* gb = core->board;
 96	switch (ch) {
 97	case 0:
 98		return gb->audio.left;
 99	case 1:
100		return gb->audio.right;
101	default:
102		return NULL;
103	}
104}
105
106static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
107	struct GB* gb = core->board;
108	GBAudioResizeBuffer(&gb->audio, samples);
109}
110
111static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
112	struct GB* gb = core->board;
113	return gb->audio.samples;
114}
115
116static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
117	// TODO
118}
119
120static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
121	return GBLoadROM(core->board, vf);
122}
123
124static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
125	UNUSED(core);
126	UNUSED(vf);
127	UNUSED(type);
128	// TODO
129	return false;
130}
131
132static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
133	return GBLoadSave(core->board, vf);
134}
135
136static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
137	if (!vf) {
138		return false;
139	}
140	struct Patch patch;
141	if (!loadPatch(vf, &patch)) {
142		return false;
143	}
144	GBApplyPatch(core->board, &patch);
145	return true;
146}
147
148static void _GBCoreUnloadROM(struct mCore* core) {
149	return GBUnloadROM(core->board);
150}
151
152static void _GBCoreReset(struct mCore* core) {
153	struct GBCore* gbcore = (struct GBCore*) core;
154	struct GB* gb = (struct GB*) core->board;
155	GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
156	LR35902Reset(core->cpu);
157}
158
159static void _GBCoreRunFrame(struct mCore* core) {
160	struct GB* gb = core->board;
161	int32_t frameCounter = gb->video.frameCounter;
162	while (gb->video.frameCounter == frameCounter) {
163		LR35902Run(core->cpu);
164	}
165}
166
167static void _GBCoreRunLoop(struct mCore* core) {
168	LR35902Run(core->cpu);
169}
170
171static void _GBCoreStep(struct mCore* core) {
172	LR35902Tick(core->cpu);
173}
174
175static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
176	UNUSED(core);
177	UNUSED(vf);
178	UNUSED(flags);
179	// TODO
180	return false;
181}
182
183static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
184	UNUSED(core);
185	UNUSED(vf);
186	UNUSED(flags);
187	// TODO
188	return false;
189}
190
191static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
192	struct GBCore* gbcore = (struct GBCore*) core;
193	gbcore->keys = keys;
194}
195
196static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
197	struct GBCore* gbcore = (struct GBCore*) core;
198	gbcore->keys |= keys;
199}
200
201static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
202	struct GBCore* gbcore = (struct GBCore*) core;
203	gbcore->keys &= ~keys;
204}
205
206static int32_t _GBCoreFrameCounter(struct mCore* core) {
207	struct GB* gb = core->board;
208	return gb->video.frameCounter;
209}
210
211static int32_t _GBCoreFrameCycles(struct mCore* core) {
212	UNUSED(core);
213	return GB_VIDEO_TOTAL_LENGTH;
214}
215
216static int32_t _GBCoreFrequency(struct mCore* core) {
217	UNUSED(core);
218	// TODO: GB differences
219	return DMG_LR35902_FREQUENCY;
220}
221
222static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
223	GBGetGameTitle(core->board, title);
224}
225
226static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
227	struct GB* gb = core->board;
228	gb->memory.rtc = rtc;
229}
230
231struct mCore* GBCoreCreate(void) {
232	struct GBCore* gbcore = malloc(sizeof(*gbcore));
233	struct mCore* core = &gbcore->d;
234	memset(&core->opts, 0, sizeof(core->opts));
235	core->cpu = 0;
236	core->board = 0;
237	core->init = _GBCoreInit;
238	core->deinit = _GBCoreDeinit;
239	core->platform = _GBCorePlatform;
240	core->setSync = _GBCoreSetSync;
241	core->loadConfig = _GBCoreLoadConfig;
242	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
243	core->setVideoBuffer = _GBCoreSetVideoBuffer;
244	core->getVideoBuffer = _GBCoreGetVideoBuffer;
245	core->getAudioChannel = _GBCoreGetAudioChannel;
246	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
247	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
248	core->setAVStream = _GBCoreSetAVStream;
249	core->isROM = GBIsROM;
250	core->loadROM = _GBCoreLoadROM;
251	core->loadBIOS = _GBCoreLoadBIOS;
252	core->loadSave = _GBCoreLoadSave;
253	core->loadPatch = _GBCoreLoadPatch;
254	core->unloadROM = _GBCoreUnloadROM;
255	core->reset = _GBCoreReset;
256	core->runFrame = _GBCoreRunFrame;
257	core->runLoop = _GBCoreRunLoop;
258	core->step = _GBCoreStep;
259	core->loadState = _GBCoreLoadState;
260	core->saveState = _GBCoreSaveState;
261	core->setKeys = _GBCoreSetKeys;
262	core->addKeys = _GBCoreAddKeys;
263	core->clearKeys = _GBCoreClearKeys;
264	core->frameCounter = _GBCoreFrameCounter;
265	core->frameCycles = _GBCoreFrameCycles;
266	core->frequency = _GBCoreFrequency;
267	core->getGameTitle = _GBCoreGetGameTitle;
268	core->setRTC = _GBCoreSetRTC;
269	return core;
270}