Why is this local var not set correctly according to ChiselTest

66 views Asked by At

I'm coming from Bluespec and not understanding this Chisel behavior. I have a simple Module:

class WhyFails extends Module {
    val io = IO(new Bundle {
        val operation = Input(UInt(2.W))
        val result = Output(UInt(32.W))
        val invalidOperation = Output(Bool())
    })

    var invalidOperation = false.B
    when (io.operation === 0.U) {
        io.result := 99.U
        printf("WF: Valid operation\n")
    }
    .otherwise {
        io.result := 0.U
        invalidOperation = true.B
        printf("WF: Invalid operation\n")
    }

    io.invalidOperation := invalidOperation
}

I'm trying to decipher why my tester is indicating the io.invalidOperation is asserted in this test.

class WhyFailsTest extends AnyFlatSpec with ChiselScalatestTester {
    behavior of "WhyFails"

    it should "Not Fail?" in {
        test(new WhyFails) { wf =>
            wf.io.operation.poke(0.U)
            wf.clock.step() // Step so printf() produces output
            wf.io.invalidOperation.expect(false.B)
            wf.io.result.expect(99.U)
        }
    }
}

The output (notice the printf indicating the valid operation)

> test
WF: Valid operation
[info] WhyFailsTest:
[info] WhyFails
[info] - should Not Fail? *** FAILED ***
[info]   io_invalidOperation=true (1, 0x1) did not equal expected=false (0, 0x0) (lines in WhyFailsTest.scala: 11) (WhyFailsTest.scala:16)
...<snip>...
[info] *** 1 TEST FAILED ***
[error] Failed tests:
[error]         temp.WhyFailsTest
[error] (Test / test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed Jun 3, 2022, 8:59:04 AM

If I instead assign io.invalidOperation directly (instead of the local var), everything works. Why is the behavior with the local var different than assigning directly to io.invalidOperation?

Thanks.

1

There are 1 answers

4
Yude Wei On BEST ANSWER

If you want do declare a new wire, you may try this:

val invalidOperation = Wire(Bool())
io.invalidOperation := invalidOperation