Overview
Branch Scenario Verification addresses a common weakness of automatic microprocessor instruction generation: unconstrained random register values rarely create useful branch behavior. For example, random 32-bit register contents make equality comparisons such as BEQ R1, R2 unlikely to be true, so forward branches tend to fall through and may miss branch-condition evaluation logic. Likewise, a backward branch such as BNE R3, R4 can keep execution in a loop for a very long time when the compared registers almost never become equal. [C1]
The technique uses constrained scenarios to restrict relationships among nearby instructions and operands. These constraints make branch outcomes more controllable while preserving randomness in the generated instruction stream. [C2]
Forward branches
For forward branches, the goal is to make both taken and not-taken outcomes reasonably likely. A cited approach is to initialize the compared operands immediately before the branch. For example, an ADDI can assign R1 = R2 + {-2:2} before a BEQ R1, R2, making equality occur when the immediate is zero; in the cited example, this raises the probability of equality to 20%. [C3]
In constraint terms, the instruction immediately preceding a forward branch is constrained to be an ADDI using the same operands as the branch and a small immediate value. This links the branch comparison to a controlled arithmetic setup while still allowing randomized immediate selection within a small range. [C4]
Backward branches
Backward branches are handled as loop scenarios. Rather than allowing arbitrary register values that may cause extremely long loops, constraints make the loop index converge toward the terminating condition. In the cited example, a negative immediate sets up the compared registers so that an increment inside the loop brings the values closer by one each iteration. When the values become equal, the backward BNE falls through and the loop exits. This creates cases ranging from immediate fall-through to loops with several iterations, while avoiding absurdly long loops. [C5]
A practical constraint pattern is:
- place an
ADDIimmediately before the backward branch, using the same operands and a small negative immediate value; - increment the loop-index operand by one inside the loop, just before the branch;
- prevent other instructions inside the loop from modifying the two branch-operand registers. [C6]
The testbench should also consider boundary conditions. For example, a backward branch such as BGT R1, R2, LABEL_X can become always taken if R2 has the smallest possible value. [C7]
Role in constrained-random stimulus generation
Branch Scenario Verification is part of a broader constrained-random verification strategy for microprocessors. Pure random instruction streams are often not useful because hardware-level rules, such as aligned load/store accesses unless an exception is intended, must be respected. A common instruction-scenario base class can encode relationships among instruction objects as constraints, and selected constraints can be disabled when exception-producing cases such as misaligned memory operations are desired. [C8]
Scenario generators can combine constrained-random, directed-random, and directed scenarios. Directed scenarios may load pre-assembled program traces, while directed-random scenarios can preload special data values before randomized arithmetic streams. This allows branch-focused constraints to coexist with broader processor stimulus strategies. [C9]