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

Combinational Logic with always: Testbench

Verilog Examples

Runnable example from the Combinational Logic with always tutorial (Testbench)

Open in IDE
design.v
module mux_2x1 (input 	a, b, sel,
				output 	reg c);

 // All inputs in sensitivity list
 always @ ( a or b or sel) begin
 	// Ternary operator: condition ? true_value : false_value
 	// If sel=1, output c = a; if sel=0, output c = b
	c = sel ? a : b;
 end
endmodule
testbench.v
module tb;
	// Declare testbench variables
 reg a, b, sel;
 wire c;
 integer i;

 // Instantiate the design and connect design inputs/outputs with
 // testbench variables
 mux_2x1 u0 ( .a(a), .b(b), .sel(sel), .c(c));

 initial begin
 	// At the beginning of time, initialize all inputs of the design
 	// to a known value, in this case we have chosen it to be 0.
 a = 0;
 b = 0;
 sel = 0;

 $monitor("a=%0b b=%0b sel=%0b c=%0b", a, b, sel, c);

 for (i = 0; i < 3; i = i + 1) begin
 {a, b, sel} = i;
 #10;
 end
 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.