0% found this document useful (0 votes)
19 views3 pages

Candy Vending Machine

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Candy Vending Machine

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

module candy_vending_machine(

input wire N, // Nickel input

input wire D, // Dime input

input wire clk, // Clock input

input wire rst, // Reset input

output reg candy, // Candy dispense output

output reg refund // Refund output

);

// Define state encoding using localparam

localparam [2:0] S0 = 3'b000, // 0 cents (initial state)

S5 = 3'b001, // 5 cents

S10 = 3'b010, // 10 cents

S15 = 3'b011, // 15 cents (dispense candy)

S20 = 3'b100; // 20 cents (refund nickel and go to S15)

// Declare current and next state variables

reg [2:0] current_state, next_state;

// Sequential block for state transitions

always @(posedge clk or posedge rst) begin

if (rst)

current_state <= S0; // Reset to start state

else

current_state <= next_state; // Move to next state

end

// Combinational block for next state logic and outputs

always @(*) begin

// Default outputs

candy = 1'b0;
refund = 1'b0;

next_state = current_state; // Default to remain in current state

// Next state logic

case (current_state)

S0: begin

if (N)

next_state = S5;

else if (D)

next_state = S10;

end

S5: begin

if (N)

next_state = S10;

else if (D)

next_state = S15;

end

S10: begin

if (N)

next_state = S15;

else if (D)

next_state = S20;

end

S15: begin

candy = 1'b1; // Dispense candy

next_state = S0; // Go back to S0 after dispensing candy

end

S20: begin

refund = 1'b1; // Refund a nickel

next_state = S15; // Move to S15 (dispense candy after refund)

end
endcase

end

endmodule

You might also like