From 945416c628656498be7a98dcd4899f2b9e830c00 Mon Sep 17 00:00:00 2001 From: mergify[bot] Date: Tue, 2 Aug 2022 16:28:23 +0000 Subject: Fix example in Chisel Type vs Scala Type article (#2655) (#2657) (cherry picked from commit 0b2c211beefeefb72c86ea69a0b2a0101b2f2c20) Co-authored-by: Megan Wachs --- docs/src/explanations/chisel-type-vs-scala-type.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/src/explanations') diff --git a/docs/src/explanations/chisel-type-vs-scala-type.md b/docs/src/explanations/chisel-type-vs-scala-type.md index 6c311a21..ead509d3 100644 --- a/docs/src/explanations/chisel-type-vs-scala-type.md +++ b/docs/src/explanations/chisel-type-vs-scala-type.md @@ -245,13 +245,13 @@ that you have more information than it can infer to convert Scala types: ```scala mdoc:silent class ScalaCastingModule(gen: () => Bundle) extends Module { - val io = gen().asInstanceOf[MyBundle] + val io = IO(Output(gen().asInstanceOf[MyBundle])) io.foo := 0.U } ``` This works if we do indeed have more information than the compiler: -``` scala mdoc:silent +```scala mdoc:silent ChiselStage.elaborate(new ScalaCastingModule( () => new MyBundle(3))) ``` -- cgit v1.2.3 From 7bad3d2ec316f24f3da79d1dfef19e128cfe8bf5 Mon Sep 17 00:00:00 2001 From: mergify[bot] Date: Fri, 12 Aug 2022 20:04:33 +0000 Subject: Add ability to suppress enum cast warnings (#2671) (#2674) (cherry picked from commit 1ad820f7f549eddcd7188b737f59a240e48a7f0a) Co-authored-by: Zachary Yedidia --- docs/src/explanations/chisel-enum.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'docs/src/explanations') diff --git a/docs/src/explanations/chisel-enum.md b/docs/src/explanations/chisel-enum.md index a390aea4..16b5570d 100644 --- a/docs/src/explanations/chisel-enum.md +++ b/docs/src/explanations/chisel-enum.md @@ -17,6 +17,7 @@ import chisel3._ import chisel3.util._ import chisel3.stage.ChiselStage import chisel3.experimental.ChiselEnum +import chisel3.experimental.suppressEnumCastWarning ``` ```scala mdoc:invisible @@ -167,6 +168,30 @@ val (log2, _) = grabLog(ChiselStage.emitChirrtl(new SafeFromUInt)) println(s"```\n$log2```") ``` +You can also suppress the warning by using `suppressEnumCastWarning`. This is +primarily used for casting from [[UInt]] to a Bundle type that contains an +Enum, where the [[UInt]] is known to be valid for the Bundle type. + +```scala mdoc +class MyBundle extends Bundle { + val addr = UInt(8.W) + val op = Opcode() +} + +class SuppressedFromUInt extends Module { + val in = IO(Input(UInt(15.W))) + val out = IO(Output(new MyBundle())) + suppressEnumCastWarning { + out := in.asTypeOf(new MyBundle) + } +} +``` + +```scala mdoc:invisible +val (log3, _) = grabLog(ChiselStage.emitChirrtl(new SuppressedFromUInt)) +assert(log3.isEmpty) +``` + ## Testing The _Type_ of the enums values is `.Type` which can be useful for passing the values -- cgit v1.2.3 From fc3fedaa9c16d7861b452388a70ec2f6e2a3dc30 Mon Sep 17 00:00:00 2001 From: mergify[bot] Date: Tue, 20 Sep 2022 18:54:01 +0000 Subject: Cleanup Cookbook and printing docs (#2727) (#2742) * Cleanup Cookbook and printing docs * Format specifiers are actually concise now Co-authored-by: Megan Wachs (cherry picked from commit df2a71833ffc8ee8a053a1e8ea41c482e46be132) Co-authored-by: Aditya Naik <91489422+adkian-sifive@users.noreply.github.com>--- docs/src/explanations/printing.md | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'docs/src/explanations') diff --git a/docs/src/explanations/printing.md b/docs/src/explanations/printing.md index abd1a427..71a6f5cf 100644 --- a/docs/src/explanations/printing.md +++ b/docs/src/explanations/printing.md @@ -13,22 +13,22 @@ Chisel provides the `printf` function for debugging purposes. It comes in two fl ### Scala-style -Chisel also supports printf in a style similar to [Scala's String Interpolation](http://docs.scala-lang.org/overviews/core/string-interpolation.html). Chisel provides a custom string interpolator `p` which can be used as follows: +Chisel also supports printf in a style similar to [Scala's String Interpolation](http://docs.scala-lang.org/overviews/core/string-interpolation.html). Chisel provides a custom string interpolator `cf` which follows C-style format specifiers (see section [C-style](#c-style) below). Here's a few examples of using the `cf` interpolator: ```scala mdoc:invisible import chisel3._ ``` ```scala mdoc:compile-only val myUInt = 33.U -printf(p"myUInt = $myUInt") // myUInt = 33 +printf(cf"myUInt = $myUInt") // myUInt = 33 ``` -Note that when concatenating `p"..."` strings, you need to start with a `p"..."` string: +Note that when concatenating `cf"..."` strings, you need to start with a `cf"..."` string: ```scala mdoc:compile-only // Does not interpolate the second string val myUInt = 33.U -printf("my normal string" + p"myUInt = $myUInt") +printf("my normal string" + cf"myUInt = $myUInt") ``` #### Simple formatting @@ -38,22 +38,20 @@ Other formats are available as follows: ```scala mdoc:compile-only val myUInt = 33.U // Hexadecimal -printf(p"myUInt = 0x${Hexadecimal(myUInt)}") // myUInt = 0x21 +printf(cf"myUInt = 0x$myUInt%x") // myUInt = 0x21 // Binary -printf(p"myUInt = ${Binary(myUInt)}") // myUInt = 100001 +printf(cf"myUInt = $myUInt%b") // myUInt = 100001 // Character -printf(p"myUInt = ${Character(myUInt)}") // myUInt = ! +printf(cf"myUInt = $myUInt%c") // myUInt = ! ``` -We recognize that the format specifiers are verbose, so we are working on a more concise syntax. - #### Aggregate data-types Chisel provides default custom "pretty-printing" for Vecs and Bundles. The default printing of a Vec is similar to printing a Seq or List in Scala while printing a Bundle is similar to printing a Scala Map. ```scala mdoc:compile-only val myVec = VecInit(5.U, 10.U, 13.U) -printf(p"myVec = $myVec") // myVec = Vec(5, 10, 13) +printf(cf"myVec = $myVec") // myVec = Vec(5, 10, 13) val myBundle = Wire(new Bundle { val foo = UInt() @@ -61,7 +59,7 @@ val myBundle = Wire(new Bundle { }) myBundle.foo := 3.U myBundle.bar := 11.U -printf(p"myBundle = $myBundle") // myBundle = Bundle(a -> 3, b -> 11) +printf(cf"myBundle = $myBundle") // myBundle = Bundle(a -> 3, b -> 11) ``` #### Custom Printing @@ -76,11 +74,11 @@ class Message extends Bundle { val data = UInt(64.W) override def toPrintable: Printable = { val char = Mux(valid, 'v'.U, '-'.U) - p"Message:\n" + - p" valid : ${Character(char)}\n" + - p" addr : 0x${Hexadecimal(addr)}\n" + - p" length : $length\n" + - p" data : 0x${Hexadecimal(data)}\n" + cf"Message:\n" + + cf" valid : $char%c\n" + + cf" addr : $addr%x\n" + + cf" length : $length\n" + + cf" data : $data%x\n" } } @@ -90,7 +88,7 @@ myMessage.addr := "h1234".U myMessage.length := 10.U myMessage.data := "hdeadbeef".U -printf(p"$myMessage") +printf(cf"$myMessage") ``` Which prints the following: @@ -103,7 +101,7 @@ Message: data : 0x00000000deadbeef ``` -Notice the use of `+` between `p` interpolated "strings". The results of `p` interpolation can be concatenated by using the `+` operator. For more information, please see the documentation +Notice the use of `+` between `cf` interpolated "strings". The results of `cf` interpolation can be concatenated by using the `+` operator. ### C-Style -- cgit v1.2.3 From c51fcfea32b6c73e623657442460fb782ff0733b Mon Sep 17 00:00:00 2001 From: Aditya Naik Date: Thu, 10 Nov 2022 13:41:00 -0800 Subject: Warn on S-interpolator usage for assert, assume and printf (backport #2751) (#2757) * Add internal methods to maintain binary compatibility Co-authored-by: Megan Wachs Co-authored-by: Jack Koenig --- docs/src/explanations/printing.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'docs/src/explanations') diff --git a/docs/src/explanations/printing.md b/docs/src/explanations/printing.md index 71a6f5cf..80e9ec70 100644 --- a/docs/src/explanations/printing.md +++ b/docs/src/explanations/printing.md @@ -13,11 +13,23 @@ Chisel provides the `printf` function for debugging purposes. It comes in two fl ### Scala-style -Chisel also supports printf in a style similar to [Scala's String Interpolation](http://docs.scala-lang.org/overviews/core/string-interpolation.html). Chisel provides a custom string interpolator `cf` which follows C-style format specifiers (see section [C-style](#c-style) below). Here's a few examples of using the `cf` interpolator: +Chisel also supports printf in a style similar to [Scala's String Interpolation](http://docs.scala-lang.org/overviews/core/string-interpolation.html). Chisel provides a custom string interpolator `cf` which follows C-style format specifiers (see section [C-style](#c-style) below). + +Note that the Scala s-interpolator is not supported in Chisel constructs and will issue a compile-time warning: ```scala mdoc:invisible import chisel3._ ``` + +```scala mdoc:warn +class MyModule extends Module { + val in = IO(Input(UInt(8.W))) + printf(s"in = $in\n") +} +``` + +Instead, use Chisel's `cf` interpolator as in the following examples: + ```scala mdoc:compile-only val myUInt = 33.U printf(cf"myUInt = $myUInt") // myUInt = 33 -- cgit v1.2.3