2019-01-14 09:18:43 +00:00
|
|
|
#include "stdafx.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2019-01-14 09:18:43 +00:00
|
|
|
#include "CPULog.h"
|
2022-07-18 08:31:00 +00:00
|
|
|
#include <Project64-core/N64System/Mips/R4300iInstruction.h>
|
2019-01-14 09:18:43 +00:00
|
|
|
|
|
|
|
CCPULog::CCPULog(size_t size) :
|
|
|
|
m_bMaxed(false),
|
|
|
|
m_Index(0)
|
|
|
|
{
|
|
|
|
if (size != 0)
|
|
|
|
{
|
|
|
|
m_Size = size;
|
|
|
|
}
|
|
|
|
else if (!g_Settings->LoadBool(Debugger_CPULoggingEnabled))
|
|
|
|
{
|
|
|
|
m_Size = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Size = g_Settings->LoadDword(Debugger_CPULogBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Size == 0)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
m_Array = nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2019-01-14 09:18:43 +00:00
|
|
|
m_Array = new CPUState[m_Size];
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPULog::~CCPULog(void)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_Array != nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
|
|
|
delete[] m_Array;
|
2021-04-12 11:35:39 +00:00
|
|
|
m_Array = nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPULog::PushState()
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_Array == nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Index == m_Size)
|
|
|
|
{
|
|
|
|
m_Index = 0;
|
|
|
|
m_bMaxed = true;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CPUState * state = &m_Array[m_Index++];
|
2019-01-14 09:18:43 +00:00
|
|
|
|
|
|
|
state->pc = g_Reg->m_PROGRAM_COUNTER;
|
|
|
|
state->opcode = R4300iOp::m_Opcode;
|
|
|
|
|
|
|
|
memcpy(state->gpr, g_Reg->m_GPR, sizeof(g_Reg->m_GPR));
|
|
|
|
state->gprHi = g_Reg->m_HI;
|
|
|
|
state->gprLo = g_Reg->m_LO;
|
|
|
|
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
state->fpr[i] = *g_Reg->m_FPR_S[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
state->fpcr = g_Reg->m_FPCR[31];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CCPULog::GetCount(void)
|
|
|
|
{
|
|
|
|
if (m_bMaxed)
|
|
|
|
{
|
|
|
|
return m_Size;
|
|
|
|
}
|
|
|
|
return m_Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CCPULog::GetSize(void)
|
|
|
|
{
|
|
|
|
return m_Size;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CPUState * CCPULog::GetEntry(size_t index)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_Array == nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_bMaxed)
|
|
|
|
{
|
|
|
|
if (index >= m_Size)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
return &m_Array[(m_Index + index) % m_Size];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= m_Index)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &m_Array[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPULog::Reset()
|
|
|
|
{
|
|
|
|
size_t newSize;
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2019-01-14 09:18:43 +00:00
|
|
|
if (!g_Settings->LoadBool(Debugger_CPULoggingEnabled))
|
|
|
|
{
|
|
|
|
newSize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newSize = g_Settings->LoadDword(Debugger_CPULogBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Index = 0;
|
|
|
|
m_bMaxed = false;
|
|
|
|
|
|
|
|
if (m_Size != newSize)
|
|
|
|
{
|
|
|
|
m_Size = newSize;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_Array != nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
|
|
|
delete[] m_Array;
|
2021-04-12 11:35:39 +00:00
|
|
|
m_Array = nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2019-01-14 09:18:43 +00:00
|
|
|
if (m_Size != 0)
|
|
|
|
{
|
|
|
|
m_Array = new CPUState[m_Size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CCPULog * CCPULog::Clone(void)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_Array == nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CCPULog * clone = new CCPULog(m_Size);
|
2019-01-14 09:18:43 +00:00
|
|
|
clone->m_bMaxed = m_bMaxed;
|
|
|
|
clone->m_Index = m_Index;
|
|
|
|
memcpy(clone->m_Array, m_Array, sizeof(CPUState) * m_Size);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CCPULog::DumpToFile(const char * path)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
FILE * fp = fopen(path, "wb");
|
2019-01-14 09:18:43 +00:00
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (fp == nullptr)
|
2019-01-14 09:18:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count = GetCount();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CPUState * state = GetEntry(i);
|
2022-07-18 08:31:00 +00:00
|
|
|
R4300iInstruction Instruction(state->pc, state->opcode.Value);
|
|
|
|
fprintf(fp, "%08X: %08X %s %s\n", state->pc, state->opcode.Value, Instruction.Name(), Instruction.Param());
|
2019-01-14 09:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
2021-03-18 03:29:16 +00:00
|
|
|
}
|