summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3/experimental
diff options
context:
space:
mode:
authorChick Markley2021-04-27 12:17:17 -0700
committerGitHub2021-04-27 12:17:17 -0700
commit6deb379b1d8bafc81a605f60476bf0f24eac60b4 (patch)
treeb12c7dfaea1948ec9a7fa2f389db0699b3d1daf4 /core/src/main/scala/chisel3/experimental
parent23b3fe8a6a2db92599bb0775626425056d47d1de (diff)
Introduce VecLiterals (#1834)
This PR provides for support for Vec literals. They can be one of two forms Inferred: ``` Vec.Lit(0x1.U, 0x2.U) ``` or explicit: ``` Vec(2, UInt(4.W)).Lit(0 -> 0x1.U, 1 -> 0x2.U) ``` - Explicit form allows for partial, or sparse, literals. - Vec literals can be used as Register initializers - Arbitrary nesting (consistent with type constraints is allowed)
Diffstat (limited to 'core/src/main/scala/chisel3/experimental')
-rw-r--r--core/src/main/scala/chisel3/experimental/package.scala33
1 files changed, 32 insertions, 1 deletions
diff --git a/core/src/main/scala/chisel3/experimental/package.scala b/core/src/main/scala/chisel3/experimental/package.scala
index 4dc7ba4b..e8360430 100644
--- a/core/src/main/scala/chisel3/experimental/package.scala
+++ b/core/src/main/scala/chisel3/experimental/package.scala
@@ -2,6 +2,8 @@
package chisel3
+import chisel3.internal.sourceinfo.SourceInfo
+
/** Package for experimental features, which may have their API changed, be removed, etc.
*
* Because its contents won't necessarily have the same level of stability and support as
@@ -124,10 +126,39 @@ package object experimental {
object BundleLiterals {
implicit class AddBundleLiteralConstructor[T <: Record](x: T) {
- def Lit(elems: (T => (Data, Data))*): T = {
+ def Lit(elems: (T => (Data, Data))*)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
+ x._makeLit(elems: _*)
+ }
+ }
+ }
+
+ /** This class provides the `Lit` method needed to define a `Vec` literal
+ */
+ object VecLiterals {
+ implicit class AddVecLiteralConstructor[T <: Data](x: Vec[T]) {
+ /** Given a generator of a list tuples of the form [Int, Data]
+ * constructs a Vec literal, parallel concept to `BundleLiteral`
+ *
+ * @param elems tuples of an index and a literal value
+ * @return
+ */
+ def Lit(elems: (Int, T)*)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Vec[T] = {
x._makeLit(elems: _*)
}
}
+
+ implicit class AddObjectLiteralConstructor(x: Vec.type) {
+ /** This provides an literal construction method for cases using
+ * object `Vec` as in `Vec.Lit(1.U, 2.U)`
+ */
+ def Lit[T <: Data](elems: T*)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Vec[T] = {
+ require(elems.nonEmpty, s"Lit.Vec(...) must have at least one element")
+ val indexElements = elems.zipWithIndex.map { case (element, index) => (index, element)}
+ val widestElement = elems.maxBy(_.getWidth)
+ val vec: Vec[T] = Vec.apply(indexElements.length, chiselTypeOf(widestElement))
+ vec.Lit(indexElements:_*)
+ }
+ }
}
// Use to add a prefix to any component generated in input scope