Sugihara
Sugihara

Reputation: 1101

Verilog, generate/loop with parameterized array declaration

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?

Upvotes: 0

Views: 1474

Answers (1)

dave_59
dave_59

Reputation: 42788

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

Upvotes: 1

Related Questions