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)
}
}