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 28

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
`else
73
`ifdef ALTERA
74 21 unneback
//altera
75 6 unneback
`else
76
 
77
`timescale 1 ns/100 ps
78
module vl_gbuf ( i, o);
79
input i;
80
output o;
81
assign o = i;
82
endmodule
83
`endif // ALTERA
84
`endif //ACTEL
85
 
86
// sync reset
87 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
88 6 unneback
// output active high global reset sync with two DFFs 
89
`timescale 1 ns/100 ps
90
module vl_sync_rst ( rst_n_i, rst_o, clk);
91
input rst_n_i, clk;
92
output rst_o;
93 18 unneback
reg [1:0] tmp;
94 6 unneback
always @ (posedge clk or negedge rst_n_i)
95
if (!rst_n_i)
96 17 unneback
        tmp <= 2'b11;
97 6 unneback
else
98 17 unneback
        tmp <= {1'b0,tmp[0]};
99
vl_gbuf buf_i0( .i(tmp[0]), .o(rst_o));
100 6 unneback
endmodule
101
 
102
// vl_pll
103
`ifdef ACTEL
104 17 unneback
`timescale 1 ps/1 ps
105 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
106
parameter index = 0;
107
parameter number_of_clk = 1;
108 17 unneback
parameter period_time_0 = 20000;
109
parameter period_time_1 = 20000;
110
parameter period_time_2 = 20000;
111
parameter lock_delay = 2000000;
112 6 unneback
input clk_i, rst_n_i;
113
output lock;
114
output reg [0:number_of_clk-1] clk_o;
115
output [0:number_of_clk-1] rst_o;
116
 
117
`ifdef SIM_PLL
118
 
119
always
120
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
121
 
122
generate if (number_of_clk > 1)
123
always
124
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
125
endgenerate
126
 
127
generate if (number_of_clk > 2)
128
always
129
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
130
endgenerate
131
 
132
genvar i;
133
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
134
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
135
end
136
endgenerate
137
 
138
assign #lock_delay lock = rst_n_i;
139
 
140
endmodule
141
`else
142
generate if (number_of_clk==1 & index==0) begin
143
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
144
end
145
endgenerate // index==0
146
generate if (number_of_clk==1 & index==1) begin
147
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
148
end
149
endgenerate // index==1
150
generate if (number_of_clk==1 & index==2) begin
151
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
152
end
153
endgenerate // index==2
154
generate if (number_of_clk==1 & index==3) begin
155
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
156
end
157
endgenerate // index==0
158
 
159
generate if (number_of_clk==2 & index==0) begin
160
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
161
end
162
endgenerate // index==0
163
generate if (number_of_clk==2 & index==1) begin
164
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
165
end
166
endgenerate // index==1
167
generate if (number_of_clk==2 & index==2) begin
168
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
169
end
170
endgenerate // index==2
171
generate if (number_of_clk==2 & index==3) begin
172
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
173
end
174
endgenerate // index==0
175
 
176
generate if (number_of_clk==3 & index==0) begin
177
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
178
end
179
endgenerate // index==0
180
generate if (number_of_clk==3 & index==1) begin
181
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
182
end
183
endgenerate // index==1
184
generate if (number_of_clk==3 & index==2) begin
185
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
186
end
187
endgenerate // index==2
188
generate if (number_of_clk==3 & index==3) begin
189
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
190
end
191
endgenerate // index==0
192
 
193
genvar i;
194
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
195
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
196
end
197
endgenerate
198
endmodule
199
`endif
200
 
201
`else
202
 
203
`ifdef ALTERA
204
 
205
`else
206
 
207
// generic PLL
208 17 unneback
`timescale 1 ps/1 ps
209 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
210
parameter index = 0;
211
parameter number_of_clk = 1;
212 17 unneback
parameter period_time_0 = 20000;
213
parameter period_time_1 = 20000;
214
parameter period_time_2 = 20000;
215 6 unneback
parameter lock_delay = 2000;
216
input clk_i, rst_n_i;
217
output lock;
218
output reg [0:number_of_clk-1] clk_o;
219
output [0:number_of_clk-1] rst_o;
220
 
221
always
222
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
223
 
224
generate if (number_of_clk > 1)
225
always
226
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
227
endgenerate
228
 
229
generate if (number_of_clk > 2)
230
always
231
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
232
endgenerate
233
 
234
genvar i;
235
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
236
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
237
end
238
endgenerate
239
 
240
assign #lock_delay lock = rst_n_i;
241
 
242
endmodule
243
 
244
`endif //altera
245 17 unneback
`endif //actel
246
//////////////////////////////////////////////////////////////////////
247 6 unneback
////                                                              ////
248
////  Versatile library, registers                                ////
249
////                                                              ////
250
////  Description                                                 ////
251
////  Different type of registers                                 ////
252
////                                                              ////
253
////                                                              ////
254
////  To Do:                                                      ////
255
////   - add more different registers                             ////
256
////                                                              ////
257
////  Author(s):                                                  ////
258
////      - Michael Unneback, unneback@opencores.org              ////
259
////        ORSoC AB                                              ////
260
////                                                              ////
261
//////////////////////////////////////////////////////////////////////
262
////                                                              ////
263
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
264
////                                                              ////
265
//// This source file may be used and distributed without         ////
266
//// restriction provided that this copyright statement is not    ////
267
//// removed from the file and that any derivative work contains  ////
268
//// the original copyright notice and the associated disclaimer. ////
269
////                                                              ////
270
//// This source file is free software; you can redistribute it   ////
271
//// and/or modify it under the terms of the GNU Lesser General   ////
272
//// Public License as published by the Free Software Foundation; ////
273
//// either version 2.1 of the License, or (at your option) any   ////
274
//// later version.                                               ////
275
////                                                              ////
276
//// This source is distributed in the hope that it will be       ////
277
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
278
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
279
//// PURPOSE.  See the GNU Lesser General Public License for more ////
280
//// details.                                                     ////
281
////                                                              ////
282
//// You should have received a copy of the GNU Lesser General    ////
283
//// Public License along with this source; if not, download it   ////
284
//// from http://www.opencores.org/lgpl.shtml                     ////
285
////                                                              ////
286
//////////////////////////////////////////////////////////////////////
287
 
288 18 unneback
module vl_dff ( d, q, clk, rst);
289 6 unneback
 
290
        parameter width = 1;
291
        parameter reset_value = 0;
292
 
293
        input [width-1:0] d;
294
        input clk, rst;
295
        output reg [width-1:0] q;
296
 
297
        always @ (posedge clk or posedge rst)
298
        if (rst)
299
                q <= reset_value;
300
        else
301
                q <= d;
302
 
303
endmodule
304
 
305 18 unneback
module vl_dff_array ( d, q, clk, rst);
306 6 unneback
 
307
        parameter width = 1;
308
        parameter depth = 2;
309
        parameter reset_value = 1'b0;
310
 
311
        input [width-1:0] d;
312
        input clk, rst;
313
        output [width-1:0] q;
314
        reg  [0:depth-1] q_tmp [width-1:0];
315
        integer i;
316
        always @ (posedge clk or posedge rst)
317
        if (rst) begin
318
            for (i=0;i<depth;i=i+1)
319
                q_tmp[i] <= {width{reset_value}};
320
        end else begin
321
            q_tmp[0] <= d;
322
            for (i=1;i<depth;i=i+1)
323
                q_tmp[i] <= q_tmp[i-1];
324
        end
325
 
326
    assign q = q_tmp[depth-1];
327
 
328
endmodule
329
 
330 18 unneback
module vl_dff_ce ( d, ce, q, clk, rst);
331 6 unneback
 
332
        parameter width = 1;
333
        parameter reset_value = 0;
334
 
335
        input [width-1:0] d;
336
        input ce, clk, rst;
337
        output reg [width-1:0] q;
338
 
339
        always @ (posedge clk or posedge rst)
340
        if (rst)
341
                q <= reset_value;
342
        else
343
                if (ce)
344
                        q <= d;
345
 
346
endmodule
347
 
348 18 unneback
module vl_dff_ce_clear ( d, ce, clear, q, clk, rst);
349 8 unneback
 
350
        parameter width = 1;
351
        parameter reset_value = 0;
352
 
353
        input [width-1:0] d;
354 10 unneback
        input ce, clear, clk, rst;
355 8 unneback
        output reg [width-1:0] q;
356
 
357
        always @ (posedge clk or posedge rst)
358
        if (rst)
359
            q <= reset_value;
360
        else
361
            if (ce)
362
                if (clear)
