aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/PresetSpec.scala
blob: d967aa6575c5cda6e1d257c9265e77a31eb9045a (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: Apache-2.0

package firrtlTests

import firrtl._
import firrtl.annotations._
import firrtl.testutils._
import firrtl.testutils.FirrtlCheckers._
import logger.{LogLevel, LogLevelAnnotation, Logger}

class PresetSpec extends VerilogTransformSpec {
  type Mod = Seq[String]
  type ModuleSeq = Seq[Mod]

  def compileBody(modules: ModuleSeq) = {
    val annos =
      Seq(PresetAnnotation(CircuitTarget("Test").module("Test").ref("reset")), firrtl.transforms.NoDCEAnnotation)
    var str = """
                |circuit Test :
                |""".stripMargin
    modules.foreach((m: Mod) => {
      val header = "|module " + m(0) + " :"
      str += header.stripMargin.stripMargin.split("\n").mkString("  ", "\n  ", "")
      str += m(1).split("\n").mkString("    ", "\n    ", "")
      str += """
               |""".stripMargin
    })
    val logLevel = LogLevel.Warn
    Logger.makeScope(Seq(LogLevelAnnotation(logLevel))) {
      compile(str, annos)
    }
  }

  "Preset" should """behave properly given a `Preset` annotated `AsyncReset` INPUT reset:
    - replace AsyncReset specific blocks by standard Register blocks 
    - add inline declaration of all registers connected to reset
    - remove the useless input port""" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |reg r : UInt<1>, clock with : (reset => (reset, UInt(0)))
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )
    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result shouldNot containLine("input   reset,")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }

  it should """behave properly given a `Preset` annotated `AsyncReset` WIRE reset:
    - replace AsyncReset specific blocks by standard Register blocks 
    - add inline declaration of all registers connected to reset
    - remove the useless wire declaration and assignation""" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input x : UInt<1>
             |output z : UInt<1>
             |wire reset : AsyncReset
             |reset <= asAsyncReset(UInt(0))
             |reg r : UInt<1>, clock with : (reset => (reset, UInt(0)))
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )

    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
    // it should also remove useless asyncReset signal, all along the path down to registers
    result shouldNot containLine("wire  reset;")
    result shouldNot containLine("assign reset = 1'h0;")
  }
  it should "replace usages of the preset reset with the constant 0 since the reset is never active" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input x : UInt<1>
             |output z : UInt<1>
             |output sz : UInt<1>
             |wire reset : AsyncReset
             |reset <= asAsyncReset(UInt(0))
             |reg r : UInt<1>, clock with : (reset => (reset, UInt(0)))
             |wire sreset : UInt<1>
             |sreset <= asUInt(reset) ; this is ok, essentially like assigning zero to the wire
             |reg s : UInt<1>, clock with : (reset => (sreset, UInt(0)))
             |r <= x
             |s <= x
             |z <= r
             |sz <= s""".stripMargin
        )
      )
    )

    result should containLine("wire  sreset = 1'h0;")
  }

  it should "propagate through bundles" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |wire bundle : {in_rst: AsyncReset, out_rst:AsyncReset}
             |bundle.in_rst <= reset
             |bundle.out_rst <= bundle.in_rst
             |reg r : UInt<1>, clock with : (reset => (bundle.out_rst, UInt(0)))
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )
    result shouldNot containLine("input   reset,")
    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }
  it should "propagate through vectors" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |wire vector : AsyncReset[2]
             |vector[0] <= reset
             |vector[1] <= vector[0]
             |reg r : UInt<1>, clock with : (reset => (vector[1], UInt(0)))
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )
    result shouldNot containLine("input   reset,")
    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }

  it should "propagate through bundles of vectors" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |wire bundle : {in_rst: AsyncReset[2], out_rst:AsyncReset}
             |bundle.in_rst[0] <= reset
             |bundle.in_rst[1] <= bundle.in_rst[0]
             |bundle.out_rst <= bundle.in_rst[1]
             |reg r : UInt<1>, clock with : (reset => (bundle.out_rst, UInt(0)))
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )
    result shouldNot containLine("input   reset,")
    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }
  it should """propagate properly accross modules: 
    - replace AsyncReset specific blocks by standard Register blocks 
    - add inline declaration of all registers connected to reset
    - remove the useless input port of instanciated module
    - remove the useless instance connections 
    - remove wires and assignations used in instance connections
    """ in {
    val result = compileBody(
      Seq(
        Seq(
          "TestA",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |reg r : UInt<1>, clock with : (reset => (reset, UInt(0)))
             |r <= x
             |z <= r
             |""".stripMargin
        ),
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input x : UInt<1>
             |output z : UInt<1>
             |wire reset : AsyncReset
             |reset <= asAsyncReset(UInt(0))
             |inst i of TestA
             |i.clock <= clock
             |i.reset <= reset
             |i.x <= x
             |z <= i.z""".stripMargin
        )
      )
    )
    // assess that all useless connections are not emitted
    result shouldNot containLine("wire i_reset;")
    result shouldNot containLine(".reset(i_reset),")
    result shouldNot containLine("assign i_reset = reset;")
    result shouldNot containLine("input   reset,")

    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }

  it should "propagate zeros for all other uses of an async reset" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |output a : UInt<2>
             |output b : UInt<2>
             |
             |node t = reset
             |node not_t = not(asUInt(reset))
             |a <= cat(asUInt(t), not_t)
             |node t2 = asUInt(t)
             |node t3 = asAsyncReset(t2)
             |b <= cat(asUInt(asSInt(reset)), asUInt(t3))
             |""".stripMargin
        )
      )
    )

    result should containLine("assign b = {1'h0,1'h0};")
  }

  it should "propagate even through disordonned statements" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input reset : AsyncReset
             |input x : UInt<1>
             |output z : UInt<1>
             |wire bundle : {in_rst: AsyncReset, out_rst:AsyncReset}
             |reg r : UInt<1>, clock with : (reset => (bundle.out_rst, UInt(0)))
             |bundle.out_rst <= bundle.in_rst
             |bundle.in_rst <= reset
             |r <= x
             |z <= r""".stripMargin
        )
      )
    )
    result shouldNot containLine("input   reset,")
    result shouldNot containLine("always @(posedge clock or posedge reset) begin")
    result shouldNot containLines("if (reset) begin", "r = 1'h0;", "end")
    result should containLine("always @(posedge clock) begin")
    result should containLine("reg  r = 1'h0;")
  }

  it should "work with verification statements that are guarded by a preset reset" in {
    val result = compileBody(
      Seq(
        Seq(
          "Test",
          s"""
             |input clock : Clock
             |input in : UInt<4>
             |input reset : AsyncReset
             |
             |node _T = eq(in, UInt<2>("h3")) @[main.scala 19:15]
             |node _T_1 = asUInt(reset) @[main.scala 19:11]
             |node _T_2 = eq(_T_1, UInt<1>("h0")) @[main.scala 19:11]
             |when _T_2 : @[main.scala 19:11]
             |  assert(clock, _T, UInt<1>("h1"), "") : assert @[main.scala 19:11]
             |  node _T_3 = eq(_T, UInt<1>("h0")) @[main.scala 19:11]
             |  when _T_3 : @[main.scala 19:11]
             |    printf(clock, UInt<1>("h1"), "Assertion failed") : printf @[main.scala 19:11]
             |
             |""".stripMargin
        )
      )
    )
    // just getting here without falling over the fact that `reset` gets removed is great!
  }

}

class PresetExecutionTest
    extends ExecutionTest(
      "PresetTester",
      "/features",
      annotations = Seq(new PresetAnnotation(CircuitTarget("PresetTester").module("PresetTester").ref("preset")))
    )