How to convert Chisel.UInt to scala Int?

3,059 views
Skip to first unread message

Ziqiang Li

unread,
May 21, 2015, 8:50:59 AM5/21/15
to chisel...@googlegroups.com
How to convert Chisel.UInt to scala Int?

Jim Lawson

unread,
May 21, 2015, 12:49:54 PM5/21/15
to chisel...@googlegroups.com
In general, this is not possible. A UInt specifies the type of a run-time signal which may only have a specific value during circuit emulation. If you want to convert a UInt() value to a Scala Int during simulation, use the peek() method.

Having said that, if the UInt is a literal, you can convert it to a Scala BigInt using the litValue() method.

Here’s some code demonstrating both methods:

import Chisel._

class LitToInt extends Module {
val io = new Bundle {
val out = UInt(width = 16, dir = OUTPUT)
}
val aUInt = UInt(42)
println("UInt literal (compile time) " + aUInt.litValue())
// Put the value where we can read it at runtime.
io.out := aUInt
}

class LitToIntTest(c: LitToInt) extends Tester(c) {
// Read the literal value directly from the node in the graph.
val aUIntLit = c.aUInt.litValue()
// Read the literal value through an io port in the simulation.
val io_out = peek(c.io.out)
println("UInt literal (runtime node) " + aUIntLit + ", (runtime port) " + io_out)
}

object LitToIntTest {
def main(args: Array[String]): Unit = {
chiselMainTest(Array("--genHarness", "--compile", "--test"),
() => Module(new LitToInt())){
c => new LitToIntTest(c)}
Reply all
Reply to author
Forward
0 new messages