-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathSimulatorSpec.scala
More file actions
337 lines (303 loc) · 11.4 KB
/
Copy pathSimulatorSpec.scala
File metadata and controls
337 lines (303 loc) · 11.4 KB
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
package chiselTests.simulator
import chisel3._
import chisel3.layer.{block, Convention, Layer, LayerConfig}
import chisel3.simulator._
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.must.Matchers
// import org.scalatest.matchers.should.Matchers.convertToStringShouldWrapperForVerb
import svsim._
class VerilatorSimulator(val workspacePath: String) extends Simulator[verilator.Backend] {
val backend = verilator.Backend.initializeFromProcessEnvironment()
val tag = "verilator"
val commonCompilationSettings = CommonCompilationSettings()
val backendSpecificCompilationSettings = verilator.Backend.CompilationSettings.default
}
class SimulatorSpec extends AnyFunSpec with Matchers {
describe("Chisel Simulator") {
it("runs GCD correctly") {
val simulator = new VerilatorSimulator("test_run_dir/simulator/GCDSimulator")
val result = simulator
.simulate(new GCD()) { module =>
val gcd = module.wrapped
val a = module.port(gcd.io.a)
val b = module.port(gcd.io.b)
val loadValues = module.port(gcd.io.loadValues)
val result = module.port(gcd.io.result)
val resultIsValid = module.port(gcd.io.resultIsValid)
val clock = module.port(gcd.clock)
a.set(24)
b.set(36)
loadValues.set(1)
clock.tick(
inPhaseValue = 0,
outOfPhaseValue = 1,
timestepsPerPhase = 1,
cycles = 1
)
loadValues.set(0)
clock.tick(
inPhaseValue = 0,
outOfPhaseValue = 1,
timestepsPerPhase = 1,
maxCycles = 10,
sentinel = Some(resultIsValid, 1)
)
result.get().asBigInt
}
.result
assert(result === 12)
}
it("runs GCD correctly with peek/poke") {
val simulator = new VerilatorSimulator("test_run_dir/simulator/GCDSimulator")
val result = simulator
.simulate(new GCD()) { module =>
import PeekPokeAPI._
val gcd = module.wrapped
gcd.clock.step(2)
gcd.io.a.poke(24.U)
gcd.io.b.poke(36.U)
gcd.io.loadValues.poke(1.B)
gcd.clock.step()
gcd.io.loadValues.poke(0.B)
gcd.clock.step(10)
gcd.io.result.expect(12)
gcd.io.result.peek().litValue
}
.result
assert(result === 12)
}
it("reports failed expects correctly") {
val simulator = new VerilatorSimulator("test_run_dir/simulator/GCDSimulator")
val thrown = the[FailedExpectationException[_]] thrownBy {
simulator
.simulate(new GCD()) { module =>
import PeekPokeAPI._
val gcd = module.wrapped
gcd.clock.step(2)
gcd.io.a.poke(24.U)
gcd.io.b.poke(36.U)
gcd.io.loadValues.poke(1.B)
gcd.clock.step()
gcd.io.loadValues.poke(0.B)
gcd.clock.step(10)
gcd.io.result.expect(5)
}
.result
}
thrown.getMessage must include("observed value 12 != 5")
(thrown.getMessage must include).regex(
""" @\[src/test/scala-2/chiselTests/simulator/SimulatorSpec\.scala:\d+:\d+\]"""
)
thrown.getMessage must include("gcd.io.result.expect(5)")
thrown.getMessage must include(" ^")
}
it("runs a design that includes an external module") {
class Bar extends ExtModule {
val a = IO(Output(Bool()))
setInline(
"Bar.sv",
"""|module Bar(
| output a
|);
| assign a = 1'b1;
|endmodule
|""".stripMargin
)
}
class Baz extends ExtModule {
val a = IO(Output(Bool()))
addResource("/chisel3/simulator/Baz.sv")
}
class Qux extends ExtModule {
val a = IO(Output(Bool()))
addPath("src/test/resources/chisel3/simulator/Qux.sv")
}
class Foo extends RawModule {
val a, b, c = IO(Output(Bool()))
a :<= Module(new Bar).a
b :<= Module(new Baz).a
c :<= Module(new Qux).a
}
new VerilatorSimulator("test_run_dir/simulator/extmodule")
.simulate(new Foo) { module =>
import PeekPokeAPI._
val foo = module.wrapped
foo.a.expect(1)
foo.b.expect(1)
foo.c.expect(1)
}
.result
}
it("runs a design with debug mode (-g) and --strip-debug-info") {
import circt.stage.ChiselStage
class Bar extends Module {
val a = IO(Input(Bool()))
val b = IO(Input(Bool()))
val out = IO(Output(Bool()))
out := a & b
}
// Check now the debug info is stripped
val expectedSV = ChiselStage.emitSystemVerilog(new Bar, firtoolOpts = Array("--strip-debug-info", "-g"))
new VerilatorSimulator("test_run_dir/simulator/bar_debug_mode")
.simulate(new Bar, firtoolOpts = Array("-strip-debug-info", "-g")) { module =>
import PeekPokeAPI._
val bar = module.wrapped
bar.a.poke(false.B)
bar.b.poke(false.B)
bar.out.expect(false.B)
bar.clock.step()
bar.a.poke(true.B)
bar.b.poke(false.B)
bar.out.expect(false.B)
bar.clock.step()
bar.a.poke(true.B)
bar.b.poke(true.B)
bar.out.expect(true.B)
bar.clock.step()
}
.result
// Check the expected SV and the generated SV are the same.
val source = io.Source.fromFile("test_run_dir/simulator/bar_debug_mode/primary-sources/Bar.sv")
val actualSV = source.mkString
expectedSV should include(actualSV)
source.close()
}
it("simulate a circuit with zero-width ports") {
val width = 0
// Run a simulation with zero width foo
new VerilatorSimulator("test_run_dir/simulator/foo_zero_width")
.simulate(new OptionalIOModule(n = width), firtoolOpts = Array("--strip-debug-info", "-g")) { module =>
import PeekPokeAPI._
val dut = module.wrapped
dut.clock.step(2)
dut.clock.step(10)
}
.result
// Check the testbench sv does not contain dut.foo and dut.out
val tbSource = io.Source.fromFile("test_run_dir/simulator/foo_zero_width/generated-sources/testbench.sv")
val tbSV = tbSource.mkString
tbSource.close()
// Check IO ports
(tbSV should not).include("dut.foo")
(tbSV should not).include("dut.bar")
(tbSV should not).include("dut.out")
(tbSV should not).include("dut.emptyBundle")
(tbSV should not).include("dut.bundle_x")
val source = io.Source.fromFile("test_run_dir/simulator/foo_zero_width/primary-sources/OptionalIOModule.sv")
val actualSV = source.mkString
source.close()
(actualSV should not).include("foo")
(actualSV should not).include("bar")
(actualSV should not).include("myReg")
(actualSV should not).include("output [7:0] out")
(actualSV should not).include("emptyBundle")
(actualSV should not).include("bundle_x")
}
it("simulate a circuit with non zero-width ports") {
val width = 8
// Run a simulation with zero width foo
new VerilatorSimulator("test_run_dir/simulator/foo_non_zero_width")
.simulate(new OptionalIOModule(n = width), firtoolOpts = Array("--strip-debug-info", "-g")) { module =>
import PeekPokeAPI._
val dut = module.wrapped
dut.clock.step(2)
dut.clock.step(10)
}
.result
// Check the testbench sv does not contain dut.foo and dut.out
val tbSource = io.Source.fromFile("test_run_dir/simulator/foo_non_zero_width/generated-sources/testbench.sv")
val tbSV = tbSource.mkString
tbSource.close()
// Check IO ports
tbSV should include("[$bits(dut.foo)-1:0] foo")
tbSV should include("[$bits(dut.bar)-1:0] bar")
tbSV should include("[$bits(dut.out)-1:0] out")
(tbSV should not).include("emptyBundle")
tbSV should include("[$bits(dut.bundle_x)-1:0] bundle_x")
val source = io.Source.fromFile("test_run_dir/simulator/foo_non_zero_width/primary-sources/OptionalIOModule.sv")
val actualSV = source.mkString
source.close()
actualSV should include("foo")
actualSV should include("bar")
actualSV should include("myReg")
actualSV should include("output [7:0] out")
(actualSV should not).include("emptyBundle")
actualSV should include("bundle_x")
}
it("support peeking and poking FlatIO ports and other views of ports") {
import chisel3.experimental.dataview._
class SimpleModule extends Module {
val io = FlatIO(new Bundle {
val in = Input(UInt(8.W))
val out = Output(UInt(8.W))
})
val viewOfClock = clock.viewAs[Clock]
val delay = RegNext(io.in)
io.out := delay
}
new VerilatorSimulator("test_run_dir/simulator/flat_io_ports")
.simulate(new SimpleModule) { module =>
import PeekPokeAPI._
val dut = module.wrapped
dut.viewOfClock.step(2)
dut.io.in.poke(12.U)
dut.viewOfClock.step(1)
dut.io.out.peek()
dut.io.out.expect(12)
}
.result
}
it("has layers enabled") {
object AssertLayer extends Layer(LayerConfig.Extract())
class Foo extends Module {
val a = IO(Input(Bool()))
block(AssertLayer) {
chisel3.assert(a, "a must be true")
}
}
intercept[Exception] {
new VerilatorSimulator("test_run_dir/simulator/has_layers_enabled")
.simulate(new Foo) { module =>
import PeekPokeAPI._
val dut = module.wrapped
dut.a.poke(false.B)
dut.clock.step(1)
}
.result
}.getMessage should include("Assertion failed")
}
it("does not compile disabled layers (#4676)") {
import ltl.Sequence.BoolSequence
class Foo extends Module {
val a = IO(Input(Bool()))
// Verilator does not support s_eventually, which this lowers to.
block(layers.Verification.Assert.Temporal) {
ltl.AssertProperty(ltl.Property.eventually(a))
}
}
info("illegal constructs cause compilation failure")
intercept[Exception] {
new VerilatorSimulator("test_run_dir/simulator/does_not_compile_disabled_layers-enabledf")
.simulate(
new Foo,
settings = Settings.default[Foo].copy(verilogLayers = LayerControl.EnableAll)
) { _ => }
.result
}.getMessage() should include("Unsupported: s_eventually")
info("disabling unsupported constracts causes compilation to succeed using LayerControl.DisableAll")
new VerilatorSimulator("test_run_dir/simulator/does_not_compile_disabled_layers-disabled")
.simulate(new Foo, settings = Settings.default[Foo].copy(verilogLayers = LayerControl.DisableAll)) { _ => }
.result
info(
"disabling unsupported constracts causes compilation to succeed using LayerControl.Disable(layers.Verification.Assert.Temporal)"
)
new VerilatorSimulator("test_run_dir/simulator/does_not_compile_disabled_layers-disabled")
.simulate(
new Foo,
settings =
Settings.default[Foo].copy(verilogLayers = LayerControl.Disable(layers.Verification.Assert.Temporal))
) { _ => }
.result
}
}
}