From ad2d71bc013bb65549377250e434442201b57d25 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 22 May 2024 18:20:55 +0300 Subject: [PATCH] Base -> DataResource, Resource -> Base --- .../hrzn/chryse/platform/BoardResources.scala | 6 +-- .../hrzn/chryse/platform/resource/Base.scala | 49 ++++++++----------- .../chryse/platform/resource/Button.scala | 2 +- .../platform/resource/ClockSource.scala | 2 +- .../chryse/platform/resource/Connector.scala | 5 +- .../platform/resource/DataResource.scala | 37 ++++++++++++++ .../hrzn/chryse/platform/resource/InOut.scala | 8 +-- .../hrzn/chryse/platform/resource/LED.scala | 2 +- .../chryse/platform/resource/Resource.scala | 30 ------------ .../platform/resource/SinglePinResource.scala | 2 +- .../hrzn/chryse/platform/resource/UART.scala | 8 +-- .../chryse/platform/resource/implicits.scala | 10 ++-- 12 files changed, 81 insertions(+), 80 deletions(-) create mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/DataResource.scala delete mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala diff --git a/src/main/scala/ee/hrzn/chryse/platform/BoardResources.scala b/src/main/scala/ee/hrzn/chryse/platform/BoardResources.scala index 0bc5b30..ed20100 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/BoardResources.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/BoardResources.scala @@ -1,8 +1,8 @@ package ee.hrzn.chryse.platform import chisel3._ +import ee.hrzn.chryse.platform.resource.DataResource import ee.hrzn.chryse.platform.resource.Base -import ee.hrzn.chryse.platform.resource.Resource import scala.collection.mutable.ArrayBuffer @@ -11,7 +11,7 @@ abstract class BoardResources { for { f <- this.getClass().getDeclaredFields() } { f.setAccessible(true) f.get(this) match { - case res: Resource => + case res: Base => res.setName(f.getName()) case _ => } @@ -19,5 +19,5 @@ abstract class BoardResources { val clock: resource.ClockSource - def all: Seq[Base[_ <: Data]] = Resource.allFromBoardResources(this) + def all: Seq[DataResource[_ <: Data]] = Base.allFromBoardResources(this) } diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Base.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Base.scala index b6d5053..56e6b74 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/Base.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/Base.scala @@ -1,37 +1,30 @@ package ee.hrzn.chryse.platform.resource import chisel3._ +import ee.hrzn.chryse.platform.BoardResources -abstract class Base[HW <: Data](gen: => HW) extends SinglePinResource { - final private[chryse] var pinId: Option[Pin] = None - final var name: Option[String] = None +import scala.collection.mutable.ArrayBuffer - // Should return Chisel datatype with Input/Output attached. - private[chryse] def makeIo(): HW = gen - - final private[chryse] var ioInst: Option[InstSides[HW]] = None +// XXX: This is more of a resource holder/container. +// It's one or possibly many (or no?) resources. Hrm. +trait Base { + def setName(name: String): Unit + def bases(): Seq[DataResource[_ <: Data]] +} - /* Instantiate an IO in the module at the point of connecting to this - * resource. These will be connected to in turn by the platform toplevel - * (which implies they can only be used in the user toplevel). */ - private[chryse] def ioInstOrMake(): InstSides[HW] = { - ioInst match { - case Some(r) => r - case None => - val r = IO(makeIo()).suggestName(s"${name.get}_int") - ioInst = Some(InstSides(r, r)) - ioInst.get +object Base { + def allFromBoardResources[T <: BoardResources]( + br: T, + ): Seq[DataResource[_ <: Data]] = { + var out = ArrayBuffer[DataResource[_ <: Data]]() + for { f <- br.getClass().getDeclaredFields().iterator } { + f.setAccessible(true) + f.get(br) match { + case res: Base => + out.appendAll(res.bases()) + case _ => + } } + out.toSeq } - - def setName(name: String): Unit = this.name = Some(name) - - def onPin(id: Pin): this.type = { - pinId = Some(id) - this - } - - def bases(): Seq[Base[_ <: Data]] = Seq(this) } - -case class InstSides[HW](user: HW, top: HW) diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Button.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Button.scala index c7c3882..a8fbba4 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/Button.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/Button.scala @@ -2,7 +2,7 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -class Button extends Base[Bool](Input(Bool())) { +class Button extends DataResource[Bool](Input(Bool())) { private var invert = false // TODO: invert possibly belongs in a higher class def inverted: this.type = { diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/ClockSource.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/ClockSource.scala index cf5ca6f..2ec473a 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/ClockSource.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/ClockSource.scala @@ -2,7 +2,7 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -case class ClockSource(hz: Int) extends Base[Clock](Input(Clock())) {} +case class ClockSource(hz: Int) extends DataResource[Clock](Input(Clock())) {} object ClockSource { def apply(hz: Int) = new ClockSource(hz) diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Connector.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Connector.scala index 6503402..bb79169 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/Connector.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/Connector.scala @@ -5,7 +5,7 @@ import chisel3._ class Connector[Ix, E <: SinglePinResource]( gen: => E, private val ixToPin: (Ix, Pin)*, -) extends Resource { +) extends Base { private val mappings: Map[Ix, E] = ixToPin .map { case (i, p) => i -> gen.onPin(p) @@ -17,7 +17,8 @@ class Connector[Ix, E <: SinglePinResource]( def setName(name: String): Unit = mappings.foreach { case (i, e) => e.setName(s"$name$i") } - def bases(): Seq[Base[_ <: Data]] = mappings.flatMap(_._2.bases()).toSeq + def bases(): Seq[DataResource[_ <: Data]] = + mappings.flatMap(_._2.bases()).toSeq } object Connector { diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/DataResource.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/DataResource.scala new file mode 100644 index 0000000..e7880f3 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/DataResource.scala @@ -0,0 +1,37 @@ +package ee.hrzn.chryse.platform.resource + +import chisel3._ + +abstract class DataResource[HW <: Data](gen: => HW) extends SinglePinResource { + final private[chryse] var pinId: Option[Pin] = None + final var name: Option[String] = None + + // Should return Chisel datatype with Input/Output attached. + private[chryse] def makeIo(): HW = gen + + final private[chryse] var ioInst: Option[InstSides[HW]] = None + + /* Instantiate an IO in the module at the point of connecting to this + * resource. These will be connected to in turn by the platform toplevel + * (which implies they can only be used in the user toplevel). */ + private[chryse] def ioInstOrMake(): InstSides[HW] = { + ioInst match { + case Some(r) => r + case None => + val r = IO(makeIo()).suggestName(s"${name.get}_int") + ioInst = Some(InstSides(r, r)) + ioInst.get + } + } + + def setName(name: String): Unit = this.name = Some(name) + + def onPin(id: Pin): this.type = { + pinId = Some(id) + this + } + + def bases(): Seq[DataResource[_ <: Data]] = Seq(this) +} + +case class InstSides[HW](user: HW, top: HW) diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/InOut.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/InOut.scala index fe9223c..4ef2e61 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/InOut.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/InOut.scala @@ -2,9 +2,9 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -class InOut extends Resource with SinglePinResource { - val i = new Base[Bool](Input(Bool())) {} - val o = new Base[Bool](Output(Bool())) {} +class InOut extends Base with SinglePinResource { + val i = new DataResource[Bool](Input(Bool())) {} + val o = new DataResource[Bool](Output(Bool())) {} def setName(name: String): Unit = { i.setName(s"$name") @@ -17,7 +17,7 @@ class InOut extends Resource with SinglePinResource { this } - def bases(): Seq[Base[_ <: Data]] = Seq(i, o) + def bases(): Seq[DataResource[_ <: Data]] = Seq(i, o) } object InOut { diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala index 702c4c0..7991c49 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala @@ -2,7 +2,7 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -class LED extends Base[Bool](Output(Bool())) { +class LED extends DataResource[Bool](Output(Bool())) { private var invert = false // TODO: invert possibly belongs in a higher class def inverted: this.type = { diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala deleted file mode 100644 index 4c98444..0000000 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala +++ /dev/null @@ -1,30 +0,0 @@ -package ee.hrzn.chryse.platform.resource - -import chisel3._ -import ee.hrzn.chryse.platform.BoardResources - -import scala.collection.mutable.ArrayBuffer - -// XXX: This is more of a resource holder/container. -// It's one or possibly many (or no?) resources. Hrm. -trait Resource { - def setName(name: String): Unit - def bases(): Seq[Base[_ <: Data]] -} - -object Resource { - def allFromBoardResources[T <: BoardResources]( - br: T, - ): Seq[Base[_ <: Data]] = { - var out = ArrayBuffer[Base[_ <: Data]]() - for { f <- br.getClass().getDeclaredFields().iterator } { - f.setAccessible(true) - f.get(br) match { - case res: Resource => - out.appendAll(res.bases()) - case _ => - } - } - out.toSeq - } -} diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/SinglePinResource.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/SinglePinResource.scala index 0b24c4f..9a1c514 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/SinglePinResource.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/SinglePinResource.scala @@ -1,5 +1,5 @@ package ee.hrzn.chryse.platform.resource -trait SinglePinResource extends Resource { +trait SinglePinResource extends Base { def onPin(id: Pin): this.type } diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala index f840309..191ae7e 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala @@ -2,9 +2,9 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -class UART extends Resource { - val rx = new Base[Bool](Input(Bool())) {} - val tx = new Base[Bool](Output(Bool())) {} +class UART extends Base { + val rx = new DataResource[Bool](Input(Bool())) {} + val tx = new DataResource[Bool](Output(Bool())) {} def setName(name: String): Unit = { rx.setName(s"${name}_rx") @@ -17,7 +17,7 @@ class UART extends Resource { this } - def bases(): Seq[Base[_ <: Data]] = Seq(rx, tx) + def bases(): Seq[DataResource[_ <: Data]] = Seq(rx, tx) } object UART { diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/implicits.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/implicits.scala index eccf4ef..55b2c91 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/implicits.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/implicits.scala @@ -9,18 +9,18 @@ object implicits { // Note that the DataView doesn't really need or care about the generated // data's direction or lack thereof. - implicit def BaseProduct[HW <: Data]: DataProduct[Base[HW]] = - new DataProduct[Base[HW]] { + implicit def BaseProduct[HW <: Data]: DataProduct[DataResource[HW]] = + new DataProduct[DataResource[HW]] { def dataIterator( - res: Base[HW], + res: DataResource[HW], path: String, ): Iterator[(Data, String)] = Seq(res.ioInst.get.user -> path).iterator } - implicit def viewBool: DataView[Base[Bool], Bool] = + implicit def viewBool: DataView[DataResource[Bool], Bool] = DataView(res => Bool(), _.ioInstOrMake().user -> _) - implicit def base2Bool(res: Base[Bool]): Bool = + implicit def base2Bool(res: DataResource[Bool]): Bool = res.viewAs[Bool] }