OpenCores
URL https://opencores.org/ocsvn/versatile_library/versatile_library/trunk

Subversion Repositories versatile_library

[/] [versatile_library/] [trunk/] [rtl/] [verilog/] [versatile_library.v] - Blame information for rev 34

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Versatile library, clock and reset                          ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  Logic related to clock and reset                            ////
7
////                                                              ////
8
////                                                              ////
9
////  To Do:                                                      ////
10
////   - add more different registers                             ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - Michael Unneback, unneback@opencores.org              ////
14
////        ORSoC AB                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
 
43
// Global buffer
44
// usage:
45
// use to enable global buffers for high fan out signals such as clock and reset
46
 
47
`ifdef ACTEL
48
 
49
`timescale 1 ns/100 ps
50
// Version: 8.4 8.4.0.33
51
module gbuf(GL,CLK);
52
output GL;
53
input  CLK;
54
 
55
    wire GND;
56
 
57
    GND GND_1_net(.Y(GND));
58
    CLKDLY Inst1(.CLK(CLK), .GL(GL), .DLYGL0(GND), .DLYGL1(GND),
59
        .DLYGL2(GND), .DLYGL3(GND), .DLYGL4(GND)) /* synthesis black_box */;
60
 
61
endmodule
62
`timescale 1 ns/1 ns
63
module vl_gbuf ( i, o);
64
input i;
65
output o;
66
`ifdef SIM_GBUF
67
assign o=i;
68
`else
69
gbuf gbuf_i0 ( .CLK(i), .GL(o));
70
`endif
71
endmodule
72 33 unneback
 
73 6 unneback
`else
74 33 unneback
 
75 6 unneback
`ifdef ALTERA
76 21 unneback
//altera
77 33 unneback
module vl_gbuf ( i, o);
78
input i;
79
output o;
80
assign o = i;
81
endmodule
82
 
83 6 unneback
`else
84
 
85
`timescale 1 ns/100 ps
86
module vl_gbuf ( i, o);
87
input i;
88
output o;
89
assign o = i;
90
endmodule
91
`endif // ALTERA
92
`endif //ACTEL
93
 
94
// sync reset
95 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
96 6 unneback
// output active high global reset sync with two DFFs 
97
`timescale 1 ns/100 ps
98
module vl_sync_rst ( rst_n_i, rst_o, clk);
99
input rst_n_i, clk;
100
output rst_o;
101 18 unneback
reg [1:0] tmp;
102 6 unneback
always @ (posedge clk or negedge rst_n_i)
103
if (!rst_n_i)
104 17 unneback
        tmp <= 2'b11;
105 6 unneback
else
106 33 unneback
        tmp <= {1'b0,tmp[1]};
107 17 unneback
vl_gbuf buf_i0( .i(tmp[0]), .o(rst_o));
108 6 unneback
endmodule
109
 
110
// vl_pll
111
`ifdef ACTEL
112 32 unneback
///////////////////////////////////////////////////////////////////////////////
113 17 unneback
`timescale 1 ps/1 ps
114 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
115
parameter index = 0;
116
parameter number_of_clk = 1;
117 17 unneback
parameter period_time_0 = 20000;
118
parameter period_time_1 = 20000;
119
parameter period_time_2 = 20000;
120
parameter lock_delay = 2000000;
121 6 unneback
input clk_i, rst_n_i;
122
output lock;
123
output reg [0:number_of_clk-1] clk_o;
124
output [0:number_of_clk-1] rst_o;
125
 
126
`ifdef SIM_PLL
127
 
128
always
129
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
130
 
131
generate if (number_of_clk > 1)
132
always
133
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
134
endgenerate
135
 
136
generate if (number_of_clk > 2)
137
always
138
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
139
endgenerate
140
 
141
genvar i;
142
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
143
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
144
end
145
endgenerate
146
 
147
assign #lock_delay lock = rst_n_i;
148
 
149
endmodule
150
`else
151
generate if (number_of_clk==1 & index==0) begin
152
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
153
end
154
endgenerate // index==0
155
generate if (number_of_clk==1 & index==1) begin
156
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
157
end
158
endgenerate // index==1
159
generate if (number_of_clk==1 & index==2) begin
160
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
161
end
162
endgenerate // index==2
163
generate if (number_of_clk==1 & index==3) begin
164
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
165
end
166
endgenerate // index==0
167
 
168
generate if (number_of_clk==2 & index==0) begin
169
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
170
end
171
endgenerate // index==0
172
generate if (number_of_clk==2 & index==1) begin
173
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
174
end
175
endgenerate // index==1
176
generate if (number_of_clk==2 & index==2) begin
177
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
178
end
179
endgenerate // index==2
180
generate if (number_of_clk==2 & index==3) begin
181
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
182
end
183
endgenerate // index==0
184
 
185
generate if (number_of_clk==3 & index==0) begin
186
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
187
end
188
endgenerate // index==0
189
generate if (number_of_clk==3 & index==1) begin
190
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
191
end
192
endgenerate // index==1
193
generate if (number_of_clk==3 & index==2) begin
194
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
195
end
196
endgenerate // index==2
197
generate if (number_of_clk==3 & index==3) begin
198
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
199
end
200
endgenerate // index==0
201
 
202
genvar i;
203
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
204
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
205
end
206
endgenerate
207
endmodule
208
`endif
209 32 unneback
///////////////////////////////////////////////////////////////////////////////
210 6 unneback
 
211
`else
212
 
213 32 unneback
///////////////////////////////////////////////////////////////////////////////
214 6 unneback
`ifdef ALTERA
215
 
216 32 unneback
`timescale 1 ps/1 ps
217
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
218
parameter index = 0;
219
parameter number_of_clk = 1;
220
parameter period_time_0 = 20000;
221
parameter period_time_1 = 20000;
222
parameter period_time_2 = 20000;
223
parameter period_time_3 = 20000;
224
parameter period_time_4 = 20000;
225
parameter lock_delay = 2000000;
226
input clk_i, rst_n_i;
227
output lock;
228
output reg [0:number_of_clk-1] clk_o;
229
output [0:number_of_clk-1] rst_o;
230
 
231
`ifdef SIM_PLL
232
 
233
always
234
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
235
 
236
generate if (number_of_clk > 1)
237
always
238
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
239
endgenerate
240
 
241
generate if (number_of_clk > 2)
242
always
243
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
244
endgenerate
245
 
246 33 unneback
generate if (number_of_clk > 3)
247 32 unneback
always
248
     #((period_time_3)/2) clk_o[3] <=  (!rst_n_i) ? 0 : ~clk_o[3];
249
endgenerate
250
 
251 33 unneback
generate if (number_of_clk > 4)
252 32 unneback
always
253
     #((period_time_4)/2) clk_o[4] <=  (!rst_n_i) ? 0 : ~clk_o[4];
254
endgenerate
255
 
256
genvar i;
257
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
258
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
259
end
260
endgenerate
261
 
262 33 unneback
//assign #lock_delay lock = rst_n_i;
263
assign lock = rst_n_i;
264 32 unneback
 
265
endmodule
266 6 unneback
`else
267
 
268 33 unneback
`ifdef VL_PLL0
269
`ifdef VL_PLL0_CLK1
270
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
271
`endif
272
`ifdef VL_PLL0_CLK2
273
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
274
`endif
275
`ifdef VL_PLL0_CLK3
276
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
277
`endif
278
`ifdef VL_PLL0_CLK4
279
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
280
`endif
281
`ifdef VL_PLL0_CLK5
282
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
283
`endif
284
`endif
285 32 unneback
 
286 33 unneback
`ifdef VL_PLL1
287
`ifdef VL_PLL1_CLK1
288
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
289
`endif
290
`ifdef VL_PLL1_CLK2
291
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
292
`endif
293
`ifdef VL_PLL1_CLK3
294
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
295
`endif
296
`ifdef VL_PLL1_CLK4
297
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
298
`endif
299
`ifdef VL_PLL1_CLK5
300
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
301
`endif
302
`endif
303 32 unneback
 
304 33 unneback
`ifdef VL_PLL2
305
`ifdef VL_PLL2_CLK1
306
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
307
`endif
308
`ifdef VL_PLL2_CLK2
309
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
310
`endif
311
`ifdef VL_PLL2_CLK3
312
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
313
`endif
314
`ifdef VL_PLL2_CLK4
315
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
316
`endif
317
`ifdef VL_PLL2_CLK5
318
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
319
`endif
320
`endif
321 32 unneback
 
322 33 unneback
`ifdef VL_PLL3
323
`ifdef VL_PLL3_CLK1
324
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
325
`endif
326
`ifdef VL_PLL3_CLK2
327
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
328
`endif
329
`ifdef VL_PLL3_CLK3
330
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
331
`endif
332
`ifdef VL_PLL3_CLK4
333
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
334
`endif
335
`ifdef VL_PLL3_CLK5
336
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
337
`endif
338
`endif
339 32 unneback
 
340
genvar i;
341
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
342 33 unneback
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
343 32 unneback
end
344
endgenerate
345
endmodule
346
`endif
347
///////////////////////////////////////////////////////////////////////////////
348
 
349
`else
350
 
351 6 unneback
// generic PLL
352 17 unneback
`timescale 1 ps/1 ps
353 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
354
parameter index = 0;
355
parameter number_of_clk = 1;
356 17 unneback
parameter period_time_0 = 20000;
357
parameter period_time_1 = 20000;
358
parameter period_time_2 = 20000;
359 6 unneback
parameter lock_delay = 2000;
360
input clk_i, rst_n_i;
361
output lock;
362
output reg [0:number_of_clk-1] clk_o;
363
output [0:number_of_clk-1] rst_o;
364
 
365
always
366
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
367
 
368
generate if (number_of_clk > 1)
369
always
370
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
371
endgenerate
372
 
373
generate if (number_of_clk > 2)
374
always
375
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
376
endgenerate
377
 
378
genvar i;
379
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
380
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
381
end
382
endgenerate
383
 
384
assign #lock_delay lock = rst_n_i;
385
 
386
endmodule
387
 
388
`endif //altera
389 17 unneback
`endif //actel
390
//////////////////////////////////////////////////////////////////////
391 6 unneback
////                                                              ////
392
////  Versatile library, registers                                ////
393
////                                                              ////
394
////  Description                                                 ////
395
////  Different type of registers                                 ////
396
////                                                              ////
397
////                                                              ////
398
////  To Do:                                                      ////
399
////   - add more different registers                             ////
400
////                                                              ////
401
////  Author(s):                                                  ////
402
////      - Michael Unneback, unneback@opencores.org              ////
403
////        ORSoC AB                                              ////
404
////                                                              ////
405
//////////////////////////////////////////////////////////////////////
406
////                                                              ////
407
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
408
////                                                              ////
409
//// This source file may be used and distributed without         ////
410
//// restriction provided that this copyright statement is not    ////
411
//// removed from the file and that any derivative work contains  ////
412
//// the original copyright notice and the associated disclaimer. ////
413
////                                                              ////
414
//// This source file is free software; you can redistribute it   ////
415
//// and/or modify it under the terms of the GNU Lesser General   ////
416
//// Public License as published by the Free Software Foundation; ////
417
//// either version 2.1 of the License, or (at your option) any   ////
418
//// later version.                                               ////
419
////                                                              ////
420
//// This source is distributed in the hope that it will be       ////
421
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
422
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
423
//// PURPOSE.  See the GNU Lesser General Public License for more ////
424
//// details.                                                     ////
425
////                                                              ////
426
//// You should have received a copy of the GNU Lesser General    ////
427
//// Public License along with this source; if not, download it   ////
428
//// from http://www.opencores.org/lgpl.shtml                     ////
429
////                                                              ////
430
//////////////////////////////////////////////////////////////////////
431
 
432 18 unneback
module vl_dff ( d, q, clk, rst);
433 6 unneback
 
434
        parameter width = 1;
435
        parameter reset_value = 0;
436
 
437
        input [width-1:0] d;
438
        input clk, rst;
439
        output reg [width-1:0] q;
440
 
441
        always @ (posedge clk or posedge rst)
442
        if (rst)
443
                q <= reset_value;
444
        else
445
                q <= d;
446
 
447
endmodule
448
 
449 18 unneback
module vl_dff_array ( d, q, clk, rst);
450 6 unneback
 
451
        parameter width = 1;
452
        parameter depth = 2;
453
        parameter reset_value = 1'b0;
454
 
455
        input [width-1:0] d;
456
        input clk, rst;
457
        output [width-1:0] q;
458
        reg  [0:depth-1] q_tmp [width-1:0];
459
        integer i;
460
        always @ (posedge clk or posedge rst)
461
        if (rst) begin
462
            for (i=0;i<depth;i=i+1)
463
                q_tmp[i] <= {width{reset_value}};
464
        end else begin
465
            q_tmp[0] <= d;
466
            for (i=1;i<depth;i=i+1)
467
                q_tmp[i] <= q_tmp[i-1];
468
        end
469
 
470
    assign q = q_tmp[depth-1];
471
 
472
endmodule
473
 
474 18 unneback
module vl_dff_ce ( d, ce, q, clk, rst);
475 6 unneback
 
476
        parameter width = 1;
477
        parameter reset_value = 0;
478
 
479
        input [width-1:0] d;
480
        input ce, clk, rst;
481
        output reg [width-1:0] q;
482
 
483
        always @ (posedge clk or posedge rst)
484
        if (rst)
485
                q <= reset_value;
486
        else
487
                if (ce)
488
                        q <= d;
489
 
490
endmodule
491
 
492 18 unneback
module vl_dff_ce_clear ( d, ce, clear, q, clk, rst);
493 8 unneback
 
494
        parameter width = 1;
495
        parameter reset_value = 0;
496
 
497
        input [width-1:0] d;
498 10 unneback
        input ce, clear, clk, rst;
499 8 unneback
        output reg [width-1:0] q;
500
 
501
        always @ (posedge clk or posedge rst)
502
        if (rst)
503
            q <= reset_value;
504
        else
505
            if (ce)
506
                if (clear)
