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 assign: Testbench

Verilog Examples

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

Open in IDE
design.v
module dec_4x16 ( 	input 					en,
									input 	[3:0] 	in,
									output [15:0] 	out);

	// When enabled, shift 1 left by 'in' positions to activate one output bit
	// Example: in=3 produces out=16'b0000_0000_0000_1000 (bit 3 set)
	// When disabled (en=0), all outputs are 0
	assign out = en ? 1 << in : 0;
endmodule
testbench.v
module tb;
	reg en; // Enable signal
	reg [3:0] in; // 4-bit input (16 possible values)
	wire [15:0] out; // 16-bit output (one-hot encoded)
	integer i;

	dec_4x16 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);

		// Test all 32 combinations (enable + 16 input values)
		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.