diff --git a/include/verilated.cpp b/include/verilated.cpp index cf1d76d8f..d5821b8f7 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -2908,6 +2908,12 @@ void VerilatedImp::versionDump() VL_MT_SAFE { VL_PRINTF_MT(" Version: %s %s\n", Verilated::productName(), Verilated::productVersion()); } +//=========================================================================== +// VerilatedModel:: Methods + +VerilatedModel::VerilatedModel(VerilatedContext& context) + : m_context{context} {} + //=========================================================================== // VerilatedModule:: Methods diff --git a/include/verilated.h b/include/verilated.h index bc1d5a3f2..3a4fab003 100644 --- a/include/verilated.h +++ b/include/verilated.h @@ -81,6 +81,7 @@ #endif // clang-format on +class VerilatedContext; class VerilatedContextImp; class VerilatedContextImpData; class VerilatedCovContext; @@ -261,11 +262,16 @@ public: class VerilatedModel VL_NOT_FINAL { VL_UNCOPYABLE(VerilatedModel); + VerilatedContext& m_context; // The VerilatedContext this model is instantiated under + protected: - explicit VerilatedModel() = default; + explicit VerilatedModel(VerilatedContext& context); virtual ~VerilatedModel() = default; public: + /// Returns the VerilatedContext this model is instantiated under + /// Used to get to e.g. simulation time via contextp()->time() + inline VerilatedContext* contextp() const { return &m_context; } /// Returns the hierarchical name of this module instance. virtual const char* hierName() = 0; /// Returns the name of this model (the name of the generated model class). diff --git a/src/V3EmitCModel.cpp b/src/V3EmitCModel.cpp index 203582609..9522b214f 100644 --- a/src/V3EmitCModel.cpp +++ b/src/V3EmitCModel.cpp @@ -195,9 +195,6 @@ class EmitCModel final : public EmitCFunc { } } - puts("/// Return current simulation context for this model.\n"); - puts("/// Used to get to e.g. simulation time via contextp()->time()\n"); - puts("VerilatedContext* contextp() const;\n"); if (!optSystemC()) { puts("/// Retrieve name of this model instance (as passed to constructor).\n"); puts("const char* name() const;\n"); @@ -241,11 +238,12 @@ class EmitCModel final : public EmitCFunc { puts(topClassName() + "::" + topClassName()); if (optSystemC()) { puts("(sc_module_name /* unused */)\n"); - puts(" : vlSymsp{new " + symClassName() - + "(Verilated::threadContextp(), name(), this)}\n"); + puts(" : VerilatedModel{*Verilated::threadContextp()}\n"); + puts(" , vlSymsp{new " + symClassName() + "(contextp(), name(), this)}\n"); } else { puts(+"(VerilatedContext* _vcontextp__, const char* _vcname__)\n"); - puts(" : vlSymsp{new " + symClassName() + "(_vcontextp__, _vcname__, this)}\n"); + puts(" : VerilatedModel{*_vcontextp__}\n"); + puts(" , vlSymsp{new " + symClassName() + "(contextp(), _vcname__, this)}\n"); } // Set up IO references @@ -271,7 +269,7 @@ class EmitCModel final : public EmitCFunc { puts("{\n"); puts("// Register model with the context\n"); - puts("vlSymsp->_vm_contextp__->addModel(this);\n"); + puts("contextp()->addModel(this);\n"); if (optSystemC()) { // Create sensitivity list for when to evaluate the model. @@ -469,10 +467,6 @@ class EmitCModel final : public EmitCFunc { } putSectionDelimiter("Utilities"); - // ::contextp - puts("\nVerilatedContext* " + topClassName() + "::contextp() const {\n"); - puts(/**/ "return vlSymsp->_vm_contextp__;\n"); - puts("}\n"); if (!optSystemC()) { // ::name