Verilog Tasks in RTL - 4:1 Mux Example

Verilog tasks can be used in synthesizable RTL code to encapsulate reusable logic. This example demonstrates a 4:1 multiplexer implemented using a task.

About Verilog Tasks

Tasks in Verilog are similar to functions but can:

  • Have multiple outputs
  • Contain timing controls (delays, waits)
  • Call other tasks and functions

Note: For synthesizable RTL, tasks must be combinational (no timing controls) and called from within an always block.

4:1 Mux Using Task

module mux_t(
  input  [3:0] in,
  input  [1:0] sel,
  output reg   y
);

  // Task definition for 4:1 mux
  task t_mux;
    input [3:0] i;
    input [1:0] s;
    output reg  y;
  begin
    case(s)
      2'b00: y = i[0];
      2'b01: y = i[1];
      2'b10: y = i[2];
      2'b11: y = i[3];
    endcase
  end
  endtask

  // Call task in combinational always block
  always @(in, sel) begin
    t_mux(in, sel, y);
  end

endmodule

Port Description

PortDirectionWidthDescription
ininput4Data inputs [3:0]
selinput2Select lines [1:0]
youtput1Mux output

Selection Logic

selOutput
00in[0]
01in[1]
10in[2]
11in[3]
Author
Mayur Kubavat
VLSI Design and Verification Engineer sharing knowledge about SystemVerilog, UVM, and hardware verification methodologies.

Comments (0)

Leave a Comment