From 6a3f5d0a48dff157ebf82f4fb6e973931e0f26e1 Mon Sep 17 00:00:00 2001 From: chick Date: Sun, 31 Jan 2021 18:24:12 -0800 Subject: [PATCH 1/3] Add scalafmt Apply it to all scala files --- .scalafmt.conf | 26 ++++++++++++++++++++++++++ src/main/scala/gcd/DecoupledGCD.scala | 19 +++++++++---------- src/main/scala/gcd/GCD.scala | 15 +++++++-------- src/test/scala/gcd/GCDSpec.scala | 7 ++++--- 4 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 .scalafmt.conf diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 00000000..c53cb608 --- /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] \ No newline at end of file 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..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..5e6f7c26 100644 --- a/src/test/scala/gcd/GCDSpec.scala +++ b/src/test/scala/gcd/GCDSpec.scala @@ -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 { From 94c8c8f0223f18b9c6e5c76f3a72ec67632840d1 Mon Sep 17 00:00:00 2001 From: chick Date: Sat, 16 Oct 2021 10:11:38 -0700 Subject: [PATCH 2/3] Add scalafmt to template --- .scalafmt.conf | 2 +- project/plugins.sbt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index c53cb608..42c4a5e7 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -23,4 +23,4 @@ verticalMultiline.atDefnSite = true optIn.annotationNewlines = true -rewrite.rules = [SortImports, PreferCurlyFors, AvoidInfix] \ No newline at end of file +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") From b193558459d1713e7fefd7fa1eb6bc69904cf437 Mon Sep 17 00:00:00 2001 From: chick Date: Sat, 16 Oct 2021 10:17:17 -0700 Subject: [PATCH 3/3] Add scalafmt to template Add .scalafmt.conf configuration file Add the scalafmt plugin Run scalafmtAll to format example code --- src/main/scala/gcd/GCD.scala | 3 +-- src/test/scala/gcd/GCDSpec.scala | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/scala/gcd/GCD.scala b/src/main/scala/gcd/GCD.scala index e14b3b22..3cbb5cfb 100644 --- a/src/main/scala/gcd/GCD.scala +++ b/src/main/scala/gcd/GCD.scala @@ -4,8 +4,7 @@ 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 */ diff --git a/src/test/scala/gcd/GCDSpec.scala b/src/test/scala/gcd/GCDSpec.scala index 5e6f7c26..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 @@ -28,10 +27,9 @@ class GCDSpec extends FreeSpec with ChiselScalatestTester { 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) + 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 {