Skip to content

Commit 95cb033

Browse files
author
Oron Port
committed
IO constraints wip
1 parent 336402b commit 95cb033

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package dfhdl.compiler.printing
2+
3+
trait HasCodeString:
4+
def codeString(using Printer): String

compiler/ir/src/main/scala/dfhdl/compiler/printing/Printer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class DFPrinter(using val getSet: MemberGetSet, val printerOptions: PrinterOptio
359359
def csDocString(doc: String): String = doc.betterLinesIterator.mkString("/**", "\n *", "*/")
360360
def csAnnotations(meta: Meta): String =
361361
if (meta.annotations.isEmpty) ""
362-
else meta.annotations.view.map(x => s"@hw.annotation.${x.codeString}").mkString("", "\n", "\n")
362+
else meta.annotations.view.map(_.codeString).mkString("", "\n", "\n")
363363
// def csTimer(timer: Timer): String =
364364
// val timerBody = timer match
365365
// case p: Timer.Periodic =>

compiler/ir/src/main/scala/dfhdl/hw/annotation.scala

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
*/
33
package dfhdl.hw
44

5-
import dfhdl.compiler.printing.Printer
5+
import dfhdl.compiler.printing.{Printer, HasCodeString}
66
import scala.annotation.StaticAnnotation
77
import dfhdl.internals.HasTypeName
88
import scala.annotation.Annotation
99
import upickle.default.*
1010
import dfhdl.internals.StableEnum
11+
import dfhdl.compiler.ir.ConfigN
1112

1213
object annotation:
13-
sealed abstract class HWAnnotation extends StaticAnnotation, Product, Serializable
14+
sealed abstract class HWAnnotation extends StaticAnnotation, Product, Serializable, HasCodeString
1415
derives CanEqual:
1516
val isActive: Boolean
16-
def codeString(using Printer): String
1717

1818
extension (annotList: List[Annotation])
1919
def getActiveHWAnnotations: List[HWAnnotation] = annotList.collect {
@@ -23,7 +23,8 @@ object annotation:
2323
given ReadWriter[HWAnnotation] = ReadWriter.merge(
2424
summon[ReadWriter[unused]],
2525
summon[ReadWriter[pure]],
26-
summon[ReadWriter[flattenMode]]
26+
summon[ReadWriter[flattenMode]],
27+
summon[ReadWriter[constraints.Constraint]]
2728
)
2829

2930
sealed trait unused extends HWAnnotation derives ReadWriter
@@ -32,24 +33,24 @@ object annotation:
3233
*/
3334
final case class quiet(isActive: Boolean) extends unused:
3435
def this() = this(true)
35-
def codeString(using Printer): String = "unused.quiet"
36+
def codeString(using Printer): String = "@hw.annotation.unused.quiet"
3637

3738
/** `keep` suppresses the unused warning, and also attempts to keep the tagged value.
3839
*/
3940
final case class keep(isActive: Boolean) extends unused:
4041
def this() = this(true)
41-
def codeString(using Printer): String = "unused.keep"
42+
def codeString(using Printer): String = "@hw.annotation.unused.keep"
4243

4344
/** `prune` removes all the redundant paths until and including the tagged value.
4445
*/
4546
final case class prune(isActive: Boolean) extends unused:
4647
def this() = this(true)
47-
def codeString(using Printer): String = "unused.prune"
48+
def codeString(using Printer): String = "@hw.annotation.unused.prune"
4849
end unused
4950

5051
final case class pure(isActive: Boolean) extends HWAnnotation derives ReadWriter:
5152
def this() = this(true)
52-
def codeString(using Printer): String = "pure"
53+
def codeString(using Printer): String = "@hw.annotation.pure"
5354

5455
/** Flattening Mode:
5556
* - transparent: $memberName
@@ -62,10 +63,61 @@ object annotation:
6263
case suffix(sep: String)
6364
val isActive: Boolean = true
6465
def codeString(using Printer): String =
65-
this match
66-
case transparent() => "flattenMode.transparent()"
67-
case prefix(sep) => s"""flattenMode.prefix("$sep")"""
68-
case suffix(sep) => s"""flattenMode.suffix("$sep")"""
66+
"@hw.annotation.flattenMode." + (
67+
this match
68+
case transparent() => "transparent()"
69+
case prefix(sep) => s"""prefix("$sep")"""
70+
case suffix(sep) => s"""suffix("$sep")"""
71+
)
6972
object flattenMode:
7073
val defaultPrefixUnderscore = flattenMode.prefix("_")
7174
end annotation
75+
76+
object constraints:
77+
sealed abstract class Constraint extends annotation.HWAnnotation:
78+
val isActive: Boolean = true
79+
object Constraint:
80+
given ReadWriter[Constraint] = ReadWriter.merge(
81+
summon[ReadWriter[io.IOConstraints]]
82+
)
83+
84+
object io:
85+
enum IOStandard extends StableEnum, HasCodeString derives CanEqual, ReadWriter:
86+
case LVCMOS33, LVCMOS25, LVCMOS18
87+
def codeString(using Printer): String = "IOStandard." + this.toString
88+
enum SlewRate extends StableEnum, HasCodeString derives CanEqual, ReadWriter:
89+
case SLOW, FAST
90+
def codeString(using Printer): String = "SlewRate." + this.toString
91+
enum PullMode extends StableEnum, HasCodeString derives CanEqual, ReadWriter:
92+
case UP, DOWN
93+
def codeString(using Printer): String = "PullMode." + this.toString
94+
95+
final case class IOConstraints(
96+
bitIdx: ConfigN[Int] = None,
97+
loc: ConfigN[String] = None,
98+
standard: ConfigN[IOStandard] = None,
99+
slewRate: ConfigN[SlewRate] = None,
100+
driveStrength: ConfigN[Int] = None,
101+
pullMode: ConfigN[PullMode] = None
102+
) extends Constraint derives ReadWriter:
103+
def codeString(using Printer): String =
104+
def byName[T](name: String, value: ConfigN[T]): String =
105+
value match
106+
case None => ""
107+
case cs: HasCodeString => s"$name = ${cs.codeString}"
108+
case str: String => s"$name = \"$str\""
109+
case _ => s"$name = ${value}"
110+
val params = List(
111+
byName("bitIdx", bitIdx),
112+
byName("loc", loc),
113+
byName("standard", standard),
114+
byName("slewRate", slewRate),
115+
byName("driveStrength", driveStrength),
116+
byName("pullMode", pullMode)
117+
).filter(_.nonEmpty).mkString(", ")
118+
s"""@IOConstraints($params)"""
119+
end codeString
120+
end IOConstraints
121+
end io
122+
123+
end constraints

0 commit comments

Comments
 (0)