Source – SV Verification Directory
Problem Statement
Create a constraint to generate an N-bit
random number that is divisible by a given natural number X
. The value of X
is parameterized, allowing it to be easily changed for different test scenarios.
Problem Breakdown
The key idea is to ensure that the random number generated satisfies the condition below:
\begin{equation} \label{eq.1} NUM \quad \% \quad X == 0 \end{equation}
The modulus operator (%) plays a key role in checking divisibility. To ensure a number is divisible by X
, the remainder when dividing the number (NUM)
by X
must be zero.
Implementation
class cstrs_challenge #(int X = 5, N = 4);
// class properties
rand bit [N:0] y;
constraint c {
y % X == 0;
}
// display function
function void disp();
$display("Rand Num = %d", y);
endfunction // disp
endclass // cstrs_challenge