|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package chisel3 |
| 4 | + |
| 5 | +import chiselTests.{ChiselFlatSpec, FileCheck} |
| 6 | +import circt.stage.ChiselStage |
| 7 | + |
| 8 | +class PlaceholderSpec extends ChiselFlatSpec with FileCheck { |
| 9 | + |
| 10 | + "Placeholders" should "allow insertion of commands" in { |
| 11 | + |
| 12 | + class Foo extends RawModule { |
| 13 | + |
| 14 | + val placeholder = new Placeholder() |
| 15 | + |
| 16 | + val a = Wire(UInt(1.W)) |
| 17 | + |
| 18 | + val b = placeholder.append { |
| 19 | + Wire(UInt(2.W)) |
| 20 | + } |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + generateFirrtlAndFileCheck(new Foo) { |
| 25 | + s"""|CHECK: wire b : UInt<2> |
| 26 | + |CHECK-NEXT: wire a : UInt<1> |
| 27 | + |""".stripMargin |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + they should "be capable of being nested" in { |
| 33 | + |
| 34 | + class Foo extends RawModule { |
| 35 | + |
| 36 | + val placeholder_1 = new Placeholder() |
| 37 | + val placeholder_2 = placeholder_1.append(new Placeholder()) |
| 38 | + |
| 39 | + val a = Wire(UInt(1.W)) |
| 40 | + |
| 41 | + val b = placeholder_1.append { |
| 42 | + Wire(UInt(2.W)) |
| 43 | + } |
| 44 | + |
| 45 | + val c = placeholder_2.append { |
| 46 | + Wire(UInt(3.W)) |
| 47 | + } |
| 48 | + |
| 49 | + val d = placeholder_1.append { |
| 50 | + Wire(UInt(4.W)) |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + generateFirrtlAndFileCheck(new Foo) { |
| 56 | + s"""|CHECK: wire c : UInt<3> |
| 57 | + |CHECK-NEXT: wire b : UInt<2> |
| 58 | + |CHECK-NEXT: wire d : UInt<4> |
| 59 | + |CHECK-NEXT: wire a : UInt<1> |
| 60 | + |""".stripMargin |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + they should "emit no statements if empty" in { |
| 66 | + |
| 67 | + class Foo extends RawModule { |
| 68 | + val a = new Placeholder() |
| 69 | + } |
| 70 | + |
| 71 | + generateFirrtlAndFileCheck(new Foo) { |
| 72 | + """|CHECK: public module Foo : |
| 73 | + |CHECK: skip |
| 74 | + |""".stripMargin |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + they should "allow constructing hardware in a parent" in { |
| 79 | + |
| 80 | + class Bar(placeholder: Placeholder, a: Bool) extends RawModule { |
| 81 | + |
| 82 | + placeholder.append { |
| 83 | + val b = Wire(Bool()) |
| 84 | + b :<= a |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + class Foo extends RawModule { |
| 90 | + |
| 91 | + val a = Wire(Bool()) |
| 92 | + val placeholder = new Placeholder |
| 93 | + |
| 94 | + val bar = Module(new Bar(placeholder, a)) |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + generateFirrtlAndFileCheck(new Foo) { |
| 99 | + """|CHECK: module Bar : |
| 100 | + |CHECK-NOT: {{wire|connect}} |
| 101 | + | |
| 102 | + |CHECK: module Foo : |
| 103 | + |CHECK: wire a : UInt<1> |
| 104 | + |CHECK-NEXT: wire b : UInt<1> |
| 105 | + |CHECK-NEXT: connect b, a |
| 106 | + |CHECK-NEXT: inst bar of Bar |
| 107 | + |""".stripMargin |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + // TODO: This test can be changed to pass in the future in support of advanced |
| 113 | + // APIs like Providers or an improved BoringUtils. |
| 114 | + they should "error if constructing hardware in a child" in { |
| 115 | + |
| 116 | + class Bar extends RawModule { |
| 117 | + |
| 118 | + val a = Wire(Bool()) |
| 119 | + val placeholder = new Placeholder() |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + class Foo extends RawModule { |
| 124 | + |
| 125 | + val bar = Module(new Bar) |
| 126 | + |
| 127 | + bar.placeholder.append { |
| 128 | + val b = Wire(Bool()) |
| 129 | + b :<= bar.a |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + intercept[IllegalArgumentException] { ChiselStage.emitCHIRRTL(new Foo) }.getMessage() should include( |
| 135 | + "Can't write to module after module close" |
| 136 | + ) |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments