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 GBARotationSource;
58
59struct GBABoard {
60 struct ARMBoard d;
61 struct GBA* p;
62};
63
64struct GBA {
65 struct ARMCore cpu;
66 struct GBABoard board;
67 struct GBAMemory memory;
68 struct GBAVideo video;
69 struct GBAAudio audio;
70
71 struct GBASync* sync;
72
73 struct ARMDebugger* debugger;
74
75 int timersEnabled;
76 struct GBATimer {
77 uint16_t reload;
78 uint16_t oldReload;
79 int32_t lastEvent;
80 int32_t nextEvent;
81 int32_t overflowInterval;
82 unsigned prescaleBits : 4;
83 unsigned countUp : 1;
84 unsigned doIrq : 1;
85 unsigned enable : 1;
86 } timers[4];
87
88 int springIRQ;
89 int* keySource;
90 struct GBARotationSource* rotationSource;
91
92 const char* activeFile;
93 const char* savefile;
94 int logLevel;
95};
96
97struct GBACartridge {
98 uint32_t entry;
99 uint8_t logo[156];
100 char title[12];
101 uint32_t id;
102 uint16_t maker;
103 uint8_t type;
104 uint8_t unit;
105 uint8_t device;
106 uint8_t reserved[7];
107 uint8_t version;
108 uint8_t checksum;
109 // And ROM data...
110};
111
112void GBAInit(struct GBA* gba);
113void GBADeinit(struct GBA* gba);
114
115void GBAMemoryInit(struct GBAMemory* memory);
116void GBAMemoryDeinit(struct GBAMemory* memory);
117
118void GBABoardInit(struct GBABoard* board);
119void GBABoardReset(struct ARMBoard* board);
120
121void GBATimerUpdateRegister(struct GBA* gba, int timer);
122void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
123void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
124
125void GBAWriteIE(struct GBA* gba, uint16_t value);
126void GBAWriteIME(struct GBA* gba, uint16_t value);
127void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
128void GBATestIRQ(struct ARMBoard* board);
129int GBAWaitForIRQ(struct GBA* gba);
130int GBAHalt(struct GBA* gba);
131
132void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
133
134void GBALoadROM(struct GBA* gba, int fd, const char* fname);
135void GBALoadBIOS(struct GBA* gba, int fd);
136
137__attribute__((format (printf, 3, 4)))
138void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
139
140#endif