Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

verilog: function with variable list of arguments?

Status
Not open for further replies.

mrflibble

Advanced Member level 5
Joined
Apr 19, 2010
Messages
2,720
Helped
679
Reputation
1,360
Reaction score
652
Trophy points
1,393
Activity points
19,551
Is there any way in verilog to specify a function such that it can take between 0 and lets say 8 arguments?

The arguments are all integers between 0 and 255.

The function will output a 256 bit wide constant.

I can make a function just fine that takes a FIXED number of 8 arguments, and then pad the unused arguments with "0" dummy values. But that's a bit ugly. Is there any better way?

So what I have now is a function where I can call like this:

Code:
localparam EXAMPLE_0 =  working_fun(0, 0, 0, 0, 0, 0, 0, 0); // works

localparam EXAMPLE_3 =  working_fun(3, 42, 99, 0, 0, 0, 0, 0); // works

localparam EXAMPLE_7 =  working_fun(3, 42, 99, 100, 101, 199, 240, 242, 0); // works

// with 8 arguments it's obviously the same
What I would like:
Code:
localparam EXAMPLE_0 =  wanted_fun(); // what I would like

localparam EXAMPLE_3 =  wanted_fun(3, 42, 99); // what I would like

localparam EXAMPLE_7 =  wanted_fun(3, 42, 99, 100, 101, 199, 240, 242); // what I would like

// with 8 arguments it's obviously the same

So this function should work for any combination of 0 to 8 arguments values. But I can only get things to work for a FIXED number of argument values. Hence the best I could do so far is just pass a fix number of 8 arguments, and any non-used argument gets a dummy value (in this case 0).

Any better options?

No system verilog please. XST 13.2 from xilinx takes verilog-2001 just fine, but that's about it.

Thanks for any solutions. :)
 

Anyone?

Maybe a clever macro to work around this problem? The only way so far I could find to sortof handle a variable argument list is with a parameterized module as wrapper, and then use static wire output as the result. But it's pretty ugly, so I was hoping someone might know anything better than my ugly wrapper module, or the slightly less ugly "fill in zeroes for non-used trailing arguments" method.
 

Hi,

interesting, but rather theoretical [or aesthetic...] problem :);

such idea, also 'ugly' but funny, I'm sure you don't need any explanations;
quartus compiled it, other tools may be more restrictive;
Code:
module mply
(  input  [3:0]  din,
   output [4:0]  dout_a, dout_b, dout_c);

parameter a = 4'h2,  b = 4'h3,  c = 4'h4;
             
localparam  sa = add(a),  sb = add({a,b}),  sc = add({a,b,c});
            
assign dout_a = din*sa,
       dout_b = din*sb,
       dout_c = din*sc; 
 
            
function integer add ( input [3*4-1:0] in );

   add =  in[11:8] + in[7:4] + in[3:0];
  
endfunction        

endmodule
---
 

Hi, thanks for taking a look at it. :)

interesting, but rather theoretical [or aesthetic...] problem :);

One man's theoretical aesthetics is another man's readability and code maintainability. ;)



such idea, also 'ugly' but funny, I'm sure you don't need any explanations;
quartus compiled it, other tools may be more restrictive;
Code:
module mply
(  input  [3:0]  din,
   output [4:0]  dout_a, dout_b, dout_c);

parameter a = 4'h2,  b = 4'h3,  c = 4'h4;
             
localparam  sa = add(a),  sb = add({a,b}),  sc = add({a,b,c});
            
assign dout_a = din*sa,
       dout_b = din*sb,
       dout_c = din*sc; 
 
            
function integer add ( input [3*4-1:0] in );

   add =  in[11:8] + in[7:4] + in[3:0];
  
endfunction        

endmodule

Yeah, I also looked at something like that.

The only way so far I could find to sortof handle a variable argument list is with a parameterized module as wrapper ...

Incidentally, both the function with mandatory filled in default arguments AND the parameterized module like in your code suffer from the same problem. That being that you have to specify up-front what you think the maximum number of arguments should be.

Mind you, in this particular case that is not a real big problem. But it doesn't count in it's favor either when weighing the pro's and con's of the several options.

The advantage of the module over the function is that with the module you can then indeed supply for example between 0 and 8 (unnamed) parameters. Or as in your example you can provide between 0 and 3 parameters. With the function you will always have to provide the fixed maximum number of arguments.

I guess this is just one of those "verilog sucks in different ways than vhdl" things and plough on. I was just hoping that someone knew of a trick to work around the function limitation thing that I hadn't noticed.

Right now I use a function that accepts 8 arguments, where you fill in the "optionals" with a 0 value. Not perfect, but I guess it will have to do. :p
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top