Search code examples
chisel

Printing UInt and SInt values in CHISEL


Is it possible to print UInt and SInt values in CHISEL?

I have the code below inside a Module.

  val foo = 0.S(2.W)
  var min : SInt = -2.S 
  println(s"DEEEEBUG Values of foo: $foo min: ${min.toString()}")
  min= min + 1.S
  println(s"min: ${min.toString()}") 

But the last line does not print the correct value of min. This is the output

DEEEEBUG Values of foo: SInt<2>(0) max: SInt<2>(-2)
min: SInt<2>(OpResult in TwoLevelBpred)

How do I print the value of min after the addition?


Solution

  • Simple answer is you should be doing something like this.

    class MyCircuit extends MultiIOModule {
      val foo = 0.S(2.W)
      var min : SInt = -2.S
      printf(s"DEEEEBUG Values of foo: %d min: %d\n", foo, min)
      val sum = min + 1.S
      printf(s"sum: %d\n", sum) 
    }
    

    Chisel is a circuit generator. The circuit is built when your code is compiled and run. Println's are scala constructs that only happen during the circuit construction. The circuit that is built must then be executed by a simulator of some sort. Printf's can be used to print values from the circuit during simulation. I would recommend working through the Chisel Bootcamp to understand more about how chisel works.

    I introduced the new variable/wire sum in your example because min = min + 1.S does not mean the same thing as you might expect in an ordinary software program. Again checkout the bootcamp