Chisel 数据类型总结

在编写 chisel 代码时,常常遇到各种 type bug 拖慢工作进度。这里参考官网和网上博客,做了一次系统总结,特此记录。

scala type

在硬件电路生成中,一般起到参数化设置、实例硬件模块的作用。

  1. 基础类型:Boolean,Int,BigInt
  2. 集合类型:Seq, List, Array。
    这篇详细介绍了的三者在 scala 层面上的区别 scala 集合List Array Seq详解_scala seq array list-CSDN博客

chisel type

一个 Data 的 chisel type 是一个 scala object,会它按名称及其类型(包括宽度)捕获实际存在的所有字段。

The Chisel type of a Data is a Scala object. It captures all the fields actually present, by names, and their types including widths.

  1. 基础类型:UInt(无符号数),SInt(有符号数)、Bool(布尔类型)
  2. 组合类型:Bundle(BaseChisel/Bundle)(包裹类)、Vec(向量)
  3. 修饰类型:Decoupled(ChiselData)(bits 包裹,外加 valid 和 ready),Valid(ChiselData)(bits 包裹,外加 valid)
  4. 方向类型:Input(ChiselData), Output(ChiselData), Flipped(ChiselData),用于设置方向属性或改变方向属性

注意:Decoupled, Valid, Input,Output, Flipped 只是改变 Data 属性,不会改变 Data 类型。

hardware type

hardware 是绑定到可合成硬件的 Data,这种绑定不是 chisel type 的属性。

Hardware is Data that is “bound” to synthesizable hardware. The binding is what determines the actual directionality of each field, it is not a property of the Chisel type.

A literal is a Data that is respresentable as a literal value without being wrapped in Wire, Reg, or IO.

  1. IO:IO(chiselData)ValidIO(chiselData)DecoupledIO(chiselData)ValidIO(chiselData)
  2. wire: Wire(chiselData)WireInit(hardwareData)
  3. reg: Reg(chiselData)RegInit(hardwareData)
  4. vec: VecInit(hardware)
  5. 带有字面量的数据类型,如 false.B,0.U(8.W)(不指定位宽,则位宽默认为满足数值的最大 bits,很容易导致计算 bug)

⚠️区别

  1. 编译检测区别

    1. scala 编译器可以区分 scala type 和 chisel type,所以在支持 scala 语法的编辑器中会有语法错误提示。
    2. scala 编译器无法区分 chisel type 和 hardware type,只能通过程序员经验+生成 firrtl 报错来区分。
  2. 用法区别

    1. asTypeOf 对 hardware type 和 chisel type 均有效
    2. := 左右两边都只能是 hardware
    3. chiselTypeOf(HardwareData)传参只能是 hardware
    4. xxxInit(hardware)传参只能是 hardware,返回的结果也是 hardware
    5. Wire, Reg, IO 不能传递 hardware
    6. ChiselData.Lit(...) 只能 chisel type 调用
    7. Bundle定义内部只能用 Chisel Type
    8. DataMirror.directionOf(hardwareData)传参只能是 hardware
    9. DataMirror.specifiedDirectionOf(hardwareData/chiselData) 可以既传入 hardware,也可以传入 chisel type
  3. xxxTypeOf 的区别

    1. .asInstanceOf scala 运行时类型转换,告诉编译器需要强制转换的类信息
    2. .asTypeOf 从一个 Data 子类转换到另一个 Data 子类。通常用于将数据分配给全 0。也可以用于将一个 chisel type 转换为另一个 chisel type(不推荐,因为过程没有 width 检查)
    3. chiselTypeOf()和上述两种不同,不是类型转换操作符,而是返回 hardware 的 chisel type

参考资料

  1. Chisel Type vs Scala Type | Chisel (chisel-lang.org)
  2. chisel数据类型和硬件类型的联系与区别(更新)_chisel type hardware type-CSDN博客
  • 26
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值