all repos — mgba @ 8615defda763c928eadb4c0919081a74b6bd28fc

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