-1

hi guys i wrote a while loop inside a for loop but it is not working is there something wrong with my coding?

always@ (posedge clk) begin
  if (delay) 
    D = 1;
  else
    D = 0;

  if (a) begin
    for (g=0;g<10;g=g+1) begin
      high <= high_in;
      low <= low_in;
      delay = 0;
      while (count == 0) begin // when i simulate the waveform cant come 
                                     out and it stuck at here
        ws = 1;
        count <= D;
        delay = 1;
      end
      delay = 0;
    end
  end
  else begin
    //other coding
  end
end
H.Modh
  • 428
  • 4
  • 12
  • Your code looks more like a C code. Remember that verilog is an HDL not a programming language. Try to make your code as synthesizable as you can – ssinad Mar 11 '16 at 19:41

2 Answers2

0

If you enter the while loop when count is zero, you have an infinite loop. You've got a non-blocking assignment on count, so it won't update. Look up blocking vs. non-blocking assignments.

EML
  • 9,619
  • 6
  • 46
  • 78
  • hi sir i had change to blocking assignment but its still the same so which part is actually wrong? – Avelyn Goh Mar 09 '16 at 13:23
  • If you've got a blocking assignment, then the loop will carry on executing until `count` gets a new value, from `D`. How is that going to happen? What if `D` is 0? Your answer is presumably that you set `delay` to 1 inside the loop. But this doesn't exit the loop. Even if you did exit the loop, you set `delay` to 0 again. Throw the code away and start again. – EML Mar 09 '16 at 15:03
0

I find some basic issues with this code :- 1. Mix and match of blocking and non-blocking in same always block. 2. use of blocking statement in a always block is a problem creator 3. No reset/default value of count, delay outside of loop. although decision making is based on that.

Few advice :-

  1. Try to use for and while loop outside always statement.
  2. try to assign default values for the loop usage.

As suggested by EML, try writing it again. Try to not combine different assignment as far as possible.

Sourabh
  • 634
  • 5
  • 12