Move barstools tapeout src to tools/tapeout
This commit is contained in:
95
tools/tapeout/src/main/scala/macrolib/ConfReader.scala
Normal file
95
tools/tapeout/src/main/scala/macrolib/ConfReader.scala
Normal file
@@ -0,0 +1,95 @@
|
||||
package mdf.macrolib
|
||||
|
||||
object ConfReader {
|
||||
import scala.util.matching.Regex._
|
||||
|
||||
type ConfPort = (String, Boolean) // prefix (e.g. "RW0") and true if masked
|
||||
|
||||
/** Rename ports like "read" to R0, "write" to W0, and "rw" to RW0, and
|
||||
* return a count of read, write, and readwrite ports.
|
||||
*/
|
||||
def renamePorts(ports: Seq[String]): (Seq[ConfPort], Int, Int, Int) = {
|
||||
var readCount = 0
|
||||
var writeCount = 0
|
||||
var readWriteCount = 0
|
||||
(
|
||||
ports.map {
|
||||
_ match {
|
||||
case "read" => readCount += 1; (s"R${readCount - 1}", false)
|
||||
case "write" => writeCount += 1; (s"W${writeCount - 1}", false)
|
||||
case "mwrite" => writeCount += 1; (s"W${writeCount - 1}", true)
|
||||
case "rw" => readWriteCount += 1; (s"RW${readWriteCount - 1}", false)
|
||||
case "mrw" => readWriteCount += 1; (s"RW${readWriteCount - 1}", true)
|
||||
}
|
||||
},
|
||||
readCount,
|
||||
writeCount,
|
||||
readWriteCount
|
||||
)
|
||||
}
|
||||
|
||||
def generateFirrtlPort(port: ConfPort, width: Int, depth: Int, maskGran: Option[Int]): MacroPort = {
|
||||
val (prefix, masked) = port
|
||||
val isReadWriter = prefix.startsWith("RW")
|
||||
val isReader = prefix.startsWith("R") && !isReadWriter
|
||||
val isWriter = prefix.startsWith("W")
|
||||
val r = if (isReadWriter) "r" else ""
|
||||
val w = if (isReadWriter) "w" else ""
|
||||
MacroPort(
|
||||
address = PolarizedPort(s"${prefix}_addr", ActiveHigh),
|
||||
clock = Some(PolarizedPort(s"${prefix}_clk", PositiveEdge)),
|
||||
writeEnable = if (isReadWriter) Some(PolarizedPort(s"${prefix}_${w}mode", ActiveHigh)) else None,
|
||||
output = if (isReader || isReadWriter) Some(PolarizedPort(s"${prefix}_${w}data", ActiveHigh)) else None,
|
||||
input = if (isWriter || isReadWriter) Some(PolarizedPort(s"${prefix}_${r}data", ActiveHigh)) else None,
|
||||
maskPort = if (masked) Some(PolarizedPort(s"${prefix}_${w}mask", ActiveHigh)) else None,
|
||||
maskGran = if (masked) maskGran else None,
|
||||
width = Some(width),
|
||||
depth = Some(depth)
|
||||
)
|
||||
}
|
||||
|
||||
/** Read a conf line into a SRAMMacro, but returns an error string in Left
|
||||
* instead of throwing errors if the line is malformed.
|
||||
*/
|
||||
def readSingleLineSafe(line: String): Either[String, SRAMMacro] = {
|
||||
val pattern = """name ([^\s]+) depth (\d+) width (\d+) ports ([a-z,]+)\s?(?:mask_gran (\d+))?""".r
|
||||
pattern.findFirstMatchIn(line) match {
|
||||
case Some(m: Match) => {
|
||||
val name: String = m.group(1)
|
||||
val depth: Int = (m.group(2)).toInt
|
||||
val width: Int = (m.group(3)).toInt
|
||||
val ports: Seq[String] = (m.group(4)).split(",")
|
||||
val (firrtlPorts, readPortCount, writePortCount, readWritePortCount) = renamePorts(ports)
|
||||
val familyStr =
|
||||
(if (readPortCount > 0) s"${readPortCount}r" else "") +
|
||||
(if (writePortCount > 0) s"${writePortCount}w" else "") +
|
||||
(if (readWritePortCount > 0) s"${readWritePortCount}rw" else "")
|
||||
val maskGran: Option[Int] = Option(m.group(5)).map(_.toInt)
|
||||
Right(
|
||||
SRAMMacro(
|
||||
name = name,
|
||||
width = width,
|
||||
depth = depth,
|
||||
family = familyStr,
|
||||
vt = "",
|
||||
mux = 1,
|
||||
ports = firrtlPorts.map(generateFirrtlPort(_, width, depth, maskGran)),
|
||||
extraPorts = List()
|
||||
)
|
||||
)
|
||||
}
|
||||
case _ => Left("Input line did not match conf regex")
|
||||
}
|
||||
}
|
||||
|
||||
/** Read a conf line into a SRAMMacro. */
|
||||
def readSingleLine(line: String): SRAMMacro = {
|
||||
readSingleLineSafe(line).right.get
|
||||
}
|
||||
|
||||
/** Read the contents of the conf file into a seq of SRAMMacro. */
|
||||
def readFromString(contents: String): Seq[SRAMMacro] = {
|
||||
// Trim, remove empty lines, then pass to readSingleLine
|
||||
contents.split("\n").map(_.trim).filter(_ != "").map(readSingleLine(_))
|
||||
}
|
||||
}
|
||||
61
tools/tapeout/src/main/scala/macrolib/FillerMacroBase.scala
Normal file
61
tools/tapeout/src/main/scala/macrolib/FillerMacroBase.scala
Normal file
@@ -0,0 +1,61 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
import scala.language.implicitConversions
|
||||
|
||||
// Filler and metal filler
|
||||
abstract class FillerMacroBase(name: String, vt: String) extends Macro {
|
||||
override def toString(): String = {
|
||||
s"${this.getClass.getSimpleName}(name=${name}, vt=${vt})"
|
||||
}
|
||||
|
||||
override def toJSON(): JsObject = {
|
||||
JsObject(
|
||||
Seq(
|
||||
"type" -> JsString(typeStr),
|
||||
"name" -> Json.toJson(name),
|
||||
"vt" -> Json.toJson(vt)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
object FillerMacroBase {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[FillerMacroBase] = {
|
||||
val typee: String = json.get("type") match {
|
||||
case Some(x: JsString) =>
|
||||
x.value match {
|
||||
case "" => return None
|
||||
case x => x
|
||||
}
|
||||
case _ => return None
|
||||
}
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) =>
|
||||
x.value match {
|
||||
case "" => return None
|
||||
case x => x
|
||||
}
|
||||
case _ => return None
|
||||
}
|
||||
val vt: String = json.get("vt") match {
|
||||
case Some(x: JsString) =>
|
||||
x.value match {
|
||||
case "" => return None
|
||||
case x => x
|
||||
}
|
||||
case _ => return None
|
||||
}
|
||||
typee match {
|
||||
case "metal filler cell" => Some(MetalFillerMacro(name, vt))
|
||||
case "filler cell" => Some(FillerMacro(name, vt))
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case class FillerMacro(name: String, vt: String) extends FillerMacroBase(name, vt) {
|
||||
override def typeStr = "filler cell"
|
||||
}
|
||||
case class MetalFillerMacro(name: String, vt: String) extends FillerMacroBase(name, vt) {
|
||||
override def typeStr = "metal filler cell"
|
||||
}
|
||||
72
tools/tapeout/src/main/scala/macrolib/FlipChipMacro.scala
Normal file
72
tools/tapeout/src/main/scala/macrolib/FlipChipMacro.scala
Normal file
@@ -0,0 +1,72 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import scala.language.implicitConversions
|
||||
|
||||
// Flip Chip Macro
|
||||
case class FlipChipMacro(
|
||||
name: String,
|
||||
bumpDimensions: (Int, Int),
|
||||
bumpLocations: Seq[Seq[String]])
|
||||
extends Macro {
|
||||
override def toJSON(): JsObject = {
|
||||
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"name" -> Json.toJson(name),
|
||||
"type" -> Json.toJson(typeStr),
|
||||
"bump_dimensions" -> JsArray(Seq(bumpDimensions._1, bumpDimensions._2).map { JsNumber(_) }),
|
||||
"bump_locations" -> JsArray(bumpLocations.map(l => JsArray(l.map(JsString))))
|
||||
)
|
||||
)
|
||||
|
||||
JsObject(output)
|
||||
}
|
||||
val maxIONameSize = bumpLocations.foldLeft(0) { (size, row) =>
|
||||
row.foldLeft(size) { (size, str) => scala.math.max(size, str.length) }
|
||||
}
|
||||
def visualize: String = {
|
||||
val output = new StringBuffer()
|
||||
for (x <- 0 until bumpDimensions._1) {
|
||||
for (y <- 0 until bumpDimensions._2) {
|
||||
val name = bumpLocations(x)(y).drop(1).dropRight(1)
|
||||
val extra = maxIONameSize - name.length()
|
||||
val leftSpace = " " * (extra / 2)
|
||||
val rightSpace = " " * (extra / 2 + extra % 2)
|
||||
output.append(leftSpace + name + rightSpace + "|")
|
||||
}
|
||||
output.append("\n")
|
||||
}
|
||||
output.toString()
|
||||
}
|
||||
|
||||
override def typeStr = "flipchip"
|
||||
}
|
||||
|
||||
object FlipChipMacro {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[FlipChipMacro] = {
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
|
||||
val bumpDimensions: (Int, Int) = json.get("bump_dimensions") match {
|
||||
case Some(JsArray(x)) if x.size == 2 =>
|
||||
val z = x.map(_.as[JsNumber].value.intValue)
|
||||
(z(0), z(1))
|
||||
case None => return None
|
||||
}
|
||||
val bumpLocations: Seq[Seq[String]] = json.get("bump_locations") match {
|
||||
case Some(JsArray(array)) =>
|
||||
array.collect { case JsArray(a2) => a2.map(_.toString).toSeq }.toSeq
|
||||
case _ => return None
|
||||
}
|
||||
// Can't have dimensions and locations which don't match
|
||||
if (bumpLocations.size != bumpDimensions._1) return None
|
||||
if (bumpLocations.collect { case x if x.size != bumpDimensions._2 => x }.nonEmpty) return None
|
||||
|
||||
Some(FlipChipMacro(name, bumpDimensions, bumpLocations))
|
||||
}
|
||||
}
|
||||
147
tools/tapeout/src/main/scala/macrolib/IOMacro.scala
Normal file
147
tools/tapeout/src/main/scala/macrolib/IOMacro.scala
Normal file
@@ -0,0 +1,147 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import scala.language.implicitConversions
|
||||
|
||||
sealed abstract class PortType { def toJSON(): JsString = JsString(toString) }
|
||||
case object Digital extends PortType { override def toString: String = "digital" }
|
||||
case object Analog extends PortType { override def toString: String = "analog" }
|
||||
case object Power extends PortType { override def toString: String = "power" }
|
||||
case object Ground extends PortType { override def toString: String = "ground" }
|
||||
case object NoConnect extends PortType { override def toString: String = "NC" }
|
||||
|
||||
sealed abstract class Direction { def toJSON(): JsString = JsString(toString) }
|
||||
case object Input extends Direction { override def toString: String = "input" }
|
||||
case object Output extends Direction { override def toString: String = "output" }
|
||||
case object InOut extends Direction { override def toString: String = "inout" }
|
||||
|
||||
sealed abstract class Termination { def toJSON(): JsValue }
|
||||
case object CMOS extends Termination { override def toJSON(): JsString = JsString("CMOS") }
|
||||
case class Resistive(ohms: Int) extends Termination { override def toJSON(): JsNumber = JsNumber(ohms) }
|
||||
|
||||
sealed abstract class TerminationType { def toJSON(): JsString }
|
||||
case object Single extends TerminationType { override def toJSON(): JsString = JsString("single") }
|
||||
case object Differential extends TerminationType { override def toJSON(): JsString = JsString("differential") }
|
||||
|
||||
// IO macro
|
||||
case class IOMacro(
|
||||
name: String,
|
||||
tpe: PortType,
|
||||
direction: Option[Direction] = None,
|
||||
termination: Option[Termination] = None,
|
||||
terminationType: Option[TerminationType] = None,
|
||||
terminationReference: Option[String] = None,
|
||||
matching: Seq[String] = Seq.empty[String],
|
||||
bbname: Option[String] = None)
|
||||
extends Macro {
|
||||
override def toJSON(): JsObject = {
|
||||
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"name" -> Json.toJson(name),
|
||||
"type" -> tpe.toJSON()
|
||||
)
|
||||
)
|
||||
if (direction.isDefined) output.append("direction" -> direction.get.toJSON)
|
||||
if (termination.isDefined) output.append("termination" -> termination.get.toJSON)
|
||||
if (terminationType.isDefined) output.append("terminationType" -> terminationType.get.toJSON)
|
||||
if (terminationReference.isDefined) output.append("terminationReference" -> JsString(terminationReference.get))
|
||||
if (matching.nonEmpty) output.append("match" -> JsArray(matching.map(JsString)))
|
||||
if (bbname.nonEmpty) output.append("blackBox" -> JsString(bbname.get))
|
||||
|
||||
JsObject(output)
|
||||
}
|
||||
|
||||
override def typeStr = "iomacro"
|
||||
}
|
||||
object IOMacro {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[IOMacro] = {
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
val tpe: PortType = json.get("type") match {
|
||||
case Some(JsString("power")) => Power
|
||||
case Some(JsString("ground")) => Ground
|
||||
case Some(JsString("digital")) => Digital
|
||||
case Some(JsString("analog")) => Analog
|
||||
case Some(JsString("NC")) => NoConnect
|
||||
case _ => return None
|
||||
}
|
||||
val direction: Option[Direction] = json.get("direction") match {
|
||||
case Some(JsString("input")) => Some(Input)
|
||||
case Some(JsString("output")) => Some(Output)
|
||||
case Some(JsString("inout")) => Some(InOut)
|
||||
case _ => None
|
||||
}
|
||||
val termination: Option[Termination] = json.get("termination") match {
|
||||
case Some(JsNumber(x)) => Some(Resistive(x.toInt))
|
||||
case Some(JsString("CMOS")) => Some(CMOS)
|
||||
case _ => None
|
||||
}
|
||||
val terminationType: Option[TerminationType] = json.get("terminationType") match {
|
||||
case Some(JsString("differential")) => Some(Differential)
|
||||
case Some(JsString("single")) => Some(Single)
|
||||
case _ => None
|
||||
}
|
||||
val terminationRef: Option[String] = json.get("terminationReference") match {
|
||||
case Some(JsString(x)) => Some(x)
|
||||
case _ if terminationType.isDefined => return None
|
||||
case _ => None
|
||||
}
|
||||
val matching: Seq[String] = json.get("match") match {
|
||||
case Some(JsArray(array)) => array.map(_.as[JsString].value).toList
|
||||
case _ => Seq.empty[String]
|
||||
}
|
||||
val bbname: Option[String] = json.get("blackBox") match {
|
||||
case Some(JsString(module)) => Some(module)
|
||||
case Some(_) => return None
|
||||
case _ => None
|
||||
}
|
||||
Some(IOMacro(name, tpe, direction, termination, terminationType, terminationRef, matching, bbname))
|
||||
}
|
||||
}
|
||||
|
||||
case class IOProperties(name: String, top: String, ios: Seq[IOMacro]) extends Macro {
|
||||
override def toJSON(): JsObject = {
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"name" -> Json.toJson(name),
|
||||
"top" -> Json.toJson(top),
|
||||
"type" -> Json.toJson(typeStr),
|
||||
"ios" -> JsArray(ios.map(_.toJSON))
|
||||
)
|
||||
)
|
||||
JsObject(output)
|
||||
}
|
||||
|
||||
override def typeStr = "io_properties"
|
||||
|
||||
}
|
||||
|
||||
object IOProperties {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[IOProperties] = {
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
val top: String = json.get("top") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
val ios: Seq[IOMacro] = json.get("ios") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
val b = IOMacro.parseJSON(a);
|
||||
if (b == None) {
|
||||
return None
|
||||
} else b.get
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
Some(IOProperties(name, top, ios))
|
||||
}
|
||||
}
|
||||
19
tools/tapeout/src/main/scala/macrolib/MacroLib.scala
Normal file
19
tools/tapeout/src/main/scala/macrolib/MacroLib.scala
Normal file
@@ -0,0 +1,19 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import scala.language.implicitConversions
|
||||
|
||||
// TODO: decide if we should always silently absorb errors
|
||||
|
||||
// See macro_format.yml for the format description.
|
||||
|
||||
// "Base class" for macros
|
||||
abstract class Macro {
|
||||
def name: String
|
||||
|
||||
// Type of macro is determined by subclass
|
||||
def typeStr: String
|
||||
|
||||
def toJSON(): JsObject
|
||||
}
|
||||
444
tools/tapeout/src/main/scala/macrolib/SRAM.scala
Normal file
444
tools/tapeout/src/main/scala/macrolib/SRAM.scala
Normal file
@@ -0,0 +1,444 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import scala.language.implicitConversions
|
||||
|
||||
// SRAM macro
|
||||
case class SRAMMacro(
|
||||
name: String,
|
||||
width: Int,
|
||||
depth: BigInt,
|
||||
family: String,
|
||||
ports: Seq[MacroPort],
|
||||
vt: String = "",
|
||||
mux: Int = 1,
|
||||
extraPorts: Seq[MacroExtraPort] = List())
|
||||
extends Macro {
|
||||
override def toJSON(): JsObject = {
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"type" -> JsString("sram"),
|
||||
"name" -> Json.toJson(name),
|
||||
"width" -> Json.toJson(width),
|
||||
"depth" -> Json.toJson(depth.toString),
|
||||
"mux" -> Json.toJson(mux),
|
||||
"mask" -> Json.toJson(ports.exists(p => p.maskPort.isDefined)),
|
||||
"ports" -> JsArray(ports.map { _.toJSON })
|
||||
)
|
||||
)
|
||||
if (family != "") {
|
||||
output.appendAll(Seq("family" -> Json.toJson(family)))
|
||||
}
|
||||
if (vt != "") {
|
||||
output.appendAll(Seq("vt" -> Json.toJson(vt)))
|
||||
}
|
||||
if (extraPorts.length > 0) {
|
||||
output.appendAll(Seq("extra ports" -> JsArray(extraPorts.map { _.toJSON })))
|
||||
}
|
||||
|
||||
JsObject(output)
|
||||
}
|
||||
|
||||
override def typeStr = "sram"
|
||||
}
|
||||
object SRAMMacro {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[SRAMMacro] = {
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
val width: Int = json.get("width") match {
|
||||
case Some(x: JsNumber) => x.value.intValue
|
||||
case _ => return None
|
||||
}
|
||||
val depth: BigInt = json.get("depth") match {
|
||||
case Some(x: JsString) =>
|
||||
try { BigInt(x.as[String]) }
|
||||
catch { case _: Throwable => return None }
|
||||
case _ => return None
|
||||
}
|
||||
val family: String = json.get("family") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => "" // optional
|
||||
}
|
||||
val vt: String = json.get("vt") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => "" // optional
|
||||
}
|
||||
val mux: Int = json.get("mux") match {
|
||||
case Some(x: JsNumber) => x.value.intValue
|
||||
case _ => 1 // default
|
||||
}
|
||||
val ports: Seq[MacroPort] = json.get("ports") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
val b = MacroPort.parseJSON(a, width, depth);
|
||||
if (b == None) {
|
||||
return None
|
||||
} else b.get
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
if (ports.length == 0) {
|
||||
// Can't have portless memories.
|
||||
return None
|
||||
}
|
||||
val extraPorts: Seq[MacroExtraPort] = json.get("extra ports") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
val b = MacroExtraPort.parseJSON(a);
|
||||
if (b == None) {
|
||||
return None
|
||||
} else b.get
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
Some(SRAMMacro(name, width, depth, family, ports, vt, mux, extraPorts))
|
||||
}
|
||||
}
|
||||
|
||||
// SRAM compiler
|
||||
case class SRAMGroup(
|
||||
name: Seq[String],
|
||||
family: String,
|
||||
vt: Seq[String],
|
||||
mux: Int,
|
||||
depth: Range,
|
||||
width: Range,
|
||||
ports: Seq[MacroPort],
|
||||
extraPorts: Seq[MacroExtraPort] = List()) {
|
||||
def toJSON: JsObject = {
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"name" -> JsArray(name.map(Json.toJson(_))),
|
||||
"vt" -> JsArray(vt.map(Json.toJson(_))),
|
||||
"mux" -> Json.toJson(mux),
|
||||
"depth" -> JsArray(Seq(depth.start, depth.end, depth.step).map { x => Json.toJson(x) }),
|
||||
"width" -> JsArray(Seq(width.start, width.end, width.step).map { x => Json.toJson(x) }),
|
||||
"ports" -> JsArray(ports.map { _.toJSON })
|
||||
)
|
||||
)
|
||||
if (family != "") {
|
||||
output.appendAll(Seq("family" -> Json.toJson(family)))
|
||||
}
|
||||
if (extraPorts.length > 0) {
|
||||
output.appendAll(Seq("extra ports" -> JsArray(extraPorts.map { _.toJSON })))
|
||||
}
|
||||
JsObject(output)
|
||||
}
|
||||
}
|
||||
object SRAMGroup {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[SRAMGroup] = {
|
||||
val family: String = json.get("family") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => "" // optional
|
||||
}
|
||||
val name: Seq[String] = json.get("name") match {
|
||||
case Some(x: JsArray) => x.as[List[JsString]].map(_.as[String])
|
||||
case _ => return None
|
||||
}
|
||||
val vt: Seq[String] = json.get("vt") match {
|
||||
case Some(x: JsArray) => x.as[List[JsString]].map(_.as[String])
|
||||
case _ => return None
|
||||
}
|
||||
val mux: Int = json.get("mux") match {
|
||||
case Some(x: JsNumber) => x.value.intValue
|
||||
case _ => return None
|
||||
}
|
||||
val depth: Range = json.get("depth") match {
|
||||
case Some(x: JsArray) =>
|
||||
val seq = x.as[List[JsNumber]].map(_.value.intValue)
|
||||
Range.inclusive(seq(0), seq(1), seq(2))
|
||||
case _ => return None
|
||||
}
|
||||
val width: Range = json.get("width") match {
|
||||
case Some(x: JsArray) =>
|
||||
val seq = x.as[List[JsNumber]].map(_.value.intValue)
|
||||
Range.inclusive(seq(0), seq(1), seq(2))
|
||||
case _ => return None
|
||||
}
|
||||
val ports: Seq[MacroPort] = json.get("ports") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
{
|
||||
val b = MacroPort.parseJSON(a, None, None);
|
||||
if (b == None) {
|
||||
return None
|
||||
} else b.get
|
||||
}
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
if (ports.length == 0) {
|
||||
// Can't have portless memories.
|
||||
return None
|
||||
}
|
||||
val extraPorts: Seq[MacroExtraPort] = json.get("extra ports") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
{
|
||||
val b = MacroExtraPort.parseJSON(a);
|
||||
if (b == None) {
|
||||
return None
|
||||
} else b.get
|
||||
}
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
Some(SRAMGroup(name, family, vt, mux, depth, width, ports, extraPorts))
|
||||
}
|
||||
}
|
||||
|
||||
case class SRAMCompiler(
|
||||
name: String,
|
||||
groups: Seq[SRAMGroup])
|
||||
extends Macro {
|
||||
override def toJSON(): JsObject = {
|
||||
val output = new ListBuffer[(String, JsValue)]()
|
||||
output.appendAll(
|
||||
Seq(
|
||||
"type" -> Json.toJson("sramcompiler"),
|
||||
"name" -> Json.toJson(name),
|
||||
"groups" -> JsArray(groups.map { _.toJSON })
|
||||
)
|
||||
)
|
||||
|
||||
JsObject(output)
|
||||
}
|
||||
|
||||
override def typeStr = "sramcompiler"
|
||||
}
|
||||
object SRAMCompiler {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[SRAMCompiler] = {
|
||||
val name: String = json.get("name") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None
|
||||
}
|
||||
val groups: Seq[SRAMGroup] = json.get("groups") match {
|
||||
case Some(x: JsArray) =>
|
||||
x.as[List[Map[String, JsValue]]].map { a =>
|
||||
{
|
||||
val b = SRAMGroup.parseJSON(a);
|
||||
if (b == None) { return None }
|
||||
else b.get
|
||||
}
|
||||
}
|
||||
case _ => List()
|
||||
}
|
||||
if (groups.length == 0) {
|
||||
// Can't have portless memories.
|
||||
return None
|
||||
}
|
||||
Some(SRAMCompiler(name, groups))
|
||||
}
|
||||
}
|
||||
|
||||
// Type of extra port
|
||||
sealed abstract class MacroExtraPortType
|
||||
case object Constant extends MacroExtraPortType
|
||||
object MacroExtraPortType {
|
||||
implicit def toMacroExtraPortType(s: Any): Option[MacroExtraPortType] = {
|
||||
s match {
|
||||
case "constant" => Some(Constant)
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
|
||||
implicit def toString(t: MacroExtraPortType): String = {
|
||||
t match {
|
||||
case Constant => "constant"
|
||||
case _ => ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extra port in SRAM
|
||||
case class MacroExtraPort(
|
||||
name: String,
|
||||
width: Int,
|
||||
portType: MacroExtraPortType,
|
||||
value: BigInt) {
|
||||
def toJSON(): JsObject = {
|
||||
JsObject(
|
||||
Seq(
|
||||
"name" -> Json.toJson(name),
|
||||
"width" -> Json.toJson(width),
|
||||
"type" -> JsString(MacroExtraPortType.toString(portType)),
|
||||
"value" -> JsNumber(BigDecimal(value))
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
object MacroExtraPort {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[MacroExtraPort] = {
|
||||
val name = json.get("name") match {
|
||||
case Some(x: JsString) => x.value
|
||||
case _ => return None
|
||||
}
|
||||
val width = json.get("width") match {
|
||||
case Some(x: JsNumber) => x.value.intValue
|
||||
case _ => return None
|
||||
}
|
||||
val portType: MacroExtraPortType = json.get("type") match {
|
||||
case Some(x: JsString) =>
|
||||
MacroExtraPortType.toMacroExtraPortType(x.value) match {
|
||||
case Some(t: MacroExtraPortType) => t
|
||||
case _ => return None
|
||||
}
|
||||
case _ => return None
|
||||
}
|
||||
val value = json.get("value") match {
|
||||
case Some(x: JsNumber) => x.value.toBigInt
|
||||
case _ => return None
|
||||
}
|
||||
Some(MacroExtraPort(name, width, portType, value))
|
||||
}
|
||||
}
|
||||
|
||||
// A named port that also has polarity.
|
||||
case class PolarizedPort(name: String, polarity: PortPolarity) {
|
||||
def toSeqMap(prefix: String): Seq[Tuple2[String, JsValue]] = {
|
||||
Seq(
|
||||
prefix + " port name" -> Json.toJson(name),
|
||||
prefix + " port polarity" -> JsString(polarity)
|
||||
)
|
||||
}
|
||||
}
|
||||
object PolarizedPort {
|
||||
// Parse a pair of "<prefix> port name" and "<prefix> port polarity" keys into a
|
||||
// polarized port definition.
|
||||
def parseJSON(json: Map[String, JsValue], prefix: String): Option[PolarizedPort] = {
|
||||
val name = json.get(prefix + " port name") match {
|
||||
case Some(x: JsString) => Some(x.value)
|
||||
case _ => None
|
||||
}
|
||||
val polarity: Option[PortPolarity] = json.get(prefix + " port polarity") match {
|
||||
case Some(x: JsString) => Some(x.value)
|
||||
case _ => None
|
||||
}
|
||||
|
||||
(name, polarity) match {
|
||||
case (Some(n: String), Some(p: PortPolarity)) => Some(PolarizedPort(n, p))
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A SRAM memory port
|
||||
case class MacroPort(
|
||||
address: PolarizedPort,
|
||||
clock: Option[PolarizedPort] = None,
|
||||
writeEnable: Option[PolarizedPort] = None,
|
||||
readEnable: Option[PolarizedPort] = None,
|
||||
chipEnable: Option[PolarizedPort] = None,
|
||||
output: Option[PolarizedPort] = None,
|
||||
input: Option[PolarizedPort] = None,
|
||||
maskPort: Option[PolarizedPort] = None,
|
||||
maskGran: Option[Int] = None,
|
||||
// For internal use only; these aren't port-specific.
|
||||
width: Option[Int],
|
||||
depth: Option[BigInt]) {
|
||||
def effectiveMaskGran = maskGran.getOrElse(width.get)
|
||||
|
||||
def toJSON(): JsObject = {
|
||||
val keys: Seq[Tuple2[String, Option[Any]]] = Seq(
|
||||
"address" -> Some(address),
|
||||
"clock" -> clock,
|
||||
"write enable" -> writeEnable,
|
||||
"read enable" -> readEnable,
|
||||
"chip enable" -> chipEnable,
|
||||
"output" -> output,
|
||||
"input" -> input,
|
||||
"mask" -> maskPort,
|
||||
"mask granularity" -> maskGran
|
||||
)
|
||||
JsObject(keys.flatMap(k => {
|
||||
val (key, value) = k
|
||||
value match {
|
||||
case Some(x: Int) => Seq(key -> JsNumber(x))
|
||||
case Some(x: PolarizedPort) => x.toSeqMap(key)
|
||||
case _ => List()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// Check that all port names are unique.
|
||||
private val polarizedPorts =
|
||||
List(Some(address), clock, writeEnable, readEnable, chipEnable, output, input, maskPort).flatten
|
||||
assert(polarizedPorts.distinct.size == polarizedPorts.size, "All port names must be unique")
|
||||
}
|
||||
object MacroPort {
|
||||
def parseJSON(json: Map[String, JsValue]): Option[MacroPort] = parseJSON(json, None, None)
|
||||
def parseJSON(json: Map[String, JsValue], width: Int, depth: BigInt): Option[MacroPort] =
|
||||
parseJSON(json, Some(width), Some(depth))
|
||||
def parseJSON(json: Map[String, JsValue], width: Option[Int], depth: Option[BigInt]): Option[MacroPort] = {
|
||||
val address = PolarizedPort.parseJSON(json, "address")
|
||||
if (address == None) {
|
||||
return None
|
||||
}
|
||||
|
||||
val clock = PolarizedPort.parseJSON(json, "clock")
|
||||
// TODO: validate based on family (e.g. 1rw must have a write enable, etc)
|
||||
val writeEnable = PolarizedPort.parseJSON(json, "write enable")
|
||||
val readEnable = PolarizedPort.parseJSON(json, "read enable")
|
||||
val chipEnable = PolarizedPort.parseJSON(json, "chip enable")
|
||||
|
||||
val output = PolarizedPort.parseJSON(json, "output")
|
||||
val input = PolarizedPort.parseJSON(json, "input")
|
||||
|
||||
val maskPort = PolarizedPort.parseJSON(json, "mask")
|
||||
val maskGran: Option[Int] = json.get("mask granularity") match {
|
||||
case Some(x: JsNumber) => Some(x.value.intValue)
|
||||
case _ => None
|
||||
}
|
||||
|
||||
if (maskPort.isDefined != maskGran.isDefined) {
|
||||
return None
|
||||
}
|
||||
|
||||
Some(
|
||||
MacroPort(
|
||||
width = width,
|
||||
depth = depth,
|
||||
address = address.get,
|
||||
clock = clock,
|
||||
writeEnable = writeEnable,
|
||||
readEnable = readEnable,
|
||||
chipEnable = chipEnable,
|
||||
output = output,
|
||||
input = input,
|
||||
maskPort = maskPort,
|
||||
maskGran = maskGran
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Port polarity
|
||||
trait PortPolarity
|
||||
case object ActiveLow extends PortPolarity
|
||||
case object ActiveHigh extends PortPolarity
|
||||
case object NegativeEdge extends PortPolarity
|
||||
case object PositiveEdge extends PortPolarity
|
||||
object PortPolarity {
|
||||
implicit def toPortPolarity(s: String): PortPolarity = (s: @unchecked) match {
|
||||
case "active low" => ActiveLow
|
||||
case "active high" => ActiveHigh
|
||||
case "negative edge" => NegativeEdge
|
||||
case "positive edge" => PositiveEdge
|
||||
}
|
||||
implicit def toPortPolarity(s: Option[String]): Option[PortPolarity] =
|
||||
s.map(toPortPolarity)
|
||||
|
||||
implicit def toString(p: PortPolarity): String = {
|
||||
p match {
|
||||
case ActiveLow => "active low"
|
||||
case ActiveHigh => "active high"
|
||||
case NegativeEdge => "negative edge"
|
||||
case PositiveEdge => "positive edge"
|
||||
}
|
||||
}
|
||||
}
|
||||
96
tools/tapeout/src/main/scala/macrolib/Utils.scala
Normal file
96
tools/tapeout/src/main/scala/macrolib/Utils.scala
Normal file
@@ -0,0 +1,96 @@
|
||||
package mdf.macrolib
|
||||
|
||||
import play.api.libs.json._
|
||||
|
||||
import java.io.FileNotFoundException
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import scala.language.implicitConversions
|
||||
|
||||
object Utils {
|
||||
// Read a MDF file from a String.
|
||||
def readMDFFromString(str: String): Option[Seq[Macro]] = {
|
||||
Json.parse(str) match {
|
||||
// Make sure that the document is a list.
|
||||
case arr: JsArray => {
|
||||
val result: List[Option[Macro]] = arr.as[List[Map[String, JsValue]]].map { obj =>
|
||||
// Check the type of object.
|
||||
val objTypeStr: String = obj.get("type") match {
|
||||
case Some(x: JsString) => x.as[String]
|
||||
case _ => return None // error, no type found
|
||||
}
|
||||
objTypeStr match {
|
||||
case "filler cell" | "metal filler cell" => FillerMacroBase.parseJSON(obj)
|
||||
case "sram" => SRAMMacro.parseJSON(obj)
|
||||
case "sramcompiler" => SRAMCompiler.parseJSON(obj)
|
||||
case "io_properties" => IOProperties.parseJSON(obj)
|
||||
case "flipchip" => FlipChipMacro.parseJSON(obj)
|
||||
case _ => None // skip unknown macro types
|
||||
}
|
||||
}
|
||||
// Remove all the Nones and convert back to Seq[Macro]
|
||||
Some(result.filter { x => x != None }.map { x => x.get })
|
||||
}
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
|
||||
// Read a MDF file from a path.
|
||||
def readMDFFromPath(path: Option[String]): Option[Seq[Macro]] = {
|
||||
path match {
|
||||
case None => None
|
||||
// Read file into string and parse
|
||||
case Some(p) =>
|
||||
try {
|
||||
Utils.readMDFFromString(scala.io.Source.fromFile(p).mkString)
|
||||
} catch {
|
||||
case f: FileNotFoundException =>
|
||||
println(s"FILE NOT FOUND $p in dir ${os.pwd}")
|
||||
throw f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write a MDF file to a String.
|
||||
def writeMDFToString(s: Seq[Macro]): String = {
|
||||
Json.prettyPrint(JsArray(s.map(_.toJSON)))
|
||||
}
|
||||
|
||||
// Write a MDF file from a path.
|
||||
// Returns true upon success.
|
||||
def writeMDFToPath(path: Option[String], s: Seq[Macro]): Boolean = {
|
||||
path match {
|
||||
case None => false
|
||||
// Read file into string and parse
|
||||
case Some(p: String) => {
|
||||
import java.io._
|
||||
val pw = new PrintWriter(new File(p))
|
||||
pw.write(writeMDFToString(s))
|
||||
val error = pw.checkError
|
||||
pw.close()
|
||||
!error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write a macro file to a String.
|
||||
def writeMacroToString(s: Macro): String = {
|
||||
Json.prettyPrint(s.toJSON)
|
||||
}
|
||||
|
||||
// Write a Macro file from a path.
|
||||
// Returns true upon success.
|
||||
def writeMacroToPath(path: Option[String], s: Macro): Boolean = {
|
||||
path match {
|
||||
case None => false
|
||||
// Read file into string and parse
|
||||
case Some(p: String) => {
|
||||
import java.io._
|
||||
val pw = new PrintWriter(new File(p))
|
||||
pw.write(writeMacroToString(s))
|
||||
val error = pw.checkError
|
||||
pw.close()
|
||||
!error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user