Loading...
Searching...
No Matches
periph_gba.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2026 HAW Hamburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
17
18#include <stdint.h>
19
20#include "assert.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
29#define REG32(ADDR) (*(uint32_t volatile(*))(ADDR))
30
34#define REG16(ADDR) (*(uint16_t volatile(*))(ADDR))
35
39#define REG8(ADDR) (*(uint8_t volatile(*))(ADDR))
40
44#define MEM16(ADDR) ((uint16_t(*))(ADDR))
45
49#define GBA_PTR_TO_IRQ_HANDLER REG32(0x3007ffc)
50
71#define GBA_IE REG16(0x4000200)
72
76#define GBA_IE_TIMERS (0x0F << 3)
77
81#define GBA_IE_UART (1 << 7)
82
103#define GBA_IF REG16(0x4000202)
104
108#define GBA_IF_CLEAR_ALL (0x3FFF)
109
113#define GBA_IF_TIMERS (0x0F << 3)
114
118#define GBA_IF_UART (1 << 7)
119
125#define GBA_IME REG16(0x4000208) /* Technically 32 bit reg, but only bit 0 is used */
126
131#define GBA_TM0_CNT_L REG16(0x4000100)
132#define GBA_TM0_CNT_H REG16(0x4000102)
133#define GBA_TM1_CNT_L REG16(0x4000104)
134#define GBA_TM1_CNT_H REG16(0x4000106)
135#define GBA_TM2_CNT_L REG16(0x4000108)
136#define GBA_TM2_CNT_H REG16(0x400010A)
137#define GBA_TM3_CNT_L REG16(0x400010C)
138#define GBA_TM3_CNT_H REG16(0x400010E)
140
144#define GBA_TIMER_MAX_TICKS (0xFFFF)
145
150#define GBA_TIMER_FRQ_16K (16400lu)
151#define GBA_TIMER_FRQ_65K (65500lu)
152#define GBA_TIMER_FRQ_262K (262200lu)
153#define GBA_TIMER_FRQ_16M (16780000lu)
155
159typedef struct {
160 uint16_t current_value;
161 uint16_t prescaler : 2;
164 uint16_t unused_3_5 : 3;
165 uint16_t irq_enable : 1;
166 uint16_t run : 1;
167 uint16_t unused_8_15 : 8;
168} gba_timer;
169/* I don't trust bit-fields over all compilers & toolchains */
170static_assert(sizeof(gba_timer) == 4, "2 x Timer register should be 4 bytes");
171
175typedef volatile gba_timer gba_timer_t;
176
181#define GBA_TIMER_0 ((gba_timer_t *)0x4000100)
182#define GBA_TIMER_1 ((gba_timer_t *)0x4000104)
183#define GBA_TIMER_2 ((gba_timer_t *)0x4000108)
184#define GBA_TIMER_3 ((gba_timer_t *)0x400010C)
186
190#define GBA_SIO_SCNT REG16(0x4000128)
191
195#define GBA_SIO_DATA8 REG8(0x400012A)
196
200#define GBA_SIO_RCNT REG16(0x4000134)
201
205struct gba_uart {
206 uint16_t baud : 2;
207 uint16_t cts : 1;
208 uint16_t parity : 1;
209 uint16_t send_data_full : 1;
210 uint16_t receive_data_empty : 1;
211 uint16_t error : 1;
212 uint16_t data_bits : 1;
213 uint16_t fifo_enable : 1;
214 uint16_t parity_enable : 1;
215 uint16_t send_enable : 1;
216 uint16_t receive_enable : 1;
217 uint16_t must_be_1_for_uart : 2;
218 uint16_t irq_enable : 1;
219 uint16_t unused_15 : 1;
220};
221
225typedef struct gba_uart gba_uart_t;
226
230#define GBA_UART ((volatile gba_uart_t *) 0x4000128)
231
236#define GBA_SCREEN_WIDTH (240)
237#define GBA_SCREEN_HEIGHT (160)
239
243#define GBA_DISPCNT REG16(0x4000000)
244
249#define GBA_DISPCNT_BGMODE_0 0x0000
250#define GBA_DISPCNT_BGMODE_1 0x0001
251#define GBA_DISPCNT_BGMODE_2 0x0002
252#define GBA_DISPCNT_BGMODE_3 0x0003
253#define GBA_DISPCNT_BGMODE_4 0x0004
254#define GBA_DISPCNT_BGMODE_5 0x0005
255
256#define GBA_DISPCNT_SDBG_0 0x0100
257#define GBA_DISPCNT_SDBG_1 0x0200
258#define GBA_DISPCNT_SDBG_2 0x0400
259#define GBA_DISPCNT_SDBG_3 0x0800
260
261#define GBA_DISPCNT_SDOBJ 0x1000
263
267#define GBA_DISPSTAT REG16(0x4000004)
268
273#define GBA_BG0CNT REG16(0x4000008)
274#define GBA_BG1CNT REG16(0x400000a)
275#define GBA_BG2CNT REG16(0x400000c)
276#define GBA_BG3CNT REG16(0x400000e)
278
282#define GBA_VRAM_PALETTE MEM16(0x5000000)
283
287#define GBA_VRAM MEM16(0x6000000)
288
289#ifdef __cplusplus
290}
291#endif
292
POSIX.1-2008 compliant version of the assert macro.
volatile gba_timer gba_timer_t
The type for the GBA timers.
Definition periph_gba.h:175
struct gba_uart gba_uart_t
The type for the GBA uart.
Definition periph_gba.h:225
Struct whose members map onto the timer register.
Definition periph_gba.h:159
uint16_t prescaler
The prescaler sets the divider for this timer.
Definition periph_gba.h:161
uint16_t count_up_on_neighbour_overflow
If set, counts up this timer when the previous overflowed.
Definition periph_gba.h:162
uint16_t unused_8_15
unused
Definition periph_gba.h:167
uint16_t unused_3_5
unused
Definition periph_gba.h:164
uint16_t irq_enable
enables interrupt request for this timer on overflow
Definition periph_gba.h:165
uint16_t run
starts the timer
Definition periph_gba.h:166
uint16_t current_value
The counter / current value of this timer.
Definition periph_gba.h:160
Struct whose members map onto the uart register.
Definition periph_gba.h:205
uint16_t fifo_enable
Enable FIFO.
Definition periph_gba.h:213
uint16_t must_be_1_for_uart
Config bits, unrelated to UART configuration.
Definition periph_gba.h:217
uint16_t unused_15
Unused.
Definition periph_gba.h:219
uint16_t cts
0: Always Send, 1: Wait for SC=LOW
Definition periph_gba.h:207
uint16_t send_data_full
Send FIFO full.
Definition periph_gba.h:209
uint16_t receive_data_empty
Receive FIFO empty.
Definition periph_gba.h:210
uint16_t error
Error occurred.
Definition periph_gba.h:211
uint16_t baud
Baud rate, 0:9600, 38400, 57600, 3:115200.
Definition periph_gba.h:206
uint16_t parity
0: even, 1: odd
Definition periph_gba.h:208
uint16_t irq_enable
Enable IRQ on send_data_full, receive_data_empty and error.
Definition periph_gba.h:218
uint16_t data_bits
0: 7 bits, 1: 8 bits
Definition periph_gba.h:212
uint16_t parity_enable
Enable parity.
Definition periph_gba.h:214
uint16_t receive_enable
Enable receiving data.
Definition periph_gba.h:216
uint16_t send_enable
Enable sending data.
Definition periph_gba.h:215