Merge pull request #194 from death-droid/MempakFix

Correct behaviour of Mempak
This commit is contained in:
zilmar 2015-03-06 18:37:54 +11:00
commit 2ef36f5f80
3 changed files with 73 additions and 63 deletions

View File

@ -10,67 +10,86 @@
****************************************************************************/ ****************************************************************************/
#include "stdafx.h" #include "stdafx.h"
static BYTE Mempaks[4][0x8000]; static BYTE Mempaks[4][0x8000];
HANDLE hMempakFile = NULL; HANDLE hMempakFile[4];
void Mempak::Close(void) { void Mempak::Close(void) {
if (hMempakFile) { for (int i = 0; i < 4; i++)
CloseHandle(hMempakFile); {
hMempakFile = NULL; if (hMempakFile[i])
{
CloseHandle(hMempakFile[i]);
hMempakFile[i] = NULL;
}
} }
} }
void LoadMempak (void) { void LoadMempak (int Control) {
CPath FileName; CPath FileName;
DWORD dwRead, count, count2; DWORD dwRead;
stdstr MempakName;
bool bFormatMempak = false;
BYTE Initilize[] = { MempakName.Format("%s_Cont_%d", g_Settings->LoadString(Game_GameName).c_str(), Control + 1);
0x81,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b, 0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17, 0x18,0x19,0x1A,0x1B, 0x1C,0x1D,0x1E,0x1F,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x71,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03,
};
for (count = 0; count < 4; count ++) {
for (count2 = 0; count2 < 0x8000; count2 += 2) {
Mempaks[count][count2] = 0x00;
Mempaks[count][count2 + 1] = 0x03;
}
memcpy(&Mempaks[count][0],Initilize,sizeof(Initilize));
}
FileName.SetDriveDirectory( g_Settings->LoadString(Directory_NativeSave).c_str()); FileName.SetDriveDirectory(g_Settings->LoadString(Directory_NativeSave).c_str());
FileName.SetName(g_Settings->LoadString(Game_GameName).c_str()); FileName.SetName(MempakName.c_str());
FileName.SetExtension("mpk"); FileName.SetExtension("mpk");
if (!FileName.DirectoryExists()) if (!FileName.Exists())
{ {
FileName.CreateDirectory(); bFormatMempak = true;
if (!FileName.DirectoryExists())
{
FileName.CreateDirectory();
}
BYTE Initilize[] = {
0x81,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b, 0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17, 0x18,0x19,0x1A,0x1B, 0x1C,0x1D,0x1E,0x1F,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0x05,0x1A,0x5F,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0x01,0xFF, 0x66,0x25,0x99,0xCD,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x71,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03,
};
memcpy(&Mempaks[Control][0], Initilize, 0x110);
for (int count = 0x110; count < 0x8000; count += 2)
{
Mempaks[Control][count] = 0x00;
Mempaks[Control][count + 1] = 0x03;
}
} }
hMempakFile = CreateFile(FileName,GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_ALWAYS, hMempakFile[Control] = CreateFile(FileName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (hMempakFile == INVALID_HANDLE_VALUE) if (hMempakFile[Control] == INVALID_HANDLE_VALUE)
{ {
WriteTraceF(TraceError,__FUNCTION__ ": Failed to open (%s), lastError = %X",(LPCTSTR)FileName, GetLastError()); WriteTraceF(TraceError,__FUNCTION__ ": Failed to open (%s), lastError = %X",(LPCTSTR)FileName, GetLastError());
return; return;
} }
SetFilePointer(hMempakFile,0,NULL,FILE_BEGIN); SetFilePointer(hMempakFile[Control], 0, NULL, FILE_BEGIN);
ReadFile(hMempakFile,Mempaks,sizeof(Mempaks),&dwRead,NULL); if (bFormatMempak)
WriteFile(hMempakFile,Mempaks,sizeof(Mempaks),&dwRead,NULL); {
WriteFile(hMempakFile[Control], Mempaks[Control], 0x8000, &dwRead, NULL);
}
else
{
ReadFile(hMempakFile[Control], Mempaks[Control], 0x8000, &dwRead, NULL);
}
} }
BYTE Mempak::CalculateCrc(BYTE * DataToCrc) { BYTE Mempak::CalculateCrc(BYTE * DataToCrc) {
@ -100,16 +119,9 @@ BYTE Mempak::CalculateCrc(BYTE * DataToCrc) {
} }
void Mempak::ReadFrom(int Control, int Address, BYTE * Buffer) { void Mempak::ReadFrom(int Control, int Address, BYTE * Buffer) {
if (Address == 0x8001) { if (Address < 0x8000) {
memset(Buffer, 0, 0x20); if (hMempakFile[Control] == NULL) {
Buffer[0x20] = CalculateCrc(Buffer); LoadMempak(Control);
return;
}
Address &= 0xFFE0;
if (Address <= 0x7FE0) {
if (hMempakFile == NULL) {
LoadMempak();
} }
memcpy(Buffer, &Mempaks[Control][Address], 0x20); memcpy(Buffer, &Mempaks[Control][Address], 0x20);
} else { } else {
@ -123,17 +135,14 @@ void Mempak::ReadFrom(int Control, int Address, BYTE * Buffer) {
void Mempak::WriteTo(int Control, int Address, BYTE * Buffer) { void Mempak::WriteTo(int Control, int Address, BYTE * Buffer) {
DWORD dwWritten; DWORD dwWritten;
if (Address == 0x8001) { Buffer[0x20] = CalculateCrc(Buffer); return; } if (Address < 0x8000) {
if (hMempakFile[Control] == NULL) {
Address &= 0xFFE0; LoadMempak(Control);
if (Address <= 0x7FE0) {
if (hMempakFile == NULL) {
LoadMempak();
} }
memcpy(&Mempaks[Control][Address], Buffer, 0x20); memcpy(&Mempaks[Control][Address], Buffer, 0x20);
SetFilePointer(hMempakFile,Control*0x8000,NULL,FILE_BEGIN); SetFilePointer(hMempakFile[Control], 0,NULL,FILE_BEGIN);
WriteFile(hMempakFile,&Mempaks[Control][0],0x8000,&dwWritten,NULL); WriteFile(hMempakFile[Control], &Mempaks[Control][0], 0x8000, &dwWritten, NULL);
} else { } else {
/* Rumble pack area */ /* Rumble pack area */
} }

View File

@ -445,7 +445,7 @@ void CPifRam::ProcessControllerCommand ( int Control, BYTE * Command)
if (Command[1] != 33) { g_Notify->DisplayError(L"What am I meant to do with this Controller Command"); } if (Command[1] != 33) { g_Notify->DisplayError(L"What am I meant to do with this Controller Command"); }
} }
if (Controllers[Control].Present == TRUE) { if (Controllers[Control].Present == TRUE) {
DWORD address = ((Command[3] << 8) | Command[4]); DWORD address = ((Command[3] << 8) | Command[4] & 0xE0);
switch (Controllers[Control].Plugin) { switch (Controllers[Control].Plugin) {
case PLUGIN_RUMBLE_PAK: case PLUGIN_RUMBLE_PAK:
@ -471,7 +471,7 @@ void CPifRam::ProcessControllerCommand ( int Control, BYTE * Command)
if (Command[1] != 1) { g_Notify->DisplayError(L"What am I meant to do with this Controller Command"); } if (Command[1] != 1) { g_Notify->DisplayError(L"What am I meant to do with this Controller Command"); }
} }
if (Controllers[Control].Present == TRUE) { if (Controllers[Control].Present == TRUE) {
DWORD address = ((Command[3] << 8) | Command[4]); DWORD address = ((Command[3] << 8) | Command[4] & 0xE0 );
switch (Controllers[Control].Plugin) { switch (Controllers[Control].Plugin) {
case PLUGIN_MEMPAK: Mempak::WriteTo(Control, address, &Command[5]); break; case PLUGIN_MEMPAK: Mempak::WriteTo(Control, address, &Command[5]); break;
case PLUGIN_RAW: if (g_Plugins->Control()->ControllerCommand) { g_Plugins->Control()->ControllerCommand(Control, Command); } break; case PLUGIN_RAW: if (g_Plugins->Control()->ControllerCommand) { g_Plugins->Control()->ControllerCommand(Control, Command); } break;

View File

@ -58,6 +58,7 @@ CN64System::CN64System ( CPlugins * Plugins, bool SavesReadOnly ) :
CN64System::~CN64System ( void ) CN64System::~CN64System ( void )
{ {
SetActiveSystem(false); SetActiveSystem(false);
Mempak::Close();
if (m_SyncCPU) if (m_SyncCPU)
{ {
m_SyncCPU->CpuStopped(); m_SyncCPU->CpuStopped();