2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2015-11-10 05:21:49 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2012-12-19 09:30:18 +00:00
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2015-03-01 02:33:12 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
static BYTE Mempaks[4][0x8000];
|
2015-03-03 11:31:37 +00:00
|
|
|
HANDLE hMempakFile[4];
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void Mempak::Close()
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-03-03 11:31:37 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (hMempakFile[i])
|
|
|
|
{
|
|
|
|
CloseHandle(hMempakFile[i]);
|
|
|
|
hMempakFile[i] = NULL;
|
|
|
|
}
|
2015-03-01 02:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
void LoadMempak (int Control)
|
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
CPath FileName;
|
2015-03-03 11:31:37 +00:00
|
|
|
DWORD dwRead;
|
|
|
|
stdstr MempakName;
|
|
|
|
bool bFormatMempak = false;
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
MempakName.Format("%s_Cont_%d", g_Settings->LoadStringVal(Game_GameName).c_str(), Control + 1);
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
FileName.SetDriveDirectory(g_Settings->LoadStringVal(Directory_NativeSave).c_str());
|
2015-03-03 11:31:37 +00:00
|
|
|
FileName.SetName(MempakName.c_str());
|
|
|
|
FileName.SetExtension("mpk");
|
|
|
|
|
|
|
|
if (!FileName.Exists())
|
2015-03-01 02:33:12 +00:00
|
|
|
{
|
2015-03-03 11:31:37 +00:00
|
|
|
bFormatMempak = true;
|
|
|
|
|
|
|
|
if (!FileName.DirectoryExists())
|
|
|
|
{
|
2015-10-25 11:10:54 +00:00
|
|
|
FileName.DirectoryCreate();
|
2015-03-03 11:31:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 20:01:18 +00:00
|
|
|
BYTE Initialize[] = {
|
2015-03-03 11:31:37 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2015-04-20 20:01:18 +00:00
|
|
|
memcpy(&Mempaks[Control][0], Initialize, 0x110);
|
2015-03-03 11:31:37 +00:00
|
|
|
|
|
|
|
for (int count = 0x110; count < 0x8000; count += 2)
|
|
|
|
{
|
|
|
|
Mempaks[Control][count] = 0x00;
|
|
|
|
Mempaks[Control][count + 1] = 0x03;
|
|
|
|
}
|
2015-03-01 02:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 11:31:37 +00:00
|
|
|
hMempakFile[Control] = CreateFile(FileName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-03-03 11:31:37 +00:00
|
|
|
if (hMempakFile[Control] == INVALID_HANDLE_VALUE)
|
2015-03-01 02:33:12 +00:00
|
|
|
{
|
|
|
|
WriteTraceF(TraceError,__FUNCTION__ ": Failed to open (%s), lastError = %X",(LPCTSTR)FileName, GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:31:37 +00:00
|
|
|
SetFilePointer(hMempakFile[Control], 0, NULL, FILE_BEGIN);
|
|
|
|
if (bFormatMempak)
|
|
|
|
{
|
|
|
|
WriteFile(hMempakFile[Control], Mempaks[Control], 0x8000, &dwRead, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReadFile(hMempakFile[Control], Mempaks[Control], 0x8000, &dwRead, NULL);
|
|
|
|
}
|
2015-03-01 02:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
BYTE Mempak::CalculateCrc(BYTE * DataToCrc)
|
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
DWORD Count;
|
|
|
|
DWORD XorTap;
|
|
|
|
|
|
|
|
int Length;
|
|
|
|
BYTE CRC = 0;
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
for (Count = 0; Count < 0x21; Count++)
|
|
|
|
{
|
|
|
|
for (Length = 0x80; Length >= 1; Length >>= 1)
|
|
|
|
{
|
2015-05-18 02:20:15 +00:00
|
|
|
XorTap = (CRC & 0x80) ? 0x85 : 0x00;
|
2015-03-01 02:33:12 +00:00
|
|
|
CRC <<= 1;
|
2015-03-29 17:19:28 +00:00
|
|
|
if (Count == 0x20)
|
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
CRC &= 0xFF;
|
2015-03-29 17:19:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((*DataToCrc & Length) != 0)
|
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
CRC |= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CRC ^= XorTap;
|
|
|
|
}
|
|
|
|
DataToCrc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CRC;
|
|
|
|
}
|
|
|
|
|
2015-05-18 02:20:15 +00:00
|
|
|
void Mempak::ReadFrom(int Control, BYTE * command)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-05-18 04:19:31 +00:00
|
|
|
DWORD address = (command[3] << 8) | (command[4] & 0xE0);
|
2015-05-18 02:20:15 +00:00
|
|
|
|
|
|
|
if (address < 0x8000)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
|
|
|
if (hMempakFile[Control] == NULL)
|
|
|
|
{
|
2015-03-03 11:31:37 +00:00
|
|
|
LoadMempak(Control);
|
2015-03-01 02:33:12 +00:00
|
|
|
}
|
2015-05-18 02:20:15 +00:00
|
|
|
memcpy(&command[5], &Mempaks[Control][address], 0x20);
|
2015-03-29 17:19:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-18 02:20:15 +00:00
|
|
|
memset(&command[5], 0x00, 0x20);
|
2015-03-01 02:33:12 +00:00
|
|
|
/* Rumble pack area */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 02:20:15 +00:00
|
|
|
void Mempak::WriteTo(int Control, BYTE * command)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
DWORD dwWritten;
|
2015-05-18 04:19:31 +00:00
|
|
|
DWORD address = (command[3] << 8) | (command[4] & 0xE0);
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-05-18 02:20:15 +00:00
|
|
|
if (address < 0x8000)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
|
|
|
if (hMempakFile[Control] == NULL)
|
|
|
|
{
|
2015-03-03 11:31:37 +00:00
|
|
|
LoadMempak(Control);
|
2015-03-01 02:33:12 +00:00
|
|
|
}
|
2015-05-18 02:20:15 +00:00
|
|
|
memcpy(&Mempaks[Control][address], &command[5], 0x20);
|
2015-03-01 02:33:12 +00:00
|
|
|
|
2015-03-03 11:31:37 +00:00
|
|
|
SetFilePointer(hMempakFile[Control], 0,NULL,FILE_BEGIN);
|
|
|
|
WriteFile(hMempakFile[Control], &Mempaks[Control][0], 0x8000, &dwWritten, NULL);
|
2015-03-29 17:19:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-01 02:33:12 +00:00
|
|
|
/* Rumble pack area */
|
|
|
|
}
|
2015-01-31 19:27:27 +00:00
|
|
|
}
|