2014-09-13 03:06:41 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2012-05-02 05:16:36 +02:00
|
|
|
package rocket
|
|
|
|
|
|
|
|
import Chisel._
|
|
|
|
|
|
|
|
object DecodeLogic
|
|
|
|
{
|
2015-07-28 11:48:49 +02:00
|
|
|
def term(lit: BitPat) =
|
|
|
|
new Term(lit.value, BigInt(2).pow(lit.getWidth)-(lit.mask+1))
|
2013-12-05 13:16:48 +01:00
|
|
|
def logic(addr: UInt, addrWidth: Int, cache: scala.collection.mutable.Map[Term,Bool], terms: Seq[Term]) = {
|
2012-05-02 05:16:36 +02:00
|
|
|
terms.map { t =>
|
2013-12-10 04:52:47 +01:00
|
|
|
cache.getOrElseUpdate(t, (if (t.mask == 0) addr else addr & Bits(BigInt(2).pow(addrWidth)-(t.mask+1), addrWidth)) === Bits(t.value, addrWidth))
|
2012-05-02 05:16:36 +02:00
|
|
|
}.foldLeft(Bool(false))(_||_)
|
|
|
|
}
|
2015-07-28 11:48:49 +02:00
|
|
|
def apply(addr: UInt, default: BitPat, mapping: Iterable[(BitPat, BitPat)]): UInt = {
|
2014-05-05 05:02:31 +02:00
|
|
|
val cache = caches.getOrElseUpdate(addr, collection.mutable.Map[Term,Bool]())
|
2013-12-05 13:16:48 +01:00
|
|
|
val dterm = term(default)
|
|
|
|
val (keys, values) = mapping.unzip
|
|
|
|
val addrWidth = keys.map(_.getWidth).max
|
|
|
|
val terms = keys.toList.map(k => term(k))
|
|
|
|
val termvalues = terms zip values.toList.map(term(_))
|
2013-08-24 06:16:28 +02:00
|
|
|
|
2013-12-05 13:16:48 +01:00
|
|
|
for (t <- keys.zip(terms).tails; if !t.isEmpty)
|
|
|
|
for (u <- t.tail)
|
|
|
|
assert(!t.head._2.intersects(u._2), "DecodeLogic: keys " + t.head + " and " + u + " overlap")
|
2012-05-02 05:16:36 +02:00
|
|
|
|
2016-08-01 02:13:52 +02:00
|
|
|
Cat((0 until default.getWidth.max(values.map(_.getWidth).max)).map({ case (i: Int) =>
|
2013-12-05 13:16:48 +01:00
|
|
|
val mint = termvalues.filter { case (k,t) => ((t.mask >> i) & 1) == 0 && ((t.value >> i) & 1) == 1 }.map(_._1)
|
|
|
|
val maxt = termvalues.filter { case (k,t) => ((t.mask >> i) & 1) == 0 && ((t.value >> i) & 1) == 0 }.map(_._1)
|
|
|
|
val dc = termvalues.filter { case (k,t) => ((t.mask >> i) & 1) == 1 }.map(_._1)
|
2013-08-24 06:16:28 +02:00
|
|
|
|
2013-12-05 13:16:48 +01:00
|
|
|
if (((dterm.mask >> i) & 1) != 0) {
|
2016-08-01 02:13:52 +02:00
|
|
|
logic(addr, addrWidth, cache, SimplifyDC(mint, maxt, addrWidth))
|
2013-12-05 13:16:48 +01:00
|
|
|
} else {
|
|
|
|
val defbit = (dterm.value.toInt >> i) & 1
|
|
|
|
val t = if (defbit == 0) mint else maxt
|
2016-08-01 02:13:52 +02:00
|
|
|
val bit = logic(addr, addrWidth, cache, Simplify(t, dc, addrWidth))
|
2013-12-05 13:16:48 +01:00
|
|
|
if (defbit == 0) bit else ~bit
|
|
|
|
}
|
2016-08-01 02:13:52 +02:00
|
|
|
}).reverse)
|
2013-12-05 13:16:48 +01:00
|
|
|
}
|
2015-07-29 09:24:58 +02:00
|
|
|
def apply(addr: UInt, default: Seq[BitPat], mappingIn: Iterable[(BitPat, Seq[BitPat])]): Seq[UInt] = {
|
2015-07-28 11:48:49 +02:00
|
|
|
val mapping = collection.mutable.ArrayBuffer.fill(default.size)(collection.mutable.ArrayBuffer[(BitPat, BitPat)]())
|
2013-12-10 04:52:47 +01:00
|
|
|
for ((key, values) <- mappingIn)
|
|
|
|
for ((value, i) <- values zipWithIndex)
|
|
|
|
mapping(i) += key -> value
|
|
|
|
for ((thisDefault, thisMapping) <- default zip mapping)
|
|
|
|
yield apply(addr, thisDefault, thisMapping)
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
2015-07-29 09:24:58 +02:00
|
|
|
def apply(addr: UInt, default: Seq[BitPat], mappingIn: List[(UInt, Seq[BitPat])]): Seq[UInt] =
|
|
|
|
apply(addr, default, mappingIn.map(m => (BitPat(m._1), m._2)).asInstanceOf[Iterable[(BitPat, Seq[BitPat])]])
|
2013-12-05 13:16:48 +01:00
|
|
|
def apply(addr: UInt, trues: Iterable[UInt], falses: Iterable[UInt]): Bool =
|
2016-08-01 02:13:52 +02:00
|
|
|
apply(addr, BitPat.dontCare(1), trues.map(BitPat(_) -> BitPat("b1")) ++ falses.map(BitPat(_) -> BitPat("b0"))).toBool
|
2014-05-05 05:02:31 +02:00
|
|
|
private val caches = collection.mutable.Map[UInt,collection.mutable.Map[Term,Bool]]()
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Term(val value: BigInt, val mask: BigInt = 0)
|
|
|
|
{
|
|
|
|
var prime = true
|
|
|
|
|
2012-11-05 09:28:25 +01:00
|
|
|
def covers(x: Term) = ((value ^ x.value) &~ mask | x.mask &~ mask) == 0
|
2012-05-02 05:16:36 +02:00
|
|
|
def intersects(x: Term) = ((value ^ x.value) &~ mask &~ x.mask) == 0
|
|
|
|
override def equals(that: Any) = that match {
|
|
|
|
case x: Term => x.value == value && x.mask == mask
|
|
|
|
case _ => false
|
|
|
|
}
|
|
|
|
override def hashCode = value.toInt
|
2012-10-16 13:51:21 +02:00
|
|
|
def < (that: Term) = value < that.value || value == that.value && mask < that.mask
|
2012-05-02 05:16:36 +02:00
|
|
|
def similar(x: Term) = {
|
|
|
|
val diff = value - x.value
|
|
|
|
mask == x.mask && value > x.value && (diff & diff-1) == 0
|
|
|
|
}
|
|
|
|
def merge(x: Term) = {
|
|
|
|
prime = false
|
|
|
|
x.prime = false
|
|
|
|
val bit = value - x.value
|
|
|
|
new Term(value &~ bit, mask | bit)
|
|
|
|
}
|
|
|
|
|
2013-08-24 06:16:28 +02:00
|
|
|
override def toString = value.toString(16) + "-" + mask.toString(16) + (if (prime) "p" else "")
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
object Simplify
|
|
|
|
{
|
2012-10-16 13:51:21 +02:00
|
|
|
def getPrimeImplicants(implicants: Seq[Term], bits: Int) = {
|
|
|
|
var prime = List[Term]()
|
2012-05-02 05:16:36 +02:00
|
|
|
implicants.foreach(_.prime = true)
|
|
|
|
val cols = (0 to bits).map(b => implicants.filter(b == _.mask.bitCount))
|
2012-10-16 13:51:21 +02:00
|
|
|
val table = cols.map(c => (0 to bits).map(b => collection.mutable.Set(c.filter(b == _.value.bitCount):_*)))
|
2012-05-02 05:16:36 +02:00
|
|
|
for (i <- 0 to bits) {
|
|
|
|
for (j <- 0 until bits-i)
|
|
|
|
table(i)(j).foreach(a => table(i+1)(j) ++= table(i)(j+1).filter(_.similar(a)).map(_.merge(a)))
|
2012-10-16 13:51:21 +02:00
|
|
|
for (r <- table(i))
|
|
|
|
for (p <- r; if p.prime)
|
|
|
|
prime = p :: prime
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
2013-07-24 05:26:17 +02:00
|
|
|
prime.sortWith(_<_)
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
def getEssentialPrimeImplicants(prime: Seq[Term], minterms: Seq[Term]): (Seq[Term],Seq[Term],Seq[Term]) = {
|
|
|
|
for (i <- 0 until prime.size) {
|
|
|
|
val icover = minterms.filter(prime(i) covers _)
|
|
|
|
for (j <- 0 until prime.size) {
|
|
|
|
val jcover = minterms.filter(prime(j) covers _)
|
|
|
|
if (icover.size > jcover.size && jcover.forall(prime(i) covers _))
|
|
|
|
return getEssentialPrimeImplicants(prime.filter(_ != prime(j)), minterms)
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val essentiallyCovered = minterms.filter(t => prime.count(_ covers t) == 1)
|
|
|
|
val essential = prime.filter(p => essentiallyCovered.exists(p covers _))
|
2012-10-16 13:51:21 +02:00
|
|
|
val nonessential = prime.filterNot(essential contains _)
|
2012-05-02 05:16:36 +02:00
|
|
|
val uncovered = minterms.filterNot(t => essential.exists(_ covers t))
|
|
|
|
if (essential.isEmpty || uncovered.isEmpty)
|
|
|
|
(essential, nonessential, uncovered)
|
|
|
|
else {
|
|
|
|
val (a, b, c) = getEssentialPrimeImplicants(nonessential, uncovered)
|
|
|
|
(essential ++ a, b, c)
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
def getCost(cover: Seq[Term], bits: Int) = cover.map(bits - _.mask.bitCount).sum
|
|
|
|
def cheaper(a: List[Term], b: List[Term], bits: Int) = {
|
|
|
|
val ca = getCost(a, bits)
|
|
|
|
val cb = getCost(b, bits)
|
|
|
|
def listLess(a: List[Term], b: List[Term]): Boolean = !b.isEmpty && (a.isEmpty || a.head < b.head || a.head == b.head && listLess(a.tail, b.tail))
|
2013-07-24 05:26:17 +02:00
|
|
|
ca < cb || ca == cb && listLess(a.sortWith(_<_), b.sortWith(_<_))
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
def getCover(implicants: Seq[Term], minterms: Seq[Term], bits: Int) = {
|
|
|
|
if (minterms.nonEmpty) {
|
|
|
|
val cover = minterms.map(m => implicants.filter(_.covers(m)).map(i => collection.mutable.Set(i)))
|
|
|
|
val all = cover.reduceLeft((c0, c1) => c0.map(a => c1.map(_ ++ a)).reduceLeft(_++_))
|
|
|
|
all.map(_.toList).reduceLeft((a, b) => if (cheaper(a, b, bits)) a else b)
|
|
|
|
} else
|
|
|
|
Seq[Term]()
|
|
|
|
}
|
|
|
|
def stringify(s: Seq[Term], bits: Int) = s.map(t => (0 until bits).map(i => if ((t.mask & (1 << i)) != 0) "x" else ((t.value >> i) & 1).toString).reduceLeft(_+_).reverse).reduceLeft(_+" + "+_)
|
2012-05-02 05:16:36 +02:00
|
|
|
|
2012-10-16 13:51:21 +02:00
|
|
|
def apply(minterms: Seq[Term], dontcares: Seq[Term], bits: Int) = {
|
2012-05-02 05:16:36 +02:00
|
|
|
val prime = getPrimeImplicants(minterms ++ dontcares, bits)
|
|
|
|
minterms.foreach(t => assert(prime.exists(_.covers(t))))
|
|
|
|
val (eprime, prime2, uncovered) = getEssentialPrimeImplicants(prime, minterms)
|
|
|
|
val cover = eprime ++ getCover(prime2, uncovered, bits)
|
|
|
|
minterms.foreach(t => assert(cover.exists(_.covers(t)))) // sanity check
|
|
|
|
cover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object SimplifyDC
|
|
|
|
{
|
2012-10-16 13:51:21 +02:00
|
|
|
def getImplicitDC(maxterms: Seq[Term], term: Term, bits: Int, above: Boolean): Term = {
|
2012-05-02 05:16:36 +02:00
|
|
|
for (i <- 0 until bits) {
|
|
|
|
var t: Term = null
|
2015-03-10 18:28:05 +01:00
|
|
|
if (above && ((term.value | term.mask) & (BigInt(1) << i)) == 0)
|
|
|
|
t = new Term(term.value | (BigInt(1) << i), term.mask)
|
|
|
|
else if (!above && (term.value & (BigInt(1) << i)) != 0)
|
|
|
|
t = new Term(term.value & ~(BigInt(1) << i), term.mask)
|
2012-05-02 05:16:36 +02:00
|
|
|
if (t != null && !maxterms.exists(_.intersects(t)))
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
null
|
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
def getPrimeImplicants(minterms: Seq[Term], maxterms: Seq[Term], bits: Int) = {
|
|
|
|
var prime = List[Term]()
|
2012-05-02 05:16:36 +02:00
|
|
|
minterms.foreach(_.prime = true)
|
|
|
|
var mint = minterms.map(t => new Term(t.value, t.mask))
|
|
|
|
val cols = (0 to bits).map(b => mint.filter(b == _.mask.bitCount))
|
2012-10-16 13:51:21 +02:00
|
|
|
val table = cols.map(c => (0 to bits).map(b => collection.mutable.Set(c.filter(b == _.value.bitCount):_*)))
|
2012-05-02 05:16:36 +02:00
|
|
|
|
|
|
|
for (i <- 0 to bits) {
|
|
|
|
for (j <- 0 until bits-i) {
|
|
|
|
table(i)(j).foreach(a => table(i+1)(j) ++= table(i)(j+1).filter(_ similar a).map(_ merge a))
|
|
|
|
}
|
|
|
|
for (j <- 0 until bits-i) {
|
|
|
|
for (a <- table(i)(j).filter(_.prime)) {
|
|
|
|
val dc = getImplicitDC(maxterms, a, bits, true)
|
|
|
|
if (dc != null)
|
|
|
|
table(i+1)(j) += dc merge a
|
|
|
|
}
|
|
|
|
for (a <- table(i)(j+1).filter(_.prime)) {
|
|
|
|
val dc = getImplicitDC(maxterms, a, bits, false)
|
|
|
|
if (dc != null)
|
|
|
|
table(i+1)(j) += a merge dc
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
for (r <- table(i))
|
|
|
|
for (p <- r; if p.prime)
|
|
|
|
prime = p :: prime
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
2013-07-24 05:26:17 +02:00
|
|
|
prime.sortWith(_<_)
|
2012-05-02 05:16:36 +02:00
|
|
|
}
|
|
|
|
|
2012-11-05 09:28:25 +01:00
|
|
|
def verify(cover: Seq[Term], minterms: Seq[Term], maxterms: Seq[Term]) = {
|
|
|
|
assert(minterms.forall(t => cover.exists(_ covers t)))
|
|
|
|
assert(maxterms.forall(t => !cover.exists(_ intersects t)))
|
|
|
|
}
|
2012-10-16 13:51:21 +02:00
|
|
|
def apply(minterms: Seq[Term], maxterms: Seq[Term], bits: Int) = {
|
2012-05-02 05:16:36 +02:00
|
|
|
val prime = getPrimeImplicants(minterms, maxterms, bits)
|
|
|
|
val (eprime, prime2, uncovered) = Simplify.getEssentialPrimeImplicants(prime, minterms)
|
|
|
|
val cover = eprime ++ Simplify.getCover(prime2, uncovered, bits)
|
2012-11-05 09:28:25 +01:00
|
|
|
verify(cover, minterms, maxterms)
|
2012-05-02 05:16:36 +02:00
|
|
|
cover
|
|
|
|
}
|
|
|
|
}
|