Created
March 5, 2017 13:26
-
-
Save Wonicon/379680704b22e1697f7edbd49cbb4b28 to your computer and use it in GitHub Desktop.
GrayCounter found in rocket-chip repo, and verify their index generation logic (why double the gray code space and then calc them down?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chisel3._ | |
import chisel3.util._ | |
import chisel3.iotesters._ | |
object GrayCounter { | |
def apply(bits: Int, inc: Bool = Bool(true), clear: Bool = Bool(false)): UInt = { | |
// Incremented | |
val next = Wire(UInt(width = bits)) | |
val curr = Reg(next = next) | |
next := Mux(clear, UInt(0), curr + inc.asUInt) | |
next ^ (next >> UInt(1)) | |
} | |
} | |
class Index(val depth: Int) extends Module { | |
val bits = log2Ceil(depth) | |
val io = IO(new Bundle { | |
val gray = Output(UInt((bits + 1).W)) | |
val index = Output(UInt(bits.W)) | |
}) | |
io.gray := GrayCounter(bits + 1) | |
io.index := io.gray(bits - 1, 0) ^ (io.gray(bits, bits) << (bits - 1)) | |
} | |
class TestIndex(idx: Index) extends PeekPokeTester(idx) { | |
def binary(u: UInt): String = peek(u).toInt.toBinaryString | |
for (x <- 0 until idx.depth * 2) { | |
step(1) | |
println(s"gray: ${binary(idx.io.gray)}, index: ${binary(idx.io.index)}") | |
} | |
} | |
object Index extends App { | |
chisel3.iotesters.Driver(() => new Index(4)) { idx => new TestIndex(idx) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment