summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/ResetSpec.scala
blob: fe0273b3fa2e51ccc151348f2d128fbf42a92057 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.util.{Counter, Queue}
import chisel3.testers.BasicTester

class ResetAgnosticModule extends RawModule {
  val clk = IO(Input(Clock()))
  val rst = IO(Input(Reset()))
  val out = IO(Output(UInt(8.W)))

  val reg = withClockAndReset(clk, rst)(RegInit(0.U(8.W)))
  reg := reg + 1.U
  out := reg
}

class AbstractResetDontCareModule extends RawModule {
  import chisel3.util.Valid
  val monoPort = IO(Output(Reset()))
  monoPort := DontCare
  val monoWire = Wire(Reset())
  monoWire := DontCare
  val monoAggPort = IO(Output(Valid(Reset())))
  monoAggPort := DontCare
  val monoAggWire = Wire(Valid(Reset()))
  monoAggWire := DontCare

  // Can't bulk connect to Wire so only ports here
  val bulkPort = IO(Output(Reset()))
  bulkPort <> DontCare
  val bulkAggPort = IO(Output(Valid(Reset())))
  bulkAggPort <> DontCare
}

class ResetSpec extends ChiselFlatSpec with Utils {

  behavior.of("Reset")

  it should "be able to be connected to DontCare" in {
    ChiselStage.elaborate(new AbstractResetDontCareModule)
  }

  it should "be able to drive Bool" in {
    ChiselStage.emitVerilog(new RawModule {
      val in = IO(Input(Bool()))
      val out = IO(Output(Bool()))
      val w = Wire(Reset())
      w := in
      out := w
    })
  }

  it should "be able to drive AsyncReset" in {
    ChiselStage.emitVerilog(new RawModule {
      val in = IO(Input(AsyncReset()))
      val out = IO(Output(AsyncReset()))
      val w = Wire(Reset())
      w := in
      out := w
    })
  }

  it should "allow writing modules that are reset agnostic" in {
    val sync = compile(new Module {
      val io = IO(new Bundle {
        val out = Output(UInt(8.W))
      })
      val inst = Module(new ResetAgnosticModule)
      inst.clk := clock
      inst.rst := reset
      assert(inst.rst.isInstanceOf[chisel3.ResetType])
      io.out := inst.out
    })
    sync should include("always @(posedge clk)")

    val async = compile(new Module {
      val io = IO(new Bundle {
        val out = Output(UInt(8.W))
      })
      val inst = Module(new ResetAgnosticModule)
      inst.clk := clock
      inst.rst := reset.asTypeOf(AsyncReset())
      assert(inst.rst.isInstanceOf[chisel3.ResetType])
      io.out := inst.out
    })
    async should include("always @(posedge clk or posedge rst)")
  }

  behavior.of("Users")

  they should "be able to force implicit reset to be synchronous" in {
    val fir = ChiselStage.emitChirrtl(new Module with RequireSyncReset {
      reset shouldBe a[Bool]
    })
    fir should include("input reset : UInt<1>")
  }

  they should "be able to force implicit reset to be asynchronous" in {
    val fir = ChiselStage.emitChirrtl(new Module with RequireAsyncReset {
      reset shouldBe an[AsyncReset]
    })
    fir should include("input reset : AsyncReset")
  }

  "Chisel" should "error if sync and async modules are nested" in {
    a[ChiselException] should be thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate(new Module with RequireAsyncReset {
        val mod = Module(new Module with RequireSyncReset)
      })
    }
  }
}