aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJack Koenig2020-04-13 21:23:54 -0700
committerGitHub2020-04-14 04:23:54 +0000
commitf8546a7c165e0bde4b3e5682dd6edd0a3e199b31 (patch)
treebbf3968663ee2be767266a4ff4e5fb1fc9939b34 /src
parent6cc66a9df1848b81a7954ee5e09925bae605fb88 (diff)
Allow casts in AsyncReset literal value check (#1523)
Chisel emits all literals as UInts cast to the correct type, make CheckResets support casts when checking that async reset registers are reset to literal values. Co-authored-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/firrtl/checks/CheckResets.scala17
-rw-r--r--src/test/scala/firrtlTests/AsyncResetSpec.scala47
2 files changed, 58 insertions, 6 deletions
diff --git a/src/main/scala/firrtl/checks/CheckResets.scala b/src/main/scala/firrtl/checks/CheckResets.scala
index 406b7f62..facd5c01 100644
--- a/src/main/scala/firrtl/checks/CheckResets.scala
+++ b/src/main/scala/firrtl/checks/CheckResets.scala
@@ -6,10 +6,12 @@ import firrtl._
import firrtl.options.{Dependency, PreservesAll}
import firrtl.passes.{Errors, PassException}
import firrtl.ir._
+import firrtl.Utils.isCast
import firrtl.traversals.Foreachers._
import firrtl.WrappedExpression._
import scala.collection.mutable
+import scala.annotation.tailrec
object CheckResets {
class NonLiteralAsyncResetValueException(info: Info, mname: String, reg: String, init: String) extends PassException(
@@ -52,12 +54,15 @@ class CheckResets extends Transform with PreservesAll[Transform] {
stmt.foreach(onStmt(regCheck, drivers))
}
- private def findDriver(drivers: DirectDriverMap)(expr: Expression): Expression =
- drivers.get(we(expr)) match {
- case Some(lit: Literal) => lit
- case Some(other) => findDriver(drivers)(other)
- case None => expr
- }
+ @tailrec
+ private def findDriver(drivers: DirectDriverMap)(expr: Expression): Expression = expr match {
+ case lit: Literal => lit
+ case DoPrim(op, args, _,_) if isCast(op) => findDriver(drivers)(args.head)
+ case other => drivers.get(we(other)) match {
+ case Some(e) => findDriver(drivers)(e)
+ case None => other
+ }
+ }
private def onMod(errors: Errors)(mod: DefModule): Unit = {
val regCheck = new RegCheckList()
diff --git a/src/test/scala/firrtlTests/AsyncResetSpec.scala b/src/test/scala/firrtlTests/AsyncResetSpec.scala
index c0c79165..60eab883 100644
--- a/src/test/scala/firrtlTests/AsyncResetSpec.scala
+++ b/src/test/scala/firrtlTests/AsyncResetSpec.scala
@@ -232,6 +232,53 @@ class AsyncResetSpec extends FirrtlFlatSpec {
result should containLine ("always @(posedge clock or posedge reset) begin")
}
+ "Cast literals" should "be allowed as reset values for AsyncReset" in {
+ // This also checks that casts can be across wires and nodes
+ val sintResult = compileBody(s"""
+ |input clock : Clock
+ |input reset : AsyncReset
+ |input x : SInt<4>
+ |output y : SInt<4>
+ |output z : SInt<4>
+ |reg r : SInt<4>, clock with : (reset => (reset, asSInt(UInt(0))))
+ |r <= x
+ |wire w : SInt<4>
+ |reg r2 : SInt<4>, clock with : (reset => (reset, w))
+ |r2 <= x
+ |node n = UInt("hf")
+ |w <= asSInt(n)
+ |y <= r2
+ |z <= r""".stripMargin
+ )
+ sintResult should containLine ("always @(posedge clock or posedge reset) begin")
+ sintResult should containLine ("r <= 4'sh0;")
+ sintResult should containLine ("r2 <= -4'sh1;")
+
+ val fixedResult = compileBody(s"""
+ |input clock : Clock
+ |input reset : AsyncReset
+ |input x : Fixed<2><<0>>
+ |output z : Fixed<2><<0>>
+ |reg r : Fixed<2><<0>>, clock with : (reset => (reset, asFixedPoint(UInt(2), 0)))
+ |r <= x
+ |z <= r""".stripMargin
+ )
+ fixedResult should containLine ("always @(posedge clock or posedge reset) begin")
+ fixedResult should containLine ("r <= -2'sh2;")
+
+ val intervalResult = compileBody(s"""
+ |input clock : Clock
+ |input reset : AsyncReset
+ |input x : Interval[0, 4].0
+ |output z : Interval[0, 4].0
+ |reg r : Interval[0, 4].0, clock with : (reset => (reset, asInterval(UInt(0), 0, 0, 0)))
+ |r <= x
+ |z <= r""".stripMargin
+ )
+ intervalResult should containLine ("always @(posedge clock or posedge reset) begin")
+ intervalResult should containLine ("r <= 4'sh0;")
+ }
+
"CheckResets" should "NOT raise StackOverflow Exception on Combinational Loops (should be caught by firrtl.transforms.CheckCombLoops)" in {
an [firrtl.transforms.CheckCombLoops.CombLoopException] shouldBe thrownBy {
compileBody(s"""