regzarr

fsm_3s

Oct 24th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module fsm_3s(
  2.   input a, b, clk, rst_b,
  3.   output reg m, n, p
  4. );
  5.  
  6. localparam s0 = 2'b00;
  7. localparam s1 = 2'b01;
  8. localparam s2 = 2'b10;
  9. reg [1:0] st, st_next;
  10.  
  11. always @ (*) begin
  12.   st_next = s0;
  13.   case(st)
  14.     s0: begin
  15.       if (!a)
  16.         st_next = s0;
  17.       else
  18.       if (a && !b)
  19.         st_next = s1;
  20.       else
  21.         if (a && b)
  22.           st_next = s2;
  23.         end
  24.     s1: st_next = s1;
  25.     s2: begin
  26.       if (!a)
  27.         st_next = s2;
  28.       else
  29.       if (a)
  30.         st_next = s0;
  31.         end
  32.   endcase
  33. end
  34.  
  35. always @ (*) begin
  36.   m = 1'd0;
  37.   n = 1'd1;
  38.   p = 1'd0;
  39.  
  40.   case(st)
  41.     s0: begin
  42.       if (!a)
  43.         m = 1'd1;
  44.       else
  45.       if (a && b)
  46.         p = 1'd1;
  47.       else
  48.       if (a && !b)
  49.         n = 1'd0;
  50.         end
  51.     s1: begin
  52.       p = 1'd1;
  53.       m = 1'd1;
  54.     end
  55.     s2: begin
  56.       if (!a)
  57.         begin
  58.           m = 1'd1;
  59.           n = 1'd0;
  60.         end
  61.       else
  62.         n = 1'd0;
  63.       end
  64.   endcase
  65. end
  66.  
  67. always @ (posedge clk, negedge rst_b) begin
  68.   if (!rst_b)
  69.     st <= s0;
  70.   else
  71.     st <= st_next;
  72. end
  73.  
  74. endmodule
Add Comment
Please, Sign In to add comment