I don't understand how to avoid inferred latches in my design of ASM Here's my code.
module ASM (
input[7:0] SW,
input Start, Reset, Clock,
output reg Led,
output reg[3:0] result);
reg[2:0] state, next_state;
reg[7:0] A;
wire Done;
parameter S1 = 3'd1;
parameter S2 = 3'd2;
parameter S3 = 3'd3;
parameter S4 = 3'd4;
parameter S5 = 3'd5;
parameter S6 = 3'd6;
always @(posedge Clock)
begin
if (!Reset)
state <= next_state;
else
state <= S1;
end
always @(state, Start, A)
begin
case(state)
S1: begin
if (Start)
next_state = S2;
else
next_state = S1;
end
S2: begin //check A == 0
if (A == 8'd0)
next_state = S6; //done
else
next_state = S3;//check A0
end
S3: begin //check A0
if (A[0])
next_state = S4; //result++
else
next_state = S5; //A>>1
end
S4: begin //result++
next_state = S5;
end
S5: begin //A>>1
next_state = S2;
end
S6: begin //done
if (Start)
next_state = S6;
else
next_state = S1;
end
default: begin
next_state = S1;
end
endcase
end
always @(state, SW)
begin
//result = 4'b0;
//A = 8'd0;
//Led = 1'b0;
case (state)
S1: begin
A = SW;
result = 4'b0;
Led = 1'b0;
end
S2: begin
end
S3: begin
end
S4: begin
result <= result + 4'd1;
end
S5: begin
A = A >> 1;
end
S6: begin
Led = 1'b1;
end
default: begin
result = 4'b0;
Led = 1'b0;
end
endcase
end
endmodule
I understand that i should initialize variables "A", "result" and "led" before "case" statement in second "always" block. But if do this every time when "always" block is active I loose my previous results of variables and assign them again to initial values and thus whole design is wrong. So that I can't avoid latches and simply shouldn't initialize these variables. But latches is bad practice. So can you explain how i should change code to avoid this?