Skip to content

Commit

Permalink
TestBench: add 'onStepSimulate' virtual function + callback
Browse files Browse the repository at this point in the history
  • Loading branch information
JimKnowler committed Jan 3, 2021
1 parent 9eb29b8 commit d260197
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
17 changes: 17 additions & 0 deletions gtestverilog/example/Counter.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace {
class Counter : public ::testing::Test {
public:
void SetUp() override {
testBench.setCallbackSimulate([this]{
auto& core = testBench.core();

// synchronize 'i_simulate' with 'i_clk'
core.i_simulate = core.i_clk;
});
}

void TearDown() override {
Expand Down Expand Up @@ -52,3 +58,14 @@ TEST_F(Counter, ShouldIncrementRepeatedly) {

ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected));
}

TEST_F(Counter, ShouldSimulate) {
testBench.reset();
testBench.tick(10);

const Trace traceExpected = TraceBuilder()
.port(i_clk).signal( "10" ).repeat(11)
.port(i_simulate).signal( "10" ).repeat(11);

ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected));
}
4 changes: 4 additions & 0 deletions gtestverilog/example/Counter.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module Counter(
input i_clk,
input i_reset_n,
/* verilator lint_off UNUSED */
input i_simulate,
/* verilator lint_on UNUSED */

output [15:0] o_value
);

Expand Down
10 changes: 9 additions & 1 deletion gtestverilog/lib/TestBench.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ namespace gtestverilog {
// evaluate core to allow combinatorial values to settle first
m_core->eval();

// invert clock and evaluate again
// invert clock
m_core->i_clk = (m_core->i_clk) ? 0 : 1;

// notify virtual function - to allow any simulation code to run with the new clock value
onStepSimulate();

// evaluate the core
m_core->eval();

// step is complete
m_stepCount += 1;

// notify virtual function - that step has completed
onStep();
}

Expand All @@ -83,6 +90,7 @@ namespace gtestverilog {
}

protected:
virtual void onStepSimulate() {}
virtual void onStep() {}

private:
Expand Down
22 changes: 19 additions & 3 deletions gtestverilog/tools/generate_testbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,19 @@ def write_source_file():
source_bottom = """
trace.append(step);
}}
}}
""".format(
)
void {name}::onStepSimulate() {{
if (callbackSimulate) {{
callbackSimulate();
}}
}}
void {name}::setCallbackSimulate(CallbackSimulate callback) {{
callbackSimulate = callback;
}}
}} // namespace
""".format(name = args.name)

with open(args.output_source, "w") as file:
file.write("// my output source\n")
Expand Down Expand Up @@ -128,9 +137,16 @@ class {name} : public ::gtestverilog::TestBench<{verilated_name}> {{
public:
virtual void onStep() override;
virtual void onStepSimulate() override;
typedef std::function<void()> CallbackSimulate;
void setCallbackSimulate(CallbackSimulate callback);
::gtestverilog::Trace trace;
private:
CallbackSimulate callbackSimulate;
}};
}}
Expand Down

0 comments on commit d260197

Please sign in to comment.