Skip to content

Commit

Permalink
RecordMap: Add some convenient accessor functions, make eltMap publich
Browse files Browse the repository at this point in the history
  • Loading branch information
mwachs5 committed Jun 6, 2020
1 parent 46ef1d5 commit e623579
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/scala/util/RecordMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

package freechips.rocketchip.util

import Chisel._
import chisel3.Record
import chisel3._
import scala.collection.immutable.ListMap
import chisel3.internal.requireIsChiselType
import chisel3.experimental.DataMirror.internal.chiselTypeClone

final class RecordMap[T <: Data](eltMap: ListMap[String, T])
final class RecordMap[T <: Data](val eltMap: ListMap[String, T])
extends Record {

eltMap.foreach { case (name, elt) => requireIsChiselType(elt, name) }

val elements = ListMap() ++ eltMap.mapValues(chiselTypeClone) // mapValues return value is lazy

override def cloneType: this.type = (new RecordMap(eltMap)).asInstanceOf[this.type]

def apply(x: Int) = eltMap.values.toSeq(x)
def apply(x: String) = eltMap.get(x)
def size = eltMap.size
def data = eltMap.values

// This is needed for Record, and doesn't give the actual elements
val elements = ListMap[String, T]() ++ eltMap.mapValues(chiselTypeClone(_)) // mapValues return value is lazy

override def cloneType: this.type = (new RecordMap(eltMap)).asInstanceOf[this.type]

}

Expand All @@ -27,6 +30,6 @@ object RecordMap {
def apply[T <: Data](eltMap: ListMap[String, T]) = new RecordMap(eltMap)

def apply[T <: Data](elements: (String, T)*) {
new RecordMap[T](ListMap(elements:_*))
new RecordMap[T](ListMap[String, T](elements:_*))
}
}

0 comments on commit e623579

Please sign in to comment.