Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module vending_fsm
- (
- input clk_i
- , input rst_i
- , input coin_put_i // 1-cycle tick
- , input [7:0] coin_sum_bi
- , input select_good_i // 1-cycle tick
- , input [7:0] select_good_num_bi
- , input [31:0] select_good_cost_bi
- , input cancel_i // 1-cycle tick
- , output logic give_order_o // 1-cycle tick
- , output logic [7:0] give_order_num_bo
- , output logic give_change_o // 1-cycle tick
- , output logic [31:0] give_change_sum_bo
- );
- enum {IDLE, CREDIT, DELIIVERY, CHANGE} state;
- logic [7:0] selected_good;
- logic [31:0] credit_accumulated;
- always @(posedge clk_i)
- begin
- if (rst_i)
- begin
- state <= IDLE;
- give_order_o <= 1'b0;
- give_order_num_bo <= 0;
- give_change_o <= 1'b0;
- give_change_sum_bo <= 0;
- end
- else
- begin
- // default values for "ticks"
- give_order_o <= 1'b0;
- give_change_o <= 1'b0;
- case (state)
- IDLE:
- begin
- if (coin_put_i)
- begin
- credit_accumulated <= coin_sum_bi;
- state <= CREDIT;
- end
- end
- CREDIT:
- begin
- if (coin_put_i)
- begin
- credit_accumulated <= credit_accumulated + coin_sum_bi;
- end
- else if (select_good_i)
- begin
- selected_good <= select_good_num_bi;
- credit_accumulated <= credit_accumulated - select_good_cost_bi;
- state <= DELIIVERY;
- end
- else if (cancel_i)
- begin
- state <= CHANGE;
- end
- end
- DELIIVERY:
- begin
- give_order_o <= 1'b1;
- give_order_num_bo <= selected_good;
- state <= CHANGE;
- end
- CHANGE:
- begin
- give_change_o <= 1'b1;
- give_change_sum_bo <= credit_accumulated;
- state <= IDLE;
- end
- endcase
- end
- end
- endmodule // vending_fsm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement