diff --git a/interface/vsrtl_interface.h b/interface/vsrtl_interface.h index 94adb2f..4745ed4 100644 --- a/interface/vsrtl_interface.h +++ b/interface/vsrtl_interface.h @@ -712,14 +712,15 @@ class SimDesign : public SimComponent { long long getCycleCount() const { return m_cycleCount; } /** - * @brief vcdDump - * @param enabled; enables dumping of all ports to a vcd file. For each port - * in the circuit, we connect an additional slot which will queue a notice to - * this top-level Design that the variable change is to be written to the VCD - * file. + * @brief vcdTrace + * @param enabled; enables dumping of all ports to a vcd file. For each + * port in the circuit, we connect an additional slot which will queue a + * notice to this top-level Design that the variable change is to be + * written to the VCD file. */ - void vcdDump(bool enabled) { + void vcdTrace(bool enabled, const std::string &filename = "") { m_dumpVcdFiles = enabled; + m_vcdFileName = filename.empty() ? getName() + ".vcd" : filename; std::map> componentGraph; getComponentGraph(componentGraph); for (const auto &compIt : componentGraph) { @@ -745,7 +746,7 @@ class SimDesign : public SimComponent { * wherein they reside. */ void resetVcdFile() { - m_vcdFile = std::make_unique(getName() + ".vcd"); + m_vcdFile = std::make_unique(m_vcdFileName); { auto def1 = m_vcdFile->writeHeader(); auto def2 = m_vcdFile->scopeDef("TOP"); @@ -819,6 +820,7 @@ class SimDesign : public SimComponent { std::set m_vcdVarChangeQueue; std::string m_vcdClkId; bool m_dumpVcdFiles = false; + std::string m_vcdFileName; #ifndef NDEBUG long long m_cycleCountPre = 0; diff --git a/interface/vsrtl_vcdfile.cpp b/interface/vsrtl_vcdfile.cpp index 1e3cd53..90945f6 100644 --- a/interface/vsrtl_vcdfile.cpp +++ b/interface/vsrtl_vcdfile.cpp @@ -23,16 +23,18 @@ std::string binStr(T val, unsigned width) { return s; } -VCDFile::VCDFile(const std::string &filename) { - m_file.open(filename, std::ios_base::trunc); +VCDFile::VCDFile(const std::string &filename) { m_filename = filename; } + +void VCDFile::ensureOpen() { + if (m_file.is_open()) + return; + m_file.open(m_filename, std::ios_base::trunc); } VCDFile::~VCDFile() { m_file.close(); } void VCDFile::writeLine(const std::string &line) { - if (!m_file.is_open()) { - throw std::runtime_error("Tried to write to file, but file was not open"); - } + ensureOpen(); const std::string indent = std::string(m_scopeLevel * 4, ' '); m_file << indent << line + "\n"; }; diff --git a/interface/vsrtl_vcdfile.h b/interface/vsrtl_vcdfile.h index 802f428..96ff51a 100644 --- a/interface/vsrtl_vcdfile.h +++ b/interface/vsrtl_vcdfile.h @@ -36,12 +36,15 @@ class VCDFile { } private: + // Ensures that the file is opened and ready for writing. + void ensureOpen(); std::string genId(); void writeLine(const std::string &line); std::ofstream m_file; std::map m_varWidths; std::map m_dumpVars; + std::string m_filename; unsigned m_varCntr = 0; unsigned m_scopeLevel = 0; };