summaryrefslogtreecommitdiff
path: root/docs/src/cookbooks/cookbook.md
diff options
context:
space:
mode:
authorJack2021-12-18 08:27:38 +0000
committerJack2021-12-18 08:27:38 +0000
commitdd9ad534771247ac16eaa47eb9794102736b5102 (patch)
treed4566d317cb8526b79017de1e438aea8217dd1d4 /docs/src/cookbooks/cookbook.md
parent440edc4436fb3a8a4175ae425a0d31c4997ee60f (diff)
parentf50f74f583fba7b98e550c440df091e559ce32b8 (diff)
Merge branch 'master' into 3.5-release
Diffstat (limited to 'docs/src/cookbooks/cookbook.md')
-rw-r--r--docs/src/cookbooks/cookbook.md39
1 files changed, 35 insertions, 4 deletions
diff --git a/docs/src/cookbooks/cookbook.md b/docs/src/cookbooks/cookbook.md
index ce49b668..d4cf3030 100644
--- a/docs/src/cookbooks/cookbook.md
+++ b/docs/src/cookbooks/cookbook.md
@@ -24,6 +24,7 @@ Please note that these examples make use of [Chisel's scala-style printing](../e
* [How do I unpack a value ("reverse concatenation") like in Verilog?](#how-do-i-unpack-a-value-reverse-concatenation-like-in-verilog)
* [How do I do subword assignment (assign to some bits in a UInt)?](#how-do-i-do-subword-assignment-assign-to-some-bits-in-a-uint)
* [How do I create an optional I/O?](#how-do-i-create-an-optional-io)
+* [How do I minimize the number of bits used in an output vector](#how-do-i-minimize-the-number-of-bits-used-in-an-output-vector)
* Predictable Naming
* [How do I get Chisel to name signals properly in blocks like when/withClockAndReset?](#how-do-i-get-chisel-to-name-signals-properly-in-blocks-like-whenwithclockandreset)
* [How do I get Chisel to name the results of vector reads properly?](#how-do-i-get-chisel-to-name-the-results-of-vector-reads-properly)
@@ -88,13 +89,14 @@ you are tying off, you can use `chiselTypeOf`:
```scala mdoc:silent:reset
import chisel3._
+import chisel3.stage.ChiselStage
class MyBundle extends Bundle {
val foo = UInt(4.W)
val bar = Vec(4, UInt(1.W))
}
-class Foo(typ: Data) extends RawModule {
+class Foo(typ: MyBundle) extends RawModule {
val bundleA = IO(Output(typ))
val bundleB = IO(Output(typ))
@@ -107,9 +109,7 @@ class Foo(typ: Data) extends RawModule {
bundleB := 0.U.asTypeOf(chiselTypeOf(bundleB))
}
-class Bar extends RawModule {
- val foo = Module(new Foo(new MyBundle()))
-}
+ChiselStage.emitVerilog(new Foo(new MyBundle))
```
### How do I create a Vec of Bools from a UInt?
@@ -405,6 +405,37 @@ class ModuleWithOptionalIO(flag: Boolean) extends Module {
}
```
+### How do I minimize the number of bits used in an output vector?
+
+Use inferred width and a `Seq` instead of a `Vec`:
+
+Consider:
+
+```scala mdoc:silent:reset
+import chisel3._
+
+// Count the number of set bits up to and including each bit position
+class CountBits(width: Int) extends Module {
+ val bits = IO(Input(UInt(width.W)))
+ val countSequence = Seq.tabulate(width)(i => IO(Output(UInt())))
+ val countVector = IO(Output(Vec(width, UInt())))
+ countSequence.zipWithIndex.foreach { case (port, i) =>
+ port := util.PopCount(bits(i, 0))
+ }
+ countVector := countSequence
+}
+```
+
+Unlike `Vecs` which represent a singular Chisel type and must have the same width for every element,
+`Seq` is a purely Scala construct, so their elements are independent from the perspective of Chisel and can have different widths.
+
+```scala mdoc:verilog
+chisel3.stage.ChiselStage.emitVerilog(new CountBits(4))
+ // remove the body of the module by removing everything after ');'
+ .split("\\);")
+ .head + ");\n"
+```
+
## Predictable Naming
### How do I get Chisel to name signals properly in blocks like when/withClockAndReset?