diff --git a/src/main/scala/util/RecordMap.scala b/src/main/scala/util/RecordMap.scala new file mode 100644 index 00000000000..d081e7d5e4b --- /dev/null +++ b/src/main/scala/util/RecordMap.scala @@ -0,0 +1,34 @@ +// See LICENSE.SiFive for license details. + +package freechips.rocketchip.util + +import chisel3._ +import scala.collection.immutable.ListMap +import chisel3.internal.requireIsChiselType +import chisel3.experimental.DataMirror.internal.chiselTypeClone + +final class RecordMap[T <: Data] private (eltMap: ListMap[String, T]) + extends Record { + + eltMap.foreach { case (name, elt) => requireIsChiselType(elt, name) } + + // This is needed for Record + val elements = ListMap[String, T]() ++ eltMap.mapValues(chiselTypeClone(_).asInstanceOf[T]) // mapValues return value is lazy + + def apply(x: Int) = elements.values.toSeq(x) + def apply(x: String) = elements.get(x) + def size = elements.size + def data = elements.values + + override def cloneType: this.type = (new RecordMap(eltMap)).asInstanceOf[this.type] + +} + +object RecordMap { + + def apply[T <: Data](eltMap: ListMap[String, T]) = new RecordMap(eltMap) + + def apply[T <: Data](elements: (String, T)*): RecordMap[T] = { + new RecordMap[T](ListMap[String, T](elements:_*)) + } +}