Search code examples
chisel

How to call the variable defined inside withClockAndReset


I am using multiple clock in chisel.

In chisel3, I write following codes.

withClockAndReset(A_clk, A_rst) {
    val data_a = RegInit(0.U(4.W))
}
withClockAndReset(B_clk, B_rst) {
    val data_b = RegInit(0.U(4.W))
}
data_b := data_a
data_a := io.data

The compiler reported that the variable data_a could not be found. The variable data_a is defined inside withClockAndReset, and I can't use this variable outside.

What can I do?


Solution

  • Try:

    val data_a = withClockAndReset(A_clk, A_rst) {
        RegInit(0.U(4.W))
    }
    

    The issue is that you're defining a val in a separate scope.

    Whatever the second argument list of withClockAndReset (what is put in the { \* ... *\ }) returns. So, you can use this to return the register, module, etc. that you construct in the other clock/reset scope.