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

$clog2 Math Function

Verilog Math Functions

Example of $clog2

Open in IDE
Related tutorial:
design.v
module des
  #(parameter NUM_UNITS = 7)

  // Use of this system function helps to reduce the
  // number of input wires to this module
  (input [$clog2(NUM_UNITS)-1:0] active_unit);

  initial
    $monitor("active_unit = %d", active_unit);
endmodule
testbench.v
`define NUM_UNITS 5

module tb;
  integer i;
  reg [`NUM_UNITS-1:0] 	active_unit;

  des #(.NUM_UNITS(`NUM_UNITS)) u0(active_unit);

  initial begin
    active_unit     = 1;
	#10 active_unit = 7;
    #10 active_unit = 8;
  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.