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