0

I am trying to find the max value inside a parameterized array, I was looking at this post and came across forloop-generate. The only change I want to make is change that approach of a explicit array to parameterized array, but I am running into genvar assignment problem.

   reg [$clog2(LENGTH)-1:0] arr [0:LENGTH-1];

   wire [$clog2(LENGTH)-1:0] value_l1[0:LENGTH-2];
   wire [$clog2(LENGTH)-1:0] index_l1[0:LENGTH-2];

   genvar gen_i, gen_j, gen_k;
   generate
      for(gen_i = 0; gen_i < LENGTH; gen_i = gen_i + 2) begin : loop1
         Compare cmp1(.A(arr[i])
                     ,.B(arr[i+1])
                     ,.indexA(i)
                     ,.indexB(i+1)
                     ,.value(value_l1[i/2])
                     ,.index(index_l1[i/2])
                     );
      end

      gen_k = 0;
      for(gen_i = 1; gen_i < $clog2(LENGTH); gen_i = gen_i + 1) begin : loop2_1
         for(gen_j = 0; gen_j < LENGTH / (2*gen_i); gen_j = gen_j + 2) begin : loop2_2
            Compare cmp2(.A(value_l1[gen_k + gen_j])
                        ,.B(value_l1[gen_k + gen_j+1])
                        ,.indexA(index_l1[gen_k + gen_j])
                        ,.indexB(index_l1[gen_k + gen_j+1])
                        ,.value(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
                        ,.index(index_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
                        );
         end
         //gen_k = gen_j; // <--------- problem here
      end
   endgenerate

My approach is to have a list of the compared elements, and then compare that list and write back the result to the same list. For example:

original array : 0 1 2 3 4 5 6 7

would results into

compare array : 1 3 5 7 | 3 7 | 7
                          ^     ^
                          |     |
                          |     largest element
                          |
                          gen_k + gen_j 

then output 7 as the largest element, but I cannot do gen_k = gen_j to save the index (offset to the second part of the compare array). Is it even possible to use generate with parameterized array? If so, how can I fix this problem?

1 Answer 1

1

You can do this with an intermediate parameter.

  for(gen_i = 1; gen_i < $clog2(LENGTH); gen_i = gen_i + 1) begin : loop2_1
         parameter param_k = LENGTH / (2*gen_i) + 1; 
         for(gen_j = 0; gen_j < LENGTH / (2*gen_i); gen_j = gen_j + 2) begin : loop2_2
            Compare cmp2(.A(value_l1[param_k + gen_j])
                        ,.B(value_l1[param_k + gen_j+1])
                        ,.indexA(index_l1[param_k + gen_j])
                        ,.indexB(index_l1[param_k + gen_j+1])
                        ,.value(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
                        ,.index(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
                        );
         end
      end
Sign up to request clarification or add additional context in comments.

Comments

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.