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, state_next;
- logic [7:0] selected_good, selected_good_next;
- logic [31:0] credit_accumulated, credit_accumulated_next;
- // "sequential" always block for register assignment
- always @(posedge clk_i)
- begin
- if (rst_i)
- begin
- state <= IDLE;
- selected_good <= 0;
- credit_accumulated <= 0;
- end
- else
- begin
- state <= state_next;
- selected_good <= selected_good_next;
- credit_accumulated <= credit_accumulated_next;
- end
- end
- // "combinational" always block for next-state generation
- always @*
- begin
- // Restoring current register values
- state_next = state;
- selected_good_next = selected_good;
- credit_accumulated_next = credit_accumulated;
- //...
- case (state_next)
- //...
- CREDIT:
- begin
- if (coin_put_i)
- begin
- credit_accumulated_next = credit_accumulated_next + coin_sum_bi;
- end
- else if (select_good_i)
- begin
- selected_good_next = select_good_num_bi;
- check_credit = credit_accumulated_next - select_good_cost_bi;
- if (check_credit < 0)
- begin
- state_next = CHANGE;
- end
- else
- begin
- credit_accumulated_next <= check_credit;
- state_next = DELIIVERY;
- end
- end
- else if (cancel_i)
- begin
- state_next = CHANGE;
- end
- end
- //...
- end
- endmodule // vending_fsm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement