SHARE
TWEET

Untitled

a guest Aug 11th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module fsm_player(enable, clk, reset_n, game_state, draw, erase, update_coordinates, finished);
  2.  
  3.     input enable; // sent from TOP
  4.     input clk;
  5.     input reset_n;
  6.     input game_state; // indicates whether alive or dead
  7.     reg [1:0]state_next;
  8.     output reg draw, erase, update_coordinates; // output to datapath
  9.     output reg finished; //output for the control FSM to select between different modules to display on VGA
  10.  
  11.    
  12.     wire slow_clk1, slow_clk2;
  13.     wire [24:0] q1, q2;
  14.     reg [1:0] state_reg;
  15.     //00 is get_coords
  16.     //01 is draw
  17.     //10 is erase
  18.     //11 is wait
  19.  
  20.     localparam [1:0] S_UPDATE_COORD = 00, S_DRAW = 01, S_ERASE = 10, S_WAIT = 11;
  21.  
  22.     ratedivider1 r0(slow_clk2, 25'd4, reset_n, q1);
  23.     //ratedivider r0(clk, 25'd100_000, reset_n, q1);
  24.     ratedivider1 r1(clk, 25'd999_999, reset_n, q2);
  25.  
  26.     assign slow_clk1 = (q1 == 0) ? 1: 0; // Controls the duration of DRAW state
  27.     assign slow_clk2 = (q2 == 0) ? 1: 0; // Controls the duration of ERASE & UPDATE_COORD states & WAIT
  28.  
  29.     always @(posedge clk, posedge reset_n) begin
  30.         if (reset_n == 1) begin
  31.             state_reg <= S_UPDATE_COORD;
  32.            
  33.         end
  34.  
  35.         else begin
  36.             state_reg <= state_next;
  37.  
  38.         end
  39.  
  40.     end
  41.  
  42.     always @(*) begin
  43.    
  44.     if(reset_n) begin
  45.         finished <= 1'b0;
  46.     end
  47.         case (state_reg)
  48.             S_UPDATE_COORD: begin
  49.                 if (game_state == 1 && slow_clk2 == 1 && enable == 1) begin
  50.                     state_next <= S_DRAW;
  51.                 end
  52.  
  53.                 else if (game_state == 0 && slow_clk2 == 1 && enable == 1) begin
  54.                     state_next <= S_ERASE;
  55.                 end
  56.                 else state_next <= state_next;
  57.  
  58.             end
  59.             S_DRAW: begin
  60.                 if (slow_clk1 == 1 && enable == 1)begin
  61.                     state_next <= S_WAIT;
  62.                     finished <= 1'b1;
  63.                     end
  64.                 else state_next <= state_next;
  65.                 end
  66.             S_ERASE: begin
  67.                 if (game_state == 1 && slow_clk2 == 1 && enable == 1) begin
  68.                     state_next <= S_UPDATE_COORD; //was previously S_UPDATE_COORD
  69.                 end
  70.            
  71.                 else if (game_state == 0 && slow_clk2 == 1) begin
  72.                     state_next <= S_ERASE;
  73.                     //loop where the game is over, can never change states or draw again unless game_state is changed.
  74.                 end
  75.                 else state_next <= state_next;
  76.  
  77.             end
  78.  
  79.             S_WAIT: begin
  80.             /*  if (enable == 1 && slow_clk2 == 1) begin //used faster of the 2 clocks, the one for erase;
  81.                     state_next <= S_ERASE;
  82.                     finished <= 1'b0; //can start getting input again, and stops signal being sent to FSM.
  83.                 end
  84.  
  85.                 else if (enable == 0 && slow_clk2 == 1) begin
  86.                     state_next <= state_next;
  87.                     finished <= 1'b0;
  88.  
  89.                 end
  90.                 else if (game_state == 0 && slow_clk2 == 1) begin
  91.                     state_next <= S_ERASE;
  92.                 end
  93.                 else state_next <= state_next; */
  94.                 state_next <= (enable == 1 && slow_clk2 == 1|| game_state == 0) ? S_ERASE: S_WAIT;
  95.  
  96.             end
  97.  
  98.         endcase
  99.  
  100.     end
  101.    
  102.  
  103.  
  104.     always @(posedge clk) begin
  105.         case(state_reg)
  106.            
  107.            
  108.            
  109.             S_UPDATE_COORD:begin    update_coordinates <= 1'b1;
  110.                                         draw <= 1'b0;
  111.                                         erase <= 1'b0;
  112.                                        
  113.             end
  114.             S_DRAW: begin draw <= 1'b1;
  115.                               update_coordinates <= 1'b0;
  116.                               erase <= 1'b0;
  117.                              
  118.             end
  119.             S_ERASE: begin erase <= 1'b1;
  120.                                 update_coordinates <= 1'b0;
  121.                                 draw <= 1'b0;
  122.             end
  123.             S_WAIT: begin erase <= 1'b0;
  124.                           update_coordinates <= 1'b0;
  125.                           draw <= 1'b0;
  126.             end
  127.            
  128.            
  129.         endcase
  130.  
  131.     end
  132.    
  133.  
  134. endmodule
  135.  
  136.  
  137. // To output the controls of the player; slow it down since 50MHZ clock too fast
  138.  module ratedivider1 (clk, load, reset_n, q); //q_qire / q_out; cant be a wire
  139.     input clk, reset_n;
  140.      input [24:0] load;
  141.     output reg [24:0] q;
  142.  
  143.     always @ (posedge clk)
  144.     begin
  145.         if (reset_n) begin
  146.             q <= load;
  147.            
  148.             end
  149.         else
  150.             begin
  151.                 if (q == 0) begin
  152.                     q <= load;
  153.                    
  154.                      end
  155.                 else begin
  156.                      
  157.                     q <= q - 1'b1;
  158.          
  159.                           end
  160.             end
  161.  
  162.     end
  163.  
  164.  
  165. endmodule
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top