Overview
Branch scenario generation is used in constrained-random processor verification to avoid the low usefulness of pure random instruction streams around branch instructions. Random register contents make branch conditions such as equality very unlikely, so forward branches often fall through and backward branches can remain in loops for a very long time. The solution described in the evidence is to generate branch-related instruction sequences with explicit constraints on surrounding operations and register relationships.
Forward branch generation
For forward branches, the evidence recommends initializing the compared register operands immediately before the branch. A specific pattern is to place an ADDI instruction before the branch, using the same operands and a small immediate value. In the cited example, setting R1 = R2 + {-2:2} raises the probability that R1 == R2 to 20%, making the branch condition much more likely to exercise both taken and not-taken behavior.
In constraint terms, the operation preceding a forward branch should therefore be an ADDI with the same operands as the branch comparison and a small immediate value.
Backward branch generation
Backward branches are treated as loop scenarios. The goal is to avoid absurdly long or endless loops while still exercising branch logic over multiple iterations.
The described constraint pattern is:
- the operation preceding a backward branch is an
ADDIwith the same operands and a small negative immediate value; - the operand acting as a loop index is incremented by 1 inside the loop, just before the branch;
- the two registers used by the branch comparison must not be modified elsewhere inside the loop.
This construction causes the compared values to converge and eventually terminate the loop, covering behavior ranging from immediate fall-through to several loop iterations.
Place in the scenario framework
The evidence places branch scenario generation within a broader scenario-based stimulus framework. User-defined scenarios can share reusable methods through a common instruction scenario base class, and constraints between instruction objects can be implemented there. More broadly, a scenario generator can select and randomize scenario objects across constrained-random, directed-random, and directed scenarios until a user-specified stopping condition is reached.
A directed scenario can also be loaded from a pre-assembled program trace file through a class method in the common scenario infrastructure, which complements constrained branch generation when exact behavior must be targeted.