diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 00000000..f74e5504 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,26 @@ +version = 2.6.4 + +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..1bea0b39 100644 --- a/src/main/scala/gcd/DecoupledGCD.scala +++ b/src/main/scala/gcd/DecoupledGCD.scala @@ -13,7 +13,7 @@ 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) } /** @@ -27,18 +27,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..e14b3b22 100644 --- a/src/main/scala/gcd/GCD.scala +++ b/src/main/scala/gcd/GCD.scala @@ -11,18 +11,17 @@ import chisel3._ */ 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..d579a05f 100644 --- a/src/test/scala/gcd/GCDSpec.scala +++ b/src/test/scala/gcd/GCDSpec.scala @@ -3,9 +3,9 @@ package gcd import chisel3._ -import chisel3.tester._ -import org.scalatest.FreeSpec +import chiseltest._ import chisel3.experimental.BundleLiterals._ +import org.scalatest.freespec.AnyFreeSpec /** * This is a trivial example of how to run this Specification @@ -18,7 +18,7 @@ import chisel3.experimental.BundleLiterals._ * sbt 'testOnly gcd.GcdDecoupledTester' * }}} */ -class GCDSpec extends FreeSpec with ChiselScalatestTester { +class GCDSpec extends AnyFreeSpec with ChiselScalatestTester { "Gcd should calculate proper greatest common denominator" in { test(new DecoupledGcd(16)) { dut => @@ -27,10 +27,11 @@ 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 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) + 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 {