0

Here's the problematic code:

function automatic [31:0] W;
    input [6:0] param;
    W = (param<16) ? 32'b0 : W(param-7);
endfunction

Basically, iverilog (Icarus Verilog) just gives me a Segmentation fault: 11 vvp svsim error.
I tried a bit of debugging and it seems not to like the recursion, even though I have a recursion anchor.
Calling the function from within itself isn't an issue though. Tested that, too.
Any help is appreciated!

EDIT:
Here I have the piece of code that calls this function unsuccessfully:

always @(negedge clk) begin
    t1 <= W(j);
end
unixb0y
  • 979
  • 1
  • 10
  • 39
  • how do you call the function? – Serge Feb 04 '18 at 02:07
  • @unixb0y Please post an [MCVE](https://stackoverflow.com/help/mcve) The code runs fine for me when I call `W(5)` or `W(55)`. Note that the function will always return `0`. – sharvil111 Feb 04 '18 at 02:44
  • Okay I’ll post later @sharvil111 . Did you use Icarus Verilog, too or an other tool? – unixb0y Feb 04 '18 at 03:21
  • Your function will never return anything other than 0. Is that what you intended? (Tried on iverilog, and others) – dave_59 Feb 04 '18 at 04:05
  • @unixb0y Yes I tried iverilog at EDAPlayground and other simulators and it worked fine. – sharvil111 Feb 04 '18 at 04:25
  • @dave_59 Yeah, that's correct. The base case / recursion anchor is going to be substituted by something else but I have it like this for this stage of the application. – unixb0y Feb 04 '18 at 13:12
  • @sharvil111 I posted the code that might be useful – unixb0y Feb 04 '18 at 13:33
  • 1
    Your code will run under stacktrace, infinite zero delay loop if `j` is unknown (`x`). It would be helpful it you check if `j` is `x` or `z` when the function is called. Driving of `j` is unclear in the current code. – sharvil111 Feb 04 '18 at 14:17
  • Good idea, I'll try that! – unixb0y Feb 04 '18 at 14:20
  • @sharvil111 If you add this as an answer, I'll be happy to accept it. This solved my issue! Thanks! – unixb0y Feb 04 '18 at 14:43
  • 1
    That's fine.. Good to see that issue is resolved :) – sharvil111 Feb 04 '18 at 14:45
  • No really, this way other people will also find the answer quicker than searching in the comments ;-) Otherwise I'll add it! – unixb0y Feb 04 '18 at 14:54

3 Answers3

1

Change your code to

function automatic [31:0] W (input [6:0] param);
    if (param>=16)
       W = W(param-7);
    else
       W = 0;
endfunction

That way if param is unknown, it won't go into infinite recursion.

dave_59
  • 39,096
  • 3
  • 24
  • 63
1

As @sharvil111 suggested, the value that I passed was 1'bX at that point of time.
So I made sure to set it to 0 before calling.
Thanks for the other suggestions, but e.g. what @dave_59 said didn't do the trick in my case as param wasn't too small, it was just not defined yet.

unixb0y
  • 979
  • 1
  • 10
  • 39
0

If you give valid inputs, then you should not see any Seg fault. if you still see, then need to check with the tool vendor.

TgP
  • 1
  • 1