How to soft reset Chisel Counter

Viewed 361

I'm using a Chisel Counter in my logic, and want to be able to reset it also on clear input signal. how can i do that ? I was thinking of something like that:

withReset(reset || io.clr) {val (count,wrap) = Counter(io.valid,512)}

My Issue with this (apart from being ugly) is that the val names are not available out of the scope of the withReset. Is there a better way to do so ? How about trying to assign 0.U the inner value of the Counter , how can i do that ?

1 Answers

withReset returns the last expression in the block, so you can just write:

val (count, wrap) =  withReset(reset.asBool || io.clr)(Counter(io.valid,512))

Note that I added .asBool to reset because in import chisel3._, val reset has the abstract type Reset. See the Chisel website docs on Reset for more information.

I think the above is the best way to do it, but you can also use the @chiselName macro to allow Chisel to name vals inside scopes:

import chisel3.experimental.chiselName

@chiselName
class MyModule extends Module {
  ...


  withReset(reset || io.clr) {val (count,wrap) = Counter(io.valid,512)}
  //                               ^ these ^ will now get named
}

Note that we're trying to get a better version of @chiselName into the 3.4.0 release in the coming weeks, but @chiselName works for the time being.

Related