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
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests.aop
import chisel3.testers.{BasicTester, TesterDriver}
import chiselTests.{ChiselFlatSpec, Utils}
import chisel3._
import chisel3.aop.Select
import chisel3.aop.injecting.InjectingAspect
import logger.{LogLevel, LogLevelAnnotation}
object InjectionHierarchy {
class SubmoduleManipulationTester extends BasicTester {
val moduleSubmoduleA = Module(new SubmoduleA)
}
class MultiModuleInjectionTester extends BasicTester {
val subA0 = Module(new SubmoduleA)
val subA1 = Module(new SubmoduleA)
}
class SubmoduleA extends Module {
val io = IO(new Bundle {
val out = Output(Bool())
})
io.out := false.B
}
class SubmoduleB extends Module {
val io = IO(new Bundle {
val in = Input(Bool())
})
}
class SubmoduleC extends experimental.ExtModule with util.HasExtModuleInline {
val io = IO(new Bundle {
val in = Input(Bool())
})
//scalastyle:off regex
setInline(
"SubmoduleC.v",
s"""
|module SubmoduleC(
| input io_in
|);
|endmodule
""".stripMargin
)
}
class AspectTester(results: Seq[Int]) extends BasicTester {
val values = VecInit(results.map(_.U))
val counter = RegInit(0.U(results.length.W))
counter := counter + 1.U
when(counter >= values.length.U) {
stop()
}.otherwise {
when(reset.asBool() === false.B) {
assert(counter === values(counter))
}
}
}
}
class InjectionSpec extends ChiselFlatSpec with Utils {
import InjectionHierarchy._
val correctValueAspect = InjectingAspect(
{ dut: AspectTester => Seq(dut) },
{ dut: AspectTester =>
for (i <- 0 until dut.values.length) {
dut.values(i) := i.U
}
}
)
val wrongValueAspect = InjectingAspect(
{ dut: AspectTester => Seq(dut) },
{ dut: AspectTester =>
for (i <- 0 until dut.values.length) {
dut.values(i) := (i + 1).U
}
}
)
val manipulateSubmoduleAspect = InjectingAspect(
{ dut: SubmoduleManipulationTester => Seq(dut) },
{ dut: SubmoduleManipulationTester =>
val moduleSubmoduleB = Module(new SubmoduleB)
moduleSubmoduleB.io.in := dut.moduleSubmoduleA.io.out
//if we're here then we've elaborated correctly
stop()
}
)
val duplicateSubmoduleAspect = InjectingAspect(
{ dut: SubmoduleManipulationTester => Seq(dut) },
{ _: SubmoduleManipulationTester =>
// By creating a second SubmoduleA, the module names would conflict unless they were uniquified
val moduleSubmoduleA2 = Module(new SubmoduleA)
//if we're here then we've elaborated correctly
stop()
}
)
val addingExternalModules = InjectingAspect(
{ dut: SubmoduleManipulationTester => Seq(dut) },
{ _: SubmoduleManipulationTester =>
// By creating a second SubmoduleA, the module names would conflict unless they were uniquified
val moduleSubmoduleC = Module(new SubmoduleC)
moduleSubmoduleC.io <> DontCare
//if we're here then we've elaborated correctly
stop()
}
)
val multiModuleInjectionAspect = InjectingAspect(
{ top: MultiModuleInjectionTester =>
Select.collectDeep(top) { case m: SubmoduleA => m }
},
{ m: Module =>
val wire = Wire(Bool())
wire := m.reset.asBool()
dontTouch(wire)
stop()
}
)
"Test" should "pass if inserted the correct values" in {
assertTesterPasses { new AspectTester(Seq(0, 1, 2)) }
}
"Test" should "fail if inserted the wrong values" in {
assertTesterFails { new AspectTester(Seq(9, 9, 9)) }
}
"Test" should "pass if pass wrong values, but correct with aspect" in {
assertTesterPasses({ new AspectTester(Seq(9, 9, 9)) }, Nil, Seq(correctValueAspect) ++ TesterDriver.verilatorOnly)
}
"Test" should "pass if pass wrong values, then wrong aspect, then correct aspect" in {
assertTesterPasses(
new AspectTester(Seq(9, 9, 9)),
Nil,
Seq(wrongValueAspect, correctValueAspect) ++ TesterDriver.verilatorOnly
)
}
"Test" should "fail if pass wrong values, then correct aspect, then wrong aspect" in {
assertTesterFails({ new AspectTester(Seq(9, 9, 9)) }, Nil, Seq(correctValueAspect, wrongValueAspect))
}
"Test" should "pass if the submodules in SubmoduleManipulationTester can be manipulated by manipulateSubmoduleAspect" in {
assertTesterPasses(
{ new SubmoduleManipulationTester },
Nil,
Seq(manipulateSubmoduleAspect) ++ TesterDriver.verilatorOnly
)
}
"Module name collisions when adding a new module" should "be resolved" in {
assertTesterPasses(
{ new SubmoduleManipulationTester },
Nil,
Seq(duplicateSubmoduleAspect) ++ TesterDriver.verilatorOnly
)
}
"Adding external modules" should "work" in {
assertTesterPasses(
{ new SubmoduleManipulationTester },
Nil,
Seq(addingExternalModules) ++ TesterDriver.verilatorOnly
)
}
"Injection into multiple submodules of the same class" should "work" in {
assertTesterPasses(
{ new MultiModuleInjectionTester },
Nil,
Seq(multiModuleInjectionAspect) ++ TesterDriver.verilatorOnly
)
}
}
|