Skip to content

Instantly share code, notes, and snippets.

@jelinski
Created January 24, 2023 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelinski/4041bcf4c0068333f8062238462b2757 to your computer and use it in GitHub Desktop.
Save jelinski/4041bcf4c0068333f8062238462b2757 to your computer and use it in GitHub Desktop.
trait Bindable {
def label: String
}
case class Port(label: String) extends Bindable
case class Net(label: String) extends Bindable
case class Binding(from: Bindable, to: Bindable){
override def toString: String = s"$from -> $to"
}
case class Instance(module: Module, bindings: Set[Binding])
trait Module {
def ports(): Set[Port]
def nets(): Set[Net]
def instances(): Set[Instance]
}
object M1 extends Module {
val x: Port = Port("x")
val y: Port = Port("y")
override def ports(): Set[Port] = Set(x, y)
override def nets(): Set[Net] = Set.empty
override def instances(): Set[Instance] = Set.empty
}
object M2 extends Module {
val u: Port = Port("u")
val v: Port = Port("v")
val t: Net = Net("t")
val i1: Instance = Instance(
M1,
Set(
Binding(M1.x, u),
Binding(M1.y, t)
)
)
val i2: Instance = Instance(
M1,
Set(
Binding(M1.x, t),
Binding(M1.y, u)
)
)
override def ports(): Set[Port] = Set(u, v)
override def nets(): Set[Net] = Set(t)
override def instances(): Set[Instance] = Set(i1, i2)
}
def traverseAndPrint(module: Module, level: Int = 0): Unit = {
def printPadded(message: String) = println(s"${" ".repeat(level*2)}$message")
printPadded(s"Module of type ${module.getClass.getSimpleName}")
printPadded(s"Ports: ${module.ports()}")
printPadded(s"Nets: ${module.nets()}")
printPadded(s"Instances: ")
if (module.instances().isEmpty) printPadded("- No instances")
else {
module.instances().foreach(i =>
if(i.bindings.isEmpty) printPadded("- No Bindings")
else printPadded(s"- Bindings: ${i.bindings.mkString("; ")}")
traverseAndPrint(i.module, level + 1)
)
}
}
println("MODULE M1:")
traverseAndPrint(M1)
println("MODULE M2:")
traverseAndPrint(M2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment