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

Subversion Repositories versatile_library

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

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

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

powered by: WebSVN 2.1.0

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