Skip to content

Commit

Permalink
Implemented rtc support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gericom committed Sep 11, 2019
1 parent 89c01e3 commit ce32975
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 6 deletions.
13 changes: 12 additions & 1 deletion arm7/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "gbsound.h"
#include "save.h"
#include "dldi_handler.h"
#include "fifo.h"
#include "../../common/fifo.h"
#include "../../common/common_defs.s"

static void vblank_handler()
Expand Down Expand Up @@ -186,6 +186,17 @@ int main()
}
break;
}
case 0xAA550100: //get rtc data
{
u8 dateTime[8];
u8 cmd = READ_TIME_AND_DATE;
rtcTransaction(&cmd, 1, dateTime, 7);
cmd = READ_STATUS_REG1;
rtcTransaction(&cmd, 1, &dateTime[7], 1);
REG_SEND_FIFO = *(u32*)&dateTime[0];
REG_SEND_FIFO = *(u32*)&dateTime[4];
break;
}
case 0x040000A0:
while (REG_FIFO_CNT & FIFO_CNT_EMPTY);
val = REG_RECV_FIFO;
Expand Down
2 changes: 1 addition & 1 deletion arm7/source/sound.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nds.h>
#include <string.h>
#include "timer.h"
#include "fifo.h"
#include "../../common/fifo.h"
#include "../../common/sd_vram.h"
#include "lock.h"
#include "sound.h"
Expand Down
17 changes: 16 additions & 1 deletion arm9/source/emu/handle_address_write.s
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ write_address_from_handler_16bit:
.word write_address_ignore
.word write_address_from_handler_vram_16
.word write_address_ignore
.word write_address_ignore
.word write_address_from_handler_rom_gpio_16
.word write_address_ignore
.word write_address_ignore
.word write_address_ignore
Expand Down Expand Up @@ -144,6 +144,21 @@ write_address_from_handler_vram_16:
strh r11, [r10]
bx lr

write_address_from_handler_rom_gpio_16:
ldr r13,= 0x080000C4
subs r13, r9, r13
bxlt lr
cmp r13, #0x4
bxgt lr
ldr sp,= address_dtcm + (16 * 1024)
push {r0-r3,lr}
mov r0, r9
mov r1, r11
ldr r12,= rio_write
blx r12
pop {r0-r3,lr}
bx lr

write_address_from_handler_sram_16:
ldr r12,= 0x01FF0000
bic r10, r9, r12
Expand Down
15 changes: 15 additions & 0 deletions arm9/source/emu/romGpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

typedef u16 RomGpioHwMask;

#define RIO_NONE 0
#define RIO_RTC (1 << 0)
#define RIO_LIGHT (1 << 1)

extern u16 gRioGpioData;
extern u16 gRioGpioDirection;
extern u16 gRioGpioControl;

void rio_init(RomGpioHwMask forceHwMask);
extern "C" void rio_write(u32 addr, u16 val);
void rio_invalidate();
130 changes: 130 additions & 0 deletions arm9/source/emu/romGpio.vram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "vram.h"
#include "consts.s"
#include "romGpioRtc.h"
#include "romGpio.h"

struct game_hw_info_t
{
u32 gameCode;
RomGpioHwMask hardware;
};

#define GAMECODE(x) ((((x) & 0xFF) << 24) | ((((x) >> 8) & 0xFF) << 16) | ((((x) >> 16) & 0xFF) << 8) | ((x) >> 24))

