|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package chisel3.util |
| 4 | + |
| 5 | +import chisel3._ |
| 6 | +import chisel3.util.BitPat |
| 7 | +import _root_.circt.stage.ChiselStage |
| 8 | +import org.scalatest.flatspec.AnyFlatSpec |
| 9 | +import org.scalatest.matchers.should.Matchers |
| 10 | + |
| 11 | +object EnumExample extends ChiselEnum { |
| 12 | + val VAL1, VAL2, VAL3 = Value |
| 13 | +} |
| 14 | + |
| 15 | +class BitPatSpec extends AnyFlatSpec with Matchers { |
| 16 | + behavior.of(classOf[BitPat].toString) |
| 17 | + |
| 18 | + it should "convert a BitPat to readable form" in { |
| 19 | + val testPattern = "0" * 32 + "1" * 32 + "?" * 32 + "?01" * 32 |
| 20 | + BitPat("b" + testPattern).toString should be(s"BitPat($testPattern)") |
| 21 | + } |
| 22 | + |
| 23 | + it should "convert a BitPat to raw form" in { |
| 24 | + val testPattern = "0" * 32 + "1" * 32 + "?" * 32 + "?01" * 32 |
| 25 | + BitPat("b" + testPattern).rawString should be(testPattern) |
| 26 | + } |
| 27 | + |
| 28 | + it should "not fail if BitPat width is 0" in { |
| 29 | + intercept[IllegalArgumentException] { BitPat("b") } |
| 30 | + } |
| 31 | + |
| 32 | + it should "concat BitPat via ##" in { |
| 33 | + (BitPat.Y(4) ## BitPat.dontCare(3) ## BitPat.N(2)).toString should be(s"BitPat(1111???00)") |
| 34 | + } |
| 35 | + |
| 36 | + it should "throw when BitPat apply to a Hardware" in { |
| 37 | + intercept[java.lang.IllegalArgumentException] { |
| 38 | + ChiselStage.emitCHIRRTL(new chisel3.Module { |
| 39 | + BitPat(chisel3.Reg(chisel3.Bool())) |
| 40 | + }) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + it should "index and return new BitPat" in { |
| 45 | + val b = BitPat("b1001???") |
| 46 | + b(0) should be(BitPat.dontCare(1)) |
| 47 | + b(6) should be(BitPat.Y()) |
| 48 | + b(5) should be(BitPat.N()) |
| 49 | + } |
| 50 | + |
| 51 | + it should "slice and return new BitPat" in { |
| 52 | + val b = BitPat("b1001???") |
| 53 | + b(2, 0) should be(BitPat("b???")) |
| 54 | + b(4, 3) should be(BitPat("b01")) |
| 55 | + b(6, 6) should be(BitPat("b1")) |
| 56 | + } |
| 57 | + |
| 58 | + it should "parse UInt literals correctly" in { |
| 59 | + BitPat(0.U) should be(new BitPat(0, 1, 1)) |
| 60 | + // Note that this parses as 1-bit width, there are other APIs that don't support zero-width UInts correctly |
| 61 | + BitPat(0.U(0.W)) should be(new BitPat(0, 1, 1)) |
| 62 | + BitPat(1.U) should be(new BitPat(1, 1, 1)) |
| 63 | + BitPat(2.U) should be(new BitPat(2, 3, 2)) |
| 64 | + BitPat(0xdeadbeefL.U) should be(new BitPat(BigInt("deadbeef", 16), BigInt("ffffffff", 16), 32)) |
| 65 | + } |
| 66 | + |
| 67 | + it should "support .hasDontCares" in { |
| 68 | + BitPat("b?").hasDontCares should be(true) |
| 69 | + BitPat("b??").hasDontCares should be(true) |
| 70 | + BitPat("b0?").hasDontCares should be(true) |
| 71 | + BitPat("b?1").hasDontCares should be(true) |
| 72 | + BitPat("b0").hasDontCares should be(false) |
| 73 | + BitPat("b10").hasDontCares should be(false) |
| 74 | + BitPat("b01").hasDontCares should be(false) |
| 75 | + BitPat(0xdeadbeefL.U).hasDontCares should be(false) |
| 76 | + // Zero-width not supported yet |
| 77 | + intercept[IllegalArgumentException] { BitPat("b").hasDontCares should be(false) } |
| 78 | + } |
| 79 | + |
| 80 | + it should "support .allZeros" in { |
| 81 | + BitPat("b?").allZeros should be(false) |
| 82 | + BitPat("b??").allZeros should be(false) |
| 83 | + BitPat("b0?").allZeros should be(false) |
| 84 | + BitPat("b?1").allZeros should be(false) |
| 85 | + BitPat("b0").allZeros should be(true) |
| 86 | + BitPat("b10").allZeros should be(false) |
| 87 | + BitPat("b01").allZeros should be(false) |
| 88 | + BitPat(0.U(128.W)).allZeros should be(true) |
| 89 | + BitPat.N(23).allZeros should be(true) |
| 90 | + BitPat(0xdeadbeefL.U).allZeros should be(false) |
| 91 | + // Zero-width not supported yet |
| 92 | + intercept[IllegalArgumentException] { BitPat("b").allZeros should be(true) } |
| 93 | + } |
| 94 | + |
| 95 | + it should "support .allOnes" in { |
| 96 | + BitPat("b?").allOnes should be(false) |
| 97 | + BitPat("b??").allOnes should be(false) |
| 98 | + BitPat("b0?").allOnes should be(false) |
| 99 | + BitPat("b?1").allOnes should be(false) |
| 100 | + BitPat("b0").allOnes should be(false) |
| 101 | + BitPat("b10").allOnes should be(false) |
| 102 | + BitPat("b01").allOnes should be(false) |
| 103 | + BitPat("b1").allOnes should be(true) |
| 104 | + BitPat("b" + ("1" * 128)).allOnes should be(true) |
| 105 | + BitPat.Y(23).allOnes should be(true) |
| 106 | + BitPat(0xdeadbeefL.U).allOnes should be(false) |
| 107 | + // Zero-width not supported yet |
| 108 | + intercept[IllegalArgumentException] { BitPat("b").allOnes should be(true) } |
| 109 | + } |
| 110 | + |
| 111 | + it should "support .allDontCares" in { |
| 112 | + BitPat("b?").allDontCares should be(true) |
| 113 | + BitPat("b??").allDontCares should be(true) |
| 114 | + BitPat("b0?").allDontCares should be(false) |
| 115 | + BitPat("b?1").allDontCares should be(false) |
| 116 | + BitPat("b0").allDontCares should be(false) |
| 117 | + BitPat("b10").allDontCares should be(false) |
| 118 | + BitPat("b1").allDontCares should be(false) |
| 119 | + BitPat("b" + ("1" * 128)).allDontCares should be(false) |
| 120 | + BitPat.dontCare(23).allDontCares should be(true) |
| 121 | + BitPat(0xdeadbeefL.U).allDontCares should be(false) |
| 122 | + // Zero-width not supported yet |
| 123 | + intercept[IllegalArgumentException] { BitPat("b").allDontCares should be(true) } |
| 124 | + } |
| 125 | + |
| 126 | + it should "convert to BitPat from ChiselEnum" in { |
| 127 | + val b = BitPat(EnumExample.VAL1) |
| 128 | + val c = BitPat(EnumExample.VAL3) |
| 129 | + b should be(BitPat("b00")) |
| 130 | + c should be(BitPat("b10")) |
| 131 | + } |
| 132 | +} |
0 commit comments