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