RGB信号转灰度信号

让我们假设需要一个将RGB颜色转换为灰度并将其写入外部存储器的组件。

io名称

方向

描述

clear

in

将所有内部寄存器清零

r,g,b

in

颜色输入

wr

out

写存储器

address

out

存储器地址,每个周期增加

data

out

存储器数据,灰度等级

case class RgbToGray() extends Component {
  val io = new Bundle {
    val clear = in Bool()
    val r,g,b = in UInt(8 bits)

    val wr = out Bool()
    val address = out UInt(16 bits)
    val data = out UInt(8 bits)
  }

  def scaled(value : UInt,by : Float): UInt = value * U((255*by).toInt,8 bits) >> 8

  val gray = RegNext(
    scaled(io.r,0.3f) +
      scaled(io.g,0.4f) +
      scaled(io.b,0.3f)
  )

  val address = CounterFreeRun(stateCount = 1 << 16)
  io.address := address
  io.wr := True
  io.data := gray

  when(io.clear) {
    gray := 0
    address.clear()
    io.wr := False
  }
}