summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/BoringUtilsSpec.scala
blob: 3af3b4fa765850144382f8f0a188cf64dba0f42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// See LICENSE for license details.

package chiselTests

import java.io.File

import chisel3._
import chisel3.util.Counter
import chisel3.testers.BasicTester
import chisel3.experimental.{MultiIOModule, RawModule, BaseModule}
import chisel3.util.experimental.BoringUtils
import firrtl.{CommonOptions, ExecutionOptionsManager, HasFirrtlOptions, FirrtlExecutionOptions, FirrtlExecutionSuccess,
  FirrtlExecutionFailure}
import firrtl.passes.wiring.WiringTransform

abstract class ShouldntAssertTester(cyclesToWait: BigInt = 4) extends BasicTester {
  val dut: BaseModule
  val (_, done) = Counter(true.B, 2)
  when (done) { stop() }
}

class BoringUtilsSpec extends ChiselFlatSpec with ChiselRunners {

  class BoringInverter extends Module {
    val io = IO(new Bundle{})
    val a = Wire(UInt(1.W))
    val notA = Wire(UInt(1.W))
    val b = Wire(UInt(1.W))
    a := 0.U
    notA := ~a
    b := a
    chisel3.assert(b === 1.U)
    BoringUtils.addSource(notA, "x")
    BoringUtils.addSink(b, "x")
  }

  behavior of "BoringUtils.{addSink, addSource}"

  it should "connect two wires within a module" in {
    runTester(new ShouldntAssertTester { val dut = Module(new BoringInverter) } ) should be (true)
  }

  trait WireX { this: BaseModule =>
    val x = Wire(UInt(4.W))
  }

  class Constant(const: Int) extends MultiIOModule with WireX {
    x := const.U
  }

  object Constant { def apply(const: Int): Constant = Module(new Constant(const)) }

  class Expect(const: Int) extends MultiIOModule with WireX {
    x := 0.U // Default value. Output is zero unless we bore...
    chisel3.assert(x === const.U, "x (0x%x) was not const.U (0x%x)", x, const.U)
  }

  object Expect { def apply(const: Int): Expect = Module(new Expect(const)) }

  // After boring, this will have the following connections:
  //   - source(0)   -> unconnected
  //   - unconnected -> expect(0)
  //   - source(1)   -> expect(1)
  //   - source(2)   -> expect(2)
  class Top(val width: Int) extends MultiIOModule {
    val source = Seq(0, 1, 2).map(x => x -> Constant(x)).toMap
    val expect = Map(0 -> Seq.fill(2)(Expect(0)),
                     1 -> Seq.fill(1)(Expect(1)),
                     2 -> Seq.fill(3)(Expect(2)))
  }

  class TopTester extends ShouldntAssertTester {
    val dut = Module(new Top(4))
    BoringUtils.bore(dut.source(1).x, dut.expect(1).map(_.x))
    BoringUtils.bore(dut.source(2).x, dut.expect(2).map(_.x))
  }

  behavior of "BoringUtils.bore"

  it should "connect across modules using BoringUtils.bore" in {
	  runTester(new TopTester) should be (true)
  }
}