Skip to content

Commit f5ef519

Browse files
author
Oron Port
committed
add DFC to all resource connections
1 parent e6050e9 commit f5ef519

File tree

10 files changed

+47
-37
lines changed

10 files changed

+47
-37
lines changed

core/src/main/scala/dfhdl/platforms/devices/Bank.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dfhdl.platforms.devices
22
import dfhdl.platforms.resources.*
3-
import Resource.CanConnect
43

54
abstract class Bank extends ResourceGroup:
65
type This <: Bank

core/src/main/scala/dfhdl/platforms/resources/ClkResource.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ trait ClkResource extends IO:
99
val rate: Rate
1010
injectConstraint(Timing.Clock(rate))
1111
object ClkResource:
12-
given [R <: ClkResource, C <: (Clk <> VAL)](using DFC): CanConnect[R, C] = (r, c) => r.connect(c)
13-
given [R <: ClkResource, DC <: core.RTDomainContainer](using dfc: DFC): CanConnect[R, DC] =
14-
(clkResource, domainContainer) =>
12+
given [R <: ClkResource, C <: (Clk <> VAL)](using DFC): CanConnect[R, C] with
13+
def connect(resource: R, dfVal: C)(using DFC): Unit = resource.connect(dfVal)
14+
given [R <: ClkResource, DC <: core.RTDomainContainer](using dfc: DFC): CanConnect[R, DC] with
15+
def connect(clkResource: R, domainContainer: DC)(using DFC): Unit =
1516
dfc.mutableDB.ResourceOwnershipContext.connectDomainOwner(
1617
domainContainer.containedOwner.asIR.asInstanceOf[DFDomainOwner],
1718
clkResource

core/src/main/scala/dfhdl/platforms/resources/IO.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ trait IO extends Resource:
1919
end connect
2020
end IO
2121
object IO:
22-
given [T <: IO, R <: IO]: CanConnect[T, R] = (resource1: T, resource2: R) =>
23-
resource1.connectFrom(resource2)
24-
resource2.connectFrom(resource1)
22+
given [T <: IO, R <: IO]: CanConnect[T, R] with
23+
def connect(resource1: T, resource2: R)(using DFC): Unit =
24+
resource1.connectFrom(resource2)
25+
resource2.connectFrom(resource1)
2526
given [R <: IO, V <: DFValOf[DFBoolOrBit]](using
2627
dfc: DFC
27-
): CanConnect[R, V] =
28-
(resource: R, dfVal: V) => resource.connect(dfVal)
28+
): CanConnect[R, V] with
29+
def connect(resource: R, dfVal: V)(using DFC): Unit =
30+
resource.connect(dfVal)

core/src/main/scala/dfhdl/platforms/resources/Resource.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ end Resource
6161
private trait ResourceLP:
6262
import Resource.CanConnect
6363
// connection is commutative, so can connect T to R if can connect R to T
64-
given [T <: Resource, R <: Resource](using cc: CanConnect[R, T]): CanConnect[T, R] =
65-
(resource1: T, resource2: R) => cc.connect(resource2, resource1)
64+
given [T <: Resource, R <: Resource](using cc: CanConnect[R, T]): CanConnect[T, R] with
65+
def connect(resource1: T, resource2: R)(using DFC): Unit = cc.connect(resource2, resource1)
6666

6767
object Resource extends ResourceLP:
6868
@implicitNotFound("Cannot connect the resource ${R} with ${T}")
6969
trait CanConnect[R <: Resource, T]:
70-
def connect(resource1: R, resource2: T): Unit
71-
given [R <: Resource, T <: Resource](using R =:= T): CanConnect[R, T] =
72-
(resource1: R, resource2: T) =>
70+
def connect(resource1: R, resource2: T)(using DFC): Unit
71+
given [R <: Resource, T <: Resource](using R =:= T): CanConnect[R, T] with
72+
def connect(resource1: R, resource2: T)(using DFC): Unit =
7373
resource1.connectFrom(resource2)
7474
resource2.connectFrom(resource1)
7575

7676
object Ops:
7777
extension [R <: Resource](resource: R)
78-
def <>[T <: Resource](that: T)(using cc: CanConnect[R, T]): Unit =
78+
def <>[T <: Resource](that: T)(using dfc: DFC, cc: CanConnect[R, T]): Unit =
7979
cc.connect(resource, that)
8080
def <>[T <: DFValAny | RTDomainContainer](that: T)(using
8181
dfc: DFC,
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package dfhdl.platforms.pmods
22
import dfhdl.platforms.Board
33
import dfhdl.platforms.resources.*
4+
import dfhdl.DFC
45
import Resource.CanConnect
56

67
trait PmodBoard extends Board, Resource:
78
protected val pmodConn: PmodConn.Male
89
object PmodBoard:
910
given [B <: PmodBoard, R <: Resource](
1011
using CanConnect[PmodConn.Male, R]
11-
): CanConnect[B, R] = (b, r) => b.pmodConn <> r
12+
): CanConnect[B, R] with
13+
def connect(board: B, resource: R)(using DFC): Unit = board.pmodConn <> resource
1214

1315
trait PmodDualBoard extends Board, Resource:
1416
protected val pmodDualConn: PmodDualConn.Male
1517
object PmodDualBoard:
1618
given [B <: PmodDualBoard, R <: Resource](using
1719
CanConnect[PmodDualConn.Male, R]
18-
): CanConnect[B, R] = (b, r) => b.pmodDualConn <> r
20+
): CanConnect[B, R] with
21+
def connect(board: B, resource: R)(using DFC): Unit = board.pmodDualConn <> resource

