Skip to content

Commit 7a9072a

Browse files
authored
Add files via upload
1 parent 9ded5e3 commit 7a9072a

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

pattern.v

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
module pattern(clk, rst, valid, in, out);
2+
parameter RST=3'b000,
3+
S1=3'b001,
4+
S10=3'b010,
5+
S101=3'b011,
6+
S1011=3'b100,
7+
S10110=3'b101;
8+
9+
input clk, rst, valid, in;
10+
output reg out;
11+
12+
reg [2 : 0] cs, ns;
13+
14+
15+
// Here cs and ns are same values as they move together
16+
/*always @ (posedge clk) begin
17+
if (rst == 1) begin
18+
out = 0;
19+
cs = RST;
20+
ns = RST;
21+
end
22+
else begin
23+
if (valid == 1) begin
24+
out = 0;
25+
case (cs)
26+
RST: begin
27+
if (in == 1) ns = S1;
28+
else ns = RST;
29+
end
30+
S1: begin
31+
if (in == 1) ns = S1;
32+
else ns = S10;
33+
end
34+
S10: begin
35+
if (in == 1) ns = S101;
36+
else ns = RST;
37+
end
38+
S101: begin
39+
if (in == 1) ns = S1011;
40+
else ns = S10;
41+
end
42+
S1011: begin
43+
if (in == 1) ns = S1;
44+
// for Moore
45+
//else ns = S10110;
46+
// for Melay
47+
else begin
48+
ns = RST;
49+
out = 1;
50+
end
51+
end
52+
// remove this state for Melay
53+
/*S10110: begin
54+
out = 1;
55+
if (in == 1) ns = S1;
56+
else ns = RST;
57+
end
58+
default: begin
59+
ns = RST;
60+
out = 0;
61+
end
62+
endcase
63+
end
64+
else begin
65+
out = 0;
66+
end
67+
end
68+
end
69+
70+
always @ (ns) cs = ns;
71+
*/
72+
73+
// The above code works same as below without even using ns
74+
always @ (posedge clk) begin
75+
if (rst == 1) begin
76+
out = 0;
77+
cs = RST;
78+
end
79+
else begin
80+
if (valid == 1) begin
81+
out = 0;
82+
case (cs)
83+
RST: begin
84+
if (in == 1) cs = S1;
85+
else cs = RST;
86+
end
87+
S1: begin
88+
if (in == 1) cs = S1;
89+
else cs = S10;
90+
end
91+
S10: begin
92+
if (in == 1) cs = S101;
93+
else cs = RST;
94+
end
95+
S101: begin
96+
if (in == 1) cs = S1011;
97+
else cs = S10;
98+
end
99+
S1011: begin
100+
if (in == 1) cs = S1;
101+
// for Moore
102+
//else cs = S10110;
103+
// for Melay
104+
else begin
105+
cs = RST;
106+
out = 1;
107+
end
108+
end
109+
// remove this state for Melay
110+
/*S10110: begin
111+
out = 1;
112+
if (in == 1) cs = S1;
113+
else cs = RST;
114+
end*/
115+
default: begin
116+
cs = RST;
117+
out = 0;
118+
end
119+
endcase
120+
end
121+
else begin
122+
out = 0;
123+
end
124+
end
125+
end
126+
127+
endmodule
128+

pattern1.v

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
module pattern(clk, rst, valid, in, out);
2+
parameter RST=3'b000,
3+
S1=3'b001,
4+
S10=3'b010,
5+
S101=3'b011,
6+
S1011=3'b100,
7+
S10110=3'b101;
8+
9+
input clk, rst, valid, in;
10+
output out;
11+
12+
reg [2 : 0] cs, ns;
13+
14+
// For Moore
15+
//assign out = (cs == S10110) ? 1 : 0;
16+
//OR
17+
/*reg out;
18+
always @ (cs) begin
19+
case (cs)
20+
RST: out = 0;
21+
S1: out = 0;
22+
S10: out = 0;
23+
S101: out = 0;
24+
S1011: out = 0;
25+
S10110: out = 1;
26+
endcase
27+
end*/
28+
29+
always @ (posedge clk) begin
30+
if (rst == 1) cs <= RST;
31+
else cs <= ns;
32+
end
33+
34+
// For Melay
35+
// the below assign statements will cause spikes of out on unwanted situations
36+
// also output will be observed on same cycle
37+
//assign out = (cs==S1011 && in==0) ? 1 : 0;
38+
// OR
39+
// This works best and output will be on next cycle like normal
40+
// also this will have no unwanted spikes of out
41+
reg out;
42+
always @ (posedge clk) begin // do not keep cs, in in sensitivity list
43+
// that will also result in spikes before posedge of clk and output will be on same cycle
44+
if (cs==S1011 && in==0) out <= 1;
45+
else out <= 0;
46+
end
47+
// Best for Melay combined
48+
/*always @ (posedge clk) begin
49+
if (rst == 1) begin
50+
cs <= RST;
51+
out <= 0;
52+
end
53+
else begin
54+
cs <= ns;
55+
out <= (cs==S1011 && in==0) ? 1 : 0;
56+
end
57+
end
58+
*/
59+
always @ (cs, in) begin
60+
if (valid == 1) begin
61+
case (cs)
62+
RST: begin
63+
if (in == 1) ns = S1;
64+
else ns = RST;
65+
end
66+
S1: begin
67+
if (in == 1) ns = S1;
68+
else ns = S10;
69+
end
70+
S10: begin
71+
if (in == 1) ns = S101;
72+
else ns = RST;
73+
end
74+
S101: begin
75+
if (in == 1) ns = S1011;
76+
else ns = S10;
77+
end
78+
S1011: begin
79+
if (in == 1) ns = S1;
80+
// For Moore
81+
//else ns = S10110;
82+
// For Melay
83+
else ns = RST;
84+
end
85+
// Remove for Melay
86+
/*S10110: begin
87+
if (in == 1) ns = S1;
88+
else ns = RST;
89+
end*/
90+
default: begin
91+
ns = RST;
92+
end
93+
endcase
94+
end
95+
else begin
96+
ns = RST;
97+
end
98+
end
99+
100+
endmodule

run.do

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib work
2+
vlog tb_pattern.v
3+
vsim tb
4+
add wave sim:/tb/dut/*
5+
run -all

tb_pattern.v

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//`include "pattern.v"
2+
`include "pattern1.v"
3+
4+
module tb;
5+
6+
reg clk, rst, valid, in;
7+
wire out;
8+
9+
pattern dut (.clk(clk), .rst(rst), .valid(valid), .in(in), .out(out));
10+
11+
always #1 clk = ~clk;
12+
13+
initial begin
14+
clk = 0;
15+
rst = 1;
16+
valid = 0;
17+
in = 0;
18+
repeat (5) @ (posedge clk);
19+
rst = 0;
20+
21+
valid = 1;
22+
repeat (400) @ (posedge clk) begin
23+
in = $random;
24+
end
25+
26+
27+
$finish;
28+
end
29+
30+
endmodule

0 commit comments

Comments
 (0)