如何基于Chisel 6.4.0生成Verilog而非SystemVerilog?
在Chisel 6.4.0中生成Verilog的方法
问题描述
Chisel 6默认生成SystemVerilog,原Chisel 3中的getVerilogString()方法在6.4.0版本中仍输出SystemVerilog,需要找到在Chisel 6.4.0中生成传统Verilog的方案。
现有代码与依赖
GCD.scala代码
package gcd import chisel3._ class GCD extends Module { val io = IO(new Bundle { val value1 = Input(UInt(16.W)) val value2 = Input(UInt(16.W)) val loadingValues = Input(Bool()) val outputGCD = Output(UInt(16.W)) val outputValid = Output(Bool()) }) val x = Reg(UInt()) val y = Reg(UInt()) when(x > y) { x := x - y }.otherwise { y := y - x } when(io.loadingValues) { x := io.value1 y := io.value2 } io.outputGCD := x io.outputValid := y === 0.U } object Elaborate extends App { val firtoolOptions = Array("--lowering-options=" + List( "disallowLocalVariables", "disallowPackedArrays", "locationInfoStyle=wrapInAtSquareBracket" ).reduce(_ + "," + _)) circt.stage.ChiselStage.emitSystemVerilogFile(new gcd.GCD(), args, firtoolOptions) }
当前依赖版本
ivy"org.chipsalliance::chisel:6.4.0" ivy"org.chipsalliance:::chisel-plugin:6.4.0" ivy"edu.berkeley.cs::chiseltest:6.0.0"
解决方案
在Chisel 6中,生成传统Verilog的核心是给firtool传递--disable-system-verilog参数,强制输出符合IEEE 1364标准的Verilog。以下是两种实现方式:
1. 生成Verilog文件
修改Elaborate对象中的firtool参数,添加--disable-system-verilog,并使用emitVerilogFile方法:
object Elaborate extends App { val firtoolOptions = Array( "--disable-system-verilog", "--lowering-options=" + List( "disallowLocalVariables", "disallowPackedArrays", "locationInfoStyle=wrapInAtSquareBracket" ).reduce(_ + "," + _) ) circt.stage.ChiselStage.emitVerilogFile(new gcd.GCD(), args, firtoolOptions) }
2. 生成Verilog字符串
如果需要在代码中直接获取Verilog字符串而非写入文件,使用emitVerilogString方法并传入参数:
object Elaborate extends App { val firtoolOptions = Array( "--disable-system-verilog", "--lowering-options=" + List( "disallowLocalVariables", "disallowPackedArrays", "locationInfoStyle=wrapInAtSquareBracket" ).reduce(_ + "," + _) ) val verilogString = circt.stage.ChiselStage.emitVerilogString(new gcd.GCD(), firtoolOptions) println(verilogString) }
关键说明
--disable-system-verilog是firtool的独立参数,不属于lowering-options,需要单独添加到参数数组中。- 原
getVerilogString()方法在Chisel 6中未提供直接切换到Verilog的接口,因此推荐使用circt.stage.ChiselStage的emitVerilogFile或emitVerilogString方法,通过firtool参数控制输出格式。
内容的提问来源于stack exchange,提问作者boat
相关产品推荐
相关产品推荐

