diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 00000000..42c4a5e7 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,26 @@ +version = 2.7.5 + +maxColumn = 120 +align = most +continuationIndent.defnSite = 2 +assumeStandardLibraryStripMargin = true +docstrings = ScalaDoc +lineEndings = preserve +includeCurlyBraceInSelectChains = false +danglingParentheses = true + +align.tokens.add = [ + { + code = ":" + } +] + +newlines.alwaysBeforeCurlyBraceLambdaParams = false +newlines.alwaysBeforeMultilineDef = false +newlines.implicitParamListModifierForce = [before] + +verticalMultiline.atDefnSite = true + +optIn.annotationNewlines = true + +rewrite.rules = [SortImports, PreferCurlyFors, AvoidInfix] diff --git a/project/plugins.sbt b/project/plugins.sbt index 5708f81a..6d2d816c 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1 +1,3 @@ logLevel := Level.Warn + +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3") diff --git a/src/main/scala/gcd/DecoupledGCD.scala b/src/main/scala/gcd/DecoupledGCD.scala index 990bebb6..fbf5a7d1 100644 --- a/src/main/scala/gcd/DecoupledGCD.scala +++ b/src/main/scala/gcd/DecoupledGCD.scala @@ -13,11 +13,10 @@ class GcdInputBundle(val w: Int) extends Bundle { class GcdOutputBundle(val w: Int) extends Bundle { val value1 = UInt(w.W) val value2 = UInt(w.W) - val gcd = UInt(w.W) + val gcd = UInt(w.W) } -/** - * Compute Gcd using subtraction method. +/** 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. @@ -27,18 +26,18 @@ class DecoupledGcd(width: Int) extends MultiIOModule { 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 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 + input.ready := !busy output.valid := resultValid output.bits := DontCare - when(busy) { + when(busy) { when(x > y) { x := x - y }.otherwise { diff --git a/src/main/scala/gcd/GCD.scala b/src/main/scala/gcd/GCD.scala index 07e434ca..3cbb5cfb 100644 --- a/src/main/scala/gcd/GCD.scala +++ b/src/main/scala/gcd/GCD.scala @@ -4,25 +4,23 @@ package gcd import chisel3._ -/** - * Compute GCD using subtraction method. +/** 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 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 outputGCD = Output(UInt(16.W)) + val outputValid = Output(Bool()) }) - val x = Reg(UInt()) - val y = Reg(UInt()) + val x = Reg(UInt()) + val y = Reg(UInt()) - when(x > y) { x := x - y } - .otherwise { y := y - x } + when(x > y) { x := x - y }.otherwise { y := y - x } when(io.loadingValues) { x := io.value1 diff --git a/src/test/scala/gcd/GCDSpec.scala b/src/test/scala/gcd/GCDSpec.scala index bc163956..88897bae 100644 --- a/src/test/scala/gcd/GCDSpec.scala +++ b/src/test/scala/gcd/GCDSpec.scala @@ -7,8 +7,7 @@ import chisel3.tester._ import org.scalatest.FreeSpec import chisel3.experimental.BundleLiterals._ -/** - * This is a trivial example of how to run this Specification +/** This is a trivial example of how to run this Specification * From within sbt use: * {{{ * testOnly gcd.GcdDecoupledTester @@ -27,10 +26,10 @@ class GCDSpec extends FreeSpec with ChiselScalatestTester { 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 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) + new GcdOutputBundle(16).Lit(_.value1 -> x.U, _.value2 -> y.U, _.gcd -> BigInt(x).gcd(BigInt(y)).U) } fork {