Вложенный IF в For Loop Verilog - PullRequest
       7

Вложенный IF в For Loop Verilog

0 голосов
/ 23 октября 2018

Я хотел бы знать, могу ли я поместить приведенный ниже код в цикл for, чтобы я мог параметризировать свой код.Спасибо.

always@(*) begin
if (exist_reg[0] == 'd0) begin
    nth_empty_location_descending = 'd1; // specify
end
else if (exist_reg[1] =='d0) begin
    nth_empty_location_descending = 'd2;
end
else if (exist_reg[2] =='d0) begin
    nth_empty_location_descending = 'd4;
end
else if (exist_reg[3] =='d0) begin
    nth_empty_location_descending = 'd8;
end
else if (exist_reg[4] =='d0) begin
    nth_empty_location_descending = 'd16;
end
else if (exist_reg[5] =='d0) begin
    nth_empty_location_descending = 'd32;
end
else if (exist_reg[6] =='d0) begin
    nth_empty_location_descending = 'd64;
end
else if (exist_reg[7] =='d0) begin
    nth_empty_location_descending = 'd128;
end
else if (exist_reg[8] =='d0) begin
    nth_empty_location_descending = 'd256;
end
else if (exist_reg[9] =='d0) begin
    nth_empty_location_descending = 'd512;
end
else begin
    nth_empty_location_descending = 'd0;
end
end

Это в основном проверка битов «exist_reg», если он встречает любой бит слева направо, равен нулю, то он возрастает в этом бите в регистре «nth_empty_location_descending» (какой-нибудь лучший подход?),Теперь я хочу создать параметризованный код для ширины регистра.В настоящее время это 10-битный код.Спасибо экспертам.

Ответы [ 2 ]

0 голосов
/ 23 октября 2018
parameter WIDTH = 10;
reg [WIDTH-1:0] exist_reg, nth_empty_location_descending2;
integer ii;
always @* begin
   nth_empty_location_descending2 = 0;
   for(ii=0;ii<WIDTH;ii=ii+1)
      if (exist_reg[j] == 1'b0 && nth_empty_location_descending2 == 0)
         nth_empty_location_descending2[ii] = 1'b1;
  end

В SystemVerilog

  parameter WIDTH = 10;
  logic [WIDTH-1:0] exist_reg, nth_empty_location_descending2;
  always_comb begin
     nth_empty_location_descending2 = 0;
     for(int ii=0;ii<WIDTH;ii++)
        if (exist_reg[j] == 1'b0) begin
           nth_empty_location_descending2[ii] = 1'b1;
           break;
        end
    end
0 голосов
/ 23 октября 2018

Прежде всего, лучше всего использовать оператор casez вместо цепочки if / else:

always@(*) begin
   casez (exist_reg)
     10'b?????????0:  nth_empty_location_descending1 = 'd1;
     10'b????????01:  nth_empty_location_descending1 = 'd2;
     10'b???????011:  nth_empty_location_descending1 = 'd4;
     10'b??????0111:  nth_empty_location_descending1 = 'd8;
     10'b?????01111:  nth_empty_location_descending1 = 'd16;
     10'b????011111:  nth_empty_location_descending1 = 'd32;
     10'b???0111111:  nth_empty_location_descending1 = 'd64;
     10'b??01111111:  nth_empty_location_descending1 = 'd128;
     10'b?011111111:  nth_empty_location_descending1 = 'd256;
     10'b0111111111:  nth_empty_location_descending1 = 'd512;
     default       :  nth_empty_location_descending1 = 'd0;
   endcase // casez (exist_reg)
end // always@ (*)

Однако, если вы настаиваете, есть решение на основе цикла:

always @* begin
   nth_empty_location_descending2 = 'd0;

   for (j = 0; j < 10; j = j + 1)  begin
      if (exist_reg[j] == 1'b0) begin
         if (nth_empty_location_descending2 == 0)
           nth_empty_location_descending2 = (10'b1 << j);
      end
   end
end // always @ *
...