363
                    q <= {width{1'b0}};
364
                else
365
                    q <= d;
366
 
367
endmodule
368
 
369 24 unneback
module vl_dff_ce_set ( d, ce, set, q, clk, rst);
370
 
371
        parameter width = 1;
372
        parameter reset_value = 0;
373
 
374
        input [width-1:0] d;
375
        input ce, set, clk, rst;
376
        output reg [width-1:0] q;
377
 
378
        always @ (posedge clk or posedge rst)
379
        if (rst)
380
            q <= reset_value;
381
        else
382
            if (ce)
383
                if (set)
384
                    q <= {width{1'b1}};
385
                else
386
                    q <= d;
387
 
388
endmodule
389
 
390 6 unneback
`ifdef ALTERA
391
// megafunction wizard: %LPM_FF%
392
// GENERATION: STANDARD
393
// VERSION: WM1.0
394
// MODULE: lpm_ff 
395
 
396
// ============================================================
397
// File Name: dff_sr.v
398
// Megafunction Name(s):
399
//                      lpm_ff
400
//
401
// Simulation Library Files(s):
402
//                      lpm
403
// ============================================================
404
// ************************************************************
405
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
406
//
407
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
408
// ************************************************************
409
 
410
 
411
//Copyright (C) 1991-2010 Altera Corporation
412
//Your use of Altera Corporation's design tools, logic functions 
413
//and other software and tools, and its AMPP partner logic 
414
//functions, and any output files from any of the foregoing 
415
//(including device programming or simulation files), and any 
416
//associated documentation or information are expressly subject 
417
//to the terms and conditions of the Altera Program License 
418
//Subscription Agreement, Altera MegaCore Function License 
419
//Agreement, or other applicable license agreement, including, 
420
//without limitation, that your use is for the sole purpose of 
421
//programming logic devices manufactured by Altera and sold by 
422
//Altera or its authorized distributors.  Please refer to the 
423
//applicable agreement for further details.
424
 
425
 
426
// synopsys translate_off
427
`timescale 1 ps / 1 ps
428
// synopsys translate_on
429 18 unneback
module vl_dff_sr (
430 6 unneback
        aclr,
431
        aset,
432
        clock,
433
        data,
434
        q);
435
 
436
        input     aclr;
437
        input     aset;
438
        input     clock;
439
        input     data;
440
        output    q;
441
 
442
        wire [0:0] sub_wire0;
443
        wire [0:0] sub_wire1 = sub_wire0[0:0];
444
        wire  q = sub_wire1;
445
        wire  sub_wire2 = data;
446
        wire  sub_wire3 = sub_wire2;
447
 
448
        lpm_ff  lpm_ff_component (
449
                                .aclr (aclr),
450
                                .clock (clock),
451
                                .data (sub_wire3),
452
                                .aset (aset),
453
                                .q (sub_wire0)
454
                                // synopsys translate_off
455
                                ,
456
                                .aload (),
457
                                .enable (),
458
                                .sclr (),
459
                                .sload (),
460
                                .sset ()
461
                                // synopsys translate_on
462
                                );
463
        defparam
464
                lpm_ff_component.lpm_fftype = "DFF",
465
                lpm_ff_component.lpm_type = "LPM_FF",
466
                lpm_ff_component.lpm_width = 1;
467
 
468
 
469
endmodule
470
 
471
// ============================================================
472
// CNX file retrieval info
473
// ============================================================
474
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
475
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
476
// Retrieval info: PRIVATE: ASET NUMERIC "1"
477
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
478
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
479
// Retrieval info: PRIVATE: DFF NUMERIC "1"
480
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
481
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
482
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
483
// Retrieval info: PRIVATE: SSET NUMERIC "0"
484
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
485
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
486
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
487
// Retrieval info: PRIVATE: nBit NUMERIC "1"
488
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
489
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
490
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
491
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
492
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
493
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
494
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
495
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
496
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
497
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
498
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
499
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
500
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
501
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
502
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
503
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
504
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
505
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
506
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
507
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
508
// Retrieval info: LIB_FILE: lpm
509
 
510
 
511
`else
512
 
513
 
514 18 unneback
module vl_dff_sr ( aclr, aset, clock, data, q);
515 6 unneback
 
516
    input         aclr;
517
    input         aset;
518
    input         clock;
519
    input         data;
520
    output reg    q;
521
 
522
   always @ (posedge clock or posedge aclr or posedge aset)
523
     if (aclr)
524
       q <= 1'b0;
525
     else if (aset)
526
       q <= 1'b1;
527
     else
528
       q <= data;
529
 
530
endmodule
531
 
532
`endif
533
 
534
// LATCH
535
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
536
`ifdef ALTERA
537 18 unneback
module vl_latch ( d, le, q, clk);
538 6 unneback
input d, le;
539
output q;
540
input clk;
541
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
542
endmodule
543
`else
544
module latch ( d, le, q, clk);
545
input d, le;
546
output q;
547
input clk;/*
548
   always @ (posedge direction_set or posedge direction_clr)
549
     if (direction_clr)
550
       direction <= going_empty;
551
     else
552
       direction <= going_full;*/
553
endmodule
554 15 unneback
`endif
555
 
556 18 unneback
module vl_shreg ( d, q, clk, rst);
557 17 unneback
parameter depth = 10;
558
input d;
559
output q;
560
input clk, rst;
561
 
562
reg [1:depth] dffs;
563
 
564
always @ (posedge clk or posedge rst)
565
if (rst)
566
    dffs <= {depth{1'b0}};
567
else
568
    dffs <= {d,dffs[1:depth-1]};
569
assign q = dffs[depth];
570
endmodule
571
 
572 18 unneback
module vl_shreg_ce ( d, ce, q, clk, rst);
573 17 unneback
parameter depth = 10;
574
input d, ce;
575
output q;
576
input clk, rst;
577
 
578
reg [1:depth] dffs;
579
 
580
always @ (posedge clk or posedge rst)
581
if (rst)
582
    dffs <= {depth{1'b0}};
583
else
584
    if (ce)
585
        dffs <= {d,dffs[1:depth-1]};
586
assign q = dffs[depth];
587
endmodule
588
 
589 18 unneback
module vl_delay ( d, q, clk, rst);
590 15 unneback
parameter depth = 10;
591
input d;
592
output q;
593
input clk, rst;
594
 
595
reg [1:depth] dffs;
596
 
597
always @ (posedge clk or posedge rst)
598
if (rst)
599
    dffs <= {depth{1'b0}};
600
else
601
    dffs <= {d,dffs[1:depth-1]};
602
assign q = dffs[depth];
603 17 unneback
endmodule
604
 
605 18 unneback
module vl_delay_emptyflag ( d, q, emptyflag, clk, rst);
606 17 unneback
parameter depth = 10;
607
input d;
608
output q, emptyflag;
609
input clk, rst;
610
 
611
reg [1:depth] dffs;
612
 
613
always @ (posedge clk or posedge rst)
614
if (rst)
615
    dffs <= {depth{1'b0}};
616
else
617
    dffs <= {d,dffs[1:depth-1]};
618
assign q = dffs[depth];
619
assign emptyflag = !(|dffs);
620
endmodule
621
//////////////////////////////////////////////////////////////////////
622 6 unneback
////                                                              ////
623 18 unneback
////  Logic functions                                             ////
624
////                                                              ////
625
////  Description                                                 ////
626
////  Logic functions such as multiplexers                        ////
627
////                                                              ////
628
////                                                              ////
629
////  To Do:                                                      ////
630
////   -                                                          ////
631
////                                                              ////
632
////  Author(s):                                                  ////
633
////      - Michael Unneback, unneback@opencores.org              ////
634
////        ORSoC AB                                              ////
635
////                                                              ////
636
//////////////////////////////////////////////////////////////////////
637
////                                                              ////
638
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
639
////                                                              ////
640
//// This source file may be used and distributed without         ////
641
//// restriction provided that this copyright statement is not    ////
642
//// removed from the file and that any derivative work contains  ////
643
//// the original copyright notice and the associated disclaimer. ////
644
////                                                              ////
645
//// This source file is free software; you can redistribute it   ////
646
//// and/or modify it under the terms of the GNU Lesser General   ////
647
//// Public License as published by the Free Software Foundation; ////
648
//// either version 2.1 of the License, or (at your option) any   ////
649
//// later version.                                               ////
650
////                                                              ////
651
//// This source is distributed in the hope that it will be       ////
652
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
653
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
654
//// PURPOSE.  See the GNU Lesser General Public License for more ////
655
//// details.                                                     ////
656
////                                                              ////
657
//// You should have received a copy of the GNU Lesser General    ////
658
//// Public License along with this source; if not, download it   ////
659
//// from http://www.opencores.org/lgpl.shtml                     ////
660
////                                                              ////
661
//////////////////////////////////////////////////////////////////////
662
 
663
module vl_mux4_andor ( a3, a2, a1, a0, sel, dout);
664
 
665
parameter width = 32;
666
parameter nr_of_ports = 4;
667
input [width-1:0] a3, a2, a1, a0;
668
input [nr_of_ports-1:0] sel;
669 22 unneback
output [width-1:0] dout;
670 18 unneback
 
671 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
672 18 unneback
integer i;
673
 
674
// and
675
assign tmp[0] = {width{sel[0]}} & a0;
676
assign tmp[1] = {width{sel[1]}} & a1;
677
assign tmp[2] = {width{sel[2]}} & a2;
678
assign tmp[3] = {width{sel[3]}} & a3;
679
 
680
// or
681
assign dout = tmp[3] | tmp[2] | tmp[1] | tmp[0];
682
 
683
endmodule
684
 
685
module vl_mux5_andor ( a4, a3, a2, a1, a0, sel, dout);
686
 
687
parameter width = 32;
688
parameter nr_of_ports = 5;
689
input [width-1:0] a4, a3, a2, a1, a0;
690
input [nr_of_ports-1:0] sel;
691 22 unneback
output [width-1:0] dout;
692 18 unneback
 
693 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
694 18 unneback
integer i;
695
 
696
// and
697
assign tmp[0] = {width{sel[0]}} & a0;
698
assign tmp[1] = {width{sel[1]}} & a1;
699
assign tmp[2] = {width{sel[2]}} & a2;
700
assign tmp[3] = {width{sel[3]}} & a3;
701
assign tmp[4] = {width{sel[4]}} & a4;
702
 
703
// or
704
assign dout = tmp[4] | tmp[3] | tmp[2] | tmp[1] | tmp[0];
705
 
706
endmodule
707
 
708
module vl_mux6_andor ( a5, a4, a3, a2, a1, a0, sel, dout);
709
 
710
parameter width = 32;
711
parameter nr_of_ports = 6;
712
input [width-1:0] a5, a4, a3, a2, a1, a0;
713
input [nr_of_ports-1:0] sel;
714 22 unneback
output [width-1:0] dout;
715 18 unneback
 
716 21 unneback
wire [width-1:0] tmp [nr_of_ports-1:0];
717 18 unneback
integer i;
718
 
719
// and
720
assign tmp[0] = {width{sel[0]}} & a0;
721
assign tmp[1] = {width{sel[1]}} & a1;
722
assign tmp[2] = {width{sel[2]}} & a2;
723
assign tmp[3] = {width{sel[3]}} & a3;
724
assign tmp[4] = {width{sel[4]}} & a4;
725
assign tmp[5] = {width{sel[5]}} & a5;
726
 
727
// or
728
assign dout = tmp[5] | tmp[4] | tmp[3] | tmp[2] | tmp[1] | tmp[0];
729
 
730
endmodule
731
//////////////////////////////////////////////////////////////////////
732
////                                                              ////
733 6 unneback
////  Versatile counter                                           ////
734
////                                                              ////
735
////  Description                                                 ////
736
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
737
////  counter                                                     ////
738
////                                                              ////
739
////  To Do:                                                      ////
740
////   - add LFSR with more taps                                  ////
741
////                                                              ////
742
////  Author(s):                                                  ////
743
////      - Michael Unneback, unneback@opencores.org              ////
744
////        ORSoC AB                                              ////
745
////                                                              ////
746
//////////////////////////////////////////////////////////////////////
747
////                                                              ////
748
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
749
////                                                              ////
750
//// This source file may be used and distributed without         ////
751
//// restriction provided that this copyright statement is not    ////
752
//// removed from the file and that any derivative work contains  ////
753
//// the original copyright notice and the associated disclaimer. ////
754
////                                                              ////
755
//// This source file is free software; you can redistribute it   ////
756
//// and/or modify it under the terms of the GNU Lesser General   ////
757
//// Public License as published by the Free Software Foundation; ////
758
//// either version 2.1 of the License, or (at your option) any   ////
759
//// later version.                                               ////
760
////                                                              ////
761
//// This source is distributed in the hope that it will be       ////
762
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
763
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
764
//// PURPOSE.  See the GNU Lesser General Public License for more ////
765
//// details.                                                     ////
766
////                                                              ////
767
//// You should have received a copy of the GNU Lesser General    ////
768
//// Public License along with this source; if not, download it   ////
769
//// from http://www.opencores.org/lgpl.shtml                     ////
770
////                                                              ////
771
//////////////////////////////////////////////////////////////////////
772
 
773
// binary counter
774 22 unneback
module vl_cnt_bin ( q, rst, clk);
775
 
776
   parameter length = 4;
777
   output [length:1] q;
778
   input rst;
779
   input clk;
780
 
781
   parameter clear_value = 0;
782
   parameter set_value = 1;
783
   parameter wrap_value = 0;
784
   parameter level1_value = 15;
785
 
786
   reg  [length:1] qi;
787
   wire [length:1] q_next;
788
   assign q_next = qi + {{length-1{1'b0}},1'b1};
789
 
790
   always @ (posedge clk or posedge rst)
791
     if (rst)
792
       qi <= {length{1'b0}};
793
     else
794
       qi <= q_next;
795
 
796
   assign q = qi;
797
 
798
endmodule
799
//////////////////////////////////////////////////////////////////////
800
////                                                              ////
801
////  Versatile counter                                           ////
802
////                                                              ////
803
////  Description                                                 ////
804
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
805
////  counter                                                     ////
806
////                                                              ////
807
////  To Do:                                                      ////
808
////   - add LFSR with more taps                                  ////
809
////                                                              ////
810
////  Author(s):                                                  ////
811
////      - Michael Unneback, unneback@opencores.org              ////
812
////        ORSoC AB                                              ////
813
////                                                              ////
814
//////////////////////////////////////////////////////////////////////
815
////                                                              ////
816
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
817
////                                                              ////
818
//// This source file may be used and distributed without         ////
819
//// restriction provided that this copyright statement is not    ////
820
//// removed from the file and that any derivative work contains  ////
821
//// the original copyright notice and the associated disclaimer. ////
822
////                                                              ////
823
//// This source file is free software; you can redistribute it   ////
824
//// and/or modify it under the terms of the GNU Lesser General   ////
825
//// Public License as published by the Free Software Foundation; ////
826
//// either version 2.1 of the License, or (at your option) any   ////
827
//// later version.                                               ////
828
////                                                              ////
829
//// This source is distributed in the hope that it will be       ////
830
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
831
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
832
//// PURPOSE.  See the GNU Lesser General Public License for more ////
833
//// details.                                                     ////
834
////                                                              ////
835
//// You should have received a copy of the GNU Lesser General    ////
836
//// Public License along with this source; if not, download it   ////
837
//// from http://www.opencores.org/lgpl.shtml                     ////
838
////                                                              ////
839
//////////////////////////////////////////////////////////////////////
840
 
841
// binary counter
842
module vl_cnt_bin_clear ( clear, q, rst, clk);
843
 
844
   parameter length = 4;
845
   input clear;
846
   output [length:1] q;
847
   input rst;
848
   input clk;
849
 
850
   parameter clear_value = 0;
851
   parameter set_value = 1;
852
   parameter wrap_value = 0;
853
   parameter level1_value = 15;
854
 
855
   reg  [length:1] qi;
856
   wire [length:1] q_next;
857
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
858
 
859
   always @ (posedge clk or posedge rst)
860
     if (rst)
861
       qi <= {length{1'b0}};
862
     else
863
       qi <= q_next;
864
 
865
   assign q = qi;
866
 
867
endmodule
868
//////////////////////////////////////////////////////////////////////
869
////                                                              ////
870
////  Versatile counter                                           ////
871
////                                                              ////
872
////  Description                                                 ////
873
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
874
////  counter                                                     ////
875
////                                                              ////
876
////  To Do:                                                      ////
877
////   - add LFSR with more taps                                  ////
878
////                                                              ////
879
////  Author(s):                                                  ////
880
////      - Michael Unneback, unneback@opencores.org              ////
881
////        ORSoC AB                                              ////
882
////                                                              ////
883
//////////////////////////////////////////////////////////////////////
884
////                                                              ////
885
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
886
////                                                              ////
887
//// This source file may be used and distributed without         ////
888
//// restriction provided that this copyright statement is not    ////
889
//// removed from the file and that any derivative work contains  ////
890
//// the original copyright notice and the associated disclaimer. ////
891
////                                                              ////
892
//// This source file is free software; you can redistribute it   ////
893
//// and/or modify it under the terms of the GNU Lesser General   ////
894
//// Public License as published by the Free Software Foundation; ////
895
//// either version 2.1 of the License, or (at your option) any   ////
896
//// later version.                                               ////
897
////                                                              ////
898
//// This source is distributed in the hope that it will be       ////
899
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
900
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
901
//// PURPOSE.  See the GNU Lesser General Public License for more ////
902
//// details.                                                     ////
903
////                                                              ////
904
//// You should have received a copy of the GNU Lesser General    ////
905
//// Public License along with this source; if not, download it   ////
906
//// from http://www.opencores.org/lgpl.shtml                     ////
907
////                                                              ////
908
//////////////////////////////////////////////////////////////////////
909
 
910
// binary counter
911 18 unneback
module vl_cnt_bin_ce ( cke, q, rst, clk);
912 6 unneback
 
913
   parameter length = 4;
914
   input cke;
915
   output [length:1] q;
916
   input rst;
917
   input clk;
918
 
919
   parameter clear_value = 0;
920
   parameter set_value = 1;
921
   parameter wrap_value = 0;
922
   parameter level1_value = 15;
923
 
924
   reg  [length:1] qi;
925
   wire [length:1] q_next;
926
   assign q_next = qi + {{length-1{1'b0}},1'b1};
927
 
928
   always @ (posedge clk or posedge rst)
929
     if (rst)
930
       qi <= {length{1'b0}};
931
     else
932
     if (cke)
933
       qi <= q_next;
934
 
935
   assign q = qi;
936
 
937
endmodule
938
//////////////////////////////////////////////////////////////////////
939
////                                                              ////
940
////  Versatile counter                                           ////
941
////                                                              ////
942
////  Description                                                 ////
943
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
944
////  counter                                                     ////
945
////                                                              ////
946
////  To Do:                                                      ////
947
////   - add LFSR with more taps                                  ////
948
////                                                              ////
949
////  Author(s):                                                  ////
950
////      - Michael Unneback, unneback@opencores.org              ////
951
////        ORSoC AB                                              ////
952
////                                                              ////
953
//////////////////////////////////////////////////////////////////////
954
////                                                              ////
955
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
956
////                                                              ////
957
//// This source file may be used and distributed without         ////
958
//// restriction provided that this copyright statement is not    ////
959
//// removed from the file and that any derivative work contains  ////
960
//// the original copyright notice and the associated disclaimer. ////
961
////                                                              ////
962
//// This source file is free software; you can redistribute it   ////
963
//// and/or modify it under the terms of the GNU Lesser General   ////
964
//// Public License as published by the Free Software Foundation; ////
965
//// either version 2.1 of the License, or (at your option) any   ////
966
//// later version.                                               ////
967
////                                                              ////
968
//// This source is distributed in the hope that it will be       ////
969
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
970
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
971
//// PURPOSE.  See the GNU Lesser General Public License for more ////
972
//// details.                                                     ////
973
////                                                              ////
974
//// You should have received a copy of the GNU Lesser General    ////
975
//// Public License along with this source; if not, download it   ////
976
//// from http://www.opencores.org/lgpl.shtml                     ////
977
////                                                              ////
978
//////////////////////////////////////////////////////////////////////
979
 
980
// binary counter
981 18 unneback
module vl_cnt_bin_ce_clear ( clear, cke, q, rst, clk);
982 6 unneback
 
983
   parameter length = 4;
984
   input clear;
985
   input cke;
986
   output [length:1] q;
987
   input rst;
988
   input clk;
989
 
990
   parameter clear_value = 0;
991
   parameter set_value = 1;
992
   parameter wrap_value = 0;
993
   parameter level1_value = 15;
994
 
995
   reg  [length:1] qi;
996
   wire [length:1] q_next;
997
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
998
 
999
   always @ (posedge clk or posedge rst)
1000
     if (rst)
1001
       qi <= {length{1'b0}};
1002
     else
1003
     if (cke)
1004
       qi <= q_next;
1005
 
1006
   assign q = qi;
1007
 
1008
endmodule
1009
//////////////////////////////////////////////////////////////////////
1010
////                                                              ////
1011
////  Versatile counter                                           ////
1012
////                                                              ////
1013
////  Description                                                 ////
1014
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1015
////  counter                                                     ////
1016
////                                                              ////
1017
////  To Do:                                                      ////
1018
////   - add LFSR with more taps                                  ////
1019
////                                                              ////
1020
////  Author(s):                                                  ////
1021
////      - Michael Unneback, unneback@opencores.org              ////
1022
////        ORSoC AB                                              ////
1023
////                                                              ////
1024
//////////////////////////////////////////////////////////////////////
1025
////                                                              ////
1026
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1027
////                                                              ////
1028
//// This source file may be used and distributed without         ////
1029
//// restriction provided that this copyright statement is not    ////
1030
//// removed from the file and that any derivative work contains  ////
1031
//// the original copyright notice and the associated disclaimer. ////
1032
////                                                              ////
1033
//// This source file is free software; you can redistribute it   ////
1034
//// and/or modify it under the terms of the GNU Lesser General   ////
1035
//// Public License as published by the Free Software Foundation; ////
1036
//// either version 2.1 of the License, or (at your option) any   ////
1037
//// later version.                                               ////
1038
////                                                              ////
1039
//// This source is distributed in the hope that it will be       ////
1040
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1041
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1042
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1043
//// details.                                                     ////
1044
////                                                              ////
1045
//// You should have received a copy of the GNU Lesser General    ////
1046
//// Public License along with this source; if not, download it   ////
1047
//// from http://www.opencores.org/lgpl.shtml                     ////
1048
////                                                              ////
1049
//////////////////////////////////////////////////////////////////////
1050
 
1051
// binary counter
1052 18 unneback
module vl_cnt_bin_ce_clear_set_rew ( clear, set, cke, rew, q, rst, clk);
1053 6 unneback
 
1054
   parameter length = 4;
1055
   input clear;
1056
   input set;
1057
   input cke;
1058
   input rew;
1059
   output [length:1] q;
1060
   input rst;
1061
   input clk;
1062
 
1063
   parameter clear_value = 0;
1064
   parameter set_value = 1;
1065
   parameter wrap_value = 0;
1066
   parameter level1_value = 15;
1067
 
1068
   reg  [length:1] qi;
1069
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1070
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
1071
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
1072
   assign q_next = rew ? q_next_rew : q_next_fw;
1073
 
1074
   always @ (posedge clk or posedge rst)
1075
     if (rst)
1076
       qi <= {length{1'b0}};
1077
     else
1078
     if (cke)
1079
       qi <= q_next;
1080
 
1081
   assign q = qi;
1082
 
1083
endmodule
1084
//////////////////////////////////////////////////////////////////////
1085
////                                                              ////
1086
////  Versatile counter                                           ////
1087
////                                                              ////
1088
////  Description                                                 ////
1089
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1090
////  counter                                                     ////
1091
////                                                              ////
1092
////  To Do:                                                      ////
1093
////   - add LFSR with more taps                                  ////
1094
////                                                              ////
1095
////  Author(s):                                                  ////
1096
////      - Michael Unneback, unneback@opencores.org              ////
1097
////        ORSoC AB                                              ////
1098
////                                                              ////
1099
//////////////////////////////////////////////////////////////////////
1100
////                                                              ////
1101
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1102
////                                                              ////
1103
//// This source file may be used and distributed without         ////
1104
//// restriction provided that this copyright statement is not    ////
1105
//// removed from the file and that any derivative work contains  ////
1106
//// the original copyright notice and the associated disclaimer. ////
1107
////                                                              ////
1108
//// This source file is free software; you can redistribute it   ////
1109
//// and/or modify it under the terms of the GNU Lesser General   ////
1110
//// Public License as published by the Free Software Foundation; ////
1111
//// either version 2.1 of the License, or (at your option) any   ////
1112
//// later version.                                               ////
1113
////                                                              ////
1114
//// This source is distributed in the hope that it will be       ////
1115
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1116
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1117
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1118
//// details.                                                     ////
1119
////                                                              ////
1120
//// You should have received a copy of the GNU Lesser General    ////
1121
//// Public License along with this source; if not, download it   ////
1122
//// from http://www.opencores.org/lgpl.shtml                     ////
1123
////                                                              ////
1124
//////////////////////////////////////////////////////////////////////
1125
 
1126
// binary counter
1127 18 unneback
module vl_cnt_bin_ce_rew_l1 ( cke, rew, level1, rst, clk);
1128 6 unneback
 
1129
   parameter length = 4;
1130
   input cke;
1131
   input rew;
1132
   output reg level1;
1133
   input rst;
1134
   input clk;
1135
 
1136
   parameter clear_value = 0;
1137
   parameter set_value = 1;
1138
   parameter wrap_value = 1;
1139
   parameter level1_value = 15;
1140
 
1141
   reg  [length:1] qi;
1142
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1143
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1144
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1145
   assign q_next = rew ? q_next_rew : q_next_fw;
1146
 
1147
   always @ (posedge clk or posedge rst)
1148
     if (rst)
1149
       qi <= {length{1'b0}};
1150
     else
1151
     if (cke)
1152
       qi <= q_next;
1153
 
1154
 
1155
 
1156
    always @ (posedge clk or posedge rst)
1157
    if (rst)
1158
        level1 <= 1'b0;
1159
    else
1160
    if (cke)
1161
    if (q_next == level1_value)
1162
        level1 <= 1'b1;
1163
    else if (qi == level1_value & rew)
1164
        level1 <= 1'b0;
1165
endmodule
1166
//////////////////////////////////////////////////////////////////////
1167
////                                                              ////
1168
////  Versatile counter                                           ////
1169
////                                                              ////
1170
////  Description                                                 ////
1171
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1172
////  counter                                                     ////
1173
////                                                              ////
1174
////  To Do:                                                      ////
1175
////   - add LFSR with more taps                                  ////
1176
////                                                              ////
1177
////  Author(s):                                                  ////
1178
////      - Michael Unneback, unneback@opencores.org              ////
1179
////        ORSoC AB                                              ////
1180
////                                                              ////
1181
//////////////////////////////////////////////////////////////////////
1182
////                                                              ////
1183
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1184
////                                                              ////
1185
//// This source file may be used and distributed without         ////
1186
//// restriction provided that this copyright statement is not    ////
1187
//// removed from the file and that any derivative work contains  ////
1188
//// the original copyright notice and the associated disclaimer. ////
1189
////                                                              ////
1190
//// This source file is free software; you can redistribute it   ////
1191
//// and/or modify it under the terms of the GNU Lesser General   ////
1192
//// Public License as published by the Free Software Foundation; ////
1193
//// either version 2.1 of the License, or (at your option) any   ////
1194
//// later version.                                               ////
1195
////                                                              ////
1196
//// This source is distributed in the hope that it will be       ////
1197
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1198
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1199
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1200
//// details.                                                     ////
1201
////                                                              ////
1202
//// You should have received a copy of the GNU Lesser General    ////
1203
//// Public License along with this source; if not, download it   ////
1204
//// from http://www.opencores.org/lgpl.shtml                     ////
1205
////                                                              ////
1206
//////////////////////////////////////////////////////////////////////
1207
 
1208 25 unneback
// binary counter
1209
module vl_cnt_bin_ce_rew_zq_l1 ( cke, rew, zq, level1, rst, clk);
1210
 
1211
   parameter length = 4;
1212
   input cke;
1213
   input rew;
1214
   output reg zq;
1215
   output reg level1;
1216
   input rst;
1217
   input clk;
1218
 
1219
   parameter clear_value = 0;
1220
   parameter set_value = 1;
1221
   parameter wrap_value = 1;
1222
   parameter level1_value = 15;
1223
 
1224
   reg  [length:1] qi;
1225
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1226
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1227
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1228
   assign q_next = rew ? q_next_rew : q_next_fw;
1229
 
1230
   always @ (posedge clk or posedge rst)
1231
     if (rst)
1232
       qi <= {length{1'b0}};
1233
     else
1234
     if (cke)
1235
       qi <= q_next;
1236
 
1237
 
1238
 
1239
   always @ (posedge clk or posedge rst)
1240
     if (rst)
1241
       zq <= 1'b1;
1242
     else
1243
     if (cke)
1244
       zq <= q_next == {length{1'b0}};
1245
 
1246
    always @ (posedge clk or posedge rst)
1247
    if (rst)
1248
        level1 <= 1'b0;
1249
    else
1250
    if (cke)
1251
    if (q_next == level1_value)
1252
        level1 <= 1'b1;
1253
    else if (qi == level1_value & rew)
1254
        level1 <= 1'b0;
1255
endmodule
1256
//////////////////////////////////////////////////////////////////////
1257
////                                                              ////
1258
////  Versatile counter                                           ////
1259
////                                                              ////
1260
////  Description                                                 ////
1261
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1262
////  counter                                                     ////
1263
////                                                              ////
1264
////  To Do:                                                      ////
1265
////   - add LFSR with more taps                                  ////
1266
////                                                              ////
1267
////  Author(s):                                                  ////
1268
////      - Michael Unneback, unneback@opencores.org              ////
1269
////        ORSoC AB                                              ////
1270
////                                                              ////
1271
//////////////////////////////////////////////////////////////////////
1272
////                                                              ////
1273
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1274
////                                                              ////
1275
//// This source file may be used and distributed without         ////
1276
//// restriction provided that this copyright statement is not    ////
1277
//// removed from the file and that any derivative work contains  ////
1278
//// the original copyright notice and the associated disclaimer. ////
1279
////                                                              ////
1280
//// This source file is free software; you can redistribute it   ////
1281
//// and/or modify it under the terms of the GNU Lesser General   ////
1282
//// Public License as published by the Free Software Foundation; ////
1283
//// either version 2.1 of the License, or (at your option) any   ////
1284
//// later version.                                               ////
1285
////                                                              ////
1286
//// This source is distributed in the hope that it will be       ////
1287
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1288
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1289
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1290
//// details.                                                     ////
1291
////                                                              ////
1292
//// You should have received a copy of the GNU Lesser General    ////
1293
//// Public License along with this source; if not, download it   ////
1294
//// from http://www.opencores.org/lgpl.shtml                     ////
1295
////                                                              ////
1296
//////////////////////////////////////////////////////////////////////
1297
 
1298
// binary counter
1299
module vl_cnt_bin_ce_rew_q_zq_l1 ( cke, rew, q, zq, level1, rst, clk);
1300
 
1301
   parameter length = 4;
1302
   input cke;
1303
   input rew;
1304
   output [length:1] q;
1305
   output reg zq;
1306
   output reg level1;
1307
   input rst;
1308
   input clk;
1309
 
1310
   parameter clear_value = 0;
1311
   parameter set_value = 1;
1312
   parameter wrap_value = 1;
1313
   parameter level1_value = 15;
1314
 
1315
   reg  [length:1] qi;
1316
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1317
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1318
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1319
   assign q_next = rew ? q_next_rew : q_next_fw;
1320
 
1321
   always @ (posedge clk or posedge rst)
1322
     if (rst)
1323
       qi <= {length{1'b0}};
1324
     else
1325
     if (cke)
1326
       qi <= q_next;
1327
 
1328
   assign q = qi;
1329
 
1330
 
1331
   always @ (posedge clk or posedge rst)
1332
     if (rst)
1333
       zq <= 1'b1;
1334
     else
1335
     if (cke)
1336
       zq <= q_next == {length{1'b0}};
1337
 
1338
    always @ (posedge clk or posedge rst)
1339
    if (rst)
1340
        level1 <= 1'b0;
1341
    else
1342
    if (cke)
1343
    if (q_next == level1_value)
1344
        level1 <= 1'b1;
1345
    else if (qi == level1_value & rew)
1346
        level1 <= 1'b0;
1347
endmodule
1348
//////////////////////////////////////////////////////////////////////
1349
////                                                              ////
1350
////  Versatile counter                                           ////
1351
////                                                              ////
1352
////  Description                                                 ////
1353
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1354
////  counter                                                     ////
1355
////                                                              ////
1356
////  To Do:                                                      ////
1357
////   - add LFSR with more taps                                  ////
1358
////                                                              ////
1359
////  Author(s):                                                  ////
1360
////      - Michael Unneback, unneback@opencores.org              ////
1361
////        ORSoC AB                                              ////
1362
////                                                              ////
1363
//////////////////////////////////////////////////////////////////////
1364
////                                                              ////
1365
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1366
////                                                              ////
1367
//// This source file may be used and distributed without         ////
1368
//// restriction provided that this copyright statement is not    ////
1369
//// removed from the file and that any derivative work contains  ////
1370
//// the original copyright notice and the associated disclaimer. ////
1371
////                                                              ////
1372
//// This source file is free software; you can redistribute it   ////
1373
//// and/or modify it under the terms of the GNU Lesser General   ////
1374
//// Public License as published by the Free Software Foundation; ////
1375
//// either version 2.1 of the License, or (at your option) any   ////
1376
//// later version.                                               ////
1377
////                                                              ////
1378
//// This source is distributed in the hope that it will be       ////
1379
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1380
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1381
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1382
//// details.                                                     ////
1383
////                                                              ////
1384
//// You should have received a copy of the GNU Lesser General    ////
1385
//// Public License along with this source; if not, download it   ////
1386
//// from http://www.opencores.org/lgpl.shtml                     ////
1387
////                                                              ////
1388
//////////////////////////////////////////////////////////////////////
1389
 
1390 6 unneback
// LFSR counter
1391 18 unneback
module vl_cnt_lfsr_zq ( zq, rst, clk);
1392 6 unneback
 
1393
   parameter length = 4;
1394
   output reg zq;
1395
   input rst;
1396
   input clk;
1397
 
1398
   parameter clear_value = 0;
1399
   parameter set_value = 1;
1400
   parameter wrap_value = 8;
1401
   parameter level1_value = 15;
1402
 
1403
   reg  [length:1] qi;
1404
   reg lfsr_fb;
1405
   wire [length:1] q_next;
1406
   reg [32:1] polynom;
1407
   integer i;
1408
 
1409
   always @ (qi)
1410
   begin
1411
        case (length)
1412
         2: polynom = 32'b11;                               // 0x3
1413
         3: polynom = 32'b110;                              // 0x6
1414
         4: polynom = 32'b1100;                             // 0xC
1415
         5: polynom = 32'b10100;                            // 0x14
1416
         6: polynom = 32'b110000;                           // 0x30
1417
         7: polynom = 32'b1100000;                          // 0x60
1418
         8: polynom = 32'b10111000;                         // 0xb8
1419
         9: polynom = 32'b100010000;                        // 0x110
1420
        10: polynom = 32'b1001000000;                       // 0x240
1421
        11: polynom = 32'b10100000000;                      // 0x500
1422
        12: polynom = 32'b100000101001;                     // 0x829
1423
        13: polynom = 32'b1000000001100;                    // 0x100C
1424
        14: polynom = 32'b10000000010101;                   // 0x2015
1425
        15: polynom = 32'b110000000000000;                  // 0x6000
1426
        16: polynom = 32'b1101000000001000;                 // 0xD008
1427
        17: polynom = 32'b10010000000000000;                // 0x12000
1428
        18: polynom = 32'b100000010000000000;               // 0x20400
1429
        19: polynom = 32'b1000000000000100011;              // 0x40023
1430
        20: polynom = 32'b10000010000000000000;             // 0x82000
1431
        21: polynom = 32'b101000000000000000000;            // 0x140000
1432
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1433
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1434
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1435
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1436
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1437
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1438
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1439
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1440
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1441
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1442
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1443
        default: polynom = 32'b0;
1444
        endcase
1445
        lfsr_fb = qi[length];
1446
        for (i=length-1; i>=1; i=i-1) begin
1447
            if (polynom[i])
1448
                lfsr_fb = lfsr_fb  ~^ qi[i];
1449
        end
1450
    end
1451
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1452
 
1453
   always @ (posedge clk or posedge rst)
1454
     if (rst)
1455
       qi <= {length{1'b0}};
1456
     else
1457
       qi <= q_next;
1458
 
1459
 
1460
 
1461
   always @ (posedge clk or posedge rst)
1462
     if (rst)
1463
       zq <= 1'b1;
1464
     else
1465
       zq <= q_next == {length{1'b0}};
1466
endmodule
1467
//////////////////////////////////////////////////////////////////////
1468
////                                                              ////
1469
////  Versatile counter                                           ////
1470
////                                                              ////
1471
////  Description                                                 ////
1472
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1473
////  counter                                                     ////
1474
////                                                              ////
1475
////  To Do:                                                      ////
1476
////   - add LFSR with more taps                                  ////
1477
////                                                              ////
1478
////  Author(s):                                                  ////
1479
////      - Michael Unneback, unneback@opencores.org              ////
1480
////        ORSoC AB                                              ////
1481
////                                                              ////
1482
//////////////////////////////////////////////////////////////////////
1483
////                                                              ////
1484
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1485
////                                                              ////
1486
//// This source file may be used and distributed without         ////
1487
//// restriction provided that this copyright statement is not    ////
1488
//// removed from the file and that any derivative work contains  ////
1489
//// the original copyright notice and the associated disclaimer. ////
1490
////                                                              ////
1491
//// This source file is free software; you can redistribute it   ////
1492
//// and/or modify it under the terms of the GNU Lesser General   ////
1493
//// Public License as published by the Free Software Foundation; ////
1494
//// either version 2.1 of the License, or (at your option) any   ////
1495
//// later version.                                               ////
1496
////                                                              ////
1497
//// This source is distributed in the hope that it will be       ////
1498
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1499
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1500
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1501
//// details.                                                     ////
1502
////                                                              ////
1503
//// You should have received a copy of the GNU Lesser General    ////
1504
//// Public License along with this source; if not, download it   ////
1505
//// from http://www.opencores.org/lgpl.shtml                     ////
1506
////                                                              ////
1507
//////////////////////////////////////////////////////////////////////
1508
 
1509
// LFSR counter
1510 18 unneback
module vl_cnt_lfsr_ce_zq ( cke, zq, rst, clk);
1511 6 unneback
 
1512
   parameter length = 4;
1513
   input cke;
1514
   output reg zq;
1515
   input rst;
1516
   input clk;
1517
 
1518
   parameter clear_value = 0;
1519
   parameter set_value = 1;
1520
   parameter wrap_value = 8;
1521
   parameter level1_value = 15;
1522
 
1523
   reg  [length:1] qi;
1524
   reg lfsr_fb;
1525
   wire [length:1] q_next;
1526
   reg [32:1] polynom;
1527
   integer i;
1528
 
1529
   always @ (qi)
1530
   begin
1531
        case (length)
1532
         2: polynom = 32'b11;                               // 0x3
1533
         3: polynom = 32'b110;                              // 0x6
1534
         4: polynom = 32'b1100;                             // 0xC
1535
         5: polynom = 32'b10100;                            // 0x14
1536
         6: polynom = 32'b110000;                           // 0x30
1537
         7: polynom = 32'b1100000;                          // 0x60
1538
         8: polynom = 32'b10111000;                         // 0xb8
1539
         9: polynom = 32'b100010000;                        // 0x110
1540
        10: polynom = 32'b1001000000;                       // 0x240
1541
        11: polynom = 32'b10100000000;                      // 0x500
1542
        12: polynom = 32'b100000101001;                     // 0x829
1543
        13: polynom = 32'b1000000001100;                    // 0x100C
1544
        14: polynom = 32'b10000000010101;                   // 0x2015
1545
        15: polynom = 32'b110000000000000;                  // 0x6000
1546
        16: polynom = 32'b1101000000001000;                 // 0xD008
1547
        17: polynom = 32'b10010000000000000;                // 0x12000
1548
        18: polynom = 32'b100000010000000000;               // 0x20400
1549
        19: polynom = 32'b1000000000000100011;              // 0x40023
1550
        20: polynom = 32'b10000010000000000000;             // 0x82000
1551
        21: polynom = 32'b101000000000000000000;            // 0x140000
1552
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1553
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1554
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1555
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1556
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1557
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1558
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1559
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1560
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1561
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1562
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1563
        default: polynom = 32'b0;
1564
        endcase
1565
        lfsr_fb = qi[length];
1566
        for (i=length-1; i>=1; i=i-1) begin
1567
            if (polynom[i])
1568
                lfsr_fb = lfsr_fb  ~^ qi[i];
1569
        end
1570
    end
1571
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1572
 
1573
   always @ (posedge clk or posedge rst)
1574
     if (rst)
1575
       qi <= {length{1'b0}};
1576
     else
1577
     if (cke)
1578
       qi <= q_next;
1579
 
1580
 
1581
 
1582
   always @ (posedge clk or posedge rst)
1583
     if (rst)
1584
       zq <= 1'b1;
1585
     else
1586
     if (cke)
1587
       zq <= q_next == {length{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 22 unneback
 
1631
// LFSR counter
1632 27 unneback
module vl_cnt_lfsr_ce_q ( cke, q, rst, clk);
1633
 
1634
   parameter length = 4;
1635
   input cke;
1636
   output [length:1] q;
1637
   input rst;
1638
   input clk;
1639
 
1640
   parameter clear_value = 0;
1641
   parameter set_value = 1;
1642
   parameter wrap_value = 8;
1643
   parameter level1_value = 15;
1644
 
1645
   reg  [length:1] qi;
1646
   reg lfsr_fb;
1647
   wire [length:1] q_next;
1648
   reg [32:1] polynom;
1649
   integer i;
1650
 
1651
   always @ (qi)
1652
   begin
1653
        case (length)
1654
         2: polynom = 32'b11;                               // 0x3
1655
         3: polynom = 32'b110;                              // 0x6
1656
         4: polynom = 32'b1100;                             // 0xC
1657
         5: polynom = 32'b10100;                            // 0x14
1658
         6: polynom = 32'b110000;                           // 0x30
1659
         7: polynom = 32'b1100000;                          // 0x60
1660
         8: polynom = 32'b10111000;                         // 0xb8
1661
         9: polynom = 32'b100010000;                        // 0x110
1662
        10: polynom = 32'b1001000000;                       // 0x240
1663
        11: polynom = 32'b10100000000;                      // 0x500
1664
        12: polynom = 32'b100000101001;                     // 0x829
1665
        13: polynom = 32'b1000000001100;                    // 0x100C
1666
        14: polynom = 32'b10000000010101;                   // 0x2015
1667
        15: polynom = 32'b110000000000000;                  // 0x6000
1668
        16: polynom = 32'b1101000000001000;                 // 0xD008
1669
        17: polynom = 32'b10010000000000000;                // 0x12000
1670
        18: polynom = 32'b100000010000000000;               // 0x20400
1671
        19: polynom = 32'b1000000000000100011;              // 0x40023
1672
        20: polynom = 32'b10000010000000000000;             // 0x82000
1673
        21: polynom = 32'b101000000000000000000;            // 0x140000
1674
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1675
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1676
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1677
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1678
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1679
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1680
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1681
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1682
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1683
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1684
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1685
        default: polynom = 32'b0;
1686
        endcase
1687
        lfsr_fb = qi[length];
1688
        for (i=length-1; i>=1; i=i-1) begin
1689
            if (polynom[i])
1690
                lfsr_fb = lfsr_fb  ~^ qi[i];
1691
        end
1692
    end
1693
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1694
 
1695
   always @ (posedge clk or posedge rst)
1696
     if (rst)
1697
       qi <= {length{1'b0}};
1698
     else
1699
     if (cke)
1700
       qi <= q_next;
1701
 
1702
   assign q = qi;
1703
 
1704
endmodule
1705
//////////////////////////////////////////////////////////////////////
1706
////                                                              ////
1707
////  Versatile counter                                           ////
1708
////                                                              ////
1709
////  Description                                                 ////
1710
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1711
////  counter                                                     ////
1712
////                                                              ////
1713
////  To Do:                                                      ////
1714
////   - add LFSR with more taps                                  ////
1715
////                                                              ////
1716
////  Author(s):                                                  ////
1717
////      - Michael Unneback, unneback@opencores.org              ////
1718
////        ORSoC AB                                              ////
1719
////                                                              ////
1720
//////////////////////////////////////////////////////////////////////
1721
////                                                              ////
1722
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1723
////                                                              ////
1724
//// This source file may be used and distributed without         ////
1725
//// restriction provided that this copyright statement is not    ////
1726
//// removed from the file and that any derivative work contains  ////
1727
//// the original copyright notice and the associated disclaimer. ////
1728
////                                                              ////
1729
//// This source file is free software; you can redistribute it   ////
1730
//// and/or modify it under the terms of the GNU Lesser General   ////
1731
//// Public License as published by the Free Software Foundation; ////
1732
//// either version 2.1 of the License, or (at your option) any   ////
1733
//// later version.                                               ////
1734
////                                                              ////
1735
//// This source is distributed in the hope that it will be       ////
1736
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1737
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1738
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1739
//// details.                                                     ////
1740
////                                                              ////
1741
//// You should have received a copy of the GNU Lesser General    ////
1742
//// Public License along with this source; if not, download it   ////
1743
//// from http://www.opencores.org/lgpl.shtml                     ////
1744
////                                                              ////
1745
//////////////////////////////////////////////////////////////////////
1746
 
1747
// LFSR counter
1748
module vl_cnt_lfsr_ce_clear_q ( clear, cke, q, rst, clk);
1749
 
1750
   parameter length = 4;
1751
   input clear;
1752
   input cke;
1753
   output [length:1] q;
1754
   input rst;
1755
   input clk;
1756
 
1757
   parameter clear_value = 0;
1758
   parameter set_value = 1;
1759
   parameter wrap_value = 8;
1760
   parameter level1_value = 15;
1761
 
1762
   reg  [length:1] qi;
1763
   reg lfsr_fb;
1764
   wire [length:1] q_next;
1765
   reg [32:1] polynom;
1766
   integer i;
1767
 
1768
   always @ (qi)
1769
   begin
1770
        case (length)
1771
         2: polynom = 32'b11;                               // 0x3
1772
         3: polynom = 32'b110;                              // 0x6
1773
         4: polynom = 32'b1100;                             // 0xC
1774
         5: polynom = 32'b10100;                            // 0x14
1775
         6: polynom = 32'b110000;                           // 0x30
1776
         7: polynom = 32'b1100000;                          // 0x60
1777
         8: polynom = 32'b10111000;                         // 0xb8
1778
         9: polynom = 32'b100010000;                        // 0x110
1779
        10: polynom = 32'b1001000000;                       // 0x240
1780
        11: polynom = 32'b10100000000;                      // 0x500
1781
        12: polynom = 32'b100000101001;                     // 0x829
1782
        13: polynom = 32'b1000000001100;                    // 0x100C
1783
        14: polynom = 32'b10000000010101;                   // 0x2015
1784
        15: polynom = 32'b110000000000000;                  // 0x6000
1785
        16: polynom = 32'b1101000000001000;                 // 0xD008
1786
        17: polynom = 32'b10010000000000000;                // 0x12000
1787
        18: polynom = 32'b100000010000000000;               // 0x20400
1788
        19: polynom = 32'b1000000000000100011;              // 0x40023
1789
        20: polynom = 32'b10000010000000000000;             // 0x82000
1790
        21: polynom = 32'b101000000000000000000;            // 0x140000
1791
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1792
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1793
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1794
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1795
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1796
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1797
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1798
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1799
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1800
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1801
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1802
        default: polynom = 32'b0;
1803
        endcase
1804
        lfsr_fb = qi[length];
1805
        for (i=length-1; i>=1; i=i-1) begin
1806
            if (polynom[i])
1807
                lfsr_fb = lfsr_fb  ~^ qi[i];
1808
        end
1809
    end
1810
   assign q_next =  clear ? {length{1'b0}} :(qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1811
 
1812
   always @ (posedge clk or posedge rst)
1813
     if (rst)
1814
       qi <= {length{1'b0}};
1815
     else
1816
     if (cke)
1817
       qi <= q_next;
1818
 
1819
   assign q = qi;
1820
 
1821
endmodule
1822
//////////////////////////////////////////////////////////////////////
1823
////                                                              ////
1824
////  Versatile counter                                           ////
1825
////                                                              ////
1826
////  Description                                                 ////
1827
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1828
////  counter                                                     ////
1829
////                                                              ////
1830
////  To Do:                                                      ////
1831
////   - add LFSR with more taps                                  ////
1832
////                                                              ////
1833
////  Author(s):                                                  ////
1834
////      - Michael Unneback, unneback@opencores.org              ////
1835
////        ORSoC AB                                              ////
1836
////                                                              ////
1837
//////////////////////////////////////////////////////////////////////
1838
////                                                              ////
1839
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1840
////                                                              ////
1841
//// This source file may be used and distributed without         ////
1842
//// restriction provided that this copyright statement is not    ////
1843
//// removed from the file and that any derivative work contains  ////
1844
//// the original copyright notice and the associated disclaimer. ////
1845
////                                                              ////
1846
//// This source file is free software; you can redistribute it   ////
1847
//// and/or modify it under the terms of the GNU Lesser General   ////
1848
//// Public License as published by the Free Software Foundation; ////
1849
//// either version 2.1 of the License, or (at your option) any   ////
1850
//// later version.                                               ////
1851
////                                                              ////
1852
//// This source is distributed in the hope that it will be       ////
1853
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1854
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1855
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1856
//// details.                                                     ////
1857
////                                                              ////
1858
//// You should have received a copy of the GNU Lesser General    ////
1859
//// Public License along with this source; if not, download it   ////
1860
//// from http://www.opencores.org/lgpl.shtml                     ////
1861
////                                                              ////
1862
//////////////////////////////////////////////////////////////////////
1863
 
1864
// LFSR counter
1865 22 unneback
module vl_cnt_lfsr_ce_q_zq ( cke, q, zq, rst, clk);
1866
 
1867
   parameter length = 4;
1868
   input cke;
1869
   output [length:1] q;
1870
   output reg zq;
1871
   input rst;
1872
   input clk;
1873
 
1874
   parameter clear_value = 0;
1875
   parameter set_value = 1;
1876
   parameter wrap_value = 8;
1877
   parameter level1_value = 15;
1878
 
1879
   reg  [length:1] qi;
1880
   reg lfsr_fb;
1881
   wire [length:1] q_next;
1882
   reg [32:1] polynom;
1883
   integer i;
1884
 
1885
   always @ (qi)
1886
   begin
1887
        case (length)
1888
         2: polynom = 32'b11;                               // 0x3
1889
         3: polynom = 32'b110;                              // 0x6
1890
         4: polynom = 32'b1100;                             // 0xC
1891
         5: polynom = 32'b10100;                            // 0x14
1892
         6: polynom = 32'b110000;                           // 0x30
1893
         7: polynom = 32'b1100000;                          // 0x60
1894
         8: polynom = 32'b10111000;                         // 0xb8
1895
         9: polynom = 32'b100010000;                        // 0x110
1896
        10: polynom = 32'b1001000000;                       // 0x240
1897
        11: polynom = 32'b10100000000;                      // 0x500
1898
        12: polynom = 32'b100000101001;                     // 0x829
1899
        13: polynom = 32'b1000000001100;                    // 0x100C
1900
        14: polynom = 32'b10000000010101;                   // 0x2015
1901
        15: polynom = 32'b110000000000000;                  // 0x6000
1902
        16: polynom = 32'b1101000000001000;                 // 0xD008
1903
        17: polynom = 32'b10010000000000000;                // 0x12000
1904
        18: polynom = 32'b100000010000000000;               // 0x20400
1905
        19: polynom = 32'b1000000000000100011;              // 0x40023
1906
        20: polynom = 32'b10000010000000000000;             // 0x82000
1907
        21: polynom = 32'b101000000000000000000;            // 0x140000
1908
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1909
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1910
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1911
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1912
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1913
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1914
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1915
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1916
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1917
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1918
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1919
        default: polynom = 32'b0;
1920
        endcase
1921
        lfsr_fb = qi[length];
1922
        for (i=length-1; i>=1; i=i-1) begin
1923
            if (polynom[i])
1924
                lfsr_fb = lfsr_fb  ~^ qi[i];
1925
        end
1926
    end
1927
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1928
 
1929
   always @ (posedge clk or posedge rst)
1930
     if (rst)
1931
       qi <= {length{1'b0}};
1932
     else
1933
     if (cke)
1934
       qi <= q_next;
1935
 
1936
   assign q = qi;
1937
 
1938
 
1939
   always @ (posedge clk or posedge rst)
1940
     if (rst)
1941
       zq <= 1'b1;
1942
     else
1943
     if (cke)
1944
       zq <= q_next == {length{1'b0}};
1945
endmodule
1946
//////////////////////////////////////////////////////////////////////
1947
////                                                              ////
1948
////  Versatile counter                                           ////
1949
////                                                              ////
1950
////  Description                                                 ////
1951
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1952
////  counter                                                     ////
1953
////                                                              ////
1954
////  To Do:                                                      ////
1955
////   - add LFSR with more taps                                  ////
1956
////                                                              ////
1957
////  Author(s):                                                  ////
1958
////      - Michael Unneback, unneback@opencores.org              ////
1959
////        ORSoC AB                                              ////
1960
////                                                              ////
1961
//////////////////////////////////////////////////////////////////////
1962
////                                                              ////
1963
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1964
////                                                              ////
1965
//// This source file may be used and distributed without         ////
1966
//// restriction provided that this copyright statement is not    ////
1967
//// removed from the file and that any derivative work contains  ////
1968
//// the original copyright notice and the associated disclaimer. ////
1969
////                                                              ////
1970
//// This source file is free software; you can redistribute it   ////
1971
//// and/or modify it under the terms of the GNU Lesser General   ////
1972
//// Public License as published by the Free Software Foundation; ////
1973
//// either version 2.1 of the License, or (at your option) any   ////
1974
//// later version.                                               ////
1975
////                                                              ////
1976
//// This source is distributed in the hope that it will be       ////
1977
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1978
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1979
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1980
//// details.                                                     ////
1981
////                                                              ////
1982
//// You should have received a copy of the GNU Lesser General    ////
1983
//// Public License along with this source; if not, download it   ////
1984
//// from http://www.opencores.org/lgpl.shtml                     ////
1985
////                                                              ////
1986
//////////////////////////////////////////////////////////////////////
1987 6 unneback
 
1988
// LFSR counter
1989 18 unneback
module vl_cnt_lfsr_ce_rew_l1 ( cke, rew, level1, rst, clk);
1990 6 unneback
 
1991
   parameter length = 4;
1992
   input cke;
1993
   input rew;
1994
   output reg level1;
1995
   input rst;
1996
   input clk;
1997
 
1998
   parameter clear_value = 0;
1999
   parameter set_value = 1;
2000
   parameter wrap_value = 8;
2001
   parameter level1_value = 15;
2002
 
2003
   reg  [length:1] qi;
2004
   reg lfsr_fb, lfsr_fb_rew;
2005
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2006
   reg [32:1] polynom_rew;
2007
   integer j;
2008
   reg [32:1] polynom;
2009
   integer i;
2010
 
2011
   always @ (qi)
2012
   begin
2013
        case (length)
2014
         2: polynom = 32'b11;                               // 0x3
2015
         3: polynom = 32'b110;                              // 0x6
2016
         4: polynom = 32'b1100;                             // 0xC
2017
         5: polynom = 32'b10100;                            // 0x14
2018
         6: polynom = 32'b110000;                           // 0x30
2019
         7: polynom = 32'b1100000;                          // 0x60
2020
         8: polynom = 32'b10111000;                         // 0xb8
2021
         9: polynom = 32'b100010000;                        // 0x110
2022
        10: polynom = 32'b1001000000;                       // 0x240
2023
        11: polynom = 32'b10100000000;                      // 0x500
2024
        12: polynom = 32'b100000101001;                     // 0x829
2025
        13: polynom = 32'b1000000001100;                    // 0x100C
2026
        14: polynom = 32'b10000000010101;                   // 0x2015
2027
        15: polynom = 32'b110000000000000;                  // 0x6000
2028
        16: polynom = 32'b1101000000001000;                 // 0xD008
2029
        17: polynom = 32'b10010000000000000;                // 0x12000
2030
        18: polynom = 32'b100000010000000000;               // 0x20400
2031
        19: polynom = 32'b1000000000000100011;              // 0x40023
2032
        20: polynom = 32'b10000010000000000000;             // 0x82000
2033
        21: polynom = 32'b101000000000000000000;            // 0x140000
2034
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2035
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2036
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2037
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2038
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2039
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2040
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2041
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2042
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2043
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2044
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2045
        default: polynom = 32'b0;
2046
        endcase
2047
        lfsr_fb = qi[length];
2048
        for (i=length-1; i>=1; i=i-1) begin
2049
            if (polynom[i])
2050
                lfsr_fb = lfsr_fb  ~^ qi[i];
2051
        end
2052
    end
2053
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2054
   always @ (qi)
2055
   begin
2056
        case (length)
2057
         2: polynom_rew = 32'b11;
2058
         3: polynom_rew = 32'b110;
2059
         4: polynom_rew = 32'b1100;
2060
         5: polynom_rew = 32'b10100;
2061
         6: polynom_rew = 32'b110000;
2062
         7: polynom_rew = 32'b1100000;
2063
         8: polynom_rew = 32'b10111000;
2064
         9: polynom_rew = 32'b100010000;
2065
        10: polynom_rew = 32'b1001000000;
2066
        11: polynom_rew = 32'b10100000000;
2067
        12: polynom_rew = 32'b100000101001;
2068
        13: polynom_rew = 32'b1000000001100;
2069
        14: polynom_rew = 32'b10000000010101;
2070
        15: polynom_rew = 32'b110000000000000;
2071
        16: polynom_rew = 32'b1101000000001000;
2072
        17: polynom_rew = 32'b10010000000000000;
2073
        18: polynom_rew = 32'b100000010000000000;
2074
        19: polynom_rew = 32'b1000000000000100011;
2075
        20: polynom_rew = 32'b10000010000000000000;
2076
        21: polynom_rew = 32'b101000000000000000000;
2077
        22: polynom_rew = 32'b1100000000000000000000;
2078
        23: polynom_rew = 32'b10000100000000000000000;
2079
        24: polynom_rew = 32'b111000010000000000000000;
2080
        25: polynom_rew = 32'b1001000000000000000000000;
2081
        26: polynom_rew = 32'b10000000000000000000100011;
2082
        27: polynom_rew = 32'b100000000000000000000010011;
2083
        28: polynom_rew = 32'b1100100000000000000000000000;
2084
        29: polynom_rew = 32'b10100000000000000000000000000;
2085
        30: polynom_rew = 32'b100000000000000000000000101001;
2086
        31: polynom_rew = 32'b1001000000000000000000000000000;
2087
        32: polynom_rew = 32'b10000000001000000000000000000011;
2088
        default: polynom_rew = 32'b0;
2089
        endcase
2090
        // rotate left
2091
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
2092
        lfsr_fb_rew = qi[length];
2093
        for (i=length-1; i>=1; i=i-1) begin
2094
            if (polynom_rew[i])
2095
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
2096
        end
2097
    end
2098
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
2099
   assign q_next = rew ? q_next_rew : q_next_fw;
2100
 
2101
   always @ (posedge clk or posedge rst)
2102
     if (rst)
2103
       qi <= {length{1'b0}};
2104
     else
2105
     if (cke)
2106
       qi <= q_next;
2107
 
2108
 
2109
 
2110
    always @ (posedge clk or posedge rst)
2111
    if (rst)
2112
        level1 <= 1'b0;
2113
    else
2114
    if (cke)
2115
    if (q_next == level1_value)
2116
        level1 <= 1'b1;
2117
    else if (qi == level1_value & rew)
2118
        level1 <= 1'b0;
2119
endmodule
2120
//////////////////////////////////////////////////////////////////////
2121
////                                                              ////
2122
////  Versatile counter                                           ////
2123
////                                                              ////
2124
////  Description                                                 ////
2125
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2126
////  counter                                                     ////
2127
////                                                              ////
2128
////  To Do:                                                      ////
2129
////   - add LFSR with more taps                                  ////
2130
////                                                              ////
2131
////  Author(s):                                                  ////
2132
////      - Michael Unneback, unneback@opencores.org              ////
2133
////        ORSoC AB                                              ////
2134
////                                                              ////
2135
//////////////////////////////////////////////////////////////////////
2136
////                                                              ////
2137
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2138
////                                                              ////
2139
//// This source file may be used and distributed without         ////
2140
//// restriction provided that this copyright statement is not    ////
2141
//// removed from the file and that any derivative work contains  ////
2142
//// the original copyright notice and the associated disclaimer. ////
2143
////                                                              ////
2144
//// This source file is free software; you can redistribute it   ////
2145
//// and/or modify it under the terms of the GNU Lesser General   ////
2146
//// Public License as published by the Free Software Foundation; ////
2147
//// either version 2.1 of the License, or (at your option) any   ////
2148
//// later version.                                               ////
2149
////                                                              ////
2150
//// This source is distributed in the hope that it will be       ////
2151
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2152
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2153
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2154
//// details.                                                     ////
2155
////                                                              ////
2156
//// You should have received a copy of the GNU Lesser General    ////
2157
//// Public License along with this source; if not, download it   ////
2158
//// from http://www.opencores.org/lgpl.shtml                     ////
2159
////                                                              ////
2160
//////////////////////////////////////////////////////////////////////
2161
 
2162
// GRAY counter
2163 18 unneback
module vl_cnt_gray ( q, rst, clk);
2164 6 unneback
 
2165
   parameter length = 4;
2166
   output reg [length:1] q;
2167
   input rst;
2168
   input clk;
2169
 
2170
   parameter clear_value = 0;
2171
   parameter set_value = 1;
2172
   parameter wrap_value = 8;
2173
   parameter level1_value = 15;
2174
 
2175
   reg  [length:1] qi;
2176
   wire [length:1] q_next;
2177
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2178
 
2179
   always @ (posedge clk or posedge rst)
2180
     if (rst)
2181
       qi <= {length{1'b0}};
2182
     else
2183
       qi <= q_next;
2184
 
2185
   always @ (posedge clk or posedge rst)
2186
     if (rst)
2187
       q <= {length{1'b0}};
2188
     else
2189
         q <= (q_next>>1) ^ q_next;
2190
 
2191
endmodule
2192
//////////////////////////////////////////////////////////////////////
2193
////                                                              ////
2194
////  Versatile counter                                           ////
2195
////                                                              ////
2196
////  Description                                                 ////
2197
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2198
////  counter                                                     ////
2199
////                                                              ////
2200
////  To Do:                                                      ////
2201
////   - add LFSR with more taps                                  ////
2202
////                                                              ////
2203
////  Author(s):                                                  ////
2204
////      - Michael Unneback, unneback@opencores.org              ////
2205
////        ORSoC AB                                              ////
2206
////                                                              ////
2207
//////////////////////////////////////////////////////////////////////
2208
////                                                              ////
2209
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2210
////                                                              ////
2211
//// This source file may be used and distributed without         ////
2212
//// restriction provided that this copyright statement is not    ////
2213
//// removed from the file and that any derivative work contains  ////
2214
//// the original copyright notice and the associated disclaimer. ////
2215
////                                                              ////
2216
//// This source file is free software; you can redistribute it   ////
2217
//// and/or modify it under the terms of the GNU Lesser General   ////
2218
//// Public License as published by the Free Software Foundation; ////
2219
//// either version 2.1 of the License, or (at your option) any   ////
2220
//// later version.                                               ////
2221
////                                                              ////
2222
//// This source is distributed in the hope that it will be       ////
2223
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2224
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2225
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2226
//// details.                                                     ////
2227
////                                                              ////
2228
//// You should have received a copy of the GNU Lesser General    ////
2229
//// Public License along with this source; if not, download it   ////
2230
//// from http://www.opencores.org/lgpl.shtml                     ////
2231
////                                                              ////
2232
//////////////////////////////////////////////////////////////////////
2233
 
2234
// GRAY counter
2235 18 unneback
module vl_cnt_gray_ce ( cke, q, rst, clk);
2236 6 unneback
 
2237
   parameter length = 4;
2238
   input cke;
2239
   output reg [length:1] q;
2240
   input rst;
2241
   input clk;
2242
 
2243
   parameter clear_value = 0;
2244
   parameter set_value = 1;
2245
   parameter wrap_value = 8;
2246
   parameter level1_value = 15;
2247
 
2248
   reg  [length:1] qi;
2249
   wire [length:1] q_next;
2250
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2251
 
2252
   always @ (posedge clk or posedge rst)
2253
     if (rst)
2254
       qi <= {length{1'b0}};
2255
     else
2256
     if (cke)
2257
       qi <= q_next;
2258
 
2259
   always @ (posedge clk or posedge rst)
2260
     if (rst)
2261
       q <= {length{1'b0}};
2262
     else
2263
       if (cke)
2264
         q <= (q_next>>1) ^ q_next;
2265
 
2266
endmodule
2267
//////////////////////////////////////////////////////////////////////
2268
////                                                              ////
2269
////  Versatile counter                                           ////
2270
////                                                              ////
2271
////  Description                                                 ////
2272
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2273
////  counter                                                     ////
2274
////                                                              ////
2275
////  To Do:                                                      ////
2276
////   - add LFSR with more taps                                  ////
2277
////                                                              ////
2278
////  Author(s):                                                  ////
2279
////      - Michael Unneback, unneback@opencores.org              ////
2280
////        ORSoC AB                                              ////
2281
////                                                              ////
2282
//////////////////////////////////////////////////////////////////////
2283
////                                                              ////
2284
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2285
////                                                              ////
2286
//// This source file may be used and distributed without         ////
2287
//// restriction provided that this copyright statement is not    ////
2288
//// removed from the file and that any derivative work contains  ////
2289
//// the original copyright notice and the associated disclaimer. ////
2290
////                                                              ////
2291
//// This source file is free software; you can redistribute it   ////
2292
//// and/or modify it under the terms of the GNU Lesser General   ////
2293
//// Public License as published by the Free Software Foundation; ////
2294
//// either version 2.1 of the License, or (at your option) any   ////
2295
//// later version.                                               ////
2296
////                                                              ////
2297
//// This source is distributed in the hope that it will be       ////
2298
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2299
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2300
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2301
//// details.                                                     ////
2302
////                                                              ////
2303
//// You should have received a copy of the GNU Lesser General    ////
2304
//// Public License along with this source; if not, download it   ////
2305
//// from http://www.opencores.org/lgpl.shtml                     ////
2306
////                                                              ////
2307
//////////////////////////////////////////////////////////////////////
2308
 
2309
// GRAY counter
2310 18 unneback
module vl_cnt_gray_ce_bin ( cke, q, q_bin, rst, clk);
2311 6 unneback
 
2312
   parameter length = 4;
2313
   input cke;
2314
   output reg [length:1] q;
2315
   output [length:1] q_bin;
2316
   input rst;
2317
   input clk;
2318
 
2319
   parameter clear_value = 0;
2320
   parameter set_value = 1;
2321
   parameter wrap_value = 8;
2322
   parameter level1_value = 15;
2323
 
2324
   reg  [length:1] qi;
2325
   wire [length:1] q_next;
2326
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2327
 
2328
   always @ (posedge clk or posedge rst)
2329
     if (rst)
2330
       qi <= {length{1'b0}};
2331
     else
2332
     if (cke)
2333
       qi <= q_next;
2334
 
2335
   always @ (posedge clk or posedge rst)
2336
     if (rst)
2337
       q <= {length{1'b0}};
2338
     else
2339
       if (cke)
2340
         q <= (q_next>>1) ^ q_next;
2341
 
2342
   assign q_bin = qi;
2343
 
2344
endmodule
2345
//////////////////////////////////////////////////////////////////////
2346
////                                                              ////
2347
////  Versatile library, counters                                 ////
2348
////                                                              ////
2349
////  Description                                                 ////
2350
////  counters                                                    ////
2351
////                                                              ////
2352
////                                                              ////
2353
////  To Do:                                                      ////
2354
////   - add more counters                                        ////
2355
////                                                              ////
2356
////  Author(s):                                                  ////
2357
////      - Michael Unneback, unneback@opencores.org              ////
2358
////        ORSoC AB                                              ////
2359
////                                                              ////
2360
//////////////////////////////////////////////////////////////////////
2361
////                                                              ////
2362
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2363
////                                                              ////
2364
//// This source file may be used and distributed without         ////
2365
//// restriction provided that this copyright statement is not    ////
2366
//// removed from the file and that any derivative work contains  ////
2367
//// the original copyright notice and the associated disclaimer. ////
2368
////                                                              ////
2369
//// This source file is free software; you can redistribute it   ////
2370
//// and/or modify it under the terms of the GNU Lesser General   ////
2371
//// Public License as published by the Free Software Foundation; ////
2372
//// either version 2.1 of the License, or (at your option) any   ////
2373
//// later version.                                               ////
2374
////                                                              ////
2375
//// This source is distributed in the hope that it will be       ////
2376
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2377
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2378
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2379
//// details.                                                     ////
2380
////                                                              ////
2381
//// You should have received a copy of the GNU Lesser General    ////
2382
//// Public License along with this source; if not, download it   ////
2383
//// from http://www.opencores.org/lgpl.shtml                     ////
2384
////                                                              ////
2385
//////////////////////////////////////////////////////////////////////
2386
 
2387 18 unneback
module vl_cnt_shreg_wrap ( q, rst, clk);
2388 6 unneback
 
2389
   parameter length = 4;
2390
   output reg [0:length-1] q;
2391
   input rst;
2392
   input clk;
2393
 
2394
    always @ (posedge clk or posedge rst)
2395
    if (rst)
2396
        q <= {1'b1,{length-1{1'b0}}};
2397
    else
2398
        q <= {q[length-1],q[0:length-2]};
2399
 
2400
endmodule
2401
 
2402 18 unneback
module vl_cnt_shreg_ce_wrap ( cke, q, rst, clk);
2403 6 unneback
 
2404
   parameter length = 4;
2405
   input cke;
2406
   output reg [0:length-1] q;
2407
   input rst;
2408
   input clk;
2409
 
2410
    always @ (posedge clk or posedge rst)
2411
    if (rst)
2412
        q <= {1'b1,{length-1{1'b0}}};
2413
    else
2414
        if (cke)
2415
            q <= {q[length-1],q[0:length-2]};
2416
 
2417
endmodule
2418
 
2419 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
2420 6 unneback
 
2421
   parameter length = 4;
2422
   input cke, clear;
2423
   output reg [0:length-1] q;
2424
   input rst;
2425
   input clk;
2426
 
2427
    always @ (posedge clk or posedge rst)
2428
    if (rst)
2429
        q <= {1'b1,{length-1{1'b0}}};
2430
    else
2431
        if (cke)
2432
            if (clear)
2433
                q <= {1'b1,{length-1{1'b0}}};
2434
            else
2435
                q <= q >> 1;
2436
 
2437
endmodule
2438
 
2439 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
2440 6 unneback
 
2441
   parameter length = 4;
2442
   input cke, clear;
2443
   output reg [0:length-1] q;
2444
   input rst;
2445
   input clk;
2446
 
2447
    always @ (posedge clk or posedge rst)
2448
    if (rst)
2449
        q <= {1'b1,{length-1{1'b0}}};
2450
    else
2451
        if (cke)
2452
            if (clear)
2453
                q <= {1'b1,{length-1{1'b0}}};
2454
            else
2455
            q <= {q[length-1],q[0:length-2]};
2456
 
2457
endmodule
2458
//////////////////////////////////////////////////////////////////////
2459
////                                                              ////
2460
////  Versatile library, memories                                 ////
2461
////                                                              ////
2462
////  Description                                                 ////
2463
////  memories                                                    ////
2464
////                                                              ////
2465
////                                                              ////
2466
////  To Do:                                                      ////
2467
////   - add more memory types                                    ////
2468
////                                                              ////
2469
////  Author(s):                                                  ////
2470
////      - Michael Unneback, unneback@opencores.org              ////
2471
////        ORSoC AB                                              ////
2472
////                                                              ////
2473
//////////////////////////////////////////////////////////////////////
2474
////                                                              ////
2475
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2476
////                                                              ////
2477
//// This source file may be used and distributed without         ////
2478
//// restriction provided that this copyright statement is not    ////
2479
//// removed from the file and that any derivative work contains  ////
2480
//// the original copyright notice and the associated disclaimer. ////
2481
////                                                              ////
2482
//// This source file is free software; you can redistribute it   ////
2483
//// and/or modify it under the terms of the GNU Lesser General   ////
2484
//// Public License as published by the Free Software Foundation; ////
2485
//// either version 2.1 of the License, or (at your option) any   ////
2486
//// later version.                                               ////
2487
////                                                              ////
2488
//// This source is distributed in the hope that it will be       ////
2489
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2490
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2491
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2492
//// details.                                                     ////
2493
////                                                              ////
2494
//// You should have received a copy of the GNU Lesser General    ////
2495
//// Public License along with this source; if not, download it   ////
2496
//// from http://www.opencores.org/lgpl.shtml                     ////
2497
////                                                              ////
2498
//////////////////////////////////////////////////////////////////////
2499
 
2500
/// ROM
2501
 
2502 7 unneback
module vl_rom_init ( adr, q, clk);
2503
   parameter data_width = 32;
2504
   parameter addr_width = 8;
2505
   input [(addr_width-1):0]       adr;
2506
   output reg [(data_width-1):0] q;
2507
   input                         clk;
2508
   reg [data_width-1:0] rom [(1<<addr_width)-1:0];
2509
   parameter memory_file = "vl_rom.vmem";
2510
   initial
2511
     begin
2512
        $readmemh(memory_file, rom);
2513
     end
2514
 
2515
   always @ (posedge clk)
2516
     q <= rom[adr];
2517 6 unneback
 
2518 7 unneback
endmodule
2519
 
2520 14 unneback
/*
2521 7 unneback
module vl_rom ( adr, q, clk);
2522
 
2523 6 unneback
parameter data_width = 32;
2524
parameter addr_width = 4;
2525
 
2526
parameter [0:1>>addr_width-1] data [data_width-1:0] = {
2527
    {32'h18000000},
2528
    {32'hA8200000},
2529
    {32'hA8200000},
2530
    {32'hA8200000},
2531
    {32'h44003000},
2532
    {32'h15000000},
2533
    {32'h15000000},
2534
    {32'h15000000},
2535
    {32'h15000000},
2536
    {32'h15000000},
2537
    {32'h15000000},
2538
    {32'h15000000},
2539
    {32'h15000000},
2540
    {32'h15000000},
2541
    {32'h15000000},
2542
    {32'h15000000}};
2543
 
2544 7 unneback
input [addr_width-1:0] adr;
2545 6 unneback
output reg [data_width-1:0] q;
2546
input clk;
2547
 
2548
always @ (posedge clk)
2549 7 unneback
    q <= data[adr];
2550 6 unneback
 
2551
endmodule
2552 14 unneback
*/
2553 6 unneback
// Single port RAM
2554
 
2555
module vl_ram ( d, adr, we, q, clk);
2556
   parameter data_width = 32;
2557
   parameter addr_width = 8;
2558
   input [(data_width-1):0]      d;
2559
   input [(addr_width-1):0]       adr;
2560
   input                         we;
2561 7 unneback
   output reg [(data_width-1):0] q;
2562 6 unneback
   input                         clk;
2563
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
2564 7 unneback
   parameter init = 0;
2565
   parameter memory_file = "vl_ram.vmem";
2566
   generate if (init) begin : init_mem
2567
   initial
2568
     begin
2569
        $readmemh(memory_file, ram);
2570
     end
2571
   end
2572
   endgenerate
2573
 
2574 6 unneback
   always @ (posedge clk)
2575
   begin
2576
   if (we)
2577
     ram[adr] <= d;
2578
   q <= ram[adr];
2579
   end
2580
 
2581
endmodule
2582
 
2583 7 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
2584
   parameter data_width = 32;
2585
   parameter addr_width = 8;
2586
   input [(data_width-1):0]      d;
2587
   input [(addr_width-1):0]       adr;
2588
   input [(addr_width/4)-1:0]    be;
2589
   input                         we;
2590
   output reg [(data_width-1):0] q;
2591
   input                         clk;
2592
 
2593
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
2594
 
2595
   parameter init = 0;
2596
   parameter memory_file = "vl_ram.vmem";
2597
   generate if (init) begin : init_mem
2598
   initial
2599
     begin
2600
        $readmemh(memory_file, ram);
2601
     end
2602
   end
2603
   endgenerate
2604
 
2605
   genvar i;
2606
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_ram
2607
      always @ (posedge clk)
2608
      if (we & be[i])
2609
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
2610
   end
2611
   endgenerate
2612
 
2613
   always @ (posedge clk)
2614
      q <= ram[adr];
2615
 
2616
endmodule
2617
 
2618
 
2619 6 unneback
// Dual port RAM
2620
 
2621
// ACTEL FPGA should not use logic to handle rw collision
2622
`ifdef ACTEL
2623
        `define SYN /*synthesis syn_ramstyle = "no_rw_check"*/
2624
`else
2625
        `define SYN
2626
`endif
2627
 
2628 7 unneback
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
2629 6 unneback
   parameter data_width = 32;
2630
   parameter addr_width = 8;
2631
   input [(data_width-1):0]      d_a;
2632
   input [(addr_width-1):0]       adr_a;
2633
   input [(addr_width-1):0]       adr_b;
2634
   input                         we_a;
2635
   output [(data_width-1):0]      q_b;
2636
   input                         clk_a, clk_b;
2637
   reg [(addr_width-1):0]         adr_b_reg;
2638
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
2639 7 unneback
 
2640
   parameter init = 0;
2641
   parameter memory_file = "vl_ram.vmem";
2642
   generate if (init) begin : init_mem
2643
   initial
2644
     begin
2645
        $readmemh(memory_file, ram);
2646
     end
2647
   end
2648
   endgenerate
2649
 
2650 6 unneback
   always @ (posedge clk_a)
2651
   if (we_a)
2652
     ram[adr_a] <= d_a;
2653
   always @ (posedge clk_b)
2654
   adr_b_reg <= adr_b;
2655
   assign q_b = ram[adr_b_reg];
2656
endmodule
2657
 
2658 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
2659 6 unneback
   parameter data_width = 32;
2660
   parameter addr_width = 8;
2661
   input [(data_width-1):0]      d_a;
2662
   input [(addr_width-1):0]       adr_a;
2663
   input [(addr_width-1):0]       adr_b;
2664
   input                         we_a;
2665
   output [(data_width-1):0]      q_b;
2666
   output reg [(data_width-1):0] q_a;
2667
   input                         clk_a, clk_b;
2668
   reg [(data_width-1):0]         q_b;
2669
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
2670 7 unneback
 
2671
   parameter init = 0;
2672
   parameter memory_file = "vl_ram.vmem";
2673
   generate if (init) begin : init_mem
2674
   initial
2675
     begin
2676
        $readmemh(memory_file, ram);
2677
     end
2678
   end
2679
   endgenerate
2680
 
2681 6 unneback
   always @ (posedge clk_a)
2682
     begin
2683
        q_a <= ram[adr_a];
2684
        if (we_a)
2685
             ram[adr_a] <= d_a;
2686
     end
2687
   always @ (posedge clk_b)
2688
          q_b <= ram[adr_b];
2689
endmodule
2690
 
2691 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 );
2692 6 unneback
   parameter data_width = 32;
2693
   parameter addr_width = 8;
2694
   input [(data_width-1):0]      d_a;
2695
   input [(addr_width-1):0]       adr_a;
2696
   input [(addr_width-1):0]       adr_b;
2697
   input                         we_a;
2698
   output [(data_width-1):0]      q_b;
2699
   input [(data_width-1):0]       d_b;
2700
   output reg [(data_width-1):0] q_a;
2701
   input                         we_b;
2702
   input                         clk_a, clk_b;
2703
   reg [(data_width-1):0]         q_b;
2704
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
2705 7 unneback
 
2706
   parameter init = 0;
2707
   parameter memory_file = "vl_ram.vmem";
2708
   generate if (init) begin : init_mem
2709
   initial
2710
     begin
2711
        $readmemh(memory_file, ram);
2712
     end
2713
   end
2714
   endgenerate
2715
 
2716 6 unneback
   always @ (posedge clk_a)
2717
     begin
2718
        q_a <= ram[adr_a];
2719
        if (we_a)
2720
             ram[adr_a] <= d_a;
2721
     end
2722
   always @ (posedge clk_b)
2723
     begin
2724
        q_b <= ram[adr_b];
2725
        if (we_b)
2726
          ram[adr_b] <= d_b;
2727
     end
2728
endmodule
2729
 
2730
// Content addresable memory, CAM
2731
 
2732
// FIFO
2733 25 unneback
module vl_fifo_1r1w_fill_level_sync (
2734
    d, wr, fifo_full,
2735
    q, rd, fifo_empty,
2736
    fill_level,
2737
    clk, rst
2738
    );
2739
 
2740
parameter data_width = 18;
2741
parameter addr_width = 4;
2742 6 unneback
 
2743 25 unneback
// write side
2744
input  [data_width-1:0] d;
2745
input                   wr;
2746
output                  fifo_full;
2747
// read side
2748
output [data_width-1:0] q;
2749
input                   rd;
2750
output                  fifo_empty;
2751
// common
2752
output [addr_width:0]   fill_level;
2753
input rst, clk;
2754
 
2755
wire [addr_width:1] wadr, radr;
2756
 
2757
vl_cnt_bin_ce
2758
    # ( .length(addr_width))
2759
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
2760
 
2761
vl_cnt_bin_ce
2762
    # (.length(addr_width))
2763
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
2764
 
2765
vl_dpram_1r1w
2766
    # (.data_width(data_width), .addr_width(addr_width))
2767
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
2768
 
2769
vl_cnt_bin_ce_rew_zq_l1
2770 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
2771 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
2772
 
2773
endmodule
2774
 
2775 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
2776
// RAM is supposed to be larger than the two FIFOs
2777
// LFSR counters used adr pointers
2778
module vl_fifo_2r2w_sync_simplex (
2779
    // a side
2780
    a_d, a_wr, a_fifo_full,
2781
    a_q, a_rd, a_fifo_empty,
2782
    a_fill_level,
2783
    // b side
2784
    b_d, b_wr, b_fifo_full,
2785
    b_q, b_rd, b_fifo_empty,
2786
    b_fill_level,
2787
    // common
2788
    clk, rst
2789
    );
2790
parameter data_width = 8;
2791
parameter addr_width = 5;
2792
parameter fifo_full_level = (1<<addr_width)-1;
2793
 
2794