diff options
| author | Jack | 2022-02-08 02:11:51 +0000 |
|---|---|---|
| committer | Jack | 2022-02-08 02:11:51 +0000 |
| commit | 4da4f252c3d7c834e13bb8e91a69cfe772996452 (patch) | |
| tree | 5acc86ebf6c429efc051954c6977ed2394498dbc /src/main/scala/chisel3/util/GrayCode.scala | |
| parent | 93d17165cc5339de3e2dc7cd9e10dd3634b49bac (diff) | |
| parent | 9d1e2082df4ecb2942a28b7039eb2ff36953380c (diff) | |
Merge branch '3.5.x' into 3.5-release
Diffstat (limited to 'src/main/scala/chisel3/util/GrayCode.scala')
| -rw-r--r-- | src/main/scala/chisel3/util/GrayCode.scala | 24 |
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) }) + } +} |
