Learning BSV‎ > ‎Advanced Bluespec‎ > ‎

Generating Random Test Patterns

This page describes a means of generating random patterns by the use of a linear feedback shift register LFSR. LFSR can generate pseudo-random bit patterns, and are often used in self-test circuits. In this example, we use an LFSR to trigger a rule condition in a probabilistic manner.

...
// a LFSR for random patterns
LFSR#( Bit#(8) ) lfsr <- mkLFSR_8 i_rand ;
...
// keep counting
rule counting ;
count <= count + 1;
lfsr.next ; // update the lfsr value
endrule

// action2 occurs at random time
// lfsr ranges from 1 to 256 so probability can be adjusted
rule action2 (lfsr.value() > 128 ) ;
mypush.go( count[3:0] );
$display( "%t -- action2 %h", $time, count[3:0] ) ;
endrule

Other LFSR library functions are described in the BSV Reference Manual
Comments