-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2014_q3fsm.v
53 lines (44 loc) · 1.07 KB
/
2014_q3fsm.v
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
//https://hdlbits.01xz.net/wiki/Exams/2014_q3fsm
module top_module (
input clk,
input reset, // Synchronous reset
input s,
input w,
output z
);
parameter A = 2'b01, B = 2'b10;
reg [1:0] state;
reg [1:0] next_state;
reg [1:0] mem;
reg [1:0] counter;
always @(*) begin
case (state)
A: next_state = (s)? B:A;
B: next_state = B;
default:state=A;
endcase
end
always @(posedge clk ) begin
if(reset)
state <= A;
else
state <= next_state;
end
always @(posedge clk ) begin
if(reset)
mem <= 2'd0;
else if(counter==2'd0)
mem <= w? 2'd1:2'd0;
else if (state==B)
mem <= w? (mem+1'b1):mem;
end
always @(posedge clk ) begin
if(reset)
counter <= 2'd0;
else if(counter==2'd2 )
counter <= 2'd0;
else if(state == B)
counter <= counter + 1'b1;
end
assign z = (state==B && counter ==2'd0 && mem == 2'd2);
endmodule