aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/ExpandWhensSpec.scala
blob: 52c87ffbc6de2956892bc99f4595a7cb032b8da2 (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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests

import firrtl._
import firrtl.passes._
import firrtl.testutils._

class ExpandWhensSpec extends FirrtlFlatSpec {
  private val transforms = Seq(
    ToWorkingIR,
    CheckHighForm,
    ResolveKinds,
    InferTypes,
    CheckTypes,
    ResolveKinds,
    InferTypes,
    ResolveFlows,
    CheckFlows,
    new InferWidths,
    CheckWidths,
    PullMuxes,
    ExpandConnects,
    RemoveAccesses,
    ExpandWhens
  )
  private def executeTest(input: String, check: String, expected: Boolean) = {
    val circuit = Parser.parse(input.split("\n").toIterator)
    val result = transforms.foldLeft(CircuitState(circuit, UnknownForm)) { (c: CircuitState, p: Transform) =>
      p.runTransform(c)
    }
    val c = result.circuit
    val lines = c.serialize.split("\n").map(normalized)

    if (expected) {
      c.serialize.contains(check) should be(true)
    } else {
      lines.foreach(_.contains(check) should be(false))
    }
  }
  "Expand Whens" should "not emit INVALID" in {
    val input =
      """|circuit Tester :
         |  module Tester :
         |    input p : UInt<1>
         |    when p :
         |      wire a : {b : UInt<64>, c : UInt<64>}
         |      a is invalid
         |      a.b <= UInt<64>("h04000000000000000")""".stripMargin
    val check = "INVALID"
    executeTest(input, check, false)
  }
  it should "void unwritten memory fields" in {
    val input =
      """|circuit Tester :
         |  module Tester :
         |    input clk : Clock
         |    mem memory:
         |      data-type => UInt<32>
         |      depth => 32
         |      reader => r0
         |      writer => w0
         |      read-latency => 0
         |      write-latency => 1
         |      read-under-write => undefined
         |    memory.r0.addr <= UInt<1>(1)
         |    memory.r0.en <= UInt<1>(1)
         |    memory.r0.clk <= clk
         |    memory.w0.addr <= UInt<1>(1)
         |    memory.w0.data <= UInt<1>(1)
         |    memory.w0.en <= UInt<1>(1)
         |    memory.w0.clk <= clk
         |    """.stripMargin
    val check = "VOID"
    executeTest(input, check, true)
  }
  it should "replace 'is invalid' with validif for wires that have a connection" in {
    val input =
      """|circuit Tester :
         |  module Tester :
         |    input p : UInt<1>
         |    output out : UInt
         |    wire w : UInt<32>
         |    w is invalid
         |    out <= w
         |    when p :
         |      w <= UInt(123)
         """.stripMargin
    val check = "validif(p"
    executeTest(input, check, true)
  }
  it should "leave 'is invalid' for wires that don't have a connection" in {
    val input =
      """|circuit Tester :
         |  module Tester :
         |    input p : UInt<1>
         |    output out : UInt
         |    wire w : UInt<32>
         |    w is invalid
         |    out <= w
         """.stripMargin
    val check = "w is invalid"
    executeTest(input, check, true)
  }
  it should "delete 'is invalid' for attached Analog wires" in {
    val input =
      """|circuit Tester :
         |  extmodule Child :
         |    input bus : Analog<32>
         |  module Tester :
         |    input bus : Analog<32>
         |    inst c of Child
         |    wire w : Analog<32>
         |    attach (w, bus)
         |    attach (w, c.bus)
         |    w is invalid
         """.stripMargin
    val check = "w is invalid"
    executeTest(input, check, false)
  }
  it should "correctly handle submodule inputs" in {
    val input =
      """circuit Test :
        |  module Child :
        |    input in : UInt<32>
        |  module Test :
        |    input in : UInt<32>[2]
        |    input p : UInt<1>
        |    inst c of Child
        |    when p :
        |      c.in <= in[0]
        |    else :
        |      c.in <= in[1]""".stripMargin
    val check = "mux(p, in[0], in[1])"
    executeTest(input, check, true)
  }
  it should "handle asserts" in {
    val input =
      """circuit Test :
        |  module Test :
        |    input clock : Clock
        |    input in : UInt<32>
        |    input p : UInt<1>
        |    when p :
        |      assert(clock, eq(in, UInt<1>("h1")), UInt<1>("h1"), "assert0") : test_assert
        |    else :
        |      skip""".stripMargin
    val check =
      "assert(clock, eq(in, UInt<1>(\"h1\")), and(and(UInt<1>(\"h1\"), p), UInt<1>(\"h1\")), \"assert0\") : test_assert"
    executeTest(input, check, true)
  }
  it should "handle stops" in {
    val input =
      """circuit Test :
        |  module Test :
        |    input clock : Clock
        |    input in : UInt<32>
        |    input p : UInt<1>
        |    when p :
        |      stop(clock, UInt(1), 1) : test_stop
        |    else :
        |      skip""".stripMargin
    val check = """stop(clock, and(and(UInt<1>("h1"), p), UInt<1>("h1")), 1) : test_stop"""
    executeTest(input, check, true)
  }
}

class ExpandWhensExecutionTest extends ExecutionTest("ExpandWhens", "/passes/ExpandWhens")