Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Interview Questions Set 9: Write randomization constraints for the following req

SystemVerilog Interview Questions

Runnable example from the SystemVerilog Interview Questions Set 9 tutorial (Write randomization constraints for the following requirements on an array.)

Open in IDE
testbench.sv
module tb;
	bit [3:0] 	data_q [9];
	
	initial begin
		bit [2:0] idx_q [2];

		randomize(data_q, idx_q) with {
											// Let idx_q represent two random indices
											// which should contain same value
											unique { idx_q };
											solve idx_q before data_q;
	
											// When indices match, assign same value to data_q[i]
											// else follow general rule to keep between 1:9
											foreach (data_q[i]) {
												if (i == idx_q[1]) {
													data_q[i] == data_q[idx_q[0]];
												} else {
													data_q[i] inside {[1:9]};
												}
											}
										};

		$display ("idx_q  = %p", idx_q);
		$display ("data_q = %p", data_q);
	end
	
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.