include/mgba/internal/gb/gb.h (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#ifndef GB_H
7#define GB_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/cpu.h>
14#include <mgba/core/interface.h>
15#include <mgba/core/log.h>
16#include <mgba/core/timing.h>
17
18#include <mgba/internal/gb/audio.h>
19#include <mgba/internal/gb/memory.h>
20#include <mgba/internal/gb/sio.h>
21#include <mgba/internal/gb/timer.h>
22#include <mgba/internal/gb/video.h>
23
24extern const uint32_t DMG_LR35902_FREQUENCY;
25extern const uint32_t CGB_LR35902_FREQUENCY;
26extern const uint32_t SGB_LR35902_FREQUENCY;
27
28mLOG_DECLARE_CATEGORY(GB);
29
30// TODO: Prefix GBAIRQ
31enum GBIRQ {
32 GB_IRQ_VBLANK = 0x0,
33 GB_IRQ_LCDSTAT = 0x1,
34 GB_IRQ_TIMER = 0x2,
35 GB_IRQ_SIO = 0x3,
36 GB_IRQ_KEYPAD = 0x4,
37};
38
39enum GBIRQVector {
40 GB_VECTOR_VBLANK = 0x40,
41 GB_VECTOR_LCDSTAT = 0x48,
42 GB_VECTOR_TIMER = 0x50,
43 GB_VECTOR_SIO = 0x58,
44 GB_VECTOR_KEYPAD = 0x60,
45};
46
47enum GBSGBCommand {
48 SGB_PAL01 = 0,
49 SGB_PAL23,
50 SGB_PAL03,
51 SGB_PAL12,
52 SGB_ATTR_BLK,
53 SGB_ATTR_LIN,
54 SGB_ATTR_DIV,
55 SGB_ATTR_CHR,
56 SGB_SOUND,
57 SGB_SOU_TRN,
58 SGB_PAL_SET,
59 SGB_PAL_TRN,
60 SGB_ATRC_EN,
61 SGB_TEST_EN,
62 SGB_PICON_EN,
63 SGB_DATA_SND,
64 SGB_DATA_TRN,
65 SGB_MLT_REQ,
66 SGB_JUMP,
67 SGB_CHR_TRN,
68 SGB_PCT_TRN,
69 SGB_ATTR_TRN,
70 SGB_ATTR_SET,
71 SGB_MASK_EN,
72 SGB_OBJ_TRN
73};
74
75struct LR35902Core;
76struct mCoreSync;
77struct mAVStream;
78struct GB {
79 struct mCPUComponent d;
80
81 struct LR35902Core* cpu;
82 struct GBMemory memory;
83 struct GBVideo video;
84 struct GBTimer timer;
85 struct GBAudio audio;
86 struct GBSIO sio;
87 enum GBModel model;
88
89 struct mCoreSync* sync;
90 struct mTiming timing;
91
92 uint8_t* keySource;
93
94 bool isPristine;
95 size_t pristineRomSize;
96 size_t yankedRomSize;
97 enum GBMemoryBankControllerType yankedMbc;
98 uint32_t romCrc32;
99 struct VFile* romVf;
100 struct VFile* biosVf;
101 struct VFile* sramVf;
102 struct VFile* sramRealVf;
103 uint32_t sramSize;
104 int sramDirty;
105 int32_t sramDirtAge;
106 bool sramMaskWriteback;
107
108 int sgbBit;
109 int currentSgbBits;
110 uint8_t sgbPacket[16];
111 uint8_t sgbControllers;
112 uint8_t sgbCurrentController;
113 bool sgbIncrement;
114
115 struct mCoreCallbacksList coreCallbacks;
116 struct mAVStream* stream;
117
118 bool cpuBlocked;
119 bool earlyExit;
120 struct mTimingEvent eiPending;
121 unsigned doubleSpeed;
122
123 bool allowOpposingDirections;
124};
125
126struct GBCartridge {
127 uint8_t entry[4];
128 uint8_t logo[48];
129 union {
130 char titleLong[16];
131 struct {
132 char titleShort[11];
133 char maker[4];
134 uint8_t cgb;
135 };
136 };
137 char licensee[2];
138 uint8_t sgb;
139 uint8_t type;
140 uint8_t romSize;
141 uint8_t ramSize;
142 uint8_t region;
143 uint8_t oldLicensee;
144 uint8_t version;
145 uint8_t headerChecksum;
146 uint16_t globalChecksum;
147 // And ROM data...
148};
149
150void GBCreate(struct GB* gb);
151void GBDestroy(struct GB* gb);
152
153void GBReset(struct LR35902Core* cpu);
154void GBSkipBIOS(struct GB* gb);
155void GBMapBIOS(struct GB* gb);
156void GBUnmapBIOS(struct GB* gb);
157void GBDetectModel(struct GB* gb);
158
159void GBUpdateIRQs(struct GB* gb);
160void GBHalt(struct LR35902Core* cpu);
161
162struct VFile;
163bool GBLoadROM(struct GB* gb, struct VFile* vf);
164bool GBLoadSave(struct GB* gb, struct VFile* vf);
165void GBUnloadROM(struct GB* gb);
166void GBSynthesizeROM(struct VFile* vf);
167void GBYankROM(struct GB* gb);
168
169void GBLoadBIOS(struct GB* gb, struct VFile* vf);
170
171void GBSramClean(struct GB* gb, uint32_t frameCount);
172void GBResizeSram(struct GB* gb, size_t size);
173void GBSavedataMask(struct GB* gb, struct VFile* vf, bool writeback);
174void GBSavedataUnmask(struct GB* gb);
175
176struct Patch;
177void GBApplyPatch(struct GB* gb, struct Patch* patch);
178
179void GBGetGameTitle(const struct GB* gba, char* out);
180void GBGetGameCode(const struct GB* gba, char* out);
181
182void GBTestKeypadIRQ(struct GB* gb);
183
184void GBFrameStarted(struct GB* gb);
185void GBFrameEnded(struct GB* gb);
186
187CXX_GUARD_END
188
189#endif