all repos — mgba @ c3c3bdc20c515892c9adf31336b928ac50bd5dc0

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
 13struct GBCore {
 14	struct mCore d;
 15	struct GBVideoSoftwareRenderer renderer;
 16	uint8_t keys;
 17};
 18
 19static bool _GBCoreInit(struct mCore* core) {
 20	struct GBCore* gbcore = (struct GBCore*) core;
 21
 22	struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
 23	struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
 24	if (!cpu || !gb) {
 25		free(cpu);
 26		free(gb);
 27		return false;
 28	}
 29	core->cpu = cpu;
 30	core->board = gb;
 31
 32	GBCreate(gb);
 33	LR35902SetComponents(cpu, &gb->d, 0, 0);
 34	LR35902Init(cpu);
 35
 36	GBVideoSoftwareRendererCreate(&gbcore->renderer);
 37
 38	gb->keySource = &gbcore->keys;
 39
 40#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 41	mDirectorySetInit(&core->dirs);
 42#endif
 43	
 44	return true;
 45}
 46
 47static void _GBCoreDeinit(struct mCore* core) {
 48	LR35902Deinit(core->cpu);
 49	GBDestroy(core->board);
 50	mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
 51	mappedMemoryFree(core->board, sizeof(struct GB));
 52#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 53	mDirectorySetDeinit(&core->dirs);
 54#endif
 55}
 56
 57static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 58	struct GB* gb = core->board;
 59	gb->sync = sync;
 60}
 61
 62static void _GBCoreLoadConfig(struct mCore* core) {
 63	UNUSED(core);
 64	// TODO
 65}
 66
 67static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 68	UNUSED(core);
 69	*width = GB_VIDEO_HORIZONTAL_PIXELS;
 70	*height = GB_VIDEO_VERTICAL_PIXELS;
 71}
 72
 73static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 74	struct GBCore* gbcore = (struct GBCore*) core;
 75	gbcore->renderer.outputBuffer = buffer;
 76	gbcore->renderer.outputBufferStride = stride;
 77}
 78
 79static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
 80	struct GB* gb = core->board;
 81	switch (ch) {
 82	case 0:
 83		return gb->audio.left;
 84	case 1:
 85		return gb->audio.right;
 86	default:
 87		return NULL;
 88	}
 89}
 90
 91static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
 92	return GBLoadROM(core->board, vf);
 93}
 94
 95static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
 96	return GBLoadSave(core->board, vf);
 97}
 98
 99static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
100	// TODO
101	UNUSED(core);
102	UNUSED(vf);
103	mLOG(GB, STUB, "Patches are not yet supported");
104	return false;
105}
106
107static void _GBCoreUnloadROM(struct mCore* core) {
108	return GBUnloadROM(core->board);
109}
110
111static void _GBCoreReset(struct mCore* core) {
112	struct GBCore* gbcore = (struct GBCore*) core;
113	struct GB* gb = (struct GB*) core->board;
114	GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
115	LR35902Reset(core->cpu);
116}
117
118static void _GBCoreRunFrame(struct mCore* core) {
119	struct GB* gb = core->board;
120	int32_t frameCounter = gb->video.frameCounter;
121	while (gb->video.frameCounter == frameCounter) {
122		LR35902Run(core->cpu);
123	}
124}
125
126static void _GBCoreRunLoop(struct mCore* core) {
127	LR35902Run(core->cpu);
128}
129
130static void _GBCoreStep(struct mCore* core) {
131	LR35902Tick(core->cpu);
132}
133
134static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
135	UNUSED(core);
136	UNUSED(vf);
137	UNUSED(flags);
138	// TODO
139	return false;
140}
141
142static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
143	UNUSED(core);
144	UNUSED(vf);
145	UNUSED(flags);
146	// TODO
147	return false;
148}
149
150static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
151	struct GBCore* gbcore = (struct GBCore*) core;
152	gbcore->keys = keys;
153}
154
155static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
156	struct GBCore* gbcore = (struct GBCore*) core;
157	gbcore->keys |= keys;
158}
159
160static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
161	struct GBCore* gbcore = (struct GBCore*) core;
162	gbcore->keys &= ~keys;
163}
164
165static int32_t _GBCoreFrameCounter(struct mCore* core) {
166	struct GB* gb = core->board;
167	return gb->video.frameCounter;
168}
169
170static int32_t _GBCoreFrameCycles(struct mCore* core) {
171	UNUSED(core);
172	return GB_VIDEO_TOTAL_LENGTH;
173}
174
175static int32_t _GBCoreFrequency(struct mCore* core) {
176	UNUSED(core);
177	// TODO: GB differences
178	return DMG_LR35902_FREQUENCY;
179}
180
181static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
182	struct GB* gb = core->board;
183	gb->memory.rtc = rtc;
184}
185
186struct mCore* GBCoreCreate(void) {
187	struct GBCore* gbcore = malloc(sizeof(*gbcore));
188	struct mCore* core = &gbcore->d;
189	memset(&core->opts, 0, sizeof(core->opts));
190	core->cpu = 0;
191	core->board = 0;
192	core->init = _GBCoreInit;
193	core->deinit = _GBCoreDeinit;
194	core->setSync = _GBCoreSetSync;
195	core->loadConfig = _GBCoreLoadConfig;
196	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
197	core->setVideoBuffer = _GBCoreSetVideoBuffer;
198	core->getAudioChannel = _GBCoreGetAudioChannel;
199	core->isROM = GBIsROM;
200	core->loadROM = _GBCoreLoadROM;
201	core->loadSave = _GBCoreLoadSave;
202	core->loadPatch = _GBCoreLoadPatch;
203	core->unloadROM = _GBCoreUnloadROM;
204	core->reset = _GBCoreReset;
205	core->runFrame = _GBCoreRunFrame;
206	core->runLoop = _GBCoreRunLoop;
207	core->step = _GBCoreStep;
208	core->loadState = _GBCoreLoadState;
209	core->saveState = _GBCoreSaveState;
210	core->setKeys = _GBCoreSetKeys;
211	core->addKeys = _GBCoreAddKeys;
212	core->clearKeys = _GBCoreClearKeys;
213	core->frameCounter = _GBCoreFrameCounter;
214	core->frameCycles = _GBCoreFrameCycles;
215	core->frequency = _GBCoreFrequency;
216	core->setRTC = _GBCoreSetRTC;
217	return core;
218}