summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Util.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/scala/chiselTests/Util.scala')
-rw-r--r--src/test/scala/chiselTests/Util.scala52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/Util.scala b/src/test/scala/chiselTests/Util.scala
index f71cd7f3..8c9bc4ea 100644
--- a/src/test/scala/chiselTests/Util.scala
+++ b/src/test/scala/chiselTests/Util.scala
@@ -5,6 +5,9 @@
package chiselTests
import chisel3._
+import chisel3.experimental.Interval
+import chisel3.internal.firrtl.{IntervalRange, KnownBinaryPoint, Width}
+import _root_.firrtl.{ir => firrtlir}
class PassthroughModuleIO extends Bundle {
val in = Input(UInt(32.W))
@@ -20,4 +23,53 @@ class PassthroughModule extends Module with AbstractPassthroughModule
class PassthroughMultiIOModule extends MultiIOModule with AbstractPassthroughModule
class PassthroughRawModule extends RawModule with AbstractPassthroughModule
+case class ScalaIntervalSimulator(intervalRange: IntervalRange) {
+ val binaryPoint: Int = intervalRange.binaryPoint.asInstanceOf[KnownBinaryPoint].value
+ val epsilon: Double = 1.0 / math.pow(2.0, binaryPoint.toDouble)
+
+ val (lower, upper) = (intervalRange.lowerBound, intervalRange.upperBound) match {
+
+ case (firrtlir.Closed(lower1), firrtlir.Closed(upper1)) => (lower1, upper1)
+ case (firrtlir.Closed(lower1), firrtlir.Open(upper1)) => (lower1, upper1 - epsilon)
+ case (firrtlir.Open(lower1), firrtlir.Closed(upper1)) => (lower1 + epsilon, upper1)
+ case (firrtlir.Open(lower1), firrtlir.Open(upper1)) => (lower1 + epsilon, upper1 - epsilon)
+ case _ =>
+ throw new Exception(s"lower and upper bounds must be defined, range here is $intervalRange")
+ }
+
+ def clip(value: BigDecimal): BigDecimal = {
+
+ if (value < lower) {
+ lower
+ }
+ else if (value > upper) {
+ upper
+ }
+ else {
+ value
+ }
+ }
+
+ def wrap(value: BigDecimal): BigDecimal = {
+
+ if (value < lower) {
+ upper + (value - lower) + epsilon
+ }
+ else if (value > upper) {
+ ((value - upper) - epsilon) + lower
+ }
+ else {
+ value
+ }
+ }
+
+ def allValues: Iterator[BigDecimal] = {
+ (lower to upper by epsilon).toIterator
+ }
+
+ def makeLit(value: BigDecimal): Interval = {
+ Interval.fromDouble(value.toDouble, width = Width(), binaryPoint = binaryPoint.BP)
+ }
+}
+