{"id":1147,"date":"2019-11-05T13:44:22","date_gmt":"2019-11-05T12:44:22","guid":{"rendered":"http:\/\/www.fabienm.eu\/flf\/?p=1147"},"modified":"2021-09-16T09:05:34","modified_gmt":"2021-09-16T08:05:34","slug":"chisel-tips","status":"publish","type":"post","link":"http:\/\/www.fabienm.eu\/flf\/chisel-tips\/","title":{"rendered":"Chisel Tips"},"content":{"rendered":"\n<p>This page lists some tips for Chisel that I could glean here and there.<\/p>\n\n\n\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-2c671891      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#bidirectional-signal\" class=\"uagb-toc-link__trigger\">Bidirectional signal<\/a><li class=\"uagb-toc__list\"><a href=\"#donttouch-keep-register-names-in-verilog\" class=\"uagb-toc-link__trigger\">dontTouch : keep register names in verilog<\/a><li class=\"uagb-toc__list\"><a href=\"#dontcare-output-signals\" class=\"uagb-toc-link__trigger\">DontCare output signals<\/a><li class=\"uagb-toc__list\"><a href=\"#keep-signal-and-variables-names-in-verilog\" class=\"uagb-toc-link__trigger\">Keep signal and variables names in Verilog<\/a><li class=\"uagb-toc__list\"><a href=\"#uint-to-vec\" class=\"uagb-toc-link__trigger\">UInt() to Vec()<\/a><li class=\"uagb-toc__list\"><a href=\"#split-an-uint-in-a-vec-of-sub-uint\" class=\"uagb-toc-link__trigger\">Split an UInt() in a Vec() of \u00absub\u00bb UInt()<\/a><li class=\"uagb-toc__list\"><a href=\"#initialize-vec-of-reg\" class=\"uagb-toc-link__trigger\">Initialize Vec() of Reg()<\/a><li class=\"uagb-toc__list\"><a href=\"#concatenate-value-in-one-uint\" class=\"uagb-toc-link__trigger\">Concatenate value in one UInt()<\/a><li class=\"uagb-toc__list\"><a href=\"#initialize-bundle\" class=\"uagb-toc-link__trigger\">Initialize Bundle<\/a><li class=\"uagb-toc__list\"><a href=\"#test-some-chisel-code-in-live\" class=\"uagb-toc-link__trigger\">Test some chisel code in live<\/a><li class=\"uagb-toc__list\"><a href=\"#for-more-chisel-cookbook\" class=\"uagb-toc-link__trigger\">For more Chisel Cookbook<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<h4 class=\"wp-block-heading\">Bidirectional signal<\/h4>\n\n\n\n<p>Bidirectionnal signals are not possible inside an FPGA or an ASIC. But it can be usefull on the boundary to drive signal like tristate buffer.<\/p>\n\n\n\n<p>To use Bidirectionnal signal use <a href=\"https:\/\/www.chisel-lang.org\/api\/latest\/chisel3\/experimental\/Analog.html\">Analog<\/a>() in conjonction with RawModule or BlackBox :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TopDesign extends RawModule {\n  val tristatebuf = Analog(1.W)\n...\n}<\/code><\/pre>\n\n\n\n<p>Connexion is made with bulk connector &lsquo;&lt;&gt;&rsquo; :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tristatebuf &lt;&gt; myblackbox.io.tristate<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">dontTouch : keep register names in verilog<\/h4>\n\n\n\n<p>Tip from <a href=\"https:\/\/stackoverflow.com\/questions\/55401525\/how-to-keep-all-variable-name-in-chisel-when-generate-verilog-code\">stackoverflow<\/a>.<\/p>\n\n\n\n<p>Sometimes, we have to keep register in verilog emitted code. But Chisel obtimize it and it often disapear. To keep it, use dontTouch() :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  val version = dontTouch(RegInit(1.U(8.W)))<\/code><\/pre>\n\n\n\n<p>Get following in verilog module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  reg &#91;7:0] version; \/\/ @&#91;wbgpio.scala 20:34]\n...\n    if (reset) begin\n      version &lt;= 8'h1;\n    end<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">DontCare output signals<\/h4>\n\n\n\n<p>All module output must be defined to avoid this kind of warning :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;error] (run-main-0) firrtl.passes.CheckInitialization$RefNotInitializedException:  : &#91;module ChisNesPad]  Reference io is not fully initialized.\n&#91;error]    : io.data.valid &lt;= VOID\n&#91;error] firrtl.passes.CheckInitialization$RefNotInitializedException:  : &#91;module ChisNesPad]  Reference io is not fully initialized.\n&#91;error]    : io.data.valid &lt;= VOID\n&#91;error] Nonzero exit code: 1\n&#91;error] (Compile \/ runMain) Nonzero exit code: 1\n&#91;error] Total time: 12 s, completed 10 d\u00e9c. 2019 13:25:11<\/code><\/pre>\n\n\n\n<p>But at the begining of a design, we don&rsquo;t know what to write. To avoid this error we can use <code>DontCare<\/code> object :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  io.data.valid := DontCare<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Keep signal and variables names in Verilog<\/h4>\n\n\n\n<p>See the good response from Jack Koening on <a href=\"https:\/\/stackoverflow.com\/a\/55403155\/4422957\">stackoverflow<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"UInt_Vec\"><strong>UInt() to Vec()<\/strong><\/h4>\n\n\n\n<p>An UInt() can be converted to a Vec of Bool() with <a href=\"https:\/\/github.com\/freechipsproject\/chisel3\/blob\/master\/chiselFrontend\/src\/main\/scala\/chisel3\/Bits.scala#L325\">asBools<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val foo = Vec(5, Bool())\nval bar = UInt(5.W)\n\nfoo := bar.asBools\nInitialize Vec() of Reg()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Split an UInt() in a Vec() of \u00absub\u00bb UInt()<\/h4>\n\n\n\n<p>Question asked on <a href=\"https:\/\/stackoverflow.com\/questions\/58995390\/how-to-split-an-uint-into-a-vec-of-uint-to-do-subword-extraction-and-assignmen\">stackoverflow<\/a>.<\/p>\n\n\n\n<p>If we have a 16 bits register declared like that.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val counterReg = RegInit(0.U(16.W))<\/code><\/pre>\n\n\n\n<p>And we want to do indexed dibit assignment on module output like that :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/..\n  val io = IO(new Bundle {\n     val dibit = Output(UInt(2.W))\n  })\n\/\/..\nvar indexReg = RegInit(0.U(4.W))\n\/\/..\nio.dibit = vectorizedCounter(indexReg) \/\/xxx doesn't work<\/code><\/pre>\n\n\n\n<p>We could do it like that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>io.dibit := (counterReg &gt;&gt; indexReg)(1, 0)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"vec_reg_init\">Initialize Vec() of Reg()<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> val initRegOfVec = RegInit(VecInit(Seq.fill(4)(0.U(32.W))))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Concatenate value in one UInt()<\/h4>\n\n\n\n<p>Of course we can use Cat(), but it take only 2 parameters. With &lsquo;##&rsquo; we can chain more signals:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val a = Reg(UInt(1.W))\nval b = Reg(UInt(1.W))\nval c = Reg(UInt(1.W))\nval y = Reg(UInt(3.W))\n\ny := a ## b ## c<\/code><\/pre>\n\n\n\n<p>With a Vec() declared like it :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val outputReg = RegInit(0.U((dibitCount*2).W)\nval valueVecReg = RegInit(VecInit(Seq.fill(dibitCount)(0.U(2.W))))\n\nouptutReg := valueVecReg.reduce(_ ## _)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize Bundle<\/h4>\n\n\n\n<p>Question asked on <a href=\"https:\/\/stackoverflow.com\/questions\/59049673\/how-to-initialize-a-reg-of-bundle-in-chisel\">stackoverflow<\/a>. Same question on stackoverflow <a href=\"https:\/\/stackoverflow.com\/questions\/61191769\/what-would-be-the-best-way-to-initialize-a-bundle-register-to-all-1s-in-chisel\">but with &lsquo;1&rsquo; initialization.<\/a><\/p>\n\n\n\n<p>If we have a Bundle declared like that :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class RValue (val cSize: Int = 16) extends Bundle {\n  val rvalue = Output(UInt(cSize.W))\n  val er     = Output(UInt((cSize\/2).W))\n  val part   = Output(Bool()) \/* set if value is partial *\/\n}<\/code><\/pre>\n\n\n\n<p>And we want to make a register with initialized value we can use the <a href=\"https:\/\/stackoverflow.com\/a\/59055878\/4422957\">new interface<\/a> named BundleLiterals:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import chisel3.experimental.BundleLiterals._\n...\nval valueReg = RegInit((new RValue(cSize)).Lit(\n          _.rvalue -&gt; 1.U,\n          _.er -&gt; 2.U,\n          _.part -&gt; true.B)<\/code><\/pre>\n\n\n\n<p>Or if we just want to initialize to all 0 values we can do <a href=\"https:\/\/stackoverflow.com\/a\/59058082\/4422957\">that<\/a> :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val valueReg = RegInit(0.U.asTypeOf(new RValue(cSize))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Test some chisel code in live<\/h4>\n\n\n\n<p>Question asked on <a href=\"https:\/\/stackoverflow.com\/questions\/58770374\/is-it-possible-to-test-chisel-reg-in-console\">stackoverflow<\/a>.<\/p>\n\n\n\n<p>It can be usefull to be able to test code in console before launching the big compilation. It&rsquo;s possible in directory where your project build.sbt is :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd myproject\/\n$ sbt\nsbt:myproject&gt; console\nscala&gt;<\/code><\/pre>\n\n\n\n<p>And once in the scala console chisel import can be done :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scala&gt; import chisel3._\nimport chisel3._\nscala&gt; val plop = \"b0010101010001\".U(13.W)\nplop: chisel3.UInt = UInt&lt;13&gt;(1361)\n<\/code><\/pre>\n\n\n\n<p>Type Ctrl+D to quit.<\/p>\n\n\n\n<p>For more simplicity it&rsquo;s also possible to use the <a href=\"https:\/\/scastie.scala-lang.org\/uHCX5wxgSzu6wXqa9OJdRA\">Scaties website<\/a> in live.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">For more Chisel Cookbook<\/h4>\n\n\n\n<p>See :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> the <a href=\"https:\/\/www.chisel-lang.org\/chisel3\/docs\/cookbooks\/cookbook\">official cookbook<\/a> on chisel website.<\/li><li>The chisel <a href=\"https:\/\/github.com\/freechipsproject\/chisel-cheatsheet\/releases\/latest\/download\/chisel_cheatsheet.pdf\">cheatsheet<\/a>.<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page lists some tips for Chisel that I could glean here and there. Bidirectional signal Bidirectionnal signals are not possible inside an FPGA or an ASIC. But it can be usefull on the boundary to drive signal like tristate buffer. To use Bidirectionnal signal use Analog() in conjonction with RawModule or BlackBox : Connexion &hellip; <a href=\"http:\/\/www.fabienm.eu\/flf\/chisel-tips\/\" class=\"more-link\">Continuer la lecture de <span class=\"screen-reader-text\">Chisel Tips<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[2,4,3,99],"tags":[57,88,117],"class_list":["post-1147","post","type-post","status-publish","format-standard","hentry","category-chisel","category-chisel-langages","category-langages","category-tipoftheweek","tag-chisel","tag-chisel3","tag-tip"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Fabien Marteau","author_link":"http:\/\/www.fabienm.eu\/flf\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"This page lists some tips for Chisel that I could glean here and there. Bidirectional signal Bidirectionnal signals are not possible inside an FPGA or an ASIC. But it can be usefull on the boundary to drive signal like tristate buffer. To use Bidirectionnal signal use Analog() in conjonction with RawModule or BlackBox : Connexion\u2026","_links":{"self":[{"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/posts\/1147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/comments?post=1147"}],"version-history":[{"count":27,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/posts\/1147\/revisions"}],"predecessor-version":[{"id":1832,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/posts\/1147\/revisions\/1832"}],"wp:attachment":[{"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/media?parent=1147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/categories?post=1147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fabienm.eu\/flf\/wp-json\/wp\/v2\/tags?post=1147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}