File tree Expand file tree Collapse file tree 2 files changed +28
-18
lines changed Expand file tree Collapse file tree 2 files changed +28
-18
lines changed Original file line number Diff line number Diff line change @@ -3,23 +3,33 @@ package examples
33
44import 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+ */
612class 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments