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 7

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
altera
75
`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
// input active lo async reset, normally from external reset generetaor and/or switch
88
// 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
reg [0:1] tmp;
94
always @ (posedge clk or negedge rst_n_i)
95
if (!rst_n_i)
96
        tmp <= 2'b00;
97
else
98
        tmp <= {1'b1,tmp[0]};
99
vl_gbuf buf_i0( .i(tmp[1]), .o(rst_o));
100
endmodule
101
 
102
// vl_pll
103
`ifdef ACTEL
104
`timescale 1 ns/100 ps
105
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
106
parameter index = 0;
107
parameter number_of_clk = 1;
108
parameter period_time_0 = 20;
109
parameter period_time_1 = 20;
110
parameter period_time_2 = 20;
111
parameter lock_delay = 2000;
112
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
`timescale 1 ns/100 ps
209
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
210
parameter index = 0;
211
parameter number_of_clk = 1;
212
parameter period_time_0 = 20;
213
parameter period_time_1 = 20;
214
parameter period_time_2 = 20;
215
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
`endif //actel//////////////////////////////////////////////////////////////////////
246
////                                                              ////
247
////  Versatile library, registers                                ////
248
////                                                              ////
249
////  Description                                                 ////
250
////  Different type of registers                                 ////
251
////                                                              ////
252
////                                                              ////
253
////  To Do:                                                      ////
254
////   - add more different registers                             ////
255
////                                                              ////
256
////  Author(s):                                                  ////
257
////      - Michael Unneback, unneback@opencores.org              ////
258
////        ORSoC AB                                              ////
259
////                                                              ////
260
//////////////////////////////////////////////////////////////////////
261
////                                                              ////
262
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
263
////                                                              ////
264
//// This source file may be used and distributed without         ////
265
//// restriction provided that this copyright statement is not    ////
266
//// removed from the file and that any derivative work contains  ////
267
//// the original copyright notice and the associated disclaimer. ////
268
////                                                              ////
269
//// This source file is free software; you can redistribute it   ////
270
//// and/or modify it under the terms of the GNU Lesser General   ////
271
//// Public License as published by the Free Software Foundation; ////
272
//// either version 2.1 of the License, or (at your option) any   ////
273
//// later version.                                               ////
274
////                                                              ////
275
//// This source is distributed in the hope that it will be       ////
276
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
277
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
278
//// PURPOSE.  See the GNU Lesser General Public License for more ////
279
//// details.                                                     ////
280
////                                                              ////
281
//// You should have received a copy of the GNU Lesser General    ////
282
//// Public License along with this source; if not, download it   ////
283
//// from http://www.opencores.org/lgpl.shtml                     ////
284
////                                                              ////
285
//////////////////////////////////////////////////////////////////////
286
 
287
module dff ( d, q, clk, rst);
288
 
289
        parameter width = 1;
290
        parameter reset_value = 0;
291
 
292
        input [width-1:0] d;
293
        input clk, rst;
294
        output reg [width-1:0] q;
295
 
296
        always @ (posedge clk or posedge rst)
297
        if (rst)
298
                q <= reset_value;
299
        else
300
                q <= d;
301
 
302
endmodule
303
 
304
module dff_array ( d, q, clk, rst);
305
 
306
        parameter width = 1;
307
        parameter depth = 2;
308
        parameter reset_value = 1'b0;
309
 
310
        input [width-1:0] d;
311
        input clk, rst;
312
        output [width-1:0] q;
313
        reg  [0:depth-1] q_tmp [width-1:0];
314
        integer i;
315
        always @ (posedge clk or posedge rst)
316
        if (rst) begin
317
            for (i=0;i<depth;i=i+1)
318
                q_tmp[i] <= {width{reset_value}};
319
        end else begin
320
            q_tmp[0] <= d;
321
            for (i=1;i<depth;i=i+1)
322
                q_tmp[i] <= q_tmp[i-1];
323
        end
324
 
325
    assign q = q_tmp[depth-1];
326
 
327
endmodule
328
 
329
module dff_ce ( d, ce, q, clk, rst);
330
 
331
        parameter width = 1;
332
        parameter reset_value = 0;
333
 
334
        input [width-1:0] d;
335
        input ce, clk, rst;
336
        output reg [width-1:0] q;
337
 
338
        always @ (posedge clk or posedge rst)
339
        if (rst)
340
                q <= reset_value;
341
        else
342
                if (ce)
343
                        q <= d;
344
 
345
endmodule
346
 
347
`ifdef ALTERA
348
// megafunction wizard: %LPM_FF%
349
// GENERATION: STANDARD
350
// VERSION: WM1.0
351
// MODULE: lpm_ff 
352
 
353
// ============================================================
354
// File Name: dff_sr.v
355
// Megafunction Name(s):
356
//                      lpm_ff
357
//
358
// Simulation Library Files(s):
359
//                      lpm
360
// ============================================================
361
// ************************************************************
362
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
363
//
364
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
365
// ************************************************************
366
 
367
 
368
//Copyright (C) 1991-2010 Altera Corporation
369
//Your use of Altera Corporation's design tools, logic functions 
370
//and other software and tools, and its AMPP partner logic 
371
//functions, and any output files from any of the foregoing 
372
//(including device programming or simulation files), and any 
373
//associated documentation or information are expressly subject 
374
//to the terms and conditions of the Altera Program License 
375
//Subscription Agreement, Altera MegaCore Function License 
376
//Agreement, or other applicable license agreement, including, 
377
//without limitation, that your use is for the sole purpose of 
378
//programming logic devices manufactured by Altera and sold by 
379
//Altera or its authorized distributors.  Please refer to the 
380
//applicable agreement for further details.
381
 
382
 
383
// synopsys translate_off
384
`timescale 1 ps / 1 ps
385
// synopsys translate_on
386
module dff_sr (
387
        aclr,
388
        aset,
389
        clock,
390
        data,
391
        q);
392
 
393
        input     aclr;
394
        input     aset;
395
        input     clock;
396
        input     data;
397
        output    q;
398
 
399
        wire [0:0] sub_wire0;
400
        wire [0:0] sub_wire1 = sub_wire0[0:0];
401
        wire  q = sub_wire1;
402
        wire  sub_wire2 = data;
403
        wire  sub_wire3 = sub_wire2;
404
 
405
        lpm_ff  lpm_ff_component (
406
                                .aclr (aclr),
407
                                .clock (clock),
408
                                .data (sub_wire3),
409
                                .aset (aset),
410
                                .q (sub_wire0)
411
                                // synopsys translate_off
412
                                ,
413
                                .aload (),
414
                                .enable (),
415
                                .sclr (),
416
                                .sload (),
417
                                .sset ()
418
                                // synopsys translate_on
419
                                );
420
        defparam
421
                lpm_ff_component.lpm_fftype = "DFF",
422
                lpm_ff_component.lpm_type = "LPM_FF",
423
                lpm_ff_component.lpm_width = 1;
424
 
425
 
426
endmodule
427
 
428
// ============================================================
429
// CNX file retrieval info
430
// ============================================================
431
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
432
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
433
// Retrieval info: PRIVATE: ASET NUMERIC "1"
434
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
435
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
436
// Retrieval info: PRIVATE: DFF NUMERIC "1"
437
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
438
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
439
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
440
// Retrieval info: PRIVATE: SSET NUMERIC "0"
441
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
442
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
443
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
444
// Retrieval info: PRIVATE: nBit NUMERIC "1"
445
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
446
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
447
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
448
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
449
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
450
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
451
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
452
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
453
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
454
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
455
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
456
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
457
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
458
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
459
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
460
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
461
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
462
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
463
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
464
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
465
// Retrieval info: LIB_FILE: lpm
466
 
467
 
468
`else
469
 
470
 
471
module dff_sr ( aclr, aset, clock, data, q);
472
 
473
    input         aclr;
474
    input         aset;
475
    input         clock;
476
    input         data;
477
    output reg    q;
478
 
479
   always @ (posedge clock or posedge aclr or posedge aset)
480
     if (aclr)
481
       q <= 1'b0;
482
     else if (aset)
483
       q <= 1'b1;
484
     else
485
       q <= data;
486
 
487
endmodule
488
 
