Skip to content

Commit

Permalink
Initial work on rom gpio and rtc
Browse files Browse the repository at this point in the history
  • Loading branch information
Gericom committed Oct 5, 2024
1 parent 1bd147d commit 69e8678
Show file tree
Hide file tree
Showing 7 changed files with 693 additions and 1 deletion.
1 change: 1 addition & 0 deletions code/core/arm9/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SOURCES := source \
source/Peripherals \
source/Peripherals/Graphics \
source/Peripherals/Interrupts \
source/Peripherals/RomGpio \
source/Peripherals/Sound \
source/Save \
source/SdCache \
Expand Down
11 changes: 10 additions & 1 deletion code/core/arm9/source/MemoryEmulator/MemoryStore16.s
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,16 @@ arm_func memu_store16Oam
bx lr

arm_func memu_store16Rom
bx lr
bic r10, r8, #0xFE000000
sub r10, r10, #0xC4
cmp r10, #(0xC4 - 0xC8)
bxhi lr // not rom gpio

push {r0-r3,lr}
mov r0, r10 // offset
mov r1, r9 // value
bl rio_write
pop {r0-r3,pc}

arm_func memu_store16Sram
tst r8, #1
Expand Down
66 changes: 66 additions & 0 deletions code/core/arm9/source/Peripherals/RomGpio/RomGpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "common.h"
#include "RomGpioRtc.h"
#include "RomGpio.h"

RomGpio gRomGpio;
static RomGpioRtc sRomGpioRtc;

void RomGpio::Initialize(rio_registers_t* romGpioRegisters)
{
_registers = romGpioRegisters;
_registersRomData = *romGpioRegisters;
Reset();
}

void RomGpio::Reset()
{
_inputData = 0;
_outputData = 0;
_direction = 0;
_control = RIO_CONTROL_READ_DISABLE;
UpdateRomRegisters();
}

void RomGpio::UpdateRomRegisters()
{
if (_control == RIO_CONTROL_READ_DISABLE)
{
// When reading of the registers is disabled, the original rom data is read.
*_registers = _registersRomData;
}
else
{
_registers->data = GetGpioState();
_registers->direction = _direction;
_registers->control = RIO_CONTROL_READ_ENABLE;
}
}

static void updateRomGpioPeripherals()
{
sRomGpioRtc.Update(gRomGpio);
}

extern "C" void rio_write(u32 offset, u16 value)
{
switch (offset)
{
case offsetof(rio_registers_t, data):
{
gRomGpio.WriteDataRegister(value);
updateRomGpioPeripherals();
break;
}
case offsetof(rio_registers_t, direction):
{
gRomGpio.WriteDirectionRegister(value);
updateRomGpioPeripherals();
break;
}
case offsetof(rio_registers_t, control):
{
gRomGpio.WriteControlRegister(value);
break;
}
}
}
88 changes: 88 additions & 0 deletions code/core/arm9/source/Peripherals/RomGpio/RomGpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

#define RIO_GBA_ADDRESS 0x080000C4

#define RIO_PIN_MASK 0xF

#define RIO_CONTROL_READ_DISABLE 0
#define RIO_CONTROL_READ_ENABLE 1
#define RIO_CONTROL_MASK 1

struct rio_registers_t
{
u16 data;
u16 direction;
u16 control;
};

class RomGpio
{
public:
void Initialize(rio_registers_t* romGpioRegisters);
void Reset();
void UpdateRomRegisters();

void WriteDataRegister(u16 value)
{
_outputData = value & RIO_PIN_MASK;
UpdateRomRegisters();
}

void WriteDirectionRegister(u16 value)
{
_direction = value & RIO_PIN_MASK;
UpdateRomRegisters();
}

void WriteControlRegister(u16 value)
{
_control = value & RIO_CONTROL_MASK;
UpdateRomRegisters();
}

bool GetPinState(u32 pin)
{
return (GetGpioState() >> pin) & 1;
}

void SetPinState(u32 pin, bool isHigh)
{
u32 mask = (1 << pin) & RIO_PIN_MASK;
if (isHigh)
{
_inputData |= mask;
}
else
{
_inputData &= ~mask;
}

UpdateRomRegisters();
}

private:
/// @brief Pointer to the actual registers readable by the GBA side.
rio_registers_t* _registers;

/// @brief The rom data behind the gpio registers.
rio_registers_t _registersRomData;

/// @brief The input data from the gpio pins.
u16 _inputData;

/// @brief The internal output data register.
u16 _outputData;

/// @brief The internal direction register.
u16 _direction;

/// @brief The internal control register.
u16 _control;

u32 GetGpioState()
{
return (_inputData & ~_direction) | (_outputData & _direction);
}
};

extern RomGpio gRomGpio;
Loading

0 comments on commit 69e8678

Please sign in to comment.