Search code examples
verilogsystem-veriloghdl

Behaviour of Blocking Assignments inside Tasks called from within always_ff blocks


Have looked for an answer to this question online everywhere but I haven't managed to find an answer yet.

I've got a SystemVerilog project at the moment where I've implemented a circular buffer in a separate module to the main module. The queue module itself has a synchronous portion that acquires data from a set of signals but it also has a combinatorial section that responds to an input. Now when I want to query the state of this queue in my main module a task, inside an always_ff block sets the input using a blocking assignment, then the next statement reads the output and acts on that.

An example would look something like this in almost SystemVerilog:

module foo(clk, ...)

    queue = queue(clk, ...)   

    always_ff@(posedge clk)
    begin
        check_queue(...)
    end

    task check_queue();
    begin
        query_in = 3;
        if (query_out == 5)
        begin
            <<THINGS HAPPEN>>
        end
    end
    endtask
endmodule

module queue(clk, query_in, query_out)
    always_comb
    begin
      query_out = query_in + 2;
    end
endmodule

My question essentially comes down to, does this idea work? In my head because the queue is combinatorial it should respond as soon as the input stimulus is applied it should be fine but because it's within a task within an always_ff block I'm a bit concerned about the use of blocking assignments.

Can someone help? If you need more information then let me know and I can give some clarifications.


Solution

  • This creates a race condition and most likely will not work. It has nothing to do with your use of a task. You are trying to read the value of a signal (queue_out) that is being assigned in another concurrent process. Whether it gets updated or not by the time you get to the If statement is a race. U?se a non-blocking assignment to all variable that go outside of the always_ff block and it guarantees you get the previous value.