-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chisel-practice): add parallel counter module
Add led indicators to the board physical constraints file. Display current number in count-combinations via the provided leds.
- Loading branch information
1 parent
4e583d8
commit 79d0835
Showing
4 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
projects/chisel-practice/src/main/scala/projects/ParallelCarryCounter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package projects | ||
|
||
import chisel3.util.log2Up | ||
import chisel3.util.Counter | ||
import chisel3.util.PopCount | ||
import chisel3._ | ||
import circt.stage.ChiselStage | ||
|
||
class ParallelCarryCounter(maxValue: Int) extends Module { | ||
val bitWidth: Int = log2Up(maxValue) | ||
|
||
val io = IO(new Bundle { | ||
val count = Output(UInt(bitWidth.W)) | ||
val overflow = Output(Bool()) | ||
}) | ||
|
||
val value = RegInit(VecInit(Seq.fill(bitWidth)(false.B))) | ||
|
||
value(0) := value(0) ^ 1.U(1.W) | ||
for (i <- 1 until bitWidth) { | ||
value(i) := value(i) ^ value | ||
.slice(0, i) | ||
.map(_.asBool) | ||
.reduce(_ & _) | ||
} | ||
|
||
io.count := value.asUInt | ||
io.overflow := (value.asUInt === (maxValue - 1).U(bitWidth.W)) | ||
when(io.overflow) { | ||
for (i <- 0 until bitWidth) { value(i) := false.B } | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
projects/chisel-practice/src/test/scala/projects/ParallelCarryCounter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package projects.tests | ||
|
||
import projects.ParallelCarryCounter | ||
import chisel3._ | ||
import chiseltest._ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
|
||
class ParallelCarryCounterTester | ||
extends AnyFlatSpec | ||
with ChiselScalatestTester { | ||
"ParallelCarryCounter" should "count up to 2" in { | ||
test(new ParallelCarryCounter(2)) { dut => | ||
dut.io.count.expect(0.U) | ||
dut.io.overflow.expect(false.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(1.U) | ||
dut.io.overflow.expect(true.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(0.U) | ||
dut.io.overflow.expect(false.B) | ||
} | ||
} | ||
|
||
"ParallelCarryCounter" should "count up to 5" in { | ||
test(new ParallelCarryCounter(5)).withAnnotations(Seq(WriteVcdAnnotation)) { | ||
dut => | ||
dut.io.count.expect(0.U) | ||
dut.io.overflow.expect(false.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(1.U) | ||
dut.io.overflow.expect(false.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(2.U) | ||
dut.io.overflow.expect(false.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(3.U) | ||
dut.io.overflow.expect(false.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(4.U) | ||
dut.io.overflow.expect(true.B) | ||
dut.clock.step(1) | ||
dut.io.count.expect(0.U) | ||
dut.io.overflow.expect(false.B) | ||
} | ||
} | ||
} |