https://mp.weixin.qq.com/s/L5eAwv--WzZdr-CfW2-XNA
Chisel提供的Valid接口。如果valid为置1,则表明输出的bits有效;反之,则输出无效。
参考链接:
https://github.com/freechipsproject/chisel3/blob/master/src/main/scala/chisel3/util/Valid.scala
??
1. Valid是一组用户自定义的输入输出接口
类的声明如下:
class Valid[+T <: Data](gen: T) extends Bundle
类Valid是一个泛型类(generic class),参数类型为T。
首先,T <: Data 表明T在类型结构树(hierarchy)中为Data的子类,就是说传入的T需要是Data类的子类。
其次,+T 在Scala中成为协变(covariance), 意思是,Valid[ChildClassOfT]是Valid[T]的子类,即是Valid[T]的子类型关系,与参数类型T的子类型关系是一致的,所以叫协变。反过来就叫逆变(contravariance),还有个不变(invariance)。有兴趣可以了解一下,没兴趣略过即可。
Valid类继承自Bundle,表明自己是一个用户自定义的复合数据类型。
包含两个数据成员:
val valid = Output(Bool())
val bits = Output(gen)
很好理解,当valid置1时,bits输出有效;当valid置0时,bits输出无效。
2. object Valid提供一个创建Valid接口的工厂方法
Companion object Valid起到的作用是一个工厂方法,可以省略new的使用,直接创建一个valid对象:
val enq = Input(Valid(gen))
3. 附录
Valid.scala:
/** Wrappers for valid interfaces and associated circuit generators using them. */ package chisel3.util import chisel3._ import chisel3.core.CompileOptions import chisel3.experimental.DataMirror import chisel3.internal.naming.chiselName // can‘t use chisel3_ version because of compile order /** An Bundle containing data and a signal determining if it is valid */ class Valid[+T <: Data](gen: T) extends Bundle { val valid = Output(Bool()) val bits = Output(gen) def fire(dummy: Int = 0): Bool = valid override def cloneType: this.type = Valid(gen).asInstanceOf[this.type] } /** Adds a valid protocol to any interface */ object Valid { def apply[T <: Data](gen: T): Valid[T] = new Valid(gen) }
原文地址:https://www.cnblogs.com/wjcdx/p/10116955.html