489
`endif
490
 
491
// LATCH
492
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
493
`ifdef ALTERA
494
module latch ( d, le, q, clk);
495
input d, le;
496
output q;
497
input clk;
498
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
499
endmodule
500
`else
501
module latch ( d, le, q, clk);
502
input d, le;
503
output q;
504
input clk;/*
505
   always @ (posedge direction_set or posedge direction_clr)
506
     if (direction_clr)
507
       direction <= going_empty;
508
     else
509
       direction <= going_full;*/
510
endmodule
511
`endif//////////////////////////////////////////////////////////////////////
512
////                                                              ////
513
////  Versatile counter                                           ////
514
////                                                              ////
515
////  Description                                                 ////
516
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
517
////  counter                                                     ////
518
////                                                              ////
519
////  To Do:                                                      ////
520
////   - add LFSR with more taps                                  ////
521
////                                                              ////
522
////  Author(s):                                                  ////
523
////      - Michael Unneback, unneback@opencores.org              ////
524
////        ORSoC AB                                              ////
525
////                                                              ////
526
//////////////////////////////////////////////////////////////////////
527
////                                                              ////
528
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
529
////                                                              ////
530
//// This source file may be used and distributed without         ////
531
//// restriction provided that this copyright statement is not    ////
532
//// removed from the file and that any derivative work contains  ////
533
//// the original copyright notice and the associated disclaimer. ////
534
////                                                              ////
535
//// This source file is free software; you can redistribute it   ////
536
//// and/or modify it under the terms of the GNU Lesser General   ////
537
//// Public License as published by the Free Software Foundation; ////
538
//// either version 2.1 of the License, or (at your option) any   ////
539
//// later version.                                               ////
540
////                                                              ////
541
//// This source is distributed in the hope that it will be       ////
542
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
543
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
544
//// PURPOSE.  See the GNU Lesser General Public License for more ////
545
//// details.                                                     ////
546
////                                                              ////
547
//// You should have received a copy of the GNU Lesser General    ////
548
//// Public License along with this source; if not, download it   ////
549
//// from http://www.opencores.org/lgpl.shtml                     ////
550
////                                                              ////
551
//////////////////////////////////////////////////////////////////////
552
 
553
// binary counter
554
module cnt_bin_ce ( cke, q, rst, clk);
555
 
556
   parameter length = 4;
557
   input cke;
558
   output [length:1] q;
559
   input rst;
560
   input clk;
561
 
562
   parameter clear_value = 0;
563
   parameter set_value = 1;
564
   parameter wrap_value = 0;
565
   parameter level1_value = 15;
566
 
567
   reg  [length:1] qi;
568
   wire [length:1] q_next;
