Verilog automatic task

cdleary picture cdleary · Aug 29, 2008 · Viewed 37.3k times · Source

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

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

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

Answer

Matt J picture Matt J · Sep 19, 2008

"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".

"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.

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.