Advertisement
AlexanderAntonov

Untitled

Oct 18th, 2022
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module vending_fsm
  2. (
  3.     input clk_i
  4.     , input rst_i
  5.    
  6.     , input coin_put_i                  // 1-cycle tick
  7.     , input [7:0] coin_sum_bi
  8.    
  9.     , input select_good_i               // 1-cycle tick
  10.     , input [7:0] select_good_num_bi
  11.     , input [31:0] select_good_cost_bi
  12.    
  13.     , input cancel_i                    // 1-cycle tick
  14.  
  15.     , output logic give_order_o                 // 1-cycle tick
  16.     , output logic [7:0]  give_order_num_bo
  17.  
  18.     , output logic give_change_o                // 1-cycle tick
  19.     , output logic [31:0] give_change_sum_bo
  20. );
  21.  
  22. enum {IDLE, CREDIT, DELIIVERY, CHANGE} state, state_next;
  23.  
  24. logic [7:0] selected_good, selected_good_next;
  25. logic [31:0] credit_accumulated, credit_accumulated_next;
  26.  
  27. // "sequential" always block for register assignment
  28. always @(posedge clk_i)
  29.     begin
  30.     if (rst_i)
  31.         begin
  32.         state <= IDLE;
  33.         selected_good <= 0;
  34.         credit_accumulated <= 0;
  35.         end
  36.     else
  37.         begin
  38.         state <= state_next;
  39.         selected_good <= selected_good_next;
  40.         credit_accumulated <= credit_accumulated_next;
  41.         end
  42.  
  43.     end
  44.  
  45. // "combinational" always block for next-state generation
  46. always @*
  47.     begin
  48.  
  49.     // Restoring current register values
  50.     state_next = state;
  51.     selected_good_next = selected_good;
  52.     credit_accumulated_next = credit_accumulated;
  53.  
  54.     //...
  55.  
  56.     case (state_next)
  57.  
  58.     //...
  59.  
  60.         CREDIT:
  61.             begin
  62.             if (coin_put_i)
  63.                 begin
  64.                 credit_accumulated_next = credit_accumulated_next + coin_sum_bi;
  65.                 end
  66.             else if (select_good_i)
  67.                 begin
  68.                 selected_good_next = select_good_num_bi;
  69.                 check_credit = credit_accumulated_next - select_good_cost_bi;
  70.                 if (check_credit < 0)
  71.                     begin
  72.                     state_next = CHANGE;
  73.                     end
  74.                 else
  75.                     begin
  76.                     credit_accumulated_next <= check_credit;
  77.                     state_next = DELIIVERY;
  78.                     end
  79.                 end
  80.             else if (cancel_i)
  81.                 begin
  82.                 state_next = CHANGE;
  83.                 end
  84.             end
  85.  
  86.  
  87.     //...
  88.  
  89.     end
  90.  
  91. endmodule // vending_fsm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement