From 53987cd9d44f4e52af848000182c77cad318f5a2 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 11:30:04 -0700 Subject: [PATCH 1/8] tilelink2 Nodes: support non-Bundle data for io type --- src/main/scala/uncore/tilelink2/LazyModule.scala | 2 +- src/main/scala/uncore/tilelink2/Nodes.scala | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/LazyModule.scala b/src/main/scala/uncore/tilelink2/LazyModule.scala index 8504e711..5937b363 100644 --- a/src/main/scala/uncore/tilelink2/LazyModule.scala +++ b/src/main/scala/uncore/tilelink2/LazyModule.scala @@ -16,7 +16,7 @@ abstract class LazyModule parent.foreach(p => p.children = this :: p.children) // Use as: connect(source -> sink, source2 -> sink2, ...) - def connect[PO, PI, EO, EI, B <: Bundle](edges: (BaseNode[PO, PI, EO, EI, B], BaseNode[PO, PI, EO, EI, B])*)(implicit sourceInfo: SourceInfo) = { + def connect[PO, PI, EO, EI, B <: Data](edges: (BaseNode[PO, PI, EO, EI, B], BaseNode[PO, PI, EO, EI, B])*)(implicit sourceInfo: SourceInfo) = { edges.foreach { case (source, sink) => bindings = (source edge sink) :: bindings } diff --git a/src/main/scala/uncore/tilelink2/Nodes.scala b/src/main/scala/uncore/tilelink2/Nodes.scala index 1679c17b..0dc8af9e 100644 --- a/src/main/scala/uncore/tilelink2/Nodes.scala +++ b/src/main/scala/uncore/tilelink2/Nodes.scala @@ -10,7 +10,7 @@ import chisel3.internal.sourceinfo.SourceInfo // PO = PortOutputParameters // EI = EdgeInput // EO = EdgeOutput -abstract class NodeImp[PO, PI, EO, EI, B <: Bundle] +abstract class NodeImp[PO, PI, EO, EI, B <: Data] { def edgeO(po: PO, pi: PI): EO def edgeI(po: PO, pi: PI): EI @@ -19,7 +19,7 @@ abstract class NodeImp[PO, PI, EO, EI, B <: Bundle] def connect(bo: B, eo: EO, bi: B, ei: EI)(implicit sourceInfo: SourceInfo): Unit } -class BaseNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B])( +class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( private val oFn: Option[Seq[PO] => PO], private val iFn: Option[Seq[PI] => PI], private val numPO: Range.Inclusive, @@ -72,34 +72,34 @@ class BaseNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B])( } } -class IdentityNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B]) +class IdentityNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends BaseNode(imp)(Some{case Seq(x) => x}, Some{case Seq(x) => x}, 1 to 1, 1 to 1) -class OutputNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp) +class OutputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp) { override def connectOut = bundleOut override def connectIn = bundleOut } -class InputNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp) +class InputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp) { override def connectOut = bundleIn override def connectIn = bundleIn } -class SourceNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B])(po: PO, num: Range.Inclusive = 1 to 1) +class SourceNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])(po: PO, num: Range.Inclusive = 1 to 1) extends BaseNode(imp)(Some{case Seq() => po}, None, num, 0 to 0) { require (num.end >= 1) } -class SinkNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B])(pi: PI, num: Range.Inclusive = 1 to 1) +class SinkNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])(pi: PI, num: Range.Inclusive = 1 to 1) extends BaseNode(imp)(None, Some{case Seq() => pi}, 0 to 0, num) { require (num.end >= 1) } -class InteriorNode[PO, PI, EO, EI, B <: Bundle](imp: NodeImp[PO, PI, EO, EI, B]) +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 BaseNode(imp)(Some(oFn), Some(iFn), numPO, numPI) { From d7df7d3109f0bfd4c42020f915349f24ec3cfe4f Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 14:41:08 -0700 Subject: [PATCH 2/8] tilelink2: connect Nodes to LazyModules for better error messages --- .../scala/uncore/tilelink2/LazyModule.scala | 11 +++++++++-- src/main/scala/uncore/tilelink2/Nodes.scala | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/LazyModule.scala b/src/main/scala/uncore/tilelink2/LazyModule.scala index 5937b363..92c6e0eb 100644 --- a/src/main/scala/uncore/tilelink2/LazyModule.scala +++ b/src/main/scala/uncore/tilelink2/LazyModule.scala @@ -3,12 +3,13 @@ package uncore.tilelink2 import Chisel._ -import chisel3.internal.sourceinfo._ +import chisel3.internal.sourceinfo.{SourceInfo, SourceLine, UnlocatableSourceInfo} abstract class LazyModule { protected[tilelink2] var bindings = List[() => Unit]() protected[tilelink2] var children = List[LazyModule]() + protected[tilelink2] var nodes = List[RootNode]() protected[tilelink2] var info: SourceInfo = UnlocatableSourceInfo protected[tilelink2] val parent = LazyModule.stack.headOption @@ -22,6 +23,12 @@ abstract class LazyModule } } + def name = getClass.getName.split('.').last + def line = info match { + case SourceLine(filename, line, col) => s" ($filename:$line:$col)" + case _ => "" + } + def module: LazyModuleImp implicit val lazyModule = this @@ -55,6 +62,6 @@ abstract class LazyModuleImp(outer: LazyModule) extends Module // .module had better not be accessed while LazyModules are still being built! require (LazyModule.stack.isEmpty) - override def desiredName = outer.getClass.getName.split('.').last + override def desiredName = outer.name outer.instantiate() } diff --git a/src/main/scala/uncore/tilelink2/Nodes.scala b/src/main/scala/uncore/tilelink2/Nodes.scala index 0dc8af9e..a2231bbd 100644 --- a/src/main/scala/uncore/tilelink2/Nodes.scala +++ b/src/main/scala/uncore/tilelink2/Nodes.scala @@ -19,11 +19,20 @@ abstract class NodeImp[PO, PI, EO, EI, B <: Data] def connect(bo: B, eo: EO, bi: B, ei: EI)(implicit sourceInfo: SourceInfo): Unit } +class RootNode +{ + // You cannot create a Node outside a LazyModule! + require (!LazyModule.stack.isEmpty) + + val lazyModule = LazyModule.stack.head + lazyModule.nodes = this :: lazyModule.nodes +} + class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( private val oFn: Option[Seq[PO] => PO], private val iFn: Option[Seq[PI] => PI], private val numPO: Range.Inclusive, - private val numPI: Range.Inclusive) + private val numPI: Range.Inclusive) extends RootNode { // At least 0 ports must be supported require (!numPO.isEmpty) @@ -42,8 +51,12 @@ class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( private var oRealized = false private var iRealized = false - private lazy val oPorts = { oRealized = true; require (numPO.contains(accPO.size)); accPO.result() } - private lazy val iPorts = { iRealized = true; require (numPI.contains(accPI.size)); accPI.result() } + def name = lazyModule.name + "." + getClass.getName.split('.').last + private def reqO() = require(numPO.contains(accPO.size), s"${name} has ${accPO.size} outputs, expected ${numPO}${lazyModule.line}") + private def reqI() = require(numPI.contains(accPI.size), s"${name} has ${accPI.size} inputs, expected ${numPI}${lazyModule.line}") + + private lazy val oPorts = { oRealized = true; reqO(); accPO.result() } + private lazy val iPorts = { iRealized = true; reqI(); accPI.result() } private lazy val oParams : Option[PO] = oFn.map(_(iPorts.map(_.oParams.get))) private lazy val iParams : Option[PI] = iFn.map(_(oPorts.map(_.iParams.get))) From 23e896ed5dabef578c0c0155641095cfac56cb0a Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 11:30:35 -0700 Subject: [PATCH 3/8] tilelink2 IntNodes: support interrupt graphs --- .../scala/uncore/tilelink2/IntNodes.scala | 72 +++++++++++++++++++ src/main/scala/uncore/tilelink2/package.scala | 1 + 2 files changed, 73 insertions(+) create mode 100644 src/main/scala/uncore/tilelink2/IntNodes.scala diff --git a/src/main/scala/uncore/tilelink2/IntNodes.scala b/src/main/scala/uncore/tilelink2/IntNodes.scala new file mode 100644 index 00000000..fb35a49f --- /dev/null +++ b/src/main/scala/uncore/tilelink2/IntNodes.scala @@ -0,0 +1,72 @@ +// See LICENSE for license details. + +package uncore.tilelink2 + +import Chisel._ +import scala.collection.mutable.ListBuffer +import scala.math.max +import chisel3.internal.sourceinfo.SourceInfo + +case class IntSourceParameters(device: String, num: Int) + +case class IntSinkPortParameters() +case class IntSourcePortParameters(sources: Seq[IntSourceParameters]) +{ + val num = sources.map(_.num).sum +} +case class IntEdge(source: IntSourcePortParameters, sink: IntSinkPortParameters) + +object IntImp extends NodeImp[IntSourcePortParameters, IntSinkPortParameters, IntEdge, IntEdge, Vec[Bool]] +{ + def edgeO(po: IntSourcePortParameters, pi: IntSinkPortParameters): IntEdge = IntEdge(po, pi) + def edgeI(po: IntSourcePortParameters, pi: IntSinkPortParameters): IntEdge = IntEdge(po, pi) + def bundleO(eo: Seq[IntEdge]): Vec[Vec[Bool]] = { + if (eo.isEmpty) Vec(0, Vec(0, Bool())) else + Vec(eo.size, Vec(eo.map(_.source.num).max, Bool())) + } + def bundleI(ei: Seq[IntEdge]): Vec[Vec[Bool]] = { + require (!ei.isEmpty) + Vec(ei.size, Vec(ei.map(_.source.num).max, Bool())).flip + } + + def connect(bo: Vec[Bool], eo: IntEdge, bi: Vec[Bool], ei: IntEdge)(implicit sourceInfo: SourceInfo): Unit = { + require (eo == ei) + // Cannot use bulk connect, because the widths could differ + (bo zip bi) foreach { case (o, i) => i := o } + } +} + +case class IntIdentityNode() extends IdentityNode(IntImp) +case class IntOutputNode() extends OutputNode(IntImp) +case class IntInputNode() extends InputNode(IntImp) + +case class IntSourceNode(device: String, num: Int) extends SourceNode(IntImp)( + IntSourcePortParameters(Seq(IntSourceParameters(device, num))), + (if (num == 0) 0 else 1) to 1) +case class IntSinkNode() extends SinkNode(IntImp)(IntSinkPortParameters()) + +case class IntAdapterNode( + sourceFn: Seq[IntSourcePortParameters] => IntSourcePortParameters, + sinkFn: Seq[IntSinkPortParameters] => IntSinkPortParameters, + numSourcePorts: Range.Inclusive = 1 to 1, + numSinkPorts: Range.Inclusive = 1 to 1) + extends InteriorNode(IntImp)(sourceFn, sinkFn, numSourcePorts, numSinkPorts) + +class IntXbar extends LazyModule +{ + val intnode = IntAdapterNode( + numSourcePorts = 1 to 1, // does it make sense to have more than one interrupt sink? + numSinkPorts = 1 to 128, + sourceFn = { seq => IntSourcePortParameters(seq.map(_.sources).flatten) }, + sinkFn = { _ => IntSinkPortParameters() }) + + lazy val module = new LazyModuleImp(this) { + val io = new Bundle { + val in = intnode.bundleIn + val out = intnode.bundleOut + } + + val cat = (intnode.edgesIn zip io.in).map{ case (e, i) => i.take(e.source.num) }.flatten + io.out.foreach { _ := cat } + } +} diff --git a/src/main/scala/uncore/tilelink2/package.scala b/src/main/scala/uncore/tilelink2/package.scala index b1e3dda2..cb78a633 100644 --- a/src/main/scala/uncore/tilelink2/package.scala +++ b/src/main/scala/uncore/tilelink2/package.scala @@ -5,6 +5,7 @@ import Chisel._ package object tilelink2 { type TLBaseNode = BaseNode[TLClientPortParameters, TLManagerPortParameters, TLEdgeOut, TLEdgeIn, TLBundle] + type IntBaseNode = BaseNode[IntSourcePortParameters, IntSinkPortParameters, IntEdge, IntEdge, Vec[Bool]] def OH1ToUInt(x: UInt) = OHToUInt((x << 1 | UInt(1)) ^ x) def UIntToOH1(x: UInt, width: Int) = ~(SInt(-1, width=width).asUInt << x)(width-1, 0) def trailingZeros(x: Int) = if (x > 0) Some(log2Ceil(x & -x)) else None From 66f58cf2d0f646576069ad5e380029937a0447d4 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 15:17:30 -0700 Subject: [PATCH 4/8] tilelink2 RegisterRouter: support new TL2 interrupts --- src/main/scala/uncore/tilelink2/GPIO.scala | 14 +++++++--- .../scala/uncore/tilelink2/RegField.scala | 1 + .../uncore/tilelink2/RegisterRouter.scala | 26 +++++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/GPIO.scala b/src/main/scala/uncore/tilelink2/GPIO.scala index a9050882..6b4e1e4b 100644 --- a/src/main/scala/uncore/tilelink2/GPIO.scala +++ b/src/main/scala/uncore/tilelink2/GPIO.scala @@ -16,14 +16,22 @@ trait GPIOModule extends HasRegMap { val params: GPIOParams val io: GPIOBundle + val interrupts: Vec[Bool] val state = RegInit(UInt(0)) - io.gpio := state + val pending = RegInit(UInt(0xf, width = 4)) - regmap(0 -> Seq(RegField(params.num, state))) + io.gpio := state + interrupts := pending.toBools + + regmap( + 0 -> Seq( + RegField(params.num, state)), + 1 -> Seq( + RegField.w1ToClear(4, pending, state))) } // Create a concrete TL2 version of the abstract GPIO slave -class TLGPIO(p: GPIOParams) extends TLRegisterRouter(p.address)( +class TLGPIO(p: GPIOParams) extends TLRegisterRouter(p.address, 4)( new TLRegBundle(p, _) with GPIOBundle)( new TLRegModule(p, _, _) with GPIOModule) diff --git a/src/main/scala/uncore/tilelink2/RegField.scala b/src/main/scala/uncore/tilelink2/RegField.scala index 32a65c0d..9c324534 100644 --- a/src/main/scala/uncore/tilelink2/RegField.scala +++ b/src/main/scala/uncore/tilelink2/RegField.scala @@ -86,6 +86,7 @@ object RegField trait HasRegMap { def regmap(mapping: RegField.Map*): Unit + val interrupts: Vec[Bool] } // See GPIO.scala for an example of how to use regmap diff --git a/src/main/scala/uncore/tilelink2/RegisterRouter.scala b/src/main/scala/uncore/tilelink2/RegisterRouter.scala index e455efda..a3b61da7 100644 --- a/src/main/scala/uncore/tilelink2/RegisterRouter.scala +++ b/src/main/scala/uncore/tilelink2/RegisterRouter.scala @@ -75,29 +75,39 @@ object TLRegisterNode // register mapped device from a totally abstract register mapped device. // See GPIO.scala in this directory for an example -abstract class TLRegisterRouterBase(address: AddressSet, concurrency: Option[Int], beatBytes: Int) extends LazyModule +abstract class TLRegisterRouterBase(address: AddressSet, interrupts: Int, concurrency: Option[Int], beatBytes: Int) extends LazyModule { val node = TLRegisterNode(address, concurrency, beatBytes) + val intnode = IntSourceNode(name + s" @ ${address.base}", interrupts) } -class TLRegBundle[P](val params: P, val in: Vec[TLBundle]) extends Bundle +case class TLRegBundleArg(interrupts: Vec[Vec[Bool]], in: Vec[TLBundle]) -class TLRegModule[P, B <: Bundle](val params: P, bundleBuilder: => B, router: TLRegisterRouterBase) +class TLRegBundleBase(arg: TLRegBundleArg) extends Bundle +{ + val interrupts = arg.interrupts + val in = arg.in +} + +class TLRegBundle[P](val params: P, arg: TLRegBundleArg) extends TLRegBundleBase(arg) + +class TLRegModule[P, B <: TLRegBundleBase](val params: P, bundleBuilder: => B, router: TLRegisterRouterBase) extends LazyModuleImp(router) with HasRegMap { val io = bundleBuilder + val interrupts = if (io.interrupts.isEmpty) Vec(0, Bool()) else io.interrupts(0) def regmap(mapping: RegField.Map*) = router.node.regmap(mapping:_*) } -class TLRegisterRouter[B <: Bundle, M <: LazyModuleImp] - (base: BigInt, size: BigInt = 4096, concurrency: Option[Int] = None, beatBytes: Int = 4) - (bundleBuilder: Vec[TLBundle] => B) +class TLRegisterRouter[B <: TLRegBundleBase, M <: LazyModuleImp] + (base: BigInt, interrupts: Int = 0, size: BigInt = 4096, concurrency: Option[Int] = None, beatBytes: Int = 4) + (bundleBuilder: TLRegBundleArg => B) (moduleBuilder: (=> B, TLRegisterRouterBase) => M) - extends TLRegisterRouterBase(AddressSet(base, size-1), concurrency, beatBytes) + extends TLRegisterRouterBase(AddressSet(base, size-1), interrupts, concurrency, beatBytes) { require (size % 4096 == 0) // devices should be 4K aligned require (isPow2(size)) require (size >= 4096) - lazy val module = moduleBuilder(bundleBuilder(node.bundleIn), this) + lazy val module = moduleBuilder(bundleBuilder(TLRegBundleArg(intnode.bundleOut, node.bundleIn)), this) } From 1b07d53f70c506f6607fe021f7b12beb85a53fdb Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 18:51:43 -0700 Subject: [PATCH 5/8] tilelink2 IntNodes: record interrupt ranges in parameters --- .../scala/uncore/tilelink2/IntNodes.scala | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/IntNodes.scala b/src/main/scala/uncore/tilelink2/IntNodes.scala index fb35a49f..df3e6152 100644 --- a/src/main/scala/uncore/tilelink2/IntNodes.scala +++ b/src/main/scala/uncore/tilelink2/IntNodes.scala @@ -7,12 +7,30 @@ import scala.collection.mutable.ListBuffer import scala.math.max import chisel3.internal.sourceinfo.SourceInfo -case class IntSourceParameters(device: String, num: Int) +// A potentially empty half-open range; [start, end) +case class IntRange(start: Int, end: Int) +{ + require (start >= 0) + require (start <= end) + def size = end - start + def overlaps(x: IntRange) = start < x.end && x.start < end + def offset(x: Int) = IntRange(x+start, x+end) +} +object IntRange +{ + implicit def apply(end: Int): IntRange = apply(0, end) +} + +case class IntSourceParameters(device: String, range: IntRange) case class IntSinkPortParameters() case class IntSourcePortParameters(sources: Seq[IntSourceParameters]) { - val num = sources.map(_.num).sum + val num = sources.map(_.range.size).sum + // The interrupts mapping must not overlap + sources.map(_.range).combinations(2).foreach { case Seq(a, b) => require (!a.overlaps(b)) } + // The interrupts must perfectly cover the range + require (sources.map(_.range.end).max == num) } case class IntEdge(source: IntSourcePortParameters, sink: IntSinkPortParameters) @@ -57,8 +75,12 @@ class IntXbar extends LazyModule val intnode = IntAdapterNode( numSourcePorts = 1 to 1, // does it make sense to have more than one interrupt sink? numSinkPorts = 1 to 128, - sourceFn = { seq => IntSourcePortParameters(seq.map(_.sources).flatten) }, - sinkFn = { _ => IntSinkPortParameters() }) + sinkFn = { _ => IntSinkPortParameters() }, + sourceFn = { seq => + IntSourcePortParameters((seq zip seq.map(_.num).scanLeft(0)(_+_).init).map { + case (s, o) => s.sources.map(z => z.copy(range = z.range.offset(o))) + }.flatten) + }) lazy val module = new LazyModuleImp(this) { val io = new Bundle { From 808a7f60f449ef2d5d59794a7a5e1fdfc3103247 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 21:09:40 -0700 Subject: [PATCH 6/8] tilelink2 Legacy: it's only an error if it's valid (#264) --- src/main/scala/uncore/tilelink2/Legacy.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/uncore/tilelink2/Legacy.scala b/src/main/scala/uncore/tilelink2/Legacy.scala index d21503cb..99ccc2fe 100644 --- a/src/main/scala/uncore/tilelink2/Legacy.scala +++ b/src/main/scala/uncore/tilelink2/Legacy.scala @@ -107,7 +107,7 @@ class TLLegacy(implicit val p: Parameters) extends LazyModule with HasTileLinkPa out.a.bits.addr_hi := ~(~address | addressMask) >> log2Ceil(tlDataBytes) // TL legacy does not support bus errors - assert (!out.d.bits.error) + assert (!out.d.valid || !out.d.bits.error) // Recreate the beat address counter val beatCounter = RegInit(UInt(0, width = tlBeatAddrBits)) From b587a409a37bfdc26d6eabcd385a7c4d21392946 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 21:11:31 -0700 Subject: [PATCH 7/8] tilelink2 Node: make it possible for {Identity,Output,Input}Node to pass a Vec In order to implement a pass-through RAM Monitor model, we will want to support a variable number of inputs and outputs with BOTH different manager and client parameters on each bundle. --- .../scala/uncore/tilelink2/LazyModule.scala | 10 ++- src/main/scala/uncore/tilelink2/Nodes.scala | 68 +++++++++++-------- src/main/scala/uncore/tilelink2/package.scala | 6 ++ 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/LazyModule.scala b/src/main/scala/uncore/tilelink2/LazyModule.scala index 92c6e0eb..48fffb48 100644 --- a/src/main/scala/uncore/tilelink2/LazyModule.scala +++ b/src/main/scala/uncore/tilelink2/LazyModule.scala @@ -24,10 +24,7 @@ abstract class LazyModule } def name = getClass.getName.split('.').last - def line = info match { - case SourceLine(filename, line, col) => s" ($filename:$line:$col)" - case _ => "" - } + def line = sourceLine(info) def module: LazyModuleImp implicit val lazyModule = this @@ -50,7 +47,8 @@ object LazyModule // Make sure the user put LazyModule around modules in the correct order // If this require fails, probably some grandchild was missing a LazyModule // ... or you applied LazyModule twice - require (!stack.isEmpty && (stack.head eq bc)) + require (!stack.isEmpty, s"LazyModule() applied to ${bc.name} twice ${sourceLine(sourceInfo)}") + require (stack.head eq bc, s"LazyModule() applied to ${bc.name} before ${stack.head.name} ${sourceLine(sourceInfo)}") stack = stack.tail bc.info = sourceInfo bc @@ -60,7 +58,7 @@ object LazyModule abstract class LazyModuleImp(outer: LazyModule) extends Module { // .module had better not be accessed while LazyModules are still being built! - require (LazyModule.stack.isEmpty) + require (LazyModule.stack.isEmpty, s"${outer.name}.module was constructed before LazyModule() was run on ${LazyModule.stack.head.name}") override def desiredName = outer.name outer.instantiate() diff --git a/src/main/scala/uncore/tilelink2/Nodes.scala b/src/main/scala/uncore/tilelink2/Nodes.scala index a2231bbd..9d9a8aed 100644 --- a/src/main/scala/uncore/tilelink2/Nodes.scala +++ b/src/main/scala/uncore/tilelink2/Nodes.scala @@ -29,39 +29,46 @@ class RootNode } class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( - private val oFn: Option[Seq[PO] => PO], - private val iFn: Option[Seq[PI] => PI], + private val oFn: (Int, Seq[PO]) => Seq[PO], + private val iFn: (Int, Seq[PI]) => Seq[PI], private val numPO: Range.Inclusive, private val numPI: Range.Inclusive) extends RootNode { // At least 0 ports must be supported - require (!numPO.isEmpty) - require (!numPI.isEmpty) - require (numPO.start >= 0) - require (numPI.start >= 0) + def name = lazyModule.name + "." + getClass.getName.split('.').last + require (!numPO.isEmpty, s"No number of outputs would be acceptable to ${name}${lazyModule.line}") + require (!numPI.isEmpty, s"No number of inputs would be acceptable to ${name}${lazyModule.line}") + require (numPO.start >= 0, s"${name} accepts a negative number of outputs${lazyModule.line}") + require (numPI.start >= 0, s"${name} accepts a negative number of inputs${lazyModule.line}") val noOs = numPO.size == 1 && numPO.contains(0) val noIs = numPI.size == 1 && numPI.contains(0) - require (noOs || oFn.isDefined) - require (noIs || iFn.isDefined) - - private val accPO = ListBuffer[BaseNode[PO, PI, EO, EI, B]]() - private val accPI = ListBuffer[BaseNode[PO, PI, EO, EI, B]]() + private val accPO = ListBuffer[(Int, BaseNode[PO, PI, EO, EI, B])]() + private val accPI = ListBuffer[(Int, BaseNode[PO, PI, EO, EI, B])]() private var oRealized = false private var iRealized = false - def name = lazyModule.name + "." + getClass.getName.split('.').last private def reqO() = require(numPO.contains(accPO.size), s"${name} has ${accPO.size} outputs, expected ${numPO}${lazyModule.line}") private def reqI() = require(numPI.contains(accPI.size), s"${name} has ${accPI.size} inputs, expected ${numPI}${lazyModule.line}") + protected def reqE(o: Int, i: Int) = require(i == o, s"${name} has ${i} inputs and ${o} outputs; they must match${lazyModule.line}") private lazy val oPorts = { oRealized = true; reqO(); accPO.result() } private lazy val iPorts = { iRealized = true; reqI(); accPI.result() } - private lazy val oParams : Option[PO] = oFn.map(_(iPorts.map(_.oParams.get))) - private lazy val iParams : Option[PI] = iFn.map(_(oPorts.map(_.iParams.get))) - lazy val edgesOut = oPorts.map { n => imp.edgeO(oParams.get, n.iParams.get) } - lazy val edgesIn = iPorts.map { n => imp.edgeI(n.oParams.get, iParams.get) } + private lazy val oParams : Seq[PO] = { + val o = oFn(oPorts.size, iPorts.map{ case (i, n) => n.oParams(i) }) + reqE(oPorts.size, o.size) + o + } + private lazy val iParams : Seq[PI] = { + val i = iFn(iPorts.size, oPorts.map{ case (o, n) => n.iParams(o) }) + reqE(i.size, iPorts.size) + i + } + + lazy val edgesOut = (oPorts zip oParams).map { case ((i, n), o) => imp.edgeO(o, n.iParams(i)) } + lazy val edgesIn = (iPorts zip iParams).map { case ((o, n), i) => imp.edgeI(n.oParams(o), i) } lazy val bundleOut = imp.bundleO(edgesOut) lazy val bundleIn = imp.bundleI(edgesIn) @@ -71,14 +78,15 @@ class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( // source.edge(sink) protected[tilelink2] def edge(x: BaseNode[PO, PI, EO, EI, B])(implicit sourceInfo: SourceInfo) = { - require (!noOs) - require (!oRealized) - require (!x.noIs) - require (!x.iRealized) + val info = sourceLine(sourceInfo, " at ", "") + 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) + require (!x.noIs, s"${x.name}${x.lazyModule.line} was incorrectly connected as a sink" + info) + require (!x.iRealized, s"${x.name}${x.lazyModule.line} was incorrectly connected as a sink after it's .module was used" + info) val i = x.accPI.size val o = accPO.size - accPO += x - x.accPI += this + accPO += ((i, x)) + x.accPI += ((o, this)) () => { imp.connect(connectOut(o), edgesOut(o), x.connectIn(i), x.edgesIn(i)) } @@ -86,7 +94,7 @@ class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( } class IdentityNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) - extends BaseNode(imp)(Some{case Seq(x) => x}, Some{case Seq(x) => x}, 1 to 1, 1 to 1) + extends BaseNode(imp)({case (_, s) => s}, {case (_, s) => s}, 0 to 999, 0 to 999) class OutputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) extends IdentityNode(imp) { @@ -101,21 +109,21 @@ class InputNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B]) exte } class SourceNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])(po: PO, num: Range.Inclusive = 1 to 1) - extends BaseNode(imp)(Some{case Seq() => po}, None, num, 0 to 0) + extends BaseNode(imp)({case (n, Seq()) => Seq.fill(n)(po)}, {case (0, _) => Seq()}, num, 0 to 0) { - require (num.end >= 1) + 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 BaseNode(imp)(None, Some{case Seq() => pi}, 0 to 0, num) + extends BaseNode(imp)({case (0, _) => Seq()}, {case (n, Seq()) => Seq.fill(n)(pi)}, 0 to 0, num) { - require (num.end >= 1) + 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 BaseNode(imp)(Some(oFn), Some(iFn), numPO, numPI) + extends BaseNode(imp)({case (n,s) => Seq.fill(n)(oFn(s))}, {case (n,s) => Seq.fill(n)(iFn(s))}, numPO, numPI) { - require (numPO.end >= 1) - require (numPI.end >= 1) + 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}") } diff --git a/src/main/scala/uncore/tilelink2/package.scala b/src/main/scala/uncore/tilelink2/package.scala index cb78a633..050a4b6d 100644 --- a/src/main/scala/uncore/tilelink2/package.scala +++ b/src/main/scala/uncore/tilelink2/package.scala @@ -1,6 +1,7 @@ package uncore import Chisel._ +import chisel3.internal.sourceinfo.{SourceInfo, SourceLine, UnlocatableSourceInfo} package object tilelink2 { @@ -9,4 +10,9 @@ package object tilelink2 def OH1ToUInt(x: UInt) = OHToUInt((x << 1 | UInt(1)) ^ x) def UIntToOH1(x: UInt, width: Int) = ~(SInt(-1, width=width).asUInt << x)(width-1, 0) def trailingZeros(x: Int) = if (x > 0) Some(log2Ceil(x & -x)) else None + + def sourceLine(sourceInfo: SourceInfo, prefix: String = " (", suffix: String = ")") = sourceInfo match { + case SourceLine(filename, line, col) => s"$prefix$filename:$line:$col$suffix" + case _ => "" + } } From c28ca37944e9be89f136dd289699ed83ae3d5b35 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Thu, 8 Sep 2016 23:06:59 -0700 Subject: [PATCH 8/8] tilelink2: get rid of fragile implicit lazyModule pattern, and support := We can more reliably find the current LazyModule from the LazyModule.stack --- src/main/scala/uncore/tilelink2/Buffer.scala | 6 +++--- .../scala/uncore/tilelink2/Fragmenter.scala | 6 +++--- .../scala/uncore/tilelink2/HintHandler.scala | 6 +++--- .../scala/uncore/tilelink2/LazyModule.scala | 5 +---- src/main/scala/uncore/tilelink2/Nodes.scala | 21 ++++++++++--------- .../scala/uncore/tilelink2/WidthWidget.scala | 6 +++--- 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/scala/uncore/tilelink2/Buffer.scala b/src/main/scala/uncore/tilelink2/Buffer.scala index 26392d2b..84d6c21b 100644 --- a/src/main/scala/uncore/tilelink2/Buffer.scala +++ b/src/main/scala/uncore/tilelink2/Buffer.scala @@ -39,10 +39,10 @@ class TLBuffer(entries: Int = 2, pipe: Boolean = false) extends LazyModule object TLBuffer { - // applied to the TL source node; connect (TLBuffer(x.node) -> y.node) - def apply(x: TLBaseNode, entries: Int = 2, pipe: Boolean = false)(implicit lazyModule: LazyModule, sourceInfo: SourceInfo): TLBaseNode = { + // applied to the TL source node; y.node := TLBuffer(x.node) + def apply(x: TLBaseNode, entries: Int = 2, pipe: Boolean = false)(implicit sourceInfo: SourceInfo): TLBaseNode = { val buffer = LazyModule(new TLBuffer(entries, pipe)) - lazyModule.connect(x -> buffer.node) + buffer.node := x buffer.node } } diff --git a/src/main/scala/uncore/tilelink2/Fragmenter.scala b/src/main/scala/uncore/tilelink2/Fragmenter.scala index 6364f72a..aa0efc5e 100644 --- a/src/main/scala/uncore/tilelink2/Fragmenter.scala +++ b/src/main/scala/uncore/tilelink2/Fragmenter.scala @@ -240,10 +240,10 @@ class TLFragmenter(minSize: Int, maxSize: Int, alwaysMin: Boolean = false) exten object TLFragmenter { - // applied to the TL source node; connect (TLFragmenter(x.node, 256, 4) -> y.node) - def apply(x: TLBaseNode, minSize: Int, maxSize: Int, alwaysMin: Boolean = false)(implicit lazyModule: LazyModule, sourceInfo: SourceInfo): TLBaseNode = { + // applied to the TL source node; y.node := TLFragmenter(x.node, 256, 4) + def apply(x: TLBaseNode, minSize: Int, maxSize: Int, alwaysMin: Boolean = false)(implicit sourceInfo: SourceInfo): TLBaseNode = { val fragmenter = LazyModule(new TLFragmenter(minSize, maxSize, alwaysMin)) - lazyModule.connect(x -> fragmenter.node) + fragmenter.node := x fragmenter.node } } diff --git a/src/main/scala/uncore/tilelink2/HintHandler.scala b/src/main/scala/uncore/tilelink2/HintHandler.scala index 0222991e..b8938fbf 100644 --- a/src/main/scala/uncore/tilelink2/HintHandler.scala +++ b/src/main/scala/uncore/tilelink2/HintHandler.scala @@ -90,10 +90,10 @@ class TLHintHandler(supportManagers: Boolean = true, supportClients: Boolean = f object TLHintHandler { - // applied to the TL source node; connect (TLHintHandler(x.node) -> y.node) - def apply(x: TLBaseNode, supportManagers: Boolean = true, supportClients: Boolean = false, passthrough: Boolean = true)(implicit lazyModule: LazyModule, sourceInfo: SourceInfo): TLBaseNode = { + // applied to the TL source node; y.node := TLHintHandler(x.node) + def apply(x: TLBaseNode, supportManagers: Boolean = true, supportClients: Boolean = false, passthrough: Boolean = true)(implicit sourceInfo: SourceInfo): TLBaseNode = { val hints = LazyModule(new TLHintHandler(supportManagers, supportClients, passthrough)) - lazyModule.connect(x -> hints.node) + hints.node := x hints.node } } diff --git a/src/main/scala/uncore/tilelink2/LazyModule.scala b/src/main/scala/uncore/tilelink2/LazyModule.scala index 48fffb48..37c0e0d9 100644 --- a/src/main/scala/uncore/tilelink2/LazyModule.scala +++ b/src/main/scala/uncore/tilelink2/LazyModule.scala @@ -18,16 +18,13 @@ abstract class LazyModule // Use as: connect(source -> sink, source2 -> sink2, ...) def connect[PO, PI, EO, EI, B <: Data](edges: (BaseNode[PO, PI, EO, EI, B], BaseNode[PO, PI, EO, EI, B])*)(implicit sourceInfo: SourceInfo) = { - edges.foreach { case (source, sink) => - bindings = (source edge sink) :: bindings - } + edges.foreach { case (source, sink) => sink := source } } def name = getClass.getName.split('.').last def line = sourceLine(info) def module: LazyModuleImp - implicit val lazyModule = this protected[tilelink2] def instantiate() = { children.reverse.foreach { c => diff --git a/src/main/scala/uncore/tilelink2/Nodes.scala b/src/main/scala/uncore/tilelink2/Nodes.scala index 9d9a8aed..025d137b 100644 --- a/src/main/scala/uncore/tilelink2/Nodes.scala +++ b/src/main/scala/uncore/tilelink2/Nodes.scala @@ -76,20 +76,21 @@ class BaseNode[PO, PI, EO, EI, B <: Data](imp: NodeImp[PO, PI, EO, EI, B])( def connectOut = bundleOut def connectIn = bundleIn - // source.edge(sink) - protected[tilelink2] def edge(x: BaseNode[PO, PI, EO, EI, B])(implicit sourceInfo: SourceInfo) = { + protected[tilelink2] def := (y: BaseNode[PO, PI, EO, EI, B])(implicit sourceInfo: SourceInfo) = { + val x = this // x := y val info = sourceLine(sourceInfo, " at ", "") - 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) + require (!LazyModule.stack.isEmpty, s"${y.name} cannot be connected to ${x.name} outside of LazyModule scope" + info) + require (!y.noOs, s"${y.name}${y.lazyModule.line} was incorrectly connected as a source" + info) + require (!y.oRealized, s"${y.name}${y.lazyModule.line} was incorrectly connected as a source after it's .module was used" + info) require (!x.noIs, s"${x.name}${x.lazyModule.line} was incorrectly connected as a sink" + info) require (!x.iRealized, s"${x.name}${x.lazyModule.line} was incorrectly connected as a sink after it's .module was used" + info) val i = x.accPI.size - val o = accPO.size - accPO += ((i, x)) - x.accPI += ((o, this)) - () => { - imp.connect(connectOut(o), edgesOut(o), x.connectIn(i), x.edgesIn(i)) - } + val o = y.accPO.size + y.accPO += ((i, x)) + x.accPI += ((o, y)) + LazyModule.stack.head.bindings = (() => { + imp.connect(y.connectOut(o), y.edgesOut(o), x.connectIn(i), x.edgesIn(i)) + }) :: LazyModule.stack.head.bindings } } diff --git a/src/main/scala/uncore/tilelink2/WidthWidget.scala b/src/main/scala/uncore/tilelink2/WidthWidget.scala index 76529564..7acf1b05 100644 --- a/src/main/scala/uncore/tilelink2/WidthWidget.scala +++ b/src/main/scala/uncore/tilelink2/WidthWidget.scala @@ -171,10 +171,10 @@ class TLWidthWidget(innerBeatBytes: Int) extends LazyModule object TLWidthWidget { - // applied to the TL source node; connect (WidthWidget(x.node, 16) -> y.node) - def apply(x: TLBaseNode, innerBeatBytes: Int)(implicit lazyModule: LazyModule, sourceInfo: SourceInfo): TLBaseNode = { + // applied to the TL source node; y.node := WidthWidget(x.node, 16) + def apply(x: TLBaseNode, innerBeatBytes: Int)(implicit sourceInfo: SourceInfo): TLBaseNode = { val widget = LazyModule(new TLWidthWidget(innerBeatBytes)) - lazyModule.connect(x -> widget.node) + widget.node := x widget.node } }