Skip to content

Commit

Permalink
TestBench: add support for clock polarity
Browse files Browse the repository at this point in the history
  • Loading branch information
JimKnowler committed Dec 29, 2020
1 parent 783f269 commit c8df5ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
23 changes: 17 additions & 6 deletions gtestverilog/lib/TestBench.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace gtestverilog {
TestBench(void) {
m_core = std::make_unique<MODULE>();

m_stepCount = 0;
m_stepCount = 0;

m_core->i_reset_n = 1;
m_core->i_clk = 0;
m_core->eval();

setClockPolarity(0);
}

~TestBench(void) {
Expand All @@ -38,11 +38,11 @@ namespace gtestverilog {
void tick(size_t numTicks = 1) {
for (size_t i=0; i<numTicks; i++) {
// rising edge
assert(m_core->i_clk == 0);
assert(m_core->i_clk == m_clockPolarity);
nextStep();

// falling edge
assert(m_core->i_clk == 1);
assert(m_core->i_clk != m_clockPolarity);
nextStep();
}
}
Expand All @@ -66,15 +66,26 @@ namespace gtestverilog {
return m_stepCount;
}

/// @brief set the polarity of i_clk
/// @value polarity the value of i_clk in IDLE state (either 0 or 1)
void setClockPolarity(int polarity) {
assert((polarity==1) || (polarity==0));
m_clockPolarity = polarity;

m_core->i_clk = m_clockPolarity;
m_core->eval();
}

protected:
virtual void onNextStep() {

}

private:
uint64_t m_stepCount;
std::unique_ptr<MODULE> m_core;

uint64_t m_stepCount;
int m_clockPolarity;
};

}
35 changes: 35 additions & 0 deletions gtestverilog/test/TestBench.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,39 @@ TEST(TestBench, ShouldGetStepCount) {

testBench.tick(3);
ASSERT_EQ(8, testBench.stepCount());
}

TEST(TestBench, ShouldSetClockPolarity0) {
TestBench<TestCore> testBench;
auto& core = testBench.core();

testBench.setClockPolarity(0);
ASSERT_EQ(0, core.i_clk);

testBench.nextStep();
ASSERT_EQ(1, core.i_clk);

testBench.nextStep();
ASSERT_EQ(0, core.i_clk);

testBench.tick();
ASSERT_EQ(0, core.i_clk);
}

TEST(TestBench, ShouldSetClockPolarity1) {
TestBench<TestCore> testBench;
auto& core = testBench.core();

testBench.setClockPolarity(1);
ASSERT_EQ(1, core.i_clk);

testBench.nextStep();
ASSERT_EQ(0, core.i_clk);

testBench.nextStep();
ASSERT_EQ(1, core.i_clk);

testBench.tick();
ASSERT_EQ(1, core.i_clk);

}

0 comments on commit c8df5ae

Please sign in to comment.