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 dec_3x8 ( 	input 			en,
					input 	[3:0] 	in,
					output reg [15:0] 	out);

 // Sensitivity list: enable and 4-bit input
 always @ (en or in) begin
 	// When en=1: shift 1 left by 'in' positions (one-hot encoding)
 	// When en=0: all outputs are 0
 	// Example: in=4'b0101 (5) -> out=16'b0000_0000_0010_0000 (bit 5 set)
 out = en ? 1 << in: 0;
 end

endmodule
testbench.v
module tb;
 reg en;
 reg [3:0] in;
 wire [15:0] out;
 integer i;

 dec_3x8 u0 ( .en(en), .in(in), .out(out));

 initial begin
 en = 0;
 in = 0;

 $monitor("en=%0b in=0x%0h out=0x%0h", en, in, out);

 for (i = 0; i < 32; i = i + 1) begin
 {en, in} = 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.