static game_hw_info_t sGameHardwareTable[] =
{
// Boktai: The Sun is in Your Hand
{ GAMECODE('U3IJ'), RIO_RTC | RIO_LIGHT },
{ GAMECODE('U3IE'), RIO_RTC | RIO_LIGHT },
{ GAMECODE('U3IP'), RIO_RTC | RIO_LIGHT },

// Boktai 2: Solar Boy Django
{ GAMECODE('U32J'), RIO_RTC | RIO_LIGHT },
{ GAMECODE('U32E'), RIO_RTC | RIO_LIGHT },
{ GAMECODE('U32P'), RIO_RTC | RIO_LIGHT },

// Pokemon Ruby
{ GAMECODE('AXVJ'), RIO_RTC },
{ GAMECODE('AXVE'), RIO_RTC },
{ GAMECODE('AXVP'), RIO_RTC },
{ GAMECODE('AXVI'), RIO_RTC },
{ GAMECODE('AXVS'), RIO_RTC },
{ GAMECODE('AXVD'), RIO_RTC },
{ GAMECODE('AXVF'), RIO_RTC },

// Pokemon Sapphire
{ GAMECODE('AXPJ'), RIO_RTC },
{ GAMECODE('AXPE'), RIO_RTC },
{ GAMECODE('AXPP'), RIO_RTC },
{ GAMECODE('AXPI'), RIO_RTC },
{ GAMECODE('AXPS'), RIO_RTC },
{ GAMECODE('AXPD'), RIO_RTC },
{ GAMECODE('AXPF'), RIO_RTC },

// Pokemon Emerald
{ GAMECODE('BPEJ'), RIO_RTC },
{ GAMECODE('BPEE'), RIO_RTC },
{ GAMECODE('BPEP'), RIO_RTC },
{ GAMECODE('BPEI'), RIO_RTC },
{ GAMECODE('BPES'), RIO_RTC },
{ GAMECODE('BPED'), RIO_RTC },
{ GAMECODE('BPEF'), RIO_RTC },

// RockMan EXE 4.5 - Real Operation
{ GAMECODE('BR4J'), RIO_RTC },

// Sennen Kazoku
{ GAMECODE('BKAJ'), RIO_RTC },

// Shin Bokura no Taiyou: Gyakushuu no Sabata
{ GAMECODE('U33J'), RIO_RTC | RIO_LIGHT },
};

#define RIO_REG_DATA 0xC4
#define RIO_REG_DIRECTION 0xC6
#define RIO_REG_CONTROL 0xC8

static RomGpioHwMask sGpioHwMask;

u16 gRioGpioData;
u16 gRioGpioDirection;
u16 gRioGpioControl;

void rio_init(RomGpioHwMask forceHwMask)
{
sGpioHwMask = forceHwMask;
u32 gameCode = *(u32*)(MAIN_MEMORY_ADDRESS_ROM_DATA + 0xAC);
for(int i = 0; i < sizeof(sGameHardwareTable) / sizeof(sGameHardwareTable[0]); i++)
{
if(sGameHardwareTable[i].gameCode == gameCode)
{
sGpioHwMask = sGameHardwareTable[i].hardware | forceHwMask;
break;
}
}
gRioGpioData = 0;
gRioGpioDirection = 0;
gRioGpioControl = 0;
if(sGpioHwMask & RIO_RTC)
rio_rtcInit();
}

static void updateHardware()
{
if(sGpioHwMask & RIO_RTC)
rio_rtcUpdate();
}

extern "C" void rio_write(u32 addr, u16 val)
{
switch(addr - 0x08000000)
{
case RIO_REG_DATA:
gRioGpioData = (gRioGpioData & ~gRioGpioDirection) | (val & gRioGpioDirection);
updateHardware();
break;
case RIO_REG_DIRECTION:
gRioGpioDirection = val & 0xF;
break;
case RIO_REG_CONTROL:
gRioGpioControl = val & 1;
break;
}
rio_invalidate();
}

void rio_invalidate()
{
if(gRioGpioControl)
{
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_DATA) = gRioGpioData;
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_DIRECTION) = gRioGpioDirection;
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_CONTROL) = gRioGpioControl;
}
else
{
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_DATA) = 0;
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_DIRECTION) = 0;
*(u16*)(MAIN_MEMORY_ADDRESS_ROM_DATA + RIO_REG_CONTROL) = 0;
}
}
4 changes: 4 additions & 0 deletions arm9/source/emu/romGpioRtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void rio_rtcInit();
void rio_rtcUpdate();
Loading

0 comments on commit ce32975

Please sign in to comment.