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
13bool GBAContextInit(struct GBAContext* context, const char* port) {
14 context->gba = anonymousMemoryMap(sizeof(struct GBA));
15 context->cpu = anonymousMemoryMap(sizeof(struct ARMCore));
16 context->rom = 0;
17 context->save = 0;
18 context->renderer = 0;
19 memset(context->components, 0, sizeof(context->components));
20
21 if (!context->gba || !context->cpu) {
22 if (context->gba) {
23 mappedMemoryFree(context->gba, sizeof(struct GBA));
24 }
25 if (context->cpu) {
26 mappedMemoryFree(context->cpu, sizeof(struct ARMCore));
27 }
28 return false;
29 }
30
31 GBAConfigInit(&context->config, port);
32 if (port) {
33 GBAConfigLoad(&context->config);
34 }
35
36 GBACreate(context->gba);
37 ARMSetComponents(context->cpu, &context->gba->d, 0, context->components);
38 ARMInit(context->cpu);
39
40 context->gba->sync = 0;
41 return true;
42}
43
44void GBAContextDeinit(struct GBAContext* context) {
45 if (context->bios) {
46 context->bios->close(context->bios);
47 context->bios = 0;
48 }
49 if (context->rom) {
50 context->rom->close(context->rom);
51 context->rom = 0;
52 }
53 if (context->save) {
54 context->save->close(context->save);
55 context->save = 0;
56 }
57 ARMDeinit(context->cpu);
58 GBADestroy(context->gba);
59 mappedMemoryFree(context->gba, 0);
60 mappedMemoryFree(context->cpu, 0);
61 GBAConfigDeinit(&context->config);
62}
63
64bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) {
65 context->rom = VFileOpen(path, O_RDONLY);
66 if (!context->rom) {
67 return false;
68 }
69
70 if (!GBAIsROM(context->rom)) {
71 context->rom->close(context->rom);
72 context->rom = 0;
73 return false;
74 }
75
76 if (autoloadSave) {
77 context->save = VDirOptionalOpenFile(0, path, 0, ".sav", O_RDWR | O_CREAT);
78 }
79 return true;
80}
81
82bool GBAContextLoadROMFromVFile(struct GBAContext* context, struct VFile* rom, struct VFile* save) {
83 context->rom = rom;
84 if (!GBAIsROM(context->rom)) {
85 context->rom = 0;
86 return false;
87 }
88 context->save = save;
89 return true;
90}
91
92bool GBAContextLoadBIOS(struct GBAContext* context, const char* path) {
93 context->bios = VFileOpen(path, O_RDONLY);
94 if (!context->bios) {
95 return false;
96 }
97
98 if (!GBAIsBIOS(context->bios)) {
99 context->bios->close(context->bios);
100 context->bios = 0;
101 return false;
102 }
103 return true;
104}
105
106bool GBAContextLoadBIOSFromVFile(struct GBAContext* context, struct VFile* bios) {
107 context->bios = bios;
108 if (!GBAIsBIOS(context->bios)) {
109 context->bios = 0;
110 return false;
111 }
112 return true;
113}
114
115bool GBAContextStart(struct GBAContext* context) {
116 struct GBAOptions opts = {};
117
118 if (context->renderer) {
119 GBAVideoAssociateRenderer(&context->gba->video, context->renderer);
120 }
121
122 if (!GBALoadROM(context->gba, context->rom, context->save, 0)) {
123 return false;
124 }
125
126 GBAConfigMap(&context->config, &opts);
127 if (opts.useBios && context->bios) {
128 GBALoadBIOS(context->gba, context->bios);
129 }
130
131 ARMReset(context->cpu);
132
133 if (opts.skipBios) {
134 GBASkipBIOS(context->cpu);
135 }
136
137 struct GBACartridgeOverride override;
138 const struct GBACartridge* cart = (const struct GBACartridge*) context->gba->memory.rom;
139 memcpy(override.id, &cart->id, sizeof(override.id));
140 if (GBAOverrideFind(GBAConfigGetOverrides(&context->config), &override)) {
141 GBAOverrideApply(context->gba, &override);
142 }
143 GBAConfigFreeOpts(&opts);
144 return true;
145}
146
147void GBAContextStop(struct GBAContext* context) {
148 UNUSED(context);
149 // TODO?
150}
151
152void GBAContextFrame(struct GBAContext* context, uint16_t keys) {
153 int activeKeys = keys;
154 context->gba->keySource = &activeKeys;
155
156 int frameCounter = context->gba->video.frameCounter;
157 while (frameCounter == context->gba->video.frameCounter) {
158 ARMRunLoop(context->cpu);
159 }
160}