-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCD.scala
executable file
·40 lines (31 loc) · 936 Bytes
/
GCD.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
#!/usr/bin/env amm
import $ivy.`edu.berkeley.cs::chisel3:3.4.+`, chisel3._
class GCDIO extends Bundle {
val value1 = Input(UInt(16.W))
val value2 = Input(UInt(16.W))
val loadingValues = Input(Bool())
val outputGCD = Output(UInt(16.W))
val outputValid = Output(Bool())
}
/**
* Compute GCD using subtraction method.
* Subtracts the smaller from the larger until register y is zero.
* value in register x is then the GCD
*/
class GCD extends Module {
// This is an artefact of ammonite's REPL de-conflicting class names
override def desiredName = "GCD"
val io = IO(new GCDIO)
val x = Reg(UInt())
val y = Reg(UInt())
when(x > y) { x := x - y }
.otherwise { y := y - x }
when(io.loadingValues) {
x := io.value1
y := io.value2
}
io.outputGCD := x
io.outputValid := y === 0.U
}
@main
def main(args: String*) = chisel3.Driver.execute(args.toArray, () => new GCD)