Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

modulo on parameters

Hi all!

I need to do operations on multiple elements of an input vector. The elements should be handled cyclic, so that the modulo operator % would come in handy. But it doesn't work (see below).

What can I do instead? Can I use << operators? Thanks!

module ring_operation(in,out);

parameter index;//can be called as any value in 0...range-1

parameter range;

input [range-1:0] in;

output out;

//this doesn't work because index-1 or index+1 can be out of range

assign out = in[index-1] | in[index+1];

//this doesn't work because % is not recognized

assign out = in[(index-1)%range] | in[(index+1)%range];

endmodule

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Afaik, verilog's modulo works on powers of two. This limitation makes sense because it can merely select the bit indexes of the modulo argument.

    I'd imagine that with that limitation, the synthesis algorithms probably just don't bother handling anything other than a power of two. You should write an iterative function which performs the modulo.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you!

    My fix is:

    parameter index_m1 = ((index-1)<0 ? range+(index-1) : (index-1));

    parameter index_p1 = ((index+1)>range-1 ? (index+1)-range: (index+1));

    assign out = in[parameter index_m1] | in[parameter index_p1];

    This is not really flexible. Do you know a better way?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Probably the function definition for modulo would work. Thanks for that advice! How can I have the neccessary If-statement in that function?