diff options
| author | Jim Lawson | 2016-07-19 13:43:48 -0700 |
|---|---|---|
| committer | Jim Lawson | 2016-07-19 13:43:48 -0700 |
| commit | b27f29902d9f1d886e8edf1fc5e960cf9a634184 (patch) | |
| tree | c6f3e27e46e5ed9c3cc62f2c368c766cdded74c6 /src/main/scala/chisel3/util/Valid.scala | |
| parent | 083610b2faa456dfccc4365dd115565d36e522fa (diff) | |
| parent | 12810b5efe6a8f872fbc1c63cdfb835ca354624f (diff) | |
Merge branch 'sdtwigg_rebase_renamechisel3' into sdtwigg_wrap_renamechisel3
Diffstat (limited to 'src/main/scala/chisel3/util/Valid.scala')
| -rw-r--r-- | src/main/scala/chisel3/util/Valid.scala | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/scala/chisel3/util/Valid.scala b/src/main/scala/chisel3/util/Valid.scala new file mode 100644 index 00000000..4078a76a --- /dev/null +++ b/src/main/scala/chisel3/util/Valid.scala @@ -0,0 +1,59 @@ +// See LICENSE for license details. + +/** Wrappers for valid interfaces and associated circuit generators using them. + */ + +package chisel3.util + +import chisel3._ + +/** 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.cloneType) + 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) +} + +/** A hardware module that delays data coming down the pipeline + by the number of cycles set by the latency parameter. Functionality + is similar to ShiftRegister but this exposes a Pipe interface. + + Example usage: + val pipe = new Pipe(UInt()) + pipe.io.enq <> produce.io.out + consumer.io.in <> pipe.io.deq + */ +object Pipe +{ + def apply[T <: Data](enqValid: Bool, enqBits: T, latency: Int): Valid[T] = { + if (latency == 0) { + val out = Wire(Valid(enqBits)) + out.valid <> enqValid + out.bits <> enqBits + out + } else { + val v = Reg(Bool(), next=enqValid, init=Bool(false)) + val b = RegEnable(enqBits, enqValid) + apply(v, b, latency-1) + } + } + def apply[T <: Data](enqValid: Bool, enqBits: T): Valid[T] = apply(enqValid, enqBits, 1) + def apply[T <: Data](enq: Valid[T], latency: Int = 1): Valid[T] = apply(enq.valid, enq.bits, latency) +} + +class Pipe[T <: Data](gen: T, latency: Int = 1) extends Module +{ + val io = IO(new Bundle { + val enq = Input(Valid(gen)) + val deq = Output(Valid(gen)) + }) + + io.deq <> Pipe(io.enq, latency) +} |
