Not sure what exactly you are trying to do. UInt
is a chisel data type, mean while Int
is a scala data type.
Chisel datatypes are used to specify the type of values held in state elements or flowing on wires. While hardware designs ultimately operate on vectors of binary digits, other more abstract representations for values allow clearer specifications and help the tools generate more optimal circuits.
Based on the discussion How to convert Chisel.UInt to scala Int? it seems that it doesn't make sense what you are trying to do
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.
It also provides some code, but I guess it's a bit outdated due to the question was done in May 21, 2015. So, I'm gonna add another example from the repo schoeberl - chisel-examples
scalaVersion := "2.12.13"
// Chisel 3.5
addCompilerPlugin("edu.berkeley.cs" % "chisel3-plugin" % "3.5.3" cross CrossVersion.full)
libraryDependencies += "edu.berkeley.cs" %% "chisel3" % "3.5.3"
libraryDependencies += "edu.berkeley.cs" %% "chiseltest" % "0.5.3"
import chisel3._
/**
* The blinking LED component.
*/
class Hello extends Module {
val io = IO(new Bundle {
val led = Output(UInt(1.W))
})
val CNT_MAX = (50000000 / 2 - 1).U
val cntReg = RegInit(0.U(32.W))
val blkReg = RegInit(0.U(1.W))
cntReg := cntReg + 1.U
when(cntReg === CNT_MAX) {
cntReg := 0.U
blkReg := ~blkReg
}
io.led := blkReg
}
/**
* An object extending App to generate the Verilog code.
*/
object Hello extends App {
(new chisel3.stage.ChiselStage).emitVerilog(new Hello())
}
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
class HelloTest extends AnyFlatSpec with ChiselScalatestTester {
behavior of "Hello"
it should "pass" in {
test(new Hello) { c => //
c.clock.setTimeout(0)
var ledStatus = BigInt(-1)
println("Start the blinking LED")
for (_ <- 0 until 100) {
c.clock.step(10000)
val ledNow = c.io.led.peek().litValue
val s = if (ledNow == 0) "o" else "*"
if (ledStatus != ledNow) {
System.out.println(s)
ledStatus = ledNow
}
}
println("\nEnd the blinking LED")
}
}
}
The error you got comes from scala. Here you have some articles that talk about that
Reason
Some Options are Nones, and get deals with them by throwing an exception:
None.get
// java.util.NoSuchElementException: None.get
// at scala.None$.get(Option.scala:366)
// at repl.Session$App$$anonfun$1.apply(option_get.md:9)
// at repl.Session$App$$anonfun$1.apply(option_get.md:9)
If you have a default value to provide for the None case, use getOrElse:
None.getOrElse(-1)
// res0: Int = -1
Scala’s solution in Scala’s way:
Make use of Option
, Some
, None
.
But what it is ???
Option
acts sort of like a container of a reference if a reference is not empty i.e. it is pointing to some memory location then it returns Some
otherwise None
.
If you look at the code of the function litValue from abstract class Data
/**
* Returns the literal value if this is a literal that is representable as bits, otherwise crashes.
*/
def litValue: BigInt = litOption.get
As it was detailed in the previous articles shared, it's not a good practice to call Option.get
directly. You can also have the method litOption
/**
* If this is a literal that is representable as bits, returns the value as a BigInt.
* If not a literal, or not representable as bits (for example, is or contains Analog), returns None.
*/
def litOption: Option[BigInt]
This one returns an Option[BigInt]
, which means you can take some an action using Pattern Matching or just return a default value using getOrElse
.