summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormergify[bot]2022-01-19 01:41:59 +0000
committerGitHub2022-01-19 01:41:59 +0000
commit042be4b2d8a846d12c5c5dfd64f10b5caee93125 (patch)
tree2382fbd3f514acd90a08470642ef30d5d3c70f9b
parentbf0f0d8545747c4aa1e5e2abe4e27e339006f86f (diff)
util: add GrayCode (#2353) (#2354)
(cherry picked from commit 91d7baa8e7162d736f46b6d4964b09016d2dd172) Co-authored-by: Kevin Laeufer <laeufer@cs.berkeley.edu>
-rw-r--r--integration-tests/src/test/scala/chiselTests/util/GrayCodeTests.scala42
-rw-r--r--src/main/scala/chisel3/util/GrayCode.scala24
2 files changed, 66 insertions, 0 deletions
diff --git a/integration-tests/src/test/scala/chiselTests/util/GrayCodeTests.scala b/integration-tests/src/test/scala/chiselTests/util/GrayCodeTests.scala
new file mode 100644
index 00000000..9562abb4
--- /dev/null
+++ b/integration-tests/src/test/scala/chiselTests/util/GrayCodeTests.scala
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: Apache-2.0
+
+package chiselTests.util
+
+import chisel3._
+import chisel3.util._
+import chiseltest._
+import chiseltest.formal._
+import org.scalatest.flatspec.AnyFlatSpec
+
+class GrayCodeTests extends AnyFlatSpec with ChiselScalatestTester with Formal {
+ behavior.of("GrayCode")
+
+ val Widths = Seq(1, 2, 3, 5, 8, 17, 65)
+ Widths.foreach { w =>
+ it should s"maintain identity (width=$w)" in {
+ verify(new GrayCodeIdentityCheck(w), Seq(BoundedCheck(1)))
+ }
+
+ it should s"ensure hamming distance of one (width=$w)" in {
+ verify(new GrayCodeHammingCheck(w), Seq(BoundedCheck(1)))
+ }
+ }
+}
+
+/** Checks that when we go from binary -> gray -> binary the result is always the same as the input. */
+private class GrayCodeIdentityCheck(width: Int) extends Module {
+ val in = IO(Input(UInt(width.W)))
+ val gray = BinaryToGray(in)
+ val out = GrayToBinary(gray)
+ assert(in === out, "%b -> %b -> %b", in, gray, out)
+}
+
+/** Checks that if we increment the binary number, the gray code equivalent only changes by one bit. */
+private class GrayCodeHammingCheck(width: Int) extends Module {
+ val a = IO(Input(UInt(width.W)))
+ val b = a + 1.U
+ val aGray = BinaryToGray(a)
+ val bGray = BinaryToGray(b)
+ val hamming = PopCount(aGray ^ bGray)
+ assert(hamming === 1.U, "%b ^ %b = %b", aGray, bGray, hamming)
+}
diff --git a/src/main/scala/chisel3/util/GrayCode.scala b/src/main/scala/chisel3/util/GrayCode.scala
new file mode 100644
index 00000000..ef310ee9
--- /dev/null
+++ b/src/main/scala/chisel3/util/GrayCode.scala
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: Apache-2.0
+
+package chisel3.util
+
+import chisel3._
+
+object BinaryToGray {
+
+ /** Turns a binary number into gray code. */
+ def apply(in: UInt): UInt = in ^ (in >> 1)
+}
+
+object GrayToBinary {
+
+ /** Inverts the [[BinaryToGray]] operation. */
+ def apply(in: UInt, width: Int): UInt = apply(in(width - 1, 0))
+
+ /** Inverts the [[BinaryToGray]] operation. */
+ def apply(in: UInt): UInt = if (in.getWidth < 2) { in }
+ else {
+ val bits = in.getWidth - 2 to 0 by -1
+ Cat(bits.scanLeft(in.head(1)) { case (prev, ii) => prev ^ in(ii) })
+ }
+}