aboutsummaryrefslogtreecommitdiffstats
path: root/example/recursive_moore.sv
blob: e940ff6ccf3b54ed048ec4759798098c87d3586f (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// moore_recursive_template.v

module moore_recursive_template
#( parameter
		param1 : <value>,
		param2 : <value>
)
(
	input wire clk, reset,
	input wire [<size>] input1, input2, ...,
	output reg [<size>] output1, output2, new_output1, new_output2
);

localparam [<size_state>] // for 4 states : size_state = 1:0
    s0 = 0,
    s1 = 1,
    s2 = 2,
    ... ;

    reg[<size_state>] state_reg, state_next;

// timer
localparam T1 = <value>;
localparam T2 = <value>;
localparam T3 = <value>;
...
reg [<size>] t; //<size> should be able to store max(T1, T2, T3)

// recursive : feedback register
reg [<size>] r1_reg, r1_next;
reg [<size>] r2_reg, r2_next, ;
...


// state register : state_reg
// This always-block contains sequential part & all the D-FF are
// included in this always-block. Hence, only 'clk' and 'reset' are
// required for this always-block.
always(posedge clk, posedge reset)
begin
    if (reset) begin
        state_reg <= s1;
    end
    else begin
        state_reg <= state_next;
    end
end

// timer (optional)
always @(posedge clk, posedge reset) begin
    if (reset) begin
        t <= 0;
    end
    else begin
        if state_reg != state_next then  // state is changing
            t <= 0;
        else
            t <= t + 1;
    end
end

// feedback registers: to feedback the outputs
always @(posedge clk, posedge reset)
begin
    if (reset) begin
        r1_reg <= <initial_value>;
        r2_reg <= <initial_value>;
        ...
    end
    else begin
        r1_reg <= r1_next;
        r2_reg <= r2_next;
        ...
    end
end



//  next state logic : state_next
//  This is combinational of the sequential design,
//  which contains the logic for next-state
//  include all signals and input in sensitive-list except state_next
always @(input1, input2, state_reg) begin
    state_next = state_reg; //  default state_next
    r1_next = r1_reg; //  default next-states
    r2_next = r2_reg;
    ...
    case (state_reg)
        s0 : begin
            if <condition> & r1_reg == <value> & t >= T1-1 begin //  if (input1 = '01') then
                state_next = s1;
                r1_next = <value>;
                r2_next = <value>;
                ...
            end
            elsif <condition> & r2_reg == <value> & t >= T2-1 begin  //  add all the required conditions
                state_next = <value>;
                r1_next = <value>;
                ...
            end
            else begin //  remain in current state
                state_next = s0;
                r2_next = <value>;
                ...
            end
        end
        s1 : begin
            ...
    end case;
end process;

// next state logic and outputs
// This is combinational of the sequential design,
// which contains the logic for next-state and outputs
// include all signals and input in sensitive-list except state_next
always @(input1, input2, ..., state_reg) begin
    state_next = state_reg; // default state_next
    // default outputs
    output1 = <value>;
    output2 = <value>;
    ...
    // default next-states
    r1_next = r1_reg;
    r2_next = r2_reg;
    ...
    case (state_reg)
        s0 : begin
            output1 = <value>;
            output2 = <value>;
            if (<condition> & r1_reg = <value> & t >= T1-1) begin // if (input1 = 2'b01) then
                ...
                r1_next = <value>;
                r2_next = <value>;
                ...
                state_next = s1;
            end
            else if (<condition> & r2_reg = <value> & t >= T2-1) begin // add all the required coditions
                r1_next = <value>;
                ...
                state_next = ...;
            end
            else begin // remain in current state
                r2_next = <value>;
                ...
                state_next = s0;
            end
        end
        s1 : begin
            output1 = <value>;
            output2 = <value>;
            if (<condition> & r1_reg = <value> & t >= T3-1) begin // if (input1 = 2'b01) then
                ...
                r1_next = <value>;
                r2_next = <value>;
                ...
                state_next = s1;
            end
            else if (<condition> & r2_reg = <value> & t >= T1-1) begin // add all the required coditions
                r1_next = <value>;
                ...
                state_next = ...;
            end
            else begin // remain in current state
                r2_next = <value>;
                ...
                state_next = s1;
            end
        end
    endcase
end

// optional D-FF to remove glitches
always @(posedge clk, posedge reset)
begin
    if (reset) begin
        new_output1 <= ... ;
        new_output2 <= ... ;
    end
    else begin
        new_output1 <= output1;
        new_output2 <= output2;
    end
end
endmodule