summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/experimental
diff options
context:
space:
mode:
authorJiuyang Liu2021-05-23 07:55:48 +0000
committerJiuyang Liu2021-06-16 10:32:04 +0800
commit28eef17430d8bbca2765b5a2b0ab0337f7484840 (patch)
tree6acd0d76f4405fde3169de01dcef64792d657a9e /src/main/scala/chisel3/util/experimental
parent1c9163bb05ff7e50885d1560a9df088ff1f2b49d (diff)
TruthTable can merge same inputs now.
Diffstat (limited to 'src/main/scala/chisel3/util/experimental')
-rw-r--r--src/main/scala/chisel3/util/experimental/decode/TruthTable.scala27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/main/scala/chisel3/util/experimental/decode/TruthTable.scala b/src/main/scala/chisel3/util/experimental/decode/TruthTable.scala
index 084403ac..37bfec77 100644
--- a/src/main/scala/chisel3/util/experimental/decode/TruthTable.scala
+++ b/src/main/scala/chisel3/util/experimental/decode/TruthTable.scala
@@ -4,9 +4,7 @@ package chisel3.util.experimental.decode
import chisel3.util.BitPat
-class TruthTable(val table: Map[BitPat, BitPat], val default: BitPat) {
- require(table.map(_._1.getWidth).toSet.size == 1, "input width not equal.")
- require(table.map(_._2.getWidth).toSet.size == 1, "output width not equal.")
+sealed class TruthTable(val table: Map[BitPat, BitPat], val default: BitPat) {
def inputWidth = table.head._1.getWidth
@@ -38,12 +36,31 @@ object TruthTable {
.filter(_.contains("->"))
.map(_.split("->").map(str => BitPat(s"b$str")))
.map(bps => bps(0) -> bps(1))
- .toMap,
+ .toSeq,
BitPat(s"b${tableString.split("\n").filterNot(_.contains("->")).head.replace(" ", "")}")
)
}
- def apply(table: Map[BitPat, BitPat], default: BitPat) = new TruthTable(table, default)
+ /** Convert a table and default output into a [[TruthTable]]. */
+ def apply(table: Iterable[(BitPat, BitPat)], default: BitPat) = {
+ require(table.map(_._1.getWidth).toSet.size == 1, "input width not equal.")
+ require(table.map(_._2.getWidth).toSet.size == 1, "output width not equal.")
+ val outputWidth = table.map(_._2.getWidth).head
+ new TruthTable(table.toSeq.groupBy(_._1).map { case (key, values) =>
+ // merge same input inputs.
+ key -> BitPat(s"b${
+ Seq.tabulate(outputWidth) { i =>
+ val outputSet = values.map(_._2)
+ .map(bpStr)
+ .map(_ (i))
+ .toSet
+ .filterNot(_ == '?')
+ require(outputSet.size != 2, s"TruthTable conflict in :\n${values.map { case (i, o) => s"${bpStr(i)}->${bpStr(o)}" }.mkString("\n")}")
+ outputSet.headOption.getOrElse('?')
+ }.mkString
+ }")
+ }, default)
+ }
/** consume 1 table, split it into up to 3 tables with the same default bits.