Initial commit
This commit is contained in:
73
src/main/scala/gcd/DecoupledGCD.scala
Normal file
73
src/main/scala/gcd/DecoupledGCD.scala
Normal file
@@ -0,0 +1,73 @@
|
||||
// See README.md for license details.
|
||||
|
||||
package gcd
|
||||
|
||||
import chisel3._
|
||||
import chisel3.util.Decoupled
|
||||
|
||||
class GcdInputBundle(val w: Int) extends Bundle {
|
||||
val value1 = UInt(w.W)
|
||||
val value2 = UInt(w.W)
|
||||
}
|
||||
|
||||
class GcdOutputBundle(val w: Int) extends Bundle {
|
||||
val value1 = UInt(w.W)
|
||||
val value2 = UInt(w.W)
|
||||
val gcd = UInt(w.W)
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute Gcd using subtraction method.
|
||||
* Subtracts the smaller from the larger until register y is zero.
|
||||
* value input register x is then the Gcd.
|
||||
* Unless first input is zero then the Gcd is y.
|
||||
* Can handle stalls on the producer or consumer side
|
||||
*/
|
||||
class DecoupledGcd(width: Int) extends Module {
|
||||
val input = IO(Flipped(Decoupled(new GcdInputBundle(width))))
|
||||
val output = IO(Decoupled(new GcdOutputBundle(width)))
|
||||
|
||||
val xInitial = Reg(UInt())
|
||||
val yInitial = Reg(UInt())
|
||||
val x = Reg(UInt())
|
||||
val y = Reg(UInt())
|
||||
val busy = RegInit(false.B)
|
||||
val resultValid = RegInit(false.B)
|
||||
|
||||
input.ready := ! busy
|
||||
output.valid := resultValid
|
||||
output.bits := DontCare
|
||||
|
||||
when(busy) {
|
||||
when(x > y) {
|
||||
x := x - y
|
||||
}.otherwise {
|
||||
y := y - x
|
||||
}
|
||||
when(x === 0.U || y === 0.U) {
|
||||
when(x === 0.U) {
|
||||
output.bits.gcd := y
|
||||
}.otherwise {
|
||||
output.bits.gcd := x
|
||||
}
|
||||
|
||||
output.bits.value1 := xInitial
|
||||
output.bits.value2 := yInitial
|
||||
resultValid := true.B
|
||||
|
||||
when(output.ready && resultValid) {
|
||||
busy := false.B
|
||||
resultValid := false.B
|
||||
}
|
||||
}
|
||||
}.otherwise {
|
||||
when(input.valid) {
|
||||
val bundle = input.deq()
|
||||
x := bundle.value1
|
||||
y := bundle.value2
|
||||
xInitial := bundle.value1
|
||||
yInitial := bundle.value2
|
||||
busy := true.B
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/main/scala/gcd/GCD.scala
Normal file
34
src/main/scala/gcd/GCD.scala
Normal file
@@ -0,0 +1,34 @@
|
||||
// See README.md for license details.
|
||||
|
||||
package gcd
|
||||
|
||||
import chisel3._
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
val io = IO(new 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())
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
52
src/test/scala/gcd/GCDSpec.scala
Normal file
52
src/test/scala/gcd/GCDSpec.scala
Normal file
@@ -0,0 +1,52 @@
|
||||
// See README.md for license details.
|
||||
|
||||
package gcd
|
||||
|
||||
import chisel3._
|
||||
import chiseltest._
|
||||
import org.scalatest.freespec.AnyFreeSpec
|
||||
import chisel3.experimental.BundleLiterals._
|
||||
|
||||
/**
|
||||
* This is a trivial example of how to run this Specification
|
||||
* From within sbt use:
|
||||
* {{{
|
||||
* testOnly gcd.GcdDecoupledTester
|
||||
* }}}
|
||||
* From a terminal shell use:
|
||||
* {{{
|
||||
* sbt 'testOnly gcd.GcdDecoupledTester'
|
||||
* }}}
|
||||
*/
|
||||
class GCDSpec extends AnyFreeSpec with ChiselScalatestTester {
|
||||
|
||||
"Gcd should calculate proper greatest common denominator" in {
|
||||
test(new DecoupledGcd(16)) { dut =>
|
||||
dut.input.initSource()
|
||||
dut.input.setSourceClock(dut.clock)
|
||||
dut.output.initSink()
|
||||
dut.output.setSinkClock(dut.clock)
|
||||
|
||||
val testValues = for { x <- 0 to 10; y <- 0 to 10} yield (x, y)
|
||||
val inputSeq = testValues.map { case (x, y) => (new GcdInputBundle(16)).Lit(_.value1 -> x.U, _.value2 -> y.U) }
|
||||
val resultSeq = testValues.map { case (x, y) =>
|
||||
(new GcdOutputBundle(16)).Lit(_.value1 -> x.U, _.value2 -> y.U, _.gcd -> BigInt(x).gcd(BigInt(y)).U)
|
||||
}
|
||||
|
||||
fork {
|
||||
// push inputs into the calculator, stall for 11 cycles one third of the way
|
||||
val (seq1, seq2) = inputSeq.splitAt(resultSeq.length / 3)
|
||||
dut.input.enqueueSeq(seq1)
|
||||
dut.clock.step(11)
|
||||
dut.input.enqueueSeq(seq2)
|
||||
}.fork {
|
||||
// retrieve computations from the calculator, stall for 10 cycles one half of the way
|
||||
val (seq1, seq2) = resultSeq.splitAt(resultSeq.length / 2)
|
||||
dut.output.expectDequeueSeq(seq1)
|
||||
dut.clock.step(10)
|
||||
dut.output.expectDequeueSeq(seq2)
|
||||
}.join()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user