Two-bit saturating counter

36%cpu-archcounters

Branch predictors commonly track each branch with a two-bit saturating counter, so a single anomalous outcome doesn't immediately flip the prediction. The counter has four states:

statemeaning
2'b00strongly not-taken
2'b01weakly not-taken
2'b10weakly taken
2'b11strongly taken

Build one such counter. On each rising clock edge, when train_valid = 1 the counter is trained with one branch outcome:

  • train_taken = 1: increment the counter, saturating at 3 (incrementing 2'b11 leaves it at 2'b11).
  • train_taken = 0: decrement the counter, saturating at 0 (decrementing 2'b00 leaves it at 2'b00).

When train_valid = 0, the counter holds its value.

areset is an asynchronous active-high reset that sets the counter to 2'b01 (weakly not-taken). The output state is the current counter value.