[英]ChiselTest: Cast a signed int to unsigned int for an expected value
I'm having trouble identifying the correct method for converting a signed int to unsigned int for unit testing using the new ChiselTest framework.我无法确定使用新的 ChiselTest 框架将有符号整数转换为无符号整数以进行单元测试的正确方法。
Here is the method I have been using to unit test an ALU (example is 16-bit), the problem is that it is not scalable:这是我一直用来对 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 */
}
The problem with this method is that when I generate a 32-bit ALU (which outputs an unsigned int), the unit test will fail because the string will zero extend to 32-bits using the hacky string method above...这种方法的问题是,当我生成一个 32 位 ALU(它输出一个无符号整数)时,单元测试将失败,因为使用上面的 hacky 字符串方法,字符串将零扩展到 32 位......
I'd like to rewrite the test like this:我想像这样重写测试:
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)
}
But, although the design does elaborate, I get the following error:但是,尽管设计确实很详细,但我收到以下错误:
[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
I can't figure out the right way of addressing this.我想不出解决这个问题的正确方法。 Any help would be much appreciated.
任何帮助将非常感激。
For reference, the ALU is pretty simple, here is the ALU class:作为参考,ALU 非常简单,这里是 ALU class:
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),
))
}
This feels a little gross, but at least it's scalable (maybe a possible solution?)... I'm still interested in the best way of casting a SInt to UInt in the test framework.这感觉有点恶心,但至少它是可扩展的(也许是一个可能的解决方案?)......我仍然对在测试框架中将 SInt 转换为 UInt 的最佳方法感兴趣。
/* 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)
def bitwidth = 16 // for example
def max = scala.math.pow(2,bitwidth).toInt
c.io.out.expect((max-3087).U)
Made some improvements based on @tonysamaritano's solution.基于@tonysamaritano 的解决方案进行了一些改进。 Write an independent Scala function to make it look better.
写一个独立的 Scala function 让它更好看。
ToSInt(x: Int): Int = scala.math.pow(2, bitwidth).toInt - x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.