diplomacy: output JSON formatted version of DTS
This commit is contained in:
parent
0178248551
commit
1eeaa390c6
@ -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)
|
||||
}
|
||||
|
45
src/main/scala/diplomacy/JSON.scala
Normal file
45
src/main/scala/diplomacy/JSON.scala
Normal 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("{", ",", "}"))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user