【问题标题】:Chisel variable Declaration Syntax Meaning rvs: Bool*Chisel 变量声明语法含义 rvs: Bool*
【发布时间】:2020-02-22 09:39:58
【问题描述】:

查看对象 DecoupledHelper 和类 DecoupledHelper 代码,我看到以下内容。

object DecoupledHelper {
 def apply(rvs: Bool*) = new DecoupledHelper(rvs)
}

class DecoupledHelper(val rvs: Seq[Bool]) {
 def fire(exclude: Bool, includes: Bool*) = {

   (rvs.filter(_ ne exclude) ++ includes).reduce(_ && _)
 }
}

我不明白 apply 方法中的参数声明语法。 (rvs: Bool*)。 Bool 类型末尾的* 是什么意思。 查看 DecoupledHelper 类的构造函数参数,它期望 (rvs: Seq[Bool])

这是否意味着Bool* 类型会自动转换为Seq[Bool] 类型?

此时,谁能解释一下 rvs.filter 方法在做什么?

【问题讨论】:

标签: chisel


【解决方案1】:

正如 Kamyar 所说,Type* 是用于可变数量参数(也称为“varags”)的 Scala 语法。它让你像这样调用DecoupledHelper apply 方法:

val helper = DecoupledHelper(a, b, c) // calling apply on the companion object
// instead of
val helper2 = new DecoupledHelper(Seq(a, b, c)) // calling constructor of the class

对于 Chisel 和 Scala 的新手,请注意 apply is a special function Scala 在您将括号“应用”到对象或实例时调用。因此DecoupledHelper(a, b, c) 等价于DecoupledHelper.apply(a, b, c)

此时,谁能解释一下 rvs.filter 方法在做什么?

它利用了 Chisel 的实现,我不建议这样做。 ne 是 Scala 函数,用于不引用相等。它可以让您检查两个对象是否不是内存中的同一个对象。

在这种情况下,您可以执行以下操作:

val helper = DecoupledHelper(a, b, c)
helper.fire()   // a && b && c
helper.fire(b)  // a && c

现在因为这是危险地使用引用相等而不是实际的硬件相等(并且存在促成这种情况的实现的限制),应该工作的东西不起作用:

val helper = DecoupledHelper(a, b, c)
val d = b           // Same reference
val e = WireInit(b) // Equivalent Wire but different reference
helper.fire(d)  // a && c
helper.fire(e)  // a && b && c

db 指向堆上的同一对象,但e 不指向,尽管从硬件角度来看它是等效的。

有关更多信息,请参阅相关的错误报告和讨论:https://github.com/chipsalliance/rocket-chip/issues/1616

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多