1

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?

4

2 回答 2

3

你可以在任何地方使用 UInt,对于初学者来说,可以说你应该这样做。但是使用更合适的类型可以让您对设计进行类型检查,这样您就不会错误地使用不适用于您的类型的运算符/函数。

于 2016-09-14T16:39:32.167 回答
1

有些设计可以为每个数字使用 UInt,但有时需要使用 SInt。

例如,假设您从内存中加载两个 uint8 值,然后将差值存储回内存中。无法判断这种差异是正面的还是负面的。

1) 01111011 (123) - 00010110 (22) = 01100101 (101) 所以你将存储 01100101

2) 00010110 (22) - 11110111 (123) = 10011011 (-101) 所以你将存储 10011011

当您在一段时间后重新加载这些数字时,除非您将其加载为 SInt,否则无法判断 10011011 是数字 155 还是 -101。您应该确保结果适合输出并且不会被截断。

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)
  }
}
于 2018-09-26T12:08:47.967 回答