You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Chisel的BlackBox时,如何避免生成文件末尾出现Verilog文件名?

问题:Chisel生成Verilog时内嵌BlackBox的文件名出现在输出末尾如何避免?

我在Chisel 6.2.0版本中使用带有setInline函数的BlackBox实现复位生成模块,代码示例如下:

class ResetGenEna(delayClk: Int) extends BlackBox with HasBlackBoxInline {
  val io = IO(new Bundle {
    val clk = Input(Clock())
    val ena = Input(Bool())
    val rst = Output(Bool())
  })

  val cptWidth = log2Ceil(delayClk)
  val cptMsb = cptWidth - 1
  setInline("ResetGenEna.v",
    f"""
    |module ResetGenEna(
    |    input clk,
    |    input ena,
    |    output rst
    |);
    |
    |reg reset_reg = 0;
    |reg [$cptMsb%d:0] reset_dly_cnt = 0;
    |always@(posedge clk, negedge ena) begin
    |  if(!ena)
    |  begin
    |    reset_dly_cnt <= $cptWidth%d'd0;
    |    reset_reg <= 0;
    |  end
    |  else
    |  begin
    |    if(reset_dly_cnt < $cptWidth%d'd$delayClk%d)
    |      reset_dly_cnt <= reset_dly_cnt + 1'b1;
    |    else
    |      reset_reg <= 1;
    |  end
    |end
    |
    |assign rst = !reset_reg;
    |endmodule
    """.stripMargin)
}

执行Verilog生成代码时:

class SNesPadLed(val mainClockFreq: Int = 100,
                 val clockFreq: Int = 1) extends RawModule {
//...
  val rstgen = Module(new ResetGenEna(/* 传入对应参数 */))
//...
//...
}

object SNesPadLed extends App {
  println("Generating snespadled verilog")
  val verilog_src = ChiselStage.emitSystemVerilogFile(
      new SNesPadLed(),
      firtoolOpts = Array("-disable-all-randomization",
                          "-strip-debug-info"))
}

生成的SNesPadLed.sv文件末尾会出现ResetGenEna.v文件名,请问是否可以避免该文件名出现在生成文件的末尾?

解决方案

  • 修改setInline的文件名参数为空字符串:
    问题根源是setInline传入的文件名会被Firtool作为注释添加到生成代码末尾。将第一个参数改为空字符串"",即可避免生成该注释。修改后的BlackBox代码如下:
class ResetGenEna(delayClk: Int) extends BlackBox with HasBlackBoxInline {
  val io = IO(new Bundle {
    val clk = Input(Clock())
    val ena = Input(Bool())
    val rst = Output(Bool())
  })

  val cptWidth = log2Ceil(delayClk)
  val cptMsb = cptWidth - 1
  // 将文件名参数改为空字符串
  setInline("",
    f"""
    |module ResetGenEna(
    |    input clk,
    |    input ena,
    |    output rst
    |);
    |
    |reg reset_reg = 0;
    |reg [$cptMsb%d:0] reset_dly_cnt = 0;
    |always@(posedge clk, negedge ena) begin
    |  if(!ena)
    |  begin
    |    reset_dly_cnt <= $cptWidth%d'd0;
    |    reset_reg <= 0;
    |  end
    |  else
    |  begin
    |    if(reset_dly_cnt < $cptWidth%d'd$delayClk%d)
    |      reset_dly_cnt <= reset_dly_cnt + 1'b1;
    |    else
    |      reset_reg <= 1;
    |  end
    |end
    |
    |assign rst = !reset_reg;
    |endmodule
    """.stripMargin)
}
  • 验证修改效果:
    重新运行Verilog生成代码后,输出的SNesPadLed.sv文件末尾将不再出现ResetGenEna.v的文件名注释。

内容的提问来源于stack exchange,提问作者FabienM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:56:10