-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branches 'qmc' and 'espresso' into decoder
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/scala/chisel3/util/experimental/decode/EspressoMinimizer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chisel3.util.experimental.decode | ||
|
||
import chisel3.util.BitPat | ||
import logger.LazyLogging | ||
|
||
object EspressoMinimizer extends Minimizer with LazyLogging { | ||
def minimize(table: TruthTable): TruthTable = | ||
TruthTable.merge(TruthTable.split(table).map{case (table, indexes) => (espresso(table), indexes)}) | ||
|
||
def espresso(table: TruthTable): TruthTable = { | ||
def writeTable(table: TruthTable): String = { | ||
def invert(string: String) = string | ||
.replace('0', 't') | ||
.replace('1', '0') | ||
.replace('t', '1') | ||
val defaultType: Char = { | ||
val t = table.default.toString.drop(7).dropRight(1).toCharArray.distinct | ||
require(t.length == 1, "Internal Error: espresso only accept unified default type.") | ||
t.head | ||
} | ||
val tableType: String = defaultType match { | ||
case '?' => "fr" | ||
case _ => "fd" | ||
} | ||
val rawTable = table | ||
.toString | ||
.split("\n") | ||
.filter(_.contains("->")) | ||
.mkString("\n") | ||
.replace("->", " ") | ||
.replace('?', '-') | ||
// invert all output, since espresso cannot handle default is on. | ||
val invertRawTable = rawTable | ||
.split("\n") | ||
.map(_.split(" ")) | ||
.map(row => s"${row(0)} ${invert(row(1))}") | ||
.mkString("\n") | ||
s""".i ${table.inputWidth} | ||
|.o ${table.outputWidth} | ||
|.type ${tableType} | ||
|""".stripMargin ++ (if (defaultType == '1') invertRawTable else rawTable) | ||
} | ||
|
||
def readTable(espressoTable: String): Map[BitPat, BitPat] = { | ||
def bitPat(espresso: String): BitPat = BitPat("b" + espresso.replace('-', '?')) | ||
|
||
espressoTable | ||
.split("\n") | ||
.filterNot(_.startsWith(".")) | ||
.map(_.split(' ')) | ||
.map(row => bitPat(row(0)) -> bitPat(row(1))) | ||
.toMap | ||
} | ||
|
||
// Since Espresso don't implements pipe, we use a temp file to do so. | ||
val input = writeTable(table) | ||
logger.trace(s"""espresso input table: | ||
|$input | ||
|""".stripMargin) | ||
val f = os.temp(input) | ||
val o = os.proc("espresso", f).call().out.chunks.mkString | ||
logger.trace(s"""espresso output table: | ||
|$o | ||
|""".stripMargin) | ||
TruthTable(readTable(o), table.default) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/scala/chiselTests/util/experimental/minimizer/EspressoSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chiselTests.util.experimental.minimizer | ||
import chisel3.util.experimental.decode.EspressoMinimizer | ||
import chisel3.util.experimental.decode.Minimizer | ||
|
||
class EspressoSpec extends MinimizerSpec { | ||
override def minimizer: Minimizer = EspressoMinimizer | ||
} |