1.1、简介
使用Scala function 生成硬件的方式与VHDL/Verilog有很大不同:
- 您可以在其中实例化寄存器、组合逻辑和组件。
- 您不必使用限制信号赋值范围的
process/@always
块。 - 一切都是通过引用传递,这使得操作变得容易。
例如,您可以将总线作为参数传递给函数,然后该函数可以在内部读取/写入总线。 您还可以从Scala返回Component、Bus或任何其他内容。
1.2、RGB to gray
例如,如果您想使用系数将红/绿/蓝色转换为灰度,则可以使用函数来应用它们:
// Input RGB color
val r, g, b = UInt(8 bits)
// Define a function to multiply a UInt by a Scala Float value.
def coef(value: UInt, by: Float): UInt = (value * U((255 * by).toInt, 8 bits) >> 8)
// Calculate the gray level
val gray = coef(r, 0.3f) + coef(g, 0.4f) + coef(b, 0.3f)
1.3、Valid Ready Payload 总线
例如,如果您定义了一个带有Valid Ready Payload的简单总线,那么您可以在其中定义一些有用的函数。
case class MyBus(payloadWidth: Int) extends Bundle with IMasterSlave {
val valid = Bool()
val ready = Bool()
val payload = Bits(payloadWidth bits)
// Define the direction of the data in a master mode
override def asMaster(): Unit = {
out(valid, payload)
in(ready)
}
// Connect that to this
def <<(that: MyBus): Unit = {
this.valid := that.valid
that.ready := this.ready
this.payload := that.payload
}
// Connect this to the FIFO input, return the fifo output
def queue(size: Int): MyBus = {
val fifo = new MyBusFifo(payloadWidth, size)
fifo.io.push << this
return fifo.io.pop
}
}
class MyBusFifo(payloadWidth: Int, depth: Int) extends Component {
val io = new Bundle {
val push = slave(MyBus(payloadWidth))
val pop = master(MyBus(payloadWidth))
}
val mem = Mem(Bits(payloadWidth bits), depth)
// ...
}