Memory Mapped IO

The GBA hardware features are accessed by reading and writing to addresses mapped as memory. This is known as “Memory Mapped Input/Output”.

Addresses from 4000000h to 40003FEh are mapped to the various I/O registers of the GBA.

gba-plusplus provides the gba::*memmap<> accessor types for memory mapped IO.

These accessor types provide volatile read/write operations that can also bit-cast the IO registers into non-fundamental types.

template<typename Type, unsigned Address>
class memmap

Base memory mapped register type

tparam Type

value type stored in the memory mapping

tparam Address

location within the memory mapping

Subclassed by gba::imemmap< Type, Address >, gba::omemmap< Type, Address >

Input (read only)

using reg_vcount = gba::imemmap<short, 0x4000006>;

// Wait for vblank
while ( reg_vcount::read() < 160 );
template<typename Type, unsigned Address>
class gba::imemmap : public gba::memmap<Type, Address>

Input (read-only) memory mapped register type

tparam Type

value type stored in the memory mapping

tparam Address

location within the memory mapping

Subclassed by gba::iomemmap< Type, Address >

Public Types

using type = typename memmap<Type, Address>::type

Value type stored in the memory mapping.

Public Static Functions

static inline type read() noexcept

Read value stored in this memory map

Returns

value stored in this memory mapping

Public Static Attributes

static constexpr auto address = memmap<Type, Address>::address

Address location within the memory mapping.

Output (write only)

struct mosaic {
    int bg_hsize : 4;
    int bg_vsize : 4;
    int obj_hsize : 4;
    int obj_vsize : 4;
};

using reg_mosaic = gba::omemmap<mosaic, 0x400004C>;

// Copy into IO register
auto m = mosaic { 2, 2, 0, 0 };
reg_mosaic::write( m );

// Move into IO register
reg_mosaic::write( mosaic { 2, 2, 0, 0 } );

// Initialize from generator
reg_mosaic::generate( []() {
    return mosaic { 2, 2, 0, 0 };
} );

// Emplace struct into IO register
reg_mosaic::emplace( 2, 2, 0, 0 );
template<typename Type, unsigned Address>
class gba::omemmap : public gba::memmap<Type, Address>

Output (write-only) memory mapped register type

tparam Type

value type stored in the memory mapping

tparam Address

location within the memory mapping

Subclassed by gba::iomemmap< Type, Address >

Public Types

using type = typename memmap<Type, Address>::type

Value type stored in the memory mapping.

Public Static Functions

static inline void write(type &&value) noexcept

Move value into this memory map

Parameters

value – value to move into this memory map

static inline void write(const type &value) noexcept

Copy value into this memory map

Parameters

value – value to copy into this memory map

template<typename ...Args>
static inline void emplace(Args&&... args) noexcept

Construct value into this memory map with the provided arguments

Template Parameters

Args – variadic argument types

Parameters

args – variadic list of arguments

template<class Generator>
static inline void generate(Generator g) noexcept

Calls generator to provide value to write into this memory map

Template Parameters

Generator – function object type

Parameters

g – function object that will be called

Public Static Attributes

static constexpr auto address = memmap<Type, Address>::address

Address location within the memory mapping.

Input & output (read & write)

struct ie {
    bool vblank : 1;
    bool hblank : 1;
    // ...
};

using reg_ie = gba::iomemmap<ie, 0x4000200>;

// Read & modify IO register
reg_ie::transform( []( ie& irqs ) {
    irqs.vblank = true;
    irqs.hlank = false;
} );
template<typename Type, unsigned Address>
class gba::iomemmap : public gba::imemmap<Type, Address>, public gba::omemmap<Type, Address>

Input/output (read/write) memory mapped register type

tparam Type

value type stored in the memory mapping

tparam Address

location within the memory mapping

Public Types

using type = typename memmap<Type, Address>::type

Value type stored in the memory mapping.

Public Static Functions

template<class TransformOp>
static inline void transform(TransformOp t) noexcept

Reads the stored value, transforms it, then writes the modified value back

Template Parameters

TransformOp – function object type

Parameters

t – function object that will be called

Public Static Attributes

static constexpr auto address = memmap<Type, Address>::address

Address location within the memory mapping.

Hardware Registers