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

如何测试不属于Module且生成硬件结构的Chisel对象函数

Chisel 非Module内硬件生成函数的测试方案

Chisel 中所有硬件节点(包括Wire、Reg、IO等)的生成必须处于合法的 Module 构建上下文内,你要测试的getSquare函数内部直接生成了Wire类型节点,脱离 Module 直接调用会触发上下文缺失报错。

测试思路

无需修改原有 Util 工具对象的代码,只需要额外实现一个最小化的测试专用 Module,将目标函数的调用、参数构造、结果导出逻辑都封装在该 Module 内,再基于这个封装 Module 编写标准 Chisel 测试用例即可。

完整实现代码

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

// 原有Util工具对象无需修改
object Util {
  def getSquare(vec2d: Vec[Vec[Bool]]) : Seq[Seq[Bool]] = {
    val size = vec2d.size max vec2d(0).size
    Seq.fill(size, size) {Wire(Bool())}
  }
}

// 测试专用封装Module,所有硬件生成逻辑都包裹在此处
class GetSquareTestModule(inputRows: Int, inputCols: Int) extends Module {
  // 构造函数入参:二维Vec输入
  val inputVec = IO(Input(Vec(inputRows, Vec(inputCols, Bool()))))
  // 调用待测试的目标函数
  val outputSquare = Util.getSquare(inputVec)
  // 导出结果到IO端口方便测试
  val outputSize = inputRows max inputCols
  val outputVec = IO(Output(Vec(outputSize, Vec(outputSize, Bool()))))
  
  // 信号连接(如果需要验证赋值逻辑可自定义此处规则)
  for (i <- 0 until outputSize; j <- 0 until outputSize) {
    outputVec(i)(j) := outputSquare(i)(j)
    // 示例:输入范围内的信号透传,超出范围默认赋值false
    if (i < inputRows && j < inputCols) {
      outputSquare(i)(j) := inputVec(i)(j)
    } else {
      outputSquare(i)(j) := false.B
    }
  }
}

// 标准Chisel测试用例
class UtilTest extends AnyFlatSpec with ChiselScalatestTester {
  "getSquare" should "generate square matrix with correct size" in {
    // 测试用例1:输入2行3列,预期输出3x3矩阵
    test(new GetSquareTestModule(2, 3)) { dut =>
      // 验证输出维度符合预期
      assert(dut.outputVec.length == 3)
      assert(dut.outputVec.head.length == 3)
      // 验证信号逻辑正确性
      dut.inputVec(0)(0).poke(true.B)
      dut.inputVec(1)(2).poke(false.B)
      dut.outputVec(0)(0).expect(true.B)
      dut.outputVec(2)(2).expect(false.B)
    }

    // 测试用例2:输入4行2列,预期输出4x4矩阵
    test(new GetSquareTestModule(4, 2)) { dut =>
      assert(dut.outputVec.length == 4)
      assert(dut.outputVec.head.length == 4)
    }
  }
}

测试逻辑调整说明

  • 如果仅需要验证函数生成的硬件结构尺寸是否正确,可省略端口赋值和信号验证逻辑,仅断言输出Vec的维度符合预期即可
  • 如果需要验证函数内部的自定义信号连接逻辑,可在封装 Module 中补充对应的赋值规则,再通过测试端口的poke/expect接口验证功能正确性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 14:09:00