569
   assign q_next = qi + {{length-1{1'b0}},1'b1};
570
 
571
   always @ (posedge clk or posedge rst)
572
     if (rst)
573
       qi <= {length{1'b0}};
574
     else
575
     if (cke)
576
       qi <= q_next;
577
 
578
   assign q = qi;
579
 
580
endmodule
581
//////////////////////////////////////////////////////////////////////
582
////                                                              ////
583
////  Versatile counter                                           ////
584
////                                                              ////
585
////  Description                                                 ////
586
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
587
////  counter                                                     ////
588
////                                                              ////
589
////  To Do:                                                      ////
590
////   - add LFSR with more taps                                  ////
591
////                                                              ////
592
////  Author(s):                                                  ////
593
////      - Michael Unneback, unneback@opencores.org              ////
594
////        ORSoC AB                                              ////
595
////                                                              ////
596
//////////////////////////////////////////////////////////////////////
597
////                                                              ////
598
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
599
////                                                              ////
600
//// This source file may be used and distributed without         ////
601
//// restriction provided that this copyright statement is not    ////
602
//// removed from the file and that any derivative work contains  ////
603
//// the original copyright notice and the associated disclaimer. ////
604
////                                                              ////
605
//// This source file is free software; you can redistribute it   ////
606
//// and/or modify it under the terms of the GNU Lesser General   ////
607
//// Public License as published by the Free Software Foundation; ////
608
//// either version 2.1 of the License, or (at your option) any   ////
609
//// later version.                                               ////
610
////                                                              ////
611
//// This source is distributed in the hope that it will be       ////
612
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
613
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
614
//// PURPOSE.  See the GNU Lesser General Public License for more ////
615
//// details.                                                     ////
616
////                                                              ////
617
//// You should have received a copy of the GNU Lesser General    ////
618
//// Public License along with this source; if not, download it   ////
619
//// from http://www.opencores.org/lgpl.shtml                     ////
620
////                                                              ////
621
//////////////////////////////////////////////////////////////////////
622
 
623
// binary counter
624
module cnt_bin_ce_clear ( clear, cke, q, rst, clk);
625
 
626
   parameter length = 4;
627
   input clear;
628
   input cke;
629
   output [length:1] q;
630
   input rst;
631
   input clk;
632
 
633
   parameter clear_value = 0;
634
   parameter set_value = 1;
635
   parameter wrap_value = 0;
636
   parameter level1_value = 15;
637
 
638
   reg  [length:1] qi;
639
   wire [length:1] q_next;
640
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
641
 
642
   always @ (posedge clk or posedge rst)
643
     if (rst)
644
       qi <= {length{1'b0}};
645
     else
646
     if (cke)
647
       qi <= q_next;
648
 
649
   assign q = qi;
650
 
651
endmodule
652
//////////////////////////////////////////////////////////////////////
653
////                                                              ////
654
////  Versatile counter                                           ////
655
////                                                              ////
656
////  Description                                                 ////
657
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
658
////  counter                                                     ////
659
////                                                              ////
660
////  To Do:                                                      ////
661
////   - add LFSR with more taps                                  ////
662
////                                                              ////
663
////  Author(s):                                                  ////
664
////      - Michael Unneback, unneback@opencores.org              ////
665
////        ORSoC AB                                              ////
666
////                                                              ////
667
//////////////////////////////////////////////////////////////////////
668
////                                                              ////
669
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
670
////                                                              ////
671
//// This source file may be used and distributed without         ////
672
//// restriction provided that this copyright statement is not    ////
673
//// removed from the file and that any derivative work contains  ////
674
//// the original copyright notice and the associated disclaimer. ////
675
////                                                              ////
676
//// This source file is free software; you can redistribute it   ////
677
//// and/or modify it under the terms of the GNU Lesser General   ////
678
//// Public License as published by the Free Software Foundation; ////
679
//// either version 2.1 of the License, or (at your option) any   ////
680
//// later version.                                               ////
681
////                                                              ////
682
//// This source is distributed in the hope that it will be       ////
683
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
684
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
685
//// PURPOSE.  See the GNU Lesser General Public License for more ////
686
//// details.                                                     ////
687
////                                                              ////
688
//// You should have received a copy of the GNU Lesser General    ////
689
//// Public License along with this source; if not, download it   ////
690
//// from http://www.opencores.org/lgpl.shtml                     ////
691
////                                                              ////
692
//////////////////////////////////////////////////////////////////////
693
 
694
// binary counter
695
module cnt_bin_ce_clear_set_rew ( clear, set, cke, rew, q, rst, clk);
696
 
697
   parameter length = 4;
698
   input clear;
699
   input set;
700
   input cke;
701
   input rew;
702
   output [length:1] q;
703
   input rst;
704
   input clk;
705
 
706
   parameter clear_value = 0;
707
   parameter set_value = 1;
708
   parameter wrap_value = 0;
709
   parameter level1_value = 15;
710
 
711
   reg  [length:1] qi;
712
   wire  [length:1] q_next, q_next_fw, q_next_rew;
713
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
714
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
715
   assign q_next = rew ? q_next_rew : q_next_fw;
716
 
717
   always @ (posedge clk or posedge rst)
718
     if (rst)
719
       qi <= {length{1'b0}};
720
     else
721
     if (cke)
722
       qi <= q_next;
723
 
724
   assign q = qi;
725
 
726
endmodule
727
//////////////////////////////////////////////////////////////////////
728
////                                                              ////
729
////  Versatile counter                                           ////
730
////                                                              ////
731
////  Description                                                 ////
732
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
733
////  counter                                                     ////
734
////                                                              ////
735
////  To Do:                                                      ////
736
////   - add LFSR with more taps                                  ////
737
////                                                              ////
738
////  Author(s):                                                  ////
739
////      - Michael Unneback, unneback@opencores.org              ////
740
////        ORSoC AB                                              ////
741
////                                                              ////
742
//////////////////////////////////////////////////////////////////////
743
////                                                              ////
744
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
745
////                                                              ////
746
//// This source file may be used and distributed without         ////
747
//// restriction provided that this copyright statement is not    ////
748
//// removed from the file and that any derivative work contains  ////
749
//// the original copyright notice and the associated disclaimer. ////
750
////                                                              ////
751
//// This source file is free software; you can redistribute it   ////
752
//// and/or modify it under the terms of the GNU Lesser General   ////
753
//// Public License as published by the Free Software Foundation; ////
754
//// either version 2.1 of the License, or (at your option) any   ////
755
//// later version.                                               ////
756
////                                                              ////
757
//// This source is distributed in the hope that it will be       ////
758
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
759
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
760
//// PURPOSE.  See the GNU Lesser General Public License for more ////
761
//// details.                                                     ////
762
////                                                              ////
763
//// You should have received a copy of the GNU Lesser General    ////
764
//// Public License along with this source; if not, download it   ////
765
//// from http://www.opencores.org/lgpl.shtml                     ////
766
////                                                              ////
767
//////////////////////////////////////////////////////////////////////
768
 
769
// binary counter
770
module cnt_bin_ce_rew_l1 ( cke, rew, level1, rst, clk);
771
 
772
   parameter length = 4;
773
   input cke;
774
   input rew;
775
   output reg level1;
776
   input rst;
777
   input clk;
778
 
779
   parameter clear_value = 0;
780
   parameter set_value = 1;
781
   parameter wrap_value = 1;
782
   parameter level1_value = 15;
783
 
784
   reg  [length:1] qi;
785
   wire  [length:1] q_next, q_next_fw, q_next_rew;
786
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
787
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
788
   assign q_next = rew ? q_next_rew : q_next_fw;
789
 
790
   always @ (posedge clk or posedge rst)
791
     if (rst)
792
       qi <= {length{1'b0}};
793
     else
794
     if (cke)
795
       qi <= q_next;
796
 
797
 
798
 
799
    always @ (posedge clk or posedge rst)
800
    if (rst)
801
        level1 <= 1'b0;
802
    else
803
    if (cke)
804
    if (q_next == level1_value)
805
        level1 <= 1'b1;
806
    else if (qi == level1_value & rew)
807
        level1 <= 1'b0;
808
endmodule
809
//////////////////////////////////////////////////////////////////////
810
////                                                              ////
811
////  Versatile counter                                           ////
812
////                                                              ////
813
////  Description                                                 ////
814
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
815
////  counter                                                     ////
816
////                                                              ////
817
////  To Do:                                                      ////
818
////   - add LFSR with more taps                                  ////
819
////                                                              ////
820
////  Author(s):                                                  ////
821
////      - Michael Unneback, unneback@opencores.org              ////
822
////        ORSoC AB                                              ////
823
////                                                              ////
824
//////////////////////////////////////////////////////////////////////
825
////                                                              ////
826
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
827
////                                                              ////
828
//// This source file may be used and distributed without         ////
829
//// restriction provided that this copyright statement is not    ////
830
//// removed from the file and that any derivative work contains  ////
831
//// the original copyright notice and the associated disclaimer. ////
832
////                                                              ////
833
//// This source file is free software; you can redistribute it   ////
834
//// and/or modify it under the terms of the GNU Lesser General   ////
835
//// Public License as published by the Free Software Foundation; ////
836
//// either version 2.1 of the License, or (at your option) any   ////
837
//// later version.                                               ////
838
////                                                              ////
839
//// This source is distributed in the hope that it will be       ////
840
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
841
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
842
//// PURPOSE.  See the GNU Lesser General Public License for more ////
843
//// details.                                                     ////
844
////                                                              ////
845
//// You should have received a copy of the GNU Lesser General    ////
846
//// Public License along with this source; if not, download it   ////
847
//// from http://www.opencores.org/lgpl.shtml                     ////
848
////                                                              ////
849
//////////////////////////////////////////////////////////////////////
850
 
851
// LFSR counter
852
module cnt_lfsr_zq ( zq, rst, clk);
853
 
854
   parameter length = 4;
855
   output reg zq;
856
   input rst;
857
   input clk;
858
 
859
   parameter clear_value = 0;
860
   parameter set_value = 1;
861
   parameter wrap_value = 8;
862
   parameter level1_value = 15;
863
 
864
   reg  [length:1] qi;
865
   reg lfsr_fb;
866
   wire [length:1] q_next;
867
   reg [32:1] polynom;
868
   integer i;
869
 
870
   always @ (qi)
871
   begin
872
        case (length)
873
         2: polynom = 32'b11;                               // 0x3
874
         3: polynom = 32'b110;                              // 0x6
875
         4: polynom = 32'b1100;                             // 0xC
876
         5: polynom = 32'b10100;                            // 0x14
877
         6: polynom = 32'b110000;                           // 0x30
878
         7: polynom = 32'b1100000;                          // 0x60
879
         8: polynom = 32'b10111000;                         // 0xb8
880
         9: polynom = 32'b100010000;                        // 0x110
881
        10: polynom = 32'b1001000000;                       // 0x240
882
        11: polynom = 32'b10100000000;                      // 0x500
883
        12: polynom = 32'b100000101001;                     // 0x829
884
        13: polynom = 32'b1000000001100;                    // 0x100C
885
        14: polynom = 32'b10000000010101;                   // 0x2015
886
        15: polynom = 32'b110000000000000;                  // 0x6000
887
        16: polynom = 32'b1101000000001000;                 // 0xD008
888
        17: polynom = 32'b10010000000000000;                // 0x12000
889
        18: polynom = 32'b100000010000000000;               // 0x20400
890
        19: polynom = 32'b1000000000000100011;              // 0x40023
891
        20: polynom = 32'b10000010000000000000;             // 0x82000
892
        21: polynom = 32'b101000000000000000000;            // 0x140000
893
        22: polynom = 32'b1100000000000000000000;           // 0x300000
894
        23: polynom = 32'b10000100000000000000000;          // 0x420000
895
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
896
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
897
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
898
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
899
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
900
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
901
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
902
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
903
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
904
        default: polynom = 32'b0;
905
        endcase
906
        lfsr_fb = qi[length];
907
        for (i=length-1; i>=1; i=i-1) begin
908
            if (polynom[i])
909
                lfsr_fb = lfsr_fb  ~^ qi[i];
910
        end
911
    end
912
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
913
 
914
   always @ (posedge clk or posedge rst)
915
     if (rst)
916
       qi <= {length{1'b0}};
917
     else
918
       qi <= q_next;
919
 
920
 
921
 
922
   always @ (posedge clk or posedge rst)
923
     if (rst)
924
       zq <= 1'b1;
925
     else
926
       zq <= q_next == {length{1'b0}};
927
endmodule
928
//////////////////////////////////////////////////////////////////////
929
////                                                              ////
930
////  Versatile counter                                           ////
931
////                                                              ////
932
////  Description                                                 ////
933
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
934
////  counter                                                     ////
935
////                                                              ////
936
////  To Do:                                                      ////
937
////   - add LFSR with more taps                                  ////
938
////                                                              ////
939
////  Author(s):                                                  ////
940
////      - Michael Unneback, unneback@opencores.org              ////
941
////        ORSoC AB                                              ////
942
////                                                              ////
943
//////////////////////////////////////////////////////////////////////
944
////                                                              ////
945
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
946
////                                                              ////
947
//// This source file may be used and distributed without         ////
948
//// restriction provided that this copyright statement is not    ////
949
//// removed from the file and that any derivative work contains  ////
950
//// the original copyright notice and the associated disclaimer. ////
951
////                                                              ////
952
//// This source file is free software; you can redistribute it   ////
953
//// and/or modify it under the terms of the GNU Lesser General   ////
954
//// Public License as published by the Free Software Foundation; ////
955
//// either version 2.1 of the License, or (at your option) any   ////
956
//// later version.                                               ////
957
////                                                              ////
958
//// This source is distributed in the hope that it will be       ////
959
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
960
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
961
//// PURPOSE.  See the GNU Lesser General Public License for more ////
962
//// details.                                                     ////
963
////                                                              ////
964
//// You should have received a copy of the GNU Lesser General    ////
965
//// Public License along with this source; if not, download it   ////
966
//// from http://www.opencores.org/lgpl.shtml                     ////
967
////                                                              ////
968
//////////////////////////////////////////////////////////////////////
969
 
970
// LFSR counter
971
module cnt_lfsr_ce_zq ( cke, zq, rst, clk);
972
 
973
   parameter length = 4;
974
   input cke;
975
   output reg zq;
976
   input rst;
977
   input clk;
978
 
979
   parameter clear_value = 0;
980
   parameter set_value = 1;
981
   parameter wrap_value = 8;
982
   parameter level1_value = 15;
983
 
984
   reg  [length:1] qi;
985
   reg lfsr_fb;
986
   wire [length:1] q_next;
987
   reg [32:1] polynom;
988
   integer i;
989
 
990
   always @ (qi)
991
   begin
992
        case (length)
993
         2: polynom = 32'b11;                               // 0x3
994
         3: polynom = 32'b110;                              // 0x6
995
         4: polynom = 32'b1100;                             // 0xC
996
         5: polynom = 32'b10100;                            // 0x14
997
         6: polynom = 32'b110000;                           // 0x30
998
         7: polynom = 32'b1100000;                          // 0x60
999
         8: polynom = 32'b10111000;                         // 0xb8
1000
         9: polynom = 32'b100010000;                        // 0x110
1001
        10: polynom = 32'b1001000000;                       // 0x240
1002
        11: polynom = 32'b10100000000;                      // 0x500
1003
        12: polynom = 32'b100000101001;                     // 0x829
1004
        13: polynom = 32'b1000000001100;                    // 0x100C
1005
        14: polynom = 32'b10000000010101;                   // 0x2015
1006
        15: polynom = 32'b110000000000000;                  // 0x6000
1007
        16: polynom = 32'b1101000000001000;                 // 0xD008
1008
        17: polynom = 32'b10010000000000000;                // 0x12000
1009
        18: polynom = 32'b100000010000000000;               // 0x20400
1010
        19: polynom = 32'b1000000000000100011;              // 0x40023
1011
        20: polynom = 32'b10000010000000000000;             // 0x82000
1012
        21: polynom = 32'b101000000000000000000;            // 0x140000
1013
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1014
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1015
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1016
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1017
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1018
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1019
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1020
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1021
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1022
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1023
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1024
        default: polynom = 32'b0;
1025
        endcase
1026
        lfsr_fb = qi[length];
1027
        for (i=length-1; i>=1; i=i-1) begin
1028
            if (polynom[i])
1029
                lfsr_fb = lfsr_fb  ~^ qi[i];
1030
        end
1031
    end
1032
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1033
 
1034
   always @ (posedge clk or posedge rst)
1035
     if (rst)
1036
       qi <= {length{1'b0}};
1037
     else
1038
     if (cke)
1039
       qi <= q_next;
1040
 
1041
 
1042
 
1043
   always @ (posedge clk or posedge rst)
1044
     if (rst)
1045
       zq <= 1'b1;
1046
     else
1047
     if (cke)
1048
       zq <= q_next == {length{1'b0}};
1049
endmodule
1050
//////////////////////////////////////////////////////////////////////
1051
////                                                              ////
1052
////  Versatile counter                                           ////
1053
////                                                              ////
1054
////  Description                                                 ////
1055
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1056
////  counter                                                     ////
1057
////                                                              ////
1058
////  To Do:                                                      ////
1059
////   - add LFSR with more taps                                  ////
1060
////                                                              ////
1061
////  Author(s):                                                  ////
1062
////      - Michael Unneback, unneback@opencores.org              ////
1063
////        ORSoC AB                                              ////
1064
////                                                              ////
1065
//////////////////////////////////////////////////////////////////////
1066
////                                                              ////
1067
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1068
////                                                              ////
1069
//// This source file may be used and distributed without         ////
1070
//// restriction provided that this copyright statement is not    ////
1071
//// removed from the file and that any derivative work contains  ////
1072
//// the original copyright notice and the associated disclaimer. ////
1073
////                                                              ////
1074
//// This source file is free software; you can redistribute it   ////
1075
//// and/or modify it under the terms of the GNU Lesser General   ////
1076
//// Public License as published by the Free Software Foundation; ////
1077
//// either version 2.1 of the License, or (at your option) any   ////
1078
//// later version.                                               ////
1079
////                                                              ////
1080
//// This source is distributed in the hope that it will be       ////
1081
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1082
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1083
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1084
//// details.                                                     ////
1085
////                                                              ////
1086
//// You should have received a copy of the GNU Lesser General    ////
1087
//// Public License along with this source; if not, download it   ////
1088
//// from http://www.opencores.org/lgpl.shtml                     ////
1089
////                                                              ////
1090
//////////////////////////////////////////////////////////////////////
1091
 
1092
// LFSR counter
1093
module cnt_lfsr_ce_rew_l1 ( cke, rew, level1, rst, clk);
1094
 
1095
   parameter length = 4;
1096
   input cke;
1097
   input rew;
1098
   output reg level1;
1099
   input rst;
1100
   input clk;
1101
 
1102
   parameter clear_value = 0;
1103
   parameter set_value = 1;
1104
   parameter wrap_value = 8;
1105
   parameter level1_value = 15;
1106
 
1107
   reg  [length:1] qi;
1108
   reg lfsr_fb, lfsr_fb_rew;
1109
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1110
   reg [32:1] polynom_rew;
1111
   integer j;
1112
   reg [32:1] polynom;
1113
   integer i;
1114
 
1115
   always @ (qi)
1116
   begin
1117
        case (length)
1118
         2: polynom = 32'b11;                               // 0x3
1119
         3: polynom = 32'b110;                              // 0x6
1120
         4: polynom = 32'b1100;                             // 0xC
1121
         5: polynom = 32'b10100;                            // 0x14
1122
         6: polynom = 32'b110000;                           // 0x30
1123
         7: polynom = 32'b1100000;                          // 0x60
1124
         8: polynom = 32'b10111000;                         // 0xb8
1125
         9: polynom = 32'b100010000;                        // 0x110
1126
        10: polynom = 32'b1001000000;                       // 0x240
1127
        11: polynom = 32'b10100000000;                      // 0x500
1128
        12: polynom = 32'b100000101001;                     // 0x829
1129
        13: polynom = 32'b1000000001100;                    // 0x100C
1130
        14: polynom = 32'b10000000010101;                   // 0x2015
1131
        15: polynom = 32'b110000000000000;                  // 0x6000
1132
        16: polynom = 32'b1101000000001000;                 // 0xD008
1133
        17: polynom = 32'b10010000000000000;                // 0x12000
1134
        18: polynom = 32'b100000010000000000;               // 0x20400
1135
        19: polynom = 32'b1000000000000100011;              // 0x40023
1136
        20: polynom = 32'b10000010000000000000;             // 0x82000
1137
        21: polynom = 32'b101000000000000000000;            // 0x140000
1138
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1139
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1140
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1141
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1142
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1143
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1144
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1145
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1146
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1147
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1148
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1149
        default: polynom = 32'b0;
1150
        endcase
1151
        lfsr_fb = qi[length];
1152
        for (i=length-1; i>=1; i=i-1) begin
1153
            if (polynom[i])
1154
                lfsr_fb = lfsr_fb  ~^ qi[i];
1155
        end
1156
    end
1157
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1158
   always @ (qi)
1159
   begin
1160
        case (length)
1161
         2: polynom_rew = 32'b11;
1162
         3: polynom_rew = 32'b110;
1163
         4: polynom_rew = 32'b1100;
1164
         5: polynom_rew = 32'b10100;
1165
         6: polynom_rew = 32'b110000;
1166
         7: polynom_rew = 32'b1100000;
1167
         8: polynom_rew = 32'b10111000;
1168
         9: polynom_rew = 32'b100010000;
1169
        10: polynom_rew = 32'b1001000000;
1170
        11: polynom_rew = 32'b10100000000;
1171
        12: polynom_rew = 32'b100000101001;
1172
        13: polynom_rew = 32'b1000000001100;
1173
        14: polynom_rew = 32'b10000000010101;
1174
        15: polynom_rew = 32'b110000000000000;
1175
        16: polynom_rew = 32'b1101000000001000;
1176
        17: polynom_rew = 32'b10010000000000000;
1177
        18: polynom_rew = 32'b100000010000000000;
1178
        19: polynom_rew = 32'b1000000000000100011;
1179
        20: polynom_rew = 32'b10000010000000000000;
1180
        21: polynom_rew = 32'b101000000000000000000;
1181
        22: polynom_rew = 32'b1100000000000000000000;
1182
        23: polynom_rew = 32'b10000100000000000000000;
1183
        24: polynom_rew = 32'b111000010000000000000000;
1184
        25: polynom_rew = 32'b1001000000000000000000000;
1185
        26: polynom_rew = 32'b10000000000000000000100011;
1186
        27: polynom_rew = 32'b100000000000000000000010011;
1187
        28: polynom_rew = 32'b1100100000000000000000000000;
1188
        29: polynom_rew = 32'b10100000000000000000000000000;
1189
        30: polynom_rew = 32'b100000000000000000000000101001;
1190
        31: polynom_rew = 32'b1001000000000000000000000000000;
1191
        32: polynom_rew = 32'b10000000001000000000000000000011;
1192
        default: polynom_rew = 32'b0;
1193
        endcase
1194
        // rotate left
1195
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
1196
        lfsr_fb_rew = qi[length];
1197
        for (i=length-1; i>=1; i=i-1) begin
1198
            if (polynom_rew[i])
1199
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
1200
        end
1201
    end
1202
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
1203
   assign q_next = rew ? q_next_rew : q_next_fw;
1204
 
1205
   always @ (posedge clk or posedge rst)
1206
     if (rst)
1207
       qi <= {length{1'b0}};
1208
     else
1209
     if (cke)
1210
       qi <= q_next;
1211
 
1212
 
1213
 
1214
    always @ (posedge clk or posedge rst)
1215
    if (rst)
1216
        level1 <= 1'b0;
1217
    else
1218
    if (cke)
1219
    if (q_next == level1_value)
1220
        level1 <= 1'b1;
1221
    else if (qi == level1_value & rew)
1222
        level1 <= 1'b0;
1223
endmodule
1224
//////////////////////////////////////////////////////////////////////
1225
////                                                              ////
1226
////  Versatile counter                                           ////
1227
////                                                              ////
1228
////  Description                                                 ////
1229
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1230
////  counter                                                     ////
1231
////                                                              ////
1232
////  To Do:                                                      ////
1233
////   - add LFSR with more taps                                  ////
1234
////                                                              ////
1235
////  Author(s):                                                  ////
1236
////      - Michael Unneback, unneback@opencores.org              ////
1237
////        ORSoC AB                                              ////
1238
////                                                              ////
1239
//////////////////////////////////////////////////////////////////////
1240
////                                                              ////
1241
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1242
////                                                              ////
1243
//// This source file may be used and distributed without         ////
1244
//// restriction provided that this copyright statement is not    ////
1245
//// removed from the file and that any derivative work contains  ////
1246
//// the original copyright notice and the associated disclaimer. ////
1247
////                                                              ////
1248
//// This source file is free software; you can redistribute it   ////
1249
//// and/or modify it under the terms of the GNU Lesser General   ////
1250
//// Public License as published by the Free Software Foundation; ////
1251
//// either version 2.1 of the License, or (at your option) any   ////
1252
//// later version.                                               ////
1253
////                                                              ////
1254
//// This source is distributed in the hope that it will be       ////
1255
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1256
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1257
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1258
//// details.                                                     ////
1259
////                                                              ////
1260
//// You should have received a copy of the GNU Lesser General    ////
1261
//// Public License along with this source; if not, download it   ////
1262
//// from http://www.opencores.org/lgpl.shtml                     ////
1263
////                                                              ////
1264
//////////////////////////////////////////////////////////////////////
1265
 
1266
// GRAY counter
1267
module cnt_gray ( q, rst, clk);
1268
 
1269
   parameter length = 4;
1270
   output reg [length:1] q;
1271
   input rst;
1272
   input clk;
1273
 
1274
   parameter clear_value = 0;
1275
   parameter set_value = 1;
1276
   parameter wrap_value = 8;
1277
   parameter level1_value = 15;
1278
 
1279
   reg  [length:1] qi;
1280
   wire [length:1] q_next;
1281
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1282
 
1283
   always @ (posedge clk or posedge rst)
1284
     if (rst)
1285
       qi <= {length{1'b0}};
1286
     else
1287
       qi <= q_next;
1288
 
1289
   always @ (posedge clk or posedge rst)
1290
     if (rst)
1291
       q <= {length{1'b0}};
1292
     else
1293
         q <= (q_next>>1) ^ q_next;
1294
 
1295
endmodule
1296
//////////////////////////////////////////////////////////////////////
1297
////                                                              ////
1298
////  Versatile counter                                           ////
1299
////                                                              ////
1300
////  Description                                                 ////
1301
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1302
////  counter                                                     ////
1303
////                                                              ////
1304
////  To Do:                                                      ////
1305
////   - add LFSR with more taps                                  ////
1306
////                                                              ////
1307
////  Author(s):                                                  ////
1308
////      - Michael Unneback, unneback@opencores.org              ////
1309
////        ORSoC AB                                              ////
1310
////                                                              ////
1311
//////////////////////////////////////////////////////////////////////
1312
////                                                              ////
1313
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1314
////                                                              ////
1315
//// This source file may be used and distributed without         ////
1316
//// restriction provided that this copyright statement is not    ////
1317
//// removed from the file and that any derivative work contains  ////
1318
//// the original copyright notice and the associated disclaimer. ////
1319
////                                                              ////
1320
//// This source file is free software; you can redistribute it   ////
1321
//// and/or modify it under the terms of the GNU Lesser General   ////
1322
//// Public License as published by the Free Software Foundation; ////
1323
//// either version 2.1 of the License, or (at your option) any   ////
1324
//// later version.                                               ////
1325
////                                                              ////
1326
//// This source is distributed in the hope that it will be       ////
1327
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1328
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1329
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1330
//// details.                                                     ////
1331
////                                                              ////
1332
//// You should have received a copy of the GNU Lesser General    ////
1333
//// Public License along with this source; if not, download it   ////
1334
//// from http://www.opencores.org/lgpl.shtml                     ////
1335
////                                                              ////
1336
//////////////////////////////////////////////////////////////////////
1337
 
1338
// GRAY counter
1339
module cnt_gray_ce ( cke, q, rst, clk);
1340
 
1341
   parameter length = 4;
1342
   input cke;
1343
   output reg [length:1] q;
1344
   input rst;
1345
   input clk;
1346
 
1347
   parameter clear_value = 0;
1348
   parameter set_value = 1;
1349
   parameter wrap_value = 8;
1350
   parameter level1_value = 15;
1351
 
1352
   reg  [length:1] qi;
1353
   wire [length:1] q_next;
1354
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1355
 
1356
   always @ (posedge clk or posedge rst)
1357
     if (rst)
1358
       qi <= {length{1'b0}};
1359
     else
1360
     if (cke)
1361
       qi <= q_next;
1362
 
1363
   always @ (posedge clk or posedge rst)
1364
     if (rst)
1365
       q <= {length{1'b0}};
1366
     else
1367
       if (cke)
1368
         q <= (q_next>>1) ^ q_next;
1369
 
1370
endmodule
1371
//////////////////////////////////////////////////////////////////////
1372
////                                                              ////
1373
////  Versatile counter                                           ////
1374
////                                                              ////
1375
////  Description                                                 ////
1376
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1377
////  counter                                                     ////
1378
////                                                              ////
1379
////  To Do:                                                      ////
1380
////   - add LFSR with more taps                                  ////
1381
////                                                              ////
1382
////  Author(s):                                                  ////
1383
////      - Michael Unneback, unneback@opencores.org              ////
1384
////        ORSoC AB                                              ////
1385
////                                                              ////
1386
//////////////////////////////////////////////////////////////////////
1387
////                                                              ////
1388
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1389
////                                                              ////
1390
//// This source file may be used and distributed without         ////
1391
//// restriction provided that this copyright statement is not    ////
1392
//// removed from the file and that any derivative work contains  ////
1393
//// the original copyright notice and the associated disclaimer. ////
1394
////                                                              ////
1395
//// This source file is free software; you can redistribute it   ////
1396
//// and/or modify it under the terms of the GNU Lesser General   ////
1397
//// Public License as published by the Free Software Foundation; ////
1398
//// either version 2.1 of the License, or (at your option) any   ////
1399
//// later version.                                               ////
1400
////                                                              ////
1401
//// This source is distributed in the hope that it will be       ////
1402
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1403
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1404
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1405
//// details.                                                     ////
1406
////                                                              ////
1407
//// You should have received a copy of the GNU Lesser General    ////
1408
//// Public License along with this source; if not, download it   ////
1409
//// from http://www.opencores.org/lgpl.shtml                     ////
1410
////                                                              ////
1411
//////////////////////////////////////////////////////////////////////
1412
 
1413
// GRAY counter
1414
module cnt_gray_ce_bin ( cke, q, q_bin, rst, clk);
1415
 
1416
   parameter length = 4;
1417
   input cke;
1418
   output reg [length:1] q;
1419
   output [length:1] q_bin;
1420
   input rst;
1421
   input clk;
1422
 
1423
   parameter clear_value = 0;
1424
   parameter set_value = 1;
1425
   parameter wrap_value = 8;
1426
   parameter level1_value = 15;
1427
 
1428
   reg  [length:1] qi;
1429
   wire [length:1] q_next;
1430
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1431
 
1432
   always @ (posedge clk or posedge rst)
1433
     if (rst)
1434
       qi <= {length{1'b0}};
1435
     else
1436
     if (cke)
1437
       qi <= q_next;
1438
 
1439
   always @ (posedge clk or posedge rst)
1440
     if (rst)
1441
       q <= {length{1'b0}};
1442
     else
1443
       if (cke)
1444
         q <= (q_next>>1) ^ q_next;
1445
 
1446
   assign q_bin = qi;
1447
 
1448
endmodule
1449
//////////////////////////////////////////////////////////////////////
1450
////                                                              ////
1451
////  Versatile library, counters                                 ////
1452
////                                                              ////
1453
////  Description                                                 ////
1454
////  counters                                                    ////
1455
////                                                              ////
1456
////                                                              ////
1457
////  To Do:                                                      ////
1458
////   - add more counters                                        ////
1459
////                                                              ////
1460
////  Author(s):                                                  ////
1461
////      - Michael Unneback, unneback@opencores.org              ////
1462
////        ORSoC AB                                              ////
1463
////                                                              ////
1464
//////////////////////////////////////////////////////////////////////
1465
////                                                              ////
1466
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1467
////                                                              ////
1468
//// This source file may be used and distributed without         ////
1469
//// restriction provided that this copyright statement is not    ////
1470
//// removed from the file and that any derivative work contains  ////
1471
//// the original copyright notice and the associated disclaimer. ////
1472
////                                                              ////
1473
//// This source file is free software; you can redistribute it   ////
1474
//// and/or modify it under the terms of the GNU Lesser General   ////
1475
//// Public License as published by the Free Software Foundation; ////
1476
//// either version 2.1 of the License, or (at your option) any   ////
1477
//// later version.                                               ////
1478
////                                                              ////
1479
//// This source is distributed in the hope that it will be       ////
1480
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1481
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1482
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1483
//// details.                                                     ////
1484
////                                                              ////
1485
//// You should have received a copy of the GNU Lesser General    ////
1486
//// Public License along with this source; if not, download it   ////
1487
//// from http://www.opencores.org/lgpl.shtml                     ////
1488
////                                                              ////
1489
//////////////////////////////////////////////////////////////////////
1490
 
1491
module cnt_shreg_wrap ( q, rst, clk);
1492
 
1493
   parameter length = 4;
1494
   output reg [0:length-1] q;
1495
   input rst;
1496
   input clk;
1497
 
1498
    always @ (posedge clk or posedge rst)
1499
    if (rst)
1500
        q <= {1'b1,{length-1{1'b0}}};
1501
    else
1502
        q <= {q[length-1],q[0:length-2]};
1503
 
1504
endmodule
1505
 
1506
module cnt_shreg_ce_wrap ( cke, q, rst, clk);
1507
 
1508
   parameter length = 4;
1509
   input cke;
1510
   output reg [0:length-1] q;
1511
   input rst;
1512
   input clk;
1513
 
1514
    always @ (posedge clk or posedge rst)
1515
    if (rst)
1516
        q <= {1'b1,{length-1{1'b0}}};
1517
    else
1518
        if (cke)
1519
            q <= {q[length-1],q[0:length-2]};
1520
 
1521
endmodule
1522
 
1523
module cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
1524
 
1525
   parameter length = 4;
1526
   input cke, clear;
1527
   output reg [0:length-1] q;
1528
   input rst;
1529
   input clk;
1530
 
1531
    always @ (posedge clk or posedge rst)
1532
    if (rst)
1533
        q <= {1'b1,{length-1{1'b0}}};
1534
    else
1535
        if (cke)
1536
            if (clear)
1537
                q <= {1'b1,{length-1{1'b0}}};
1538
            else
1539
                q <= q >> 1;
1540
 
1541
endmodule
1542
 
1543
module cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
1544
 
1545
   parameter length = 4;
1546
   input cke, clear;
1547
   output reg [0:length-1] q;
1548
   input rst;
1549
   input clk;
1550
 
1551
    always @ (posedge clk or posedge rst)
1552
    if (rst)
1553
        q <= {1'b1,{length-1{1'b0}}};
1554
    else
1555
        if (cke)
1556
            if (clear)
1557
                q <= {1'b1,{length-1{1'b0}}};
1558
            else
1559
            q <= {q[length-1],q[0:length-2]};
1560
 
1561
endmodule
1562
//////////////////////////////////////////////////////////////////////
1563
////                                                              ////
1564
////  Versatile library, memories                                 ////
1565
////                                                              ////
1566
////  Description                                                 ////
1567
////  memories                                                    ////
1568
////                                                              ////
1569
////                                                              ////
1570
////  To Do:                                                      ////
1571
////   - add more memory types                                    ////
1572
////                                                              ////
1573
////  Author(s):                                                  ////
1574
////      - Michael Unneback, unneback@opencores.org              ////
1575
////        ORSoC AB                                              ////
1576
////                                                              ////
1577
//////////////////////////////////////////////////////////////////////
1578
////                                                              ////
1579
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1580
////                                                              ////
1581
//// This source file may be used and distributed without         ////
1582
//// restriction provided that this copyright statement is not    ////
1583
//// removed from the file and that any derivative work contains  ////
1584
//// the original copyright notice and the associated disclaimer. ////
1585
////                                                              ////
1586
//// This source file is free software; you can redistribute it   ////
1587
//// and/or modify it under the terms of the GNU Lesser General   ////
1588
//// Public License as published by the Free Software Foundation; ////
1589
//// either version 2.1 of the License, or (at your option) any   ////
1590
//// later version.                                               ////
1591
////                                                              ////
1592
//// This source is distributed in the hope that it will be       ////
1593
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1594
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1595
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1596
//// details.                                                     ////
1597
////                                                              ////
1598
//// You should have received a copy of the GNU Lesser General    ////
1599
//// Public License along with this source; if not, download it   ////
1600
//// from http://www.opencores.org/lgpl.shtml                     ////
1601
////                                                              ////
1602
//////////////////////////////////////////////////////////////////////
1603
 
1604
/// ROM
1605
 
1606 7 unneback
module vl_rom_init ( adr, q, clk);
1607
   parameter data_width = 32;
1608
   parameter addr_width = 8;
1609
   input [(addr_width-1):0]       adr;
1610
   output reg [(data_width-1):0] q;
1611
   input                         clk;
1612
   reg [data_width-1:0] rom [(1<<addr_width)-1:0];
1613
   parameter memory_file = "vl_rom.vmem";
1614
   initial
1615
     begin
1616
        $readmemh(memory_file, rom);
1617
     end
1618
 
1619
   always @ (posedge clk)
1620
     q <= rom[adr];
1621 6 unneback
 
1622 7 unneback
endmodule
1623
 
1624
module vl_rom ( adr, q, clk);
1625
 
1626 6 unneback
parameter data_width = 32;
1627
parameter addr_width = 4;
1628
 
1629
parameter [0:1>>addr_width-1] data [data_width-1:0] = {
1630
    {32'h18000000},
1631
    {32'hA8200000},
1632
    {32'hA8200000},
1633
    {32'hA8200000},
1634
    {32'h44003000},
1635
    {32'h15000000},
1636
    {32'h15000000},
1637
    {32'h15000000},
1638
    {32'h15000000},
1639
    {32'h15000000},
1640
    {32'h15000000},
1641
    {32'h15000000},
1642
    {32'h15000000},
1643
    {32'h15000000},
1644
    {32'h15000000},
1645
    {32'h15000000}};
1646
 
1647 7 unneback
input [addr_width-1:0] adr;
1648 6 unneback
output reg [data_width-1:0] q;
1649
input clk;
1650
 
1651
always @ (posedge clk)
1652 7 unneback
    q <= data[adr];
1653 6 unneback
 
1654
endmodule
1655
 
1656
// Single port RAM
1657
 
1658
module vl_ram ( d, adr, we, q, clk);
1659
   parameter data_width = 32;
1660
   parameter addr_width = 8;
1661
   input [(data_width-1):0]      d;
1662
   input [(addr_width-1):0]       adr;
1663
   input                         we;
1664 7 unneback
   output reg [(data_width-1):0] q;
1665 6 unneback
   input                         clk;
1666
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
1667 7 unneback
   parameter init = 0;
1668
   parameter memory_file = "vl_ram.vmem";
1669
   generate if (init) begin : init_mem
1670
   initial
1671
     begin
1672
        $readmemh(memory_file, ram);
1673
     end
1674
   end
1675
   endgenerate
1676
 
1677 6 unneback
   always @ (posedge clk)
1678
   begin
1679
   if (we)
1680
     ram[adr] <= d;
1681
   q <= ram[adr];
1682
   end
1683
 
1684
endmodule
1685
 
1686 7 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
1687
   parameter data_width = 32;
1688
   parameter addr_width = 8;
1689
   input [(data_width-1):0]      d;
1690
   input [(addr_width-1):0]       adr;
1691
   input [(addr_width/4)-1:0]    be;
1692
   input                         we;
1693
   output reg [(data_width-1):0] q;
1694
   input                         clk;
1695
 
1696
   reg [data_width-1:0] ram [(1<<addr_width)-1:0];
1697
 
1698
   parameter init = 0;
1699
   parameter memory_file = "vl_ram.vmem";
1700
   generate if (init) begin : init_mem
1701
   initial
1702
     begin
1703
        $readmemh(memory_file, ram);
1704
     end
1705
   end
1706
   endgenerate
1707
 
1708
   genvar i;
1709
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_ram
1710
      always @ (posedge clk)
1711
      if (we & be[i])
1712
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
1713
   end
1714
   endgenerate
1715
 
1716
   always @ (posedge clk)
1717
      q <= ram[adr];
1718
 
1719
endmodule
1720
 
1721
 
1722 6 unneback
// Dual port RAM
1723
 
1724
// ACTEL FPGA should not use logic to handle rw collision
1725
`ifdef ACTEL
1726
        `define SYN /*synthesis syn_ramstyle = "no_rw_check"*/
1727
`else
1728
        `define SYN
1729
`endif
1730
 
1731 7 unneback
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1732 6 unneback
   parameter data_width = 32;
1733
   parameter addr_width = 8;
1734
   input [(data_width-1):0]      d_a;
1735
   input [(addr_width-1):0]       adr_a;
1736
   input [(addr_width-1):0]       adr_b;
1737
   input                         we_a;
1738
   output [(data_width-1):0]      q_b;
1739
   input                         clk_a, clk_b;
1740
   reg [(addr_width-1):0]         adr_b_reg;
1741
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
1742 7 unneback
 
1743
   parameter init = 0;
1744
   parameter memory_file = "vl_ram.vmem";
1745
   generate if (init) begin : init_mem
1746
   initial
1747
     begin
1748
        $readmemh(memory_file, ram);
1749
     end
1750
   end
1751
   endgenerate
1752
 
1753 6 unneback
   always @ (posedge clk_a)
1754
   if (we_a)
1755
     ram[adr_a] <= d_a;
1756
   always @ (posedge clk_b)
1757
   adr_b_reg <= adr_b;
1758
   assign q_b = ram[adr_b_reg];
1759
endmodule
1760
 
1761 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1762 6 unneback
   parameter data_width = 32;
1763
   parameter addr_width = 8;
1764
   input [(data_width-1):0]      d_a;
1765
   input [(addr_width-1):0]       adr_a;
1766
   input [(addr_width-1):0]       adr_b;
1767
   input                         we_a;
1768
   output [(data_width-1):0]      q_b;
1769
   output reg [(data_width-1):0] q_a;
1770
   input                         clk_a, clk_b;
1771
   reg [(data_width-1):0]         q_b;
1772
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
1773 7 unneback
 
1774
   parameter init = 0;
1775
   parameter memory_file = "vl_ram.vmem";
1776
   generate if (init) begin : init_mem
1777
   initial
1778
     begin
1779
        $readmemh(memory_file, ram);
1780
     end
1781
   end
1782
   endgenerate
1783
 
1784 6 unneback
   always @ (posedge clk_a)
1785
     begin
1786
        q_a <= ram[adr_a];
1787
        if (we_a)
1788
             ram[adr_a] <= d_a;
1789
     end
1790
   always @ (posedge clk_b)
1791
          q_b <= ram[adr_b];
1792
endmodule
1793
 
1794 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 );
1795 6 unneback
   parameter data_width = 32;
1796
   parameter addr_width = 8;
1797
   input [(data_width-1):0]      d_a;
1798
   input [(addr_width-1):0]       adr_a;
1799
   input [(addr_width-1):0]       adr_b;
1800
   input                         we_a;
1801
   output [(data_width-1):0]      q_b;
1802
   input [(data_width-1):0]       d_b;
1803
   output reg [(data_width-1):0] q_a;
1804
   input                         we_b;
1805
   input                         clk_a, clk_b;
1806
   reg [(data_width-1):0]         q_b;
1807
   reg [data_width-1:0] ram [(1<<addr_width)-1:0] `SYN;
1808 7 unneback
 
1809
   parameter init = 0;
1810
   parameter memory_file = "vl_ram.vmem";
1811
   generate if (init) begin : init_mem
1812
   initial
1813
     begin
1814
        $readmemh(memory_file, ram);
1815
     end
1816
   end
1817
   endgenerate
1818
 
1819 6 unneback
   always @ (posedge clk_a)
1820
     begin
1821
        q_a <= ram[adr_a];
1822
        if (we_a)
1823
             ram[adr_a] <= d_a;
1824
     end
1825
   always @ (posedge clk_b)
1826
     begin
1827
        q_b <= ram[adr_b];
1828
        if (we_b)
1829
          ram[adr_b] <= d_b;
1830
     end
1831
endmodule
1832
 
1833
// Content addresable memory, CAM
1834
 
1835
// FIFO
1836
 
1837
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
1838
 
1839
   parameter ADDR_WIDTH = 4;
1840
   parameter N = ADDR_WIDTH-1;
1841
 
1842
   parameter Q1 = 2'b00;
1843
   parameter Q2 = 2'b01;
1844
   parameter Q3 = 2'b11;
1845
   parameter Q4 = 2'b10;
1846
 
1847
   parameter going_empty = 1'b0;
1848
   parameter going_full  = 1'b1;
1849
 
1850
   input [N:0]  wptr, rptr;
1851
   output reg   fifo_empty;
1852
   output       fifo_full;
1853
   input        wclk, rclk, rst;
1854
 
1855
`ifndef GENERATE_DIRECTION_AS_LATCH
1856
   wire direction;
1857
`endif
1858
`ifdef GENERATE_DIRECTION_AS_LATCH
1859
   reg direction;
1860
`endif
1861
   reg  direction_set, direction_clr;
1862
 
1863
   wire async_empty, async_full;
1864
   wire fifo_full2;
1865
   reg  fifo_empty2;
1866
 
1867
   // direction_set
1868
   always @ (wptr[N:N-1] or rptr[N:N-1])
1869
     case ({wptr[N:N-1],rptr[N:N-1]})
1870
       {Q1,Q2} : direction_set <= 1'b1;
1871
       {Q2,Q3} : direction_set <= 1'b1;
1872
       {Q3,Q4} : direction_set <= 1'b1;
1873
       {Q4,Q1} : direction_set <= 1'b1;
1874
       default : direction_set <= 1'b0;
1875
     endcase
1876
 
1877
   // direction_clear
1878
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
1879
     if (rst)
1880
       direction_clr <= 1'b1;
1881
     else
1882
       case ({wptr[N:N-1],rptr[N:N-1]})
1883
         {Q2,Q1} : direction_clr <= 1'b1;
1884
         {Q3,Q2} : direction_clr <= 1'b1;
1885
         {Q4,Q3} : direction_clr <= 1'b1;
1886
         {Q1,Q4} : direction_clr <= 1'b1;
1887
         default : direction_clr <= 1'b0;
1888
       endcase
1889
 
1890
`ifndef GENERATE_DIRECTION_AS_LATCH
1891
    dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
1892
`endif
1893
 
1894
`ifdef GENERATE_DIRECTION_AS_LATCH
1895
   always @ (posedge direction_set or posedge direction_clr)
1896
     if (direction_clr)
1897
       direction <= going_empty;
1898
     else
1899
       direction <= going_full;
1900
`endif
1901
 
1902
   assign async_empty = (wptr == rptr) && (direction==going_empty);
1903
   assign async_full  = (wptr == rptr) && (direction==going_full);
1904
 
1905
    dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
1906
    dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
1907
 
1908
/*
1909
   always @ (posedge wclk or posedge rst or posedge async_full)
1910
     if (rst)
1911
       {fifo_full, fifo_full2} <= 2'b00;
1912
     else if (async_full)
1913
       {fifo_full, fifo_full2} <= 2'b11;
1914
     else
1915
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
1916
*/
1917
   always @ (posedge rclk or posedge async_empty)
1918
     if (async_empty)
1919
       {fifo_empty, fifo_empty2} <= 2'b11;
1920
     else
1921
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty};
1922
 
1923
endmodule // async_comp
1924
 
1925
module vl_fifo_1r1w_async (
1926
    d, wr, fifo_full, wr_clk, wr_rst,
1927
    q, rd, fifo_empty, rd_clk, rd_rst
1928
    );
1929
 
1930
parameter data_width = 18;
1931
parameter addr_width = 4;
1932
 
1933
// write side
1934
input  [data_width-1:0] d;
1935
input                   wr;
1936
output                  fifo_full;
1937
input                   wr_clk;
1938
input                   wr_rst;
1939
// read side
1940
output [data_width-1:0] q;
1941
input                   rd;
1942
output                  fifo_empty;
1943
input                   rd_clk;
1944
input                   rd_rst;
1945
 
1946
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
1947
 
1948
vl_fifo_1r1w_async (
1949
    d, wr, fifo_full, wr_clk, wr_rst,
1950
    q, rd, fifo_empty, rd_clk, rd_rst
1951
    );
1952
 
1953 7 unneback
cnt_gray_ce_bin
1954 6 unneback
    # ( .length(addr_width))
1955
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
1956
 
1957 7 unneback
cnt_gray_ce_bin
1958 6 unneback
    # (.length(addr_width))
1959
    fifo_rd_adr( .cke(wr), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_rst));
1960
 
1961 7 unneback
vl_dpram_1r1w
1962 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
1963
    dpram ( .d_a(d), .adr_a(wadr_bin), .we_a(wr), .clk_a(wr_clk), .q_b(q), .adr_b(radr_bin), .clk_b(rd_clk));
1964
 
1965
vl_fifo_cmp_async
1966
    # (.addr_width(addr_width))
1967
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
1968
 
1969
endmodule
1970
 
1971
module vl_fifo_2r2w (
1972
    // a side
1973
    a_d, a_wr, a_fifo_full,
1974
    a_q, a_rd, a_fifo_empty,
1975
    a_clk, a_rst,
1976
    // b side
1977
    b_d, b_wr, b_fifo_full,
1978
    b_q, b_rd, b_fifo_empty,
1979
    b_clk, b_rst
1980
    );
1981
 
1982
parameter data_width = 18;
1983
parameter addr_width = 4;
1984
 
1985
// a side
1986
input  [data_width-1:0] a_d;
1987
input                   a_wr;
1988
output                  a_fifo_full;
1989
output [data_width-1:0] a_q;
1990
input                   a_rd;
1991
output                  a_fifo_empty;
1992
input                   a_clk;
1993
input                   a_rst;
1994
 
1995
// b side
1996
input  [data_width-1:0] b_d;
1997
input                   b_wr;
1998
output                  b_fifo_full;
1999
output [data_width-1:0] b_q;
2000
input                   b_rd;
2001
output                  b_fifo_empty;
2002
input                   b_clk;
2003
input                   b_rst;
2004
 
2005
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
2006
vl_fifo_1r1w_async_a (
2007
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
2008
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
2009
    );
2010
 
2011
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
2012
vl_fifo_1r1w_async_b (
2013
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
2014
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
2015
    );
2016
 
2017
endmodule
2018
 
2019
module vl_fifo_2r2w_simplex (
2020
    // a side
2021
    a_d, a_wr, a_fifo_full,
2022
    a_q, a_rd, a_fifo_empty,
2023
    a_clk, a_rst,
2024
    // b side
2025
    b_d, b_wr, b_fifo_full,
2026
    b_q, b_rd, b_fifo_empty,
2027
    b_clk, b_rst
2028
    );
2029
 
2030
parameter data_width = 18;
2031
parameter addr_width = 4;
2032
 
2033
// a side
2034
input  [data_width-1:0] a_d;
2035
input                   a_wr;
2036
output                  a_fifo_full;
2037
output [data_width-1:0] a_q;
2038
input                   a_rd;
2039
output                  a_fifo_empty;
2040
input                   a_clk;
2041
input                   a_rst;
2042
 
2043
// b side
2044
input  [data_width-1:0] b_d;
2045
input                   b_wr;
2046
output                  b_fifo_full;
2047
output [data_width-1:0] b_q;
2048
input                   b_rd;
2049
output                  b_fifo_empty;
2050
input                   b_clk;
2051
input                   b_rst;
2052
 
2053
// adr_gen
2054
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
2055
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
2056
// dpram
2057
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
2058
 
2059 7 unneback
cnt_gray_ce_bin
2060 6 unneback
    # ( .length(addr_width))
2061
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
2062
 
2063 7 unneback
cnt_gray_ce_bin
2064 6 unneback
    # (.length(addr_width))
2065
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
2066
 
2067 7 unneback
cnt_gray_ce_bin
2068 6 unneback
    # ( .length(addr_width))
2069
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
2070
 
2071 7 unneback
cnt_gray_ce_bin
2072 6 unneback
    # (.length(addr_width))
2073
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
2074
 
2075
// mux read or write adr to DPRAM
2076
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
2077
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
2078
 
2079 7 unneback
vl_dp_ram_2r2w
2080 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
2081
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
2082
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
2083
 
2084
vl_fifo_async_cmp
2085
    # (.addr_width(addr_width))
2086
    cmp1 ( .wptr(a_wadr), .rptr(b_radr), .fifo_empty(b_fifo_empty), .fifo_full(a_fifo_full), .wclk(a_clk), .rclk(b_clk), .rst(a_rst) );
2087
 
2088 7 unneback
vl_fifo_async_cmp
2089 6 unneback
    # (.addr_width(addr_width))
2090
    cmp2 ( .wptr(b_wadr), .rptr(a_radr), .fifo_empty(a_fifo_empty), .fifo_full(b_fifo_full), .wclk(b_clk), .rclk(a_clk), .rst(b_rst) );
2091
 
2092
endmodule

powered by: WebSVN 2.1.0

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