SpinalHDL之结构(七)

发布于:2024-10-12 ⋅ 阅读:(12) ⋅ 点赞:(0)

本文作为SpinalHDL学习笔记第六十七篇,介绍SpinalHDL的保留名称(Preserving names)

目录:

1.简介(Introduction)

2.可命名的基础类(Nameable base class)

3.从Scala中提取名字(Name extraction from Scala)

4.模块中的区域(Area in a Component)

5.函数中的区域(Area in a function)

6.在函数中组合(Composite in a function)

7.组合链(Composite chains)

8.Bundle函数中的组合(Composite in a Bundle's function)

9.处理未命名信号(Unamed signal handling)

⼀、简介(Introduction)

本文会介绍SpinalHDL如何把名字从scala代码传递到产⽣的硬件中。知道这些能帮助你更好地了解如何保留名字, 以尽可能增加⽣成的⽹表的可读性。

⼆、可命名的基础类(Nameable base class)

在SpinalHDL中所有可以被命名的东⻄都扩展了可命名的基类。
所以实际上, 以下的类拓展了可命名特点:
◆模块(Component)
◆区域(Area)
◆数据(UInt, SInt, Bundle, ...)

这⾥有⼀些可命名的API的例⼦:

class MyComponent extends Component {
val a, b, c, d = Bool()
b.setName("rawrr") //强制命名
c.setName("rawrr", weak = true) //提出⼀个弱名字, 如果有更强的名字已经被使⽤则不会更改名
字
d.setCompositeName(b, postfix = "wuff") //强制命名成b.getname()+"_wuff"
}

上述代码会⽣成:

module Mycomponent ();
wire a;
wire rawrr;
wire c;
wire rawrr_wuff;
endmodule

Note:⼀般来说, 除非你出于debug或精细化的⽬的, 你不必⼀定要⽤这个API来修改名字。

三、从Scala中提取名字(Name extraction from Scala)

⾸先, ⾃从v.1.4.0, 在类的建立期间, 当每次新的val被定义的时候, SpinalHDL⽤的Scala编译器插件可以提供⼀个返回值。
以下这个例⼦或多或少地展⽰了SpinalHDL是如何实现的:

//spinal.idslplugin.ValCallback是Scala编译器插件的特征, ⽤来产⽣callback
class Component extends spinal.idslplugin.ValCallback {override def valCallback[T](ref: T, name: String) : T = {
println(s"Got $ref named $name") //这⾥我们把我们得到的打印出来作为demo
ref
}
}
class UInt
class Bits
class MyComponent extends Component {
val two = 2
val wuff = "miaou"
val toto = new UInt
val rawrr = new Bits
}
object Debug3 extends App {
new MyComponent()
//这会打印出:
// Got 2 named two
// Got miaou named wuff
// Got spinal.tester