Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Base -> DataResource, Resource -> Base
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed May 22, 2024
1 parent 42f72af commit ad2d71b
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 80 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/ee/hrzn/chryse/platform/BoardResources.scala
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -11,13 +11,13 @@ 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 _ =>
}
}

val clock: resource.ClockSource

def all: Seq[Base[_ <: Data]] = Resource.allFromBoardResources(this)
def all: Seq[DataResource[_ <: Data]] = Base.allFromBoardResources(this)
}
49 changes: 21 additions & 28 deletions src/main/scala/ee/hrzn/chryse/platform/resource/Base.scala
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions src/main/scala/ee/hrzn/chryse/platform/resource/DataResource.scala
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions src/main/scala/ee/hrzn/chryse/platform/resource/InOut.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
30 changes: 0 additions & 30 deletions src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package ee.hrzn.chryse.platform.resource

trait SinglePinResource extends Resource {
trait SinglePinResource extends Base {
def onPin(id: Pin): this.type
}
8 changes: 4 additions & 4 deletions src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/ee/hrzn/chryse/platform/resource/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

0 comments on commit ad2d71b

Please sign in to comment.