aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/ZeroWidthTests.scala
blob: 4844fa6f59687a0f4520d72d6febddb5c319a035 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// SPDX-License-Identifier: Apache-2.0

package firrtlTests

import firrtl._
import firrtl.options.Dependency
import firrtl.passes._
import firrtl.testutils._

class ZeroWidthTests extends LeanTransformSpec(Seq(Dependency(ZeroWidth))) {
  // =============================
  "Zero width port" should " be deleted" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y : UInt<0>
        |    output x : UInt<1>
        |    x <= y""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output x : UInt<1>
        |    x <= UInt<1>(0)""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Add of <0> and <2> " should " put in zero" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y : UInt<0>
        |    output x : UInt
        |    x <= add(y, UInt<2>(2))""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output x : UInt<3>
        |    x <= add(UInt<1>(0), UInt<2>(2))""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Mux on <0>" should "not be allowed" in {
    // Note that this used to be allowed, but the support seems to have bit-rotted
    // and modern firrtl enforces 1-bit UInt for muxes.
    val input =
      """circuit Top :
        |  module Top :
        |    input y : UInt<0>
        |    output x : UInt
        |    x <= mux(y, UInt<2>(2), UInt<2>(1))""".stripMargin
    val e = intercept[PassException] { compile(input) }
    assert(e.getMessage.contains("A mux condition must be of type 1-bit UInt"))
  }
  "Bundle with field of <0>" should "get deleted" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y : { a: UInt<0> }
        |    output x : { a: UInt<0>, b: UInt<1>}
        |    x.b <= UInt(1)
        |    x.a <= y.a""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output x : { b: UInt<1> }
        |    skip
        |    x.b <= UInt(1)
        |    """.stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Vector with type of <0>" should "get deleted" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y : UInt<0>[10]
        |    output x : UInt<0>[10]
        |    x <= y""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    skip""".stripMargin
    removeSkip(compile(input).circuit).serialize should be(parse(check).serialize)
  }
  "Node with <0>" should "be removed" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y: UInt<0>
        |    node x = y""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    skip""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "IsInvalid on <0>" should "be deleted" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output y: UInt<0>
        |    y is invalid""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    skip""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Expression in node with type <0>" should "be replaced by UInt<1>(0)" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    input y: UInt<0>
        |    node z = add(x, y)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    node z = add(x, UInt<1>(0))""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Expression in cat with type <0>" should "be removed" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    input y: UInt<0>
        |    node z = cat(x, y)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    node z = x""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Nested cats with type <0>" should "be removed" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x: UInt<0>
        |    input y: UInt<0>
        |    input z: UInt<0>
        |    node a = cat(cat(x, y), z)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    skip""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Nested cats where one has type <0>" should "be unaffected" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    input y: UInt<0>
        |    input z: UInt<1>
        |    node a = cat(cat(x, y), z)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input x: UInt<1>
        |    input z: UInt<1>
        |    node a = cat(x, z)""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }
  "Stop with type <0>" should "be replaced with UInt(0)" in {
    // Note that this used to be allowed, but the support seems to have bit-rotted
    // and modern firrtl enforces 1-bit UInt for stop enables.
    val input =
      """circuit Top :
        |  module Top :
        |    input clk: Clock
        |    input x: UInt<1>
        |    input y: UInt<0>
        |    input z: UInt<1>
        |    stop(clk, y, 1)""".stripMargin
    val e = intercept[PassException] { compile(input) }
    assert(e.getMessage.contains("Enable must be a 1-bit UIntType typed signal"))
  }
  "Print with type <0>" should "be replaced with UInt(0)" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input clk: Clock
        |    input x: UInt<1>
        |    input y: UInt<0>
        |    input z: UInt<1>
        |    printf(clk, UInt(1), "%d %d %d\n", x, y, z)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input clk: Clock
        |    input x: UInt<1>
        |    input z: UInt<1>
        |    printf(clk, UInt(1), "%d %d %d\n", x, UInt(0), z)""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }

  "Andr of zero-width expression" should "return true" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input y : UInt<0>
        |    output x : UInt<1>
        |    x <= andr(y)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output x : UInt<1>
        |    x <= UInt<1>(1)""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }

  "Cat of SInt with zero-width" should "keep type correctly" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x : SInt<0>
        |    input y : SInt<1>
        |    output z : UInt<1>
        |    z <= cat(y, x)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input y : SInt<1>
        |    output z : UInt<1>
        |    z <= asUInt(y)""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }

  "dshl with zero-width" should "canonicalize to the un-shifted expression" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input x : UInt<0>
        |    input y : SInt<1>
        |    output z : SInt<1>
        |    z <= dshl(y, x)""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    input y : SInt<1>
        |    output z : SInt<1>
        |    z <= y""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }

  "Memories with zero-width data-type" should "be fully removed" in {
    val input =
      """circuit Foo:
        |  module Foo:
        |    input clock: Clock
        |    input rAddr: UInt<4>
        |    input rEn: UInt<1>
        |    output rData: UInt<0>
        |    input wAddr: UInt<4>
        |    input wEn: UInt<1>
        |    input wMask: UInt<1>
        |    input wData: UInt<0>
        |    input rwEn: UInt<1>
        |    input rwMode: UInt<1>
        |    input rwAddr: UInt<1>
        |    input rwMask: UInt<1>
        |    input rwDataIn: UInt<0>
        |    output rwDataOut: UInt<0>
        |
        |    mem memory:
        |      data-type => UInt<0>
        |      depth => 16
        |      reader => r
        |      writer => w
        |      readwriter => rw
        |      read-latency => 0
        |      write-latency => 1
        |      read-under-write => undefined
        |
        |    memory.r.clk <= clock
        |    memory.r.en <= rEn
        |    memory.r.addr <= rAddr
        |    rData <= memory.r.data
        |    memory.w.clk <= clock
        |    memory.w.en <= wEn
        |    memory.w.addr <= wAddr
        |    memory.w.mask <= wMask
        |    memory.w.data <= wData
        |    memory.rw.clk <= clock
        |    memory.rw.en <= rwEn
        |    memory.rw.addr <= rwAddr
        |    memory.rw.wmode <= rwMode
        |    memory.rw.wmask <= rwMask
        |    memory.rw.wdata <= rwDataIn
        |    rwDataOut <= memory.rw.rdata""".stripMargin
    val check =
      s"""circuit Foo:
         |  module Foo:
         |    input clock: Clock
         |    input rAddr: UInt<4>
         |    input rEn: UInt<1>
         |    input wAddr: UInt<4>
         |    input wEn: UInt<1>
         |    input wMask: UInt<1>
         |    input rwEn: UInt<1>
         |    input rwMode: UInt<1>
         |    input rwAddr: UInt<1>
         |    input rwMask: UInt<1>
         |
         |${Seq.tabulate(17)(_ => "    skip").mkString("\n")}""".stripMargin
    compile(input).circuit.serialize should be(parse(check).serialize)
  }

  "zero width literals" should "be permissible" in {
    val input =
      """circuit Foo:
        |  module Foo:
        |    output x : UInt<1>
        |    output y : SInt<3>
        |
        |    x <= UInt<0>(0)
        |    y <= SInt<0>(0)
        |""".stripMargin

    val result = compile(input).circuit
    val lines = result.serialize.split('\n').map(_.trim)
    assert(lines.contains("x <= UInt<1>(\"h0\")"))
    assert(lines.contains("y <= SInt<1>(\"h0\")"))
  }
}

class ZeroWidthVerilog extends FirrtlFlatSpec {
  "Circuit" should "accept zero width wires" in {
    val compiler = new VerilogCompiler
    val input =
      """circuit Top :
        |  module Top :
        |    input y: UInt<0>
        |    output x: UInt<3>
        |    x <= y""".stripMargin
    val check =
      """module Top(
        |  output  [2:0] x
        |);
        |  assign x = 3'h0;
        |endmodule
        |""".stripMargin.split("\n").map(normalized)
    executeTest(input, check, compiler)
  }
}