0
votes

I am a newbie to verilog coding. In my problem statement, I will get number of entries in a sorted table from another module and based on number of entries I need to decide where should I start my binary search (e.g. Let num_entries be 15, then start index should be 8). Code snippet is given below:

srch_addr <= {{(TBL_AW-msb_loc(num_entries)-1){1'b0}},2'b10, {(msb_loc(num_entries)-1){1'b0}}};


 //function to find out MSB 1
       integer LOC;
       function [3:0] msb_loc;
input [TBL_AW:0]  num_entries;
reg          found;
//input             start;
begin
  //if(start = 1) 
  //begin
  found   = 1'b0;
  msb_loc = 3'b000;
  for (LOC=TBL_AW; LOC> 0; LOC=LOC-1)
  begin
      if((num_entries[LOC] == 1) && !found) 
      begin
         msb_loc = LOC;
         found   = 1'b1;   //TO exit the loop
      end
  end
  //end
end
endfunction

Compiler gives me this error "Illegal operand for constant expression". What can be done to resolve this error?

1
I wasn't clear enough in my question. Other vectors may vary based on num_entries. e.g.mask_clr <= {{(TBL_AW-msb_loc(num_entries)-1){1'b1}},2'b10, {(msb_loc(num_entries)-1){1'b1}}}; - Shantanu

1 Answers

0
votes

The replicator 'count' value must be a non-zero, non-X and non-Z constant expression.

 {(TBL_AW-msb_loc(num_entries)-1){1'b0}}
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This is not a constant.

To get the 'halfway' address you can just the standard mathematical way: divided and round upwards. In this case add one and divide by two:

srch_addr <= (msb_loc(num_entries)+1)>>1;

Coming back to your formula. This part just makes zeros: {(TBL_AW-msb_loc(num_entries)-1){1'b0}} You don't need that.

This part 2'b10, {(msb_loc(num_entries)-1){1'b0}} actually shifts 2'b10 left by "(msb_loc(num_entries)-1)" positions.

This does the same but then without syntax errors:

srch_addr <= 1'b1 << msb_loc(num_entries);

What I can see it does NOT give you the half-way address.

Further:
Your integer LOC; should be inside your function as a local variable.