-
Notifications
You must be signed in to change notification settings - Fork 175
/
GCDUnitTest.scala
61 lines (52 loc) · 1.34 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
// See LICENSE for license details.
package example.test
import Chisel.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}
import example.GCD
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.a, i)
poke(gcd.io.b, j)
poke(gcd.io.e, 1)
step(1)
poke(gcd.io.e, 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.z, expected_gcd)
expect(gcd.io.v, 1)
}
}
}
class GCDTester extends ChiselFlatSpec {
private val backendNames = Array[String]("firrtl", "verilator")
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)
}
}
}