From bffc8b9e851af88128f1c683e67634ebde25c14b Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 24 May 2018 11:46:32 -0700 Subject: Fix UIntToOH for output widths larger than 2^(input width) (#823) * Add test for UIntToOH * Pad UIntToOH inputs to support oversized output widthds * Optimize Bits.pad in case of known widths * Add missing import and fix test in OneHotMuxSpec --- src/main/scala/chisel3/util/OneHot.scala | 3 ++- src/test/scala/chiselTests/OneHotMuxSpec.scala | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/scala/chisel3/util/OneHot.scala b/src/main/scala/chisel3/util/OneHot.scala index 9a911c41..8a0bb9fc 100644 --- a/src/main/scala/chisel3/util/OneHot.scala +++ b/src/main/scala/chisel3/util/OneHot.scala @@ -46,7 +46,8 @@ object UIntToOH { def apply(in: UInt, width: Int): UInt = width match { case 0 => 0.U(0.W) case _ => - val shiftAmount = in((log2Ceil(width) - 1) max 0, 0) + val shiftAmountWidth = log2Ceil(width) + val shiftAmount = in.pad(shiftAmountWidth)((shiftAmountWidth - 1) max 0, 0) (1.U << shiftAmount)(width - 1, 0) } } diff --git a/src/test/scala/chiselTests/OneHotMuxSpec.scala b/src/test/scala/chiselTests/OneHotMuxSpec.scala index 9495703d..e12d7913 100644 --- a/src/test/scala/chiselTests/OneHotMuxSpec.scala +++ b/src/test/scala/chiselTests/OneHotMuxSpec.scala @@ -5,7 +5,7 @@ package chiselTests import Chisel.testers.BasicTester import chisel3._ import chisel3.experimental.FixedPoint -import chisel3.util.Mux1H +import chisel3.util.{Mux1H, UIntToOH} import org.scalatest._ //scalastyle:off magic.number @@ -40,6 +40,9 @@ class OneHotMuxSpec extends FreeSpec with Matchers with ChiselRunners { assertTesterPasses(new DifferentBundleOneHotTester) } } + "UIntToOH with output width greater than 2^(input width)" in { + assertTesterPasses(new UIntToOHTester) + } } class SimpleOneHotTester extends BasicTester { @@ -283,4 +286,11 @@ class DifferentBundleOneHotTester extends BasicTester { stop() } +class UIntToOHTester extends BasicTester { + val out = UIntToOH(1.U, 3) + + require(out.getWidth == 3) + assert(out === 2.U) + stop() +} -- cgit v1.2.3