Skip to content

Commit

Permalink
Add toString method to BitPat (#1819)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
yqszxx and mergify[bot] authored Mar 18, 2021
1 parent ac094a5 commit 2a56c65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/scala/chisel3/util/BitPat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,11 @@ sealed class BitPat(val value: BigInt, val mask: BigInt, width: Int) extends Sou
!(this === that)
}

override def toString = {
"BitPat(" +
(0 until width).map(i =>
if (((mask >> i) & 1) == 1) if (((value >> i) & 1) == 1) "1" else "0" else "?"
).reverse.reduce(_ + _) +
")"
}
}
17 changes: 17 additions & 0 deletions src/test/scala/chiselTests/util/BitPatSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0

package chiselTests.util

import chisel3.util.BitPat
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers


class BitPatSpec extends AnyFlatSpec with Matchers {
behavior of classOf[BitPat].toString

it should "convert a BitPat to readable form" in {
val testPattern = "0" * 32 + "1" * 32 + "?" * 32 + "?01" * 32
BitPat("b" + testPattern).toString should be (s"BitPat($testPattern)")
}
}

0 comments on commit 2a56c65

Please sign in to comment.