summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/GrayCode.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/chisel3/util/GrayCode.scala')
-rw-r--r--src/main/scala/chisel3/util/GrayCode.scala24
1 files changed, 24 insertions, 0 deletions
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) })
+ }
+}