1

I have a problem that requires converging wires down in stages, where each stage has half as many inputs and outputs as the previous one (think similar to staged selectors). I don't want to have unconnected wires in my module.

Can I declare an array of size numStages of vectors, and then define the size of each vector in a generate loop? For example:

module Log2Alg(x, a, b);
    parameter N = 1;
    localparam numStages = $clog2(N);
    output x;
    input [N-1:0] a, b;
    wire [???] stageInputs [0:numStages-1];
    wire [???] stageOutputs [0:numStages-1];

    genvar i, j;
    generate
        for(i = 0; i < numStages; i = i + 1)
        begin: generateStage
            stageInputs[i] = [2**(N-i):0]; // ???
            stageOutputs[i+1] = [2**(N-i-1)-1:0] // ???

            // Do stuff. Loop over j = i**2-1:0
        end
    endgenerate

endmodule

Is there another approach I should be considering, or a way to accomplish what I am attempting?

2
  • Why not define the wires inside generate block? So that you can adjust the wire dimensions and you can access if as if it was an array. (E.g. generateStage[3].stageInputs) Commented Feb 9, 2017 at 7:15
  • Can you merge 2 output wires of a stage or all outputs are different? And how many stages do u have? Commented Feb 9, 2017 at 13:53

1 Answer 1

2

You can move the declarations of your wires into the generate loop instead of making them arrays.

module Log2Alg #(N=1) (
    output x, 
    input [N-1:0] a, b
    );
    localparam numStages = $clog2(N);


    genvar i, j;
    generate
        for(i = 0; i < numStages; i = i + 1)
        begin: generateStage
        wire stageInputs [2**(N-i):0];
        wire stageOutputs [2**(N-i-1)-1:0];
            // Do _stuff_. Loop over j = i**2-1:0
            //
        end
    endgenerate

endmodule

Then if you need to access wires in a different stage from the current stuff, you can use something like generateStage[i+1].stageInputs. The index you use access generateStage must be a constant because it's not really an array of identical elements.

Sign up to request clarification or add additional context in comments.

2 Comments

Follow up question: Is there any way I could create or access an implicit generateStage[-1]?
You can start the generate loop at -1. You can also put conditional generates around the stuff code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.