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