Advertisement
markkurvers

Untitled

Jun 30th, 2021
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 100ps
  2.  
  3. module pump_fsm (
  4.     input  wire [0:0] clk,
  5.     input  wire [0:0] reset,
  6.     input  wire [0:0] update,
  7.     input  wire [1:0] command,
  8.     output reg  [0:0] pump,
  9.     output reg  [0:0] led
  10.     );
  11.  
  12.     /* Define state parameters */
  13.     localparam
  14.     off     = 0,
  15.     standby = 1,
  16.     working = 2;
  17.    
  18.     /* Define command values */
  19.     localparam
  20.     turnoff   = 2'b00,
  21.     turnon    = 2'b01,
  22.     stoppump  = 2'b10,
  23.     startpump = 2'b11;    
  24.    
  25.     // Add your own code here
  26.     reg [1:0] cur_state;
  27.     reg [1:0] next_state;
  28.     reg prev_update;
  29.     reg cur_led;
  30.     reg cur_pump;
  31.    
  32.     always @(posedge clk) begin
  33.         cur_state<=next_state;
  34.         prev_update<=update;
  35.          led <= cur_led;
  36.      pump <= cur_pump;
  37.     end
  38.    
  39.     always @(*) begin
  40.     if(reset)   begin   next_state=0;
  41.                         cur_led=1'b0;
  42.                         cur_pump=1'b0;
  43.                 end
  44.     else if(update||update!=prev_update) begin
  45.             case(cur_state)
  46.                 off:    begin   if(command==turnon) begin   next_state=standby;
  47.                                                             cur_led=1'b1;
  48.                                                             cur_pump=1'b0;
  49.                                                     end        
  50.                                 else    begin  
  51.                                             next_state=off;
  52.                                             cur_led=1'b0;
  53.                                             cur_pump=1'b0;
  54.                                         end    
  55.                         end
  56.                 standby:begin   if(command==turnoff)begin   next_state=off;
  57.                                                             cur_led=1'b0;
  58.                                                             cur_pump=1'b0;
  59.                                                     end
  60.                                 else if(command==startpump) begin   next_state=working;
  61.                                                                     cur_led=1'b1;
  62.                                                                     cur_pump=1'b1;
  63.                                                             end
  64.                                 else    begin   next_state = standby;
  65.                                                 cur_led=1'b1;
  66.                                                 cur_pump=1'b0;
  67.                                         end
  68.                         end
  69.                 working:begin   if(command==turnoff)begin   next_state=off;
  70.                                                             cur_led=1'b0;
  71.                                                             cur_pump=1'b0;
  72.                                                     end
  73.                                 else if(command==stoppump)begin next_state=standby;
  74.                                                                 cur_led=1'b1;
  75.                                                                 cur_pump=1'b0;
  76.                                                         end    
  77.                                 else begin  next_state = working;
  78.                                             cur_led=1'b1;
  79.                                             cur_pump=1'b1;
  80.                                     end
  81.                         end
  82.                 default:
  83.                 begin
  84.                         next_state=off;
  85.                         cur_led=1'b0;
  86.                         cur_pump=1'b0;
  87.                 end
  88.             endcase
  89.         end
  90.     end
  91.    
  92.    
  93.    
  94.    
  95.    
  96.    
  97.     endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement