queencythea
Newbie level 4
- Joined
- Sep 14, 2013
- Messages
- 5
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 49
I HAVE THIS VERILOG CODE TO IMPLEMENT VENDING MACHINE ON MY ALTERA DE2 BOARD. IT WORKS BUT IT TRANSFERS TO THE NEXT STATE EVEN IF I INPUTTED NOTHING. PLEASE HELP.
module mealyvend(N, D, clk, reset, open);
// Mealy FSM for a vending machine
input N, D, clk, reset;
output open;
reg [1:0] pstate, nstate;
reg [0:6]open;
wire clk1Hz;
parameter S0=2'b00, S5=2'b01, S10=2'b10, S15=2'b11;
onesecond u1(clk,clk1Hz);
// Next state and ouptut combinational logic
always @(N or D or pstate or reset)
if (reset)
begin nstate = S0; open = 7'b0000001; end
else
case (pstate)
S0: begin open = 7'b0000001; if (N) nstate = S5;
else if (D) nstate = S10;
else nstate = S0; end
S5: begin open = 7'b0000001; if (N) nstate = S10;
else if (D) nstate = S15;
else nstate = S5; end
S10: if (N | D) begin nstate = S15; open = 7'b0000001; end
else begin nstate = S10; open = 7'b0000001; end
S15: begin nstate = S0; open = 7'b1001111; end
default: nstate=S0;
endcase
// FF logic, use nonblocking assignments "<="
always @(posedge clk1Hz)
pstate <= nstate;
endmodule
module mealyvend(N, D, clk, reset, open);
// Mealy FSM for a vending machine
input N, D, clk, reset;
output open;
reg [1:0] pstate, nstate;
reg [0:6]open;
wire clk1Hz;
parameter S0=2'b00, S5=2'b01, S10=2'b10, S15=2'b11;
onesecond u1(clk,clk1Hz);
// Next state and ouptut combinational logic
always @(N or D or pstate or reset)
if (reset)
begin nstate = S0; open = 7'b0000001; end
else
case (pstate)
S0: begin open = 7'b0000001; if (N) nstate = S5;
else if (D) nstate = S10;
else nstate = S0; end
S5: begin open = 7'b0000001; if (N) nstate = S10;
else if (D) nstate = S15;
else nstate = S5; end
S10: if (N | D) begin nstate = S15; open = 7'b0000001; end
else begin nstate = S10; open = 7'b0000001; end
S15: begin nstate = S0; open = 7'b1001111; end
default: nstate=S0;
endcase
// FF logic, use nonblocking assignments "<="
always @(posedge clk1Hz)
pstate <= nstate;
endmodule