1
0

diplomacy: output JSON formatted version of DTS

This commit is contained in:
Wesley W. Terpstra 2017-03-03 02:45:11 -08:00
parent 0178248551
commit 1eeaa390c6
2 changed files with 47 additions and 0 deletions

View File

@ -25,6 +25,7 @@ trait CoreplexRISCVPlatform extends CoreplexNetwork {
plic.intnode := intBar.intnode
lazy val dts = DTS(bindingTree)
lazy val json = JSON(bindingTree)
}
trait CoreplexRISCVPlatformBundle extends CoreplexNetworkBundle {
@ -49,4 +50,5 @@ trait CoreplexRISCVPlatformModule extends CoreplexNetworkModule {
println(outer.dts)
ElaborationArtefacts.add("dts", outer.dts)
ElaborationArtefacts.add("json", outer.json)
}

View File

@ -0,0 +1,45 @@
// See LICENSE.SiFive for license details.
package diplomacy
import scala.collection.immutable.SortedMap
object JSON
{
def apply(res: ResourceValue): String = {
val root = res match {
case ResourceMap(value, _) => value.toList match {
case Seq(("/", Seq(subtree))) => subtree
case _ => res
}
case _ => res
}
helper(root)(SortedMap(map(root):_*)).mkString
}
private def map(res: ResourceValue, path: String = ""): Seq[(String, String)] = res match {
case ResourceMap(value, labels) => {
labels.map(_ -> path) ++
value.flatMap { case (key, seq) => seq.flatMap(map(_, path + "/" + key)) }
}
case _ => Nil
}
private def helper(res: ResourceValue)(implicit path: Map[String, String]): Seq[String] = res match {
case ResourceAddress(address, r, w, x) =>
AddressRange.fromSets(address).map { case AddressRange(base, size) =>
s"""{"base":${base},"size":${size},"r":${r},"w":${w},"x":${x}}"""}
case ResourceMapping(address, offset) =>
AddressRange.fromSets(address).map { case AddressRange(base, size) =>
s"""{"base":${base},"size":${size},"offset":${offset}}"""}
case ResourceInt(value) => Seq(value.toString)
case ResourceString(value) => Seq("\"" + value + "\"")
case ResourceReference(value) => Seq("\"&" + path(value) + "\"")
case ResourceMap(value, _) => {
Seq(value.map {
case (key, Seq(v: ResourceMap)) => s""""${key}":${helper(v).mkString}"""
case (key, seq) => s""""${key}":[${seq.flatMap(helper).mkString(",")}]"""
}.mkString("{", ",", "}"))
}
}
}