From 4a6f2834843dc562b5c87a2dc89987f2790b8637 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 4 Nov 2019 00:55:07 +1000 Subject: [PATCH] CPU: Add trace log to file support --- src/core/bus.cpp | 6 ++++++ src/core/cpu_core.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/core/cpu_core.h | 5 +++++ 3 files changed, 49 insertions(+) diff --git a/src/core/bus.cpp b/src/core/bus.cpp index 9d9da929c..5b5b95c6e 100644 --- a/src/core/bus.cpp +++ b/src/core/bus.cpp @@ -376,7 +376,13 @@ void Bus::DoWriteEXP2(MemoryAccessSize size, u32 offset, u32 value) if (value == '\n') { if (!m_tty_line_buffer.IsEmpty()) + { Log_InfoPrintf("TTY: %s", m_tty_line_buffer.GetCharArray()); +#ifdef _DEBUG + if (CPU::LOG_EXECUTION) + CPU::WriteToExecutionLog("TTY: %s\n", m_tty_line_buffer.GetCharArray()); +#endif + } m_tty_line_buffer.Clear(); } else diff --git a/src/core/cpu_core.cpp b/src/core/cpu_core.cpp index d54674cc2..20ed2e900 100644 --- a/src/core/cpu_core.cpp +++ b/src/core/cpu_core.cpp @@ -6,7 +6,33 @@ Log_SetChannel(CPU::Core); namespace CPU { + bool TRACE_EXECUTION = false; +bool LOG_EXECUTION = false; + +void WriteToExecutionLog(const char* format, ...) +{ + static std::FILE* log_file = nullptr; + static bool log_file_opened = false; + + std::va_list ap; + va_start(ap, format); + + if (!log_file_opened) + { + log_file = std::fopen("cpu_log.txt", "wb"); + log_file_opened = true; + } + + if (log_file) + { + std::vfprintf(log_file, format, ap); + std::fflush(log_file); + } + + va_end(ap); +} + Core::Core() = default; @@ -495,6 +521,14 @@ static void PrintInstruction(u32 bits, u32 pc, Core* state) std::printf("%08x: %08x %s\n", pc, bits, instr.GetCharArray()); } +static void LogInstruction(u32 bits, u32 pc, Core* state) +{ + TinyString instr; + DisassembleInstruction(&instr, pc, bits, state); + + WriteToExecutionLog("%08x: %08x %s\n", pc, bits, instr.GetCharArray()); +} + static constexpr bool AddOverflow(u32 old_value, u32 add_value, u32 new_value) { return (((new_value ^ old_value) & (new_value ^ add_value)) & UINT32_C(0x80000000)) != 0; @@ -596,8 +630,12 @@ void Core::ExecuteInstruction() } #endif +#ifdef _DEBUG if (TRACE_EXECUTION) PrintInstruction(inst.bits, m_current_instruction_pc, this); + if (LOG_EXECUTION) + LogInstruction(inst.bits, m_current_instruction_pc, this); +#endif switch (inst.op) { diff --git a/src/core/cpu_core.h b/src/core/cpu_core.h index 2a0abf653..924d46bf3 100644 --- a/src/core/cpu_core.h +++ b/src/core/cpu_core.h @@ -88,6 +88,7 @@ private: } void DisassembleAndPrint(u32 addr); + void DisassembleAndLog(u32 addr); void DisassembleAndPrint(u32 addr, u32 instructions_before, u32 instructions_after); // Fetches the instruction at m_regs.npc @@ -156,6 +157,10 @@ private: }; extern bool TRACE_EXECUTION; +extern bool LOG_EXECUTION; + +// Write to CPU execution log file. +void WriteToExecutionLog(const char* format, ...); } // namespace CPU