-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathcl_clk_freq.sv
347 lines (316 loc) · 11.9 KB
/
cl_clk_freq.sv
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// ============================================================================
// Amazon FPGA Hardware Development Kit
//
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Amazon Software License (the "License"). You may not use
// this file except in compliance with the License. A copy of the License is
// located at
//
// http://aws.amazon.com/asl/
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
// implied. See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================
///////////////////////////////////////////////////////////////////////////////////
// Module
// -------
// CL_CLK_FREQ
//
// Description
// -----------
// * Measure Clock Frequency of input clocks
// * Ref clock runs at 100MHz.
// * Registers on CFG bus
///////////////////////////////////////////////////////////////////////////////////
module cl_clk_freq
#(
parameter NUM_OF_CLKS = 9
)
(
input logic i_ref_clk,
input logic i_ref_rst_n,
input logic i_clk_main_a0,
input logic i_rst_main_n,
cfg_bus_t.slave s_cfg_bus, // in clk_main_a0
input logic [NUM_OF_CLKS-1:0] i_test_clks
);
//=================================================================================
// local signals and params
//=================================================================================
`ifdef AWS_SIM
localparam REF_CNT_MAX = 10000;
`else
localparam REF_CNT_MAX = 100000000; //Counter value for 1 second at 100MHz clk
`endif
localparam CTR_WIDTH = 32;
localparam MAX_NUM_CLKS_SUPPORTED = 10;
logic [CTR_WIDTH-1:0] ref_ctr_q = '0;
logic [CTR_WIDTH-1:0] sync_clk_freq_ctr_q [MAX_NUM_CLKS_SUPPORTED-1:0];
logic [CTR_WIDTH-1:0] clk_freq_ctr_q [NUM_OF_CLKS-1:0] = {default: '0};
logic clr_ctr_q = '0;
logic cdc_clr_ctr_q;
logic measure_inp_q = '0;
logic main_measure_done_q;
logic ref_measure_done_q = '0;
logic [NUM_OF_CLKS-1:0] cdc_active_q, cdc_clr_clk_ctr;
logic [CTR_WIDTH-1:0] ref_freq_ctr_reg;
logic [7:0] cfg_addr_q = '0;
logic cfg_wr_q = '0;
logic cfg_rd_q = '0;
logic cfg_wr_pulse_q = '0;
logic cfg_rd_pulse_q = '0;
logic [31:0] cfg_rdata_q = '0;
logic [31:0] cfg_wdata_q = '0;
cfg_bus_t cfg_bus_q();
//=================================================================================
// CSR
//=================================================================================
localparam CTL_REG = 8'h00;
localparam REF_FREQ_CTR = 8'h04;
localparam FREQ_CTR_0 = 8'h10;
localparam FREQ_CTR_1 = 8'h14;
localparam FREQ_CTR_2 = 8'h18;
localparam FREQ_CTR_3 = 8'h1C;
localparam FREQ_CTR_4 = 8'h20;
localparam FREQ_CTR_5 = 8'h24;
localparam FREQ_CTR_6 = 8'h28;
localparam FREQ_CTR_7 = 8'h2C;
localparam FREQ_CTR_8 = 8'h30;
localparam FREQ_CTR_9 = 8'h34;
logic [31:0] ctl_reg = '0;
//===================================================================
// CFG WR/RD reqs
//===================================================================
// Pipeline cfg_bus for better timing
lib_pipe
#(
.WIDTH ($bits(cfg_addr_q) + $bits(cfg_wdata_q) + $bits(cfg_wr_q) + $bits(cfg_rd_q)),
.STAGES (4)
)
PIPE_CFG_REQ_CCT_I
(
.clk (i_clk_main_a0 ),
.rst_n (1'd1 ),
.in_bus ({s_cfg_bus.addr[7:0], s_cfg_bus.wdata, s_cfg_bus.wr, s_cfg_bus.rd}),
.out_bus ({cfg_bus_q.addr[7:0], cfg_bus_q.wdata, cfg_bus_q.wr, cfg_bus_q.rd})
);
// ACK
lib_pipe
#(
.WIDTH ($bits(cfg_rdata_q) + $bits(cfg_rd_q)),
.STAGES (4)
)
PIPE_CFG_ACK_CCT_I
(
.clk (i_clk_main_a0 ),
.rst_n (1'd1 ),
.in_bus ({cfg_bus_q.rdata, cfg_bus_q.ack}),
.out_bus ({s_cfg_bus.rdata, s_cfg_bus.ack})
);
always_ff @(posedge i_clk_main_a0)
if (!i_rst_main_n) begin //{
cfg_wr_q <= '0;
cfg_rd_q <= '0;
end //}
else begin //{
cfg_wr_q <= cfg_bus_q.wr;
cfg_rd_q <= cfg_bus_q.rd;
cfg_addr_q <= cfg_bus_q.addr[0+:$bits(cfg_addr_q)];
cfg_wdata_q <= cfg_bus_q.wdata;
cfg_wr_pulse_q <= ~cfg_wr_q & cfg_bus_q.wr;
cfg_rd_pulse_q <= ~cfg_rd_q & cfg_bus_q.rd;
end //}
always_ff @(posedge i_clk_main_a0)
if (!i_rst_main_n)
cfg_bus_q.ack <= '0;
else
cfg_bus_q.ack <= cfg_wr_pulse_q | cfg_rd_pulse_q;
assign cfg_bus_q.rdata = cfg_rdata_q;
//===================================================================
// Read Datapath
//===================================================================
always_ff @(posedge i_clk_main_a0)
unique case (cfg_addr_q) //{
CTL_REG : cfg_rdata_q <= ctl_reg;
REF_FREQ_CTR : cfg_rdata_q <= 32'(ref_freq_ctr_reg);
FREQ_CTR_0 : cfg_rdata_q <= sync_clk_freq_ctr_q[0];
FREQ_CTR_1 : cfg_rdata_q <= sync_clk_freq_ctr_q[1];
FREQ_CTR_2 : cfg_rdata_q <= sync_clk_freq_ctr_q[2];
FREQ_CTR_3 : cfg_rdata_q <= sync_clk_freq_ctr_q[3];
FREQ_CTR_4 : cfg_rdata_q <= sync_clk_freq_ctr_q[4];
FREQ_CTR_5 : cfg_rdata_q <= sync_clk_freq_ctr_q[5];
FREQ_CTR_6 : cfg_rdata_q <= sync_clk_freq_ctr_q[6];
FREQ_CTR_7 : cfg_rdata_q <= sync_clk_freq_ctr_q[7];
FREQ_CTR_8 : cfg_rdata_q <= sync_clk_freq_ctr_q[8];
FREQ_CTR_9 : cfg_rdata_q <= sync_clk_freq_ctr_q[9];
default : cfg_rdata_q <= 32'hDEAD_DEAD;
endcase // unique case (cfg_addr_q) }
//=================================================================================
// Start/Stop clock measurements
//=================================================================================
//
// Set by SW, Cleared by HW after REF_CNT_MAX cycles
// synchronize "start/clr signal" between i_clk_main_a0 (faster) and i_ref_clk (slower)
//
always_ff @(posedge i_clk_main_a0) begin //{
if (cfg_wr_pulse_q && (cfg_addr_q == CTL_REG))
clr_ctr_q <= cfg_wdata_q[31];
// freq measurement in progress
if (cfg_wr_pulse_q && (cfg_addr_q == CTL_REG) && cfg_wdata_q[0])
measure_inp_q <= '1;
else if (main_measure_done_q || clr_ctr_q)
measure_inp_q <= '0;
end //}
//==================================================================
// CTL_REG
// -------
// bit [31] | RW | 1 = Clear all the counters
// bit [30:1] | RO | 0 = RSVD
// bit [0] | RW | Set by SW to Start frequency measurement
// | Cleared by HW when frequency measurement is done.
//==================================================================
always_comb begin //{
ctl_reg[31] = clr_ctr_q;
ctl_reg[30:1] = '0;
ctl_reg[0] = measure_inp_q;
end //}
// sync "start" into i_ref_clk. This is level signal.
xpm_cdc_single
#(
.DEST_SYNC_FF (4),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0)
)
CDC_START_I
(
.src_clk (i_clk_main_a0 ),
.src_in (measure_inp_q ),
.dest_clk (i_ref_clk ),
.dest_out (cdc_measure_inp_q )
);
// sync clr into i_ref_clk. This is level signal.
xpm_cdc_single
#(
.DEST_SYNC_FF (4),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0)
)
CDC_CLR_I
(
.src_clk (i_clk_main_a0 ),
.src_in (clr_ctr_q ),
.dest_clk (i_ref_clk ),
.dest_out (cdc_clr_ctr_q )
);
always_ff @(posedge i_ref_clk) begin : CLK_MSRE_INP_I //{
// Reference counter
if (cdc_clr_ctr_q)
ref_ctr_q <= '0;
else if (cdc_measure_inp_q && (ref_ctr_q != REF_CNT_MAX))
ref_ctr_q <= ref_ctr_q + 1;
ref_measure_done_q <= (ref_ctr_q == REF_CNT_MAX);
end : CLK_MSRE_INP_I //}
// sync active_q back into clk_main_a0 for CSRs
xpm_cdc_single
#(
.DEST_SYNC_FF (4),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0)
)
CDC_MAIN_ACTV_I
(
.src_clk (i_ref_clk ),
.src_in (ref_measure_done_q ),
.dest_clk (i_clk_main_a0 ),
.dest_out (main_measure_done_q )
);
//
// Sync ref_ctr_q into clk_main_a0 for REF_FREQ_CTR reg
//
xpm_cdc_array_single
#(
.DEST_SYNC_FF (2),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0),
.WIDTH (CTR_WIDTH)
)
CDC_CLK_CTR_I
(
.src_clk (i_ref_clk ),
.src_in (ref_ctr_q ),
.dest_clk (i_clk_main_a0 ),
.dest_out (ref_freq_ctr_reg )
);
//=================================================================================
// Frequency Measurement Logic for all the input test clocks.
// CDC for active signals and clock frequency counters in all other clock domains.
//=================================================================================
generate //{
for (genvar gg = 0; gg < NUM_OF_CLKS; gg++) begin : FOR_CDC_ACTV //{
// sync "clr" into all test clocks domain
xpm_cdc_single
#(
.DEST_SYNC_FF (4),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0)
)
CDC_SYNC_CLR_I
(
.src_clk (i_ref_clk ),
.src_in (cdc_clr_ctr_q ),
.dest_clk (i_test_clks[gg] ),
.dest_out (cdc_clr_clk_ctr[gg] )
);
// Sync "active" into all the test clks domain
xpm_cdc_single
#(
.DEST_SYNC_FF (4),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0)
)
CDC_SYNC_ACTV_I
(
.src_clk (i_ref_clk ),
.src_in (cdc_measure_inp_q ),
.dest_clk (i_test_clks[gg] ),
.dest_out (cdc_active_q[gg] )
);
//
// Clock counters in each i_test_clks domain
//
always_ff @(posedge i_test_clks[gg])
if (cdc_clr_clk_ctr[gg])
clk_freq_ctr_q[gg] <= '0;
else if (cdc_active_q[gg] && (clk_freq_ctr_q[gg] != '1))
clk_freq_ctr_q[gg] <= clk_freq_ctr_q[gg] + 1;
//
// Sync back clock counters into i_clk_main_a0 domain
// We read back this value only when active_q = 0. So simple multi-bit synchronizer is sufficient.
//
xpm_cdc_array_single
#(
.DEST_SYNC_FF (2),
.INIT_SYNC_FF (0),
.SIM_ASSERT_CHK (0),
.SRC_INPUT_REG (0),
.WIDTH (CTR_WIDTH)
)
CDC_CLK_CTR_I
(
.src_clk (i_test_clks[gg] ),
.src_in (clk_freq_ctr_q[gg] ),
.dest_clk (i_clk_main_a0 ),
.dest_out (sync_clk_freq_ctr_q[gg] )
);
end : FOR_CDC_ACTV //}
endgenerate //}
endmodule // cl_clk_freq