blob: b10b477e736b2af555002ccdcfa8a5cf92f0bfcf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package Chisel
private class Emitter(circuit: Circuit) {
override def toString = res.toString
private def emitPort(e: Port): String =
s"${e.dir} ${e.id.getRef.name} : ${e.id.toType}"
private def emit(e: Command, ctx: Component): String = e match {
case e: DefPrim[_] => s"node ${e.name} = ${e.op.name}(${e.args.map(_.fullName(ctx)).reduce(_+", "+_)})"
case e: DefWire => s"wire ${e.name} : ${e.id.toType}"
case e: DefPoison[_] => s"poison ${e.name} : ${e.id.toType}"
case e: DefRegister => s"reg ${e.name} : ${e.id.toType}, ${e.clock.fullName(ctx)}, ${e.reset.fullName(ctx)}"
case e: DefMemory => s"cmem ${e.name} : ${e.t.toType}[${e.size}], ${e.clock.fullName(ctx)}"
case e: DefSeqMemory => s"smem ${e.name} : ${e.t.toType}[${e.size}], ${e.clock.fullName(ctx)}"
case e: DefAccessor[_] => s"infer accessor ${e.name} = ${e.source.fullName(ctx)}[${e.index.fullName(ctx)}]"
case e: Connect => s"${e.loc.fullName(ctx)} := ${e.exp.fullName(ctx)}"
case e: BulkConnect => s"${e.loc1.fullName(ctx)} <> ${e.loc2.fullName(ctx)}"
case e: ConnectInit => s"onreset ${e.loc.fullName(ctx)} := ${e.exp.fullName(ctx)}"
case e: DefInstance => {
val modName = moduleMap.getOrElse(e.id.name, e.id.name)
s"inst ${e.name} of $modName"
}
case w: WhenBegin =>
indent()
s"when ${w.pred.fullName(ctx)} :"
case _: WhenElse =>
indent()
"else :"
case _: WhenEnd =>
unindent()
"skip"
}
private def emitBody(m: Component) = {
val me = new StringBuilder
withIndent {
for (p <- m.ports)
me ++= newline + emitPort(p)
me ++= newline
for (cmd <- m.commands)
me ++= newline + emit(cmd, m)
me ++= newline
}
me
}
private val bodyMap = collection.mutable.HashMap[StringBuilder, String]()
private val moduleMap = collection.mutable.HashMap[String, String]()
private def emit(m: Component): String = {
val body = emitBody(m)
bodyMap get body match {
case Some(name) =>
moduleMap(m.name) = name
""
case None =>
bodyMap(body) = m.name
newline + s"module ${m.name} : " + body
}
}
private var indentLevel = 0
private def newline = "\n" + (" " * indentLevel)
private def indent(): Unit = indentLevel += 1
private def unindent() { require(indentLevel > 0); indentLevel -= 1 }
private def withIndent(f: => Unit) { indent(); f; unindent() }
private val res = new StringBuilder(s"circuit ${circuit.name} : ")
withIndent { circuit.components.foreach(c => res ++= emit(c)) }
res ++= newline
}
|