Error in generate loop

Hi,
Kindly help me to remove the small error in generate loop: below s the code :::

module barrel_shifter#(parameter DATA_WIDTH = 32)
  (  input [DATA_WIDTH-1:0]         data_in,
    input [$clog2(DATA_WIDTH)-1:0] shift_amount, // log2(DATA_WIDTH) bits for shift amount
    input                          shift_right,      // 0 for left shift, 1 for right shift
    output reg [DATA_WIDTH-1:0]    data_out
);

     
    // Helper function to calculate log base 2 ceiling (e.g., clog2(8) = 3)
    function integer clog2;
        input integer value;
        integer i;
        begin
            clog2 = 0;
            for (i = 1; i < value; i = i * 2) begin
                clog2 = clog2 + 1;
            end
        end
    endfunction

    // Intermediate wires for multi-stage shifting
    wire [DATA_WIDTH-1:0] stage_0_out;
    wire [DATA_WIDTH-1:0] stage_1_out;
    // ... up to stage_N_out where N = $clog2(DATA_WIDTH) - 1

   // genvar i; // Used for generate blocks

    // The core idea of a barrel shifter is a series of MUX stages.
    // Each stage shifts by a power of 2 (1, 2, 4, 8, ...).
    // The shift_amount bits select which stages are active.

    // A common implementation for synthesis uses nested conditional operators or a series of generate blocks.
    // Let's use a series of 'assign' statements with conditional operators for clarity,
    // which synthesize to multi-input multiplexers.

    // Using a multi-stage approach (this is how it's typically synthesized)
    // The `shifted_data` variable will hold the result after each stage.
    reg [DATA_WIDTH-1:0] current_data;

    always @(*) begin
        current_data = data_in; // Start with the input data

        // Iteratively apply shifts based on each bit of shift_amount
        // Stage 0: Shift by 1 if shift_amount[0] is set
        if (shift_amount[0] == 1'b1) begin
            if (shift_right)
                current_data = {1'b0, current_data[DATA_WIDTH-1:1]}; // Logical right shift
            else
                current_data = {current_data[DATA_WIDTH-2:0], 1'b0}; // Logical left shift
        end

        // Stage 1: Shift by 2 if shift_amount[1] is set
        if (clog2(DATA_WIDTH) > 1 && shift_amount[1] == 1'b1) begin
            if (shift_right)
                current_data = {2'b0, current_data[DATA_WIDTH-1:2]};
            else
                current_data = {current_data[DATA_WIDTH-3:0], 2'b0};
        end

        // ... and so on for all bits of shift_amount
        // This can be done with a loop within the always block or generate block
        // for full parameterization.

        // More robust parameterized loop
          genvar i;
        for (i = 0; i < clog2(DATA_WIDTH); i = i + 1) begin
            if (shift_amount[i] == 1'b1) begin
                if (shift_right) begin
                    // Right shift: Concatenate zeros at the MSB side
                    current_data = { { (1 << i) {1'b0} }, current_data[DATA_WIDTH-1 : (1 << i)] };
                end else begin
                    // Left shift: Concatenate zeros at the LSB side
                    current_data = { current_data[DATA_WIDTH-1 - (1 << i) : 0], { (1 << i) {1'b0} } };
                end
            end
        end
        data_out = current_data;
    end

endmodule

it is giving error :::::::::

** Error: (vlog-13069) C:\Users\sunil\Desktop\verilog\barrel_shifter\barrel_shifter.v(65): near "genvar": syntax error, unexpected genvar.
** Error: C:\Users\sunil\Desktop\verilog\barrel_shifter\barrel_shifter.v(66): (vlog-2730) Undefined variable: 'i'.

Please format your code making your code easier for others to read. I have done that for you here.

Generating blocks cannot be placed within procedural code. I attempted to modify your code by relocating the always block within the generate for loop. However, I did not test it since you did not provide a testbench.

module barrel_shifter#(parameter DATA_WIDTH = 32, localparam DW_clog2 = $clog2(DATA_WIDTH))
  ( input [DATA_WIDTH-1:0]  data_in,
    input [DW_clog2-1:0]    shift_amount, // log2(DATA_WIDTH) bits for shift amount
    input                   shift_right,      // 0 for left shift, 1 for right shift
    output [DATA_WIDTH-1:0] data_out
  );

  logic [DATA_WIDTH-1:0] stage_out[DW_clog2+1];

  assign stage_out[0] = data_in;
  for(genvar i=0;i<DW_clog2;i++) 
    always_comb
      if (shift_amount[i] == 1'b1) 
        if (shift_right) 
          stage_out[i+1] = stage_out[i][DATA_WIDTH-1 : (1 << i)];
        else 
          stage_out[i+1] = stage_out[i][DATA_WIDTH-1 - (1 << i) : 0] << ((1 << i) - 1);  
  assign data_out = stage_out[DW_clog2];
  
endmodule

Hi,
Kindly help me to remove the small error in generate loop: below s the code :::

module barrel_shifter#(parameter DATA_WIDTH = 32)
  (  input [DATA_WIDTH-1:0]         data_in,
    input [$clog2(DATA_WIDTH)-1:0] shift_amount, // log2(DATA_WIDTH) bits for shift amount
    input                          shift_right,      // 0 for left shift, 1 for right shift
    output reg [DATA_WIDTH-1:0]    data_out
);

     
    // Helper function to calculate log base 2 ceiling (e.g., clog2(8) = 3)
    function integer clog2;
        input integer value;
        integer i;
        begin
            clog2 = 0;
            for (i = 1; i < value; i = i * 2) begin
                clog2 = clog2 + 1;
            end
        end
    endfunction

    // Intermediate wires for multi-stage shifting
    wire [DATA_WIDTH-1:0] stage_0_out;
    wire [DATA_WIDTH-1:0] stage_1_out;
    // ... up to stage_N_out where N = $clog2(DATA_WIDTH) - 1

   // genvar i; // Used for generate blocks

    // The core idea of a barrel shifter is a series of MUX stages.
    // Each stage shifts by a power of 2 (1, 2, 4, 8, ...).
    // The shift_amount bits select which stages are active.

    // A common implementation for synthesis uses nested conditional operators or a series of generate blocks.
    // Let's use a series of 'assign' statements with conditional operators for clarity,
    // which synthesize to multi-input multiplexers.

    // Using a multi-stage approach (this is how it's typically synthesized)
    // The `shifted_data` variable will hold the result after each stage.
    reg [DATA_WIDTH-1:0] current_data;

    always @(*) begin
        current_data = data_in; // Start with the input data

        // Iteratively apply shifts based on each bit of shift_amount
        // Stage 0: Shift by 1 if shift_amount[0] is set
        if (shift_amount[0] == 1'b1) begin
            if (shift_right)
                current_data = {1'b0, current_data[DATA_WIDTH-1:1]}; // Logical right shift
            else
                current_data = {current_data[DATA_WIDTH-2:0], 1'b0}; // Logical left shift
        end

        // Stage 1: Shift by 2 if shift_amount[1] is set
        if (clog2(DATA_WIDTH) > 1 && shift_amount[1] == 1'b1) begin
            if (shift_right)
                current_data = {2'b0, current_data[DATA_WIDTH-1:2]};
            else
                current_data = {current_data[DATA_WIDTH-3:0], 2'b0};
        end

        // ... and so on for all bits of shift_amount
        // This can be done with a loop within the always block or generate block
        // for full parameterization.

        // More robust parameterized loop
          genvar i;
        for (i = 0; i < clog2(DATA_WIDTH); i = i + 1) begin
            if (shift_amount[i] == 1'b1) begin
                if (shift_right) begin
                    // Right shift: Concatenate zeros at the MSB side
                    current_data = { { (1 << i) {1'b0} }, current_data[DATA_WIDTH-1 : (1 << i)] };
                end else begin
                    // Left shift: Concatenate zeros at the LSB side
                    current_data = { current_data[DATA_WIDTH-1 - (1 << i) : 0], { (1 << i) {1'b0} } };
                end
            end
        end
        data_out = current_data;
    end

endmodule

it is giving error :::::::::

** Error: (vlog-13069) C:\Users\sunil\Desktop\verilog\barrel_shifter\barrel_shifter.v(65): near "genvar": syntax error, unexpected genvar.
** Error: C:\Users\sunil\Desktop\verilog\barrel_shifter\barrel_shifter.v(66): (vlog-2730) Undefined variable: 'i'.

[/quote]

Hi Dave,
It is working now.