Skip to content

Commit db681a4

Browse files
committed
Pattern is being detected in any bit position now
1 parent fd38354 commit db681a4

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

pattern_detect.sv

+40-13
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,80 @@
55
//------------------------------------------------------------------------------
66

77
// INFO ------------------------------------------------------------------------
8-
// Detects data pattern specified by the provided PATTERN
8+
// Detects data pattern specified by the provided PAT parameter
99
//
1010
// Features capturing WIDTH bits simultaneously in case your data
11-
// comes in parallel, like in QSPI interface, for example.
11+
// comes in parallel, like in QSPI interface, for example
12+
//
13+
// Detects pattern in any possible bit position, supposing that input data
14+
// is an unaligned bit stream
1215
//
1316

1417
/* --- INSTANTIATION TEMPLATE BEGIN ---
1518
1619
pattern_detect #(
1720
.DEPTH( 2 ),
18-
.WIDTH( 5 ),
19-
.PATTERN( 10'b11111_10011 )
21+
.WIDTH( 16 ),
22+
23+
// pattern parameters
24+
.PAT_WIDTH( 5 ), // must be less than DEPTH*WIDTH
25+
.PAT( 5'b10011 )
2026
) PD1 (
2127
.clk( clk ),
2228
.nrst( nrst ),
2329
.ena( 1'b1 ),
2430
.data( data[4:0] ),
31+
32+
.detected_pos( )
2533
.detected( )
2634
);
2735
2836
--- INSTANTIATION TEMPLATE END ---*/
2937

3038

3139
module pattern_detect #( parameter
32-
DEPTH = 1,
33-
WIDTH = 1,
34-
logic [DEPTH*WIDTH-1:0] PATTERN = '0
40+
DEPTH = 2,
41+
WIDTH = 16,
42+
43+
PAT_WIDTH = 5, // must be less than DEPTH*WIDTH
44+
bit [PAT_WIDTH-1:0] PAT = '1
3545
)(
3646
input clk,
3747
input nrst,
3848

3949
input ena,
4050
input [WIDTH-1:0] data,
4151

42-
output detected
52+
output logic detected,
53+
output logic [DEPTH*WIDTH-1:0] detected_mask
4354
);
4455

45-
logic [DEPTH*WIDTH-1:0] samples = '0;
56+
logic [DEPTH*WIDTH-1:0] sample_buf = '0;
57+
logic [DEPTH*WIDTH-1:0] ena_buf = '0;
4658
always @ (posedge clk) begin
4759
if( ~nrst ) begin
48-
samples[DEPTH*WIDTH-1:0] <= '0;
49-
end else if( ena ) begin
50-
samples[DEPTH*WIDTH-1:0] <= {samples[DEPTH*WIDTH-WIDTH-1:0],data[WIDTH-1:0]};
60+
sample_buf[DEPTH*WIDTH-1:0] <= {DEPTH*WIDTH{1'b0}};
61+
ena_buf[DEPTH*WIDTH-1:0] <= {DEPTH*WIDTH{1'b0}};
62+
end else begin
63+
sample_buf[DEPTH*WIDTH-1:0] <= {sample_buf[DEPTH*WIDTH-WIDTH-1:0],
64+
data[WIDTH-1:0]};
65+
ena_buf[DEPTH*WIDTH-1:0] <= {ena_buf[DEPTH*WIDTH-WIDTH-1:0],
66+
{WIDTH{ena}} };
5167
end
5268
end
5369

54-
assign detected = (samples[DEPTH*WIDTH-1:0] == PATTERN[DEPTH*WIDTH-1:0]);
70+
always_comb begin
71+
integer i;
72+
73+
detected_mask[DEPTH*WIDTH-1:0] = '0;
74+
for( i=0; i<(DEPTH*WIDTH-PAT_WIDTH); i++ ) begin
75+
if( sample_buf[i+:PAT_WIDTH] == PAT[PAT_WIDTH-1:0] &&
76+
ena_buf[i+:PAT_WIDTH]) begin
77+
detected_mask[i] = 1'b1;
78+
end
79+
end
80+
detected = |detected_mask[DEPTH*WIDTH-1:0];
81+
end
5582

5683
endmodule
5784

pattern_detect_tb.sv

+8-2
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,22 @@ end
103103
// Module under test ===========================================================
104104

105105
logic [15:0] detected;
106+
logic [15:0][9:0] detected_mask;
107+
106108
pattern_detect #(
107109
.DEPTH( 5 ),
108110
.WIDTH( 2 ),
109-
.PATTERN( 10'b10_01_11_00_11 )
111+
112+
.PAT_WIDTH( 7 ),
113+
.PAT( 7'b1_11_00_11 )
110114
) pd [15:0] (
111115
.clk( {16{clk200}} ),
112116
.nrst( {16{nrst_once}} ),
113117
.ena( '1 ),
114118
.data( rnd_data[31:0] ),
115-
.detected( detected[15:0] )
119+
120+
.detected( detected[15:0] ),
121+
.detected_mask( detected_mask )
116122
);
117123

118124

0 commit comments

Comments
 (0)