src/gba/context/context.c (view raw)
1/* Copyright (c) 2013-2015 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 "gba/context/context.h"
7
8#include "gba/context/overrides.h"
9
10#include "util/memory.h"
11#include "util/vfs.h"
12
13static struct VFile* _logFile = 0;
14static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
15
16bool GBAContextInit(struct GBAContext* context, const char* port) {
17 context->gba = anonymousMemoryMap(sizeof(struct GBA));
18 context->cpu = anonymousMemoryMap(sizeof(struct ARMCore));
19 context->rom = 0;
20 context->bios = 0;
21 context->fname = 0;
22 context->save = 0;
23 context->renderer = 0;
24 GBADirectorySetInit(&context->dirs);
25 memset(context->components, 0, sizeof(context->components));
26
27 if (!context->gba || !context->cpu) {
28 if (context->gba) {
29 mappedMemoryFree(context->gba, sizeof(struct GBA));
30 }
31 if (context->cpu) {
32 mappedMemoryFree(context->cpu, sizeof(struct ARMCore));
33 }
34 return false;
35 }
36 GBACreate(context->gba);
37 ARMSetComponents(context->cpu, &context->gba->d, GBA_COMPONENT_MAX, context->components);
38 ARMInit(context->cpu);
39
40 GBAConfigInit(&context->config, port);
41#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
42 if (port) {
43 if (!_logFile) {
44 char logPath[PATH_MAX];
45 GBAConfigDirectory(logPath, PATH_MAX);
46 strncat(logPath, PATH_SEP "log", PATH_MAX - strlen(logPath));
47 _logFile = VFileOpen(logPath, O_WRONLY | O_CREAT | O_TRUNC);
48 }
49 context->gba->logHandler = _GBAContextLog;
50
51 char biosPath[PATH_MAX];
52 GBAConfigDirectory(biosPath, PATH_MAX);
53 strncat(biosPath, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(biosPath));
54
55 struct GBAOptions opts = {
56 .bios = biosPath,
57 .useBios = true,
58 .idleOptimization = IDLE_LOOP_DETECT,
59 .logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS
60 };
61 GBAConfigLoad(&context->config);
62 GBAConfigLoadDefaults(&context->config, &opts);
63 }
64#else
65 UNUSED(port);
66#endif
67
68 context->gba->sync = 0;
69 return true;
70}
71
72void GBAContextDeinit(struct GBAContext* context) {
73 ARMDeinit(context->cpu);
74 GBADestroy(context->gba);
75 if (context->bios) {
76 context->bios->close(context->bios);
77 context->bios = 0;
78 }
79 mappedMemoryFree(context->gba, 0);
80 mappedMemoryFree(context->cpu, 0);
81 GBAConfigDeinit(&context->config);
82 GBADirectorySetDeinit(&context->dirs);
83}
84
85bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) {
86 context->rom = GBADirectorySetOpenPath(&context->dirs, path, GBAIsROM);
87 if (!context->rom) {
88 return false;
89 }
90
91 context->fname = path;
92#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
93 if (autoloadSave) {
94 char dirname[PATH_MAX];
95 char basename[PATH_MAX];
96 separatePath(context->fname, dirname, basename, 0);
97 GBADirectorySetAttachBase(&context->dirs, VDirOpen(dirname));
98 strncat(basename, ".sav", PATH_MAX - strlen(basename) - 1);
99 context->save = context->dirs.save->openFile(context->dirs.save, basename, O_RDWR | O_CREAT);
100 }
101#else
102 UNUSED(autoloadSave);
103#endif
104 return true;
105}
106
107void GBAContextUnloadROM(struct GBAContext* context) {
108 GBAUnloadROM(context->gba);
109 GBADirectorySetDetachBase(&context->dirs);
110 if (context->rom) {
111 context->rom->close(context->rom);
112 context->rom = 0;
113 }
114 if (context->save) {
115 context->save->close(context->save);
116 context->save = 0;
117 }
118}
119
120bool GBAContextLoadROMFromVFile(struct GBAContext* context, struct VFile* rom, struct VFile* save) {
121 context->rom = rom;
122 if (!GBAIsROM(context->rom)) {
123 context->rom = 0;
124 return false;
125 }
126 context->save = save;
127 return true;
128}
129
130bool GBAContextLoadBIOS(struct GBAContext* context, const char* path) {
131 context->bios = VFileOpen(path, O_RDONLY);
132 if (!context->bios) {
133 return false;
134 }
135
136 if (!GBAIsBIOS(context->bios)) {
137 context->bios->close(context->bios);
138 context->bios = 0;
139 return false;
140 }
141 return true;
142}
143
144bool GBAContextLoadBIOSFromVFile(struct GBAContext* context, struct VFile* bios) {
145 context->bios = bios;
146 if (!GBAIsBIOS(context->bios)) {
147 context->bios = 0;
148 return false;
149 }
150 return true;
151}
152
153bool GBAContextStart(struct GBAContext* context) {
154 struct GBAOptions opts = { .bios = 0 };
155
156 if (context->renderer) {
157 GBAVideoAssociateRenderer(&context->gba->video, context->renderer);
158 }
159
160 if (!GBALoadROM(context->gba, context->rom, context->save, context->fname)) {
161 return false;
162 }
163
164 GBAConfigMap(&context->config, &opts);
165
166 if (!context->bios && opts.bios) {
167 GBAContextLoadBIOS(context, opts.bios);
168 }
169 if (opts.useBios && context->bios) {
170 GBALoadBIOS(context->gba, context->bios);
171 }
172 context->gba->logLevel = opts.logLevel;
173 context->gba->idleOptimization = opts.idleOptimization;
174
175 GBAContextReset(context);
176
177 // TODO: Move this into GBAContextReset
178 if (opts.skipBios) {
179 GBASkipBIOS(context->gba);
180 }
181
182 struct GBACartridgeOverride override;
183 const struct GBACartridge* cart = (const struct GBACartridge*) context->gba->memory.rom;
184 memcpy(override.id, &cart->id, sizeof(override.id));
185 struct Configuration* overrides = 0;
186#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
187 overrides = GBAConfigGetOverrides(&context->config);
188 GBAConfigFreeOpts(&opts);
189#endif
190 if (GBAOverrideFind(overrides, &override)) {
191 GBAOverrideApply(context->gba, &override);
192 }
193 return true;
194}
195
196void GBAContextReset(struct GBAContext* context) {
197 ARMReset(context->cpu);
198}
199
200void GBAContextStop(struct GBAContext* context) {
201 UNUSED(context);
202 // TODO?
203}
204
205void GBAContextFrame(struct GBAContext* context, uint16_t keys) {
206 int activeKeys = keys;
207 context->gba->keySource = &activeKeys;
208
209 int frameCounter = context->gba->video.frameCounter;
210 while (frameCounter == context->gba->video.frameCounter) {
211 ARMRunLoop(context->cpu);
212 }
213}
214
215static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
216 UNUSED(thread);
217 UNUSED(level);
218 // TODO: Make this local
219 if (!_logFile) {
220 return;
221 }
222 char out[256];
223 size_t len = vsnprintf(out, sizeof(out), format, args);
224 if (len >= sizeof(out)) {
225 len = sizeof(out) - 1;
226 }
227 out[len] = '\n';
228 _logFile->write(_logFile, out, len + 1);
229}