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 fa (	input 	a, b, cin,
			output reg	sum, cout);

 // All THREE inputs in sensitivity list
 always @ (a or b or cin) begin
 	// Three input addition: a + b + cin
 	// Result is 2 bits: cout (bit[1]) and sum (bit[0])
 {cout, sum} = a + b + cin;
 end

endmodule
testbench.v
module tb;
 reg a, b, cin;
 wire sum, cout;
 integer i;

 fa u0 ( .a(a), .b(b), .cin(cin), .sum(sum), .cout(cout));

 initial begin
 a = 0;
 b = 0;

 $monitor("a=%0b b=%0b cin=%0b cout=%0b sum=%0b", a, b, cin, cout, sum);

 for (i = 0; i < 8; i = i + 1) begin
 {a, b, cin} = 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.