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 GB {
48 struct mCPUComponent d;
49
50 struct LR35902Core* cpu;
51 struct GBMemory memory;
52 struct GBVideo video;
53 struct GBTimer timer;
54 struct GBAudio audio;
55 struct GBSIO sio;
56 enum GBModel model;
57
58 struct mCoreSync* sync;
59
60 uint8_t* keySource;
61
62 void* pristineRom;
63 size_t pristineRomSize;
64 size_t yankedRomSize;
65 uint32_t romCrc32;
66 struct VFile* romVf;
67 struct VFile* biosVf;
68 struct VFile* sramVf;
69 struct VFile* sramRealVf;
70 uint32_t sramSize;
71 int sramDirty;
72 int32_t sramDirtAge;
73 bool sramMaskWriteback;
74
75 struct mAVStream* stream;
76
77 int32_t eiPending;
78 unsigned doubleSpeed;
79};
80
81struct GBCartridge {
82 uint8_t entry[4];
83 uint8_t logo[48];
84 union {
85 char titleLong[16];
86 struct {
87 char titleShort[11];
88 char maker[4];
89 uint8_t cgb;
90 };
91 };
92 char licensee[2];
93 uint8_t sgb;
94 uint8_t type;
95 uint8_t romSize;
96 uint8_t ramSize;
97 uint8_t region;
98 uint8_t oldLicensee;
99 uint8_t version;
100 uint8_t headerChecksum;
101 uint16_t globalChecksum;
102 // And ROM data...
103};
104
105void GBCreate(struct GB* gb);
106void GBDestroy(struct GB* gb);
107
108void GBReset(struct LR35902Core* cpu);
109void GBDetectModel(struct GB* gb);
110
111void GBUpdateIRQs(struct GB* gb);
112void GBHalt(struct LR35902Core* cpu);
113
114struct VFile;
115bool GBLoadROM(struct GB* gb, struct VFile* vf);
116bool GBLoadSave(struct GB* gb, struct VFile* vf);
117void GBYankROM(struct GB* gb);
118void GBUnloadROM(struct GB* gb);
119
120bool GBIsBIOS(struct VFile* vf);
121void GBLoadBIOS(struct GB* gb, struct VFile* vf);
122
123void GBSramClean(struct GB* gb, uint32_t frameCount);
124void GBResizeSram(struct GB* gb, size_t size);
125void GBSavedataMask(struct GB* gb, struct VFile* vf, bool writeback);
126void GBSavedataUnmask(struct GB* gb);
127
128struct Patch;
129void GBApplyPatch(struct GB* gb, struct Patch* patch);
130
131bool GBIsROM(struct VFile* vf);
132void GBGetGameTitle(const struct GB* gba, char* out);
133void GBGetGameCode(const struct GB* gba, char* out);
134
135void GBFrameEnded(struct GB* gb);
136
137#endif