Advertisement
STANAANDREY

patt

Nov 2nd, 2023
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module patt(
  2.     input clk, rst,
  3.     input x,
  4.     output reg o
  5. );
  6.     localparam S0 = 0, S1 = 1, S2 = 2, S3 = 3, S4 = 4;
  7.     reg st, st_nxt;
  8.     always @(*)
  9.         case (st)
  10.         S0:
  11.             if (x) st_nxt = S1;
  12.             else st_nxt = S0;
  13.         S1:
  14.             if (!x) st_nxt = S2;
  15.             else st_nxt = S1;
  16.         S2:
  17.             if (x) st_nxt = S3;
  18.             else st_nxt = S0;
  19.         S3:
  20.             if (x) st_nxt = S4;
  21.             else st_nxt = S2;
  22.         S4:
  23.             if (x) st_nxt = S1;
  24.             else st_nxt = S2;
  25.         endcase
  26.     always @(*) begin
  27.         o = 0;
  28.         case (st)
  29.         S3: if (x) o = 1;
  30.         endcase
  31.     end
  32.     always @(posedge clk, negedge rst)
  33.         if (!rst) st <= S0;
  34.         else st <= st_nxt;
  35. endmodule
  36.  
  37.  
  38. module patt_tb;
  39.     reg clk, rst, x;
  40.     wire o;
  41.     patt inst(.clk(clk), .rst(rst), .x(x));
  42.     localparam CLK_PERIOD = 100, RST_PULSE = 10, CLK_CYCLES = 5;
  43.     initial begin
  44.         clk = 0;
  45.         repeat (2*CLK_CYCLES)
  46.             #(CLK_PERIOD/2) clk=~clk;
  47.     end
  48.     initial begin
  49.         rst = 0;
  50.         #(RST_PULSE) rst = 1;
  51.     end
  52.     initial begin
  53.         x=0;
  54.         #(1*CLK_PERIOD) x=0;
  55.         #(1*CLK_PERIOD) x=1;
  56.         #(1*CLK_PERIOD) x=1;
  57.     end
  58. endmodule
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement