Forum Discussion
Altera_Forum
Honored Contributor
16 years agoThe previous posts have basically answered the question but I'll throw in my 2 cents if it helps.
1 - The first thing I always tell beginners is never use a blocking assignment '=' inside a clocked always block. If you know what you are doing it's fine and can be useful but in general just don't do it and it's certainly not necessary. Use non-blocking assignments '<=' for all clocked processes. Use blocking assignments '=' in combinatorial always blocks. This one rule will save you many headaches. Once you're experienced and truly understand the language, you can violate this rule. Mixing blocking and non-blocking assignments inside an always block can confuse people so I've seen many company's prohibit it in their coding standards. 2 - The problem has to do with the simulator. The simulator processes the code and detects these two always blocks. Because they are separate from each other, the simulator is free to schedule them however it wants. So you have no idea which always block the simulator will choose to execute first and it can and will very between simulators. Because the statements within the always block are blocking, they are executed immediately. So if we follow the simulator's execution with the blocking assignment: 1 - reset is applied. y1 = 0; y2 = 1; 2 - reset is removed. 3 - posedge clk occurs. 4 - Simulator evaluates 2nd always block first. . 4a - y2 = y1 (which is 0) so y2 = 0; 5 - Simulator evaluates 1st always block next. . 5a - y1 = y2(which has been set to 0) so y1 = 0; Now let's follow the simulators execution with non-blocking assignments: 1 - reset is applied. y1 = 0; y2 = 1; 2 - reset is removed. 3 - posedge clk occurs. 4 - Simulator evaluates 2nd always block first. . 4a - y2 is scheduled to be set to the current value of y1. so y2_next = 0; (current value of y2 is still 1). 5 - Simulator evaluates 1st always block next. . 5a - y1 is scheduled to be set to the current value of y2. so y1_next = 1; (current value of y1 is still 0). 6 - All always blocks have been evaluated. Simulator executes scheduled assignments. . 6a y2 = y2_next = 0; . 6b y1 = y1_next = 1; Jake