[Project64] Get N64 Rom Class.cpp to use standard types
This commit is contained in:
parent
63096c99d3
commit
1a4166a1de
|
@ -28,7 +28,8 @@ CN64Rom::~CN64Rom()
|
||||||
UnallocateRomImage();
|
UnallocateRomImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnly)
|
||||||
|
{
|
||||||
//Try to open the target file
|
//Try to open the target file
|
||||||
HANDLE hFile = CreateFile(FileLoc, GENERIC_READ, FILE_SHARE_READ, NULL,
|
HANDLE hFile = CreateFile(FileLoc, GENERIC_READ, FILE_SHARE_READ, NULL,
|
||||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
|
||||||
|
@ -36,11 +37,12 @@ bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
if (hFile == INVALID_HANDLE_VALUE) { return false; }
|
if (hFile == INVALID_HANDLE_VALUE) { return false; }
|
||||||
|
|
||||||
//Read the first 4 bytes and make sure it is a valid n64 image
|
//Read the first 4 bytes and make sure it is a valid n64 image
|
||||||
DWORD dwRead; BYTE Test[4];
|
DWORD dwRead; uint8_t Test[4];
|
||||||
|
|
||||||
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
|
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
|
||||||
ReadFile(hFile, Test, 4, &dwRead, NULL);
|
ReadFile(hFile, Test, 4, &dwRead, NULL);
|
||||||
if (!IsValidRomImage(Test)) {
|
if (!IsValidRomImage(Test))
|
||||||
|
{
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -50,8 +52,9 @@ bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
//if loading boot code then just load the first 0x1000 bytes
|
//if loading boot code then just load the first 0x1000 bytes
|
||||||
if (LoadBootCodeOnly) { RomFileSize = 0x1000; }
|
if (LoadBootCodeOnly) { RomFileSize = 0x1000; }
|
||||||
|
|
||||||
BYTE * Image = (BYTE *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
uint8_t * Image = (uint8_t *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||||
if (Image == NULL) {
|
if (Image == NULL)
|
||||||
|
{
|
||||||
SetError(MSG_MEM_ALLOC_ERROR);
|
SetError(MSG_MEM_ALLOC_ERROR);
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
return false;
|
return false;
|
||||||
|
@ -62,11 +65,13 @@ bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
|
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
|
||||||
|
|
||||||
DWORD count, TotalRead = 0;
|
DWORD count, TotalRead = 0;
|
||||||
for (count = 0; count < (int)RomFileSize; count += ReadFromRomSection) {
|
for (count = 0; count < (int)RomFileSize; count += ReadFromRomSection)
|
||||||
|
{
|
||||||
DWORD dwToRead = RomFileSize - count;
|
DWORD dwToRead = RomFileSize - count;
|
||||||
if (dwToRead > ReadFromRomSection) { dwToRead = ReadFromRomSection; }
|
if (dwToRead > ReadFromRomSection) { dwToRead = ReadFromRomSection; }
|
||||||
|
|
||||||
if (!ReadFile(hFile, &Image[count], dwToRead, &dwRead, NULL)) {
|
if (!ReadFile(hFile, &Image[count], dwToRead, &dwRead, NULL))
|
||||||
|
{
|
||||||
VirtualFree(Image, 0, MEM_RELEASE);
|
VirtualFree(Image, 0, MEM_RELEASE);
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
SetError(MSG_FAIL_IMAGE);
|
SetError(MSG_FAIL_IMAGE);
|
||||||
|
@ -79,7 +84,8 @@ bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
}
|
}
|
||||||
dwRead = TotalRead;
|
dwRead = TotalRead;
|
||||||
|
|
||||||
if (RomFileSize != dwRead) {
|
if (RomFileSize != dwRead)
|
||||||
|
{
|
||||||
VirtualFree(Image, 0, MEM_RELEASE);
|
VirtualFree(Image, 0, MEM_RELEASE);
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
SetError(MSG_FAIL_IMAGE);
|
SetError(MSG_FAIL_IMAGE);
|
||||||
|
@ -101,40 +107,49 @@ bool CN64Rom::AllocateAndLoadN64Image(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CN64Rom::AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnly) {
|
bool CN64Rom::AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnly)
|
||||||
|
{
|
||||||
unzFile file = unzOpen(FileLoc);
|
unzFile file = unzOpen(FileLoc);
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int port = unzGoToFirstFile(file);
|
int port = unzGoToFirstFile(file);
|
||||||
bool FoundRom = false;
|
bool FoundRom = false;
|
||||||
|
|
||||||
//scan through all files in zip to a suitable file is found
|
//scan through all files in zip to a suitable file is found
|
||||||
while (port == UNZ_OK && !FoundRom) {
|
while (port == UNZ_OK && !FoundRom)
|
||||||
|
{
|
||||||
unz_file_info info;
|
unz_file_info info;
|
||||||
char zname[_MAX_PATH];
|
char zname[_MAX_PATH];
|
||||||
|
|
||||||
unzGetCurrentFileInfo(file, &info, zname, sizeof(zname), NULL, 0, NULL, 0);
|
unzGetCurrentFileInfo(file, &info, zname, sizeof(zname), NULL, 0, NULL, 0);
|
||||||
if (unzLocateFile(file, zname, 1) != UNZ_OK) {
|
if (unzLocateFile(file, zname, 1) != UNZ_OK)
|
||||||
|
{
|
||||||
SetError(MSG_FAIL_ZIP);
|
SetError(MSG_FAIL_ZIP);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (unzOpenCurrentFile(file) != UNZ_OK) {
|
if (unzOpenCurrentFile(file) != UNZ_OK)
|
||||||
|
{
|
||||||
SetError(MSG_FAIL_ZIP);
|
SetError(MSG_FAIL_ZIP);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read the first 4 bytes to check magic number
|
//Read the first 4 bytes to check magic number
|
||||||
BYTE Test[4];
|
uint8_t Test[4];
|
||||||
unzReadCurrentFile(file, Test, sizeof(Test));
|
unzReadCurrentFile(file, Test, sizeof(Test));
|
||||||
if (IsValidRomImage(Test)) {
|
if (IsValidRomImage(Test))
|
||||||
|
{
|
||||||
//Get the size of the rom and try to allocate the memory needed.
|
//Get the size of the rom and try to allocate the memory needed.
|
||||||
DWORD RomFileSize = info.uncompressed_size;
|
DWORD RomFileSize = info.uncompressed_size;
|
||||||
if (LoadBootCodeOnly) {
|
if (LoadBootCodeOnly)
|
||||||
|
{
|
||||||
RomFileSize = 0x1000;
|
RomFileSize = 0x1000;
|
||||||
}
|
}
|
||||||
BYTE * Image = (BYTE *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
uint8_t * Image = (uint8_t *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||||
if (Image == NULL) {
|
if (Image == NULL)
|
||||||
|
{
|
||||||
SetError(MSG_MEM_ALLOC_ERROR);
|
SetError(MSG_MEM_ALLOC_ERROR);
|
||||||
unzCloseCurrentFile(file);
|
unzCloseCurrentFile(file);
|
||||||
break;
|
break;
|
||||||
|
@ -145,12 +160,14 @@ bool CN64Rom::AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
memcpy(Image, Test, 4);
|
memcpy(Image, Test, 4);
|
||||||
|
|
||||||
DWORD dwRead, count, TotalRead = 0;
|
DWORD dwRead, count, TotalRead = 0;
|
||||||
for (count = 4; count < (int)RomFileSize; count += ReadFromRomSection) {
|
for (count = 4; count < (int)RomFileSize; count += ReadFromRomSection)
|
||||||
|
{
|
||||||
DWORD dwToRead = RomFileSize - count;
|
DWORD dwToRead = RomFileSize - count;
|
||||||
if (dwToRead > ReadFromRomSection) { dwToRead = ReadFromRomSection; }
|
if (dwToRead > ReadFromRomSection) { dwToRead = ReadFromRomSection; }
|
||||||
|
|
||||||
dwRead = unzReadCurrentFile(file, &Image[count], dwToRead);
|
dwRead = unzReadCurrentFile(file, &Image[count], dwToRead);
|
||||||
if (dwRead == 0) {
|
if (dwRead == 0)
|
||||||
|
{
|
||||||
SetError(MSG_FAIL_ZIP);
|
SetError(MSG_FAIL_ZIP);
|
||||||
VirtualFree(Image, 0, MEM_RELEASE);
|
VirtualFree(Image, 0, MEM_RELEASE);
|
||||||
unzCloseCurrentFile(file);
|
unzCloseCurrentFile(file);
|
||||||
|
@ -163,7 +180,8 @@ bool CN64Rom::AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
}
|
}
|
||||||
dwRead = TotalRead + 4;
|
dwRead = TotalRead + 4;
|
||||||
|
|
||||||
if (RomFileSize != dwRead) {
|
if (RomFileSize != dwRead)
|
||||||
|
{
|
||||||
VirtualFree(Image, 0, MEM_RELEASE);
|
VirtualFree(Image, 0, MEM_RELEASE);
|
||||||
unzCloseCurrentFile(file);
|
unzCloseCurrentFile(file);
|
||||||
SetError(MSG_FAIL_ZIP);
|
SetError(MSG_FAIL_ZIP);
|
||||||
|
@ -188,19 +206,24 @@ bool CN64Rom::AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnl
|
||||||
unzCloseCurrentFile(file);
|
unzCloseCurrentFile(file);
|
||||||
|
|
||||||
if (!FoundRom)
|
if (!FoundRom)
|
||||||
|
{
|
||||||
port = unzGoToNextFile(file);
|
port = unzGoToNextFile(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unzClose(file);
|
unzClose(file);
|
||||||
|
|
||||||
return FoundRom;
|
return FoundRom;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CN64Rom::ByteSwapRom() {
|
void CN64Rom::ByteSwapRom()
|
||||||
|
{
|
||||||
DWORD count;
|
DWORD count;
|
||||||
|
|
||||||
switch (*((DWORD *)&m_ROMImage[0])) {
|
switch (*((DWORD *)&m_ROMImage[0]))
|
||||||
|
{
|
||||||
case 0x12408037:
|
case 0x12408037:
|
||||||
for (count = 0; count < m_RomFileSize; count += 4) {
|
for (count = 0; count < m_RomFileSize; count += 4)
|
||||||
|
{
|
||||||
m_ROMImage[count] ^= m_ROMImage[count + 2];
|
m_ROMImage[count] ^= m_ROMImage[count + 2];
|
||||||
m_ROMImage[count + 2] ^= m_ROMImage[count];
|
m_ROMImage[count + 2] ^= m_ROMImage[count];
|
||||||
m_ROMImage[count] ^= m_ROMImage[count + 2];
|
m_ROMImage[count] ^= m_ROMImage[count + 2];
|
||||||
|
@ -211,7 +234,8 @@ void CN64Rom::ByteSwapRom() {
|
||||||
break;
|
break;
|
||||||
case 0x40072780: //64DD IPL
|
case 0x40072780: //64DD IPL
|
||||||
case 0x40123780:
|
case 0x40123780:
|
||||||
for (count = 0; count < m_RomFileSize; count += 4) {
|
for (count = 0; count < m_RomFileSize; count += 4)
|
||||||
|
{
|
||||||
m_ROMImage[count] ^= m_ROMImage[count + 3];
|
m_ROMImage[count] ^= m_ROMImage[count + 3];
|
||||||
m_ROMImage[count + 3] ^= m_ROMImage[count];
|
m_ROMImage[count + 3] ^= m_ROMImage[count];
|
||||||
m_ROMImage[count] ^= m_ROMImage[count + 3];
|
m_ROMImage[count] ^= m_ROMImage[count + 3];
|
||||||
|
@ -228,7 +252,7 @@ void CN64Rom::ByteSwapRom() {
|
||||||
|
|
||||||
void CN64Rom::CalculateCicChip()
|
void CN64Rom::CalculateCicChip()
|
||||||
{
|
{
|
||||||
__int64 CRC = 0;
|
int64_t CRC = 0;
|
||||||
|
|
||||||
if (m_ROMImage == NULL)
|
if (m_ROMImage == NULL)
|
||||||
{
|
{
|
||||||
|
@ -236,7 +260,8 @@ void CN64Rom::CalculateCicChip()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int count = 0x40; count < 0x1000; count += 4) {
|
for (int count = 0x40; count < 0x1000; count += 4)
|
||||||
|
{
|
||||||
CRC += *(DWORD *)(m_ROMImage + count);
|
CRC += *(DWORD *)(m_ROMImage + count);
|
||||||
}
|
}
|
||||||
switch (CRC) {
|
switch (CRC) {
|
||||||
|
@ -273,7 +298,8 @@ void CN64Rom::CalculateRomCrc()
|
||||||
// 64DD IPL at=0x02E90EDD , s6=0xdd
|
// 64DD IPL at=0x02E90EDD , s6=0xdd
|
||||||
|
|
||||||
//v0 = 0xFFFFFFFF & (0x3F * at) + 1;
|
//v0 = 0xFFFFFFFF & (0x3F * at) + 1;
|
||||||
switch (m_CicChip){
|
switch (m_CicChip)
|
||||||
|
{
|
||||||
case CIC_NUS_6101:
|
case CIC_NUS_6101:
|
||||||
case CIC_NUS_6102: v0 = 0xF8CA4DDC; break;
|
case CIC_NUS_6102: v0 = 0xF8CA4DDC; break;
|
||||||
case CIC_NUS_6103: v0 = 0xA3886759; break;
|
case CIC_NUS_6103: v0 = 0xA3886759; break;
|
||||||
|
@ -297,7 +323,8 @@ void CN64Rom::CalculateRomCrc()
|
||||||
a2 = v0;
|
a2 = v0;
|
||||||
t4 = v0;
|
t4 = v0;
|
||||||
|
|
||||||
for (t0 = 0; t0 < 0x00100000; t0 += 4){
|
for (t0 = 0; t0 < 0x00100000; t0 += 4)
|
||||||
|
{
|
||||||
v0 = *(DWORD *)(m_ROMImage + t0 + 0x1000);
|
v0 = *(DWORD *)(m_ROMImage + t0 + 0x1000);
|
||||||
|
|
||||||
v1 = a3 + v0;
|
v1 = a3 + v0;
|
||||||
|
@ -315,20 +342,24 @@ void CN64Rom::CalculateRomCrc()
|
||||||
if (a2 < v0) a2 = a3 ^ v0 ^ a2;
|
if (a2 < v0) a2 = a3 ^ v0 ^ a2;
|
||||||
else a2 = a2 ^ a0;
|
else a2 = a2 ^ a0;
|
||||||
|
|
||||||
if (m_CicChip == CIC_NUS_6105){
|
if (m_CicChip == CIC_NUS_6105)
|
||||||
|
{
|
||||||
t4 = (v0 ^ (*(DWORD *)(m_ROMImage + (0xFF & t0) + 0x750))) + t4;
|
t4 = (v0 ^ (*(DWORD *)(m_ROMImage + (0xFF & t0) + 0x750))) + t4;
|
||||||
}
|
}
|
||||||
else t4 = (v0 ^ s0) + t4;
|
else t4 = (v0 ^ s0) + t4;
|
||||||
}
|
}
|
||||||
if (m_CicChip == CIC_NUS_6103){
|
if (m_CicChip == CIC_NUS_6103)
|
||||||
|
{
|
||||||
a3 = (a3 ^ t2) + t3;
|
a3 = (a3 ^ t2) + t3;
|
||||||
s0 = (s0 ^ a2) + t4;
|
s0 = (s0 ^ a2) + t4;
|
||||||
}
|
}
|
||||||
else if (m_CicChip == CIC_NUS_6106){
|
else if (m_CicChip == CIC_NUS_6106)
|
||||||
|
{
|
||||||
a3 = 0xFFFFFFFF & (a3 * t2) + t3;
|
a3 = 0xFFFFFFFF & (a3 * t2) + t3;
|
||||||
s0 = 0xFFFFFFFF & (s0 * a2) + t4;
|
s0 = 0xFFFFFFFF & (s0 * a2) + t4;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
a3 = a3 ^ t2 ^ t3;
|
a3 = a3 ^ t2 ^ t3;
|
||||||
s0 = s0 ^ a2 ^ t4;
|
s0 = s0 ^ a2 ^ t4;
|
||||||
}
|
}
|
||||||
|
@ -344,7 +375,8 @@ CICChip CN64Rom::CicChipID()
|
||||||
return m_CicChip;
|
return m_CicChip;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CN64Rom::IsValidRomImage(BYTE Test[4]) {
|
bool CN64Rom::IsValidRomImage(uint8_t Test[4])
|
||||||
|
{
|
||||||
if (*((DWORD *)&Test[0]) == 0x40123780) { return true; }
|
if (*((DWORD *)&Test[0]) == 0x40123780) { return true; }
|
||||||
if (*((DWORD *)&Test[0]) == 0x12408037) { return true; }
|
if (*((DWORD *)&Test[0]) == 0x12408037) { return true; }
|
||||||
if (*((DWORD *)&Test[0]) == 0x80371240) { return true; }
|
if (*((DWORD *)&Test[0]) == 0x80371240) { return true; }
|
||||||
|
@ -357,7 +389,8 @@ void CN64Rom::NotificationCB(LPCWSTR Status, CN64Rom * /*_this*/)
|
||||||
g_Notify->DisplayMessage(5, stdstr_f("%s", Status).ToUTF16().c_str());
|
g_Notify->DisplayMessage(5, stdstr_f("%s", Status).ToUTF16().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly)
|
||||||
|
{
|
||||||
UnallocateRomImage();
|
UnallocateRomImage();
|
||||||
m_ErrorMsg = EMPTY_STRING;
|
m_ErrorMsg = EMPTY_STRING;
|
||||||
|
|
||||||
|
@ -378,7 +411,8 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
//allocate memory for sub name and copy selected file name to var
|
//allocate memory for sub name and copy selected file name to var
|
||||||
//return false; //remove once dialog is done
|
//return false; //remove once dialog is done
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
*SubFile = '\0';
|
*SubFile = '\0';
|
||||||
SubFile += 1;
|
SubFile += 1;
|
||||||
}
|
}
|
||||||
|
@ -408,8 +442,9 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
//if loading boot code then just load the first 0x1000 bytes
|
//if loading boot code then just load the first 0x1000 bytes
|
||||||
if (LoadBootCodeOnly) { RomFileSize = 0x1000; }
|
if (LoadBootCodeOnly) { RomFileSize = 0x1000; }
|
||||||
|
|
||||||
BYTE * Image = (BYTE *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
uint8_t * Image = (uint8_t *)VirtualAlloc(NULL, RomFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||||
if (Image == NULL) {
|
if (Image == NULL)
|
||||||
|
{
|
||||||
SetError(MSG_MEM_ALLOC_ERROR);
|
SetError(MSG_MEM_ALLOC_ERROR);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -454,8 +489,10 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
//Try to open the file as a zip file
|
//Try to open the file as a zip file
|
||||||
if (!Loaded7zFile)
|
if (!Loaded7zFile)
|
||||||
{
|
{
|
||||||
if (!AllocateAndLoadZipImage(FileLoc, LoadBootCodeOnly)) {
|
if (!AllocateAndLoadZipImage(FileLoc, LoadBootCodeOnly))
|
||||||
if (m_ErrorMsg != EMPTY_STRING) {
|
{
|
||||||
|
if (m_ErrorMsg != EMPTY_STRING)
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,7 +503,8 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
|
|
||||||
//Create a file map
|
//Create a file map
|
||||||
HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||||
if (hFileMapping == NULL) {
|
if (hFileMapping == NULL)
|
||||||
|
{
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
SetError(MSG_FAIL_OPEN_IMAGE);
|
SetError(MSG_FAIL_OPEN_IMAGE);
|
||||||
return false;
|
return false;
|
||||||
|
@ -474,15 +512,17 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
|
|
||||||
//Map the file to a memory pointer .. ie a way of pretending to load the rom
|
//Map the file to a memory pointer .. ie a way of pretending to load the rom
|
||||||
//loose the bonus of being able to flip it on the fly tho.
|
//loose the bonus of being able to flip it on the fly tho.
|
||||||
BYTE * Image = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
|
uint8_t * Image = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
|
||||||
if (Image == NULL) {
|
if (Image == NULL)
|
||||||
|
{
|
||||||
CloseHandle(hFileMapping);
|
CloseHandle(hFileMapping);
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
SetError(MSG_FAIL_OPEN_IMAGE);
|
SetError(MSG_FAIL_OPEN_IMAGE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsValidRomImage(Image)) {
|
if (!IsValidRomImage(Image))
|
||||||
|
{
|
||||||
UnmapViewOfFile(Image);
|
UnmapViewOfFile(Image);
|
||||||
CloseHandle(hFileMapping);
|
CloseHandle(hFileMapping);
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
|
@ -490,13 +530,15 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*((DWORD *)Image) == 0x80371240) {
|
if (*((DWORD *)Image) == 0x80371240)
|
||||||
|
{
|
||||||
m_hRomFile = hFile;
|
m_hRomFile = hFile;
|
||||||
m_hRomFileMapping = hFileMapping;
|
m_hRomFileMapping = hFileMapping;
|
||||||
m_ROMImage = Image;
|
m_ROMImage = Image;
|
||||||
m_RomFileSize = GetFileSize(hFile, NULL);
|
m_RomFileSize = GetFileSize(hFile, NULL);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
if (!AllocateAndLoadN64Image(FileLoc, LoadBootCodeOnly)) { return false; }
|
if (!AllocateAndLoadN64Image(FileLoc, LoadBootCodeOnly)) { return false; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -506,7 +548,8 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
int count;
|
int count;
|
||||||
//Get the header from the rom image
|
//Get the header from the rom image
|
||||||
memcpy(&RomName[0], (void *)(m_ROMImage + 0x20), 20);
|
memcpy(&RomName[0], (void *)(m_ROMImage + 0x20), 20);
|
||||||
for (count = 0; count < 20; count += 4) {
|
for (count = 0; count < 20; count += 4)
|
||||||
|
{
|
||||||
RomName[count] ^= RomName[count + 3];
|
RomName[count] ^= RomName[count + 3];
|
||||||
RomName[count + 3] ^= RomName[count];
|
RomName[count + 3] ^= RomName[count];
|
||||||
RomName[count] ^= RomName[count + 3];
|
RomName[count] ^= RomName[count + 3];
|
||||||
|
@ -516,26 +559,33 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//truncate all the spaces at the end of the string
|
//truncate all the spaces at the end of the string
|
||||||
for (count = 19; count >= 0; count--) {
|
for (count = 19; count >= 0; count--)
|
||||||
if (RomName[count] == ' ') {
|
{
|
||||||
|
if (RomName[count] == ' ')
|
||||||
|
{
|
||||||
RomName[count] = '\0';
|
RomName[count] = '\0';
|
||||||
}
|
}
|
||||||
else if (RomName[count] == '\0') {
|
else if (RomName[count] == '\0')
|
||||||
|
{
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
count = -1;
|
count = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RomName[20] = '\0';
|
RomName[20] = '\0';
|
||||||
if (strlen(RomName) == 0) {
|
if (strlen(RomName) == 0)
|
||||||
|
{
|
||||||
char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
|
char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
|
||||||
_splitpath(FileLoc, drive, dir, fname, ext);
|
_splitpath(FileLoc, drive, dir, fname, ext);
|
||||||
strcpy(RomName, fname);
|
strcpy(RomName, fname);
|
||||||
}
|
}
|
||||||
|
|
||||||
//remove all /,\,: from the string
|
//remove all /,\,: from the string
|
||||||
for (count = 0; count < (int)strlen(RomName); count++) {
|
for (count = 0; count < (int)strlen(RomName); count++)
|
||||||
switch (RomName[count]) {
|
{
|
||||||
|
switch (RomName[count])
|
||||||
|
{
|
||||||
case '/': case '\\': RomName[count] = '-'; break;
|
case '/': case '\\': RomName[count] = '-'; break;
|
||||||
case ':': RomName[count] = ';'; break;
|
case ':': RomName[count] = ';'; break;
|
||||||
}
|
}
|
||||||
|
@ -545,7 +595,8 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
m_FileName = FileLoc;
|
m_FileName = FileLoc;
|
||||||
m_MD5 = "";
|
m_MD5 = "";
|
||||||
|
|
||||||
if (!LoadBootCodeOnly) {
|
if (!LoadBootCodeOnly)
|
||||||
|
{
|
||||||
//Calculate files MD5
|
//Calculate files MD5
|
||||||
m_MD5 = MD5((const unsigned char *)m_ROMImage, m_RomFileSize).hex_digest();
|
m_MD5 = MD5((const unsigned char *)m_ROMImage, m_RomFileSize).hex_digest();
|
||||||
}
|
}
|
||||||
|
@ -556,6 +607,7 @@ bool CN64Rom::LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly) {
|
||||||
|
|
||||||
if (!LoadBootCodeOnly && g_Rom == this)
|
if (!LoadBootCodeOnly && g_Rom == this)
|
||||||
{
|
{
|
||||||
|
g_Settings->SaveBool(GameRunning_LoadingInProgress, false);
|
||||||
SaveRomSettingID(false);
|
SaveRomSettingID(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -602,20 +654,23 @@ void CN64Rom::SetError(LanguageStringID ErrorMsg)
|
||||||
|
|
||||||
void CN64Rom::UnallocateRomImage()
|
void CN64Rom::UnallocateRomImage()
|
||||||
{
|
{
|
||||||
if (m_hRomFileMapping) {
|
if (m_hRomFileMapping)
|
||||||
|
{
|
||||||
UnmapViewOfFile(m_ROMImage);
|
UnmapViewOfFile(m_ROMImage);
|
||||||
CloseHandle(m_hRomFileMapping);
|
CloseHandle(m_hRomFileMapping);
|
||||||
m_ROMImage = NULL;
|
m_ROMImage = NULL;
|
||||||
m_hRomFileMapping = NULL;
|
m_hRomFileMapping = NULL;
|
||||||
}
|
}
|
||||||
if (m_hRomFile) {
|
if (m_hRomFile)
|
||||||
|
{
|
||||||
CloseHandle(m_hRomFile);
|
CloseHandle(m_hRomFile);
|
||||||
m_hRomFile = NULL;
|
m_hRomFile = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if this value is still set then the image was not created a map
|
//if this value is still set then the image was not created a map
|
||||||
//file but created with VirtualAllocate
|
//file but created with VirtualAllocate
|
||||||
if (m_ROMImage) {
|
if (m_ROMImage)
|
||||||
|
{
|
||||||
VirtualFree(m_ROMImage, 0, MEM_RELEASE);
|
VirtualFree(m_ROMImage, 0, MEM_RELEASE);
|
||||||
m_ROMImage = NULL;
|
m_ROMImage = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,9 +19,9 @@ class CN64Rom :
|
||||||
enum { ReadFromRomSection = 0x400000 };
|
enum { ReadFromRomSection = 0x400000 };
|
||||||
|
|
||||||
//class variables
|
//class variables
|
||||||
HANDLE m_hRomFile, m_hRomFileMapping;
|
void * m_hRomFile, *m_hRomFileMapping;
|
||||||
BYTE * m_ROMImage;
|
uint8_t * m_ROMImage;
|
||||||
DWORD m_RomFileSize;
|
uint32_t m_RomFileSize;
|
||||||
Country m_Country;
|
Country m_Country;
|
||||||
CICChip m_CicChip;
|
CICChip m_CicChip;
|
||||||
LanguageStringID m_ErrorMsg;
|
LanguageStringID m_ErrorMsg;
|
||||||
|
@ -31,7 +31,7 @@ class CN64Rom :
|
||||||
bool AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnly);
|
bool AllocateAndLoadZipImage(const char * FileLoc, bool LoadBootCodeOnly);
|
||||||
void ByteSwapRom();
|
void ByteSwapRom();
|
||||||
void SetError(LanguageStringID ErrorMsg);
|
void SetError(LanguageStringID ErrorMsg);
|
||||||
static void __stdcall NotificationCB(LPCWSTR Status, CN64Rom * _this);
|
static void __stdcall NotificationCB(const wchar_t * Status, CN64Rom * _this);
|
||||||
void CalculateCicChip();
|
void CalculateCicChip();
|
||||||
void CalculateRomCrc();
|
void CalculateRomCrc();
|
||||||
|
|
||||||
|
@ -40,12 +40,12 @@ public:
|
||||||
~CN64Rom();
|
~CN64Rom();
|
||||||
|
|
||||||
bool LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly = false);
|
bool LoadN64Image(const char * FileLoc, bool LoadBootCodeOnly = false);
|
||||||
static bool IsValidRomImage(BYTE Test[4]);
|
static bool IsValidRomImage(uint8_t Test[4]);
|
||||||
void SaveRomSettingID(bool temp);
|
void SaveRomSettingID(bool temp);
|
||||||
void ClearRomSettingID();
|
void ClearRomSettingID();
|
||||||
CICChip CicChipID();
|
CICChip CicChipID();
|
||||||
BYTE * GetRomAddress() { return m_ROMImage; }
|
uint8_t * GetRomAddress() { return m_ROMImage; }
|
||||||
DWORD GetRomSize() const { return m_RomFileSize; }
|
uint32_t GetRomSize() const { return m_RomFileSize; }
|
||||||
stdstr GetRomMD5() const { return m_MD5; }
|
stdstr GetRomMD5() const { return m_MD5; }
|
||||||
stdstr GetRomName() const { return m_RomName; }
|
stdstr GetRomName() const { return m_RomName; }
|
||||||
stdstr GetFileName() const { return m_FileName; }
|
stdstr GetFileName() const { return m_FileName; }
|
||||||
|
|
Loading…
Reference in New Issue