Skip to content

Commit 1bc6adf

Browse files
author
Oron Port
committed
refactored resource ownership and constraints
moved resource context to core module
1 parent 8fffa3f commit 1bc6adf

File tree

19 files changed

+45
-94
lines changed

19 files changed

+45
-94
lines changed

core/src/main/scala/dfhdl/core/MutableDB.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ final class MutableDB():
311311
def ownerOption: Option[DFOwner] = stack.headOption
312312
end OwnershipContext
313313

314+
object ResourceOwnershipContext:
315+
import dfhdl.platforms.resources.ResourceOwner
316+
private var stack: List[ResourceOwner] = Nil
317+
def enter(owner: ResourceOwner): Unit =
318+
stack = owner :: stack
319+
def exit(): Unit =
320+
stack = stack.drop(1)
321+
def owner: ResourceOwner = stack.head
322+
def ownerOpt: Option[ResourceOwner] = stack.headOption
323+
314324
object GlobalTagContext:
315325
private[MutableDB] var tags: DFTags = DFTags.empty
316326
def set[CT <: DFTag: ClassTag](tag: CT): Unit = tags = tags.tag(tag)

lib/src/main/scala/dfhdl/platforms/resources/Resource.scala renamed to core/src/main/scala/dfhdl/platforms/resources/Resource.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package dfhdl.platforms.resources
22
import scala.annotation.implicitNotFound
33
import scala.collection.mutable.ListBuffer
44

5-
trait Resource(using ctx: RCtx):
6-
final val id: String = ctx.id
7-
final val owner: ResourceOwner = ctx.owner
5+
trait Resource extends ResourceContext:
6+
private val owner: ResourceOwner = dfc.mutableDB.ResourceOwnershipContext.owner
87
private val connections = ListBuffer[Resource]()
98
protected[resources] def connect(that: Resource): Unit =
109
connections += that
11-
owner.add(this)
10+
owner.addResource(this)
1211

1312
private trait ResourceLP:
1413
import Resource.CanConnect
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package dfhdl.core
1+
package dfhdl.platforms.resources
22
import dfhdl.internals.*
3+
import dfhdl.core.*
34
import scala.annotation.Annotation
45

5-
class ResourceContainer extends OnCreateEvents, HasDFC, HasClsMetaArgs:
6+
trait ResourceContext extends OnCreateEvents, HasDFC, HasClsMetaArgs:
67
final lazy val dfc: DFC = __dfc
8+
final lazy val id: String = dfc.nameOpt.get
79
protected def __dfc: DFC =
810
println("Severe error: missing DFHDL context!\nMake sure you enable the DFHDL compiler plugin.")
911
sys.exit(1)
@@ -13,4 +15,4 @@ class ResourceContainer extends OnCreateEvents, HasDFC, HasClsMetaArgs:
1315
docOpt: Option[String],
1416
annotations: List[Annotation]
1517
): Unit = {}
16-
end ResourceContainer
18+
end ResourceContext

lib/src/main/scala/dfhdl/platforms/resources/ResourceDeps.scala renamed to core/src/main/scala/dfhdl/platforms/resources/ResourceDeps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dfhdl.platforms.resources
22

3-
trait ResourceDeps(using RCtx) extends Resource:
3+
trait ResourceDeps extends Resource:
44
protected def deps: List[Resource]
55
override protected[resources] def connect(that: Resource): Unit =
66
deps.lazyZip(that.asInstanceOf[ResourceDeps].deps).foreach {

lib/src/main/scala/dfhdl/platforms/resources/ResourceGroup.scala renamed to core/src/main/scala/dfhdl/platforms/resources/ResourceGroup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dfhdl.platforms.resources
22

3-
trait ResourceGroup(using RCtx) extends Resource, ResourceOwner:
3+
trait ResourceGroup extends Resource, ResourceOwner:
44
override protected[resources] def connect(that: Resource): Unit =
55
getResources.lazyZip(that.asInstanceOf[ResourceGroup].getResources).foreach {
66
// skipping ResourceDeps in the group, since their dependencies are already connected
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dfhdl.platforms.resources
2+
import scala.collection.mutable.ListBuffer
3+
trait ResourceOwner extends ResourceContext:
4+
dfc.mutableDB.ResourceOwnershipContext.enter(this)
5+
private val resources = ListBuffer[Resource]()
6+
private val children = ListBuffer[ResourceOwner]()
7+
private[resources] def getResources: List[Resource] = resources.toList
8+
private[resources] def getChildren: List[ResourceOwner] = children.toList
9+
private[resources] def addResource(resource: Resource): Unit = resources += resource
10+
override def onCreateEnd(thisOwner: Option[This]): Unit =
11+
dfc.mutableDB.ResourceOwnershipContext.exit()
12+
dfc.mutableDB.ResourceOwnershipContext.ownerOpt.foreach(_.children += this)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package dfhdl.platforms.resources
22
import dfhdl.Encoded
3-
import dfhdl.compiler.ir.constraints
43

54
enum Button extends Encoded.Toggle:
65
case Released, Pressed
76

87
object Button:
9-
final case class Resource(
10-
activeState: Button = Button.Pressed,
11-
ioc: constraints.IO = constraints.IO(standard = constraints.IO.Standard.LVCMOS33)
12-
)(using RCtx) extends ToggleIO
8+
class Resource(val activeState: Button = Button.Pressed) extends ToggleIO
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dfhdl.platforms.resources
22

3-
trait DiffPair(using RCtx) extends ResourceDeps:
3+
trait DiffPair extends ResourceDeps:
44
val pPin: IO
55
val nPin: IO
66
protected def deps: List[Resource] = List(pPin, nPin)
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package dfhdl.platforms.resources
22
import dfhdl.platforms.resources.Resource.CanConnect
3-
import dfhdl.compiler.ir.constraints
43

54
trait IO extends Resource
65
object IO:
76
given [T <: IO, R <: IO]: CanConnect[T, R] = (resource1: T, resource2: R) =>
87
resource1.connect(resource2)
98
resource2.connect(resource1)
10-
11-
trait HasIOConstraints extends IO:
12-
val ioc: constraints.IO

lib/src/main/scala/dfhdl/platforms/resources/IOBus.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package dfhdl.platforms.resources
22
import dfhdl.internals.CTName
33
import dfhdl.compiler.ir.constraints
44

5-
final case class IOBus[T <: IO](val ios: T*)(using RCtx) extends ResourceDeps:
5+
class IOBus[T <: IO](val ios: T*) extends ResourceDeps:
66
def apply(i: Int): T = ios(i)
77
protected def deps: List[Resource] = ios.toList

0 commit comments

Comments
 (0)