In Verilog concatenation allows you to combine multiple smaller signals into a larger signal. This is helpful for creating multi-bit signals from smaller bits or combining variables of different sizes. Concatenation is performed using the {} operator. You can concatenate wires, variables, constants, or even expressions.

What is Verilog Concatenation?

Verilog concatenation combines multiple smaller signals, like wires or variables, into one larger signal. It can include expressions or sized constants as well. When concatenating signals, you must know the size of each operand to determine the total size.

Verilog Concatenation Example

Here’s a simple example to demonstrate how concatenation works:

wire a, b;          // 1-bit wires
wire [1:0] res;     // 2-bit wire to store a and b

// Concatenate a and b into res
assign res = {a, b};

In the example above:

  • res[1] will store a.
  • res[0] will store b.

Now, let’s see a more complex example:

wire [2:0] c;
wire [7:0] res1;

// Concatenate values into res1
assign res1 = {b, a, c[1:0], 2'b00, c[2]};

In this example:

  • res[0] stores c[2].
  • res[4:3] stores c[1:0].
  • Constant 2'b00 is inserted between c[1:0] and c[2].

A Full Verilog Design Example of Verilog Concatenation

module des (
    input [1:0] a,
    input [2:0] b,
    output [4:0] out1,
    output [3:0] out2
);
  assign out1 = {a, b};      // Concatenate a and b into out1
  assign out2 = {a[1], 2'b01, b[2]};  // Concatenate a[1], constant 2'b01, and b[2]
endmodule

Testbench for the Design:

module tb;
  reg [1:0] a;
  reg [2:0] b;
  wire [4:0] out1;
  wire [3:0] out2;

  des u0 (a, b, out1, out2);

  initial begin
    a = 0;
    b = 0;
    #10 a = 3;
    #5  b = 5;
    #10 a = 2;
    #5  b = 1;
    #10 $finish;
  end
  
  initial begin 
    $monitor("[%0t] a=%b b=%b, out1=%b out2=%b", $time, a, b, out1, out2);
  end
endmodule

Verilog Replication Operator | Verilog Concatenation

What is the Replication Operator?

The replication operator {} is used when you want to repeat an expression multiple times. This operator requires a non-negative constant number, and it cannot be an undefined or variable value like X or Z.

Example of replication

wire logicmadness;
wire [6:0] res;

assign res = {5{logicmadness}};  // Repeat the value of 'a' 5 times

Illegal Usage of Replication:

Replication cannot use undefined values like X or Z. It’s also illegal to assign replication to the left-hand side of an expression.

{2'bz{2'b0}} // Illegal: Z cannot be used
{2'bx{2'b0}} // Illegal: X cannot be used

Example of Replication in Action:

module des;
  reg [1:0] a;
  reg [2:0] b;

  initial begin
    a = 2;
    b = 4;

    #10;
    $display("a=%b b=%b res=%b", a, b, {{2{a}}, {3{b}}});  // Replicate 'a' twice and 'b' three times
  end
endmodule

In the example, a is repeated twice and b is repeated three times.

Nested Replication | Verilog Concatenation

You can nest replication expressions inside concatenation. Here’s an example:

module des;
  reg [1:0] a;
  reg [2:0] b;

  initial begin
    a = 2;
    b = 4;

    #10;
    $display("a=%b b=%b res=%b", a, b, {a, b, 3'b000, {{2{a}}, {3{b}}}});  // Nested replication
  end
endmodule

Illegal Usage in Replication

Verilog does not allow variables as replication constants. For example, this will result in an error:

module des;
  reg [1:0] a;
  reg [2:0] b;
  reg [3:0] _var;

  initial begin
    a = 2;
    b = 4;
    _var = 3;

    // This will cause an error
    $display("a=%b b=%b res=%b", a, b, {_var{a}});
  end
endmodule

This generates the error:

Illegal operand for constant expression

Verilog Sign Extension | Verilog Concatenation

What is Sign Extension?

Sign extension is used to expand a signed number from a smaller bit width to a larger bit width, by replicating the sign bit (most significant bit).

Example:

  • A 4-bit signed number 1101 represents -3.
  • When adding it to an 8-bit number, you need to sign-extend it to make it compatible.

Here’s how to do it:

module sign_extension(input signed [3:0] input_num, output reg signed [7:0] output_num);

  always @(*) begin
    if (input_num[3] == 1'b1)  // If the sign bit is 1
      output_num = { {4{1'b1}}, input_num };  // Extend with ones
    else
      output_num = { {4{1'b0}}, input_num };  // Extend with zeros
  end

endmodule

In this example:

  • If the sign bit (input_num[3]) is 1, we extend with ones.
  • Otherwise, we extend with zeros.

Summary

  • Concatenation: Combines smaller signals into larger ones using {}.
  • Replication: Repeats a signal using {N{expression}} where N is a constant.
  • Sign Extension: Expands signed numbers to a larger width by replicating the sign bit.

These Verilog operators are essential for creating efficient designs in digital circuits.

Scroll to Top