convert RTC to OOP

This commit is contained in:
Arisotura
2023-11-03 21:20:09 +01:00
parent 440b356674
commit e4f4e94694
6 changed files with 138 additions and 109 deletions

View File

@ -24,52 +24,29 @@
using Platform::Log;
using Platform::LogLevel;
namespace RTC
{
/// This value represents the Nintendo DS IO register,
/// \em not the value of the system's clock.
/// The actual system time is taken directly from the host.
u16 IO;
u8 Input;
u32 InputBit;
u32 InputPos;
u8 Output[8];
u32 OutputBit;
u32 OutputPos;
u8 CurCmd;
StateData State;
s32 TimerError;
u32 ClockCount;
void WriteDateTime(int num, u8 val);
bool Init()
RTC::RTC()
{
NDS::RegisterEventFunc(NDS::Event_RTC, 0, ClockTimer);
NDS::RegisterEventFunc(NDS::Event_RTC, 0, MemberEventFunc(RTC, ClockTimer));
ResetState();
// indicate the power was off
// this will be changed if a previously saved RTC state is loaded
State.StatusReg1 = 0x80;
return true;
}
void DeInit()
RTC::~RTC()
{
NDS::UnregisterEventFunc(NDS::Event_RTC, 0);
}
void Reset()
void RTC::Reset()
{
Input = 0;
InputBit = 0;
@ -84,7 +61,7 @@ void Reset()
ScheduleTimer(true);
}
void DoSavestate(Savestate* file)
void RTC::DoSavestate(Savestate* file)
{
file->Section("RTC.");
@ -107,17 +84,17 @@ void DoSavestate(Savestate* file)
}
u8 BCD(u8 val)
u8 RTC::BCD(u8 val)
{
return (val % 10) | ((val / 10) << 4);
}
u8 FromBCD(u8 val)
u8 RTC::FromBCD(u8 val)
{
return (val & 0xF) + ((val >> 4) * 10);
}
u8 BCDIncrement(u8 val)
u8 RTC::BCDIncrement(u8 val)
{
val++;
if ((val & 0x0F) >= 0x0A)
@ -127,7 +104,7 @@ u8 BCDIncrement(u8 val)
return val;
}
u8 BCDSanitize(u8 val, u8 vmin, u8 vmax)
u8 RTC::BCDSanitize(u8 val, u8 vmin, u8 vmax)
{
if (val < vmin || val > vmax)
val = vmin;
@ -140,12 +117,12 @@ u8 BCDSanitize(u8 val, u8 vmin, u8 vmax)
}
void GetState(StateData& state)
void RTC::GetState(StateData& state)
{
memcpy(&state, &State, sizeof(State));
}
void SetState(StateData& state)
void RTC::SetState(StateData& state)
{
memcpy(&State, &state, sizeof(State));
@ -155,7 +132,7 @@ void SetState(StateData& state)
WriteDateTime(i+1, State.DateTime[i]);
}
void GetDateTime(int& year, int& month, int& day, int& hour, int& minute, int& second)
void RTC::GetDateTime(int& year, int& month, int& day, int& hour, int& minute, int& second)
{
year = FromBCD(State.DateTime[0]);
year += 2000;
@ -176,7 +153,7 @@ void GetDateTime(int& year, int& month, int& day, int& hour, int& minute, int& s
second = FromBCD(State.DateTime[6] & 0x7F);
}
void SetDateTime(int year, int month, int day, int hour, int minute, int second)
void RTC::SetDateTime(int year, int month, int day, int hour, int minute, int second)
{
int monthdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
@ -226,7 +203,7 @@ void SetDateTime(int year, int month, int day, int hour, int minute, int second)
State.StatusReg1 &= ~0x80;
}
void ResetState()
void RTC::ResetState()
{
memset(&State, 0, sizeof(State));
State.DateTime[1] = 1;
@ -234,7 +211,7 @@ void ResetState()
}
void SetIRQ(u8 irq)
void RTC::SetIRQ(u8 irq)
{
u8 oldstat = State.IRQFlag;
State.IRQFlag |= irq;
@ -250,12 +227,12 @@ void SetIRQ(u8 irq)
}
}
void ClearIRQ(u8 irq)
void RTC::ClearIRQ(u8 irq)
{
State.IRQFlag &= ~irq;
}
void ProcessIRQ(int type) // 0=minute carry 1=periodic 2=status reg write
void RTC::ProcessIRQ(int type) // 0=minute carry 1=periodic 2=status reg write
{
// INT1
@ -395,7 +372,7 @@ void ProcessIRQ(int type) // 0=minute carry 1=periodic 2=status reg write
}
u8 DaysInMonth()
u8 RTC::DaysInMonth()
{
u8 numdays;
@ -438,12 +415,12 @@ u8 DaysInMonth()
return numdays;
}
void CountYear()
void RTC::CountYear()
{
State.DateTime[0] = BCDIncrement(State.DateTime[0]);
}
void CountMonth()
void RTC::CountMonth()
{
State.DateTime[1] = BCDIncrement(State.DateTime[1]);
if (State.DateTime[1] > 0x12)
@ -453,7 +430,7 @@ void CountMonth()
}
}
void CheckEndOfMonth()
void RTC::CheckEndOfMonth()
{
if (State.DateTime[2] > DaysInMonth())
{
@ -462,7 +439,7 @@ void CheckEndOfMonth()
}
}
void CountDay()
void RTC::CountDay()
{
// day-of-week counter
State.DateTime[3]++;
@ -474,7 +451,7 @@ void CountDay()
CheckEndOfMonth();
}
void CountHour()
void RTC::CountHour()
{
u8 hour = BCDIncrement(State.DateTime[4] & 0x3F);
u8 pm = State.DateTime[4] & 0x40;
@ -506,7 +483,7 @@ void CountHour()
State.DateTime[4] = hour | pm;
}
void CountMinute()
void RTC::CountMinute()
{
State.MinuteCount++;
State.DateTime[5] = BCDIncrement(State.DateTime[5]);
@ -520,7 +497,7 @@ void CountMinute()
ProcessIRQ(0);
}
void CountSecond()
void RTC::CountSecond()
{
State.DateTime[6] = BCDIncrement(State.DateTime[6]);
if (State.DateTime[6] >= 0x60)
@ -531,7 +508,7 @@ void CountSecond()
}
void ScheduleTimer(bool first)
void RTC::ScheduleTimer(bool first)
{
if (first) TimerError = 0;
@ -544,7 +521,7 @@ void ScheduleTimer(bool first)
NDS::ScheduleEvent(NDS::Event_RTC, !first, delay, 0, 0);
}
void ClockTimer(u32 param)
void RTC::ClockTimer(u32 param)
{
ClockCount++;
@ -565,7 +542,7 @@ void ClockTimer(u32 param)
}
void WriteDateTime(int num, u8 val)
void RTC::WriteDateTime(int num, u8 val)
{
switch (num)
{
@ -619,14 +596,14 @@ void WriteDateTime(int num, u8 val)
}
}
void SaveDateTime()
void RTC::SaveDateTime()
{
int y, m, d, h, i, s;
GetDateTime(y, m, d, h, i, s);
Platform::WriteDateTime(y, m, d, h, i, s);
}
void CmdRead()
void RTC::CmdRead()
{
if ((CurCmd & 0x0F) == 0x06)
{
@ -704,7 +681,7 @@ void CmdRead()
Log(LogLevel::Debug, "RTC: unknown read command %02X\n", CurCmd);
}
void CmdWrite(u8 val)
void RTC::CmdWrite(u8 val)
{
if ((CurCmd & 0x0F) == 0x06)
{
@ -861,7 +838,7 @@ void CmdWrite(u8 val)
Log(LogLevel::Debug, "RTC: unknown write command %02X\n", CurCmd);
}
void ByteIn(u8 val)
void RTC::ByteIn(u8 val)
{
if (InputPos == 0)
{
@ -895,13 +872,13 @@ void ByteIn(u8 val)
}
u16 Read()
u16 RTC::Read()
{
//printf("RTC READ %04X\n", IO);
return IO;
}
void Write(u16 val, bool byte)
void RTC::Write(u16 val, bool byte)
{
if (byte) val |= (IO & 0xFF00);
@ -963,5 +940,3 @@ void Write(u16 val, bool byte)
else
IO = (IO & 0x0001) | (val & 0xFFFE);
}
}