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

Fix Decodetable #3563

Merged
merged 3 commits into from
Oct 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ trait DecodeField[T <: DecodePattern, D <: Data] {

def chiselType: D

def default: BitPat = dc

final def width: Int = chiselType.getWidth

def dc: BitPat = BitPat.dontCare(width)

def genTable(op: T): BitPat

require(width == default.width)
}

/**
Expand All @@ -47,9 +51,9 @@ trait BoolDecodeField[T <: DecodePattern] extends DecodeField[T, Bool] {
* Output of DecoderTable
* @param fields all fields to be decoded
*/
class DecodeBundle(fields: Seq[DecodeField[_, _]]) extends Record {
class DecodeBundle(fields: Seq[DecodeField[_, _ <: Data]]) extends Record {
require(fields.map(_.name).distinct.size == fields.size, "Field names must be unique")
val elements: SeqMap[String, Data] = SeqMap(fields.map(k => k.name -> UInt(k.width.W)): _*)
val elements: SeqMap[String, Data] = SeqMap(fields.map(k => k.name -> k.chiselType): _*)

/**
* Get result of each field in decoding result
Expand All @@ -58,7 +62,7 @@ class DecodeBundle(fields: Seq[DecodeField[_, _]]) extends Record {
* @tparam D type of field
* @return hardware value of decoded output
*/
def apply[D <: Data](field: DecodeField[_, D]): D = elements(field.name).asTypeOf(field.chiselType)
def apply[D <: Data](field: DecodeField[_, D]): D = elements(field.name).asInstanceOf[D]
}

/**
Expand All @@ -71,15 +75,15 @@ class DecodeBundle(fields: Seq[DecodeField[_, _]]) extends Record {
* @param fields all fields as decoded output
* @tparam I concrete type of input patterns trait
*/
class DecodeTable[I <: DecodePattern](patterns: Seq[I], fields: Seq[DecodeField[I, _]]) {
class DecodeTable[I <: DecodePattern](patterns: Seq[I], fields: Seq[DecodeField[I, _ <: Data]]) {
require(patterns.map(_.bitPat.getWidth).distinct.size == 1, "All instructions must have the same width")

def bundle: DecodeBundle = new DecodeBundle(fields)

private val _table = patterns.map { op =>
op.bitPat -> fields.reverse.map(field => field.genTable(op)).reduce(_ ## _)
}
val table: TruthTable = TruthTable(_table, BitPat.dontCare(_table.head._2.getWidth))
val table: TruthTable = TruthTable(
patterns.map { op => op.bitPat -> fields.reverse.map(field => field.genTable(op)).reduce(_ ## _) },
fields.reverse.map(_.default).reduce(_ ## _)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this part of the change, this feels very different than the previous use of BitPat.dontCare.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because some time we don't use don't care as our default selection(e.g. illinstr will default be false), thus I added a default field to let user choose their own default.

)

def decode(input: UInt): DecodeBundle = chisel3.util.experimental.decode.decoder(input, table).asTypeOf(bundle)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ class DecoderTableSpec extends ChiselFlatSpec {
}
}

object legal extends BoolDecodeField[Insn] {
override def name = "legal"

// default decode to n rather than don't care, for some special elements
override def default: BitPat = n

override def genTable(i: Insn): BitPat = y
}

class ExampleALUDecoder extends Module {
val inst = IO(Input(UInt(4.W)))
val isWideOp = IO(Output(Bool()))
val isAddOp = IO(Output(Bool()))
val isLegal = IO(Output(Bool()))

val allInstructions = Seq(
Insn(Op.add, true),
Expand All @@ -54,10 +64,11 @@ class DecoderTableSpec extends ChiselFlatSpec {
Insn(Op.sub, false)
)

val decodeTable = new DecodeTable(allInstructions, Seq(IsWideOp, IsAddOp))
val decodeTable = new DecodeTable(allInstructions, Seq(IsWideOp, IsAddOp, legal))
val decodedBundle = decodeTable.decode(inst)
isWideOp := decodedBundle(IsWideOp)
isAddOp := decodedBundle(IsAddOp)
isLegal := decodedBundle(legal)
}

"DecoderTable" should "elaborate a decoder" in {
Expand Down