2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
|
|
|
* Project 64 - A Nintendo 64 emulator. *
|
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-05-23 10:05:41 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
DWORD CMemoryLabel::AsciiToHex (char * HexValue)
|
|
|
|
{
|
2015-07-28 21:41:53 +00:00
|
|
|
DWORD Count, Finish, Current, Value = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
Finish = strlen(HexValue);
|
2015-03-29 17:19:28 +00:00
|
|
|
if (Finish > 8 )
|
|
|
|
{
|
|
|
|
Finish = 8;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
for (Count = 0; Count < Finish; Count++)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
Value = (Value << 4);
|
2015-07-28 21:41:53 +00:00
|
|
|
Current = HexValue[Count];
|
|
|
|
if(Current >= '0' && Current <= '9')
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-07-28 21:41:53 +00:00
|
|
|
Value += Current - '0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(Current < 'A')
|
2015-07-29 22:23:14 +00:00
|
|
|
Current += 'A' - 'a';
|
2015-07-28 21:41:53 +00:00
|
|
|
|
|
|
|
if (Current >= 'A' && Current <= 'F')
|
|
|
|
{
|
|
|
|
Value += Current + 10 - 'A';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Value = (Value >> 4);
|
|
|
|
Count = Finish;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
void CMemoryLabel::AddMemoryLabel ( DWORD Address, const char * Message, ... )
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
StringMap::iterator Item = m_LabelList.find(Address);
|
2015-03-29 17:19:28 +00:00
|
|
|
if (Item == m_LabelList.end())
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
char Msg[1000];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start( ap, Message );
|
|
|
|
_vsnprintf( Msg,sizeof(Msg),Message, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
//if item is already in the list then do not add it
|
|
|
|
m_LabelList.insert(StringMap::value_type(Address,stdstr(Msg)));
|
|
|
|
m_NewLabels += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
stdstr CMemoryLabel::LabelName ( DWORD Address ) const
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
//StringMap::iterator theIterator = m_LabelList.find(Address);
|
2015-03-29 17:19:28 +00:00
|
|
|
//if (theIterator != m_LabelList.end())
|
|
|
|
//{
|
2008-09-18 03:15:49 +00:00
|
|
|
// return (*theIterator).second;
|
|
|
|
//}
|
2015-07-28 21:41:53 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
char strLabelName[100];
|
2015-07-28 21:41:53 +00:00
|
|
|
sprintf(strLabelName,"0x%08X",Address);
|
2008-09-18 03:15:49 +00:00
|
|
|
return stdstr(strLabelName);
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
stdstr CMemoryLabel::StoredLabelName ( DWORD Address )
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
StringMap::iterator theIterator = m_LabelList.find(Address);
|
2015-03-29 17:19:28 +00:00
|
|
|
if (theIterator != m_LabelList.end())
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
return (*theIterator).second;
|
|
|
|
}
|
|
|
|
return stdstr("");
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
void CMemoryLabel::LoadLabelList ( char * file )
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
m_LabelList.clear();
|
|
|
|
CurrentLabelFile = file;
|
|
|
|
|
|
|
|
HANDLE hFile = CreateFile(file,GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
|
|
|
|
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
2015-03-29 17:19:28 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
SetFilePointer(hFile,0,NULL,FILE_BEGIN);
|
2015-07-28 21:41:53 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD FileSize = GetFileSize(hFile,NULL);
|
|
|
|
void * FileContents = VirtualAlloc(NULL,FileSize,MEM_COMMIT,PAGE_READWRITE );
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (FileContents)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD dwRead;
|
2015-03-29 17:19:28 +00:00
|
|
|
if (!ReadFile(hFile,FileContents,FileSize,&dwRead,NULL))
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
VirtualFree(FileContents, 0, MEM_RELEASE);
|
|
|
|
FileContents = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (FileContents)
|
|
|
|
{
|
|
|
|
ProcessCODFile((BYTE *)FileContents, FileSize);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-07-28 21:41:53 +00:00
|
|
|
VirtualFree(FileContents, 0, MEM_RELEASE);
|
2008-09-18 03:15:49 +00:00
|
|
|
CloseHandle(hFile);
|
|
|
|
|
|
|
|
m_NewLabels = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
// How many new labels been added since loading/saving label file
|
2015-04-28 22:19:02 +00:00
|
|
|
int CMemoryLabel::NewLabels()
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
return m_NewLabels;
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CMemoryLabel::SaveLabelList()
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
m_NewLabels = 0;
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (CurrentLabelFile.length() == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
HANDLE hFile = CreateFile(CurrentLabelFile.c_str(),GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
|
|
|
|
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
SetFilePointer(hFile,0,NULL,FILE_BEGIN);
|
2015-07-28 21:41:53 +00:00
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
for (StringMap::iterator Item = m_LabelList.begin(); Item != m_LabelList.end(); Item++)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
char Text[300];
|
|
|
|
DWORD dwWritten;
|
|
|
|
|
|
|
|
sprintf(Text, "0x%08X,%s\r\n",(*Item).first,((*Item).second).c_str());
|
|
|
|
|
2015-07-28 21:41:53 +00:00
|
|
|
WriteFile( hFile,Text,strlen(Text),&dwWritten,NULL );
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2015-07-28 21:41:53 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
CloseHandle(hFile);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
void CMemoryLabel::ProcessCODFile(BYTE * File, DWORD FileLen)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
char * CurrentPos = (char *)File;
|
|
|
|
char Label[40];
|
|
|
|
DWORD Address;
|
|
|
|
int Length;
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
while ( CurrentPos < (char *)File + FileLen )
|
|
|
|
{
|
|
|
|
if (*CurrentPos != '0')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
CurrentPos += 1;
|
2015-03-29 17:19:28 +00:00
|
|
|
if (*CurrentPos != 'x')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
CurrentPos += 1;
|
2015-07-28 21:41:53 +00:00
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (strchr(CurrentPos,',') - CurrentPos != 8)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
Address = AsciiToHex (CurrentPos);
|
|
|
|
CurrentPos += 9;
|
|
|
|
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (strchr(CurrentPos,'\r') == NULL)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
Length = strchr(CurrentPos,'\n') - CurrentPos;
|
2015-03-29 17:19:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
Length = strchr(CurrentPos,'\r') - CurrentPos;
|
2015-03-29 17:19:28 +00:00
|
|
|
if (Length > (strchr(CurrentPos,'\n') - CurrentPos))
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
Length = strchr(CurrentPos,'\n') - CurrentPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:44:06 +00:00
|
|
|
// Stay within label array bounds
|
|
|
|
if (Length > 39)
|
|
|
|
Length = 39;
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
memcpy(Label,CurrentPos,Length);
|
|
|
|
Label[Length] = '\0';
|
|
|
|
|
|
|
|
AddMemoryLabel (Address, Label);
|
|
|
|
CurrentPos = strchr(CurrentPos,'\n') + 1;
|
|
|
|
}
|
|
|
|
}
|