Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decodeAs API to output decoded signals directly to a Bundle #2328

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/scala/chisel3/util/experimental/decode/decoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,41 @@ object decoder extends LazyLogging {
)
)
}

object decodeAs {
/** Decode signals using a [[Bundle]] as the output
*
* @param b the output bundle to be used as the decoded signal
* @param input input signal that contains decode table input
* @param truthTable [[TruthTable]] to decode user input.
* @return bundle with the decode output.
*
* @note The bundle width must match the TruthTable width.
* @example
* {{{
* class OutputBundle extends Bundle {
* val s1 = UInt(2.W)
* val s2 = UInt(2.W)
* val s3 = Bool()
* }
* val selector = "b001".U
* val signals = decodeAs(
* (new OutputBundle),
* selector,
* TruthTable(
* Array(
* BitPat("b001") -> BitPat(1.U(2.W)) ## BitPat(1.U(2.W)) ## BitPat.Y(),
* BitPat("b?11") -> BitPat(2.U(2.W)) ## BitPat(2.U(2.W)) ## BitPat.N(),
* ), BitPat(0.U(2.W)) ## BitPat(0.U(2.W)) ## BitPat.dontCare(1) // Default values
* )
* )
* val a = signals.s1 // Should be 1.U(2.W)
* val b = signals.s2 // Should be 1.U(2.W)
* val c = signals.s3 // Should be true.B
* }}}
*/
def apply[T <: Bundle](b: T, input: UInt, truthTable: TruthTable): T =
decoder(input, truthTable).asTypeOf(b)
def apply[T <: Bundle](minimizer: Minimizer, b: T, input: UInt, truthTable: TruthTable): T =
decoder(minimizer, input, truthTable).asTypeOf(b)
}
52 changes: 52 additions & 0 deletions src/test/scala/chiselTests/util/experimental/DecoderSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package chiselTests.util.experimental

import chisel3._
import chiselTests._
import chisel3.testers.BasicTester
import chisel3.stage.ChiselStage
import chisel3.util.BitPat
import chisel3.util.experimental.decode.{TruthTable, decodeAs}
// import org.scalatest.flatspec.AnyFlatSpec
class OutputBundle extends Bundle {
val s1 = UInt(2.W)
val s2 = UInt(2.W)
val s3 = Bool()
}
class DecodeAs extends Module {
val io = IO(new Bundle {
val i = Input(UInt(3.W))
val o = Output(new OutputBundle())
})

val signals = decodeAs(
(new OutputBundle),
io.i,
TruthTable(
Array(
BitPat("b001") -> BitPat(1.U(2.W)) ## BitPat(2.U(2.W)) ## BitPat.Y(),
BitPat("b?11") -> BitPat(2.U(2.W)) ## BitPat(3.U(2.W)) ## BitPat.Y(),
), BitPat(0.U(2.W)) ## BitPat(0.U(2.W)) ## BitPat.N() // Default values
)
)
io.o := signals
}

class DecodeAsTester(i: String, o1: Int, o2: Int, o3: Boolean) extends BasicTester {
val dut = Module(new DecodeAs())
dut.io.i := i.U

assert(dut.io.o.s1 === o1.U)
assert(dut.io.o.s2 === o2.U)
assert(dut.io.o.s3 === o3.B)
stop()
}

class DecoderSpec extends ChiselPropSpec {
property("decoder should decodeAs to an existing bundle") {
assertTesterPasses{ new DecodeAsTester("b001", 1, 2, true) }
}

property("decoder should decodeAs to an existing bundle with default values") {
assertTesterPasses{ new DecodeAsTester("b101", 0, 0, false) }
}
}