Search code examples
scalaintbitchiseluint

why chisel UInt(32.W) can not take a unsigned number which bit[32] happens to be 1?


It is defined that UInt is the type of unsigned integer. But in such case it seems like the MSB is still a sign. e.g., the most relative QA is Chisel UInt negative value error which works out a workaround but no why. Could you enlight me about the 'why'?

The UInt seems to be defined in chisel3/chiselFrontend/src/main/scala/chisel3/core/Bits.scala but I cannot understand the details. Is the UInt is derived from Bits and Bits is derived from Int of scala?


Solution

  • The simple answer is that this is due to how Scala evaluates things. Consider an example like

    val x = 0xFFFFFFFF.U
    

    This statement causes an error. UInt literal are represented internally by BigInts, but the 0xFFFFFFFF is an specifying an Int value. 0xFFFFFFFF is equivalent to the Int value -1. The -1 Int value is converted to BigInt -1 and -1.U is illegal because the .U literal creation method will not accept negative values. Adding the L fixes this because 0xFFFFFFFL is a positive Long value.