This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convert DecodeTableAnnotation regression test (#698)
* Add convert decode regression test * add decode table annotation convert * Ignore DecodeTableAnnotation * Format test
- Loading branch information
1 parent
5b9c0b1
commit b2d8908
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/scala/chiseltest/regressions/ConvertDecodeTableTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chiseltest.regressions | ||
|
||
import chisel3._ | ||
import chisel3.util.experimental.decode.{decoder, TruthTable} | ||
import chisel3.util.{BitPat, Cat, Fill} | ||
import chiseltest._ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
// https://github.com/sequencer/arithmetic/blob/dd9bd585a8d444399eb5a31d088567e0ef56f43a/arithmetic/src/multiplier/Booth.scala | ||
class Booth(width: Int)(radixLog2: Int, signed: Boolean = true) extends Module { | ||
val input = IO(Input(UInt(width.W))) | ||
val output = IO( | ||
Output( | ||
Vec( | ||
width / radixLog2 + 1, // = ceil(width / radixLog2) | ||
SInt((radixLog2 + 1).W) | ||
) | ||
) | ||
) | ||
def extend(x: Bits, len: Int, signed: Boolean = true): Bits = { | ||
if (x.getWidth >= len) | ||
x | ||
else { | ||
val fillBit = if (signed) x.head(1) else 0.B | ||
Fill(len - x.getWidth, fillBit) ## x.asUInt | ||
} | ||
} | ||
|
||
/** Because .asUInt() do not set .litOption properly */ | ||
def sIntToBitPat(x: Int, w: Int): BitPat = { | ||
if (x >= 0) | ||
BitPat(x.U(w.W)) | ||
else | ||
BitPat((x + (1 << w)).U(w.W)) | ||
} | ||
|
||
val paddingLeftWidth = width + radixLog2 - width % radixLog2 | ||
val paddedInput = Cat(extend(input, paddingLeftWidth, signed), 0.U(1.W)) | ||
|
||
val boothEncodingCoeff = Seq.tabulate(radixLog2 + 1) { | ||
case i if i == radixLog2 => -(1 << (radixLog2 - 1)) | ||
case i if i == 0 => 1 | ||
case i => 1 << (i - 1) | ||
} | ||
|
||
val boothEncodingTable = TruthTable( | ||
Seq | ||
.tabulate(1 << (radixLog2 + 1)) { i => | ||
Seq | ||
.tabulate(radixLog2 + 1)((bit: Int) => if (BigInt(i).testBit(bit)) 1 else 0) | ||
.zip(boothEncodingCoeff) | ||
.map { case (a, b) => | ||
a * b | ||
} | ||
.sum | ||
} | ||
.zipWithIndex | ||
.map { case (o, i) => | ||
val w = radixLog2 + 1 | ||
(sIntToBitPat(i, w), sIntToBitPat(o, w)) | ||
}, | ||
BitPat.dontCare(radixLog2 + 1) | ||
) | ||
|
||
output := Seq | ||
.tabulate(output.size) { i => | ||
decoder(paddedInput(radixLog2 * (i + 1), radixLog2 * i), boothEncodingTable) | ||
} | ||
.map(_.asSInt) | ||
} | ||
|
||
class ConvertDecodeTableAnnotation extends AnyFlatSpec with ChiselScalatestTester with Matchers { | ||
behavior.of("Convert DecodeTableAnnotation Regression") | ||
|
||
it should "work" in { | ||
test(new Booth(16)(8)) { dut => | ||
dut.input.poke(7.U) | ||
dut.output(0).expect(7.S) | ||
dut.output(1).expect(0.S) | ||
dut.output(2).expect(0.S) | ||
} | ||
} | ||
} |