From 04e21bea16a8c1c03224cbef499d872c88e34aae Mon Sep 17 00:00:00 2001 From: Emmet Young Date: Wed, 26 Sep 2012 20:36:52 +1000 Subject: [PATCH] Slightly re-order the PifRam.cpp file. Add in support for the Real time clock, it reads the current time and date straight from the computer. --- Source/Project64/N64 System/Mips/Eeprom.cpp | 62 +++++++++++++++++--- Source/Project64/N64 System/Mips/Pif Ram.cpp | 58 +++++++++--------- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/Source/Project64/N64 System/Mips/Eeprom.cpp b/Source/Project64/N64 System/Mips/Eeprom.cpp index 97ff9a827..786bb4164 100644 --- a/Source/Project64/N64 System/Mips/Eeprom.cpp +++ b/Source/Project64/N64 System/Mips/Eeprom.cpp @@ -1,4 +1,7 @@ #include "stdafx.h" +#include + + CEeprom::CEeprom (bool ReadOnly): m_ReadOnly(ReadOnly), @@ -15,7 +18,16 @@ CEeprom::~CEeprom (void) { } } +unsigned char byte2bcd(int n) +{ + n %= 100; + return ((n / 10) << 4) | (n % 10); +} + void CEeprom::EepromCommand ( BYTE * Command) { + time_t curtime_time; + struct tm curtime; + if (g_SaveUsing == SaveChip_Auto) { g_SaveUsing = SaveChip_Eeprom_4K; } switch (Command[2]) { @@ -24,15 +36,15 @@ void CEeprom::EepromCommand ( BYTE * Command) { Command[1] |= 0x80; break; } - if (Command[1] != 3) { + if (Command[1] != 3) + { Command[1] |= 0x40; - if ((Command[1] & 3) > 0) { Command[3] = 0x00; } - if (g_SaveUsing == SaveChip_Eeprom_4K) { - if ((Command[1] & 3) > 1) { Command[4] = 0x80; } - } else { - if ((Command[1] & 3) > 1) { Command[4] = 0xC0; } - } - if ((Command[1] & 3) > 2) { Command[5] = 0x00; } + if ((Command[1] & 3) > 0) + Command[3] = 0x00; + if ((Command[1] & 3) > 1) + Command[4] = (g_SaveUsing == SaveChip_Eeprom_4K) ? 0x80 : 0xC0; + if ((Command[1] & 3) > 2) + Command[5] = 0x00; } else { Command[3] = 0x00; Command[4] = g_SaveUsing == SaveChip_Eeprom_4K?0x80:0xC0; @@ -53,6 +65,40 @@ void CEeprom::EepromCommand ( BYTE * Command) { #endif WriteTo(&Command[4],Command[3]); break; + case 6: //RTC Status query + Command[3] = 0x00; + Command[4] = 0x10; + Command[12] = 0x00; + break; + case 7: //Read RTC + switch(Command[3]) + { + case 0: + Command[4] = 0x00; + Command[5] = 0x02; + Command[12] = 0x00; + break; + case 1: + //read block, Command[2], Unimplemented + break; + case 2: //Set RTC Time + time(&curtime_time); + memcpy(&curtime, localtime(&curtime_time), sizeof(curtime)); // fd's fix + Command[4] = byte2bcd(curtime.tm_sec); + Command[5] = byte2bcd(curtime.tm_min); + Command[6] = 0x80 + byte2bcd(curtime.tm_hour); + Command[7] = byte2bcd(curtime.tm_mday); + Command[8] = byte2bcd(curtime.tm_wday); + Command[9] = byte2bcd(curtime.tm_mon + 1); + Command[10] = byte2bcd(curtime.tm_year); + Command[11] = byte2bcd(curtime.tm_year / 100); + Command[12] = 0x00; // status + break; + } + break; + case 8: + //Write RTC, unimplemented + break; default: if (_Settings->LoadDword(Debugger_ShowPifErrors)) { DisplayError("Unknown EepromCommand %d",Command[2]); } } diff --git a/Source/Project64/N64 System/Mips/Pif Ram.cpp b/Source/Project64/N64 System/Mips/Pif Ram.cpp index 9fa02de61..583c4d210 100644 --- a/Source/Project64/N64 System/Mips/Pif Ram.cpp +++ b/Source/Project64/N64 System/Mips/Pif Ram.cpp @@ -52,6 +52,34 @@ void CPifRam::Reset ( void ) memset(m_PifRom,0,sizeof(m_PifRom)); } +void CPifRam::n64_cic_nus_6105(char challenge[], char respone[], int length) +{ + static char lut0[0x10] = { + 0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, + 0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9 + }; + static char lut1[0x10] = { + 0x4, 0x1, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, + 0xC, 0x9, 0x8, 0x5, 0x6, 0x3, 0xC, 0x9 + }; + char key, *lut; + int i, sgn, mag, mod; + + for (key = 0xB, lut = lut0, i = 0; i < length; i++) { + respone[i] = (key + 5 * challenge[i]) & 0xF; + key = lut[respone[i]]; + sgn = (respone[i] >> 3) & 0x1; + mag = ((sgn == 1) ? ~respone[i] : respone[i]) & 0x7; + mod = (mag % 3 == 1) ? sgn : 1 - sgn; + if (lut == lut1 && (respone[i] == 0x1 || respone[i] == 0x9)) + mod = 1; + if (lut == lut1 && (respone[i] == 0xB || respone[i] == 0xE)) + mod = 0; + lut = (mod == 1) ? lut1 : lut0; + } +} + + void CPifRam::PifRamRead (void) { if (m_PifRam[0x3F] == 0x2) @@ -114,7 +142,7 @@ void CPifRam::PifRamWrite (void) { Challenge[i*2+1] = m_PifRam[48+i] & 0x0f; } //Calcuate the proper respone for the give challange(X-Scales algorithm) - n64_cic_nus_6105(Challenge, Response, CHALLENGE_LENGTH - 2); + n64_cic_nus_6105(Challenge, Response, CHL_LEN - 2); // re-format the 'response' into a byte stream for (int i = 0; i < 15; i++) { @@ -371,34 +399,6 @@ void CPifRam::SI_DMA_WRITE (void) } } -void CPifRam::n64_cic_nus_6105(char challenge[], char respone[], int length) -{ - static char lut0[0x10] = { - 0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, - 0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9 - }; - static char lut1[0x10] = { - 0x4, 0x1, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, - 0xC, 0x9, 0x8, 0x5, 0x6, 0x3, 0xC, 0x9 - }; - char key, *lut; - int i, sgn, mag, mod; - - for (key = 0xB, lut = lut0, i = 0; i < length; i++) { - respone[i] = (key + 5 * challenge[i]) & 0xF; - key = lut[respone[i]]; - sgn = (respone[i] >> 3) & 0x1; - mag = ((sgn == 1) ? ~respone[i] : respone[i]) & 0x7; - mod = (mag % 3 == 1) ? sgn : 1 - sgn; - if (lut == lut1 && (respone[i] == 0x1 || respone[i] == 0x9)) - mod = 1; - if (lut == lut1 && (respone[i] == 0xB || respone[i] == 0xE)) - mod = 0; - lut = (mod == 1) ? lut1 : lut0; - } -} - - void CPifRam::ProcessControllerCommand ( int Control, BYTE * Command) { CONTROL * Controllers = _Plugins->Control()->PluginControllers();