Skip to content

Commit

Permalink
test(countcomb): add chiseltest-based tests with vcd output (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
serjzimmerman authored Apr 13, 2024
1 parent f560b0b commit 4e583d8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions nix/shells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
scala_2_12
circt
verible
gtkwave
]
++ (with pkgs.python3Packages; [ apycula ]);

Expand Down
1 change: 1 addition & 0 deletions projects/chisel-practice/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
target/
generated/
project/
test_run_dir/
2 changes: 2 additions & 0 deletions projects/chisel-practice/build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
val chiselVersion = "6.2.0"
val chiselTestVersion = "6.0.0"

lazy val root = (project in file("."))
.settings(
Expand All @@ -13,6 +14,7 @@ lazy val root = (project in file("."))
"-Ymacro-annotations"
),
libraryDependencies ++= Seq(
"edu.berkeley.cs" %% "chiseltest" % chiselTestVersion,
"org.chipsalliance" %% "chisel" % chiselVersion
),
addCompilerPlugin(
Expand Down
7 changes: 6 additions & 1 deletion projects/chisel-practice/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ mkSbtDerivation {
runHook postBuild
'';

depsSha256 = "sha256-g1aqoVPugEIxSrmw7S0+MM6TnD4O0IzdFZiRpmQCHQI=";
depsSha256 = "sha256-zenDa/z5edMIRRwwsbHh99IezYQSQFtYRwtqlDfAYwA=";

doCheck = true;
checkPhase = ''
sbt test
'';

installPhase = ''
mkdir -p $out/share
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package projects.tests

import projects.CountCombinations
import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec

class CountCombinationsTester extends AnyFlatSpec with ChiselScalatestTester {
"CountCombinations of 0b11 in 0b11111111" should "be equal to 7" in {
test(new CountCombinations(8)) { dut =>
dut.io.value.poke("b11111111".U)
dut.io.count.expect(7.U)
}
}

"CountCombinations of 0b11 in 0b00000000" should "be equal to 0" in {
test(new CountCombinations(8)) { dut =>
dut.io.value.poke("b00000000".U)
dut.io.count.expect(0.U)
}
}
}

class CountCombinationsTestbench(
numBits: Int,
countMax: Int
) extends Module {
val io = IO(new Bundle {
val value = Output(UInt(util.log2Up(countMax).W))
val count = Output(UInt(util.log2Up(numBits).W))
})

val dut = Module(new CountCombinations(numBits))
val (value, _) = util.Counter(true.B, countMax)

dut.io.value := value
io.value <> value
io.count := dut.io.count
}

class CountCombinationsTopTester
extends AnyFlatSpec
with ChiselScalatestTester {
"CountCombinations" should "output 0, 0, 0, 1" in {
test(new CountCombinationsTestbench(8, 4))
.withAnnotations(Seq(WriteVcdAnnotation)) { dut =>
dut.io.value.expect(0.U)
dut.io.count.expect(0.U)
dut.clock.step(1)
dut.io.value.expect(1.U)
dut.io.count.expect(0.U)
dut.clock.step(1)
dut.io.value.expect(2.U)
dut.io.count.expect(0.U)
dut.clock.step(1)
dut.io.value.expect(3.U)
dut.io.count.expect(1.U)
dut.clock.step(1)
}
}

"CountCombinations" should "work for all 8-bit numbers" in {
test(new CountCombinationsTestbench(8, 256))
.withAnnotations(Seq(WriteVcdAnnotation)) { dut =>
dut.clock.step(256)
}
}
}

0 comments on commit 4e583d8

Please sign in to comment.