From 91df9ea48b9dedc783c91b18de019ca68edcffb3 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Thu, 30 Dec 2021 12:32:34 +0000 Subject: [PATCH] Make Z80 profile output conditional on the availability of data. --- Z80/inc/Profiler.h | 21 +++++++++++++++++++++ Z80/src/Profiler.cpp | 26 +++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Z80/inc/Profiler.h b/Z80/inc/Profiler.h index 4943a38..5f7975f 100644 --- a/Z80/inc/Profiler.h +++ b/Z80/inc/Profiler.h @@ -24,6 +24,27 @@ namespace EightBit { Z80& m_cpu; Disassembler& m_disassembler; + [[nodiscard]] constexpr const auto& instructions() const { return m_instructions; } + [[nodiscard]] constexpr const auto& addresses() const { return m_addresses; } + + [[nodiscard]] constexpr auto instructions_available() const noexcept { + const auto found = std::find_if( + instructions().begin(), instructions().end(), + [](const auto& value) { return value != 0; }); + return found != instructions().end(); + } + + [[nodiscard]] constexpr auto addresses_available() const noexcept { + const auto found = std::find_if( + addresses().begin(), addresses().end(), + [](const auto& value) { return value != 0; }); + return found != addresses().end(); + } + + [[nodiscard]] constexpr auto available() const noexcept { + return instructions_available() || addresses_available(); + } + void dumpInstructionProfiles() const; void dumpAddressProfiles() const; }; diff --git a/Z80/src/Profiler.cpp b/Z80/src/Profiler.cpp index e5d9f56..1d43600 100644 --- a/Z80/src/Profiler.cpp +++ b/Z80/src/Profiler.cpp @@ -27,28 +27,32 @@ void EightBit::Profiler::dump() const { } void EightBit::Profiler::dumpInstructionProfiles() const { - std::cout << "** instructions" << std::endl; - for (int i = 0; i < 0x100; ++i) { - auto count = m_instructions[i]; - if (count > 0) - std::cout << Disassembler::hex((uint8_t)i) << "\t" << count << std::endl; + if (instructions_available()) { + std::cout << "** instructions" << std::endl; + for (int i = 0; i < 0x100; ++i) { + auto count = m_instructions[i]; + if (count > 0) + std::cout << Disassembler::hex((uint8_t)i) << "\t" << count << std::endl; + } } } void EightBit::Profiler::dumpAddressProfiles() const { - std::cout << "** addresses" << std::endl; - for (int i = 0; i < 0x10000; ++i) { - auto count = m_addresses[i]; + if (addresses_available()) { + std::cout << "** addresses" << std::endl; + for (int i = 0; i < 0x10000; ++i) { + auto count = m_addresses[i]; - m_cpu.PC().word = i; + m_cpu.PC().word = i; - if (count > 0) - std::cout + if (count > 0) + std::cout << Disassembler::hex((uint16_t)i) << "\t" << count << "\t" << m_disassembler.disassemble(m_cpu) << std::endl; + } } }