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

如何在新版Chisel中生成VCD文件?

新版Chisel(svsim替代chiseltest)生成VCD文件的方法

问题背景

之前使用chiseltest时,通过withAnnotations(Seq(WriteVcdAnnotation))可以生成VCD文件,示例代码如下:

import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec

class MyModuleSpec extends AnyFlatSpec with ChiselScalatestTester {
  behavior of "MyModule"
  it should "do something" in {
    test(new MyModule).withAnnotations(Seq(WriteVcdAnnotation)) { c =>
      c.io.in.poke(0.U)
      c.clock.step()
      c.io.out.expect(0.U)
      c.io.in.poke(42.U)
      c.clock.step()
      c.io.out.expect(42.U)
      println("Last output value : " + c.io.out.peek().litValue)
    }
  }
}

适配新版Chisel(使用svsim替代chiseltest)后,修改测试代码时发现simulate函数无法使用.withAnnotations链式调用,代码报错,示例代码如下:

import chisel3._
import chisel3.experimental.BundleLiterals._
import chisel3.simulator.EphemeralSimulator._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers

class MyModuleSpec extends AnyFreeSpec with Matchers{

  "My module do something" in {
    simulate(new MyModule).withAnnotations(Seq(WriteVcdAnnotation)) { c =>
      c.io.in.poke(0.U)
      c.clock.step()
      c.io.out.expect(0.U)
      c.io.in.poke(42.U)
      c.clock.step()
      c.io.out.expect(42.U)
      println("Last output value : " + c.io.out.peek().litValue)
    }
  }
}

需要了解新版Chisel中生成VCD文件的替代方法。

解决方案

在新版Chisel(基于svsim的模拟器API)中,生成VCD文件需要调整注解的传递方式,以下是两种可行方案:

1. 直接在simulate函数中传入注解参数

新版simulate函数支持直接接收annotations参数,无需链式调用.withAnnotations,修改后的代码如下:

import chisel3._
import chisel3.experimental.BundleLiterals._
import chisel3.simulator.EphemeralSimulator._
import chisel3.simulator.WriteVcdAnnotation // 注意导入路径
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers

class MyModuleSpec extends AnyFreeSpec with Matchers {

  "My module do something" in {
    simulate(new MyModule, annotations = Seq(WriteVcdAnnotation)) { c =>
      c.io.in.poke(0.U)
      c.clock.step()
      c.io.out.expect(0.U)
      c.io.in.poke(42.U)
      c.clock.step()
      c.io.out.expect(42.U)
      println("Last output value : " + c.io.out.peek().litValue)
    }
  }
}

2. 通过模拟器配置传递注解

如果需要配置更多模拟器参数,可以通过SimulatorConfiguration封装注解后传入:

import chisel3._
import chisel3.experimental.BundleLiterals._
import chisel3.simulator.{EphemeralSimulator, SimulatorConfiguration, WriteVcdAnnotation}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers

class MyModuleSpec extends AnyFreeSpec with Matchers {

  "My module do something" in {
    val simConfig = SimulatorConfiguration(annotations = Seq(WriteVcdAnnotation))
    EphemeralSimulator.simulate(new MyModule, simConfig) { c =>
      c.io.in.poke(0.U)
      c.clock.step()
      c.io.out.expect(0.U)
      c.io.in.poke(42.U)
      c.clock.step()
      c.io.out.expect(42.U)
      println("Last output value : " + c.io.out.peek().litValue)
    }
  }
}

关键注意点

  • 确保导入正确的WriteVcdAnnotation:路径为chisel3.simulator.WriteVcdAnnotation,而非旧chiseltest中的路径。
  • 生成的VCD文件默认存放在项目的./test_run_dir/下对应测试类的目录中,默认文件名为waveform.vcd。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:03:35