all repos — mgba @ d1b3ad08d89cb9289f28878be1d8e1ac19f172d2

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