0

I know that I should be using a non-blocking assignments in sequential always assignments. However, I accidentally happen to use a blocking assignment in part of my code, here it is:

reg tb_strobe = 0;
reg [9:0] tb_strobe_cnt = 0;
reg tb_sync = 0;

always@(posedge tb_clkh)
begin
    if (~tb_resetl) begin
        tb_strobe     <= 0;
        tb_strobe_cnt <= 0;
        tb_sync     <= 0;
    end
    else begin

        if (tb_strobe_cnt == 1022) begin
            tb_strobe <= 1;
            tb_strobe_cnt <= tb_strobe_cnt + 1;
        end else if (tb_strobe_cnt == 1023) begin
            tb_strobe <= 0;
            tb_strobe_cnt <= 0;
        end else begin
            tb_strobe <= 0;
            tb_strobe_cnt <= tb_strobe_cnt + 1;
        end

        if (tb_strobe == 1) begin
            tb_sync = 1;             //  <-- this is the mistakenly used blocking assignment
        end else begin
        end

    end
end

And then my simulator behaves unpredictably, and once I fixed that assignment to be a non-blocking one, it started working fine !!!!

I am curious as to what was wrong with the above (in my specific code)? In the way that I used it, since I am only calling tb_sync once in my code, I wasn't expecting any unpredictable behavior... And tb_sync is not being assigned anywhere else in the code. Any idea what is wrong?

1 Answer 1

1

Non-blocking assignments are used to prevent race conditions between multiple processes that write and read the same variable on the same clock edge. It just takes one process that writes, and another process that reads the same variable on the same clock edge to create that race. Your example does not show the process that reads tb_sync, but I'm assuming that's where the race is.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect explanation. It makes lots of sense. Yes in another process, I am reading "tb_sync", and that is exactly the problem. Because on the same clock edge one of them writes and the other reads. However, when I make it non-blocking, the write process will not happen until the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.