ChiselTest - expect a bit value in a UInt

416 views Asked by At

Per this: https://github.com/chipsalliance/chisel3/issues/2439 I've decided to change all Vec[Bool] to UInt where possible. One feature of a Vec[Bool] is that testing the values of each bit is simple.

For example, let's say I have a module that gives back a UInt. I want to test a particular bit is set but I don't care what the other bits are. With a Vec[Bool] I could easily do this:

dut.io.v(bit).expect(true.B)

Is there an equivalent using UInt?

dut.io.u(bit).expect(true.B)  // this isn't allowed

The best I can come up with is this ugly mess:

assert(dut.io.u.peek().litValue & (1 << bit) != 0)

If this is the best that can be done in Chisel, should there exist a special expectBit() on UInt wherein the user can expect a value on a particular bit?

I suppose the same question could be asked of sub portions of the UInt (aka extracting a portion of the UInt for testing)

1

There are 1 answers

0
Kevin Laeufer On

The only way I could think of to improve your "ugly mess" suggestion is to use the new (since X.5.1) peekInt() method. So something like:

assert(dut.io.u.peekInt() & (1 << bit) != 0)

I would be happy to accept a PR that adds an expectBit() like method. There are multiple possibilities for how we could do this:

  1. expectTrue(bit: Int) and expectFalse(bit: Int)
  2. expectBit(bit: Int, value: BigInt)
  3. something else?

Personally I would prefer option 1, because for option 2, it is unclear which argument is the value and which one is the bit.

Let's discuss on a chiseltest issue or PR.