Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create RecordMap for better diplomatic I/O naming #2486

Merged
merged 17 commits into from
Jun 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/scala/util/RecordMap.scala
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def apply[T <: Data](eltMap: ListMap[String, T]) = new RecordMap(eltMap)
def apply[T <: Data](eltMap: ListMap[String, T]): RecordMap[T] = new RecordMap(eltMap)


def apply[T <: Data](elements: (String, T)*): RecordMap[T] = {
new RecordMap[T](ListMap[String, T](elements:_*))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the type parameters needed here? Does the following now just work?

new RecordMap(ListMap(elements:_*))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably just works. will try

}
}