project64/Source/Project64/UserInterface/Debugger/CPULog.cpp

176 lines
3.0 KiB
C++
Raw Normal View History

#include "stdafx.h"
2022-09-21 05:16:07 +00:00
#include "CPULog.h"
#include <Project64-core/N64System/Mips/R4300iInstruction.h>
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;
return;
}
2022-09-21 05:16:07 +00:00
m_Array = new CPUState[m_Size];
}
CCPULog::~CCPULog(void)
{
2021-04-12 11:35:39 +00:00
if (m_Array != nullptr)
{
delete[] m_Array;
2021-04-12 11:35:39 +00:00
m_Array = nullptr;
}
}
void CCPULog::PushState()
{
2021-04-12 11:35:39 +00:00
if (m_Array == nullptr)
{
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++];
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)
{
2021-04-12 11:35:39 +00:00
if (m_Array == nullptr)
{
2021-04-12 11:35:39 +00:00
return nullptr;
}
if (m_bMaxed)
{
if (index >= m_Size)
{
2021-04-12 11:35:39 +00:00
return nullptr;
}
return &m_Array[(m_Index + index) % m_Size];
}
if (index >= m_Index)
{
2021-04-12 11:35:39 +00:00
return nullptr;
}
return &m_Array[index];
}
void CCPULog::Reset()
{
size_t newSize;
2022-09-21 05:16:07 +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)
{
delete[] m_Array;
2021-04-12 11:35:39 +00:00
m_Array = nullptr;
}
2022-09-21 05:16:07 +00:00
if (m_Size != 0)
{
m_Array = new CPUState[m_Size];
}
}
}
2022-09-21 05:16:07 +00:00
CCPULog * CCPULog::Clone(void)
{
2021-04-12 11:35:39 +00:00
if (m_Array == nullptr)
{
2021-04-12 11:35:39 +00:00
return nullptr;
}
2022-09-21 05:16:07 +00:00
CCPULog * clone = new CCPULog(m_Size);
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)
{
2022-09-21 05:16:07 +00:00
FILE * fp = fopen(path, "wb");
2021-04-12 11:35:39 +00:00
if (fp == nullptr)
{
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);
R4300iInstruction Instruction(state->pc, state->opcode.Value);
fprintf(fp, "%08X: %08X %s %s\n", state->pc, state->opcode.Value, Instruction.Name(), Instruction.Param());
}
fclose(fp);
2021-03-18 03:29:16 +00:00
}