1
votes

I know that SInt is for Signed numbers and UInt is for Unsigned numbers, so If I used SInt instead of UInt it will infer a sign extension for example?

Another thing I am confused with is the difference between Bits and UInt types. Can someone clarify this to me?

Is it safe to use UInt all the time while designing with Chisel?

2

2 Answers

3
votes

You can use UInt everywhere, and for beginners, arguably you should do so. But using more appropriate types allow you to type check your design so you don't mistakenly use an operator/function that doesn't apply to your type.

1
votes

There are designs that you can use use UInt for every number, but sometimes it is necessary to use SInt.

For example, say that you load two uint8 values from memory and then store the difference back in the memory. There is no way to tell if this difference is positive or negative.

1) 01111011 (123) - 00010110 (22) = 01100101 (101) so you would store 01100101

2) 00010110 (22) - 11110111 (123) = 10011011 (-101) so you would store 10011011

When you load back these numbers after a while there is no way to tell if 10011011 is the number 155 or -101 except if you load it as SInt. You should be sure that the result will fit in the Output and won't be truncated.

package StackOverflow

import chisel3._

class UIntSInt extends Module {
  val io = IO(new Bundle {
    val x = Input(UInt(8.W))
    val y = Input(UInt(8.W))
    val z = Output(SInt(9.W))
  })
  io.z := (io.x -& io.y).asSInt
}

class UIntSIntUnitTest(c: UIntSInt) extends chisel3.iotesters.PeekPokeTester(c) {
  poke(c.io.x, 22)
  poke(c.io.y, 124)
  println(s"Result: ${peek(c.io.z)}")
}

object UIntSIntTest extends App {
  chisel3.iotesters.Driver.execute(args, () => new UIntSInt) {
    c => new UIntSIntUnitTest(c)
  }
}