Skip to content

Commit 2827cb9

Browse files
authored
Improve descriptiveness of GCD example (#113)
1 parent fc6e6f7 commit 2827cb9

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/main/scala/examples/GCD.scala

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@ package examples
33

44
import chisel3._
55

6+
/**
7+
* Compute the GCD of 'a' and 'b' using Euclid's algorithm.
8+
* To start a computation, load the values into 'a' and 'b' and toggle 'load'
9+
* high.
10+
* The GCD will be returned in 'out' when 'valid' is high.
11+
*/
612
class GCD extends Module {
713
val io = IO(new Bundle {
8-
val a = Input(UInt(16.W))
9-
val b = Input(UInt(16.W))
10-
val e = Input(Bool())
11-
val z = Output(UInt(16.W))
12-
val v = Output(Bool())
14+
val a = Input(UInt(16.W))
15+
val b = Input(UInt(16.W))
16+
val load = Input(Bool())
17+
val out = Output(UInt(16.W))
18+
val valid = Output(Bool())
1319
})
20+
val x = Reg(UInt())
21+
val y = Reg(UInt())
1422

15-
val x = Reg(UInt())
16-
val y = Reg(UInt())
17-
when (x > y) {
18-
x := x - y
23+
when (io.load) {
24+
x := io.a; y := io.b
25+
} .otherwise {
26+
when (x > y) {
27+
x := x - y
28+
} .elsewhen (x <= y) {
29+
y := y - x
30+
}
1931
}
20-
.elsewhen (x <= y) { y := y - x }
2132

22-
when (io.e) { x := io.a; y := io.b }
23-
io.z := x
24-
io.v := y === 0.U
25-
}
33+
io.out := x
34+
io.valid := y === 0.U
35+
}

src/test/scala/examples/GCDTests.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ class GCDTests(c: GCD) extends PeekPokeTester(c) {
1313
poke(c.io.a, inputs(i)._1)
1414
poke(c.io.b, inputs(i)._2)
1515

16-
poke(c.io.e, 1)
16+
poke(c.io.load, 1)
1717
step(1)
18-
poke(c.io.e, 0)
18+
poke(c.io.load, 0)
1919

2020
var ready = false
2121

2222
do {
23-
ready = peek(c.io.v) == 1
23+
ready = peek(c.io.valid) == 1
2424
step(1)
2525
} while (t < 100 && ! ready)
2626

27-
expect(c.io.z, outputs(i))
27+
expect(c.io.out, outputs(i))
2828
i += 1
2929
} while (t < 100 && i < 3)
3030

0 commit comments

Comments
 (0)