-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathef_util_lib.v
393 lines (337 loc) · 9.2 KB
/
ef_util_lib.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
`ifdef PRINT_LICENSE
/*
Copyright 2024 Efabless Corp
Author: Efabless Corp ([email protected] )
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
`endif // PRINT_LICENSE
`timescale 1ns/1ps
`default_nettype none
`define PED(clk, sig, pulse) reg last_``sig``; always @(posedge clk) last_``sig`` <= sig; assign pulse = sig & ~last_``sig``;
`define NED(clk, sig, pulse) reg last_``sig``; always @(posedge clk) last_``sig`` <= sig; assign pulse = ~sig & last_``sig``;
/*
Brute-force Synchronizer
*/
module ef_util_sync #(parameter NUM_STAGES = 2) (
input wire clk,
input wire in,
output wire out
);
reg [NUM_STAGES-1:0] sync;
always @(posedge clk)
sync <= {sync[NUM_STAGES-2:0], in};
assign out = sync[NUM_STAGES-1];
endmodule
/*
A positive edge detector
*/
module ef_util_ped (
input wire clk,
input wire in,
output wire out
);
reg last_in;
always @(posedge clk)
last_in <= in;
assign out = in & ~last_in;
endmodule
/*
A negative edge detector
*/
module ef_util_ned (
input wire clk,
input wire in,
output wire out
);
reg last_in;
always @(posedge clk)
last_in <= in;
assign out = ~in & last_in;
endmodule
/*
A tick generator
*/
module ef_util_ticker #(parameter W=8) (
input wire clk,
input wire rst_n,
input wire en,
input wire [W-1:0] clk_div,
output wire tick
);
reg [W-1:0] counter;
wire counter_is_zero = (counter == 'b0);
wire tick_w;
reg tick_reg;
always @(posedge clk, negedge rst_n)
if(~rst_n)
counter <= 'b0;
else if(en)
if(counter_is_zero)
counter <= clk_div;
else
counter <= counter - 'b1;
assign tick_w = (clk_div == 'b0) ? 1'b1 : counter_is_zero;
always @(posedge clk or negedge rst_n)
if(!rst_n)
tick_reg <= 1'b0;
else if(en)
tick_reg <= tick_w;
else
tick_reg <= 0;
assign tick = tick_reg;
endmodule
/*
A glitch filter
*/
module ef_util_glitch_filter #(parameter N = 8, CLKDIV = 8'd1) (
input wire clk,
input wire rst_n,
input wire in,
input wire en,
output reg out
);
reg [N-1:0] shifter;
wire tick;
ef_util_ticker ticker (
.clk(clk),
.rst_n(rst_n),
.en(en),
.clk_div(CLKDIV),
.tick(tick)
);
always @(posedge clk, negedge rst_n)
if(!rst_n)
shifter <= 'b0;
else if(tick)
shifter <= {shifter[N-2:0], in};
wire all_ones = & shifter;
wire all_zeros = ~| shifter;
always @(posedge clk, negedge rst_n)
if(!rst_n)
out <= 1'b0;
else
if(all_ones)
out <= 1'b1;
else if(all_zeros)
out <= 1'b0;
endmodule
/*
A FIFO
Depth = 2^AW
Width = DW
*/
module ef_util_fifo #(parameter DW=8, AW=4)(
input wire clk,
input wire rst_n,
input wire rd,
input wire wr,
input wire flush,
input wire [DW-1:0] wdata,
output wire empty,
output wire full,
output wire [DW-1:0] rdata,
output wire [AW-1:0] level
);
localparam DEPTH = 2**AW;
//Internal Signal declarations
reg [DW-1:0] array_reg [DEPTH-1:0];
reg [AW-1:0] w_ptr_reg;
reg [AW-1:0] w_ptr_next;
reg [AW-1:0] w_ptr_succ;
reg [AW-1:0] r_ptr_reg;
reg [AW-1:0] r_ptr_next;
reg [AW-1:0] r_ptr_succ;
// Level
reg [AW-1:0] level_reg;
reg [AW-1:0] level_next;
reg full_reg;
reg empty_reg;
reg full_next;
reg empty_next;
wire w_en;
always @ (posedge clk)
if(w_en) begin
array_reg[w_ptr_reg] <= wdata;
end
assign rdata = array_reg[r_ptr_reg];
assign w_en = wr & ~full_reg;
//State Machine
always @ (posedge clk, negedge rst_n) begin
if(!rst_n)
begin
w_ptr_reg <= 'b0;
r_ptr_reg <= 'b0;
full_reg <= 1'b0;
empty_reg <= 1'b1;
level_reg <= 'd0;
end
else if(flush)
begin
w_ptr_reg <= 'b0;
r_ptr_reg <= 'b0;
full_reg <= 1'b0;
empty_reg <= 1'b1;
level_reg <= 'd0;
end
else
begin
w_ptr_reg <= w_ptr_next;
r_ptr_reg <= r_ptr_next;
full_reg <= full_next;
empty_reg <= empty_next;
level_reg <= level_next;
end
end
//Next State Logic
always @* begin
w_ptr_succ = w_ptr_reg + 1;
r_ptr_succ = r_ptr_reg + 1;
w_ptr_next = w_ptr_reg;
r_ptr_next = r_ptr_reg;
full_next = full_reg;
empty_next = empty_reg;
level_next = level_reg;
case({w_en,rd})
//2'b00: nop
2'b01:
if(~empty_reg) begin
r_ptr_next = r_ptr_succ;
full_next = 1'b0;
level_next = level_reg - 1;
if (r_ptr_succ == w_ptr_reg)
empty_next = 1'b1;
end
2'b10:
if(~full_reg) begin
w_ptr_next = w_ptr_succ;
empty_next = 1'b0;
level_next = level_reg + 1;
if (w_ptr_succ == r_ptr_reg)
full_next = 1'b1;
end
2'b11: begin
w_ptr_next = w_ptr_succ;
r_ptr_next = r_ptr_succ;
end
endcase
end
//Set Full and Empty
assign full = full_reg;
assign empty = empty_reg;
assign level = level_reg;
endmodule
/*
Glitch-free 2x1 Clock Multiplexor
based on the following old EDN article:
https://www.edn.com/techniques-to-make-clock-switching-glitch-free/
*/
module ef_util_clkmux_2x1 (
input wire rst_n,
input wire clk0,
input wire clk1,
input wire sel,
output wire clko
);
reg Q1a, Q1b, Q2a, Q2b;
wire q1a_in, q2a_in;
assign clko = (clk0 & Q1b) | (clk1 & Q2b);
wire Q2b_bar = ~Q2b;
wire Q1b_bar = ~Q1b;
wire sel_bar = ~sel;
assign q1a_in = Q2b_bar & sel_bar;
assign q2a_in = Q1b_bar & sel;
always @(posedge clk0 or negedge rst_n)
if (~rst_n)
Q1a <= 1'b0;
else
Q1a <= q1a_in;
always @(negedge clk0 or negedge rst_n)
if (~rst_n)
Q1b <= 1'b0;
else
Q1b <= Q1a;
always @(posedge clk1 or negedge rst_n)
if (~rst_n)
Q2a <= 1'b0;
else
Q2a <= q2a_in;
always @(negedge clk1 or negedge rst_n)
if (~rst_n)
Q2b <= 1'b0;
else
Q2b <= Q2a;
endmodule
/*
Glitch-free 4x1 Clock Multiplexor
*/
module ef_util_clkmux_4x1 (
input wire rst_n,
input wire clk0,
input wire clk1,
input wire clk2,
input wire clk3,
input wire [1:0] sel,
output wire clko
);
wire clko1, clko2;
ef_util_clkmux_2x1 m1(
.rst_n(rst_n),
.clk0(clk0),
.clk1(clk1),
.clko(clko1),
.sel(sel[0])
);
ef_util_clkmux_2x1 m2(
.rst_n(rst_n),
.clk0(clk2),
.clk1(clk3),
.clko(clko2),
.sel(sel[0])
);
ef_util_clkmux_2x1 m3(
.rst_n(rst_n),
.clk0(clko1),
.clk1(clko2),
.clko(clko),
.sel(sel[1])
);
endmodule
/*
Clocking Gating Cell
*/
module ef_util_gating_cell(
`ifdef USE_POWER_PINS
input wire vpwr,
input wire vgnd,
`endif // USE_POWER_PINS
input wire clk,
input wire rst_n,
input wire clk_en,
output wire clk_o
);
`ifdef CLKG_SKY130_HD
(* keep *) sky130_fd_sc_hd__dlclkp_4 clk_gate(
`ifdef USE_POWER_PINS
.VPWR(vpwr),
.VGND(vgnd),
.VNB(vpwr),
.VPB(vgnd),
`endif // CLKG_SKY130_HD
.GCLK(clk_o),
.GATE(clk_en),
.CLK(clk)
);
`elsif CLKG_GENERIC
assign clk_o = clk & clk_en;
`else
assign clk_o = clk;
`endif // CLKG_SKY130_HD
endmodule