lib/src/main/scala/dfhdl/platforms/pmods/PmodConn.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ object PmodDualConn:
2424
class Female(val pm1: PmodConn.Female, val pm2: PmodConn.Female) extends ResourceDeps:
2525
lazy val upstreamDeps: List[Resource] = List(pm1, pm2)
2626

27-
given [M <: Male, F <: Female]: CanConnect[M, F] = (m, f) =>
28-
m.pm1 <> f.pm1
29-
m.pm2 <> f.pm2
27+
given [M <: Male, F <: Female]: CanConnect[M, F] with
28+
def connect(male: M, female: F)(using DFC): Unit =
29+
male.pm1 <> female.pm1
30+
male.pm2 <> female.pm2
3031
end PmodDualConn

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ object Connector:
4949
T <: Type,
5050
CM <: Connector[T, Form.Male.type],
5151
CF <: Connector[T, Form.Female.type]
52-
]: CanConnect[CM, CF] =
53-
(male, female) =>
52+
]: CanConnect[CM, CF] with
53+
def connect(male: CM, female: CF)(using DFC): Unit =
5454
for (i <- 1 to male.`type`.pinCount)
5555
male.P(male.`type`.pinMap(i)) <> female.P(female.`type`.pinMap(i))
5656
given femaleMale[
5757
T <: Type,
5858
CM <: Connector[T, Form.Male.type],
5959
CF <: Connector[T, Form.Female.type]
60-
]: CanConnect[CF, CM] =
61-
(male, female) =>
60+
]: CanConnect[CF, CM] with
61+
def connect(female: CF, male: CM)(using DFC): Unit =
6262
for (i <- 1 to male.`type`.pinCount)
6363
male.P(male.`type`.pinMap(i)) <> female.P(female.`type`.pinMap(i))
6464
end Connector

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ object IOBus:
4343
") is different than the DFHDL value width (" + TW + ")."
4444
]
4545
given [RT <: IO, RL <: Int, R <: IOBus[RT, RL], T <: DFTypeAny, V <: DFValOf[T]](using
46-
dfc: DFC,
4746
wT: Width[T]
4847
)(using
4948
check: `RW == TW`.Check[RL, wT.OutI],
5049
cc: CanConnect[RT, DFValOf[DFBit]]
51-
): CanConnect[R, V] = (resource: R, dfVal: V) =>
52-
import dfhdl.core.DFVal.Ops.apply
53-
check(resource.length, dfVal.widthInt)
54-
for (i <- 0 until resource.ios.length)
55-
cc.connect(resource.ios(i), dfVal.bits(i))
50+
): CanConnect[R, V] with
51+
def connect(resource: R, dfVal: V)(using DFC): Unit =
52+
import dfhdl.core.DFVal.Ops.apply
53+
check(resource.length, dfVal.widthInt)
54+
for (i <- 0 until resource.ios.length)
55+
cc.connect(resource.ios(i), dfVal.bits(i))
5656
end IOBus

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import dfhdl.compiler.ir.constraints
33
import dfhdl.compiler.ir.ConfigN
44
import dfhdl.platforms.devices.Bank
55
import Resource.CanConnect
6+
import dfhdl.DFC
67

78
class Power(val levelVolt: ConfigN[constraints.IO.LevelVolt] = None) extends IO:
89
injectConstraint(constraints.IO(levelVolt = levelVolt))
@@ -24,5 +25,7 @@ end Power
2425
object Power:
2526
// power is unidirectional, but the connection is commutative.
2627
// always constraints travel from power to bank, not the other way around.
27-
given [B <: Bank, P <: Power]: CanConnect[B, P] = (bank, power) => bank.connectFrom(power)
28-
given [P <: Power, B <: Bank]: CanConnect[P, B] = (power, bank) => bank.connectFrom(power)
28+
given [B <: Bank, P <: Power]: CanConnect[B, P] with
29+
def connect(bank: B, power: P)(using DFC): Unit = bank.connectFrom(power)
30+
given [P <: Power, B <: Bank]: CanConnect[P, B] with
31+
def connect(power: P, bank: B)(using DFC): Unit = bank.connectFrom(power)

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ trait ToggleIO[T <: Toggle] extends IO:
1010

1111
object ToggleIO:
1212
given [T <: Toggle, R <: ToggleIO[T], V <: DFValOf[DFBoolOrBit]](using
13-
dfc: DFC,
1413
t: ShowType[T]
1514
)(using
1615
expectedActiveState: GivenOrError[
@@ -19,10 +18,12 @@ object ToggleIO:
1918
t.Out +
2019
"`.\nTo fix this, add:\n `given ExpectedActiveState[" + t.Out + "] = " + t.Out + ".EXPECTED_ACTIVE_STATE`"
2120
]
22-
): CanConnect[R, V] = (resource: R, dfVal: V) =>
23-
resource.injectConstraint(constraints.IO(invertActiveState =
24-
resource.activeState != expectedActiveState.value
25-
)).connect(dfVal)
21+
): CanConnect[R, V] with
22+
def connect(resource: R, dfVal: V)(using DFC): Unit =
23+
resource.injectConstraint(constraints.IO(invertActiveState =
24+
resource.activeState != expectedActiveState.value
25+
)).connect(dfVal)
26+
end given
2627
end ToggleIO
2728

2829
abstract class ToggleIOComp[T <: Toggle](

0 commit comments

Comments
 (0)