-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCDUnitTest.scala
104 lines (93 loc) · 2.76 KB
/
GCDUnitTest.scala
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
import chisel3.iotesters
import chisel3.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}
class GCDUnitTester(c: GCD) extends PeekPokeTester(c) {
/**
* compute the gcd and the number of steps it should take to do it
*
* @param a positive integer
* @param b positive integer
* @return the GCD of a and b
*/
def computeGcd(a: Int, b: Int): (Int, Int) = {
var x = a
var y = b
var depth = 1
while (y > 0) {
if (x > y) {
x -= y
}
else {
y -= x
}
depth += 1
}
(x, depth)
}
private val gcd = c
for (i <- 1 to 40 by 3) {
for (j <- 1 to 40 by 7) {
poke(gcd.io.value1, i)
poke(gcd.io.value2, j)
poke(gcd.io.loadingValues, 1)
step(1)
poke(gcd.io.loadingValues, 0)
val (expected_gcd, steps) = computeGcd(i, j)
step(steps - 1) // -1 is because we step(1) already to toggle the enable
expect(gcd.io.outputGCD, expected_gcd)
expect(gcd.io.outputValid, 1)
}
}
}
/**
* This is a trivial example of how to run this Specification
* From within sbt use:
* {{{
* testOnly example.test.GCDTester
* }}}
* From a terminal shell use:
* {{{
* sbt 'testOnly example.test.GCDTester'
* }}}
*/
class GCDTester extends ChiselFlatSpec {
private val backendNames = if (firrtl.FileUtils.isCommandAvailable("verilator")) {
Array("firrtl", "verilator")
}
else {
Array("firrtl")
}
for (backendName <- backendNames) {
"GCD" should s"calculate proper greatest common denominator (with $backendName)" in {
Driver(() => new GCD, backendName) {
c => new GCDUnitTester(c)
} should be(true)
}
}
"Basic test using Driver.execute" should "be used as an alternative way to run specification" in {
iotesters.Driver.execute(Array(), () => new GCD) {
c => new GCDUnitTester(c)
} should be(true)
}
"using --backend-name verilator" should "be an alternative way to run using verilator" in {
if (backendNames.contains("verilator")) {
iotesters.Driver.execute(Array("--backend-name", "verilator"), () => new GCD) {
c => new GCDUnitTester(c)
} should be(true)
}
}
"running with --is-verbose" should "show more about what's going on in your tester" in {
iotesters.Driver.execute(Array("--is-verbose"), () => new GCD) {
c => new GCDUnitTester(c)
} should be(true)
}
"running with --fint-write-vcd" should "create a vcd file from your test" in {
iotesters.Driver.execute(Array("--fint-write-vcd"), () => new GCD) {
c => new GCDUnitTester(c)
} should be(true)
}
"using --help" should s"show the many options available" in {
iotesters.Driver.execute(Array("--help"), () => new GCD) {
c => new GCDUnitTester(c)
} should be(true)
}
}