diff options
| author | Henry Cook | 2015-08-13 01:14:55 -0700 |
|---|---|---|
| committer | Henry Cook | 2015-08-13 01:14:55 -0700 |
| commit | 976ff1439f51afc08c61ff39ecf0ad2e71a88e2a (patch) | |
| tree | ce77dd94dd84fc024efc372fa444b712aea8c974 /src/main/scala/Chisel/Utils.scala | |
| parent | 85d7403f9bf7bc2b3520f924736c237f21f70ebd (diff) | |
Streamline files, breaking up Core.scala and resorting some smaller ones
Diffstat (limited to 'src/main/scala/Chisel/Utils.scala')
| -rw-r--r-- | src/main/scala/Chisel/Utils.scala | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main/scala/Chisel/Utils.scala b/src/main/scala/Chisel/Utils.scala index a7c60677..be675da5 100644 --- a/src/main/scala/Chisel/Utils.scala +++ b/src/main/scala/Chisel/Utils.scala @@ -6,6 +6,17 @@ import scala.language.experimental.macros import scala.reflect.runtime.universe._ import scala.reflect.macros.blackbox._ +object Enum { + /** create n enum values of given type */ + def apply[T <: Bits](nodeType: T, n: Int): List[T] = Range(0, n).map(x => nodeType.fromInt(x)).toList + + /** create enum values of given type and names */ + def apply[T <: Bits](nodeType: T, l: Symbol *): Map[Symbol, T] = (l.toList zip (Range(0, l.length).map(x => nodeType.fromInt(x)))).toMap + + /** create enum values of given type and names */ + def apply[T <: Bits](nodeType: T, l: List[Symbol]): Map[Symbol, T] = (l zip (Range(0, l.length).map(x => nodeType.fromInt(x)))).toMap +} + object log2Up { def apply(in: Int): Int = 1 max BigInt(in-1).bitLength } @@ -247,6 +258,36 @@ object ShiftRegister } } +object Log2 { + def apply(x: Bits, width: Int): UInt = { + if (width < 2) UInt(0) + else if (width == 2) x(1) + else Mux(x(width-1), UInt(width-1), apply(x, width-1)) + } + + def apply(x: Bits): UInt = apply(x, x.getWidth) +} + +object OHToUInt { + def apply(in: Seq[Bool]): UInt = apply(Vec(in)) + def apply(in: Vec[Bool]): UInt = apply(in.toBits, in.size) + def apply(in: Bits): UInt = apply(in, in.getWidth) + + def apply(in: Bits, width: Int): UInt = { + if (width <= 2) Log2(in, width) + else { + val mid = 1 << (log2Up(width)-1) + val hi = in(width-1, mid) + val lo = in(mid-1, 0) + Cat(hi.orR, apply(hi | lo, mid)) + } + } +} + +object PriorityEncoder { + def apply(in: Seq[Bool]): UInt = PriorityMux(in, (0 until in.size).map(UInt(_))) + def apply(in: Bits): UInt = apply(in.toBools) +} /** Returns the one hot encoding of the input UInt. */ object UIntToOH |
