Altera_Forum
Honored Contributor
17 years agoThis short verilog code is not working
The following code is supposed to be easy and straight forward, but its not giving the correct results!
I want the output to follow input whenever the counter = 5. But whenever there is an edge in the input, i want the counter to load the value of 3. The counter is initialied to 3. assign invdata = ~data; always @ (posedge clk or posedge data or posedge invdata) begin if (clk) begin if (counter==5) begin output<=input; counter<=1; end else counter<=counter+1; end else if (data) counter<=3; else counter<=3; end the results shows that the counter will always be stuck on 3. but, on every clock edge the counter will change randomly to around 5 values then go back to 3 after a while.why? isnt that code 100% correct? if not, whats the correct code to do the same job? im simulating on quartus II. Another thought, what if data has a positive edge, and the it enters the above always loop at the same time clk is high. this way it will act exactly the same as if a posedge clk occured. no? and thats wrong! what should i do in such a case? PS: the input is a clock with period = 200ns. clk is a clk with period = 20ns. Thanx.