在使用新的ChiselTest框架进行单元测试时,我遇到了将有符号整型转换为无符号整型的正确方法。
下面是我用来对ALU (例如16位)进行单元测试的方法,问题是它是不可伸缩的:
    test(new ALU) { c =>
    ...
        /* Sub */
        c.io.ctl.poke(Control.ALU_SUB)
        c.io.src0.poke(4321.U)
        c.io.src1.poke(1234.U)
        c.io.out.expect(3087.U)
        c.io.src0.poke(1234.U)
        c.io.src1.poke(4321.U)
        c.io.out.expect("b_1111_0011_1111_0001".U) /* 2's compliment */
    }这种方法的问题是,当我生成一个32位的ALU (它输出一个无符号的int)时,单元测试将失败,因为使用上面的hacky string方法,字符串将从零扩展到32位...
我想像这样重写测试:
test(new ALU) { c =>
    /* Sub */
    c.io.ctl.poke(Control.ALU_SUB)
    c.io.src0.poke(4321.U)
    c.io.src1.poke(1234.U)
    c.io.out.expect(3087.U)
    c.io.src0.poke(1234.U)
    c.io.src1.poke(4321.U)
    c.io.out.expect(-3087.S.asUInt)
}但是,尽管设计确实很详细,但我得到了以下错误:
[error] (run-main-9) chisel3.internal.ChiselException: Error: Not in a UserModule. Likely cause: Missed Module() wrap, bare chisel API call, or attempting to construct hardware inside a BlackBox.
[error] chisel3.internal.ChiselException: Error: Not in a UserModule. Likely cause: Missed Module() wrap, bare chisel API call, or attempting to construct hardware inside a BlackBox.
[error]         at chisel3.internal.throwException$.apply(Error.scala:85)
[error]         at chisel3.internal.Builder$.forcedUserModule(Builder.scala:298)
[error]         at chisel3.internal.Builder$.pushOp(Builder.scala:336)
[error]         at chisel3.SInt.do_asUInt(Bits.scala:925)
[error]         at cpu.alu$.$anonfun$new$2(Main.scala:26)
[error]         at cpu.alu$.$anonfun$new$2$adapted(Main.scala:18)
[error]         at chiseltest.backends.treadle.TreadleBackend.$anonfun$run$1(TreadleBackend.scala:144)
[error]         at chiseltest.internal.ThreadedBackend$TesterThread$$anon$1.$anonfun$run$1(ThreadedBackend.scala:453)
[error]         at chiseltest.backends.treadle.TreadleBackend.doTimescope(TreadleBackend.scala:103)
[error]         at chiseltest.internal.ThreadedBackend$TesterThread$$anon$1.run(ThreadedBackend.scala:453)
[error]         at java.lang.Thread.run(Thread.java:748)
[error] stack trace is suppressed; run last Test / bgRunMain for the full output
[error] Nonzero exit code: 1
[error] (Test / runMain) Nonzero exit code: 1我想不出解决这个问题的正确方法。任何帮助都将不胜感激。
作为参考,ALU非常简单,下面是ALU类:
class ALU extends Module {
  val io = IO(new Bundle {
    val ctl   = Input(UInt(Control.ALU_BITWIDTH))
    val src0  = Input(UInt(Instructions.WORD_SIZE.W))
    val src1  = Input(UInt(Instructions.WORD_SIZE.W))
    val out   = Output(UInt(Instructions.WORD_SIZE.W))
  })
  val shift = io.src1(3,0).asUInt
  /* Lookup the operation to execute */
  io.out := MuxLookup(io.ctl, 0.U, Seq(
    Control.ALU_ADD -> (io.src0 + io.src1),
    Control.ALU_SUB -> (io.src0 - io.src1),
    Control.ALU_AND -> (io.src0 & io.src1),
    Control.ALU_OR  -> (io.src0 | io.src1),
    Control.ALU_XOR -> (io.src0 ^ io.src1),
    Control.ALU_NOT -> (~io.src0),
    Control.ALU_SLL -> (io.src0 << shift),
    Control.ALU_SRL -> (io.src0 >> shift),
  ))
}发布于 2021-05-28 09:52:04
基于@tonysamaritano的解决方案做了一些改进。编写一个独立的Scala函数,让它看起来更好。
ToSInt(x: Int): Int = scala.math.pow(2, bitwidth).toInt - xhttps://stackoverflow.com/questions/66294784
复制相似问题