tilelink2: move general-purpose code out of tilelink2 package
This commit is contained in:
@ -3,191 +3,124 @@
|
||||
package uncore.tilelink2
|
||||
|
||||
import Chisel._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import chisel3.internal.sourceinfo.SourceInfo
|
||||
import diplomacy._
|
||||
import scala.collection.mutable.ListBuffer
|
||||
|
||||
// DI = Downwards flowing Parameters received on the inner side of the node
|
||||
// UI = Upwards flowing Parameters generated by the inner side of the node
|
||||
// EI = Edge Parameters describing a connection on the inner side of the node
|
||||
// BI = Bundle type used when connecting to the inner side of the node
|
||||
trait InwardNodeImp[DI, UI, EI, BI <: Data]
|
||||
object TLImp extends NodeImp[TLClientPortParameters, TLManagerPortParameters, TLEdgeOut, TLEdgeIn, TLBundle]
|
||||
{
|
||||
def edgeI(pd: DI, pu: UI): EI
|
||||
def bundleI(ei: Seq[EI]): Vec[BI]
|
||||
def mixI(pu: UI, node: InwardNode[DI, UI, BI]): UI = pu
|
||||
def colour: String
|
||||
def connect(bo: => BI, bi: => BI, e: => EI)(implicit sourceInfo: SourceInfo): (Option[LazyModule], () => Unit)
|
||||
}
|
||||
|
||||
// DO = Downwards flowing Parameters generated by the outner side of the node
|
||||
// UO = Upwards flowing Parameters received on the outner side of the node
|
||||
// EO = Edge Parameters describing a connection on the outer side of the node
|
||||
// BO = Bundle type used when connecting to the outer side of the node
|
||||
trait OutwardNodeImp[DO, UO, EO, BO <: Data]
|
||||
{
|
||||
def edgeO(pd: DO, pu: UO): EO
|
||||
def bundleO(eo: Seq[EO]): Vec[BO]
|
||||
def mixO(pd: DO, node: OutwardNode[DO, UO, BO]): DO = pd
|
||||
}
|
||||
|
||||
abstract class NodeImp[D, U, EO, EI, B <: Data]
|
||||
extends Object with InwardNodeImp[D, U, EI, B] with OutwardNodeImp[D, U, EO, B]
|
||||
|
||||
abstract class BaseNode
|
||||
{
|
||||
// You cannot create a Node outside a LazyModule!
|
||||
require (!LazyModule.stack.isEmpty)
|
||||
|
||||
val lazyModule = LazyModule.stack.head
|
||||
val index = lazyModule.nodes.size
|
||||
lazyModule.nodes = this :: lazyModule.nodes
|
||||
|
||||
def name = lazyModule.name + "." + getClass.getName.split('.').last
|
||||
def omitGraphML = outputs.isEmpty && inputs.isEmpty
|
||||
|
||||
protected[tilelink2] def outputs: Seq[BaseNode]
|
||||
protected[tilelink2] def inputs: Seq[BaseNode]
|
||||
protected[tilelink2] def colour: String
|
||||
}
|
||||
|
||||
trait InwardNode[DI, UI, BI <: Data] extends BaseNode
|
||||
{
|
||||
protected[tilelink2] val numPI: Range.Inclusive
|
||||
require (!numPI.isEmpty, s"No number of inputs would be acceptable to ${name}${lazyModule.line}")
|
||||
require (numPI.start >= 0, s"${name} accepts a negative number of inputs${lazyModule.line}")
|
||||
|
||||
private val accPI = ListBuffer[(Int, OutwardNode[DI, UI, BI])]()
|
||||
private var iRealized = false
|
||||
|
||||
protected[tilelink2] def iPushed = accPI.size
|
||||
protected[tilelink2] def iPush(index: Int, node: OutwardNode[DI, UI, BI])(implicit sourceInfo: SourceInfo) {
|
||||
val info = sourceLine(sourceInfo, " at ", "")
|
||||
val noIs = numPI.size == 1 && numPI.contains(0)
|
||||
require (!noIs, s"${name}${lazyModule.line} was incorrectly connected as a sink" + info)
|
||||
require (!iRealized, s"${name}${lazyModule.line} was incorrectly connected as a sink after it's .module was used" + info)
|
||||
accPI += ((index, node))
|
||||
def edgeO(pd: TLClientPortParameters, pu: TLManagerPortParameters): TLEdgeOut = new TLEdgeOut(pd, pu)
|
||||
def edgeI(pd: TLClientPortParameters, pu: TLManagerPortParameters): TLEdgeIn = new TLEdgeIn(pd, pu)
|
||||
def bundleO(eo: Seq[TLEdgeOut]): Vec[TLBundle] = {
|
||||
require (!eo.isEmpty)
|
||||
Vec(eo.size, TLBundle(eo.map(_.bundle).reduce(_.union(_))))
|
||||
}
|
||||
def bundleI(ei: Seq[TLEdgeIn]): Vec[TLBundle] = {
|
||||
require (!ei.isEmpty)
|
||||
Vec(ei.size, TLBundle(ei.map(_.bundle).reduce(_.union(_)))).flip
|
||||
}
|
||||
|
||||
private def reqI() = require(numPI.contains(accPI.size), s"${name} has ${accPI.size} inputs, expected ${numPI}${lazyModule.line}")
|
||||
protected[tilelink2] lazy val iPorts = { iRealized = true; reqI(); accPI.result() }
|
||||
|
||||
protected[tilelink2] val iParams: Seq[UI]
|
||||
protected[tilelink2] def iConnect: Vec[BI]
|
||||
}
|
||||
|
||||
trait OutwardNode[DO, UO, BO <: Data] extends BaseNode
|
||||
{
|
||||
protected[tilelink2] val numPO: Range.Inclusive
|
||||
require (!numPO.isEmpty, s"No number of outputs would be acceptable to ${name}${lazyModule.line}")
|
||||
require (numPO.start >= 0, s"${name} accepts a negative number of outputs${lazyModule.line}")
|
||||
|
||||
private val accPO = ListBuffer[(Int, InwardNode [DO, UO, BO])]()
|
||||
private var oRealized = false
|
||||
|
||||
protected[tilelink2] def oPushed = accPO.size
|
||||
protected[tilelink2] def oPush(index: Int, node: InwardNode [DO, UO, BO])(implicit sourceInfo: SourceInfo) {
|
||||
val info = sourceLine(sourceInfo, " at ", "")
|
||||
val noOs = numPO.size == 1 && numPO.contains(0)
|
||||
require (!noOs, s"${name}${lazyModule.line} was incorrectly connected as a source" + info)
|
||||
require (!oRealized, s"${name}${lazyModule.line} was incorrectly connected as a source after it's .module was used" + info)
|
||||
accPO += ((index, node))
|
||||
def colour = "#000000" // black
|
||||
def connect(bo: => TLBundle, bi: => TLBundle, ei: => TLEdgeIn)(implicit sourceInfo: SourceInfo): (Option[LazyModule], () => Unit) = {
|
||||
val monitor = LazyModule(new TLMonitor(() => new TLBundleSnoop(bo.params), () => ei, sourceInfo))
|
||||
(Some(monitor), () => {
|
||||
bi <> bo
|
||||
monitor.module.io.in := TLBundleSnoop(bo)
|
||||
})
|
||||
}
|
||||
|
||||
private def reqO() = require(numPO.contains(accPO.size), s"${name} has ${accPO.size} outputs, expected ${numPO}${lazyModule.line}")
|
||||
protected[tilelink2] lazy val oPorts = { oRealized = true; reqO(); accPO.result() }
|
||||
|
||||
protected[tilelink2] val oParams: Seq[DO]
|
||||
protected[tilelink2] def oConnect: Vec[BO]
|
||||
override def mixO(pd: TLClientPortParameters, node: OutwardNode[TLClientPortParameters, TLManagerPortParameters, TLBundle]): TLClientPortParameters =
|
||||
pd.copy(clients = pd.clients.map { c => c.copy (nodePath = node +: c.nodePath) })
|
||||
override def mixI(pu: TLManagerPortParameters, node: InwardNode[TLClientPortParameters, TLManagerPortParameters, TLBundle]): TLManagerPortParameters =
|
||||
pu.copy(managers = pu.managers.map { m => m.copy (nodePath = node +: m.nodePath) })
|
||||
}
|
||||
|
||||
class MixedNode[DI, UI, EI, BI <: Data, DO, UO, EO, BO <: Data](
|
||||
inner: InwardNodeImp [DI, UI, EI, BI],
|
||||
outer: OutwardNodeImp[DO, UO, EO, BO])(
|
||||
private val dFn: (Int, Seq[DI]) => Seq[DO],
|
||||
private val uFn: (Int, Seq[UO]) => Seq[UI],
|
||||
protected[tilelink2] val numPO: Range.Inclusive,
|
||||
protected[tilelink2] val numPI: Range.Inclusive)
|
||||
extends BaseNode with InwardNode[DI, UI, BI] with OutwardNode[DO, UO, BO]
|
||||
{
|
||||
// meta-data for printing the node graph
|
||||
protected[tilelink2] def colour = inner.colour
|
||||
protected[tilelink2] def outputs = oPorts.map(_._2)
|
||||
protected[tilelink2] def inputs = iPorts.map(_._2)
|
||||
case class TLIdentityNode() extends IdentityNode(TLImp)
|
||||
case class TLOutputNode() extends OutputNode(TLImp)
|
||||
case class TLInputNode() extends InputNode(TLImp)
|
||||
|
||||
private def reqE(o: Int, i: Int) = require(i == o, s"${name} has ${i} inputs and ${o} outputs; they must match${lazyModule.line}")
|
||||
protected[tilelink2] lazy val oParams: Seq[DO] = {
|
||||
val o = dFn(oPorts.size, iPorts.map { case (i, n) => n.oParams(i) })
|
||||
reqE(oPorts.size, o.size)
|
||||
o.map(outer.mixO(_, this))
|
||||
}
|
||||
protected[tilelink2] lazy val iParams: Seq[UI] = {
|
||||
val i = uFn(iPorts.size, oPorts.map { case (o, n) => n.iParams(o) })
|
||||
reqE(i.size, iPorts.size)
|
||||
i.map(inner.mixI(_, this))
|
||||
case class TLClientNode(portParams: TLClientPortParameters, numPorts: Range.Inclusive = 1 to 1)
|
||||
extends SourceNode(TLImp)(portParams, numPorts)
|
||||
case class TLManagerNode(portParams: TLManagerPortParameters, numPorts: Range.Inclusive = 1 to 1)
|
||||
extends SinkNode(TLImp)(portParams, numPorts)
|
||||
|
||||
object TLClientNode
|
||||
{
|
||||
def apply(params: TLClientParameters) =
|
||||
new TLClientNode(TLClientPortParameters(Seq(params)), 1 to 1)
|
||||
}
|
||||
|
||||
object TLManagerNode
|
||||
{
|
||||
def apply(beatBytes: Int, params: TLManagerParameters) =
|
||||
new TLManagerNode(TLManagerPortParameters(Seq(params), beatBytes, 0), 1 to 1)
|
||||
}
|
||||
|
||||
case class TLAdapterNode(
|
||||
clientFn: Seq[TLClientPortParameters] => TLClientPortParameters,
|
||||
managerFn: Seq[TLManagerPortParameters] => TLManagerPortParameters,
|
||||
numClientPorts: Range.Inclusive = 1 to 1,
|
||||
numManagerPorts: Range.Inclusive = 1 to 1)
|
||||
extends InteriorNode(TLImp)(clientFn, managerFn, numClientPorts, numManagerPorts)
|
||||
|
||||
/** Synthesizeable unit tests */
|
||||
import unittest._
|
||||
|
||||
class TLInputNodeTest extends UnitTest(500000) {
|
||||
class Acceptor extends LazyModule {
|
||||
val node = TLInputNode()
|
||||
val tlram = LazyModule(new TLRAM(AddressSet(0x54321000, 0xfff)))
|
||||
tlram.node := node
|
||||
|
||||
lazy val module = new LazyModuleImp(this) {
|
||||
val io = new Bundle {
|
||||
val in = node.bundleIn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lazy val edgesOut = (oPorts zip oParams).map { case ((i, n), o) => outer.edgeO(o, n.iParams(i)) }
|
||||
lazy val edgesIn = (iPorts zip iParams).map { case ((o, n), i) => inner.edgeI(n.oParams(o), i) }
|
||||
val fuzzer = LazyModule(new TLFuzzer(5000))
|
||||
LazyModule(new Acceptor).node := TLFragmenter(4, 64)(fuzzer.node)
|
||||
|
||||
lazy val bundleOut = outer.bundleO(edgesOut)
|
||||
lazy val bundleIn = inner.bundleI(edgesIn)
|
||||
io.finished := Module(fuzzer.module).io.finished
|
||||
}
|
||||
|
||||
def oConnect = bundleOut
|
||||
def iConnect = bundleIn
|
||||
|
||||
// connects the outward part of a node with the inward part of this node
|
||||
def := (y: OutwardNode[DI, UI, BI])(implicit sourceInfo: SourceInfo): Option[LazyModule] = {
|
||||
val x = this // x := y
|
||||
val info = sourceLine(sourceInfo, " at ", "")
|
||||
require (!LazyModule.stack.isEmpty, s"${y.name} cannot be connected to ${x.name} outside of LazyModule scope" + info)
|
||||
val i = x.iPushed
|
||||
val o = y.oPushed
|
||||
y.oPush(i, x)
|
||||
x.iPush(o, y)
|
||||
val (out, binding) = inner.connect(y.oConnect(o), x.iConnect(i), x.edgesIn(i))
|
||||
LazyModule.stack.head.bindings = binding :: LazyModule.stack.head.bindings
|
||||
out
|
||||
object TLAsyncImp extends NodeImp[TLAsyncClientPortParameters, TLAsyncManagerPortParameters, TLAsyncEdgeParameters, TLAsyncEdgeParameters, TLAsyncBundle]
|
||||
{
|
||||
def edgeO(pd: TLAsyncClientPortParameters, pu: TLAsyncManagerPortParameters): TLAsyncEdgeParameters = TLAsyncEdgeParameters(pd, pu)
|
||||
def edgeI(pd: TLAsyncClientPortParameters, pu: TLAsyncManagerPortParameters): TLAsyncEdgeParameters = TLAsyncEdgeParameters(pd, pu)
|
||||
def bundleO(eo: Seq[TLAsyncEdgeParameters]): Vec[TLAsyncBundle] = {
|
||||
require (eo.size == 1)
|
||||
Vec(eo.size, new TLAsyncBundle(eo(0).bundle))
|
||||
}
|
||||
def bundleI(ei: Seq[TLAsyncEdgeParameters]): Vec[TLAsyncBundle] = {
|
||||
require (ei.size == 1)
|
||||
Vec(ei.size, new TLAsyncBundle(ei(0).bundle)).flip
|
||||
}
|
||||
|
||||
def colour = "#ff0000" // red
|
||||
def connect(bo: => TLAsyncBundle, bi: => TLAsyncBundle, ei: => TLAsyncEdgeParameters)(implicit sourceInfo: SourceInfo): (Option[LazyModule], () => Unit) = {
|
||||
(None, () => { bi <> bo })
|
||||
}
|
||||
|
||||
override def mixO(pd: TLAsyncClientPortParameters, node: OutwardNode[TLAsyncClientPortParameters, TLAsyncManagerPortParameters, TLAsyncBundle]): TLAsyncClientPortParameters =
|
||||
pd.copy(base = pd.base.copy(clients = pd.base.clients.map { c => c.copy (nodePath = node +: c.nodePath) }))
|
||||
override def mixI(pu: TLAsyncManagerPortParameters, node: InwardNode[TLAsyncClientPortParameters, TLAsyncManagerPortParameters, TLAsyncBundle]): TLAsyncManagerPortParameters =
|
||||
pu.copy(base = pu.base.copy(managers = pu.base.managers.map { m => m.copy (nodePath = node +: m.nodePath) }))
|
||||
}
|
||||
|
||||
class SimpleNode[D, U, EO, EI, B <: Data](imp: NodeImp[D, U, EO, EI, B])(
|
||||
oFn: (Int, Seq[D]) => Seq[D],
|
||||
iFn: (Int, Seq[U]) => Seq[U],
|
||||
numPO: Range.Inclusive,
|
||||
numPI: Range.Inclusive)
|
||||
extends MixedNode[D, U, EI, B, D, U, EO, B](imp, imp)(oFn, iFn, numPO, numPI)
|
||||
case class TLAsyncIdentityNode() extends IdentityNode(TLAsyncImp)
|
||||
case class TLAsyncOutputNode() extends OutputNode(TLAsyncImp)
|
||||
case class TLAsyncInputNode() extends InputNode(TLAsyncImp)
|
||||
|
||||
class IdentityNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])
|
||||
extends SimpleNode(imp)({case (_, s) => s}, {case (_, s) => s}, 0 to 999, 0 to 999)
|
||||
case class TLAsyncSourceNode() extends MixedNode(TLImp, TLAsyncImp)(
|
||||
dFn = { case (1, s) => s.map(TLAsyncClientPortParameters(_)) },
|
||||
uFn = { case (1, s) => s.map(_.base) },
|
||||
numPO = 1 to 1,
|
||||
numPI = 1 to 1)
|
||||
|
||||
class OutputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp)
|
||||
{
|
||||
override def oConnect = bundleOut
|
||||
override def iConnect = bundleOut
|
||||
}
|
||||
|
||||
class InputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp)
|
||||
{
|
||||
override def oConnect = bundleIn
|
||||
override def iConnect = bundleIn
|
||||
}
|
||||
|
||||
class SourceNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])(po: PO, num: Range.Inclusive = 1 to 1)
|
||||
extends SimpleNode(imp)({case (n, Seq()) => Seq.fill(n)(po)}, {case (0, _) => Seq()}, num, 0 to 0)
|
||||
{
|
||||
require (num.end >= 1, s"${name} is a source which does not accept outputs${lazyModule.line}")
|
||||
}
|
||||
|
||||
class SinkNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])(pi: PI, num: Range.Inclusive = 1 to 1)
|
||||
extends SimpleNode(imp)({case (0, _) => Seq()}, {case (n, Seq()) => Seq.fill(n)(pi)}, 0 to 0, num)
|
||||
{
|
||||
require (num.end >= 1, s"${name} is a sink which does not accept inputs${lazyModule.line}")
|
||||
}
|
||||
|
||||
class InteriorNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])
|
||||
(oFn: Seq[PO] => PO, iFn: Seq[PI] => PI, numPO: Range.Inclusive, numPI: Range.Inclusive)
|
||||
extends SimpleNode(imp)({case (n,s) => Seq.fill(n)(oFn(s))}, {case (n,s) => Seq.fill(n)(iFn(s))}, numPO, numPI)
|
||||
{
|
||||
require (numPO.end >= 1, s"${name} is an adapter which does not accept outputs${lazyModule.line}")
|
||||
require (numPI.end >= 1, s"${name} is an adapter which does not accept inputs${lazyModule.line}")
|
||||
}
|
||||
case class TLAsyncSinkNode(depth: Int) extends MixedNode(TLAsyncImp, TLImp)(
|
||||
dFn = { case (1, s) => s.map(_.base) },
|
||||
uFn = { case (1, s) => s.map(TLAsyncManagerPortParameters(depth, _)) },
|
||||
numPO = 1 to 1,
|
||||
numPI = 1 to 1)
|
||||
|
Reference in New Issue
Block a user