Wednesday, 1 June 2016

Race Condition:

Race condition is nothing but if two expressions are evaluated in same time unit then depending on the order of evaluation of expressions output value changes.

Example:
always@(posedge clk, negedge rst) begin
  if (rst) a = 0;
  else a = b;
end

always@(posedge clk, negedge rst) begin
  if (rst) b = 1;
  else b = a;
end

Here after reset  if first always block executes first and then second always then a = 1, b = 1. If second one executes first and then first one executes then a = 0, b = 0.

We can avoid the race condition by using NBA as follows:

always@(posedge clk, negedge rst) begin
  if (rst) a <= 0;
  else a <= b;
end

always@(posedge clk, negedge rst) begin
  if (rst) b <= 1;
  else b <= a;
end

Here what ever the order of execution of always block the output is same as a=1,b=0 during first posedge of clock and a=0,b=1 in next posedge of clock i.e., nothing but feedback oscillator.

Causes of race condition:

1) If two always blocks drives same variable.

Example:
always@(posedge clk) begin
  a = 1;
end
always@(posedge clk) begin
  a = 0;
end

2) one always block is dependent on other always block.

Example:
always@(posedge clk) begin
  a = b;
end
always@(posedge clk) begin
  c = a;
end

Tips to avoid Race condition:

1) Never ever drive single variable in multiple always blocks.
2) Use NBA inside always block.

No comments:

Post a Comment