507
                    q <= {width{1'b0}};
508
                else
509
                    q <= d;
510
 
511
endmodule
512
 
513 24 unneback
module vl_dff_ce_set ( d, ce, set, q, clk, rst);
514
 
515
        parameter width = 1;
516
        parameter reset_value = 0;
517
 
518
        input [width-1:0] d;
519
        input ce, set, clk, rst;
520
        output reg [width-1:0] q;
521
 
522
        always @ (posedge clk or posedge rst)
523
        if (rst)
524
            q <= reset_value;
525
        else
526
            if (ce)
527
                if (set)
528
                    q <= {width{1'b1}};
529
                else
530
                    q <= d;
531
 
532
endmodule
533
 
534 29 unneback
module vl_spr ( sp, r, q, clk, rst);
535
 
536
        parameter width = 1;
537
        parameter reset_value = 0;
538
 
539
        input sp, r;
540
        output reg q;
541
        input clk, rst;
542
 
543
        always @ (posedge clk or posedge rst)
544
        if (rst)
545
            q <= reset_value;
546
        else
547
            if (sp)
548
                q <= 1'b1;
549
            else if (r)
550
                q <= 1'b0;
551
 
552
endmodule
553
 
554
module vl_srp ( s, rp, q, clk, rst);
555
 
556
        parameter width = 1;
557
        parameter reset_value = 0;
558
 
559
        input s, rp;
560
        output reg q;
561
        input clk, rst;
562
 
563
        always @ (posedge clk or posedge rst)
564
        if (rst)
565
            q <= reset_value;
566
        else
567
            if (rp)
568
                q <= 1'b0;
569
            else if (s)
570
                q <= 1'b1;
571
 
572
endmodule
573
 
574
 
575 6 unneback
`ifdef ALTERA
576
// megafunction wizard: %LPM_FF%
577
// GENERATION: STANDARD
578
// VERSION: WM1.0
579
// MODULE: lpm_ff 
580
 
581
// ============================================================
582
// File Name: dff_sr.v
583
// Megafunction Name(s):
584
//                      lpm_ff
585
//
586
// Simulation Library Files(s):
587
//                      lpm
588
// ============================================================
589
// ************************************************************
590
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
591
//
592
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
593
// ************************************************************
594
 
595
 
596
//Copyright (C) 1991-2010 Altera Corporation
597
//Your use of Altera Corporation's design tools, logic functions 
598
//and other software and tools, and its AMPP partner logic 
599
//functions, and any output files from any of the foregoing 
600
//(including device programming or simulation files), and any 
601
//associated documentation or information are expressly subject 
602
//to the terms and conditions of the Altera Program License 
603
//Subscription Agreement, Altera MegaCore Function License 
604
//Agreement, or other applicable license agreement, including, 
605
//without limitation, that your use is for the sole purpose of 
606
//programming logic devices manufactured by Altera and sold by 
607
//Altera or its authorized distributors.  Please refer to the 
608
//applicable agreement for further details.
609
 
610
 
611
// synopsys translate_off
612
`timescale 1 ps / 1 ps
613
// synopsys translate_on
614 18 unneback
module vl_dff_sr (
615 6 unneback
        aclr,
616
        aset,
617
        clock,
618
        data,
619
        q);
620
 
621
        input     aclr;
622
        input     aset;
623
        input     clock;
624
        input     data;
625
        output    q;
626
 
627
        wire [0:0] sub_wire0;
628
        wire [0:0] sub_wire1 = sub_wire0[0:0];
629
        wire  q = sub_wire1;
630
        wire  sub_wire2 = data;
631
        wire  sub_wire3 = sub_wire2;
632
 
633
        lpm_ff  lpm_ff_component (
634
                                .aclr (aclr),
635
                                .clock (clock),
636
                                .data (sub_wire3),
637
                                .aset (aset),
638
                                .q (sub_wire0)
639
                                // synopsys translate_off
640
                                ,
641
                                .aload (),
642
                                .enable (),
643
                                .sclr (),
644
                                .sload (),
645
                                .sset ()
646
                                // synopsys translate_on
647
                                );
648
        defparam
649
                lpm_ff_component.lpm_fftype = "DFF",
650
                lpm_ff_component.lpm_type = "LPM_FF",
651
                lpm_ff_component.lpm_width = 1;
652
 
653
 
654
endmodule
655
 
656
// ============================================================
657
// CNX file retrieval info
658
// ============================================================
659
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
660
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
661
// Retrieval info: PRIVATE: ASET NUMERIC "1"
662
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
663
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
664
// Retrieval info: PRIVATE: DFF NUMERIC "1"
665
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
666
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
667
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
668
// Retrieval info: PRIVATE: SSET NUMERIC "0"
669
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
670
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
671
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
672
// Retrieval info: PRIVATE: nBit NUMERIC "1"
673
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
674
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
675
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
676
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
677
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
678
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
679
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
680
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
681
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
682
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
683
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
684
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
685
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
686
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
687
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
688
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
689
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
690
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
691
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
692
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
693
// Retrieval info: LIB_FILE: lpm
694
 
695
 
696
`else
697
 
698
 
699 18 unneback
module vl_dff_sr ( aclr, aset, clock, data, q);
700 6 unneback
 
701
    input         aclr;
702
    input         aset;
703
    input         clock;
704
    input         data;
705
    output reg    q;
706
 
707
   always @ (posedge clock or posedge aclr or posedge aset)
708
     if (aclr)
709
       q <= 1'b0;
710
     else if (aset)
711
       q <= 1'b1;
712
     else
713
       q <= data;
714
 
715
endmodule
716
 
717
`endif
718
 
719
// LATCH
720
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
721
`ifdef ALTERA
722 18 unneback
module vl_latch ( d, le, q, clk);
723 6 unneback
input d, le;
724
output q;
725
input clk;
726
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
727
endmodule
728
`else
729
module latch ( d, le, q, clk);
730
input d, le;
731
output q;
732
input clk;/*
733
   always @ (posedge direction_set or posedge direction_clr)
734
     if (direction_clr)
735
       direction <= going_empty;
736
     else
737
       direction <= going_full;*/
738
endmodule
739 15 unneback
`endif
740
 
741 18 unneback
module vl_shreg ( d, q, clk, rst);
742 17 unneback
parameter depth = 10;
743
input d;
744
output q;
745
input clk, rst;
746
 
747
reg [1:depth] dffs;
748
 
749
always @ (posedge clk or posedge rst)
750
if (rst)
751
    dffs <= {depth{1'b0}};
752
else
753
    dffs <= {d,dffs[1:depth-1]};
754
assign q = dffs[depth];
755
endmodule
756
 
757 18 unneback
module vl_shreg_ce ( d, ce, q, clk, rst);
758 17 unneback
parameter depth = 10;
759
input d, ce;
760
output q;
761
input clk, rst;
762
 
763
reg [1:depth] dffs;
764
 
765
always @ (posedge clk or posedge rst)
766
if (rst)
767
    dffs <= {depth{1'b0}};
768
else
769
    if (ce)
770
        dffs <= {d,dffs[1:depth-1]};
771
assign q = dffs[depth];
772
endmodule
773
 
774 18 unneback
module vl_delay ( d, q, clk, rst);
775 15 unneback
parameter depth = 10;
776
input d;
777
output q;
778
input clk, rst;
779
 
780
reg [1:depth] dffs;
781
 
782
always @ (posedge clk or posedge rst)
783
if (rst)
784
    dffs <= {depth{1'b0}};
785
else
786
    dffs <= {d,dffs[1:depth-1]};
787
assign q = dffs[depth];
788 17 unneback
endmodule
789
 
790 18 unneback
module vl_delay_emptyflag ( d, q, emptyflag, clk, rst);
791 17 unneback
parameter depth = 10;
792
input d;
793
output q, emptyflag;
794
input clk, rst;
795
 
796
reg [1:depth] dffs;
797
 
798
always @ (posedge clk or posedge rst)
799
if (rst)
800
    dffs <= {depth{1'b0}};
801
else
802
    dffs <= {d,dffs[1:depth-1]};
803
assign q = dffs[depth];
804
assign emptyflag = !(|dffs);
805
endmodule
806
//////////////////////////////////////////////////////////////////////
807 6 unneback
////                                                              ////
808 18 unneback
////  Logic functions                                             ////
809
////                                                              ////
810
////  Description                                                 ////
811
////  Logic functions such as multiplexers                        ////
812
////                                                              ////
813
////                                                              ////
814
////  To Do:                                                      ////
815
////   -                                                          ////
816
////                                                              ////
817
////  Author(s):                                                  ////
818
////      - Michael Unneback, unneback@opencores.org              ////
819
////        ORSoC AB                                              ////
820
////                                                              ////
821
//////////////////////////////////////////////////////////////////////
822
////                                                              ////
823
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
824
////                                                              ////
825
//// This source file may be used and distributed without         ////
826
//// restriction provided that this copyright statement is not    ////
827
//// removed from the file and that any derivative work contains  ////
828
//// the original copyright notice and the associated disclaimer. ////
829
////                                                              ////
830
//// This source file is free software; you can redistribute it   ////
831
//// and/or modify it under the terms of the GNU Lesser General   ////
832
//// Public License as published by the Free Software Foundation; ////
833
//// either version 2.1 of the License, or (at your option) any   ////
834
//// later version.                                               ////
835
////                                                              ////
836
//// This source is distributed in the hope that it will be       ////
837
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
838
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
839
//// PURPOSE.  See the GNU Lesser General Public License for more ////
840
//// details.                                                     ////
841
////                                                              ////
842
//// You should have received a copy of the GNU Lesser General    ////
843
//// Public License along with this source; if not, download it   ////
844
//// from http://www.opencores.org/lgpl.shtml                     ////
845
////                                                              ////
846
//////////////////////////////////////////////////////////////////////
847 34 unneback
module vl_mux2_andor ( a1, a0, sel, dout);
848 18 unneback
 
849 34 unneback
parameter width = 32;
850
parameter nr_of_ports = 2;
851
input [width-1:0] a1, a0;
852
input [nr_of_ports-1:0] sel;
853
output [width-1:0] dout;
854
 
855
wire [width-1:0] tmp [nr_of_ports-1:0];
856
integer i;
857
 
858
// and
859
assign tmp[0] = {width{sel[0]}} & a0;
860
assign tmp[1] = {width{sel[1]}} & a1;
861
 
862
// or
863
assign dout = tmp[1] | tmp[0];
864
 
865
endmodule
866
 
867
module vl_mux3_andor ( a2, a1, a0, sel, dout);
868
 
869
parameter width = 32;
870
parameter nr_of_ports = 3;
871
input [width-1:0] a2, a1, a0;
872
input [nr_of_ports-1:0] sel;
873
output [width-1:0] dout;
874
 
875
wire [width-1:0] tmp [nr_of_ports-1:0];
876
integer i;
877
 
878
// and
879
assign tmp[0] = {width{sel[0]}} & a0;
880
assign tmp[1] = {width{sel[1]}} & a1;
881
assign tmp[2] = {width{sel[2]}} & a2;
882
 
883
// or
884
assign dout = tmp[2] | tmp[1] | tmp[0];
885
 
886
endmodule
887
 
888 18 unneback
module vl_mux4_andor ( a3, a2, a1, a0, sel, dout);
889
 
890
parameter width = 32;
891
parameter nr_of_ports = 4;
892
input [width-1:0] a3, a2, a1, a0;
893
input [nr_of_ports-1:0] sel;
894 22 unneback
output [width-1:0] dout;
895 18 unneback
 
896 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
897 18 unneback
integer i;
898
 
899
// and
900
assign tmp[0] = {width{sel[0]}} & a0;
901
assign tmp[1] = {width{sel[1]}} & a1;
902
assign tmp[2] = {width{sel[2]}} & a2;
903
assign tmp[3] = {width{sel[3]}} & a3;
904
 
905
// or
906
assign dout = tmp[3] | tmp[2] | tmp[1] | tmp[0];
907
 
908
endmodule
909
 
910
module vl_mux5_andor ( a4, a3, a2, a1, a0, sel, dout);
911
 
912
parameter width = 32;
913
parameter nr_of_ports = 5;
914
input [width-1:0] a4, a3, a2, a1, a0;
915
input [nr_of_ports-1:0] sel;
916 22 unneback
output [width-1:0] dout;
917 18 unneback
 
918 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
919 18 unneback
integer i;
920
 
921
// and
922
assign tmp[0] = {width{sel[0]}} & a0;
923
assign tmp[1] = {width{sel[1]}} & a1;
924
assign tmp[2] = {width{sel[2]}} & a2;
925
assign tmp[3] = {width{sel[3]}} & a3;
926
assign tmp[4] = {width{sel[4]}} & a4;
927
 
928
// or
929
assign dout = tmp[4] | tmp[3] | tmp[2] | tmp[1] | tmp[0];
930
 
931
endmodule
932
 
933
module vl_mux6_andor ( a5, a4, a3, a2, a1, a0, sel, dout);
934
 
935
parameter width = 32;
936
parameter nr_of_ports = 6;
937
input [width-1:0] a5, a4, a3, a2, a1, a0;
938
input [nr_of_ports-1:0] sel;
939 22 unneback
output [width-1:0] dout;
940 18 unneback
 
941 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
942 18 unneback
integer i;
943
 
944
// and
945
assign tmp[0] = {width{sel[0]}} & a0;
946
assign tmp[1] = {width{sel[1]}} & a1;
947
assign tmp[2] = {width{sel[2]}} & a2;
948
assign tmp[3] = {width{sel[3]}} & a3;
949
assign tmp[4] = {width{sel[4]}} & a4;
950
assign tmp[5] = {width{sel[5]}} & a5;
951
 
952
// or
953
assign dout = tmp[5] | tmp[4] | tmp[3] | tmp[2] | tmp[1] | tmp[0];
954
 
955
endmodule
956
//////////////////////////////////////////////////////////////////////
957
////                                                              ////
958 6 unneback
////  Versatile counter                                           ////
959
////                                                              ////
960
////  Description                                                 ////
961
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
962
////  counter                                                     ////
963
////                                                              ////
964
////  To Do:                                                      ////
965
////   - add LFSR with more taps                                  ////
966
////                                                              ////
967
////  Author(s):                                                  ////
968
////      - Michael Unneback, unneback@opencores.org              ////
969
////        ORSoC AB                                              ////
970
////                                                              ////
971
//////////////////////////////////////////////////////////////////////
972
////                                                              ////
973
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
974
////                                                              ////
975
//// This source file may be used and distributed without         ////
976
//// restriction provided that this copyright statement is not    ////
977
//// removed from the file and that any derivative work contains  ////
978
//// the original copyright notice and the associated disclaimer. ////
979
////                                                              ////
980
//// This source file is free software; you can redistribute it   ////
981
//// and/or modify it under the terms of the GNU Lesser General   ////
982
//// Public License as published by the Free Software Foundation; ////
983
//// either version 2.1 of the License, or (at your option) any   ////
984
//// later version.                                               ////
985
////                                                              ////
986
//// This source is distributed in the hope that it will be       ////
987
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
988
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
989
//// PURPOSE.  See the GNU Lesser General Public License for more ////
990
//// details.                                                     ////
991
////                                                              ////
992
//// You should have received a copy of the GNU Lesser General    ////
993
//// Public License along with this source; if not, download it   ////
994
//// from http://www.opencores.org/lgpl.shtml                     ////
995
////                                                              ////
996
//////////////////////////////////////////////////////////////////////
997
 
998
// binary counter
999 22 unneback
module vl_cnt_bin ( q, rst, clk);
1000
 
1001
   parameter length = 4;
1002
   output [length:1] q;
1003
   input rst;
1004
   input clk;
1005
 
1006
   parameter clear_value = 0;
1007
   parameter set_value = 1;
1008
   parameter wrap_value = 0;
1009
   parameter level1_value = 15;
1010
 
1011
   reg  [length:1] qi;
1012
   wire [length:1] q_next;
1013
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1014
 
1015
   always @ (posedge clk or posedge rst)
1016
     if (rst)
1017
       qi <= {length{1'b0}};
1018
     else
1019
       qi <= q_next;
1020
 
1021
   assign q = qi;
1022
 
1023
endmodule
1024
//////////////////////////////////////////////////////////////////////
1025
////                                                              ////
1026
////  Versatile counter                                           ////
1027
////                                                              ////
1028
////  Description                                                 ////
1029
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1030
////  counter                                                     ////
1031
////                                                              ////
1032
////  To Do:                                                      ////
1033
////   - add LFSR with more taps                                  ////
1034
////                                                              ////
1035
////  Author(s):                                                  ////
1036
////      - Michael Unneback, unneback@opencores.org              ////
1037
////        ORSoC AB                                              ////
1038
////                                                              ////
1039
//////////////////////////////////////////////////////////////////////
1040
////                                                              ////
1041
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1042
////                                                              ////
1043
//// This source file may be used and distributed without         ////
1044
//// restriction provided that this copyright statement is not    ////
1045
//// removed from the file and that any derivative work contains  ////
1046
//// the original copyright notice and the associated disclaimer. ////
1047
////                                                              ////
1048
//// This source file is free software; you can redistribute it   ////
1049
//// and/or modify it under the terms of the GNU Lesser General   ////
1050
//// Public License as published by the Free Software Foundation; ////
1051
//// either version 2.1 of the License, or (at your option) any   ////
1052
//// later version.                                               ////
1053
////                                                              ////
1054
//// This source is distributed in the hope that it will be       ////
1055
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1056
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1057
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1058
//// details.                                                     ////
1059
////                                                              ////
1060
//// You should have received a copy of the GNU Lesser General    ////
1061
//// Public License along with this source; if not, download it   ////
1062
//// from http://www.opencores.org/lgpl.shtml                     ////
1063
////                                                              ////
1064
//////////////////////////////////////////////////////////////////////
1065
 
1066
// binary counter
1067
module vl_cnt_bin_clear ( clear, q, rst, clk);
1068
 
1069
   parameter length = 4;
1070
   input clear;
1071
   output [length:1] q;
1072
   input rst;
1073
   input clk;
1074
 
1075
   parameter clear_value = 0;
1076
   parameter set_value = 1;
1077
   parameter wrap_value = 0;
1078
   parameter level1_value = 15;
1079
 
1080
   reg  [length:1] qi;
1081
   wire [length:1] q_next;
1082
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1083
 
1084
   always @ (posedge clk or posedge rst)
1085
     if (rst)
1086
       qi <= {length{1'b0}};
1087
     else
1088
       qi <= q_next;
1089
 
1090
   assign q = qi;
1091
 
1092
endmodule
1093
//////////////////////////////////////////////////////////////////////
1094
////                                                              ////
1095
////  Versatile counter                                           ////
1096
////                                                              ////
1097
////  Description                                                 ////
1098
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1099
////  counter                                                     ////
1100
////                                                              ////
1101
////  To Do:                                                      ////
1102
////   - add LFSR with more taps                                  ////
1103
////                                                              ////
1104
////  Author(s):                                                  ////
1105
////      - Michael Unneback, unneback@opencores.org              ////
1106
////        ORSoC AB                                              ////
1107
////                                                              ////
1108
//////////////////////////////////////////////////////////////////////
1109
////                                                              ////
1110
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1111
////                                                              ////
1112
//// This source file may be used and distributed without         ////
1113
//// restriction provided that this copyright statement is not    ////
1114
//// removed from the file and that any derivative work contains  ////
1115
//// the original copyright notice and the associated disclaimer. ////
1116
////                                                              ////
1117
//// This source file is free software; you can redistribute it   ////
1118
//// and/or modify it under the terms of the GNU Lesser General   ////
1119
//// Public License as published by the Free Software Foundation; ////
1120
//// either version 2.1 of the License, or (at your option) any   ////
1121
//// later version.                                               ////
1122
////                                                              ////
1123
//// This source is distributed in the hope that it will be       ////
1124
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1125
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1126
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1127
//// details.                                                     ////
1128
////                                                              ////
1129
//// You should have received a copy of the GNU Lesser General    ////
1130
//// Public License along with this source; if not, download it   ////
1131
//// from http://www.opencores.org/lgpl.shtml                     ////
1132
////                                                              ////
1133
//////////////////////////////////////////////////////////////////////
1134
 
1135
// binary counter
1136 18 unneback
module vl_cnt_bin_ce ( cke, q, rst, clk);
1137 6 unneback
 
1138
   parameter length = 4;
1139
   input cke;
1140
   output [length:1] q;
1141
   input rst;
1142
   input clk;
1143
 
1144
   parameter clear_value = 0;
1145
   parameter set_value = 1;
1146
   parameter wrap_value = 0;
1147
   parameter level1_value = 15;
1148
 
1149
   reg  [length:1] qi;
1150
   wire [length:1] q_next;
1151
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1152
 
1153
   always @ (posedge clk or posedge rst)
1154
     if (rst)
1155
       qi <= {length{1'b0}};
1156
     else
1157
     if (cke)
1158
       qi <= q_next;
1159
 
1160
   assign q = qi;
1161
 
1162
endmodule
1163
//////////////////////////////////////////////////////////////////////
1164
////                                                              ////
1165
////  Versatile counter                                           ////
1166
////                                                              ////
1167
////  Description                                                 ////
1168
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1169
////  counter                                                     ////
1170
////                                                              ////
1171
////  To Do:                                                      ////
1172
////   - add LFSR with more taps                                  ////
1173
////                                                              ////
1174
////  Author(s):                                                  ////
1175
////      - Michael Unneback, unneback@opencores.org              ////
1176
////        ORSoC AB                                              ////
1177
////                                                              ////
1178
//////////////////////////////////////////////////////////////////////
1179
////                                                              ////
1180
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1181
////                                                              ////
1182
//// This source file may be used and distributed without         ////
1183
//// restriction provided that this copyright statement is not    ////
1184
//// removed from the file and that any derivative work contains  ////
1185
//// the original copyright notice and the associated disclaimer. ////
1186
////                                                              ////
1187
//// This source file is free software; you can redistribute it   ////
1188
//// and/or modify it under the terms of the GNU Lesser General   ////
1189
//// Public License as published by the Free Software Foundation; ////
1190
//// either version 2.1 of the License, or (at your option) any   ////
1191
//// later version.                                               ////
1192
////                                                              ////
1193
//// This source is distributed in the hope that it will be       ////
1194
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1195
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1196
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1197
//// details.                                                     ////
1198
////                                                              ////
1199
//// You should have received a copy of the GNU Lesser General    ////
1200
//// Public License along with this source; if not, download it   ////
1201
//// from http://www.opencores.org/lgpl.shtml                     ////
1202
////                                                              ////
1203
//////////////////////////////////////////////////////////////////////
1204
 
1205
// binary counter
1206 18 unneback
module vl_cnt_bin_ce_clear ( clear, cke, q, rst, clk);
1207 6 unneback
 
1208
   parameter length = 4;
1209
   input clear;
1210
   input cke;
1211
   output [length:1] q;
1212
   input rst;
1213
   input clk;
1214
 
1215
   parameter clear_value = 0;
1216
   parameter set_value = 1;
1217
   parameter wrap_value = 0;
1218
   parameter level1_value = 15;
1219
 
1220
   reg  [length:1] qi;
1221
   wire [length:1] q_next;
1222
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1223
 
1224
   always @ (posedge clk or posedge rst)
1225
     if (rst)
1226
       qi <= {length{1'b0}};
1227
     else
1228
     if (cke)
1229
       qi <= q_next;
1230
 
1231
   assign q = qi;
1232
 
1233
endmodule
1234
//////////////////////////////////////////////////////////////////////
1235
////                                                              ////
1236
////  Versatile counter                                           ////
1237
////                                                              ////
1238
////  Description                                                 ////
1239
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1240
////  counter                                                     ////
1241
////                                                              ////
1242
////  To Do:                                                      ////
1243
////   - add LFSR with more taps                                  ////
1244
////                                                              ////
1245
////  Author(s):                                                  ////
1246
////      - Michael Unneback, unneback@opencores.org              ////
1247
////        ORSoC AB                                              ////
1248
////                                                              ////
1249
//////////////////////////////////////////////////////////////////////
1250
////                                                              ////
1251
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1252
////                                                              ////
1253
//// This source file may be used and distributed without         ////
1254
//// restriction provided that this copyright statement is not    ////
1255
//// removed from the file and that any derivative work contains  ////
1256
//// the original copyright notice and the associated disclaimer. ////
1257
////                                                              ////
1258
//// This source file is free software; you can redistribute it   ////
1259
//// and/or modify it under the terms of the GNU Lesser General   ////
1260
//// Public License as published by the Free Software Foundation; ////
1261
//// either version 2.1 of the License, or (at your option) any   ////
1262
//// later version.                                               ////
1263
////                                                              ////
1264
//// This source is distributed in the hope that it will be       ////
1265
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1266
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1267
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1268
//// details.                                                     ////
1269
////                                                              ////
1270
//// You should have received a copy of the GNU Lesser General    ////
1271
//// Public License along with this source; if not, download it   ////
1272
//// from http://www.opencores.org/lgpl.shtml                     ////
1273
////                                                              ////
1274
//////////////////////////////////////////////////////////////////////
1275
 
1276
// binary counter
1277 29 unneback
module vl_cnt_bin_ce_clear_l1_l2 ( clear, cke, q, level1, level2, rst, clk);
1278
 
1279
   parameter length = 4;
1280
   input clear;
1281
   input cke;
1282
   output [length:1] q;
1283
   output reg level1;
1284
   output reg level2;
1285
   input rst;
1286
   input clk;
1287
 
1288
   parameter clear_value = 0;
1289
   parameter set_value = 1;
1290 30 unneback
   parameter wrap_value = 15;
1291
   parameter level1_value = 8;
1292
   parameter level2_value = 15;
1293 29 unneback
 
1294
   wire rew;
1295 30 unneback
   assign rew = 1'b0;
1296 29 unneback
   reg  [length:1] qi;
1297
   wire [length:1] q_next;
1298
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1299
 
1300
   always @ (posedge clk or posedge rst)
1301
     if (rst)
1302
       qi <= {length{1'b0}};
1303
     else
1304
     if (cke)
1305
       qi <= q_next;
1306
 
1307
   assign q = qi;
1308
 
1309
 
1310
    always @ (posedge clk or posedge rst)
1311
    if (rst)
1312
        level1 <= 1'b0;
1313
    else
1314
    if (cke)
1315
    if (clear)
1316
        level1 <= 1'b0;
1317
    else if (q_next == level1_value)
1318
        level1 <= 1'b1;
1319
    else if (qi == level1_value & rew)
1320
        level1 <= 1'b0;
1321
 
1322
    always @ (posedge clk or posedge rst)
1323
    if (rst)
1324
        level2 <= 1'b0;
1325
    else
1326
    if (cke)
1327
    if (clear)
1328
        level2 <= 1'b0;
1329
    else if (q_next == level2_value)
1330
        level2 <= 1'b1;
1331
    else if (qi == level2_value & rew)
1332
        level2 <= 1'b0;
1333
endmodule
1334
//////////////////////////////////////////////////////////////////////
1335
////                                                              ////
1336
////  Versatile counter                                           ////
1337
////                                                              ////
1338
////  Description                                                 ////
1339
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1340
////  counter                                                     ////
1341
////                                                              ////
1342
////  To Do:                                                      ////
1343
////   - add LFSR with more taps                                  ////
1344
////                                                              ////
1345
////  Author(s):                                                  ////
1346
////      - Michael Unneback, unneback@opencores.org              ////
1347
////        ORSoC AB                                              ////
1348
////                                                              ////
1349
//////////////////////////////////////////////////////////////////////
1350
////                                                              ////
1351
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1352
////                                                              ////
1353
//// This source file may be used and distributed without         ////
1354
//// restriction provided that this copyright statement is not    ////
1355
//// removed from the file and that any derivative work contains  ////
1356
//// the original copyright notice and the associated disclaimer. ////
1357
////                                                              ////
1358
//// This source file is free software; you can redistribute it   ////
1359
//// and/or modify it under the terms of the GNU Lesser General   ////
1360
//// Public License as published by the Free Software Foundation; ////
1361
//// either version 2.1 of the License, or (at your option) any   ////
1362
//// later version.                                               ////
1363
////                                                              ////
1364
//// This source is distributed in the hope that it will be       ////
1365
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1366
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1367
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1368
//// details.                                                     ////
1369
////                                                              ////
1370
//// You should have received a copy of the GNU Lesser General    ////
1371
//// Public License along with this source; if not, download it   ////
1372
//// from http://www.opencores.org/lgpl.shtml                     ////
1373
////                                                              ////
1374
//////////////////////////////////////////////////////////////////////
1375
 
1376
// binary counter
1377 18 unneback
module vl_cnt_bin_ce_clear_set_rew ( clear, set, cke, rew, q, rst, clk);
1378 6 unneback
 
1379
   parameter length = 4;
1380
   input clear;
1381
   input set;
1382
   input cke;
1383
   input rew;
1384
   output [length:1] q;
1385
   input rst;
1386
   input clk;
1387
 
1388
   parameter clear_value = 0;
1389
   parameter set_value = 1;
1390
   parameter wrap_value = 0;
1391
   parameter level1_value = 15;
1392
 
1393
   reg  [length:1] qi;
1394
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1395
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
1396
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
1397
   assign q_next = rew ? q_next_rew : q_next_fw;
1398
 
1399
   always @ (posedge clk or posedge rst)
1400
     if (rst)
1401
       qi <= {length{1'b0}};
1402
     else
1403
     if (cke)
1404
       qi <= q_next;
1405
 
1406
   assign q = qi;
1407
 
1408
endmodule
1409
//////////////////////////////////////////////////////////////////////
1410
////                                                              ////
1411
////  Versatile counter                                           ////
1412
////                                                              ////
1413
////  Description                                                 ////
1414
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1415
////  counter                                                     ////
1416
////                                                              ////
1417
////  To Do:                                                      ////
1418
////   - add LFSR with more taps                                  ////
1419
////                                                              ////
1420
////  Author(s):                                                  ////
1421
////      - Michael Unneback, unneback@opencores.org              ////
1422
////        ORSoC AB                                              ////
1423
////                                                              ////
1424
//////////////////////////////////////////////////////////////////////
1425
////                                                              ////
1426
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1427
////                                                              ////
1428
//// This source file may be used and distributed without         ////
1429
//// restriction provided that this copyright statement is not    ////
1430
//// removed from the file and that any derivative work contains  ////
1431
//// the original copyright notice and the associated disclaimer. ////
1432
////                                                              ////
1433
//// This source file is free software; you can redistribute it   ////
1434
//// and/or modify it under the terms of the GNU Lesser General   ////
1435
//// Public License as published by the Free Software Foundation; ////
1436
//// either version 2.1 of the License, or (at your option) any   ////
1437
//// later version.                                               ////
1438
////                                                              ////
1439
//// This source is distributed in the hope that it will be       ////
1440
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1441
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1442
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1443
//// details.                                                     ////
1444
////                                                              ////
1445
//// You should have received a copy of the GNU Lesser General    ////
1446
//// Public License along with this source; if not, download it   ////
1447
//// from http://www.opencores.org/lgpl.shtml                     ////
1448
////                                                              ////
1449
//////////////////////////////////////////////////////////////////////
1450
 
1451
// binary counter
1452 18 unneback
module vl_cnt_bin_ce_rew_l1 ( cke, rew, level1, rst, clk);
1453 6 unneback
 
1454
   parameter length = 4;
1455
   input cke;
1456
   input rew;
1457
   output reg level1;
1458
   input rst;
1459
   input clk;
1460
 
1461
   parameter clear_value = 0;
1462
   parameter set_value = 1;
1463
   parameter wrap_value = 1;
1464
   parameter level1_value = 15;
1465
 
1466 29 unneback
   wire clear;
1467 30 unneback
   assign clear = 1'b0;
1468 6 unneback
   reg  [length:1] qi;
1469
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1470
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1471
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1472
   assign q_next = rew ? q_next_rew : q_next_fw;
1473
 
1474
   always @ (posedge clk or posedge rst)
1475
     if (rst)
1476
       qi <= {length{1'b0}};
1477
     else
1478
     if (cke)
1479
       qi <= q_next;
1480
 
1481
 
1482
 
1483
    always @ (posedge clk or posedge rst)
1484
    if (rst)
1485
        level1 <= 1'b0;
1486
    else
1487
    if (cke)
1488 29 unneback
    if (clear)
1489
        level1 <= 1'b0;
1490
    else if (q_next == level1_value)
1491 6 unneback
        level1 <= 1'b1;
1492
    else if (qi == level1_value & rew)
1493
        level1 <= 1'b0;
1494
endmodule
1495
//////////////////////////////////////////////////////////////////////
1496
////                                                              ////
1497
////  Versatile counter                                           ////
1498
////                                                              ////
1499
////  Description                                                 ////
1500
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1501
////  counter                                                     ////
1502
////                                                              ////
1503
////  To Do:                                                      ////
1504
////   - add LFSR with more taps                                  ////
1505
////                                                              ////
1506
////  Author(s):                                                  ////
1507
////      - Michael Unneback, unneback@opencores.org              ////
1508
////        ORSoC AB                                              ////
1509
////                                                              ////
1510
//////////////////////////////////////////////////////////////////////
1511
////                                                              ////
1512
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1513
////                                                              ////
1514
//// This source file may be used and distributed without         ////
1515
//// restriction provided that this copyright statement is not    ////
1516
//// removed from the file and that any derivative work contains  ////
1517
//// the original copyright notice and the associated disclaimer. ////
1518
////                                                              ////
1519
//// This source file is free software; you can redistribute it   ////
1520
//// and/or modify it under the terms of the GNU Lesser General   ////
1521
//// Public License as published by the Free Software Foundation; ////
1522
//// either version 2.1 of the License, or (at your option) any   ////
1523
//// later version.                                               ////
1524
////                                                              ////
1525
//// This source is distributed in the hope that it will be       ////
1526
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1527
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1528
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1529
//// details.                                                     ////
1530
////                                                              ////
1531
//// You should have received a copy of the GNU Lesser General    ////
1532
//// Public License along with this source; if not, download it   ////
1533
//// from http://www.opencores.org/lgpl.shtml                     ////
1534
////                                                              ////
1535
//////////////////////////////////////////////////////////////////////
1536
 
1537 25 unneback
// binary counter
1538
module vl_cnt_bin_ce_rew_zq_l1 ( cke, rew, zq, level1, rst, clk);
1539
 
1540
   parameter length = 4;
1541
   input cke;
1542
   input rew;
1543
   output reg zq;
1544
   output reg level1;
1545
   input rst;
1546
   input clk;
1547
 
1548
   parameter clear_value = 0;
1549
   parameter set_value = 1;
1550
   parameter wrap_value = 1;
1551
   parameter level1_value = 15;
1552
 
1553 29 unneback
   wire clear;
1554 30 unneback
   assign clear = 1'b0;
1555 25 unneback
   reg  [length:1] qi;
1556
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1557
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1558
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1559
   assign q_next = rew ? q_next_rew : q_next_fw;
1560
 
1561
   always @ (posedge clk or posedge rst)
1562
     if (rst)
1563
       qi <= {length{1'b0}};
1564
     else
1565
     if (cke)
1566
       qi <= q_next;
1567
 
1568
 
1569
 
1570
   always @ (posedge clk or posedge rst)
1571
     if (rst)
1572
       zq <= 1'b1;
1573
     else
1574
     if (cke)
1575
       zq <= q_next == {length{1'b0}};
1576
 
1577
    always @ (posedge clk or posedge rst)
1578
    if (rst)
1579
        level1 <= 1'b0;
1580
    else
1581
    if (cke)
1582 29 unneback
    if (clear)
1583
        level1 <= 1'b0;
1584
    else if (q_next == level1_value)
1585 25 unneback
        level1 <= 1'b1;
1586
    else if (qi == level1_value & rew)
1587
        level1 <= 1'b0;
1588
endmodule
1589
//////////////////////////////////////////////////////////////////////
1590
////                                                              ////
1591
////  Versatile counter                                           ////
1592
////                                                              ////
1593
////  Description                                                 ////
1594
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1595
////  counter                                                     ////
1596
////                                                              ////
1597
////  To Do:                                                      ////
1598
////   - add LFSR with more taps                                  ////
1599
////                                                              ////
1600
////  Author(s):                                                  ////
1601
////      - Michael Unneback, unneback@opencores.org              ////
1602
////        ORSoC AB                                              ////
1603
////                                                              ////
1604
//////////////////////////////////////////////////////////////////////
1605
////                                                              ////
1606
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1607
////                                                              ////
1608
//// This source file may be used and distributed without         ////
1609
//// restriction provided that this copyright statement is not    ////
1610
//// removed from the file and that any derivative work contains  ////
1611
//// the original copyright notice and the associated disclaimer. ////
1612
////                                                              ////
1613
//// This source file is free software; you can redistribute it   ////
1614
//// and/or modify it under the terms of the GNU Lesser General   ////
1615
//// Public License as published by the Free Software Foundation; ////
1616
//// either version 2.1 of the License, or (at your option) any   ////
1617
//// later version.                                               ////
1618
////                                                              ////
1619
//// This source is distributed in the hope that it will be       ////
1620
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1621
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1622
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1623
//// details.                                                     ////
1624
////                                                              ////
1625
//// You should have received a copy of the GNU Lesser General    ////
1626
//// Public License along with this source; if not, download it   ////
1627
//// from http://www.opencores.org/lgpl.shtml                     ////
1628
////                                                              ////
1629
//////////////////////////////////////////////////////////////////////
1630
 
1631
// binary counter
1632
module vl_cnt_bin_ce_rew_q_zq_l1 ( cke, rew, q, zq, level1, rst, clk);
1633
 
1634
   parameter length = 4;
1635
   input cke;
1636
   input rew;
1637
   output [length:1] q;
1638
   output reg zq;
1639
   output reg level1;
1640
   input rst;
1641
   input clk;
1642
 
1643
   parameter clear_value = 0;
1644
   parameter set_value = 1;
1645
   parameter wrap_value = 1;
1646
   parameter level1_value = 15;
1647
 
1648 29 unneback
   wire clear;
1649 30 unneback
   assign clear = 1'b0;
1650 25 unneback
   reg  [length:1] qi;
1651
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1652
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1653
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1654
   assign q_next = rew ? q_next_rew : q_next_fw;
1655
 
1656
   always @ (posedge clk or posedge rst)
1657
     if (rst)
1658
       qi <= {length{1'b0}};
1659
     else
1660
     if (cke)
1661
       qi <= q_next;
1662
 
1663
   assign q = qi;
1664
 
1665
 
1666
   always @ (posedge clk or posedge rst)
1667
     if (rst)
1668
       zq <= 1'b1;
1669
     else
1670
     if (cke)
1671
       zq <= q_next == {length{1'b0}};
1672
 
1673
    always @ (posedge clk or posedge rst)
1674
    if (rst)
1675
        level1 <= 1'b0;
1676
    else
1677
    if (cke)
1678 29 unneback
    if (clear)
1679
        level1 <= 1'b0;
1680
    else if (q_next == level1_value)
1681 25 unneback
        level1 <= 1'b1;
1682
    else if (qi == level1_value & rew)
1683
        level1 <= 1'b0;
1684
endmodule
1685
//////////////////////////////////////////////////////////////////////
1686
////                                                              ////
1687
////  Versatile counter                                           ////
1688
////                                                              ////
1689
////  Description                                                 ////
1690
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1691
////  counter                                                     ////
1692
////                                                              ////
1693
////  To Do:                                                      ////
1694
////   - add LFSR with more taps                                  ////
1695
////                                                              ////
1696
////  Author(s):                                                  ////
1697
////      - Michael Unneback, unneback@opencores.org              ////
1698
////        ORSoC AB                                              ////
1699
////                                                              ////
1700
//////////////////////////////////////////////////////////////////////
1701
////                                                              ////
1702
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1703
////                                                              ////
1704
//// This source file may be used and distributed without         ////
1705
//// restriction provided that this copyright statement is not    ////
1706
//// removed from the file and that any derivative work contains  ////
1707
//// the original copyright notice and the associated disclaimer. ////
1708
////                                                              ////
1709
//// This source file is free software; you can redistribute it   ////
1710
//// and/or modify it under the terms of the GNU Lesser General   ////
1711
//// Public License as published by the Free Software Foundation; ////
1712
//// either version 2.1 of the License, or (at your option) any   ////
1713
//// later version.                                               ////
1714
////                                                              ////
1715
//// This source is distributed in the hope that it will be       ////
1716
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1717
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1718
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1719
//// details.                                                     ////
1720
////                                                              ////
1721
//// You should have received a copy of the GNU Lesser General    ////
1722
//// Public License along with this source; if not, download it   ////
1723
//// from http://www.opencores.org/lgpl.shtml                     ////
1724
////                                                              ////
1725
//////////////////////////////////////////////////////////////////////
1726
 
1727 6 unneback
// LFSR counter
1728 18 unneback
module vl_cnt_lfsr_zq ( zq, rst, clk);
1729 6 unneback
 
1730
   parameter length = 4;
1731
   output reg zq;
1732
   input rst;
1733
   input clk;
1734
 
1735
   parameter clear_value = 0;
1736
   parameter set_value = 1;
1737
   parameter wrap_value = 8;
1738
   parameter level1_value = 15;
1739
 
1740
   reg  [length:1] qi;
1741
   reg lfsr_fb;
1742
   wire [length:1] q_next;
1743
   reg [32:1] polynom;
1744
   integer i;
1745
 
1746
   always @ (qi)
1747
   begin
1748
        case (length)
1749
         2: polynom = 32'b11;                               // 0x3
1750
         3: polynom = 32'b110;                              // 0x6
1751
         4: polynom = 32'b1100;                             // 0xC
1752
         5: polynom = 32'b10100;                            // 0x14
1753
         6: polynom = 32'b110000;                           // 0x30
1754
         7: polynom = 32'b1100000;                          // 0x60
1755
         8: polynom = 32'b10111000;                         // 0xb8
1756
         9: polynom = 32'b100010000;                        // 0x110
1757
        10: polynom = 32'b1001000000;                       // 0x240
1758
        11: polynom = 32'b10100000000;                      // 0x500
1759
        12: polynom = 32'b100000101001;                     // 0x829
1760
        13: polynom = 32'b1000000001100;                    // 0x100C
1761
        14: polynom = 32'b10000000010101;                   // 0x2015
1762
        15: polynom = 32'b110000000000000;                  // 0x6000
1763
        16: polynom = 32'b1101000000001000;                 // 0xD008
1764
        17: polynom = 32'b10010000000000000;                // 0x12000
1765
        18: polynom = 32'b100000010000000000;               // 0x20400
1766
        19: polynom = 32'b1000000000000100011;              // 0x40023
1767
        20: polynom = 32'b10000010000000000000;             // 0x82000
1768
        21: polynom = 32'b101000000000000000000;            // 0x140000
1769
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1770
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1771
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1772
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1773
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1774
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1775
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1776
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1777
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1778
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1779
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1780
        default: polynom = 32'b0;
1781
        endcase
1782
        lfsr_fb = qi[length];
1783
        for (i=length-1; i>=1; i=i-1) begin
1784
            if (polynom[i])
1785
                lfsr_fb = lfsr_fb  ~^ qi[i];
1786
        end
1787
    end
1788
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1789
 
1790
   always @ (posedge clk or posedge rst)
1791
     if (rst)
1792
       qi <= {length{1'b0}};
1793
     else
1794
       qi <= q_next;
1795
 
1796
 
1797
 
1798
   always @ (posedge clk or posedge rst)
1799
     if (rst)
1800
       zq <= 1'b1;
1801
     else
1802
       zq <= q_next == {length{1'b0}};
1803
endmodule
1804
//////////////////////////////////////////////////////////////////////
1805
////                                                              ////
1806
////  Versatile counter                                           ////
1807
////                                                              ////
1808
////  Description                                                 ////
1809
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1810
////  counter                                                     ////
1811
////                                                              ////
1812
////  To Do:                                                      ////
1813
////   - add LFSR with more taps                                  ////
1814
////                                                              ////
1815
////  Author(s):                                                  ////
1816
////      - Michael Unneback, unneback@opencores.org              ////
1817
////        ORSoC AB                                              ////
1818
////                                                              ////
1819
//////////////////////////////////////////////////////////////////////
1820
////                                                              ////
1821
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1822
////                                                              ////
1823
//// This source file may be used and distributed without         ////
1824
//// restriction provided that this copyright statement is not    ////
1825
//// removed from the file and that any derivative work contains  ////
1826
//// the original copyright notice and the associated disclaimer. ////
1827
////                                                              ////
1828
//// This source file is free software; you can redistribute it   ////
1829
//// and/or modify it under the terms of the GNU Lesser General   ////
1830
//// Public License as published by the Free Software Foundation; ////
1831
//// either version 2.1 of the License, or (at your option) any   ////
1832
//// later version.                                               ////
1833
////                                                              ////
1834
//// This source is distributed in the hope that it will be       ////
1835
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1836
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1837
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1838
//// details.                                                     ////
1839
////                                                              ////
1840
//// You should have received a copy of the GNU Lesser General    ////
1841
//// Public License along with this source; if not, download it   ////
1842
//// from http://www.opencores.org/lgpl.shtml                     ////
1843
////                                                              ////
1844
//////////////////////////////////////////////////////////////////////
1845
 
1846
// LFSR counter
1847 18 unneback
module vl_cnt_lfsr_ce_zq ( cke, zq, rst, clk);
1848 6 unneback
 
1849
   parameter length = 4;
1850
   input cke;
1851
   output reg zq;
1852
   input rst;
1853
   input clk;
1854
 
1855
   parameter clear_value = 0;
1856
   parameter set_value = 1;
1857
   parameter wrap_value = 8;
1858
   parameter level1_value = 15;
1859
 
1860
   reg  [length:1] qi;
1861
   reg lfsr_fb;
1862
   wire [length:1] q_next;
1863
   reg [32:1] polynom;
1864
   integer i;
1865
 
1866
   always @ (qi)
1867
   begin
1868
        case (length)
1869
         2: polynom = 32'b11;                               // 0x3
1870
         3: polynom = 32'b110;                              // 0x6
1871
         4: polynom = 32'b1100;                             // 0xC
1872
         5: polynom = 32'b10100;                            // 0x14
1873
         6: polynom = 32'b110000;                           // 0x30
1874
         7: polynom = 32'b1100000;                          // 0x60
1875
         8: polynom = 32'b10111000;                         // 0xb8
1876
         9: polynom = 32'b100010000;                        // 0x110
1877
        10: polynom = 32'b1001000000;                       // 0x240
1878
        11: polynom = 32'b10100000000;                      // 0x500
1879
        12: polynom = 32'b100000101001;                     // 0x829
1880
        13: polynom = 32'b1000000001100;                    // 0x100C
1881
        14: polynom = 32'b10000000010101;                   // 0x2015
1882
        15: polynom = 32'b110000000000000;                  // 0x6000
1883
        16: polynom = 32'b1101000000001000;                 // 0xD008
1884
        17: polynom = 32'b10010000000000000;                // 0x12000
1885
        18: polynom = 32'b100000010000000000;               // 0x20400
1886
        19: polynom = 32'b1000000000000100011;              // 0x40023
1887
        20: polynom = 32'b10000010000000000000;             // 0x82000
1888
        21: polynom = 32'b101000000000000000000;            // 0x140000
1889
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1890
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1891
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1892
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1893
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1894
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1895
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1896
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1897
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1898
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1899
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1900
        default: polynom = 32'b0;
1901
        endcase
1902
        lfsr_fb = qi[length];
1903
        for (i=length-1; i>=1; i=i-1) begin
1904
            if (polynom[i])
1905
                lfsr_fb = lfsr_fb  ~^ qi[i];
1906
        end
1907
    end
1908
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1909
 
1910
   always @ (posedge clk or posedge rst)
1911
     if (rst)
1912
       qi <= {length{1'b0}};
1913
     else
1914
     if (cke)
1915
       qi <= q_next;
1916
 
1917
 
1918
 
1919
   always @ (posedge clk or posedge rst)
1920
     if (rst)
1921
       zq <= 1'b1;
1922
     else
1923
     if (cke)
1924
       zq <= q_next == {length{1'b0}};
1925
endmodule
1926
//////////////////////////////////////////////////////////////////////
1927
////                                                              ////
1928
////  Versatile counter                                           ////
1929
////                                                              ////
1930
////  Description                                                 ////
1931
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1932
////  counter                                                     ////
1933
////                                                              ////
1934
////  To Do:                                                      ////
1935
////   - add LFSR with more taps                                  ////
1936
////                                                              ////
1937
////  Author(s):                                                  ////
1938
////      - Michael Unneback, unneback@opencores.org              ////
1939
////        ORSoC AB                                              ////
1940
////                                                              ////
1941
//////////////////////////////////////////////////////////////////////
1942
////                                                              ////
1943
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1944
////                                                              ////
1945
//// This source file may be used and distributed without         ////
1946
//// restriction provided that this copyright statement is not    ////
1947
//// removed from the file and that any derivative work contains  ////
1948
//// the original copyright notice and the associated disclaimer. ////
1949
////                                                              ////
1950
//// This source file is free software; you can redistribute it   ////
1951
//// and/or modify it under the terms of the GNU Lesser General   ////
1952
//// Public License as published by the Free Software Foundation; ////
1953
//// either version 2.1 of the License, or (at your option) any   ////
1954
//// later version.                                               ////
1955
////                                                              ////
1956
//// This source is distributed in the hope that it will be       ////
1957
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1958
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1959
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1960
//// details.                                                     ////
1961
////                                                              ////
1962
//// You should have received a copy of the GNU Lesser General    ////
1963
//// Public License along with this source; if not, download it   ////
1964
//// from http://www.opencores.org/lgpl.shtml                     ////
1965
////                                                              ////
1966
//////////////////////////////////////////////////////////////////////
1967 22 unneback
 
1968
// LFSR counter
1969 27 unneback
module vl_cnt_lfsr_ce_q ( cke, q, rst, clk);
1970
 
1971
   parameter length = 4;
1972
   input cke;
1973
   output [length:1] q;
1974
   input rst;
1975
   input clk;
1976
 
1977
   parameter clear_value = 0;
1978
   parameter set_value = 1;
1979
   parameter wrap_value = 8;
1980
   parameter level1_value = 15;
1981
 
1982
   reg  [length:1] qi;
1983
   reg lfsr_fb;
1984
   wire [length:1] q_next;
1985
   reg [32:1] polynom;
1986
   integer i;
1987
 
1988
   always @ (qi)
1989
   begin
1990
        case (length)
1991
         2: polynom = 32'b11;                               // 0x3
1992
         3: polynom = 32'b110;                              // 0x6
1993
         4: polynom = 32'b1100;                             // 0xC
1994
         5: polynom = 32'b10100;                            // 0x14
1995
         6: polynom = 32'b110000;                           // 0x30
1996
         7: polynom = 32'b1100000;                          // 0x60
1997
         8: polynom = 32'b10111000;                         // 0xb8
1998
         9: polynom = 32'b100010000;                        // 0x110
1999
        10: polynom = 32'b1001000000;                       // 0x240
2000
        11: polynom = 32'b10100000000;                      // 0x500
2001
        12: polynom = 32'b100000101001;                     // 0x829
2002
        13: polynom = 32'b1000000001100;                    // 0x100C
2003
        14: polynom = 32'b10000000010101;                   // 0x2015
2004
        15: polynom = 32'b110000000000000;                  // 0x6000
2005
        16: polynom = 32'b1101000000001000;                 // 0xD008
2006
        17: polynom = 32'b10010000000000000;                // 0x12000
2007
        18: polynom = 32'b100000010000000000;               // 0x20400
2008
        19: polynom = 32'b1000000000000100011;              // 0x40023
2009
        20: polynom = 32'b10000010000000000000;             // 0x82000
2010
        21: polynom = 32'b101000000000000000000;            // 0x140000
2011
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2012
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2013
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2014
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2015
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2016
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2017
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2018
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2019
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2020
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2021
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2022
        default: polynom = 32'b0;
2023
        endcase
2024
        lfsr_fb = qi[length];
2025
        for (i=length-1; i>=1; i=i-1) begin
2026
            if (polynom[i])
2027
                lfsr_fb = lfsr_fb  ~^ qi[i];
2028
        end
2029
    end
2030
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2031
 
2032
   always @ (posedge clk or posedge rst)
2033
     if (rst)
2034
       qi <= {length{1'b0}};
2035
     else
2036
     if (cke)
2037
       qi <= q_next;
2038
 
2039
   assign q = qi;
2040
 
2041
endmodule
2042
//////////////////////////////////////////////////////////////////////
2043
////                                                              ////
2044
////  Versatile counter                                           ////
2045
////                                                              ////
2046
////  Description                                                 ////
2047
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2048
////  counter                                                     ////
2049
////                                                              ////
2050
////  To Do:                                                      ////
2051
////   - add LFSR with more taps                                  ////
2052
////                                                              ////
2053
////  Author(s):                                                  ////
2054
////      - Michael Unneback, unneback@opencores.org              ////
2055
////        ORSoC AB                                              ////
2056
////                                                              ////
2057
//////////////////////////////////////////////////////////////////////
2058
////                                                              ////
2059
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2060
////                                                              ////
2061
//// This source file may be used and distributed without         ////
2062
//// restriction provided that this copyright statement is not    ////
2063
//// removed from the file and that any derivative work contains  ////
2064
//// the original copyright notice and the associated disclaimer. ////
2065
////                                                              ////
2066
//// This source file is free software; you can redistribute it   ////
2067
//// and/or modify it under the terms of the GNU Lesser General   ////
2068
//// Public License as published by the Free Software Foundation; ////
2069
//// either version 2.1 of the License, or (at your option) any   ////
2070
//// later version.                                               ////
2071
////                                                              ////
2072
//// This source is distributed in the hope that it will be       ////
2073
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2074
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2075
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2076
//// details.                                                     ////
2077
////                                                              ////
2078
//// You should have received a copy of the GNU Lesser General    ////
2079
//// Public License along with this source; if not, download it   ////
2080
//// from http://www.opencores.org/lgpl.shtml                     ////
2081
////                                                              ////
2082
//////////////////////////////////////////////////////////////////////
2083
 
2084
// LFSR counter
2085
module vl_cnt_lfsr_ce_clear_q ( clear, cke, q, rst, clk);
2086
 
2087
   parameter length = 4;
2088
   input clear;
2089
   input cke;
2090
   output [length:1] q;
2091
   input rst;
2092
   input clk;
2093
 
2094
   parameter clear_value = 0;
2095
   parameter set_value = 1;
2096
   parameter wrap_value = 8;
2097
   parameter level1_value = 15;
2098
 
2099
   reg  [length:1] qi;
2100
   reg lfsr_fb;
2101
   wire [length:1] q_next;
2102
   reg [32:1] polynom;
2103
   integer i;
2104
 
2105
   always @ (qi)
2106
   begin
2107
        case (length)
2108
         2: polynom = 32'b11;                               // 0x3
2109
         3: polynom = 32'b110;                              // 0x6
2110
         4: polynom = 32'b1100;                             // 0xC
2111
         5: polynom = 32'b10100;                            // 0x14
2112
         6: polynom = 32'b110000;                           // 0x30
2113
         7: polynom = 32'b1100000;                          // 0x60
2114
         8: polynom = 32'b10111000;                         // 0xb8
2115
         9: polynom = 32'b100010000;                        // 0x110
2116
        10: polynom = 32'b1001000000;                       // 0x240
2117
        11: polynom = 32'b10100000000;                      // 0x500
2118
        12: polynom = 32'b100000101001;                     // 0x829
2119
        13: polynom = 32'b1000000001100;                    // 0x100C
2120
        14: polynom = 32'b10000000010101;                   // 0x2015
2121
        15: polynom = 32'b110000000000000;                  // 0x6000
2122
        16: polynom = 32'b1101000000001000;                 // 0xD008
2123
        17: polynom = 32'b10010000000000000;                // 0x12000
2124
        18: polynom = 32'b100000010000000000;               // 0x20400
2125
        19: polynom = 32'b1000000000000100011;              // 0x40023
2126
        20: polynom = 32'b10000010000000000000;             // 0x82000
2127
        21: polynom = 32'b101000000000000000000;            // 0x140000
2128
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2129
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2130
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2131
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2132
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2133
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2134
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2135
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2136
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2137
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2138
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2139
        default: polynom = 32'b0;
2140
        endcase
2141
        lfsr_fb = qi[length];
2142
        for (i=length-1; i>=1; i=i-1) begin
2143
            if (polynom[i])
2144
                lfsr_fb = lfsr_fb  ~^ qi[i];
2145
        end
2146
    end
2147
   assign q_next =  clear ? {length{1'b0}} :(qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2148
 
2149
   always @ (posedge clk or posedge rst)
2150
     if (rst)
2151
       qi <= {length{1'b0}};
2152
     else
2153
     if (cke)
2154
       qi <= q_next;
2155
 
2156
   assign q = qi;
2157
 
2158
endmodule
2159
//////////////////////////////////////////////////////////////////////
2160
////                                                              ////
2161
////  Versatile counter                                           ////
2162
////                                                              ////
2163
////  Description                                                 ////
2164
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2165
////  counter                                                     ////
2166
////                                                              ////
2167
////  To Do:                                                      ////
2168
////   - add LFSR with more taps                                  ////
2169
////                                                              ////
2170
////  Author(s):                                                  ////
2171
////      - Michael Unneback, unneback@opencores.org              ////
2172
////        ORSoC AB                                              ////
2173
////                                                              ////
2174
//////////////////////////////////////////////////////////////////////
2175
////                                                              ////
2176
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2177
////                                                              ////
2178
//// This source file may be used and distributed without         ////
2179
//// restriction provided that this copyright statement is not    ////
2180
//// removed from the file and that any derivative work contains  ////
2181
//// the original copyright notice and the associated disclaimer. ////
2182
////                                                              ////
2183
//// This source file is free software; you can redistribute it   ////
2184
//// and/or modify it under the terms of the GNU Lesser General   ////
2185
//// Public License as published by the Free Software Foundation; ////
2186
//// either version 2.1 of the License, or (at your option) any   ////
2187
//// later version.                                               ////
2188
////                                                              ////
2189
//// This source is distributed in the hope that it will be       ////
2190
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2191
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2192
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2193
//// details.                                                     ////
2194
////                                                              ////
2195
//// You should have received a copy of the GNU Lesser General    ////
2196
//// Public License along with this source; if not, download it   ////
2197
//// from http://www.opencores.org/lgpl.shtml                     ////
2198
////                                                              ////
2199
//////////////////////////////////////////////////////////////////////
2200
 
2201
// LFSR counter
2202 22 unneback
module vl_cnt_lfsr_ce_q_zq ( cke, q, zq, rst, clk);
2203
 
2204
   parameter length = 4;
2205
   input cke;
2206
   output [length:1] q;
2207
   output reg zq;
2208
   input rst;
2209
   input clk;
2210
 
2211
   parameter clear_value = 0;
2212
   parameter set_value = 1;
2213
   parameter wrap_value = 8;
2214
   parameter level1_value = 15;
2215
 
2216
   reg  [length:1] qi;
2217
   reg lfsr_fb;
2218
   wire [length:1] q_next;
2219
   reg [32:1] polynom;
2220
   integer i;
2221
 
2222
   always @ (qi)
2223
   begin
2224
        case (length)
2225
         2: polynom = 32'b11;                               // 0x3
2226
         3: polynom = 32'b110;                              // 0x6
2227
         4: polynom = 32'b1100;                             // 0xC
2228
         5: polynom = 32'b10100;                            // 0x14
2229
         6: polynom = 32'b110000;                           // 0x30
2230
         7: polynom = 32'b1100000;                          // 0x60
2231
         8: polynom = 32'b10111000;                         // 0xb8
2232
         9: polynom = 32'b100010000;                        // 0x110
2233
        10: polynom = 32'b1001000000;                       // 0x240
2234
        11: polynom = 32'b10100000000;                      // 0x500
2235
        12: polynom = 32'b100000101001;                     // 0x829
2236
        13: polynom = 32'b1000000001100;                    // 0x100C
2237
        14: polynom = 32'b10000000010101;                   // 0x2015
2238
        15: polynom = 32'b110000000000000;                  // 0x6000
2239
        16: polynom = 32'b1101000000001000;                 // 0xD008
2240
        17: polynom = 32'b10010000000000000;                // 0x12000
2241
        18: polynom = 32'b100000010000000000;               // 0x20400
2242
        19: polynom = 32'b1000000000000100011;              // 0x40023
2243
        20: polynom = 32'b10000010000000000000;             // 0x82000
2244
        21: polynom = 32'b101000000000000000000;            // 0x140000
2245
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2246
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2247
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2248
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2249
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2250
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2251
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2252
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2253
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2254
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2255
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2256
        default: polynom = 32'b0;
2257
        endcase
2258
        lfsr_fb = qi[length];
2259
        for (i=length-1; i>=1; i=i-1) begin
2260
            if (polynom[i])
2261
                lfsr_fb = lfsr_fb  ~^ qi[i];
2262
        end
2263
    end
2264
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2265
 
2266
   always @ (posedge clk or posedge rst)
2267
     if (rst)
2268
       qi <= {length{1'b0}};
2269
     else
2270
     if (cke)
2271
       qi <= q_next;
2272
 
2273
   assign q = qi;
2274
 
2275
 
2276
   always @ (posedge clk or posedge rst)
2277
     if (rst)
2278
       zq <= 1'b1;
2279
     else
2280
     if (cke)
2281
       zq <= q_next == {length{1'b0}};
2282
endmodule
2283
//////////////////////////////////////////////////////////////////////
2284
////                                                              ////
2285
////  Versatile counter                                           ////
2286
////                                                              ////
2287
////  Description                                                 ////
2288
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2289
////  counter                                                     ////
2290
////                                                              ////
2291
////  To Do:                                                      ////
2292
////   - add LFSR with more taps                                  ////
2293
////                                                              ////
2294
////  Author(s):                                                  ////
2295
////      - Michael Unneback, unneback@opencores.org              ////
2296
////        ORSoC AB                                              ////
2297
////                                                              ////
2298
//////////////////////////////////////////////////////////////////////
2299
////                                                              ////
2300
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2301
////                                                              ////
2302
//// This source file may be used and distributed without         ////
2303
//// restriction provided that this copyright statement is not    ////
2304
//// removed from the file and that any derivative work contains  ////
2305
//// the original copyright notice and the associated disclaimer. ////
2306
////                                                              ////
2307
//// This source file is free software; you can redistribute it   ////
2308
//// and/or modify it under the terms of the GNU Lesser General   ////
2309
//// Public License as published by the Free Software Foundation; ////
2310
//// either version 2.1 of the License, or (at your option) any   ////
2311
//// later version.                                               ////
2312
////                                                              ////
2313
//// This source is distributed in the hope that it will be       ////
2314
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2315
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2316
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2317
//// details.                                                     ////
2318
////                                                              ////
2319
//// You should have received a copy of the GNU Lesser General    ////
2320
//// Public License along with this source; if not, download it   ////
2321
//// from http://www.opencores.org/lgpl.shtml                     ////
2322
////                                                              ////
2323
//////////////////////////////////////////////////////////////////////
2324 6 unneback
 
2325
// LFSR counter
2326 18 unneback
module vl_cnt_lfsr_ce_rew_l1 ( cke, rew, level1, rst, clk);
2327 6 unneback
 
2328
   parameter length = 4;
2329
   input cke;
2330
   input rew;
2331
   output reg level1;
2332
   input rst;
2333
   input clk;
2334
 
2335
   parameter clear_value = 0;
2336
   parameter set_value = 1;
2337
   parameter wrap_value = 8;
2338
   parameter level1_value = 15;
2339
 
2340 29 unneback
   wire clear;
2341 30 unneback
   assign clear = 1'b0;
2342 6 unneback
   reg  [length:1] qi;
2343
   reg lfsr_fb, lfsr_fb_rew;
2344
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2345
   reg [32:1] polynom_rew;
2346
   integer j;
2347
   reg [32:1] polynom;
2348
   integer i;
2349
 
2350
   always @ (qi)
2351
   begin
2352
        case (length)
2353
         2: polynom = 32'b11;                               // 0x3
2354
         3: polynom = 32'b110;                              // 0x6
2355
         4: polynom = 32'b1100;                             // 0xC
2356
         5: polynom = 32'b10100;                            // 0x14
2357
         6: polynom = 32'b110000;                           // 0x30
2358
         7: polynom = 32'b1100000;                          // 0x60
2359
         8: polynom = 32'b10111000;                         // 0xb8
2360
         9: polynom = 32'b100010000;                        // 0x110
2361
        10: polynom = 32'b1001000000;                       // 0x240
2362
        11: polynom = 32'b10100000000;                      // 0x500
2363
        12: polynom = 32'b100000101001;                     // 0x829
2364
        13: polynom = 32'b1000000001100;                    // 0x100C
2365
        14: polynom = 32'b10000000010101;                   // 0x2015
2366
        15: polynom = 32'b110000000000000;                  // 0x6000
2367
        16: polynom = 32'b1101000000001000;                 // 0xD008
2368
        17: polynom = 32'b10010000000000000;                // 0x12000
2369
        18: polynom = 32'b100000010000000000;               // 0x20400
2370
        19: polynom = 32'b1000000000000100011;              // 0x40023
2371
        20: polynom = 32'b10000010000000000000;             // 0x82000
2372
        21: polynom = 32'b101000000000000000000;            // 0x140000
2373
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2374
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2375
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2376
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2377
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2378
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2379
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2380
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2381
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2382
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2383
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2384
        default: polynom = 32'b0;
2385
        endcase
2386
        lfsr_fb = qi[length];
2387
        for (i=length-1; i>=1; i=i-1) begin
2388
            if (polynom[i])
2389
                lfsr_fb = lfsr_fb  ~^ qi[i];
2390
        end
2391
    end
2392
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2393
   always @ (qi)
2394
   begin
2395
        case (length)
2396
         2: polynom_rew = 32'b11;
2397
         3: polynom_rew = 32'b110;
2398
         4: polynom_rew = 32'b1100;
2399
         5: polynom_rew = 32'b10100;
2400
         6: polynom_rew = 32'b110000;
2401
         7: polynom_rew = 32'b1100000;
2402
         8: polynom_rew = 32'b10111000;
2403
         9: polynom_rew = 32'b100010000;
2404
        10: polynom_rew = 32'b1001000000;
2405
        11: polynom_rew = 32'b10100000000;
2406
        12: polynom_rew = 32'b100000101001;
2407
        13: polynom_rew = 32'b1000000001100;
2408
        14: polynom_rew = 32'b10000000010101;
2409
        15: polynom_rew = 32'b110000000000000;
2410
        16: polynom_rew = 32'b1101000000001000;
2411
        17: polynom_rew = 32'b10010000000000000;
2412
        18: polynom_rew = 32'b100000010000000000;
2413
        19: polynom_rew = 32'b1000000000000100011;
2414
        20: polynom_rew = 32'b10000010000000000000;
2415
        21: polynom_rew = 32'b101000000000000000000;
2416
        22: polynom_rew = 32'b1100000000000000000000;
2417
        23: polynom_rew = 32'b10000100000000000000000;
2418
        24: polynom_rew = 32'b111000010000000000000000;
2419
        25: polynom_rew = 32'b1001000000000000000000000;
2420
        26: polynom_rew = 32'b10000000000000000000100011;
2421
        27: polynom_rew = 32'b100000000000000000000010011;
2422
        28: polynom_rew = 32'b1100100000000000000000000000;
2423
        29: polynom_rew = 32'b10100000000000000000000000000;
2424
        30: polynom_rew = 32'b100000000000000000000000101001;
2425
        31: polynom_rew = 32'b1001000000000000000000000000000;
2426
        32: polynom_rew = 32'b10000000001000000000000000000011;
2427
        default: polynom_rew = 32'b0;
2428
        endcase
2429
        // rotate left
2430
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
2431
        lfsr_fb_rew = qi[length];
2432
        for (i=length-1; i>=1; i=i-1) begin
2433
            if (polynom_rew[i])
2434
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
2435
        end
2436
    end
2437
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
2438
   assign q_next = rew ? q_next_rew : q_next_fw;
2439
 
2440
   always @ (posedge clk or posedge rst)
2441
     if (rst)
2442
       qi <= {length{1'b0}};
2443
     else
2444
     if (cke)
2445
       qi <= q_next;
2446
 
2447
 
2448
 
2449
    always @ (posedge clk or posedge rst)
2450
    if (rst)
2451
        level1 <= 1'b0;
2452
    else
2453
    if (cke)
2454 29 unneback
    if (clear)
2455
        level1 <= 1'b0;
2456
    else if (q_next == level1_value)
2457 6 unneback
        level1 <= 1'b1;
2458
    else if (qi == level1_value & rew)
2459
        level1 <= 1'b0;
2460
endmodule
2461
//////////////////////////////////////////////////////////////////////
2462
////                                                              ////
2463
////  Versatile counter                                           ////
2464
////                                                              ////
2465
////  Description                                                 ////
2466
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2467
////  counter                                                     ////
2468
////                                                              ////
2469
////  To Do:                                                      ////
2470
////   - add LFSR with more taps                                  ////
2471
////                                                              ////
2472
////  Author(s):                                                  ////
2473
////      - Michael Unneback, unneback@opencores.org              ////
2474
////        ORSoC AB                                              ////
2475
////                                                              ////
2476
//////////////////////////////////////////////////////////////////////
2477
////                                                              ////
2478
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2479
////                                                              ////
2480
//// This source file may be used and distributed without         ////
2481
//// restriction provided that this copyright statement is not    ////
2482
//// removed from the file and that any derivative work contains  ////
2483
//// the original copyright notice and the associated disclaimer. ////
2484
////                                                              ////
2485
//// This source file is free software; you can redistribute it   ////
2486
//// and/or modify it under the terms of the GNU Lesser General   ////
2487
//// Public License as published by the Free Software Foundation; ////
2488
//// either version 2.1 of the License, or (at your option) any   ////
2489
//// later version.                                               ////
2490
////                                                              ////
2491
//// This source is distributed in the hope that it will be       ////
2492
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2493
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2494
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2495
//// details.                                                     ////
2496
////                                                              ////
2497
//// You should have received a copy of the GNU Lesser General    ////
2498
//// Public License along with this source; if not, download it   ////
2499
//// from http://www.opencores.org/lgpl.shtml                     ////
2500
////                                                              ////
2501
//////////////////////////////////////////////////////////////////////
2502
 
2503
// GRAY counter
2504 18 unneback
module vl_cnt_gray ( q, rst, clk);
2505 6 unneback
 
2506
   parameter length = 4;
2507
   output reg [length:1] q;
2508
   input rst;
2509
   input clk;
2510
 
2511
   parameter clear_value = 0;
2512
   parameter set_value = 1;
2513
   parameter wrap_value = 8;
2514
   parameter level1_value = 15;
2515
 
2516
   reg  [length:1] qi;
2517
   wire [length:1] q_next;
2518
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2519
 
2520
   always @ (posedge clk or posedge rst)
2521
     if (rst)
2522
       qi <= {length{1'b0}};
2523
     else
2524
       qi <= q_next;
2525
 
2526
   always @ (posedge clk or posedge rst)
2527
     if (rst)
2528
       q <= {length{1'b0}};
2529
     else
2530
         q <= (q_next>>1) ^ q_next;
2531
 
2532
endmodule
2533
//////////////////////////////////////////////////////////////////////
2534
////                                                              ////
2535
////  Versatile counter                                           ////
2536
////                                                              ////
2537
////  Description                                                 ////
2538
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2539
////  counter                                                     ////
2540
////                                                              ////
2541
////  To Do:                                                      ////
2542
////   - add LFSR with more taps                                  ////
2543
////                                                              ////
2544
////  Author(s):                                                  ////
2545
////      - Michael Unneback, unneback@opencores.org              ////
2546
////        ORSoC AB                                              ////
2547
////                                                              ////
2548
//////////////////////////////////////////////////////////////////////
2549
////                                                              ////
2550
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2551
////                                                              ////
2552
//// This source file may be used and distributed without         ////
2553
//// restriction provided that this copyright statement is not    ////
2554
//// removed from the file and that any derivative work contains  ////
2555
//// the original copyright notice and the associated disclaimer. ////
2556
////                                                              ////
2557
//// This source file is free software; you can redistribute it   ////
2558
//// and/or modify it under the terms of the GNU Lesser General   ////
2559
//// Public License as published by the Free Software Foundation; ////
2560
//// either version 2.1 of the License, or (at your option) any   ////
2561
//// later version.                                               ////
2562
////                                                              ////
2563
//// This source is distributed in the hope that it will be       ////
2564
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2565
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2566
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2567
//// details.                                                     ////
2568
////                                                              ////
2569
//// You should have received a copy of the GNU Lesser General    ////
2570
//// Public License along with this source; if not, download it   ////
2571
//// from http://www.opencores.org/lgpl.shtml                     ////
2572
////                                                              ////
2573
//////////////////////////////////////////////////////////////////////
2574
 
2575
// GRAY counter
2576 18 unneback
module vl_cnt_gray_ce ( cke, q, rst, clk);
2577 6 unneback
 
2578
   parameter length = 4;
2579
   input cke;
2580
   output reg [length:1] q;
2581
   input rst;
2582
   input clk;
2583
 
2584
   parameter clear_value = 0;
2585
   parameter set_value = 1;
2586
   parameter wrap_value = 8;
2587
   parameter level1_value = 15;
2588
 
2589
   reg  [length:1] qi;
2590
   wire [length:1] q_next;
2591
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2592
 
2593
   always @ (posedge clk or posedge rst)
2594
     if (rst)
2595
       qi <= {length{1'b0}};
2596
     else
2597
     if (cke)
2598
       qi <= q_next;
2599
 
2600
   always @ (posedge clk or posedge rst)
2601
     if (rst)
2602
       q <= {length{1'b0}};
2603
     else
2604
       if (cke)
2605
         q <= (q_next>>1) ^ q_next;
2606
 
2607
endmodule
2608
//////////////////////////////////////////////////////////////////////
2609
////                                                              ////
2610
////  Versatile counter                                           ////
2611
////                                                              ////
2612
////  Description                                                 ////
2613
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2614
////  counter                                                     ////
2615
////                                                              ////
2616
////  To Do:                                                      ////
2617
////   - add LFSR with more taps                                  ////
2618
////                                                              ////
2619
////  Author(s):                                                  ////
2620
////      - Michael Unneback, unneback@opencores.org              ////
2621
////        ORSoC AB                                              ////
2622
////                                                              ////
2623
//////////////////////////////////////////////////////////////////////
2624
////                                                              ////
2625
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2626
////                                                              ////
2627
//// This source file may be used and distributed without         ////
2628
//// restriction provided that this copyright statement is not    ////
2629
//// removed from the file and that any derivative work contains  ////
2630
//// the original copyright notice and the associated disclaimer. ////
2631
////                                                              ////
2632
//// This source file is free software; you can redistribute it   ////
2633
//// and/or modify it under the terms of the GNU Lesser General   ////
2634
//// Public License as published by the Free Software Foundation; ////
2635
//// either version 2.1 of the License, or (at your option) any   ////
2636
//// later version.                                               ////
2637
////                                                              ////
2638
//// This source is distributed in the hope that it will be       ////
2639
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2640
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2641
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2642
//// details.                                                     ////
2643
////                                                              ////
2644
//// You should have received a copy of the GNU Lesser General    ////
2645
//// Public License along with this source; if not, download it   ////
2646
//// from http://www.opencores.org/lgpl.shtml                     ////
2647
////                                                              ////
2648
//////////////////////////////////////////////////////////////////////
2649
 
2650
// GRAY counter
2651 18 unneback
module vl_cnt_gray_ce_bin ( cke, q, q_bin, rst, clk);
2652 6 unneback
 
2653
   parameter length = 4;
2654
   input cke;
2655
   output reg [length:1] q;
2656
   output [length:1] q_bin;
2657
   input rst;
2658
   input clk;
2659
 
2660
   parameter clear_value = 0;
2661
   parameter set_value = 1;
2662
   parameter wrap_value = 8;
2663
   parameter level1_value = 15;
2664
 
2665
   reg  [length:1] qi;
2666
   wire [length:1] q_next;
2667
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2668
 
2669
   always @ (posedge clk or posedge rst)
2670
     if (rst)
2671
       qi <= {length{1'b0}};
2672
     else
2673
     if (cke)
2674
       qi <= q_next;
2675
 
2676
   always @ (posedge clk or posedge rst)
2677
     if (rst)
2678
       q <= {length{1'b0}};
2679
     else
2680
       if (cke)
2681
         q <= (q_next>>1) ^ q_next;
2682
 
2683
   assign q_bin = qi;
2684
 
2685
endmodule
2686
//////////////////////////////////////////////////////////////////////
2687
////                                                              ////
2688
////  Versatile library, counters                                 ////
2689
////                                                              ////
2690
////  Description                                                 ////
2691
////  counters                                                    ////
2692
////                                                              ////
2693
////                                                              ////
2694
////  To Do:                                                      ////
2695
////   - add more counters                                        ////
2696
////                                                              ////
2697
////  Author(s):                                                  ////
2698
////      - Michael Unneback, unneback@opencores.org              ////
2699
////        ORSoC AB                                              ////
2700
////                                                              ////
2701
//////////////////////////////////////////////////////////////////////
2702
////                                                              ////
2703
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2704
////                                                              ////
2705
//// This source file may be used and distributed without         ////
2706
//// restriction provided that this copyright statement is not    ////
2707
//// removed from the file and that any derivative work contains  ////
2708
//// the original copyright notice and the associated disclaimer. ////
2709
////                                                              ////
2710
//// This source file is free software; you can redistribute it   ////
2711
//// and/or modify it under the terms of the GNU Lesser General   ////
2712
//// Public License as published by the Free Software Foundation; ////
2713
//// either version 2.1 of the License, or (at your option) any   ////
2714
//// later version.                                               ////
2715
////                                                              ////
2716
//// This source is distributed in the hope that it will be       ////
2717
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2718
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2719
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2720
//// details.                                                     ////
2721
////                                                              ////
2722
//// You should have received a copy of the GNU Lesser General    ////
2723
//// Public License along with this source; if not, download it   ////
2724
//// from http://www.opencores.org/lgpl.shtml                     ////
2725
////                                                              ////
2726
//////////////////////////////////////////////////////////////////////
2727
 
2728 18 unneback
module vl_cnt_shreg_wrap ( q, rst, clk);
2729 6 unneback
 
2730
   parameter length = 4;
2731
   output reg [0:length-1] q;
2732
   input rst;
2733
   input clk;
2734
 
2735
    always @ (posedge clk or posedge rst)
2736
    if (rst)
2737
        q <= {1'b1,{length-1{1'b0}}};
2738
    else
2739
        q <= {q[length-1],q[0:length-2]};
2740
 
2741
endmodule
2742
 
2743 18 unneback
module vl_cnt_shreg_ce_wrap ( cke, q, rst, clk);
2744 6 unneback
 
2745
   parameter length = 4;
2746
   input cke;
2747
   output reg [0:length-1] q;
2748
   input rst;
2749
   input clk;
2750
 
2751
    always @ (posedge clk or posedge rst)
2752
    if (rst)
2753
        q <= {1'b1,{length-1{1'b0}}};
2754
    else
2755
        if (cke)
2756
            q <= {q[length-1],q[0:length-2]};
2757
 
2758
endmodule
2759
 
2760 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
2761 6 unneback
 
2762
   parameter length = 4;
2763
   input cke, clear;
2764
   output reg [0:length-1] q;
2765
   input rst;
2766
   input clk;
2767
 
2768
    always @ (posedge clk or posedge rst)
2769
    if (rst)
2770
        q <= {1'b1,{length-1{1'b0}}};
2771
    else
2772
        if (cke)
2773
            if (clear)
2774
                q <= {1'b1,{length-1{1'b0}}};
2775
            else
2776
                q <= q >> 1;
2777
 
2778
endmodule
2779
 
2780 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
2781 6 unneback
 
2782
   parameter length = 4;
2783
   input cke, clear;
2784
   output reg [0:length-1] q;
2785
   input rst;
2786
   input clk;
2787
 
2788
    always @ (posedge clk or posedge rst)
2789
    if (rst)
2790
        q <= {1'b1,{length-1{1'b0}}};
2791
    else
2792
        if (cke)
2793
            if (clear)
2794
                q <= {1'b1,{length-1{1'b0}}};
2795
            else
2796
            q <= {q[length-1],q[0:length-2]};
2797
 
2798
endmodule
2799
//////////////////////////////////////////////////////////////////////
2800
////                                                              ////
2801
////  Versatile library, memories                                 ////
2802
////                                                              ////
2803
////  Description                                                 ////
2804
////  memories                                                    ////
2805
////                                                              ////
2806
////                                                              ////
2807
////  To Do:                                                      ////
2808
////   - add more memory types                                    ////
2809
////                                                              ////
2810
////  Author(s):                                                  ////
2811
////      - Michael Unneback, unneback@opencores.org              ////
2812
////        ORSoC AB                                              ////
2813
////                                                              ////
2814
//////////////////////////////////////////////////////////////////////
2815
////                                                              ////
2816
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2817
////                                                              ////
2818
//// This source file may be used and distributed without         ////
2819
//// restriction provided that this copyright statement is not    ////
2820
//// removed from the file and that any derivative work contains  ////
2821
//// the original copyright notice and the associated disclaimer. ////
2822
////                                                              ////
2823
//// This source file is free software; you can redistribute it   ////
2824
//// and/or modify it under the terms of the GNU Lesser General   ////
2825
//// Public License as published by the Free Software Foundation; ////
2826
//// either version 2.1 of the License, or (at your option) any   ////
2827
//// later version.                                               ////
2828
////                                                              ////
2829
//// This source is distributed in the hope that it will be       ////
2830
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2831
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2832
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2833
//// details.                                                     ////
2834
////                                                              ////
2835
//// You should have received a copy of the GNU Lesser General    ////
2836
//// Public License along with this source; if not, download it   ////
2837
//// from http://www.opencores.org/lgpl.shtml                     ////
2838
////                                                              ////
2839
//////////////////////////////////////////////////////////////////////
2840
 
2841
/// ROM
2842
 
2843 7 unneback
module vl_rom_init ( adr, q, clk);
2844
   parameter data_width = 32;
2845
   parameter addr_width = 8;
2846
   input [(addr_width-1):0]       adr;
2847
   output reg [(data_width-1):0] q;
2848
   input                         clk;
2849
   reg [data_width-1:0] rom [(1<<addr_width)-1:0];
2850
   parameter memory_file = "vl_rom.vmem";
2851
   initial
2852
     begin
2853
        $readmemh(memory_file, rom);
2854
     end
2855
 
2856
   always @ (posedge clk)
2857
     q <= rom[adr];
2858 6 unneback
 
2859 7 unneback
endmodule
2860
 
2861 14 unneback
/*
2862 7 unneback
module vl_rom ( adr, q, clk);
2863
 
2864 6 unneback
parameter data_width = 32;
2865
parameter addr_width = 4;
2866
 
2867
parameter [0:1>>addr_width-1] data [data_width-1:0] = {
2868
    {32'h18000000},
2869
    {32'hA8200000},
2870
    {32'hA8200000},
2871
    {32'hA8200000},
2872
    {32'h44003000},
2873
    {32'h15000000},
2874
    {32'h15000000},
2875
    {32'h15000000},
2876
    {32'h15000000},
2877
    {32'h15000000},
2878
    {32'h15000000},
2879
    {32'h15000000},
2880
    {32'h15000000},
2881
    {32'h15000000},
2882
    {32'h15000000},
2883
    {32'h15000000}};
2884
 
2885 7 unneback
input [addr_width-1:0] adr;
2886 6 unneback
output reg [data_width-1:0] q;
2887
input clk;
2888
 
2889
always @ (posedge clk)
2890 7 unneback
    q <= data[adr];
2891 6 unneback
 
2892
endmodule
2893 14 unneback
*/
2894 6 unneback
// Single port RAM
2895
 
2896
module vl_ram ( d, adr, we, q, clk);
2897
   parameter data_width = 32;
2898
   parameter addr_width = 8;
2899
   input [(data_width-1):0]      d;
2900
   input [(addr_width-1):0]       adr;
2901
   input                         we;
2902 7 unneback
   output reg [(data_width-1):0] q;
2903 6 unneback
   input                         clk;
2904
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
2905 7 unneback
   parameter init = 0;
2906
   parameter memory_file = "vl_ram.vmem";
2907
   generate if (init) begin : init_mem
2908
   initial
2909
     begin
2910
        $readmemh(memory_file, ram);
2911
     end
2912
   end
2913
   endgenerate
2914
 
2915 6 unneback
   always @ (posedge clk)
2916
   begin
2917
   if (we)
2918
     ram[adr] <= d;
2919
   q <= ram[adr];
2920
   end
2921
 
2922
endmodule
2923
 
2924 7 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
2925
   parameter data_width = 32;
2926
   parameter addr_width = 8;
2927
   input [(data_width-1):0]      d;
2928
   input [(addr_width-1):0]       adr;
2929
   input [(addr_width/4)-1:0]    be;
2930
   input                         we;
2931
   output reg [(data_width-1):0] q;
2932
   input                         clk;
2933
 
2934
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
2935
 
2936
   parameter init = 0;
2937
   parameter memory_file = "vl_ram.vmem";
2938
   generate if (init) begin : init_mem
2939
   initial
2940
     begin
2941
        $readmemh(memory_file, ram);
2942
     end
2943
   end
2944
   endgenerate
2945
 
2946
   genvar i;
2947
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_ram
2948
      always @ (posedge clk)
2949
      if (we & be[i])
2950
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
2951
   end
2952
   endgenerate
2953
 
2954
   always @ (posedge clk)
2955
      q <= ram[adr];
2956
 
2957
endmodule
2958
 
2959
 
2960 6 unneback
// Dual port RAM
2961
 
2962
// ACTEL FPGA should not use logic to handle rw collision
2963
`ifdef ACTEL
2964
        `define SYN /*synthesis syn_ramstyle = "no_rw_check"*/
2965
`else
2966
        `define SYN
2967
`endif
2968
 
2969 7 unneback
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
2970 6 unneback
   parameter data_width = 32;
2971
   parameter addr_width = 8;
2972
   input [(data_width-1):0]      d_a;
2973
   input [(addr_width-1):0]       adr_a;
2974
   input [(addr_width-1):0]       adr_b;
2975
   input                         we_a;
2976
   output [(data_width-1):0]      q_b;
2977
   input                         clk_a, clk_b;
2978
   reg [(addr_width-1):0]         adr_b_reg;
2979
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
2980 7 unneback
 
2981
   parameter init = 0;
2982
   parameter memory_file = "vl_ram.vmem";
2983
   generate if (init) begin : init_mem
2984
   initial
2985
     begin
2986
        $readmemh(memory_file, ram);
2987
     end
2988
   end
2989
   endgenerate
2990
 
2991 6 unneback
   always @ (posedge clk_a)
2992
   if (we_a)
2993
     ram[adr_a] <= d_a;
2994
   always @ (posedge clk_b)
2995
   adr_b_reg <= adr_b;
2996
   assign q_b = ram[adr_b_reg];
2997
endmodule
2998
 
2999 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
3000 6 unneback
   parameter data_width = 32;
3001
   parameter addr_width = 8;
3002
   input [(data_width-1):0]      d_a;
3003
   input [(addr_width-1):0]       adr_a;
3004
   input [(addr_width-1):0]       adr_b;
3005
   input                         we_a;
3006
   output [(data_width-1):0]      q_b;
3007
   output reg [(data_width-1):0] q_a;
3008
   input                         clk_a, clk_b;
3009
   reg [(data_width-1):0]         q_b;
3010
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
3011 7 unneback
 
3012
   parameter init = 0;
3013
   parameter memory_file = "vl_ram.vmem";
3014
   generate if (init) begin : init_mem
3015
   initial
3016
     begin
3017
        $readmemh(memory_file, ram);
3018
     end
3019
   end
3020
   endgenerate
3021
 
3022 6 unneback
   always @ (posedge clk_a)
3023
     begin
3024
        q_a <= ram[adr_a];
3025
        if (we_a)
3026
             ram[adr_a] <= d_a;
3027
     end
3028
   always @ (posedge clk_b)
3029
          q_b <= ram[adr_b];
3030
endmodule
3031
 
3032 7 unneback
module vl_dpram_2r2w ( d_a, q_a, adr_a, we_a, clk_a, d_b, q_b, adr_b, we_b, clk_b );
3033 6 unneback
   parameter data_width = 32;
3034
   parameter addr_width = 8;
3035
   input [(data_width-1):0]      d_a;
3036
   input [(addr_width-1):0]       adr_a;
3037
   input [(addr_width-1):0]       adr_b;
3038
   input                         we_a;
3039
   output [(data_width-1):0]      q_b;
3040
   input [(data_width-1):0]       d_b;
3041
   output reg [(data_width-1):0] q_a;
3042
   input                         we_b;
3043
   input                         clk_a, clk_b;
3044
   reg [(data_width-1):0]         q_b;
3045
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
3046 7 unneback
 
3047
   parameter init = 0;
3048
   parameter memory_file = "vl_ram.vmem";
3049
   generate if (init) begin : init_mem
3050
   initial
3051
     begin
3052
        $readmemh(memory_file, ram);
3053
     end
3054
   end
3055
   endgenerate
3056
 
3057 6 unneback
   always @ (posedge clk_a)
3058
     begin
3059
        q_a <= ram[adr_a];
3060
        if (we_a)
3061
             ram[adr_a] <= d_a;
3062
     end
3063
   always @ (posedge clk_b)
3064
     begin
3065
        q_b <= ram[adr_b];
3066
        if (we_b)
3067
          ram[adr_b] <= d_b;
3068
     end
3069
endmodule
3070
 
3071
// Content addresable memory, CAM
3072
 
3073
// FIFO
3074 25 unneback
module vl_fifo_1r1w_fill_level_sync (
3075
    d, wr, fifo_full,
3076
    q, rd, fifo_empty,
3077
    fill_level,
3078
    clk, rst
3079
    );
3080
 
3081
parameter data_width = 18;
3082
parameter addr_width = 4;
3083 6 unneback
 
3084 25 unneback
// write side
3085
input  [data_width-1:0] d;
3086
input                   wr;
3087
output                  fifo_full;
3088
// read side
3089
output [data_width-1:0] q;
3090
input                   rd;
3091
output                  fifo_empty;
3092
// common
3093
output [addr_width:0]   fill_level;
3094
input rst, clk;
3095
 
3096
wire [addr_width:1] wadr, radr;
3097
 
3098
vl_cnt_bin_ce
3099
    # ( .length(addr_width))
3100
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
3101
 
3102
vl_cnt_bin_ce
3103
    # (.length(addr_width))
3104
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
3105
 
3106
vl_dpram_1r1w
3107
    # (.data_width(data_width), .addr_width(addr_width))
3108
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
3109
 
3110 31 unneback
vl_cnt_bin_ce_rew_q_zq_l1
3111 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
3112 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
3113
 
3114
endmodule
3115
 
3116 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
3117
// RAM is supposed to be larger than the two FIFOs
3118
// LFSR counters used adr pointers
3119
module vl_fifo_2r2w_sync_simplex (
3120
    // a side
3121
    a_d, a_wr, a_fifo_full,
3122
    a_q, a_rd, a_fifo_empty,
3123
    a_fill_level,
3124
    // b side
3125
    b_d, b_wr, b_fifo_full,
3126
    b_q, b_rd, b_fifo_empty,
3127
    b_fill_level,
3128
    // common
3129
    clk, rst
3130
    );
3131
parameter data_width = 8;
3132
parameter addr_width = 5;
3133
parameter fifo_full_level = (1<<addr_width)-1;
3134
 
3135
// a side
3136
input  [data_width-1:0] a_d;
3137
input                   a_wr;
3138
output                  a_fifo_full;
3139
output [data_width-1:0] a_q;
3140
input                   a_rd;
3141
output                  a_fifo_empty;
3142
output [addr_width-1:0] a_fill_level;
3143
 
3144
// b side
3145
input  [data_width-1:0] b_d;
3146
input                   b_wr;
3147
output                  b_fifo_full;
3148
output [data_width-1:0] b_q;
3149
input                   b_rd;
3150
output                  b_fifo_empty;
3151
output [addr_width-1:0] b_fill_level;
3152
 
3153
input                   clk;
3154
input                   rst;
3155
 
3156
// adr_gen
3157
wire [addr_width:1] a_wadr, a_radr;
3158
wire [addr_width:1] b_wadr, b_radr;
3159
// dpram
3160
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
3161
 
3162
vl_cnt_lfsr_ce
3163
    # ( .length(addr_width))
3164
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
3165
 
3166
vl_cnt_lfsr_ce
3167
    # (.length(addr_width))
3168
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
3169
 
3170
vl_cnt_lfsr_ce
3171
    # ( .length(addr_width))
3172
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
3173
 
3174
vl_cnt_lfsr_ce
3175
    # (.length(addr_width))
3176
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
3177
 
3178
// mux read or write adr to DPRAM
3179
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
3180
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
3181
 
3182
vl_dpram_2r2w
3183
    # (.data_width(data_width), .addr_width(addr_width+1))
3184
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
3185
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
3186
 
3187
vl_cnt_bin_ce_rew_zq_l1
3188 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
3189 27 unneback
    a_fill_level_cnt( .cke(a_rd ^ a_wr), .rew(a_rd), .q(a_fill_level), .zq(a_fifo_empty), .level1(a_fifo_full), .rst(rst), .clk(clk));
3190
 
3191
vl_cnt_bin_ce_rew_zq_l1
3192 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
3193 27 unneback
    b_fill_level_cnt( .cke(b_rd ^ b_wr), .rew(b_rd), .q(b_fill_level), .zq(b_fifo_empty), .level1(b_fifo_full), .rst(rst), .clk(clk));
3194
 
3195
endmodule
3196
 
3197 6 unneback
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
3198
 
3199 11 unneback
   parameter addr_width = 4;
3200
   parameter N = addr_width-1;
3201 6 unneback
 
3202
   parameter Q1 = 2'b00;
3203
   parameter Q2 = 2'b01;
3204
   parameter Q3 = 2'b11;
3205
   parameter Q4 = 2'b10;
3206
 
3207
   parameter going_empty = 1'b0;
3208
   parameter going_full  = 1'b1;
3209
 
3210
   input [N:0]  wptr, rptr;
3211 14 unneback
   output       fifo_empty;
3212 6 unneback
   output       fifo_full;
3213
   input        wclk, rclk, rst;
3214
 
3215
`ifndef GENERATE_DIRECTION_AS_LATCH
3216
   wire direction;
3217
`endif
3218
`ifdef GENERATE_DIRECTION_AS_LATCH
3219
   reg direction;
3220
`endif
3221
   reg  direction_set, direction_clr;
3222
 
3223
   wire async_empty, async_full;
3224
   wire fifo_full2;
3225 14 unneback
   wire fifo_empty2;
3226 6 unneback
 
3227
   // direction_set
3228
   always @ (wptr[N:N-1] or rptr[N:N-1])
3229
     case ({wptr[N:N-1],rptr[N:N-1]})
3230
       {Q1,Q2} : direction_set <= 1'b1;
3231
       {Q2,Q3} : direction_set <= 1'b1;
3232
       {Q3,Q4} : direction_set <= 1'b1;
3233
       {Q4,Q1} : direction_set <= 1'b1;
3234
       default : direction_set <= 1'b0;
3235
     endcase
3236
 
3237
   // direction_clear
3238
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
3239
     if (rst)
3240
       direction_clr <= 1'b1;
3241
     else
3242
       case ({wptr[N:N-1],rptr[N:N-1]})
3243
         {Q2,Q1} : direction_clr <= 1'b1;
3244
         {Q3,Q2} : direction_clr <= 1'b1;
3245
         {Q4,Q3} : direction_clr <= 1'b1;
3246
         {Q1,Q4} : direction_clr <= 1'b1;
3247
         default : direction_clr <= 1'b0;
3248
       endcase
3249
 
3250
`ifndef GENERATE_DIRECTION_AS_LATCH
3251 18 unneback
    vl_dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
3252 6 unneback
`endif
3253
 
3254
`ifdef GENERATE_DIRECTION_AS_LATCH
3255
   always @ (posedge direction_set or posedge direction_clr)
3256
     if (direction_clr)
3257
       direction <= going_empty;
3258
     else
3259
       direction <= going_full;
3260
`endif
3261
 
3262
   assign async_empty = (wptr == rptr) && (direction==going_empty);
3263
   assign async_full  = (wptr == rptr) && (direction==going_full);
3264
 
3265 18 unneback
    vl_dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
3266
    vl_dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
3267 6 unneback
 
3268
/*
3269
   always @ (posedge wclk or posedge rst or posedge async_full)
3270
     if (rst)
3271
       {fifo_full, fifo_full2} <= 2'b00;
3272
     else if (async_full)
3273
       {fifo_full, fifo_full2} <= 2'b11;
3274
     else
3275
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
3276
*/
3277 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
3278 6 unneback
     if (async_empty)
3279
       {fifo_empty, fifo_empty2} <= 2'b11;
3280
     else
3281 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
3282 18 unneback
    vl_dff # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
3283
    vl_dff # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
3284 6 unneback
 
3285 27 unneback
endmodule // async_compb
3286 6 unneback
 
3287
module vl_fifo_1r1w_async (
3288
    d, wr, fifo_full, wr_clk, wr_rst,
3289
    q, rd, fifo_empty, rd_clk, rd_rst
3290
    );
3291
 
3292
parameter data_width = 18;
3293
parameter addr_width = 4;
3294
 
3295
// write side
3296
input  [data_width-1:0] d;
3297
input                   wr;
3298
output                  fifo_full;
3299
input                   wr_clk;
3300
input                   wr_rst;
3301
// read side
3302
output [data_width-1:0] q;
3303
input                   rd;
3304
output                  fifo_empty;
3305
input                   rd_clk;
3306
input                   rd_rst;
3307
 
3308
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
3309 23 unneback
 
3310 18 unneback
vl_cnt_gray_ce_bin
3311 6 unneback
    # ( .length(addr_width))
3312
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
3313
 
3314 18 unneback
vl_cnt_gray_ce_bin
3315 6 unneback
    # (.length(addr_width))
3316 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
3317 6 unneback
 
3318 7 unneback
vl_dpram_1r1w
3319 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
3320
    dpram ( .d_a(d), .adr_a(wadr_bin), .we_a(wr), .clk_a(wr_clk), .q_b(q), .adr_b(radr_bin), .clk_b(rd_clk));
3321
 
3322
vl_fifo_cmp_async
3323
    # (.addr_width(addr_width))
3324
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
3325
 
3326
endmodule
3327
 
3328 8 unneback
module vl_fifo_2r2w_async (
3329 6 unneback
    // a side
3330
    a_d, a_wr, a_fifo_full,
3331
    a_q, a_rd, a_fifo_empty,
3332
    a_clk, a_rst,
3333
    // b side
3334
    b_d, b_wr, b_fifo_full,
3335
    b_q, b_rd, b_fifo_empty,
3336
    b_clk, b_rst
3337
    );
3338
 
3339
parameter data_width = 18;
3340
parameter addr_width = 4;
3341
 
3342
// a side
3343
input  [data_width-1:0] a_d;
3344
input                   a_wr;
3345
output                  a_fifo_full;
3346
output [data_width-1:0] a_q;
3347
input                   a_rd;
3348
output                  a_fifo_empty;
3349
input                   a_clk;
3350
input                   a_rst;
3351
 
3352
// b side
3353
input  [data_width-1:0] b_d;
3354
input                   b_wr;
3355
output                  b_fifo_full;
3356
output [data_width-1:0] b_q;
3357
input                   b_rd;
3358
output                  b_fifo_empty;
3359
input                   b_clk;
3360
input                   b_rst;
3361
 
3362
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
3363
vl_fifo_1r1w_async_a (
3364
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
3365
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
3366
    );
3367
 
3368
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
3369
vl_fifo_1r1w_async_b (
3370
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
3371
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
3372
    );
3373
 
3374
endmodule
3375
 
3376 8 unneback
module vl_fifo_2r2w_async_simplex (
3377 6 unneback
    // a side
3378
    a_d, a_wr, a_fifo_full,
3379
    a_q, a_rd, a_fifo_empty,
3380
    a_clk, a_rst,
3381
    // b side
3382
    b_d, b_wr, b_fifo_full,
3383
    b_q, b_rd, b_fifo_empty,
3384
    b_clk, b_rst
3385
    );
3386
 
3387
parameter data_width = 18;
3388
parameter addr_width = 4;
3389
 
3390
// a side
3391
input  [data_width-1:0] a_d;
3392
input                   a_wr;
3393
output                  a_fifo_full;
3394
output [data_width-1:0] a_q;
3395
input                   a_rd;
3396
output                  a_fifo_empty;
3397
input                   a_clk;
3398
input                   a_rst;
3399
 
3400
// b side
3401
input  [data_width-1:0] b_d;
3402
input                   b_wr;
3403
output                  b_fifo_full;
3404
output [data_width-1:0] b_q;
3405
input                   b_rd;
3406
output                  b_fifo_empty;
3407
input                   b_clk;
3408
input                   b_rst;
3409
 
3410
// adr_gen
3411
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
3412
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
3413
// dpram
3414
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
3415
 
3416 18 unneback
vl_cnt_gray_ce_bin
3417 6 unneback
    # ( .length(addr_width))
3418
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
3419
 
3420 18 unneback
vl_cnt_gray_ce_bin
3421 6 unneback
    # (.length(addr_width))
3422
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
3423
 
3424 18 unneback
vl_cnt_gray_ce_bin
3425 6 unneback
    # ( .length(addr_width))
3426
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
3427
 
3428 18 unneback
vl_cnt_gray_ce_bin
3429 6 unneback
    # (.length(addr_width))
3430
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
3431
 
3432
// mux read or write adr to DPRAM
3433
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
3434
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
3435
 
3436 11 unneback
vl_dpram_2r2w
3437 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
3438
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
3439
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
3440
 
3441 11 unneback
vl_fifo_cmp_async
3442 6 unneback
    # (.addr_width(addr_width))
3443
    cmp1 ( .wptr(a_wadr), .rptr(b_radr), .fifo_empty(b_fifo_empty), .fifo_full(a_fifo_full), .wclk(a_clk), .rclk(b_clk), .rst(a_rst) );
3444
 
3445 11 unneback
vl_fifo_cmp_async
3446 6 unneback
    # (.addr_width(addr_width))
3447
    cmp2 ( .wptr(b_wadr), .rptr(a_radr), .fifo_empty(a_fifo_empty), .fifo_full(b_fifo_full), .wclk(b_clk), .rclk(a_clk), .rst(b_rst) );
3448
 
3449
endmodule
3450 12 unneback
//////////////////////////////////////////////////////////////////////
3451
////                                                              ////
3452
////  Versatile library, wishbone stuff                           ////
3453
////                                                              ////
3454
////  Description                                                 ////
3455
////  Wishbone compliant modules                                  ////
3456
////                                                              ////
3457
////                                                              ////
3458
////  To Do:                                                      ////
3459
////   -                                                          ////
3460
////                                                              ////
3461
////  Author(s):                                                  ////
3462
////      - Michael Unneback, unneback@opencores.org              ////
3463
////        ORSoC AB                                              ////
3464
////                                                              ////
3465
//////////////////////////////////////////////////////////////////////
3466
////                                                              ////
3467
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3468
////                                                              ////
3469
//// This source file may be used and distributed without         ////
3470
//// restriction provided that this copyright statement is not    ////
3471
//// removed from the file and that any derivative work contains  ////
3472
//// the original copyright notice and the associated disclaimer. ////
3473
////                                                              ////
3474
//// This source file is free software; you can redistribute it   ////
3475
//// and/or modify it under the terms of the GNU Lesser General   ////
3476
//// Public License as published by the Free Software Foundation; ////
3477
//// either version 2.1 of the License, or (at your option) any   ////
3478
//// later version.                                               ////
3479
////                                                              ////
3480
//// This source is distributed in the hope that it will be       ////
3481
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3482
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3483
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3484
//// details.                                                     ////
3485
////                                                              ////
3486
//// You should have received a copy of the GNU Lesser General    ////
3487
//// Public License along with this source; if not, download it   ////
3488
//// from http://www.opencores.org/lgpl.shtml                     ////
3489
////                                                              ////
3490
//////////////////////////////////////////////////////////////////////
3491
 
3492
// async wb3 - wb3 bridge
3493
`timescale 1ns/1ns
3494 18 unneback
module vl_wb3wb3_bridge (
3495 12 unneback
        // wishbone slave side
3496
        wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_bte_i, wbs_cti_i, wbs_we_i, wbs_cyc_i, wbs_stb_i, wbs_dat_o, wbs_ack_o, wbs_clk, wbs_rst,
3497
        // wishbone master side
3498
        wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_bte_o, wbm_cti_o, wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_dat_i, wbm_ack_i, wbm_clk, wbm_rst);
3499
 
3500
input [31:0] wbs_dat_i;
3501
input [31:2] wbs_adr_i;
3502
input [3:0]  wbs_sel_i;
3503
input [1:0]  wbs_bte_i;
3504
input [2:0]  wbs_cti_i;
3505
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
3506
output [31:0] wbs_dat_o;
3507 14 unneback
output wbs_ack_o;
3508 12 unneback
input wbs_clk, wbs_rst;
3509
 
3510
output [31:0] wbm_dat_o;
3511
output reg [31:2] wbm_adr_o;
3512
output [3:0]  wbm_sel_o;
3513
output reg [1:0]  wbm_bte_o;
3514
output reg [2:0]  wbm_cti_o;
3515 14 unneback
output reg wbm_we_o;
3516
output wbm_cyc_o;
3517 12 unneback
output wbm_stb_o;
3518
input [31:0]  wbm_dat_i;
3519
input wbm_ack_i;
3520
input wbm_clk, wbm_rst;
3521
 
3522
parameter addr_width = 4;
3523
 
3524
// bte
3525
parameter linear       = 2'b00;
3526
parameter wrap4        = 2'b01;
3527
parameter wrap8        = 2'b10;
3528
parameter wrap16       = 2'b11;
3529
// cti
3530
parameter classic      = 3'b000;
3531
parameter incburst     = 3'b010;
3532
parameter endofburst   = 3'b111;
3533
 
3534
parameter wbs_adr  = 1'b0;
3535
parameter wbs_data = 1'b1;
3536
 
3537 33 unneback
parameter wbm_adr0      = 2'b00;
3538
parameter wbm_adr1      = 2'b01;
3539
parameter wbm_data      = 2'b10;
3540
parameter wbm_data_wait = 2'b11;
3541 12 unneback
 
3542
reg [1:0] wbs_bte_reg;
3543
reg wbs;
3544
wire wbs_eoc_alert, wbm_eoc_alert;
3545
reg wbs_eoc, wbm_eoc;
3546
reg [1:0] wbm;
3547
 
3548 14 unneback
wire [1:16] wbs_count, wbm_count;
3549 12 unneback
 
3550
wire [35:0] a_d, a_q, b_d, b_q;
3551
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
3552
reg a_rd_reg;
3553
wire b_rd_adr, b_rd_data;
3554 14 unneback
wire b_rd_data_reg;
3555
wire [35:0] temp;
3556 12 unneback
 
3557
`define WE 5
3558
`define BTE 4:3
3559
`define CTI 2:0
3560
 
3561
assign wbs_eoc_alert = (wbs_bte_reg==wrap4 & wbs_count[3]) | (wbs_bte_reg==wrap8 & wbs_count[7]) | (wbs_bte_reg==wrap16 & wbs_count[15]);
3562
always @ (posedge wbs_clk or posedge wbs_rst)
3563
if (wbs_rst)
3564
        wbs_eoc <= 1'b0;
3565
else
3566
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
3567
                wbs_eoc <= wbs_bte_i==linear;
3568
        else if (wbs_eoc_alert & (a_rd | a_wr))
3569
                wbs_eoc <= 1'b1;
3570
 
3571 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
3572 12 unneback
    cnt0 (
3573
        .cke(wbs_ack_o),
3574
        .clear(wbs_eoc),
3575
        .q(wbs_count),
3576
        .rst(wbs_rst),
3577
        .clk(wbs_clk));
3578
 
3579
always @ (posedge wbs_clk or posedge wbs_rst)
3580
if (wbs_rst)
3581
        wbs <= wbs_adr;
3582
else
3583
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & !a_fifo_full)
3584
                wbs <= wbs_data;
3585
        else if (wbs_eoc & wbs_ack_o)
3586
                wbs <= wbs_adr;
3587
 
3588
// wbs FIFO
3589
assign a_d = (wbs==wbs_adr) ? {wbs_adr_i[31:2],wbs_we_i,wbs_bte_i,wbs_cti_i} : {wbs_dat_i,wbs_sel_i};
3590
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & !a_fifo_full :
3591
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
3592
              1'b0;
3593
assign a_rd = !a_fifo_empty;
3594
always @ (posedge wbs_clk or posedge wbs_rst)
3595
if (wbs_rst)
3596
        a_rd_reg <= 1'b0;
3597
else
3598
        a_rd_reg <= a_rd;
3599
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
3600
 
3601
assign wbs_dat_o = a_q[35:4];
3602
 
3603
always @ (posedge wbs_clk or posedge wbs_rst)
3604
if (wbs_rst)
3605 13 unneback
        wbs_bte_reg <= 2'b00;
3606 12 unneback
else
3607 13 unneback
        wbs_bte_reg <= wbs_bte_i;
3608 12 unneback
 
3609
// wbm FIFO
3610
assign wbm_eoc_alert = (wbm_bte_o==wrap4 & wbm_count[3]) | (wbm_bte_o==wrap8 & wbm_count[7]) | (wbm_bte_o==wrap16 & wbm_count[15]);
3611
always @ (posedge wbm_clk or posedge wbm_rst)
3612
if (wbm_rst)
3613
        wbm_eoc <= 1'b0;
3614
else
3615
        if (wbm==wbm_adr0 & !b_fifo_empty)
3616
                wbm_eoc <= b_q[`BTE] == linear;
3617
        else if (wbm_eoc_alert & wbm_ack_i)
3618
                wbm_eoc <= 1'b1;
3619
 
3620
always @ (posedge wbm_clk or posedge wbm_rst)
3621
if (wbm_rst)
3622
        wbm <= wbm_adr0;
3623
else
3624 33 unneback
/*
3625 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
3626
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
3627
        (wbm==wbm_adr1 & !wbm_we_o) |
3628
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
3629
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
3630 33 unneback
*/
3631
    case (wbm)
3632
    wbm_adr0:
3633
        if (!b_fifo_empty)
3634
            wbm <= wbm_adr1;
3635
    wbm_adr1:
3636
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
3637
            wbm <= wbm_data;
3638
    wbm_data:
3639
        if (wbm_ack_i & wbm_eoc)
3640
            wbm <= wbm_adr0;
3641
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
3642
            wbm <= wbm_data_wait;
3643
    wbm_data_wait:
3644
        if (!b_fifo_empty)
3645
            wbm <= wbm_data;
3646
    endcase
3647 12 unneback
 
3648
assign b_d = {wbm_dat_i,4'b1111};
3649
assign b_wr = !wbm_we_o & wbm_ack_i;
3650
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
3651
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
3652
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
3653 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
3654 12 unneback
                   1'b0;
3655
assign b_rd = b_rd_adr | b_rd_data;
3656
 
3657 18 unneback
vl_dff dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
3658
vl_dff_ce # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
3659 12 unneback
 
3660
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
3661
 
3662 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
3663 12 unneback
    cnt1 (
3664
        .cke(wbm_ack_i),
3665
        .clear(wbm_eoc),
3666
        .q(wbm_count),
3667
        .rst(wbm_rst),
3668
        .clk(wbm_clk));
3669
 
3670 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
3671
assign wbm_stb_o = (wbm==wbm_data);
3672 12 unneback
 
3673
always @ (posedge wbm_clk or posedge wbm_rst)
3674
if (wbm_rst)
3675
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
3676
else begin
3677
        if (wbm==wbm_adr0 & !b_fifo_empty)
3678
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
3679
        else if (wbm_eoc_alert & wbm_ack_i)
3680
                wbm_cti_o <= endofburst;
3681
end
3682
 
3683
//async_fifo_dw_simplex_top
3684
vl_fifo_2r2w_async_simplex
3685
# ( .data_width(36), .addr_width(addr_width))
3686
fifo (
3687
    // a side
3688
    .a_d(a_d),
3689
    .a_wr(a_wr),
3690
    .a_fifo_full(a_fifo_full),
3691
    .a_q(a_q),
3692
    .a_rd(a_rd),
3693
    .a_fifo_empty(a_fifo_empty),
3694
    .a_clk(wbs_clk),
3695
    .a_rst(wbs_rst),
3696
    // b side
3697
    .b_d(b_d),
3698
    .b_wr(b_wr),
3699
    .b_fifo_full(b_fifo_full),
3700
    .b_q(b_q),
3701
    .b_rd(b_rd),
3702
    .b_fifo_empty(b_fifo_empty),
3703
    .b_clk(wbm_clk),
3704
    .b_rst(wbm_rst)
3705
    );
3706
 
3707
endmodule
3708 17 unneback
 
3709
// WB ROM
3710 18 unneback
module vl_wb_boot_rom (
3711 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
3712 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
3713 17 unneback
 
3714 18 unneback
    parameter adr_hi = 31;
3715
    parameter adr_lo = 28;
3716
    parameter adr_sel = 4'hf;
3717
    parameter addr_width = 5;
3718 33 unneback
/*
3719 17 unneback
`ifndef BOOT_ROM
3720
`define BOOT_ROM "boot_rom.v"
3721
`endif
3722 33 unneback
*/
3723 18 unneback
    input [adr_hi:2]    wb_adr_i;
3724
    input               wb_stb_i;
3725
    input               wb_cyc_i;
3726
    output [31:0]        wb_dat_o;
3727
    output              wb_ack_o;
3728
    output              hit_o;
3729
    input               wb_clk;
3730
    input               wb_rst;
3731
 
3732
    wire hit;
3733
    reg [31:0] wb_dat;
3734
    reg wb_ack;
3735
 
3736
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
3737 17 unneback
 
3738
always @ (posedge wb_clk or posedge wb_rst)
3739
    if (wb_rst)
3740 18 unneback
        wb_dat <= 32'h15000000;
3741 17 unneback
    else
3742 18 unneback
         case (wb_adr_i[addr_width-1:2])
3743 33 unneback
`ifdef BOOT_ROM
3744 17 unneback
`include `BOOT_ROM
3745 33 unneback
`endif
3746 17 unneback
           /*
3747
            // Zero r0 and jump to 0x00000100
3748 18 unneback
 
3749
            1 : wb_dat <= 32'hA8200000;
3750
            2 : wb_dat <= 32'hA8C00100;
3751
            3 : wb_dat <= 32'h44003000;
3752
            4 : wb_dat <= 32'h15000000;
3753 17 unneback
            */
3754
           default:
3755 18 unneback
             wb_dat <= 32'h00000000;
3756 17 unneback
 
3757
         endcase // case (wb_adr_i)
3758
 
3759
 
3760
always @ (posedge wb_clk or posedge wb_rst)
3761
    if (wb_rst)
3762 18 unneback
        wb_ack <= 1'b0;
3763 17 unneback
    else
3764 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
3765 17 unneback
 
3766 18 unneback
assign hit_o = hit;
3767
assign wb_dat_o = wb_dat & {32{wb_ack}};
3768
assign wb_ack_o = wb_ack;
3769
 
3770 17 unneback
endmodule
3771 32 unneback
 
3772
module vl_wb_dpram (
3773
        // wishbone slave side a
3774
        wbsa_dat_i, wbsa_adr_i, wbsa_we_i, wbsa_cyc_i, wbsa_stb_i, wbsa_dat_o, wbsa_ack_o,
3775
        wbsa_clk, wbsa_rst,
3776
        // wishbone slave side a
3777
        wbsb_dat_i, wbsb_adr_i, wbsb_we_i, wbsb_cyc_i, wbsb_stb_i, wbsb_dat_o, wbsb_ack_o,
3778
        wbsb_clk, wbsb_rst);
3779
 
3780
parameter data_width = 32;
3781
parameter addr_width = 8;
3782
 
3783
parameter dat_o_mask_a = 1;
3784
parameter dat_o_mask_b = 1;
3785
 
3786
input [31:0] wbsa_dat_i;
3787
input [addr_width-1:2] wbsa_adr_i;
3788
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
3789
output [31:0] wbsa_dat_o;
3790
output wbsa_ack_o;
3791
input wbsa_clk, wbsa_rst;
3792
 
3793
input [31:0] wbsb_dat_i;
3794
input [addr_width-1:2] wbsb_adr_i;
3795
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
3796
output [31:0] wbsb_dat_o;
3797
output wbsb_ack_o;
3798
input wbsb_clk, wbsb_rst;
3799
 
3800
wire wbsa_dat_tmp, wbsb_dat_tmp;
3801
 
3802
vl_dpram_2r2w # (
3803 33 unneback
    .data_width(data_width), .addr_width(addr_width) )
3804 32 unneback
dpram0(
3805
    .d_a(wbsa_dat_i),
3806
    .q_a(wbsa_dat_tmp),
3807
    .adr_a(wbsa_adr_i),
3808
    .we_a(wbsa_we_i),
3809
    .clk_a(wbsa_clk),
3810
    .d_b(wbsb_dat_i),
3811
    .q_b(wbsb_dat_tmp),
3812
    .adr_b(wbsb_adr_i),
3813
    .we_b(wbsb_we_i),
3814
    .clk_b(wbsb_clk) );
3815
 
3816 33 unneback
generate if (dat_o_mask_a==1)
3817 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp & {data_width{wbsa_ack_o}};
3818
endgenerate
3819 33 unneback
generate if (dat_o_mask_a==0)
3820 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp;
3821
endgenerate
3822
 
3823 33 unneback
generate if (dat_o_mask_b==1)
3824 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp & {data_width{wbsb_ack_o}};
3825
endgenerate
3826 33 unneback
generate if (dat_o_mask_b==0)
3827 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp;
3828
endgenerate
3829
 
3830
vl_spr ack_a( .sp(wbsa_cyc_i & wbsa_stb_i & !wbsa_ack_o), .r(1'b1), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
3831
vl_spr ack_b( .sp(wbsb_cyc_i & wbsb_stb_i & !wbsb_ack_o), .r(1'b1), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
3832
 
3833
endmodule
3834 18 unneback
//////////////////////////////////////////////////////////////////////
3835
////                                                              ////
3836
////  Arithmetic functions                                        ////
3837
////                                                              ////
3838
////  Description                                                 ////
3839
////  Arithmetic functions for ALU and DSP                        ////
3840
////                                                              ////
3841
////                                                              ////
3842
////  To Do:                                                      ////
3843
////   -                                                          ////
3844
////                                                              ////
3845
////  Author(s):                                                  ////
3846
////      - Michael Unneback, unneback@opencores.org              ////
3847
////        ORSoC AB                                              ////
3848
////                                                              ////
3849
//////////////////////////////////////////////////////////////////////
3850
////                                                              ////
3851
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3852
////                                                              ////
3853
//// This source file may be used and distributed without         ////
3854
//// restriction provided that this copyright statement is not    ////
3855
//// removed from the file and that any derivative work contains  ////
3856
//// the original copyright notice and the associated disclaimer. ////
3857
////                                                              ////
3858
//// This source file is free software; you can redistribute it   ////
3859
//// and/or modify it under the terms of the GNU Lesser General   ////
3860
//// Public License as published by the Free Software Foundation; ////
3861
//// either version 2.1 of the License, or (at your option) any   ////
3862
//// later version.                                               ////
3863
////                                                              ////
3864
//// This source is distributed in the hope that it will be       ////
3865
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3866
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3867
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3868
//// details.                                                     ////
3869
////                                                              ////
3870
//// You should have received a copy of the GNU Lesser General    ////
3871
//// Public License along with this source; if not, download it   ////
3872
//// from http://www.opencores.org/lgpl.shtml                     ////
3873
////                                                              ////
3874
//////////////////////////////////////////////////////////////////////
3875
 
3876
// signed multiplication
3877
module vl_mults (a,b,p);
3878
parameter operand_a_width = 18;
3879
parameter operand_b_width = 18;
3880
parameter result_hi = 35;
3881
parameter result_lo = 0;
3882
input [operand_a_width-1:0] a;
3883
input [operand_b_width-1:0] b;
3884
output [result_hi:result_lo] p;
3885
wire signed [operand_a_width-1:0] ai;
3886
wire signed [operand_b_width-1:0] bi;
3887
wire signed [operand_a_width+operand_b_width-1:0] result;
3888
 
3889
    assign ai = a;
3890
    assign bi = b;
3891
    assign result = ai * bi;
3892
    assign p = result[result_hi:result_lo];
3893
 
3894
endmodule
3895
 
3896
module vl_mults18x18 (a,b,p);
3897
input [17:0] a,b;
3898
output [35:0] p;
3899
vl_mult
3900
    # (.operand_a_width(18), .operand_b_width(18))
3901
    mult0 (.a(a), .b(b), .p(p));
3902
endmodule
3903
 
3904
// unsigned multiplication
3905
module vl_mult (a,b,p);
3906
parameter operand_a_width = 18;
3907
parameter operand_b_width = 18;
3908
parameter result_hi = 35;
3909
parameter result_lo = 0;
3910
input [operand_a_width-1:0] a;
3911
input [operand_b_width-1:0] b;
3912
output [result_hi:result_hi] p;
3913
 
3914
wire [operand_a_width+operand_b_width-1:0] result;
3915
 
3916
    assign result = a * b;
3917
    assign p = result[result_hi:result_lo];
3918
 
3919
endmodule
3920
 
3921
// shift unit
3922
// supporting the following shift functions
3923
//   SLL
3924
//   SRL
3925
//   SRA
3926
`define SHIFT_UNIT_MULT # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7))
3927
module vl_shift_unit_32( din, s, dout, opcode);
3928
input [31:0] din; // data in operand
3929
input [4:0] s; // shift operand
3930
input [1:0] opcode;
3931
output [31:0] dout;
3932
 
3933
parameter opcode_sll = 2'b00;
3934
//parameter opcode_srl = 2'b01;
3935
parameter opcode_sra = 2'b10;
3936
//parameter opcode_ror = 2'b11;
3937
 
3938
wire sll, sra;
3939
assign sll = opcode == opcode_sll;
3940
assign sra = opcode == opcode_sra;
3941
 
3942
wire [15:1] s1;
3943
wire [3:0] sign;
3944
wire [7:0] tmp [0:3];
3945
 
3946
// first stage is multiplier based
3947
// shift operand as fractional 8.7
3948
assign s1[15] = sll & s[2:0]==3'd7;
3949
assign s1[14] = sll & s[2:0]==3'd6;
3950
assign s1[13] = sll & s[2:0]==3'd5;
3951
assign s1[12] = sll & s[2:0]==3'd4;
3952
assign s1[11] = sll & s[2:0]==3'd3;
3953
assign s1[10] = sll & s[2:0]==3'd2;
3954
assign s1[ 9] = sll & s[2:0]==3'd1;
3955
assign s1[ 8] = s[2:0]==3'd0;
3956
assign s1[ 7] = !sll & s[2:0]==3'd1;
3957
assign s1[ 6] = !sll & s[2:0]==3'd2;
3958
assign s1[ 5] = !sll & s[2:0]==3'd3;
3959
assign s1[ 4] = !sll & s[2:0]==3'd4;
3960
assign s1[ 3] = !sll & s[2:0]==3'd5;
3961
assign s1[ 2] = !sll & s[2:0]==3'd6;
3962
assign s1[ 1] = !sll & s[2:0]==3'd7;
3963
 
3964
assign sign[3] = din[31] & sra;
3965
assign sign[2] = sign[3] & (&din[31:24]);
3966
assign sign[1] = sign[2] & (&din[23:16]);
3967
assign sign[0] = sign[1] & (&din[15:8]);
3968
vl_mults `SHIFT_UNIT_MULT mult_byte3 ( .a({sign[3], {8{sign[3]}},din[31:24], din[23:16]}), .b({1'b0,s1}), .p(tmp[3]));
3969
vl_mults `SHIFT_UNIT_MULT mult_byte2 ( .a({sign[2], din[31:24]  ,din[23:16],  din[15:8]}), .b({1'b0,s1}), .p(tmp[2]));
3970
vl_mults `SHIFT_UNIT_MULT mult_byte1 ( .a({sign[1], din[23:16]  ,din[15:8],   din[7:0]}), .b({1'b0,s1}), .p(tmp[1]));
3971
vl_mults `SHIFT_UNIT_MULT mult_byte0 ( .a({sign[0], din[15:8]   ,din[7:0],    8'h00}),      .b({1'b0,s1}), .p(tmp[0]));
3972
 
3973
// second stage is multiplexer based
3974
// shift on byte level
3975
 
3976
// mux byte 3
3977
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
3978
                     (sll & s[4:3]==2'b01) ? tmp[2] :
3979
                     (sll & s[4:3]==2'b10) ? tmp[1] :
3980
                     (sll & s[4:3]==2'b11) ? tmp[0] :
3981
                     {8{sign[3]}};
3982
 
3983
// mux byte 2
3984
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
3985
                     (sll & s[4:3]==2'b01) ? tmp[1] :
3986
                     (sll & s[4:3]==2'b10) ? tmp[0] :
3987
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
3988
                     (s[4:3]==2'b01) ? tmp[3] :
3989
                     {8{sign[3]}};
3990
 
3991
// mux byte 1
3992
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
3993
                     (sll & s[4:3]==2'b01) ? tmp[0] :
3994
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
3995
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
3996
                     (s[4:3]==2'b01) ? tmp[2] :
3997
                     (s[4:3]==2'b10) ? tmp[3] :
3998
                     {8{sign[3]}};
3999
 
4000
// mux byte 0
4001
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
4002
                     (sll) ?  {8{1'b0}}:
4003
                     (s[4:3]==2'b01) ? tmp[1] :
4004
                     (s[4:3]==2'b10) ? tmp[2] :
4005
                     tmp[3];
4006
 
4007
endmodule
4008
 
4009
// logic unit
4010
// supporting the following logic functions
4011
//    a and b
4012
//    a or  b
4013
//    a xor b
4014
//    not b
4015
module vl_logic_unit( a, b, result, opcode);
4016
parameter width = 32;
4017
parameter opcode_and = 2'b00;
4018
parameter opcode_or  = 2'b01;
4019
parameter opcode_xor = 2'b10;
4020
input [width-1:0] a,b;
4021
output [width-1:0] result;
4022
input [1:0] opcode;
4023
 
4024
assign result = (opcode==opcode_and) ? a & b :
4025
                (opcode==opcode_or)  ? a | b :
4026
                (opcode==opcode_xor) ? a ^ b :
4027
                b;
4028
 
4029
endmodule
4030
 
4031
module vl_arith_unit ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
4032
parameter width = 32;
4033
parameter opcode_add = 1'b0;
4034
parameter opcode_sub = 1'b1;
4035
input [width-1:0] a,b;
4036
input c_in, add_sub, sign;
4037
output [width-1:0] result;
4038
output c_out, z, ovfl;
4039
 
4040
assign {c_out,result} = {(a[width-1] & sign),a} + ({a[width-1] & sign,b} ^ {(width+1){(add_sub==opcode_sub)}}) + {{(width-1){1'b0}},(c_in | (add_sub==opcode_sub))};
4041
assign z = (result=={width{1'b0}});
4042
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
4043
               (~a[width-1] & ~b[width-1] &  result[width-1]);
4044
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.