Skip to content

Instantly share code, notes, and snippets.

@Wonicon
Created March 3, 2017 12:13
Show Gist options
  • Save Wonicon/719219220a9fe55a73c4d973d134451f to your computer and use it in GitHub Desktop.
Save Wonicon/719219220a9fe55a73c4d973d134451f to your computer and use it in GitHub Desktop.
[rocket-chip] Generate a bit array whose length equals to a UInt
/**
* The `UIntToOH1` method is found from rocket-chip repo at
* https://github.com/ucb-bar/rocket-chip/blob/dfa61bc48709a7733b73d24eb81acc2da227cfd4/src/main/scala/uncore/tilelink2/package.scala#L19
* As it is hard to me to figure out what the abbreviation of OH and OH1 is,
* I write this standalone demo in order to find out its semnatic from the pattern output.
*
* Finally, I find that the `UIntToOH1` method will convert a UInt value to bits of 1 whose length equals to that value.
*
* To run this demo:
* 1. git clone https://github.com/ucb-bar/chisel-template demo
* 2. cd demo
* 3. wget this script
* 4. sbt "runMain OH1"
*
* References:
* https://github.com/ucb-bar/rocket-chip
* https://github.com/ucb-bar/chisel-template
* https://github.com/ucb-bar/chisel-testers
*/
import chisel3._
import chisel3.util._
import chisel3.iotesters._
class OH1 extends Module {
val inputWidth = 19 // Width of dshl shift amount cannot be larger than 20 bits
val outputWidth = 64
val io = IO(new Bundle {
val x = Input(UInt(width = inputWidth))
val y = Output(UInt(width = outputWidth))
})
def UIntToOH1(x: UInt) = ~(SInt(-1, width = outputWidth).asUInt << x)(outputWidth - 1, 0)
io.y := UIntToOH1(io.x)
}
class Tester(oh1: OH1) extends PeekPokeTester(oh) {
for (x <- 0 to 30) {
poke(oh1.io.x, x)
println(s"x: ${x} -> oh1: ${peek(oh1.io.y).toInt.toBinaryString}")
}
}
object OH1 extends App {
chisel3.iotesters.Driver(() => new OH1) { oh1 => new Tester(oh1) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment