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