-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm3.v
78 lines (67 loc) · 1.53 KB
/
fsm3.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// The block diagram shown above has one input “IN” which is coming serially @
// clock, “CLK”. It has 4 outputs A, B,C & D . A will be 1 if IN has even number of 1’s &
// even number of 0’s. Similarly B will be 1 for even 1’s odd 0’s, C for odd 1’s even 0’s and
// D for both odd. Give the FSM required to design above block.
module fsm(input x, input clk, input rst, output reg A, output reg B, output reg C, output reg D);
reg [1:0] ps,ns;
parameter S0=2'b00,S1=2'b01,S2=2'b10,S3=2'b11;
initial begin ps=S0;ns=S0;end
always@(posedge clk)
begin
if(rst)
ps<=S0;
else
ps<=ns;
end
always @(ps or x)
begin
ns=S0;
case(ps)
S0: begin
A=1;B=0;C=0;D=0;
if(x==1)ns=S1;
else ns=S2;
end
S1: begin
A=0;B=0;C=1;D=0;
if(x==1)ns=S0;
else ns=S3;
end
S2: begin
A=0;B=1;C=0;D=0;
if(x==1)ns=S3;
else ns=S0;
end
S3: begin
A=0;B=0;C=0;D=1;
if(x==1)ns=S2;
else ns=S1;
end
default: begin
ns=S0;
A=0;B=0;C=0;D=0;
end
endcase
end
endmodule
//Testbench
module tb();
reg clk, rst, x;
wire a,b,c,d;
fsm f(x, clk, rst, a, b,c, d);
always #15 clk=~clk;
initial
begin
clk=1'b0;
x=1; #1$strobe($time," ",a,b,c,d);
#30 x=1;#1$strobe($time," ",a,b,c,d);
#30 x=0; #1$strobe($time," ",a,b,c,d);
#30 x=1; #1$strobe($time," ",a,b,c,d);
#30 x=0;#1$strobe($time," ",a,b,c,d);
#30 x=0; #1$strobe($time," ",a,b,c,d);
#30 x=0; #1$strobe($time," ",a,b,c,d);
#30 x=1; #1$strobe($time," ",a,b,c,d);
#30 x=1; #1$strobe($time," ",a,b,c,d);
#30 $strobe($time," ",a,b,c,d);
end
endmodule