2019-07-25 22:22:09 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "SpcDebugger.h"
|
|
|
|
#include "DisassemblyInfo.h"
|
|
|
|
#include "Disassembler.h"
|
|
|
|
#include "Spc.h"
|
|
|
|
#include "TraceLogger.h"
|
|
|
|
#include "CallstackManager.h"
|
2019-10-11 16:07:30 -04:00
|
|
|
#include "BreakpointManager.h"
|
2019-07-25 22:22:09 -04:00
|
|
|
#include "MemoryManager.h"
|
|
|
|
#include "Debugger.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "MemoryAccessCounter.h"
|
2019-10-11 16:07:30 -04:00
|
|
|
#include "ExpressionEvaluator.h"
|
|
|
|
#include "EmuSettings.h"
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
SpcDebugger::SpcDebugger(Debugger* debugger)
|
|
|
|
{
|
|
|
|
_debugger = debugger;
|
|
|
|
_traceLogger = debugger->GetTraceLogger().get();
|
|
|
|
_disassembler = debugger->GetDisassembler().get();
|
|
|
|
_memoryAccessCounter = debugger->GetMemoryAccessCounter().get();
|
|
|
|
_spc = debugger->GetConsole()->GetSpc().get();
|
|
|
|
_memoryManager = debugger->GetConsole()->GetMemoryManager().get();
|
2019-10-11 16:07:30 -04:00
|
|
|
_settings = debugger->GetConsole()->GetSettings().get();
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
_callstackManager.reset(new CallstackManager(debugger));
|
2019-10-11 16:07:30 -04:00
|
|
|
_breakpointManager.reset(new BreakpointManager(debugger, CpuType::Spc));
|
2019-07-25 22:22:09 -04:00
|
|
|
_step.reset(new StepRequest());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpcDebugger::Reset()
|
|
|
|
{
|
|
|
|
_prevOpCode = 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpcDebugger::ProcessRead(uint16_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
if(type == MemoryOperationType::DummyRead) {
|
|
|
|
//Ignore all dummy reads for now
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressInfo addressInfo = _spc->GetAbsoluteAddress(addr);
|
|
|
|
MemoryOperationInfo operation { addr, value, type };
|
|
|
|
|
|
|
|
if(type == MemoryOperationType::ExecOpCode) {
|
2019-10-11 16:07:30 -04:00
|
|
|
SpcState spcState = _spc->GetState();
|
2019-07-25 22:22:09 -04:00
|
|
|
|
2019-10-11 16:07:30 -04:00
|
|
|
if(_traceLogger->IsCpuLogged(CpuType::Spc) || _settings->CheckDebuggerFlag(DebuggerFlags::SpcDebuggerEnabled)) {
|
|
|
|
_disassembler->BuildCache(addressInfo, 0, CpuType::Spc);
|
2019-07-25 22:22:09 -04:00
|
|
|
|
2019-10-11 16:07:30 -04:00
|
|
|
if(_traceLogger->IsCpuLogged(CpuType::Spc)) {
|
|
|
|
DebugState debugState;
|
|
|
|
_debugger->GetState(debugState, true);
|
|
|
|
|
|
|
|
DisassemblyInfo disInfo = _disassembler->GetDisassemblyInfo(addressInfo);
|
|
|
|
_traceLogger->Log(CpuType::Spc, debugState, disInfo);
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
if(_prevOpCode == 0x3F || _prevOpCode == 0x0F) {
|
|
|
|
//JSR, BRK
|
|
|
|
uint8_t opSize = DisassemblyInfo::GetOpSize(_prevOpCode, 0, CpuType::Spc);
|
|
|
|
uint16_t returnPc = _prevProgramCounter + opSize;
|
2019-10-11 16:07:30 -04:00
|
|
|
_callstackManager->Push(_prevProgramCounter, spcState.PC, returnPc, StackFrameFlags::None);
|
2019-07-25 22:22:09 -04:00
|
|
|
} else if(_prevOpCode == 0x6F || _prevOpCode == 0x7F) {
|
|
|
|
//RTS, RTI
|
2019-10-11 16:07:30 -04:00
|
|
|
_callstackManager->Pop(spcState.PC);
|
2019-07-25 22:22:09 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:07:30 -04:00
|
|
|
if(_step->BreakAddress == (int32_t)spcState.PC && (_prevOpCode == 0x6F || _prevOpCode == 0x7F)) {
|
2019-07-25 22:22:09 -04:00
|
|
|
//RTS/RTI found, if we're on the expected return address, break immediately (for step over/step out)
|
|
|
|
_step->StepCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_prevOpCode = value;
|
2019-10-11 16:07:30 -04:00
|
|
|
_prevProgramCounter = spcState.PC;
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
if(_step->StepCount > 0) {
|
|
|
|
_step->StepCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 16:07:30 -04:00
|
|
|
_debugger->ProcessBreakConditions(_step->StepCount == 0, GetBreakpointManager(), operation, addressInfo);
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
_memoryAccessCounter->ProcessMemoryAccess(addressInfo, type, _memoryManager->GetMasterClock());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpcDebugger::ProcessWrite(uint16_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
AddressInfo addressInfo { addr, SnesMemoryType::SpcRam }; //Writes never affect the SPC ROM
|
|
|
|
MemoryOperationInfo operation { addr, value, type };
|
2019-10-11 16:07:30 -04:00
|
|
|
_debugger->ProcessBreakConditions(false, GetBreakpointManager(), operation, addressInfo);
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
|
|
_disassembler->InvalidateCache(addressInfo, CpuType::Spc);
|
|
|
|
|
|
|
|
_memoryAccessCounter->ProcessMemoryAccess(addressInfo, type, _memoryManager->GetMasterClock());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpcDebugger::Run()
|
|
|
|
{
|
|
|
|
_step.reset(new StepRequest());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpcDebugger::Step(int32_t stepCount, StepType type)
|
|
|
|
{
|
|
|
|
StepRequest step;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case StepType::Step: step.StepCount = stepCount; break;
|
|
|
|
case StepType::StepOut: step.BreakAddress = _callstackManager->GetReturnAddress(); break;
|
|
|
|
case StepType::StepOver:
|
|
|
|
if(_prevOpCode == 0x3F || _prevOpCode == 0x0F) {
|
|
|
|
//JSR, BRK
|
|
|
|
step.BreakAddress = _prevProgramCounter + DisassemblyInfo::GetOpSize(_prevOpCode, 0, CpuType::Spc);
|
|
|
|
} else {
|
|
|
|
//For any other instruction, step over is the same as step into
|
|
|
|
step.StepCount = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StepType::SpecificScanline:
|
|
|
|
case StepType::PpuStep:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_step.reset(new StepRequest(step));
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<CallstackManager> SpcDebugger::GetCallstackManager()
|
|
|
|
{
|
|
|
|
return _callstackManager;
|
2019-10-11 16:07:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BreakpointManager* SpcDebugger::GetBreakpointManager()
|
|
|
|
{
|
|
|
|
return _breakpointManager.get();
|
2019-07-25 22:22:09 -04:00
|
|
|
}
|