Mesen/Core/Debugger.h

266 lines
7.5 KiB
C
Raw Normal View History

#pragma once
#include "stdafx.h"
#include <atomic>
2015-08-09 18:47:27 +00:00
#include <deque>
#include <unordered_set>
using std::atomic;
2015-08-09 18:47:27 +00:00
using std::deque;
using std::unordered_set;
#include "../Utilities/SimpleLock.h"
#include "DebuggerTypes.h"
class CPU;
2017-08-30 22:31:27 +00:00
class APU;
class PPU;
class MemoryManager;
class Console;
class Assembler;
class Disassembler;
class LabelManager;
class MemoryDumper;
class MemoryAccessCounter;
class Profiler;
class CodeRunner;
class BaseMapper;
2017-08-30 22:31:27 +00:00
class ScriptHost;
class TraceLogger;
class Breakpoint;
class CodeDataLogger;
class ExpressionEvaluator;
struct ExpressionData;
enum EvalResultType : int32_t;
enum class CdlStripFlag;
class Debugger
{
private:
static constexpr int BreakpointTypeCount = 6;
//Must be static to be thread-safe when switching game
static string _disassemblerOutput;
shared_ptr<Disassembler> _disassembler;
shared_ptr<Assembler> _assembler;
shared_ptr<MemoryDumper> _memoryDumper;
shared_ptr<CodeDataLogger> _codeDataLogger;
shared_ptr<MemoryAccessCounter> _memoryAccessCounter;
shared_ptr<LabelManager> _labelManager;
shared_ptr<TraceLogger> _traceLogger;
shared_ptr<Profiler> _profiler;
unique_ptr<CodeRunner> _codeRunner;
shared_ptr<Console> _console;
shared_ptr<CPU> _cpu;
shared_ptr<PPU> _ppu;
2017-08-30 22:31:27 +00:00
shared_ptr<APU> _apu;
shared_ptr<MemoryManager> _memoryManager;
shared_ptr<BaseMapper> _mapper;
2017-08-30 22:31:27 +00:00
bool _hasScript;
SimpleLock _scriptLock;
2017-08-30 22:31:27 +00:00
int _nextScriptId;
vector<shared_ptr<ScriptHost>> _scripts;
atomic<int32_t> _preventResume;
atomic<bool> _stopFlag;
atomic<bool> _executionStopped;
atomic<int32_t> _suspendCount;
vector<Breakpoint> _breakpoints[BreakpointTypeCount];
vector<ExpressionData> _breakpointRpnList[BreakpointTypeCount];
bool _hasBreakpoint[BreakpointTypeCount] = {};
vector<uint8_t> _frozenAddresses;
deque<StackFrameInfo> _callstack;
deque<int32_t> _subReturnAddresses;
int32_t _stepOutReturnAddress;
unordered_set<uint32_t> _functionEntryPoints;
unique_ptr<ExpressionEvaluator> _watchExpEval;
unique_ptr<ExpressionEvaluator> _bpExpEval;
DebugState _debugState;
SimpleLock _breakLock;
//Used to alter the executing address via "Set Next Statement"
uint16_t *_currentReadAddr;
uint8_t *_currentReadValue;
int32_t _nextReadAddr;
uint16_t _returnToAddress;
2015-08-18 01:59:22 +00:00
uint32_t _flags;
string _romName;
atomic<int32_t> _stepCount;
2016-06-04 19:38:48 +00:00
atomic<int32_t> _ppuStepCount;
atomic<int32_t> _stepCycleCount;
atomic<uint8_t> _lastInstruction;
atomic<bool> _stepOut;
atomic<int32_t> _stepOverAddr;
bool _enableBreakOnUninitRead;
atomic<bool> _breakRequested;
bool _pausedForDebugHelper;
atomic<int32_t> _breakOnScanline;
bool _proccessPpuCycle[341];
std::unordered_map<int, int> _ppuViewerUpdateCycle;
uint16_t _ppuScrollX;
uint16_t _ppuScrollY;
int32_t _prevInstructionCycle;
int32_t _curInstructionCycle;
int32_t _runToCycle;
bool _needRewind;
vector<stringstream> _rewindCache;
vector<uint32_t> _rewindPrevInstructionCycleCache;
uint32_t _inputOverride[4];
vector<DebugEventInfo> _prevDebugEvents;
vector<DebugEventInfo> _debugEvents;
vector<vector<int>> _debugEventMarkerRpn;
private:
void ProcessBreakpoints(BreakpointType type, OperationInfo &operationInfo, bool allowBreak = true);
void AddCallstackFrame(uint16_t source, uint16_t target, StackFrameFlags flags);
void UpdateCallstack(uint8_t currentInstruction, uint32_t addr);
2018-03-24 15:22:43 +00:00
void ProcessStepConditions(uint16_t addr);
bool SleepUntilResume(BreakSource source = BreakSource::Break);
void AddDebugEvent(DebugEventType type, uint16_t address = -1, uint8_t value = 0, int16_t breakpointId = -1, int8_t ppuLatch = -1);
void UpdatePpuCyclesToProcess();
public:
2017-08-30 22:31:27 +00:00
Debugger(shared_ptr<Console> console, shared_ptr<CPU> cpu, shared_ptr<PPU> ppu, shared_ptr<APU> apu, shared_ptr<MemoryManager> memoryManager, shared_ptr<BaseMapper> mapper);
~Debugger();
Console* GetConsole();
void SetFlags(uint32_t flags);
bool CheckFlag(DebuggerFlags flag);
void SetBreakpoints(Breakpoint breakpoints[], uint32_t length);
shared_ptr<LabelManager> GetLabelManager();
void GetFunctionEntryPoints(int32_t* entryPoints, int32_t maxCount);
int32_t GetFunctionEntryPointCount();
void GetCallstack(StackFrameInfo* callstackArray, uint32_t &callstackSize);
2018-01-02 04:23:18 +00:00
void GetApuState(ApuState *state);
__forceinline void GetState(DebugState *state, bool includeMapperInfo = true);
void SetState(DebugState state);
void Suspend();
void Resume();
void Break();
void ResumeFromBreak();
2016-06-04 19:38:48 +00:00
void PpuStep(uint32_t count = 1);
void Step(uint32_t count = 1);
void StepCycles(uint32_t cycleCount = 1);
void StepOver();
void StepOut();
void StepBack();
void Run();
void BreakImmediately();
void BreakOnScanline(int16_t scanline);
bool LoadCdlFile(string cdlFilepath);
void SetCdlData(uint8_t* cdlData, uint32_t length);
2017-08-20 02:00:12 +00:00
void ResetCdl();
void UpdateCdlCache();
bool IsMarkedAsCode(uint16_t relativeAddress);
shared_ptr<CodeDataLogger> GetCodeDataLogger();
2015-08-18 01:59:22 +00:00
void SetNextStatement(uint16_t addr);
void SetPpuViewerScanlineCycle(int32_t ppuViewerId, int32_t scanline, int32_t cycle);
void ClearPpuViewerSettings(int32_t ppuViewer);
2015-08-18 01:59:22 +00:00
bool IsExecutionStopped();
bool IsPauseIconShown();
void PreventResume();
void AllowResume();
void GenerateCodeOutput();
const char* GetCode(uint32_t &length);
int32_t GetRelativeAddress(uint32_t addr, AddressType type);
int32_t GetAbsoluteAddress(uint32_t addr);
int32_t GetAbsoluteChrAddress(uint32_t addr);
int32_t GetRelativeChrAddress(uint32_t addr);
void GetAbsoluteAddressAndType(uint32_t relativeAddr, AddressTypeInfo* info);
void GetPpuAbsoluteAddressAndType(uint32_t relativeAddr, PpuAddressTypeInfo* info);
shared_ptr<Profiler> GetProfiler();
shared_ptr<Assembler> GetAssembler();
shared_ptr<TraceLogger> GetTraceLogger();
shared_ptr<MemoryDumper> GetMemoryDumper();
shared_ptr<MemoryAccessCounter> GetMemoryAccessCounter();
int32_t EvaluateExpression(string expression, EvalResultType &resultType, bool useCache);
void ProcessPpuCycle();
bool ProcessRamOperation(MemoryOperationType type, uint16_t &addr, uint8_t &value);
void ProcessVramReadOperation(MemoryOperationType type, uint16_t addr, uint8_t &value);
void ProcessVramWriteOperation(uint16_t addr, uint8_t &value);
2017-08-30 22:31:27 +00:00
void SetLastFramePpuScroll(uint16_t addr, uint8_t xScroll, bool updateHorizontalScrollOnly);
uint32_t GetPpuScroll();
void ProcessInterrupt(uint16_t cpuAddr, uint16_t destCpuAddr, bool forNmi);
void AddTrace(const char *log);
void SetFreezeState(uint16_t address, bool frozen);
void GetFreezeState(uint16_t startAddress, uint16_t length, bool* freezeState);
void StartCodeRunner(uint8_t *byteCode, uint32_t codeLength);
void StopCodeRunner();
2017-08-12 20:52:45 +00:00
void GetNesHeader(uint8_t* header);
void SaveRomToDisk(string filename, bool saveAsIps, uint8_t* header, CdlStripFlag cdlStripflag);
void RevertPrgChrChanges();
bool HasPrgChrChanges();
2017-03-10 01:42:53 +00:00
int32_t FindSubEntryPoint(uint16_t relativeAddress);
void SetInputOverride(uint8_t port, uint32_t state);
2017-08-30 22:31:27 +00:00
int32_t LoadScript(string name, string content, int32_t scriptId);
2017-08-30 22:31:27 +00:00
void RemoveScript(int32_t scriptId);
const char* GetScriptLog(int32_t scriptId);
void ResetCounters();
void UpdateProgramCounter(uint16_t &addr, uint8_t &value);
void ProcessScriptSaveState(uint16_t &addr, uint8_t &value);
void ProcessCpuOperation(uint16_t &addr, uint8_t &value, MemoryOperationType type);
void ProcessPpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type);
2017-08-30 22:31:27 +00:00
void ProcessEvent(EventType type);
void GetDebugEvents(uint32_t* pictureBuffer, DebugEventInfo *infoArray, uint32_t &maxEventCount, bool returnPreviousFrameData);
uint32_t GetDebugEventCount(bool returnPreviousFrameData);
uint32_t GetScreenPixel(uint8_t x, uint8_t y);
};