src/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 "util/common.h"
10
11CXX_GUARD_START
12
13#include "core/log.h"
14#include "core/timing.h"
15
16#include "lr35902/lr35902.h"
17
18#include "gb/audio.h"
19#include "gb/interface.h"
20#include "gb/memory.h"
21#include "gb/sio.h"
22#include "gb/timer.h"
23#include "gb/video.h"
24
25extern const uint32_t DMG_LR35902_FREQUENCY;
26extern const uint32_t CGB_LR35902_FREQUENCY;
27extern const uint32_t SGB_LR35902_FREQUENCY;
28
29mLOG_DECLARE_CATEGORY(GB);
30
31// TODO: Prefix GBAIRQ
32enum GBIRQ {
33 GB_IRQ_VBLANK = 0x0,
34 GB_IRQ_LCDSTAT = 0x1,
35 GB_IRQ_TIMER = 0x2,
36 GB_IRQ_SIO = 0x3,
37 GB_IRQ_KEYPAD = 0x4,
38};
39
40enum GBIRQVector {
41 GB_VECTOR_VBLANK = 0x40,
42 GB_VECTOR_LCDSTAT = 0x48,
43 GB_VECTOR_TIMER = 0x50,
44 GB_VECTOR_SIO = 0x58,
45 GB_VECTOR_KEYPAD = 0x60,
46};
47
48struct mCoreSync;
49struct mAVStream;
50struct mCoreCallbacks;
51struct GB {
52 struct mCPUComponent d;
53
54 struct LR35902Core* cpu;
55 struct GBMemory memory;
56 struct GBVideo video;
57 struct GBTimer timer;
58 struct GBAudio audio;
59 struct GBSIO sio;
60 enum GBModel model;
61
62 struct mCoreSync* sync;
63 struct mTiming timing;
64
65 uint8_t* keySource;
66
67 void* pristineRom;
68 size_t pristineRomSize;
69 size_t yankedRomSize;
70 uint32_t romCrc32;
71 struct VFile* romVf;
72 struct VFile* biosVf;
73 struct VFile* sramVf;
74 struct VFile* sramRealVf;
75 uint32_t sramSize;
76 int sramDirty;
77 int32_t sramDirtAge;
78 bool sramMaskWriteback;
79
80 struct mCoreCallbacks* coreCallbacks;
81 struct mAVStream* stream;
82
83 bool cpuBlocked;
84 bool earlyExit;
85 struct mTimingEvent eiPending;
86 unsigned doubleSpeed;
87};
88
89struct GBCartridge {
90 uint8_t entry[4];
91 uint8_t logo[48];
92 union {
93 char titleLong[16];
94 struct {
95 char titleShort[11];
96 char maker[4];
97 uint8_t cgb;
98 };
99 };
100 char licensee[2];
101 uint8_t sgb;
102 uint8_t type;
103 uint8_t romSize;
104 uint8_t ramSize;
105 uint8_t region;
106 uint8_t oldLicensee;
107 uint8_t version;
108 uint8_t headerChecksum;
109 uint16_t globalChecksum;
110 // And ROM data...
111};
112
113void GBCreate(struct GB* gb);
114void GBDestroy(struct GB* gb);
115
116void GBReset(struct LR35902Core* cpu);
117void GBDetectModel(struct GB* gb);
118
119void GBUpdateIRQs(struct GB* gb);
120void GBHalt(struct LR35902Core* cpu);
121
122struct VFile;
123bool GBLoadROM(struct GB* gb, struct VFile* vf);
124bool GBLoadSave(struct GB* gb, struct VFile* vf);
125void GBYankROM(struct GB* gb);
126void GBUnloadROM(struct GB* gb);
127void GBSynthesizeROM(struct VFile* vf);
128
129bool GBIsBIOS(struct VFile* vf);
130void GBLoadBIOS(struct GB* gb, struct VFile* vf);
131
132void GBSramClean(struct GB* gb, uint32_t frameCount);
133void GBResizeSram(struct GB* gb, size_t size);
134void GBSavedataMask(struct GB* gb, struct VFile* vf, bool writeback);
135void GBSavedataUnmask(struct GB* gb);
136
137struct Patch;
138void GBApplyPatch(struct GB* gb, struct Patch* patch);
139
140bool GBIsROM(struct VFile* vf);
141void GBGetGameTitle(const struct GB* gba, char* out);
142void GBGetGameCode(const struct GB* gba, char* out);
143
144void GBFrameEnded(struct GB* gb);
145
146CXX_GUARD_END
147
148#endif