src/gba/gba.h (view raw)
1#ifndef GBA_H
2#define GBA_H
3
4#include "common.h"
5
6#include "arm.h"
7#include "debugger/debugger.h"
8
9#include "gba-memory.h"
10#include "gba-video.h"
11#include "gba-audio.h"
12#include "gba-sio.h"
13
14extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
15
16enum GBAIRQ {
17 IRQ_VBLANK = 0x0,
18 IRQ_HBLANK = 0x1,
19 IRQ_VCOUNTER = 0x2,
20 IRQ_TIMER0 = 0x3,
21 IRQ_TIMER1 = 0x4,
22 IRQ_TIMER2 = 0x5,
23 IRQ_TIMER3 = 0x6,
24 IRQ_SIO = 0x7,
25 IRQ_DMA0 = 0x8,
26 IRQ_DMA1 = 0x9,
27 IRQ_DMA2 = 0xA,
28 IRQ_DMA3 = 0xB,
29 IRQ_KEYPAD = 0xC,
30 IRQ_GAMEPAK = 0xD
31};
32
33enum GBAError {
34 GBA_NO_ERROR = 0,
35 GBA_OUT_OF_MEMORY = -1
36};
37
38enum GBALogLevel {
39 GBA_LOG_STUB = 0x01,
40 GBA_LOG_DEBUG = 0x02,
41 GBA_LOG_INFO = 0x04,
42 GBA_LOG_WARN = 0x08,
43 GBA_LOG_ERROR = 0x10,
44 GBA_LOG_FATAL = 0x20,
45
46 GBA_LOG_GAME_ERROR = 0x100
47};
48
49enum GBAKey {
50 GBA_KEY_A = 0,
51 GBA_KEY_B = 1,
52 GBA_KEY_SELECT = 2,
53 GBA_KEY_START = 3,
54 GBA_KEY_RIGHT = 4,
55 GBA_KEY_LEFT = 5,
56 GBA_KEY_UP = 6,
57 GBA_KEY_DOWN = 7,
58 GBA_KEY_R = 8,
59 GBA_KEY_L = 9,
60 GBA_KEY_NONE = -1
61};
62
63struct GBARotationSource;
64struct GBA;
65typedef void (*GBALogHandler)(struct GBA*, enum GBALogLevel, const char* format, va_list args);
66
67struct GBA {
68 struct ARMCore cpu;
69 struct GBAMemory memory;
70 struct GBAVideo video;
71 struct GBAAudio audio;
72 struct GBASIO sio;
73
74 struct GBASync* sync;
75
76 struct ARMDebugger* debugger;
77
78 int timersEnabled;
79 struct GBATimer {
80 uint16_t reload;
81 uint16_t oldReload;
82 int32_t lastEvent;
83 int32_t nextEvent;
84 int32_t overflowInterval;
85 unsigned prescaleBits : 4;
86 unsigned countUp : 1;
87 unsigned doIrq : 1;
88 unsigned enable : 1;
89 } timers[4];
90
91 int springIRQ;
92 uint32_t biosChecksum;
93 int* keySource;
94 struct GBARotationSource* rotationSource;
95 struct GBARumble* rumble;
96
97 const char* activeFile;
98 const char* savefile;
99
100 int logLevel;
101 GBALogHandler logHandler;
102};
103
104struct GBACartridge {
105 uint32_t entry;
106 uint8_t logo[156];
107 char title[12];
108 uint32_t id;
109 uint16_t maker;
110 uint8_t type;
111 uint8_t unit;
112 uint8_t device;
113 uint8_t reserved[7];
114 uint8_t version;
115 uint8_t checksum;
116 // And ROM data...
117};
118
119void GBAInit(struct GBA* gba);
120void GBADeinit(struct GBA* gba);
121
122void GBABoardInit(struct ARMBoard* board);
123void GBABoardReset(struct ARMCore* cpu);
124
125void GBATimerUpdateRegister(struct GBA* gba, int timer);
126void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
127void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
128
129void GBAWriteIE(struct GBA* gba, uint16_t value);
130void GBAWriteIME(struct GBA* gba, uint16_t value);
131void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
132void GBATestIRQ(struct ARMCore* cpu);
133void GBAHalt(struct GBA* gba);
134
135void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
136void GBADetachDebugger(struct GBA* gba);
137
138void GBALoadROM(struct GBA* gba, int fd, const char* fname);
139void GBALoadBIOS(struct GBA* gba, int fd);
140
141__attribute__((format (printf, 3, 4)))
142void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
143
144__attribute__((format (printf, 3, 4)))
145void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
146
147#endif