Altera_Forum
Honored Contributor
13 years agoProblems with verilog state machine
Hi.
My state machine seems to get stuck in one of the states, but if you remove px_gate it begins to work properly. I can not understand this strange behavior. Code:
module sync2de(clk,hs,vs,px_in,de,px_out);
input clk;
input hs;
input vs;
input px_in;
output px_out;
output de;
wire px_gate;
reg state=0;
reg counter;
assign px_out=clk && px_gate;
always@(state)
begin
case(state)
0: begin
px_gate=1;
de=1;
end
1: begin
de=0; px_gate=1;
end
2: begin
de=0; px_gate=1;
end
3: begin
de=0; px_gate=0;
end
4: begin
de=0; px_gate=0;
end
endcase
end
always@(negedge clk)
begin
case(state)
0: //wait sync
begin
if(!vs)
begin
counter=v_blank_lines-1;
state=1;
end
else
if(!hs)
begin
counter=h_blank_px-1;
state=2;
end
end
1: // vs
begin
if(counter)
begin
state=3;
end
else counter=counter-1;
end
2: //hs
begin
if(counter)
begin
state=4;
end
else counter=counter-1;
end
3: //vs_end
begin
if(vs)
begin
state=0;
end
end
4: //hs_end
begin
if(hs)
begin
state=0;
end
end
endcase
end
endmodule
Thanks.