here is the working FSM. I like this better as it is easier to see the output states along with the state priority in one shot.
reg [7:0] Rps2_command;
reg Rps2_send_command;
assign ps2_send_command = Rps2_send_command;
assign ps2_command = Rps2_command;
/*****************************************************************************
* Finite State Machine(s) *
*****************************************************************************/
// This FSM resets the PS/2 device and then keeps track of packets, which
// consist of three data bytes. Packets could be from a key board or mouse,
// but this code is needed only for tracking movements of the mouse.
always @(y_Q, was_sent, ps2_data_en, plugged_in, ack_received)
begin: state_table
case (y_Q)
S0:
begin
Rps2_send_command <= 1'b1;//S0 output values
Rps2_command <= 8'hF4;
mouse_packet <= 1'b0;
if
(was_sent==1'b1)//state priority 1
Y_D<=S1;
else
Y_D<=S0;//state priority 2
end
S1:
begin
Rps2_send_command <= 1'b0;//S1 output values
Rps2_command <= 8'hF4;
mouse_packet <= 1'b0;
if
(ack_received==1'b1)//state priority 1
Y_D= S2;
else
Y_D= S1;//state priority 2
end
S2:
begin
Rps2_send_command <= 1'b1;//S2 output values
Rps2_command <= 8'hF4;
mouse_packet <= 1'b0;
if
(ps2_data_en==1'b1)//state priority 1
Y_D= S3;
else
Y_D= S2;//state priority 2
end
S3:
begin
Rps2_send_command <= 1'b1;//S3 output values
Rps2_command <= 8'hF4;
mouse_packet <= 1'b0;
if
(plugged_in==1'b1)//state priority 1
Y_D= S0;
else if
(ps2_data_en==1'b0)//state priority 2
Y_D= S3;
else
Y_D= S4;//state priority 3 when plugged_in==0 and ps2_data_en==1
end
S4:
begin
Rps2_send_command <= 1'b1;//S4 output values
Rps2_command <= 8'hF4;
mouse_packet <= 1'b0;
if
(plugged_in==1'b1)//state priority 1
Y_D= S0;
else if
(ps2_data_en==1'b0)//state priority 2
Y_D= S4;
else
Y_D= S5;//state priority 3 when plugged_in==0 and ps2_data_en==1
end
S5:
begin
if
(!plugged_in)
mouse_packet=1'b1;//S5 output values
if (plugged_in==1'b1)//state priority 1
Y_D= S0;
else
Y_D= S4;//state priority 2
end
default: Y_D = 3'bxxx;
endcase
end // state_table