summaryrefslogtreecommitdiff
path: root/homework_2/SRC/duv.sv
blob: 62e5b99e0d63f71ac0e6d9aa9a0b8c9aeadfb10c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module duv (
    input  logic clk,
    input  logic reset,
    input  logic inp,
    output logic outp
);
  localparam S0 = 3'b000, S1 = 3'b001, S11 = 3'b010, S10 = 3'b011, S110 = 3'b100, S101 = 3'b101;

  logic [2:0] state;
  logic [2:0] next_state;

  // State register with asynchronous active-high reset
  always_ff @(posedge clk or posedge reset) begin
    if (reset) state <= S0;
    else state <= next_state;
  end

  // Mealy next-state and output logic for 1011 and 1100
  always_comb begin
    next_state = state;
    outp = 1'b0;

    case (state)
      S0: begin
        if (inp) next_state = S1;
        else next_state = S0;
      end

      S1: begin
        if (inp) next_state = S11;
        else next_state = S10;
      end

      S11: begin
        if (inp) next_state = S11;
        else next_state = S110;
      end

      S10: begin
        if (inp) next_state = S101;
        else next_state = S0;
      end

      S110: begin
        if (inp) begin
          next_state = S101;
        end else begin
          next_state = S0;
          outp = 1'b1;  // Detected 1100
        end
      end

      S101: begin
        if (inp) begin
          next_state = S11;
          outp = 1'b1;  // Detected 1011
        end else begin
          next_state = S10;
        end
      end

      default: next_state = S0;
    endcase
  end
endmodule