<p>What does it mean if a task is declared with the automatic keyword in Verilog?</p>

<pre><code>task automatic do_things;
  input [31:0] number_of_things;
  reg [31:0] tmp_thing;
  begin
    // ...
  end
endtask;
</code></pre>

<p>Note: This question is mostly because I'm curious if there are any hardware programmers on the site. :)</p>

[{'answer_id': 35193, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 5, 'selected': True, 'text': '<p>It means that the task is re-entrant - items declared within the task are dynamically allocated rather than shared between different invocations of the task.</p>\n\n<p>You see - some of us do Verilog...  (ugh)</p>\n'}, {'answer_id': 38600, 'author': 'Marty', 'author_id': 4131, 'author_profile': 'https://Stackoverflow.com/users/4131', 'pm_score': 2, 'selected': False, 'text': '<p>The "automatic" keyword also allows you to write recursive functions (since verilog 2001). I believe they should be synthesisable if they bottom out, but I\'m not sure if they have tool support.  </p>\n\n<p>I too, do verilog! </p>\n'}, {'answer_id': 70497, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>As Will and Marty say, the automatic was intended for recursive functions.</p>\n\n<p>If a normal (i.e. not automatic) function is called with different values and processed by the simulator in the same time slice, the returned value is indeterminate. That can be quite a tricky bug to spot! This is only a simulation issue, when synthesised the logic will be correct.</p>\n\n<p>Making the function automatic fixes this.</p>\n'}, {'answer_id': 99929, 'author': 'Matt J', 'author_id': 18528, 'author_profile': 'https://Stackoverflow.com/users/18528', 'pm_score': 5, 'selected': False, 'text': '<p>"automatic" does in fact mean "re-entrant".  The term itself is stolen from software languages -- for example, C has the "auto" keyword for declaring variables as being allocated on the stack when the scope it\'s in is executed, and deallocated afterwards, so that multiple invocations of the same scope do not see persistent values of that variable.  The reason you may not have heard of this keyword in C is that it is the default storage class for all types :-)  The alternatives are "static", which means "allocate this variable statically (to a single global location in memory), and refer to this same memory location throughout the execution of the program, regardless of how many times the function is invoked", and "volatile", which means "this is a register elsewhere on my SoC or something on another device which I have no control over; compiler, please don\'t optimize reads to me away, even when you think you know my value from previous reads with no intermediate writes in the code".</p>\n\n<p>"automatic" is intended for recursive functions, but also for running the same function in different threads of execution concurrently.  For instance, if you "fork" off N different blocks (using Verilog\'s fork->join statement), and have them all call the same function at the same time, the same problems arise as a function calling itself recursively.</p>\n\n<p>In many cases, your code will be just fine without declaring the task or function as "automatic", but it\'s good practice to put it in there unless you specifically need it to be otherwise.</p>\n'}, {'answer_id': 57490184, 'author': 'TRoa', 'author_id': 9547499, 'author_profile': 'https://Stackoverflow.com/users/9547499', 'pm_score': 0, 'selected': False, 'text': '<p>In computing, a computer program or subroutine is called re-entrant if multiple invocations can safely run concurrently (Wikipedia). \nIn simple words, the keyword automatic makes it safe, when multiple instances of a task run at a same time.\n:D</p>\n'}]
['https://Stackoverflow.com/questions/35170', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3594/']