From c720c99a75f565c8b4fbc7147d7b9daee9123d10 Mon Sep 17 00:00:00 2001 From: chick Date: Wed, 18 Dec 2019 10:04:13 -0800 Subject: BitPat supports whitespace and underscores, presumably for human readability. The BitPat.parse factory though did not remove these from the returned count. This fixes that adds whitespace and underscores to the unit tests This is an updated vesion of Chisel PR #1069 --- src/main/scala/chisel3/util/BitPat.scala | 9 ++++++--- src/test/scala/chiselTests/Decoder.scala | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/scala/chisel3/util/BitPat.scala b/src/main/scala/chisel3/util/BitPat.scala index 83475d1b..7c0abdb2 100644 --- a/src/main/scala/chisel3/util/BitPat.scala +++ b/src/main/scala/chisel3/util/BitPat.scala @@ -14,7 +14,7 @@ object BitPat { * @return bits the literal value, with don't cares being 0 * @return mask the mask bits, with don't cares being 0 and cares being 1 * @return width the number of bits in the literal, including values and - * don't cares. + * don't cares, but not including the white space and underscores */ private def parse(x: String): (BigInt, BigInt, Int) = { // Notes: @@ -25,14 +25,17 @@ object BitPat { require(x.head == 'b', "BitPats must be in binary and be prefixed with 'b'") var bits = BigInt(0) var mask = BigInt(0) + var count = 0 for (d <- x.tail) { - if (d != '_') { + if (! (d == '_' || d.isWhitespace)) { require("01?".contains(d), "Literal: " + x + " contains illegal character: " + d) mask = (mask << 1) + (if (d == '?') 0 else 1) bits = (bits << 1) + (if (d == '1') 1 else 0) + count += 1 } } - (bits, mask, x.length - 1) + + (bits, mask, count) } /** Creates a [[BitPat]] literal from a string. diff --git a/src/test/scala/chiselTests/Decoder.scala b/src/test/scala/chiselTests/Decoder.scala index 59ad6324..44cacccc 100644 --- a/src/test/scala/chiselTests/Decoder.scala +++ b/src/test/scala/chiselTests/Decoder.scala @@ -36,8 +36,18 @@ class DecoderSpec extends ChiselPropSpec { val bitpatPair = for(seed <- Arbitrary.arbitrary[Int]) yield { val rnd = new scala.util.Random(seed) val bs = seed.toBinaryString - val bp = bs.map(if(rnd.nextBoolean) _ else "?").mkString - ("b" + bs, "b" + bp) + val bp = bs.map(if(rnd.nextBoolean) _ else "?") + + // The following randomly throws in white space and underscores which are legal and ignored. + val bpp = bp.map { a => + if (rnd.nextBoolean) { + a + } else { + a + (if (rnd.nextBoolean) "_" else " ") + } + }.mkString + + ("b" + bs, "b" + bpp) } private def nPairs(n: Int) = Gen.containerOfN[List, (String,String)](n,bitpatPair) -- cgit v1.2.3