2014-09-13 03:06:41 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2012-02-26 02:09:26 +01:00
|
|
|
package rocket
|
2011-11-09 23:52:17 +01:00
|
|
|
|
|
|
|
import Chisel._
|
2014-04-08 03:22:46 +02:00
|
|
|
import uncore._
|
2013-06-13 19:31:04 +02:00
|
|
|
import scala.math._
|
2015-10-22 03:18:32 +02:00
|
|
|
import cde.{Parameters, Field}
|
2012-01-14 00:55:56 +01:00
|
|
|
|
2014-03-24 12:36:53 +01:00
|
|
|
object Util {
|
2016-05-27 03:52:08 +02:00
|
|
|
implicit def uintToBitPat(x: UInt): BitPat = BitPat(x)
|
2013-08-12 19:39:11 +02:00
|
|
|
implicit def intToUInt(x: Int): UInt = UInt(x)
|
2016-06-02 01:57:10 +02:00
|
|
|
implicit def bigIntToUInt(x: BigInt): UInt = UInt(x)
|
2012-11-26 04:46:48 +01:00
|
|
|
implicit def booleanToBool(x: Boolean): Bits = Bool(x)
|
2015-07-29 09:24:58 +02:00
|
|
|
implicit def intSeqToUIntSeq(x: Seq[Int]): Seq[UInt] = x.map(UInt(_))
|
2013-08-12 19:39:11 +02:00
|
|
|
implicit def wcToUInt(c: WideCounter): UInt = c.value
|
2016-07-15 06:42:12 +02:00
|
|
|
|
|
|
|
implicit class UIntToAugmentedUInt(val x: UInt) extends AnyVal {
|
2016-03-31 07:48:51 +02:00
|
|
|
def sextTo(n: Int): UInt =
|
|
|
|
if (x.getWidth == n) x
|
|
|
|
else Cat(Fill(n - x.getWidth, x(x.getWidth-1)), x)
|
2016-07-15 06:42:12 +02:00
|
|
|
|
|
|
|
def extract(hi: Int, lo: Int): UInt = {
|
|
|
|
if (hi == lo-1) UInt(0)
|
|
|
|
else x(hi, lo)
|
|
|
|
}
|
2015-05-19 03:23:58 +02:00
|
|
|
}
|
2014-03-24 12:36:53 +01:00
|
|
|
|
2014-05-26 08:59:24 +02:00
|
|
|
implicit def booleanToIntConv(x: Boolean) = new AnyRef {
|
|
|
|
def toInt: Int = if (x) 1 else 0
|
|
|
|
}
|
2015-11-18 03:14:30 +01:00
|
|
|
|
2016-07-06 10:31:56 +02:00
|
|
|
implicit class SeqToAugmentedSeq[T <: Data](val x: Seq[T]) extends AnyVal {
|
|
|
|
def apply(idx: UInt): T = {
|
|
|
|
if (x.size == 1) {
|
|
|
|
x.head
|
|
|
|
} else {
|
|
|
|
val half = 1 << (log2Ceil(x.size) - 1)
|
|
|
|
val newIdx = idx & (half - 1)
|
|
|
|
Mux(idx >= UInt(half), x.drop(half)(newIdx), x.take(half)(newIdx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:13:52 +02:00
|
|
|
def asUInt(): UInt = Cat(x.map(_.asUInt).reverse)
|
2016-07-06 10:31:56 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 00:18:39 +02:00
|
|
|
implicit class UIntIsOneOf(val x: UInt) extends AnyVal {
|
|
|
|
def isOneOf(s: Seq[UInt]): Bool = s.map(x === _).reduce(_||_)
|
|
|
|
|
|
|
|
def isOneOf(u1: UInt, u2: UInt*): Bool = isOneOf(u1 +: u2.toSeq)
|
|
|
|
}
|
2012-11-17 15:48:44 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 13:22:04 +01:00
|
|
|
import Util._
|
|
|
|
|
2013-06-13 19:31:04 +02:00
|
|
|
object Str
|
|
|
|
{
|
2013-12-10 04:52:47 +01:00
|
|
|
def apply(s: String): UInt = {
|
2013-06-13 19:31:04 +02:00
|
|
|
var i = BigInt(0)
|
|
|
|
require(s.forall(validChar _))
|
|
|
|
for (c <- s)
|
|
|
|
i = (i << 8) | c
|
2013-12-10 04:52:47 +01:00
|
|
|
UInt(i, s.length*8)
|
2013-06-13 19:31:04 +02:00
|
|
|
}
|
2013-12-10 04:52:47 +01:00
|
|
|
def apply(x: Char): UInt = {
|
2013-06-13 19:31:04 +02:00
|
|
|
require(validChar(x))
|
2013-12-10 04:52:47 +01:00
|
|
|
UInt(x.toInt, 8)
|
2013-06-13 19:31:04 +02:00
|
|
|
}
|
2013-12-10 04:52:47 +01:00
|
|
|
def apply(x: UInt): UInt = apply(x, 10)
|
|
|
|
def apply(x: UInt, radix: Int): UInt = {
|
2013-08-12 19:39:11 +02:00
|
|
|
val rad = UInt(radix)
|
2013-06-13 19:31:04 +02:00
|
|
|
val w = x.getWidth
|
|
|
|
require(w > 0)
|
|
|
|
|
|
|
|
var q = x
|
2013-08-24 06:16:28 +02:00
|
|
|
var s = digit(q % rad)
|
2013-06-13 19:31:04 +02:00
|
|
|
for (i <- 1 until ceil(log(2)/log(radix)*w).toInt) {
|
|
|
|
q = q / rad
|
2013-08-24 06:16:28 +02:00
|
|
|
s = Cat(Mux(Bool(radix == 10) && q === UInt(0), Str(' '), digit(q % rad)), s)
|
2013-06-13 19:31:04 +02:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2013-12-10 04:52:47 +01:00
|
|
|
def apply(x: SInt): UInt = apply(x, 10)
|
|
|
|
def apply(x: SInt, radix: Int): UInt = {
|
2013-08-12 19:39:11 +02:00
|
|
|
val neg = x < SInt(0)
|
2013-06-15 09:45:53 +02:00
|
|
|
val abs = x.abs
|
2013-06-13 19:31:04 +02:00
|
|
|
if (radix != 10) {
|
|
|
|
Cat(Mux(neg, Str('-'), Str(' ')), Str(abs, radix))
|
|
|
|
} else {
|
2013-08-12 19:39:11 +02:00
|
|
|
val rad = UInt(radix)
|
2013-06-13 19:31:04 +02:00
|
|
|
val w = abs.getWidth
|
|
|
|
require(w > 0)
|
|
|
|
|
|
|
|
var q = abs
|
2013-08-24 06:16:28 +02:00
|
|
|
var s = digit(q % rad)
|
2013-06-13 19:31:04 +02:00
|
|
|
var needSign = neg
|
|
|
|
for (i <- 1 until ceil(log(2)/log(radix)*w).toInt) {
|
|
|
|
q = q / rad
|
2013-08-12 19:39:11 +02:00
|
|
|
val placeSpace = q === UInt(0)
|
2013-06-13 19:31:04 +02:00
|
|
|
val space = Mux(needSign, Str('-'), Str(' '))
|
|
|
|
needSign = needSign && !placeSpace
|
2013-08-24 06:16:28 +02:00
|
|
|
s = Cat(Mux(placeSpace, space, digit(q % rad)), s)
|
2013-06-13 19:31:04 +02:00
|
|
|
}
|
|
|
|
Cat(Mux(needSign, Str('-'), Str(' ')), s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 04:52:47 +01:00
|
|
|
private def digit(d: UInt): UInt = Mux(d < UInt(10), Str('0')+d, Str(('a'-10).toChar)+d)(7,0)
|
2013-06-13 19:31:04 +02:00
|
|
|
private def validChar(x: Char) = x == (x & 0xFF)
|
|
|
|
}
|
|
|
|
|
2013-01-06 14:18:33 +01:00
|
|
|
object Split
|
|
|
|
{
|
|
|
|
// is there a better way to do do this?
|
|
|
|
def apply(x: Bits, n0: Int) = {
|
|
|
|
val w = checkWidth(x, n0)
|
|
|
|
(x(w-1,n0), x(n0-1,0))
|
|
|
|
}
|
|
|
|
def apply(x: Bits, n1: Int, n0: Int) = {
|
|
|
|
val w = checkWidth(x, n1, n0)
|
|
|
|
(x(w-1,n1), x(n1-1,n0), x(n0-1,0))
|
|
|
|
}
|
|
|
|
def apply(x: Bits, n2: Int, n1: Int, n0: Int) = {
|
|
|
|
val w = checkWidth(x, n2, n1, n0)
|
|
|
|
(x(w-1,n2), x(n2-1,n1), x(n1-1,n0), x(n0-1,0))
|
|
|
|
}
|
|
|
|
|
|
|
|
private def checkWidth(x: Bits, n: Int*) = {
|
|
|
|
val w = x.getWidth
|
|
|
|
def decreasing(x: Seq[Int]): Boolean =
|
|
|
|
if (x.tail.isEmpty) true
|
2015-05-19 03:23:17 +02:00
|
|
|
else x.head >= x.tail.head && decreasing(x.tail)
|
2013-01-06 14:18:33 +01:00
|
|
|
require(decreasing(w :: n.toList))
|
|
|
|
w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:48:44 +01:00
|
|
|
// a counter that clock gates most of its MSBs using the LSB carry-out
|
2014-01-25 01:36:36 +01:00
|
|
|
case class WideCounter(width: Int, inc: UInt = UInt(1))
|
2012-11-17 15:48:44 +01:00
|
|
|
{
|
2014-01-25 01:36:36 +01:00
|
|
|
private val isWide = width > 2*inc.getWidth
|
|
|
|
private val smallWidth = if (isWide) inc.getWidth max log2Up(width) else width
|
2013-08-16 00:28:15 +02:00
|
|
|
private val small = Reg(init=UInt(0, smallWidth))
|
2016-07-15 09:51:01 +02:00
|
|
|
private val nextSmall = small +& inc
|
|
|
|
small := nextSmall
|
2012-11-17 15:48:44 +01:00
|
|
|
|
|
|
|
private val large = if (isWide) {
|
2013-08-16 00:28:15 +02:00
|
|
|
val r = Reg(init=UInt(0, width - smallWidth))
|
2016-07-15 09:51:01 +02:00
|
|
|
when (nextSmall(smallWidth)) { r := r + UInt(1) }
|
2012-11-17 15:48:44 +01:00
|
|
|
r
|
|
|
|
} else null
|
|
|
|
|
2016-07-15 09:51:01 +02:00
|
|
|
val value = if (isWide) Cat(large, small) else small
|
2012-11-17 15:48:44 +01:00
|
|
|
|
2013-08-12 19:39:11 +02:00
|
|
|
def := (x: UInt) = {
|
2016-07-15 09:51:01 +02:00
|
|
|
small := x
|
|
|
|
if (isWide) large := x >> smallWidth
|
2012-11-17 15:48:44 +01:00
|
|
|
}
|
2011-12-09 09:42:43 +01:00
|
|
|
}
|
2013-08-25 02:33:11 +02:00
|
|
|
|
|
|
|
object Random
|
|
|
|
{
|
2014-03-25 13:22:04 +01:00
|
|
|
def apply(mod: Int, random: UInt): UInt = {
|
|
|
|
if (isPow2(mod)) random(log2Up(mod)-1,0)
|
|
|
|
else PriorityEncoder(partition(apply(1 << log2Up(mod*8), random), mod))
|
|
|
|
}
|
|
|
|
def apply(mod: Int): UInt = apply(mod, randomizer)
|
|
|
|
def oneHot(mod: Int, random: UInt): UInt = {
|
|
|
|
if (isPow2(mod)) UIntToOH(random(log2Up(mod)-1,0))
|
2016-08-01 02:13:52 +02:00
|
|
|
else PriorityEncoderOH(partition(apply(1 << log2Up(mod*8), random), mod)).asUInt
|
2013-08-25 02:33:11 +02:00
|
|
|
}
|
2014-03-25 13:22:04 +01:00
|
|
|
def oneHot(mod: Int): UInt = oneHot(mod, randomizer)
|
|
|
|
|
|
|
|
private def randomizer = LFSR16()
|
|
|
|
private def round(x: Double): Int =
|
|
|
|
if (x.toInt.toDouble == x) x.toInt else (x.toInt + 1) & -2
|
|
|
|
private def partition(value: UInt, slices: Int) =
|
2016-04-27 09:28:39 +02:00
|
|
|
Seq.tabulate(slices)(i => value < round((i << value.getWidth).toDouble / slices))
|
2013-08-25 02:33:11 +02:00
|
|
|
}
|