DecoupledDataSource.scala 1.5 KB
Newer Older
1
package chisel.miscutils
Jens Korinth's avatar
Jens Korinth committed
2
3
import  chisel3._
import  chisel3.util._
Jens Korinth's avatar
Jens Korinth committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

/**
 * Interface for DecoupledDataSource.
 **/
class DecoupledDataSourceIO[T <: Data](gen: T) extends Bundle {
  val out = Decoupled(gen.cloneType)
}

/**
 * Data source providing fixed data via Decoupled interface.
 * Provides the data given via Decoupled handshakes; if repeat
 * is true, data is wrapped around.
 * @param gen Type.
 * @param size Total number of elements.
 * @param data Function providing data for each index
 * @param repeat If true, will always have data via wrap-around,
                 otherwise valid will go low after data was
		 consumed.
 **/
class DecoupledDataSource[T <: Data](gen: T, val size : Int, data: (Int) => T, val repeat: Boolean = true) extends Module {
24
25

  println ("DecoupledDataSource: size = %d, repeat = %s".format(size, if (repeat) "true" else "false"))
Jens Korinth's avatar
Jens Korinth committed
26
  println("  width = %d".format(log2Ceil(if (repeat) size else size + 1)))
27
28


Jens Korinth's avatar
Jens Korinth committed
29
  val ds  = for (i <- 0 until size) yield data(i) // evaluate data to array
Jens Korinth's avatar
Jens Korinth committed
30
31
  val io  = IO(new DecoupledDataSourceIO(gen)) // interface
  val i   = Reg(UInt(log2Ceil(if (repeat) size else size + 1).W)) // index
Jens Korinth's avatar
Jens Korinth committed
32
33
  val rom = Vec.tabulate(size)(n => ds(n)) // ROM with data
  io.out.bits  := rom(i) // current index data
Jens Korinth's avatar
Jens Korinth committed
34
  io.out.valid := !reset && i < size.U // valid until exceeded
Jens Korinth's avatar
Jens Korinth committed
35
  when (reset) {
Jens Korinth's avatar
Jens Korinth committed
36
    i := 0.U
Jens Korinth's avatar
Jens Korinth committed
37
38
39
  }
  .otherwise {
    if (repeat)
Jens Korinth's avatar
Jens Korinth committed
40
      when (io.out.ready && io.out.valid) { i := i + 1.U }
Jens Korinth's avatar
Jens Korinth committed
41
    else
Jens Korinth's avatar
Jens Korinth committed
42
      when (io.out.ready && io.out.valid && i < size.U) { i := i + 1.U }
Jens Korinth's avatar
Jens Korinth committed
43
44
  }
}