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 90

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

Line No. Rev Author Line
1 40 unneback
`ifndef BASE
2
`define BASE vl_
3
`endif
4
 
5 60 unneback
// default SYN_KEEP definition
6
`define SYN_KEEP /*synthesis syn_keep = 1*/
7
 
8 44 unneback
`ifdef ACTEL
9 60 unneback
`undef SYN_KEEP
10 44 unneback
`define SYN_KEEP /*synthesis syn_keep = 1*/
11
`endif
12
 
13 40 unneback
`ifdef ALL
14
 
15
`define GBUF
16
`define SYNC_RST
17
`define PLL
18
 
19
`define MULTS
20
`define MULTS18X18
21
`define MULT
22
`define SHIFT_UNIT_32
23
`define LOGIC_UNIT
24
 
25
`define CNT_SHREG_WRAP
26
`define CNT_SHREG_CE_WRAP
27
`define CNT_SHREG_CE_CLEAR
28
`define CNT_SHREG_CE_CLEAR_WRAP
29
 
30
`define MUX_ANDOR
31
`define MUX2_ANDOR
32
`define MUX3_ANDOR
33
`define MUX4_ANDOR
34
`define MUX5_ANDOR
35
`define MUX6_ANDOR
36 43 unneback
`define PARITY
37 40 unneback
 
38
`define ROM_INIT
39
`define RAM
40
`define RAM_BE
41
`define DPRAM_1R1W
42
`define DPRAM_2R1W
43
`define DPRAM_2R2W
44 75 unneback
`define DPRAM_BE_2R2W
45 40 unneback
`define FIFO_1R1W_FILL_LEVEL_SYNC
46
`define FIFO_2R2W_SYNC_SIMPLEX
47
`define FIFO_CMP_ASYNC
48
`define FIFO_1R1W_ASYNC
49
`define FIFO_2R2W_ASYNC
50
`define FIFO_2R2W_ASYNC_SIMPLEX
51 48 unneback
`define REG_FILE
52 40 unneback
 
53
`define DFF
54
`define DFF_ARRAY
55
`define DFF_CE
56
`define DFF_CE_CLEAR
57
`define DF_CE_SET
58
`define SPR
59
`define SRP
60
`define DFF_SR
61
`define LATCH
62
`define SHREG
63
`define SHREG_CE
64
`define DELAY
65
`define DELAY_EMPTYFLAG
66
 
67 75 unneback
`define WB3AVALON_BRIDGE
68 40 unneback
`define WB3WB3_BRIDGE
69
`define WB3_ARBITER_TYPE1
70 83 unneback
`define WB_ADR_INC
71 59 unneback
`define WB_B3_RAM_BE
72 49 unneback
`define WB_B4_RAM_BE
73 48 unneback
`define WB_B4_ROM
74 40 unneback
`define WB_BOOT_ROM
75
`define WB_DPRAM
76
 
77 44 unneback
`define IO_DFF_OE
78
`define O_DFF
79
 
80 40 unneback
`endif
81
 
82
`ifdef PLL
83
`ifndef SYNC_RST
84
`define SYNC_RST
85
`endif
86
`endif
87
 
88
`ifdef SYNC_RST
89
`ifndef GBUF
90
`define GBUF
91
`endif
92
`endif
93
 
94
`ifdef WB_DPRAM
95
`ifndef DPRAM_2R2W
96
`define DPRAM_2R2W
97
`endif
98
`ifndef SPR
99
`define SPR
100
`endif
101
`endif
102
 
103 62 unneback
`ifdef WB_B3_RAM_BE
104 83 unneback
`ifndef WB_ADR_INC
105
`define WB_ADR_INC
106 62 unneback
`endif
107
`ifndef RAM_BE
108
`define RAM_BE
109
`endif
110
`endif
111
 
112 40 unneback
`ifdef WB3_ARBITER_TYPE1
113 42 unneback
`ifndef SPR
114
`define SPR
115
`endif
116 40 unneback
`ifndef MUX_ANDOR
117
`define MUX_ANDOR
118
`endif
119
`endif
120
 
121 76 unneback
`ifdef WB3AVALON_BRIDGE
122
`ifndef WB3WB3_BRIDGE
123
`define WB3WB3_BRIDGE
124
`endif
125
`endif
126
 
127 40 unneback
`ifdef WB3WB3_BRIDGE
128
`ifndef CNT_SHREG_CE_CLEAR
129
`define CNT_SHREG_CE_CLEAR
130
`endif
131
`ifndef DFF
132
`define DFF
133
`endif
134
`ifndef DFF_CE
135
`define DFF_CE
136
`endif
137
`ifndef CNT_SHREG_CE_CLEAR
138
`define CNT_SHREG_CE_CLEAR
139
`endif
140
`ifndef FIFO_2R2W_ASYNC_SIMPLEX
141
`define FIFO_2R2W_ASYNC_SIMPLEX
142
`endif
143
`endif
144
 
145
`ifdef MULTS18X18
146
`ifndef MULTS
147
`define MULTS
148
`endif
149
`endif
150
 
151
`ifdef SHIFT_UNIT_32
152
`ifndef MULTS
153
`define MULTS
154
`endif
155
`endif
156
 
157
`ifdef MUX2_ANDOR
158
`ifndef MUX_ANDOR
159
`define MUX_ANDOR
160
`endif
161
`endif
162
 
163
`ifdef MUX3_ANDOR
164
`ifndef MUX_ANDOR
165
`define MUX_ANDOR
166
`endif
167
`endif
168
 
169
`ifdef MUX4_ANDOR
170
`ifndef MUX_ANDOR
171
`define MUX_ANDOR
172
`endif
173
`endif
174
 
175
`ifdef MUX5_ANDOR
176
`ifndef MUX_ANDOR
177
`define MUX_ANDOR
178
`endif
179
`endif
180
 
181
`ifdef MUX6_ANDOR
182
`ifndef MUX_ANDOR
183
`define MUX_ANDOR
184
`endif
185
`endif
186
 
187
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
188
`ifndef CNT_BIN_CE
189
`define CNT_BIN_CE
190
`endif
191
`ifndef DPRAM_1R1W
192
`define DPRAM_1R1W
193
`endif
194
`ifndef CNT_BIN_CE_REW_Q_ZQ_L1
195
`define CNT_BIN_CE_REW_Q_ZQ_L1
196
`endif
197
`endif
198
 
199
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
200
`ifndef CNT_LFSR_CE
201
`define CNT_LFSR_CE
202
`endif
203
`ifndef DPRAM_2R2W
204
`define DPRAM_2R2W
205
`endif
206
`ifndef CNT_BIN_CE_REW_ZQ_L1
207
`define CNT_BIN_CE_REW_ZQ_L1
208
`endif
209
`endif
210
 
211
`ifdef FIFO_2R2W_ASYNC_SIMPLEX
212
`ifndef CNT_GRAY_CE_BIN
213
`define CNT_GRAY_CE_BIN
214
`endif
215
`ifndef DPRAM_2R2W
216
`define DPRAM_2R2W
217
`endif
218
`ifndef FIFO_CMP_ASYNC
219
`define FIFO_CMP_ASYNC
220
`endif
221
`endif
222
 
223
`ifdef FIFO_2R2W_ASYNC
224
`ifndef FIFO_1R1W_ASYNC
225
`define FIFO_1R1W_ASYNC
226
`endif
227
`endif
228
 
229
`ifdef FIFO_1R1W_ASYNC
230
`ifndef CNT_GRAY_CE_BIN
231
`define CNT_GRAY_CE_BIN
232
`endif
233
`ifndef DPRAM_1R1W
234
`define DPRAM_1R1W
235
`endif
236
`ifndef FIFO_CMP_ASYNC
237
`define FIFO_CMP_ASYNC
238
`endif
239
`endif
240
 
241
`ifdef FIFO_CMP_ASYNC
242
`ifndef DFF_SR
243
`define DFF_SR
244
`endif
245
`ifndef DFF
246
`define DFF
247
`endif
248
`endif
249 48 unneback
 
250
`ifdef REG_FILE
251
`ifndef DPRAM_1R1W
252
`define DPRAM_1R1W
253
`endif
254
`endif
255 62 unneback
//////////////////////////////////////////////////////////////////////
256 6 unneback
////                                                              ////
257
////  Versatile library, clock and reset                          ////
258
////                                                              ////
259
////  Description                                                 ////
260
////  Logic related to clock and reset                            ////
261
////                                                              ////
262
////                                                              ////
263
////  To Do:                                                      ////
264
////   - add more different registers                             ////
265
////                                                              ////
266
////  Author(s):                                                  ////
267
////      - Michael Unneback, unneback@opencores.org              ////
268
////        ORSoC AB                                              ////
269
////                                                              ////
270
//////////////////////////////////////////////////////////////////////
271
////                                                              ////
272
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
273
////                                                              ////
274
//// This source file may be used and distributed without         ////
275
//// restriction provided that this copyright statement is not    ////
276
//// removed from the file and that any derivative work contains  ////
277
//// the original copyright notice and the associated disclaimer. ////
278
////                                                              ////
279
//// This source file is free software; you can redistribute it   ////
280
//// and/or modify it under the terms of the GNU Lesser General   ////
281
//// Public License as published by the Free Software Foundation; ////
282
//// either version 2.1 of the License, or (at your option) any   ////
283
//// later version.                                               ////
284
////                                                              ////
285
//// This source is distributed in the hope that it will be       ////
286
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
287
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
288
//// PURPOSE.  See the GNU Lesser General Public License for more ////
289
//// details.                                                     ////
290
////                                                              ////
291
//// You should have received a copy of the GNU Lesser General    ////
292
//// Public License along with this source; if not, download it   ////
293
//// from http://www.opencores.org/lgpl.shtml                     ////
294
////                                                              ////
295
//////////////////////////////////////////////////////////////////////
296
 
297 48 unneback
`ifdef ACTEL
298
`ifdef GBUF
299
`timescale 1 ns/100 ps
300 6 unneback
// Global buffer
301
// usage:
302
// use to enable global buffers for high fan out signals such as clock and reset
303
// Version: 8.4 8.4.0.33
304
module gbuf(GL,CLK);
305
output GL;
306
input  CLK;
307
 
308
    wire GND;
309
 
310
    GND GND_1_net(.Y(GND));
311
    CLKDLY Inst1(.CLK(CLK), .GL(GL), .DLYGL0(GND), .DLYGL1(GND),
312
        .DLYGL2(GND), .DLYGL3(GND), .DLYGL4(GND)) /* synthesis black_box */;
313
 
314
endmodule
315
`timescale 1 ns/1 ns
316 40 unneback
`define MODULE gbuf
317
module `BASE`MODULE ( i, o);
318
`undef MODULE
319 6 unneback
input i;
320
output o;
321
`ifdef SIM_GBUF
322
assign o=i;
323
`else
324
gbuf gbuf_i0 ( .CLK(i), .GL(o));
325
`endif
326
endmodule
327 40 unneback
`endif
328 33 unneback
 
329 6 unneback
`else
330 33 unneback
 
331 40 unneback
`ifdef ALTERA
332
`ifdef GBUF
333 21 unneback
//altera
334 40 unneback
`define MODULE gbuf
335
module `BASE`MODULE ( i, o);
336
`undef MODULE
337 33 unneback
input i;
338
output o;
339
assign o = i;
340
endmodule
341 40 unneback
`endif
342 33 unneback
 
343 6 unneback
`else
344
 
345 40 unneback
`ifdef GBUF
346 6 unneback
`timescale 1 ns/100 ps
347 40 unneback
`define MODULE
348
module `BASE`MODULE ( i, o);
349
`undef MODULE
350 6 unneback
input i;
351
output o;
352
assign o = i;
353
endmodule
354 40 unneback
`endif
355 6 unneback
`endif // ALTERA
356
`endif //ACTEL
357
 
358 40 unneback
`ifdef SYNC_RST
359 6 unneback
// sync reset
360 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
361 6 unneback
// output active high global reset sync with two DFFs 
362
`timescale 1 ns/100 ps
363 40 unneback
`define MODULE sync_rst
364
module `BASE`MODULE ( rst_n_i, rst_o, clk);
365
`undef MODULE
366 6 unneback
input rst_n_i, clk;
367
output rst_o;
368 18 unneback
reg [1:0] tmp;
369 6 unneback
always @ (posedge clk or negedge rst_n_i)
370
if (!rst_n_i)
371 17 unneback
        tmp <= 2'b11;
372 6 unneback
else
373 33 unneback
        tmp <= {1'b0,tmp[1]};
374 40 unneback
`define MODULE gbuf
375
`BASE`MODULE buf_i0( .i(tmp[0]), .o(rst_o));
376
`undef MODULE
377 6 unneback
endmodule
378 40 unneback
`endif
379 6 unneback
 
380 40 unneback
`ifdef PLL
381 6 unneback
// vl_pll
382
`ifdef ACTEL
383 32 unneback
///////////////////////////////////////////////////////////////////////////////
384 17 unneback
`timescale 1 ps/1 ps
385 40 unneback
`define MODULE pll
386
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
387
`undef MODULE
388 6 unneback
parameter index = 0;
389
parameter number_of_clk = 1;
390 17 unneback
parameter period_time_0 = 20000;
391
parameter period_time_1 = 20000;
392
parameter period_time_2 = 20000;
393
parameter lock_delay = 2000000;
394 6 unneback
input clk_i, rst_n_i;
395
output lock;
396
output reg [0:number_of_clk-1] clk_o;
397
output [0:number_of_clk-1] rst_o;
398
 
399
`ifdef SIM_PLL
400
 
401
always
402
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
403
 
404
generate if (number_of_clk > 1)
405
always
406
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
407
endgenerate
408
 
409
generate if (number_of_clk > 2)
410
always
411
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
412
endgenerate
413
 
414
genvar i;
415
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
416
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
417
end
418
endgenerate
419
 
420
assign #lock_delay lock = rst_n_i;
421
 
422
endmodule
423
`else
424
generate if (number_of_clk==1 & index==0) begin
425
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
426
end
427
endgenerate // index==0
428
generate if (number_of_clk==1 & index==1) begin
429
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
430
end
431
endgenerate // index==1
432
generate if (number_of_clk==1 & index==2) begin
433
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
434
end
435
endgenerate // index==2
436
generate if (number_of_clk==1 & index==3) begin
437
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
438
end
439
endgenerate // index==0
440
 
441
generate if (number_of_clk==2 & index==0) begin
442
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
443
end
444
endgenerate // index==0
445
generate if (number_of_clk==2 & index==1) begin
446
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
447
end
448
endgenerate // index==1
449
generate if (number_of_clk==2 & index==2) begin
450
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
451
end
452
endgenerate // index==2
453
generate if (number_of_clk==2 & index==3) begin
454
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
455
end
456
endgenerate // index==0
457
 
458
generate if (number_of_clk==3 & index==0) begin
459
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
460
end
461
endgenerate // index==0
462
generate if (number_of_clk==3 & index==1) begin
463
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
464
end
465
endgenerate // index==1
466
generate if (number_of_clk==3 & index==2) begin
467
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
468
end
469
endgenerate // index==2
470
generate if (number_of_clk==3 & index==3) begin
471
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
472
end
473
endgenerate // index==0
474
 
475
genvar i;
476
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
477 40 unneback
`define MODULE sync_rst
478
        `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
479
`undef MODULE
480 6 unneback
end
481
endgenerate
482
endmodule
483
`endif
484 32 unneback
///////////////////////////////////////////////////////////////////////////////
485 6 unneback
 
486
`else
487
 
488 32 unneback
///////////////////////////////////////////////////////////////////////////////
489 6 unneback
`ifdef ALTERA
490
 
491 32 unneback
`timescale 1 ps/1 ps
492 40 unneback
`define MODULE pll
493
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
494
`undef MODULE
495 32 unneback
parameter index = 0;
496
parameter number_of_clk = 1;
497
parameter period_time_0 = 20000;
498
parameter period_time_1 = 20000;
499
parameter period_time_2 = 20000;
500
parameter period_time_3 = 20000;
501
parameter period_time_4 = 20000;
502
parameter lock_delay = 2000000;
503
input clk_i, rst_n_i;
504
output lock;
505
output reg [0:number_of_clk-1] clk_o;
506
output [0:number_of_clk-1] rst_o;
507
 
508
`ifdef SIM_PLL
509
 
510
always
511
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
512
 
513
generate if (number_of_clk > 1)
514
always
515
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
516
endgenerate
517
 
518
generate if (number_of_clk > 2)
519
always
520
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
521
endgenerate
522
 
523 33 unneback
generate if (number_of_clk > 3)
524 32 unneback
always
525
     #((period_time_3)/2) clk_o[3] <=  (!rst_n_i) ? 0 : ~clk_o[3];
526
endgenerate
527
 
528 33 unneback
generate if (number_of_clk > 4)
529 32 unneback
always
530
     #((period_time_4)/2) clk_o[4] <=  (!rst_n_i) ? 0 : ~clk_o[4];
531
endgenerate
532
 
533
genvar i;
534
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
535
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
536
end
537
endgenerate
538
 
539 33 unneback
//assign #lock_delay lock = rst_n_i;
540
assign lock = rst_n_i;
541 32 unneback
 
542
endmodule
543 6 unneback
`else
544
 
545 33 unneback
`ifdef VL_PLL0
546
`ifdef VL_PLL0_CLK1
547
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
548
`endif
549
`ifdef VL_PLL0_CLK2
550
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
551
`endif
552
`ifdef VL_PLL0_CLK3
553
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
554
`endif
555
`ifdef VL_PLL0_CLK4
556
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
557
`endif
558
`ifdef VL_PLL0_CLK5
559
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
560
`endif
561
`endif
562 32 unneback
 
563 33 unneback
`ifdef VL_PLL1
564
`ifdef VL_PLL1_CLK1
565
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
566
`endif
567
`ifdef VL_PLL1_CLK2
568
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
569
`endif
570
`ifdef VL_PLL1_CLK3
571
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
572
`endif
573
`ifdef VL_PLL1_CLK4
574
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
575
`endif
576
`ifdef VL_PLL1_CLK5
577
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
578
`endif
579
`endif
580 32 unneback
 
581 33 unneback
`ifdef VL_PLL2
582
`ifdef VL_PLL2_CLK1
583
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
584
`endif
585
`ifdef VL_PLL2_CLK2
586
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
587
`endif
588
`ifdef VL_PLL2_CLK3
589
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
590
`endif
591
`ifdef VL_PLL2_CLK4
592
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
593
`endif
594
`ifdef VL_PLL2_CLK5
595
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
596
`endif
597
`endif
598 32 unneback
 
599 33 unneback
`ifdef VL_PLL3
600
`ifdef VL_PLL3_CLK1
601
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
602
`endif
603
`ifdef VL_PLL3_CLK2
604
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
605
`endif
606
`ifdef VL_PLL3_CLK3
607
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
608
`endif
609
`ifdef VL_PLL3_CLK4
610
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]));
611
`endif
612
`ifdef VL_PLL3_CLK5
613
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]), .c3(clk_o[3]), .c4(clk_o[4]));
614
`endif
615
`endif
616 32 unneback
 
617
genvar i;
618
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
619 40 unneback
`define MODULE sync_rst
620
        `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
621
`undef MODULE
622 32 unneback
end
623
endgenerate
624
endmodule
625
`endif
626
///////////////////////////////////////////////////////////////////////////////
627
 
628
`else
629
 
630 6 unneback
// generic PLL
631 17 unneback
`timescale 1 ps/1 ps
632 40 unneback
`define MODULE pll
633
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
634
`undef MODULE
635 6 unneback
parameter index = 0;
636
parameter number_of_clk = 1;
637 17 unneback
parameter period_time_0 = 20000;
638
parameter period_time_1 = 20000;
639
parameter period_time_2 = 20000;
640 6 unneback
parameter lock_delay = 2000;
641
input clk_i, rst_n_i;
642
output lock;
643
output reg [0:number_of_clk-1] clk_o;
644
output [0:number_of_clk-1] rst_o;
645
 
646
always
647
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
648
 
649
generate if (number_of_clk > 1)
650
always
651
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
652
endgenerate
653
 
654
generate if (number_of_clk > 2)
655
always
656
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
657
endgenerate
658
 
659
genvar i;
660
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
661 40 unneback
`define MODULE sync_rst
662
     `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
663
`undef MODULE
664 6 unneback
end
665
endgenerate
666
 
667
assign #lock_delay lock = rst_n_i;
668
 
669
endmodule
670
 
671
`endif //altera
672 17 unneback
`endif //actel
673 40 unneback
`undef MODULE
674
`endif//////////////////////////////////////////////////////////////////////
675 6 unneback
////                                                              ////
676
////  Versatile library, registers                                ////
677
////                                                              ////
678
////  Description                                                 ////
679
////  Different type of registers                                 ////
680
////                                                              ////
681
////                                                              ////
682
////  To Do:                                                      ////
683
////   - add more different registers                             ////
684
////                                                              ////
685
////  Author(s):                                                  ////
686
////      - Michael Unneback, unneback@opencores.org              ////
687
////        ORSoC AB                                              ////
688
////                                                              ////
689
//////////////////////////////////////////////////////////////////////
690
////                                                              ////
691
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
692
////                                                              ////
693
//// This source file may be used and distributed without         ////
694
//// restriction provided that this copyright statement is not    ////
695
//// removed from the file and that any derivative work contains  ////
696
//// the original copyright notice and the associated disclaimer. ////
697
////                                                              ////
698
//// This source file is free software; you can redistribute it   ////
699
//// and/or modify it under the terms of the GNU Lesser General   ////
700
//// Public License as published by the Free Software Foundation; ////
701
//// either version 2.1 of the License, or (at your option) any   ////
702
//// later version.                                               ////
703
////                                                              ////
704
//// This source is distributed in the hope that it will be       ////
705
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
706
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
707
//// PURPOSE.  See the GNU Lesser General Public License for more ////
708
//// details.                                                     ////
709
////                                                              ////
710
//// You should have received a copy of the GNU Lesser General    ////
711
//// Public License along with this source; if not, download it   ////
712
//// from http://www.opencores.org/lgpl.shtml                     ////
713
////                                                              ////
714
//////////////////////////////////////////////////////////////////////
715
 
716 40 unneback
`ifdef DFF
717
`define MODULE dff
718
module `BASE`MODULE ( d, q, clk, rst);
719
`undef MODULE
720 6 unneback
        parameter width = 1;
721
        parameter reset_value = 0;
722
 
723
        input [width-1:0] d;
724
        input clk, rst;
725
        output reg [width-1:0] q;
726
 
727
        always @ (posedge clk or posedge rst)
728
        if (rst)
729
                q <= reset_value;
730
        else
731
                q <= d;
732
 
733
endmodule
734 40 unneback
`endif
735 6 unneback
 
736 40 unneback
`ifdef DFF_ARRAY
737
`define MODULE dff_array
738
module `BASE`MODULE ( d, q, clk, rst);
739
`undef MODULE
740 6 unneback
 
741
        parameter width = 1;
742
        parameter depth = 2;
743
        parameter reset_value = 1'b0;
744
 
745
        input [width-1:0] d;
746
        input clk, rst;
747
        output [width-1:0] q;
748
        reg  [0:depth-1] q_tmp [width-1:0];
749
        integer i;
750
        always @ (posedge clk or posedge rst)
751
        if (rst) begin
752
            for (i=0;i<depth;i=i+1)
753
                q_tmp[i] <= {width{reset_value}};
754
        end else begin
755
            q_tmp[0] <= d;
756
            for (i=1;i<depth;i=i+1)
757
                q_tmp[i] <= q_tmp[i-1];
758
        end
759
 
760
    assign q = q_tmp[depth-1];
761
 
762
endmodule
763 40 unneback
`endif
764 6 unneback
 
765 40 unneback
`ifdef DFF_CE
766
`define MODULE dff_ce
767
module `BASE`MODULE ( d, ce, q, clk, rst);
768
`undef MODULE
769 6 unneback
 
770
        parameter width = 1;
771
        parameter reset_value = 0;
772
 
773
        input [width-1:0] d;
774
        input ce, clk, rst;
775
        output reg [width-1:0] q;
776
 
777
        always @ (posedge clk or posedge rst)
778
        if (rst)
779
                q <= reset_value;
780
        else
781
                if (ce)
782
                        q <= d;
783
 
784
endmodule
785 40 unneback
`endif
786 6 unneback
 
787 40 unneback
`ifdef DFF_CE_CLEAR
788
`define MODULE dff_ce_clear
789
module `BASE`MODULE ( d, ce, clear, q, clk, rst);
790
`undef MODULE
791 8 unneback
 
792
        parameter width = 1;
793
        parameter reset_value = 0;
794
 
795
        input [width-1:0] d;
796 10 unneback
        input ce, clear, clk, rst;
797 8 unneback
        output reg [width-1:0] q;
798
 
799
        always @ (posedge clk or posedge rst)
800
        if (rst)
801
            q <= reset_value;
802
        else
803
            if (ce)
804
                if (clear)
805
                    q <= {width{1'b0}};
806
                else
807
                    q <= d;
808
 
809
endmodule
810 40 unneback
`endif
811 8 unneback
 
812 40 unneback
`ifdef DF_CE_SET
813
`define MODULE dff_ce_set
814
module `BASE`MODULE ( d, ce, set, q, clk, rst);
815
`undef MODULE
816 24 unneback
 
817
        parameter width = 1;
818
        parameter reset_value = 0;
819
 
820
        input [width-1:0] d;
821
        input ce, set, clk, rst;
822
        output reg [width-1:0] q;
823
 
824
        always @ (posedge clk or posedge rst)
825
        if (rst)
826
            q <= reset_value;
827
        else
828
            if (ce)
829
                if (set)
830
                    q <= {width{1'b1}};
831
                else
832
                    q <= d;
833
 
834
endmodule
835 40 unneback
`endif
836 24 unneback
 
837 40 unneback
`ifdef SPR
838
`define MODULE spr
839
module `BASE`MODULE ( sp, r, q, clk, rst);
840
`undef MODULE
841
 
842 64 unneback
        //parameter width = 1;
843
        parameter reset_value = 1'b0;
844 29 unneback
 
845
        input sp, r;
846
        output reg q;
847
        input clk, rst;
848
 
849
        always @ (posedge clk or posedge rst)
850
        if (rst)
851
            q <= reset_value;
852
        else
853
            if (sp)
854
                q <= 1'b1;
855
            else if (r)
856
                q <= 1'b0;
857
 
858
endmodule
859 40 unneback
`endif
860 29 unneback
 
861 40 unneback
`ifdef SRP
862
`define MODULE srp
863
module `BASE`MODULE ( s, rp, q, clk, rst);
864
`undef MODULE
865
 
866 29 unneback
        parameter width = 1;
867
        parameter reset_value = 0;
868
 
869
        input s, rp;
870
        output reg q;
871
        input clk, rst;
872
 
873
        always @ (posedge clk or posedge rst)
874
        if (rst)
875
            q <= reset_value;
876
        else
877
            if (rp)
878
                q <= 1'b0;
879
            else if (s)
880
                q <= 1'b1;
881
 
882
endmodule
883 40 unneback
`endif
884 29 unneback
 
885 40 unneback
`ifdef ALTERA
886 29 unneback
 
887 40 unneback
`ifdef DFF_SR
888 6 unneback
// megafunction wizard: %LPM_FF%
889
// GENERATION: STANDARD
890
// VERSION: WM1.0
891
// MODULE: lpm_ff 
892
 
893
// ============================================================
894
// File Name: dff_sr.v
895
// Megafunction Name(s):
896
//                      lpm_ff
897
//
898
// Simulation Library Files(s):
899
//                      lpm
900
// ============================================================
901
// ************************************************************
902
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
903
//
904
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
905
// ************************************************************
906
 
907
 
908
//Copyright (C) 1991-2010 Altera Corporation
909
//Your use of Altera Corporation's design tools, logic functions 
910
//and other software and tools, and its AMPP partner logic 
911
//functions, and any output files from any of the foregoing 
912
//(including device programming or simulation files), and any 
913
//associated documentation or information are expressly subject 
914
//to the terms and conditions of the Altera Program License 
915
//Subscription Agreement, Altera MegaCore Function License 
916
//Agreement, or other applicable license agreement, including, 
917
//without limitation, that your use is for the sole purpose of 
918
//programming logic devices manufactured by Altera and sold by 
919
//Altera or its authorized distributors.  Please refer to the 
920
//applicable agreement for further details.
921
 
922
 
923
// synopsys translate_off
924
`timescale 1 ps / 1 ps
925
// synopsys translate_on
926 40 unneback
`define MODULE dff_sr
927
module `BASE`MODULE (
928
`undef MODULE
929
 
930 6 unneback
        aclr,
931
        aset,
932
        clock,
933
        data,
934
        q);
935
 
936
        input     aclr;
937
        input     aset;
938
        input     clock;
939
        input     data;
940
        output    q;
941
 
942
        wire [0:0] sub_wire0;
943
        wire [0:0] sub_wire1 = sub_wire0[0:0];
944
        wire  q = sub_wire1;
945
        wire  sub_wire2 = data;
946
        wire  sub_wire3 = sub_wire2;
947
 
948
        lpm_ff  lpm_ff_component (
949
                                .aclr (aclr),
950
                                .clock (clock),
951
                                .data (sub_wire3),
952
                                .aset (aset),
953
                                .q (sub_wire0)
954
                                // synopsys translate_off
955
                                ,
956
                                .aload (),
957
                                .enable (),
958
                                .sclr (),
959
                                .sload (),
960
                                .sset ()
961
                                // synopsys translate_on
962
                                );
963
        defparam
964
                lpm_ff_component.lpm_fftype = "DFF",
965
                lpm_ff_component.lpm_type = "LPM_FF",
966
                lpm_ff_component.lpm_width = 1;
967
 
968
 
969
endmodule
970
 
971
// ============================================================
972
// CNX file retrieval info
973
// ============================================================
974
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
975
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
976
// Retrieval info: PRIVATE: ASET NUMERIC "1"
977
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
978
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
979
// Retrieval info: PRIVATE: DFF NUMERIC "1"
980
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
981
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
982
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
983
// Retrieval info: PRIVATE: SSET NUMERIC "0"
984
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
985
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
986
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
987
// Retrieval info: PRIVATE: nBit NUMERIC "1"
988
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
989
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
990
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
991
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
992
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
993
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
994
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
995
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
996
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
997
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
998
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
999
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
1000
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
1001
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
1002
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
1003
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
1004
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
1005
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
1006
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
1007
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
1008
// Retrieval info: LIB_FILE: lpm
1009 40 unneback
`endif
1010 6 unneback
 
1011
`else
1012
 
1013 40 unneback
`ifdef DFF_SR
1014
`define MODULE dff_sr
1015
module `BASE`MODULE ( aclr, aset, clock, data, q);
1016
`undef MODULE
1017 6 unneback
 
1018
    input         aclr;
1019
    input         aset;
1020
    input         clock;
1021
    input         data;
1022
    output reg    q;
1023
 
1024
   always @ (posedge clock or posedge aclr or posedge aset)
1025
     if (aclr)
1026
       q <= 1'b0;
1027
     else if (aset)
1028
       q <= 1'b1;
1029
     else
1030
       q <= data;
1031
 
1032
endmodule
1033 40 unneback
`endif
1034 6 unneback
 
1035
`endif
1036
 
1037
// LATCH
1038
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
1039
`ifdef ALTERA
1040 40 unneback
 
1041
`ifdef LATCH
1042
`define MODULE latch
1043
module `BASE`MODULE ( d, le, q, clk);
1044
`undef MODULE
1045 6 unneback
input d, le;
1046
output q;
1047
input clk;
1048
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
1049
endmodule
1050 40 unneback
`endif
1051
 
1052 6 unneback
`else
1053 40 unneback
 
1054
`ifdef LATCH
1055
`define MODULE latch
1056
module `BASE`MODULE ( d, le, q, clk);
1057
`undef MODULE
1058 6 unneback
input d, le;
1059 48 unneback
input clk;
1060
always @ (le or d)
1061 60 unneback
if (le)
1062 48 unneback
    d <= q;
1063 6 unneback
endmodule
1064 15 unneback
`endif
1065
 
1066 40 unneback
`endif
1067
 
1068
`ifdef SHREG
1069
`define MODULE shreg
1070
module `BASE`MODULE ( d, q, clk, rst);
1071
`undef MODULE
1072
 
1073 17 unneback
parameter depth = 10;
1074
input d;
1075
output q;
1076
input clk, rst;
1077
 
1078
reg [1:depth] dffs;
1079
 
1080
always @ (posedge clk or posedge rst)
1081
if (rst)
1082
    dffs <= {depth{1'b0}};
1083
else
1084
    dffs <= {d,dffs[1:depth-1]};
1085
assign q = dffs[depth];
1086
endmodule
1087 40 unneback
`endif
1088 17 unneback
 
1089 40 unneback
`ifdef SHREG_CE
1090
`define MODULE shreg_ce
1091
module `BASE`MODULE ( d, ce, q, clk, rst);
1092
`undef MODULE
1093 17 unneback
parameter depth = 10;
1094
input d, ce;
1095
output q;
1096
input clk, rst;
1097
 
1098
reg [1:depth] dffs;
1099
 
1100
always @ (posedge clk or posedge rst)
1101
if (rst)
1102
    dffs <= {depth{1'b0}};
1103
else
1104
    if (ce)
1105
        dffs <= {d,dffs[1:depth-1]};
1106
assign q = dffs[depth];
1107
endmodule
1108 40 unneback
`endif
1109 17 unneback
 
1110 40 unneback
`ifdef DELAY
1111
`define MODULE delay
1112
module `BASE`MODULE ( d, q, clk, rst);
1113
`undef MODULE
1114 15 unneback
parameter depth = 10;
1115
input d;
1116
output q;
1117
input clk, rst;
1118
 
1119
reg [1:depth] dffs;
1120
 
1121
always @ (posedge clk or posedge rst)
1122
if (rst)
1123
    dffs <= {depth{1'b0}};
1124
else
1125
    dffs <= {d,dffs[1:depth-1]};
1126
assign q = dffs[depth];
1127 17 unneback
endmodule
1128 40 unneback
`endif
1129 17 unneback
 
1130 40 unneback
`ifdef DELAY_EMPTYFLAG
1131
`define MODULE delay_emptyflag
1132 41 unneback
module `BASE`MODULE ( d, q, emptyflag, clk, rst);
1133 40 unneback
`undef MODULE
1134 17 unneback
parameter depth = 10;
1135
input d;
1136
output q, emptyflag;
1137
input clk, rst;
1138
 
1139
reg [1:depth] dffs;
1140
 
1141
always @ (posedge clk or posedge rst)
1142
if (rst)
1143
    dffs <= {depth{1'b0}};
1144
else
1145
    dffs <= {d,dffs[1:depth-1]};
1146
assign q = dffs[depth];
1147
assign emptyflag = !(|dffs);
1148
endmodule
1149 40 unneback
`endif
1150 75 unneback
 
1151
`ifdef ASYNC_REG_REQ_ACK
1152
`define MODULE async_reg_req_ack
1153
module `BASE`MODULE ( d, q, req_i, req_o, ack_i, ack_o, clk_a, rst_a, clk_b, rst_b);
1154
`undef MODULE
1155
parameter data_width = 8;
1156
input [data_width-1:0] d;
1157
output [data_width-1:0] q;
1158
input req_i;
1159
output req_o;
1160
input ack_i;
1161
output ack_o;
1162
input clk_a, rst_a, clk_b, rst_b;
1163
 
1164
reg [3:0] reqi; // 3: last req in clk_a, 2: input dff, 1-0: sync
1165
wire rst;
1166
 
1167
always @ (posedge clk_a or rst_a)
1168
if (rst_a)
1169
    q <= {data_width{1'b0}};
1170
else
1171
    if (req_i)
1172
        q <= d;
1173
 
1174
assign rst = ack_i | rst_a;
1175
always @ (posedge clk_a or posedge rst)
1176
if (rst)
1177
    req[2] <= 1'b0;
1178
else
1179
    req[2] <= req_i & !ack_o;
1180
 
1181
always @ (posedge clk_a or posedge rst_a)
1182
if (rst_a)
1183
    req[3] <= 1'b0;
1184
else
1185
    req[3] <= req[2];
1186
 
1187
always @ (posedge clk_b or posedge rst_b)
1188
if (rst_b)
1189
    req[1:0] <= 2'b00;
1190
else
1191
    if (ack_i)
1192
        req[1:0] <= 2'b00;
1193
    else
1194
        req[1:0] <= req[2:1];
1195
assign req_o = req[0];
1196
 
1197
always @ (posedge clk_a or posedge rst_a)
1198
if (rst_a)
1199
    ack_o <= 1'b0;
1200
else
1201
    ack_o <= req[3] & req[2];
1202
 
1203
endmodule
1204
`endif
1205 17 unneback
//////////////////////////////////////////////////////////////////////
1206 6 unneback
////                                                              ////
1207 18 unneback
////  Logic functions                                             ////
1208
////                                                              ////
1209
////  Description                                                 ////
1210
////  Logic functions such as multiplexers                        ////
1211
////                                                              ////
1212
////                                                              ////
1213
////  To Do:                                                      ////
1214
////   -                                                          ////
1215
////                                                              ////
1216
////  Author(s):                                                  ////
1217
////      - Michael Unneback, unneback@opencores.org              ////
1218
////        ORSoC AB                                              ////
1219
////                                                              ////
1220
//////////////////////////////////////////////////////////////////////
1221
////                                                              ////
1222
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1223
////                                                              ////
1224
//// This source file may be used and distributed without         ////
1225
//// restriction provided that this copyright statement is not    ////
1226
//// removed from the file and that any derivative work contains  ////
1227
//// the original copyright notice and the associated disclaimer. ////
1228
////                                                              ////
1229
//// This source file is free software; you can redistribute it   ////
1230
//// and/or modify it under the terms of the GNU Lesser General   ////
1231
//// Public License as published by the Free Software Foundation; ////
1232
//// either version 2.1 of the License, or (at your option) any   ////
1233
//// later version.                                               ////
1234
////                                                              ////
1235
//// This source is distributed in the hope that it will be       ////
1236
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1237
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1238
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1239
//// details.                                                     ////
1240
////                                                              ////
1241
//// You should have received a copy of the GNU Lesser General    ////
1242
//// Public License along with this source; if not, download it   ////
1243
//// from http://www.opencores.org/lgpl.shtml                     ////
1244
////                                                              ////
1245
//////////////////////////////////////////////////////////////////////
1246 40 unneback
`ifdef MUX_ANDOR
1247
`define MODULE mux_andor
1248
module `BASE`MODULE ( a, sel, dout);
1249
`undef MODULE
1250 36 unneback
 
1251
parameter width = 32;
1252
parameter nr_of_ports = 4;
1253
 
1254
input [nr_of_ports*width-1:0] a;
1255
input [nr_of_ports-1:0] sel;
1256
output reg [width-1:0] dout;
1257
 
1258 38 unneback
integer i,j;
1259
 
1260 36 unneback
always @ (a, sel)
1261
begin
1262
    dout = a[width-1:0] & {width{sel[0]}};
1263 42 unneback
    for (i=1;i<nr_of_ports;i=i+1)
1264
        for (j=0;j<width;j=j+1)
1265
            dout[j] = (a[i*width + j] & sel[i]) | dout[j];
1266 36 unneback
end
1267
 
1268
endmodule
1269 40 unneback
`endif
1270 36 unneback
 
1271 40 unneback
`ifdef MUX2_ANDOR
1272
`define MODULE mux2_andor
1273
module `BASE`MODULE ( a1, a0, sel, dout);
1274
`undef MODULE
1275 18 unneback
 
1276 34 unneback
parameter width = 32;
1277 35 unneback
localparam nr_of_ports = 2;
1278 34 unneback
input [width-1:0] a1, a0;
1279
input [nr_of_ports-1:0] sel;
1280
output [width-1:0] dout;
1281
 
1282 40 unneback
`define MODULE mux_andor
1283
`BASE`MODULE
1284 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1285 36 unneback
    mux0( .a({a1,a0}), .sel(sel), .dout(dout));
1286 40 unneback
`undef MODULE
1287
 
1288 34 unneback
endmodule
1289 40 unneback
`endif
1290 34 unneback
 
1291 40 unneback
`ifdef MUX3_ANDOR
1292
`define MODULE mux3_andor
1293
module `BASE`MODULE ( a2, a1, a0, sel, dout);
1294
`undef MODULE
1295 34 unneback
 
1296
parameter width = 32;
1297 35 unneback
localparam nr_of_ports = 3;
1298 34 unneback
input [width-1:0] a2, a1, a0;
1299
input [nr_of_ports-1:0] sel;
1300
output [width-1:0] dout;
1301
 
1302 40 unneback
`define MODULE mux_andor
1303
`BASE`MODULE
1304 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1305 36 unneback
    mux0( .a({a2,a1,a0}), .sel(sel), .dout(dout));
1306 40 unneback
`undef MODULE
1307 34 unneback
endmodule
1308 40 unneback
`endif
1309 34 unneback
 
1310 40 unneback
`ifdef MUX4_ANDOR
1311
`define MODULE mux4_andor
1312
module `BASE`MODULE ( a3, a2, a1, a0, sel, dout);
1313
`undef MODULE
1314 18 unneback
 
1315
parameter width = 32;
1316 35 unneback
localparam nr_of_ports = 4;
1317 18 unneback
input [width-1:0] a3, a2, a1, a0;
1318
input [nr_of_ports-1:0] sel;
1319 22 unneback
output [width-1:0] dout;
1320 18 unneback
 
1321 40 unneback
`define MODULE mux_andor
1322
`BASE`MODULE
1323 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1324 36 unneback
    mux0( .a({a3,a2,a1,a0}), .sel(sel), .dout(dout));
1325 40 unneback
`undef MODULE
1326 18 unneback
 
1327
endmodule
1328 40 unneback
`endif
1329 18 unneback
 
1330 40 unneback
`ifdef MUX5_ANDOR
1331
`define MODULE mux5_andor
1332
module `BASE`MODULE ( a4, a3, a2, a1, a0, sel, dout);
1333
`undef MODULE
1334 18 unneback
 
1335
parameter width = 32;
1336 35 unneback
localparam nr_of_ports = 5;
1337 18 unneback
input [width-1:0] a4, a3, a2, a1, a0;
1338
input [nr_of_ports-1:0] sel;
1339 22 unneback
output [width-1:0] dout;
1340 18 unneback
 
1341 40 unneback
`define MODULE mux_andor
1342
`BASE`MODULE
1343 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1344 36 unneback
    mux0( .a({a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
1345 40 unneback
`undef MODULE
1346 18 unneback
 
1347
endmodule
1348 40 unneback
`endif
1349 18 unneback
 
1350 40 unneback
`ifdef MUX6_ANDOR
1351
`define MODULE mux6_andor
1352
module `BASE`MODULE ( a5, a4, a3, a2, a1, a0, sel, dout);
1353
`undef MODULE
1354 18 unneback
 
1355
parameter width = 32;
1356 35 unneback
localparam nr_of_ports = 6;
1357 18 unneback
input [width-1:0] a5, a4, a3, a2, a1, a0;
1358
input [nr_of_ports-1:0] sel;
1359 22 unneback
output [width-1:0] dout;
1360 18 unneback
 
1361 40 unneback
`define MODULE mux_andor
1362
`BASE`MODULE
1363 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1364 36 unneback
    mux0( .a({a5,a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
1365 40 unneback
`undef MODULE
1366 18 unneback
 
1367
endmodule
1368 40 unneback
`endif
1369 43 unneback
 
1370
`ifdef PARITY
1371
 
1372
`define MODULE parity_generate
1373
module `BASE`MODULE (data, parity);
1374
`undef MODULE
1375
parameter word_size = 32;
1376
parameter chunk_size = 8;
1377
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
1378
input [word_size-1:0] data;
1379
output reg [word_size/chunk_size-1:0] parity;
1380
integer i,j;
1381
always @ (data)
1382
for (i=0;i<word_size/chunk_size;i=i+1) begin
1383
    parity[i] = parity_type;
1384
    for (j=0;j<chunk_size;j=j+1) begin
1385 46 unneback
        parity[i] = data[i*chunk_size+j] ^ parity[i];
1386 43 unneback
    end
1387
end
1388
endmodule
1389
 
1390
`define MODULE parity_check
1391
module `BASE`MODULE( data, parity, parity_error);
1392
`undef MODULE
1393
parameter word_size = 32;
1394
parameter chunk_size = 8;
1395
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
1396
input [word_size-1:0] data;
1397
input [word_size/chunk_size-1:0] parity;
1398
output parity_error;
1399 44 unneback
reg [word_size/chunk_size-1:0] error_flag;
1400 43 unneback
integer i,j;
1401
always @ (data or parity)
1402
for (i=0;i<word_size/chunk_size;i=i+1) begin
1403
    error_flag[i] = parity[i] ^ parity_type;
1404
    for (j=0;j<chunk_size;j=j+1) begin
1405 46 unneback
        error_flag[i] = data[i*chunk_size+j] ^ error_flag[i];
1406 43 unneback
    end
1407
end
1408
assign parity_error = |error_flag;
1409
endmodule
1410
 
1411 44 unneback
`endif//////////////////////////////////////////////////////////////////////
1412
////                                                              ////
1413
////  IO functions                                                ////
1414
////                                                              ////
1415
////  Description                                                 ////
1416
////  IO functions such as IOB flip-flops                         ////
1417
////                                                              ////
1418
////                                                              ////
1419
////  To Do:                                                      ////
1420
////   -                                                          ////
1421
////                                                              ////
1422
////  Author(s):                                                  ////
1423
////      - Michael Unneback, unneback@opencores.org              ////
1424
////        ORSoC AB                                              ////
1425
////                                                              ////
1426 18 unneback
//////////////////////////////////////////////////////////////////////
1427
////                                                              ////
1428 44 unneback
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1429
////                                                              ////
1430
//// This source file may be used and distributed without         ////
1431
//// restriction provided that this copyright statement is not    ////
1432
//// removed from the file and that any derivative work contains  ////
1433
//// the original copyright notice and the associated disclaimer. ////
1434
////                                                              ////
1435
//// This source file is free software; you can redistribute it   ////
1436
//// and/or modify it under the terms of the GNU Lesser General   ////
1437
//// Public License as published by the Free Software Foundation; ////
1438
//// either version 2.1 of the License, or (at your option) any   ////
1439
//// later version.                                               ////
1440
////                                                              ////
1441
//// This source is distributed in the hope that it will be       ////
1442
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1443
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1444
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1445
//// details.                                                     ////
1446
////                                                              ////
1447
//// You should have received a copy of the GNU Lesser General    ////
1448
//// Public License along with this source; if not, download it   ////
1449
//// from http://www.opencores.org/lgpl.shtml                     ////
1450
////                                                              ////
1451
//////////////////////////////////////////////////////////////////////
1452 45 unneback
`timescale 1ns/1ns
1453 44 unneback
`ifdef O_DFF
1454
`define MODULE o_dff
1455
module `BASE`MODULE (d_i, o_pad, clk, rst);
1456
`undef MODULE
1457
parameter width = 1;
1458 45 unneback
parameter reset_value = {width{1'b0}};
1459
input  [width-1:0]  d_i;
1460 44 unneback
output [width-1:0] o_pad;
1461
input clk, rst;
1462
wire [width-1:0] d_i_int `SYN_KEEP;
1463 45 unneback
reg  [width-1:0] o_pad_int;
1464 44 unneback
assign d_i_int = d_i;
1465
genvar i;
1466 45 unneback
generate
1467 44 unneback
for (i=0;i<width;i=i+1) begin
1468
    always @ (posedge clk or posedge rst)
1469
    if (rst)
1470 45 unneback
        o_pad_int[i] <= reset_value[i];
1471 44 unneback
    else
1472 45 unneback
        o_pad_int[i] <= d_i_int[i];
1473
    assign #1 o_pad[i] = o_pad_int[i];
1474 44 unneback
end
1475
endgenerate
1476
endmodule
1477
`endif
1478
 
1479 45 unneback
`timescale 1ns/1ns
1480 44 unneback
`ifdef IO_DFF_OE
1481
`define MODULE io_dff_oe
1482
module `BASE`MODULE ( d_i, d_o, oe, io_pad, clk, rst);
1483
`undef MODULE
1484
parameter width = 1;
1485
input  [width-1:0] d_o;
1486
output reg [width-1:0] d_i;
1487
input oe;
1488
inout [width-1:0] io_pad;
1489
input clk, rst;
1490
wire [width-1:0] oe_d `SYN_KEEP;
1491
reg [width-1:0] oe_q;
1492
reg [width-1:0] d_o_q;
1493
assign oe_d = {width{oe}};
1494
genvar i;
1495
generate
1496
for (i=0;i<width;i=i+1) begin
1497
    always @ (posedge clk or posedge rst)
1498
    if (rst)
1499
        oe_q[i] <= 1'b0;
1500
    else
1501
        oe_q[i] <= oe_d[i];
1502
    always @ (posedge clk or posedge rst)
1503
    if (rst)
1504
        d_o_q[i] <= 1'b0;
1505
    else
1506
        d_o_q[i] <= d_o[i];
1507
    always @ (posedge clk or posedge rst)
1508
    if (rst)
1509
        d_i[i] <= 1'b0;
1510
    else
1511
        d_i[i] <= io_pad[i];
1512 45 unneback
    assign #1 io_pad[i] = (oe_q[i]) ? d_o_q[i] : 1'bz;
1513 44 unneback
end
1514
endgenerate
1515
endmodule
1516
`endif
1517
`ifdef CNT_BIN
1518
//////////////////////////////////////////////////////////////////////
1519
////                                                              ////
1520 6 unneback
////  Versatile counter                                           ////
1521
////                                                              ////
1522
////  Description                                                 ////
1523
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1524
////  counter                                                     ////
1525
////                                                              ////
1526
////  To Do:                                                      ////
1527
////   - add LFSR with more taps                                  ////
1528
////                                                              ////
1529
////  Author(s):                                                  ////
1530
////      - Michael Unneback, unneback@opencores.org              ////
1531
////        ORSoC AB                                              ////
1532
////                                                              ////
1533
//////////////////////////////////////////////////////////////////////
1534
////                                                              ////
1535
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1536
////                                                              ////
1537
//// This source file may be used and distributed without         ////
1538
//// restriction provided that this copyright statement is not    ////
1539
//// removed from the file and that any derivative work contains  ////
1540
//// the original copyright notice and the associated disclaimer. ////
1541
////                                                              ////
1542
//// This source file is free software; you can redistribute it   ////
1543
//// and/or modify it under the terms of the GNU Lesser General   ////
1544
//// Public License as published by the Free Software Foundation; ////
1545
//// either version 2.1 of the License, or (at your option) any   ////
1546
//// later version.                                               ////
1547
////                                                              ////
1548
//// This source is distributed in the hope that it will be       ////
1549
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1550
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1551
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1552
//// details.                                                     ////
1553
////                                                              ////
1554
//// You should have received a copy of the GNU Lesser General    ////
1555
//// Public License along with this source; if not, download it   ////
1556
//// from http://www.opencores.org/lgpl.shtml                     ////
1557
////                                                              ////
1558
//////////////////////////////////////////////////////////////////////
1559
 
1560
// binary counter
1561 22 unneback
 
1562 40 unneback
`define MODULE cnt_bin
1563
module `BASE`MODULE (
1564
`undef MODULE
1565
 q, rst, clk);
1566
 
1567 22 unneback
   parameter length = 4;
1568
   output [length:1] q;
1569
   input rst;
1570
   input clk;
1571
 
1572
   parameter clear_value = 0;
1573
   parameter set_value = 1;
1574
   parameter wrap_value = 0;
1575
   parameter level1_value = 15;
1576
 
1577
   reg  [length:1] qi;
1578
   wire [length:1] q_next;
1579
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1580
 
1581
   always @ (posedge clk or posedge rst)
1582
     if (rst)
1583
       qi <= {length{1'b0}};
1584
     else
1585
       qi <= q_next;
1586
 
1587
   assign q = qi;
1588
 
1589
endmodule
1590 40 unneback
`endif
1591
`ifdef CNT_BIN_CLEAR
1592 22 unneback
//////////////////////////////////////////////////////////////////////
1593
////                                                              ////
1594
////  Versatile counter                                           ////
1595
////                                                              ////
1596
////  Description                                                 ////
1597
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1598
////  counter                                                     ////
1599
////                                                              ////
1600
////  To Do:                                                      ////
1601
////   - add LFSR with more taps                                  ////
1602
////                                                              ////
1603
////  Author(s):                                                  ////
1604
////      - Michael Unneback, unneback@opencores.org              ////
1605
////        ORSoC AB                                              ////
1606
////                                                              ////
1607
//////////////////////////////////////////////////////////////////////
1608
////                                                              ////
1609
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1610
////                                                              ////
1611
//// This source file may be used and distributed without         ////
1612
//// restriction provided that this copyright statement is not    ////
1613
//// removed from the file and that any derivative work contains  ////
1614
//// the original copyright notice and the associated disclaimer. ////
1615
////                                                              ////
1616
//// This source file is free software; you can redistribute it   ////
1617
//// and/or modify it under the terms of the GNU Lesser General   ////
1618
//// Public License as published by the Free Software Foundation; ////
1619
//// either version 2.1 of the License, or (at your option) any   ////
1620
//// later version.                                               ////
1621
////                                                              ////
1622
//// This source is distributed in the hope that it will be       ////
1623
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1624
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1625
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1626
//// details.                                                     ////
1627
////                                                              ////
1628
//// You should have received a copy of the GNU Lesser General    ////
1629
//// Public License along with this source; if not, download it   ////
1630
//// from http://www.opencores.org/lgpl.shtml                     ////
1631
////                                                              ////
1632
//////////////////////////////////////////////////////////////////////
1633
 
1634
// binary counter
1635
 
1636 40 unneback
`define MODULE cnt_bin_clear
1637
module `BASE`MODULE (
1638
`undef MODULE
1639
 clear, q, rst, clk);
1640
 
1641 22 unneback
   parameter length = 4;
1642
   input clear;
1643
   output [length:1] q;
1644
   input rst;
1645
   input clk;
1646
 
1647
   parameter clear_value = 0;
1648
   parameter set_value = 1;
1649
   parameter wrap_value = 0;
1650
   parameter level1_value = 15;
1651
 
1652
   reg  [length:1] qi;
1653
   wire [length:1] q_next;
1654
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1655
 
1656
   always @ (posedge clk or posedge rst)
1657
     if (rst)
1658
       qi <= {length{1'b0}};
1659
     else
1660
       qi <= q_next;
1661
 
1662
   assign q = qi;
1663
 
1664
endmodule
1665 40 unneback
`endif
1666
`ifdef CNT_BIN_CE
1667 22 unneback
//////////////////////////////////////////////////////////////////////
1668
////                                                              ////
1669
////  Versatile counter                                           ////
1670
////                                                              ////
1671
////  Description                                                 ////
1672
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1673
////  counter                                                     ////
1674
////                                                              ////
1675
////  To Do:                                                      ////
1676
////   - add LFSR with more taps                                  ////
1677
////                                                              ////
1678
////  Author(s):                                                  ////
1679
////      - Michael Unneback, unneback@opencores.org              ////
1680
////        ORSoC AB                                              ////
1681
////                                                              ////
1682
//////////////////////////////////////////////////////////////////////
1683
////                                                              ////
1684
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1685
////                                                              ////
1686
//// This source file may be used and distributed without         ////
1687
//// restriction provided that this copyright statement is not    ////
1688
//// removed from the file and that any derivative work contains  ////
1689
//// the original copyright notice and the associated disclaimer. ////
1690
////                                                              ////
1691
//// This source file is free software; you can redistribute it   ////
1692
//// and/or modify it under the terms of the GNU Lesser General   ////
1693
//// Public License as published by the Free Software Foundation; ////
1694
//// either version 2.1 of the License, or (at your option) any   ////
1695
//// later version.                                               ////
1696
////                                                              ////
1697
//// This source is distributed in the hope that it will be       ////
1698
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1699
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1700
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1701
//// details.                                                     ////
1702
////                                                              ////
1703
//// You should have received a copy of the GNU Lesser General    ////
1704
//// Public License along with this source; if not, download it   ////
1705
//// from http://www.opencores.org/lgpl.shtml                     ////
1706
////                                                              ////
1707
//////////////////////////////////////////////////////////////////////
1708
 
1709
// binary counter
1710 6 unneback
 
1711 40 unneback
`define MODULE cnt_bin_ce
1712
module `BASE`MODULE (
1713
`undef MODULE
1714
 cke, q, rst, clk);
1715
 
1716 6 unneback
   parameter length = 4;
1717
   input cke;
1718
   output [length:1] q;
1719
   input rst;
1720
   input clk;
1721
 
1722
   parameter clear_value = 0;
1723
   parameter set_value = 1;
1724
   parameter wrap_value = 0;
1725
   parameter level1_value = 15;
1726
 
1727
   reg  [length:1] qi;
1728
   wire [length:1] q_next;
1729
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1730
 
1731
   always @ (posedge clk or posedge rst)
1732
     if (rst)
1733
       qi <= {length{1'b0}};
1734
     else
1735
     if (cke)
1736
       qi <= q_next;
1737
 
1738
   assign q = qi;
1739
 
1740
endmodule
1741 40 unneback
`endif
1742
`ifdef CNT_BIN_CE_CLEAR
1743 6 unneback
//////////////////////////////////////////////////////////////////////
1744
////                                                              ////
1745
////  Versatile counter                                           ////
1746
////                                                              ////
1747
////  Description                                                 ////
1748
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1749
////  counter                                                     ////
1750
////                                                              ////
1751
////  To Do:                                                      ////
1752
////   - add LFSR with more taps                                  ////
1753
////                                                              ////
1754
////  Author(s):                                                  ////
1755
////      - Michael Unneback, unneback@opencores.org              ////
1756
////        ORSoC AB                                              ////
1757
////                                                              ////
1758
//////////////////////////////////////////////////////////////////////
1759
////                                                              ////
1760
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1761
////                                                              ////
1762
//// This source file may be used and distributed without         ////
1763
//// restriction provided that this copyright statement is not    ////
1764
//// removed from the file and that any derivative work contains  ////
1765
//// the original copyright notice and the associated disclaimer. ////
1766
////                                                              ////
1767
//// This source file is free software; you can redistribute it   ////
1768
//// and/or modify it under the terms of the GNU Lesser General   ////
1769
//// Public License as published by the Free Software Foundation; ////
1770
//// either version 2.1 of the License, or (at your option) any   ////
1771
//// later version.                                               ////
1772
////                                                              ////
1773
//// This source is distributed in the hope that it will be       ////
1774
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1775
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1776
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1777
//// details.                                                     ////
1778
////                                                              ////
1779
//// You should have received a copy of the GNU Lesser General    ////
1780
//// Public License along with this source; if not, download it   ////
1781
//// from http://www.opencores.org/lgpl.shtml                     ////
1782
////                                                              ////
1783
//////////////////////////////////////////////////////////////////////
1784
 
1785
// binary counter
1786
 
1787 40 unneback
`define MODULE cnt_bin_ce_clear
1788
module `BASE`MODULE (
1789
`undef MODULE
1790
 clear, cke, q, rst, clk);
1791
 
1792 6 unneback
   parameter length = 4;
1793
   input clear;
1794
   input cke;
1795
   output [length:1] q;
1796
   input rst;
1797
   input clk;
1798
 
1799
   parameter clear_value = 0;
1800
   parameter set_value = 1;
1801
   parameter wrap_value = 0;
1802
   parameter level1_value = 15;
1803
 
1804
   reg  [length:1] qi;
1805
   wire [length:1] q_next;
1806
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1807
 
1808
   always @ (posedge clk or posedge rst)
1809
     if (rst)
1810
       qi <= {length{1'b0}};
1811
     else
1812
     if (cke)
1813
       qi <= q_next;
1814
 
1815
   assign q = qi;
1816
 
1817
endmodule
1818 40 unneback
`endif
1819
`ifdef CNT_BIN_CE_CLEAR_L1_L2
1820 6 unneback
//////////////////////////////////////////////////////////////////////
1821
////                                                              ////
1822
////  Versatile counter                                           ////
1823
////                                                              ////
1824
////  Description                                                 ////
1825
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1826
////  counter                                                     ////
1827
////                                                              ////
1828
////  To Do:                                                      ////
1829
////   - add LFSR with more taps                                  ////
1830
////                                                              ////
1831
////  Author(s):                                                  ////
1832
////      - Michael Unneback, unneback@opencores.org              ////
1833
////        ORSoC AB                                              ////
1834
////                                                              ////
1835
//////////////////////////////////////////////////////////////////////
1836
////                                                              ////
1837
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1838
////                                                              ////
1839
//// This source file may be used and distributed without         ////
1840
//// restriction provided that this copyright statement is not    ////
1841
//// removed from the file and that any derivative work contains  ////
1842
//// the original copyright notice and the associated disclaimer. ////
1843
////                                                              ////
1844
//// This source file is free software; you can redistribute it   ////
1845
//// and/or modify it under the terms of the GNU Lesser General   ////
1846
//// Public License as published by the Free Software Foundation; ////
1847
//// either version 2.1 of the License, or (at your option) any   ////
1848
//// later version.                                               ////
1849
////                                                              ////
1850
//// This source is distributed in the hope that it will be       ////
1851
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1852
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1853
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1854
//// details.                                                     ////
1855
////                                                              ////
1856
//// You should have received a copy of the GNU Lesser General    ////
1857
//// Public License along with this source; if not, download it   ////
1858
//// from http://www.opencores.org/lgpl.shtml                     ////
1859
////                                                              ////
1860
//////////////////////////////////////////////////////////////////////
1861
 
1862
// binary counter
1863 29 unneback
 
1864 40 unneback
`define MODULE cnt_bin_ce_clear_l1_l2
1865
module `BASE`MODULE (
1866
`undef MODULE
1867
 clear, cke, q, level1, level2, rst, clk);
1868
 
1869 29 unneback
   parameter length = 4;
1870
   input clear;
1871
   input cke;
1872
   output [length:1] q;
1873
   output reg level1;
1874
   output reg level2;
1875
   input rst;
1876
   input clk;
1877
 
1878
   parameter clear_value = 0;
1879
   parameter set_value = 1;
1880 30 unneback
   parameter wrap_value = 15;
1881
   parameter level1_value = 8;
1882
   parameter level2_value = 15;
1883 29 unneback
 
1884
   wire rew;
1885 30 unneback
   assign rew = 1'b0;
1886 29 unneback
   reg  [length:1] qi;
1887
   wire [length:1] q_next;
1888
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1889
 
1890
   always @ (posedge clk or posedge rst)
1891
     if (rst)
1892
       qi <= {length{1'b0}};
1893
     else
1894
     if (cke)
1895
       qi <= q_next;
1896
 
1897
   assign q = qi;
1898
 
1899
 
1900
    always @ (posedge clk or posedge rst)
1901
    if (rst)
1902
        level1 <= 1'b0;
1903
    else
1904
    if (cke)
1905
    if (clear)
1906
        level1 <= 1'b0;
1907
    else if (q_next == level1_value)
1908
        level1 <= 1'b1;
1909
    else if (qi == level1_value & rew)
1910
        level1 <= 1'b0;
1911
 
1912
    always @ (posedge clk or posedge rst)
1913
    if (rst)
1914
        level2 <= 1'b0;
1915
    else
1916
    if (cke)
1917
    if (clear)
1918
        level2 <= 1'b0;
1919
    else if (q_next == level2_value)
1920
        level2 <= 1'b1;
1921
    else if (qi == level2_value & rew)
1922
        level2 <= 1'b0;
1923
endmodule
1924 40 unneback
`endif
1925
`ifdef CNT_BIN_CE_CLEAR_SET_REW
1926 29 unneback
//////////////////////////////////////////////////////////////////////
1927
////                                                              ////
1928
////  Versatile counter                                           ////
1929
////                                                              ////
1930
////  Description                                                 ////
1931
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1932
////  counter                                                     ////
1933
////                                                              ////
1934
////  To Do:                                                      ////
1935
////   - add LFSR with more taps                                  ////
1936
////                                                              ////
1937
////  Author(s):                                                  ////
1938
////      - Michael Unneback, unneback@opencores.org              ////
1939
////        ORSoC AB                                              ////
1940
////                                                              ////
1941
//////////////////////////////////////////////////////////////////////
1942
////                                                              ////
1943
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1944
////                                                              ////
1945
//// This source file may be used and distributed without         ////
1946
//// restriction provided that this copyright statement is not    ////
1947
//// removed from the file and that any derivative work contains  ////
1948
//// the original copyright notice and the associated disclaimer. ////
1949
////                                                              ////
1950
//// This source file is free software; you can redistribute it   ////
1951
//// and/or modify it under the terms of the GNU Lesser General   ////
1952
//// Public License as published by the Free Software Foundation; ////
1953
//// either version 2.1 of the License, or (at your option) any   ////
1954
//// later version.                                               ////
1955
////                                                              ////
1956
//// This source is distributed in the hope that it will be       ////
1957
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1958
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1959
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1960
//// details.                                                     ////
1961
////                                                              ////
1962
//// You should have received a copy of the GNU Lesser General    ////
1963
//// Public License along with this source; if not, download it   ////
1964
//// from http://www.opencores.org/lgpl.shtml                     ////
1965
////                                                              ////
1966
//////////////////////////////////////////////////////////////////////
1967
 
1968
// binary counter
1969 6 unneback
 
1970 40 unneback
`define MODULE cnt_bin_ce_clear_set_rew
1971
module `BASE`MODULE (
1972
`undef MODULE
1973
 clear, set, cke, rew, q, rst, clk);
1974
 
1975 6 unneback
   parameter length = 4;
1976
   input clear;
1977
   input set;
1978
   input cke;
1979
   input rew;
1980
   output [length:1] q;
1981
   input rst;
1982
   input clk;
1983
 
1984
   parameter clear_value = 0;
1985
   parameter set_value = 1;
1986
   parameter wrap_value = 0;
1987
   parameter level1_value = 15;
1988
 
1989
   reg  [length:1] qi;
1990
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1991
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
1992
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
1993
   assign q_next = rew ? q_next_rew : q_next_fw;
1994
 
1995
   always @ (posedge clk or posedge rst)
1996
     if (rst)
1997
       qi <= {length{1'b0}};
1998
     else
1999
     if (cke)
2000
       qi <= q_next;
2001
 
2002
   assign q = qi;
2003
 
2004
endmodule
2005 40 unneback
`endif
2006
`ifdef CNT_BIN_CE_REW_L1
2007 6 unneback
//////////////////////////////////////////////////////////////////////
2008
////                                                              ////
2009
////  Versatile counter                                           ////
2010
////                                                              ////
2011
////  Description                                                 ////
2012
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2013
////  counter                                                     ////
2014
////                                                              ////
2015
////  To Do:                                                      ////
2016
////   - add LFSR with more taps                                  ////
2017
////                                                              ////
2018
////  Author(s):                                                  ////
2019
////      - Michael Unneback, unneback@opencores.org              ////
2020
////        ORSoC AB                                              ////
2021
////                                                              ////
2022
//////////////////////////////////////////////////////////////////////
2023
////                                                              ////
2024
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2025
////                                                              ////
2026
//// This source file may be used and distributed without         ////
2027
//// restriction provided that this copyright statement is not    ////
2028
//// removed from the file and that any derivative work contains  ////
2029
//// the original copyright notice and the associated disclaimer. ////
2030
////                                                              ////
2031
//// This source file is free software; you can redistribute it   ////
2032
//// and/or modify it under the terms of the GNU Lesser General   ////
2033
//// Public License as published by the Free Software Foundation; ////
2034
//// either version 2.1 of the License, or (at your option) any   ////
2035
//// later version.                                               ////
2036
////                                                              ////
2037
//// This source is distributed in the hope that it will be       ////
2038
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2039
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2040
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2041
//// details.                                                     ////
2042
////                                                              ////
2043
//// You should have received a copy of the GNU Lesser General    ////
2044
//// Public License along with this source; if not, download it   ////
2045
//// from http://www.opencores.org/lgpl.shtml                     ////
2046
////                                                              ////
2047
//////////////////////////////////////////////////////////////////////
2048
 
2049
// binary counter
2050
 
2051 40 unneback
`define MODULE cnt_bin_ce_rew_l1
2052
module `BASE`MODULE (
2053
`undef MODULE
2054
 cke, rew, level1, rst, clk);
2055
 
2056 6 unneback
   parameter length = 4;
2057
   input cke;
2058
   input rew;
2059
   output reg level1;
2060
   input rst;
2061
   input clk;
2062
 
2063
   parameter clear_value = 0;
2064
   parameter set_value = 1;
2065
   parameter wrap_value = 1;
2066
   parameter level1_value = 15;
2067
 
2068 29 unneback
   wire clear;
2069 30 unneback
   assign clear = 1'b0;
2070 6 unneback
   reg  [length:1] qi;
2071
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2072
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2073
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2074
   assign q_next = rew ? q_next_rew : q_next_fw;
2075
 
2076
   always @ (posedge clk or posedge rst)
2077
     if (rst)
2078
       qi <= {length{1'b0}};
2079
     else
2080
     if (cke)
2081
       qi <= q_next;
2082
 
2083
 
2084
 
2085
    always @ (posedge clk or posedge rst)
2086
    if (rst)
2087
        level1 <= 1'b0;
2088
    else
2089
    if (cke)
2090 29 unneback
    if (clear)
2091
        level1 <= 1'b0;
2092
    else if (q_next == level1_value)
2093 6 unneback
        level1 <= 1'b1;
2094
    else if (qi == level1_value & rew)
2095
        level1 <= 1'b0;
2096
endmodule
2097 40 unneback
`endif
2098
`ifdef CNT_BIN_CE_REW_ZQ_L1
2099 6 unneback
//////////////////////////////////////////////////////////////////////
2100
////                                                              ////
2101
////  Versatile counter                                           ////
2102
////                                                              ////
2103
////  Description                                                 ////
2104
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2105
////  counter                                                     ////
2106
////                                                              ////
2107
////  To Do:                                                      ////
2108
////   - add LFSR with more taps                                  ////
2109
////                                                              ////
2110
////  Author(s):                                                  ////
2111
////      - Michael Unneback, unneback@opencores.org              ////
2112
////        ORSoC AB                                              ////
2113
////                                                              ////
2114
//////////////////////////////////////////////////////////////////////
2115
////                                                              ////
2116
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2117
////                                                              ////
2118
//// This source file may be used and distributed without         ////
2119
//// restriction provided that this copyright statement is not    ////
2120
//// removed from the file and that any derivative work contains  ////
2121
//// the original copyright notice and the associated disclaimer. ////
2122
////                                                              ////
2123
//// This source file is free software; you can redistribute it   ////
2124
//// and/or modify it under the terms of the GNU Lesser General   ////
2125
//// Public License as published by the Free Software Foundation; ////
2126
//// either version 2.1 of the License, or (at your option) any   ////
2127
//// later version.                                               ////
2128
////                                                              ////
2129
//// This source is distributed in the hope that it will be       ////
2130
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2131
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2132
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2133
//// details.                                                     ////
2134
////                                                              ////
2135
//// You should have received a copy of the GNU Lesser General    ////
2136
//// Public License along with this source; if not, download it   ////
2137
//// from http://www.opencores.org/lgpl.shtml                     ////
2138
////                                                              ////
2139
//////////////////////////////////////////////////////////////////////
2140
 
2141 25 unneback
// binary counter
2142
 
2143 40 unneback
`define MODULE cnt_bin_ce_rew_zq_l1
2144
module `BASE`MODULE (
2145
`undef MODULE
2146
 cke, rew, zq, level1, rst, clk);
2147
 
2148 25 unneback
   parameter length = 4;
2149
   input cke;
2150
   input rew;
2151
   output reg zq;
2152
   output reg level1;
2153
   input rst;
2154
   input clk;
2155
 
2156
   parameter clear_value = 0;
2157
   parameter set_value = 1;
2158
   parameter wrap_value = 1;
2159
   parameter level1_value = 15;
2160
 
2161 29 unneback
   wire clear;
2162 30 unneback
   assign clear = 1'b0;
2163 25 unneback
   reg  [length:1] qi;
2164
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2165
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2166
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2167
   assign q_next = rew ? q_next_rew : q_next_fw;
2168
 
2169
   always @ (posedge clk or posedge rst)
2170
     if (rst)
2171
       qi <= {length{1'b0}};
2172
     else
2173
     if (cke)
2174
       qi <= q_next;
2175
 
2176
 
2177
 
2178
   always @ (posedge clk or posedge rst)
2179
     if (rst)
2180
       zq <= 1'b1;
2181
     else
2182
     if (cke)
2183
       zq <= q_next == {length{1'b0}};
2184
 
2185
    always @ (posedge clk or posedge rst)
2186
    if (rst)
2187
        level1 <= 1'b0;
2188
    else
2189
    if (cke)
2190 29 unneback
    if (clear)
2191
        level1 <= 1'b0;
2192
    else if (q_next == level1_value)
2193 25 unneback
        level1 <= 1'b1;
2194
    else if (qi == level1_value & rew)
2195
        level1 <= 1'b0;
2196
endmodule
2197 40 unneback
`endif
2198
`ifdef CNT_BIN_CE_REW_Q_ZQ_L1
2199 25 unneback
//////////////////////////////////////////////////////////////////////
2200
////                                                              ////
2201
////  Versatile counter                                           ////
2202
////                                                              ////
2203
////  Description                                                 ////
2204
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2205
////  counter                                                     ////
2206
////                                                              ////
2207
////  To Do:                                                      ////
2208
////   - add LFSR with more taps                                  ////
2209
////                                                              ////
2210
////  Author(s):                                                  ////
2211
////      - Michael Unneback, unneback@opencores.org              ////
2212
////        ORSoC AB                                              ////
2213
////                                                              ////
2214
//////////////////////////////////////////////////////////////////////
2215
////                                                              ////
2216
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2217
////                                                              ////
2218
//// This source file may be used and distributed without         ////
2219
//// restriction provided that this copyright statement is not    ////
2220
//// removed from the file and that any derivative work contains  ////
2221
//// the original copyright notice and the associated disclaimer. ////
2222
////                                                              ////
2223
//// This source file is free software; you can redistribute it   ////
2224
//// and/or modify it under the terms of the GNU Lesser General   ////
2225
//// Public License as published by the Free Software Foundation; ////
2226
//// either version 2.1 of the License, or (at your option) any   ////
2227
//// later version.                                               ////
2228
////                                                              ////
2229
//// This source is distributed in the hope that it will be       ////
2230
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2231
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2232
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2233
//// details.                                                     ////
2234
////                                                              ////
2235
//// You should have received a copy of the GNU Lesser General    ////
2236
//// Public License along with this source; if not, download it   ////
2237
//// from http://www.opencores.org/lgpl.shtml                     ////
2238
////                                                              ////
2239
//////////////////////////////////////////////////////////////////////
2240
 
2241
// binary counter
2242
 
2243 40 unneback
`define MODULE cnt_bin_ce_rew_q_zq_l1
2244
module `BASE`MODULE (
2245
`undef MODULE
2246
 cke, rew, q, zq, level1, rst, clk);
2247
 
2248 25 unneback
   parameter length = 4;
2249
   input cke;
2250
   input rew;
2251
   output [length:1] q;
2252
   output reg zq;
2253
   output reg level1;
2254
   input rst;
2255
   input clk;
2256
 
2257
   parameter clear_value = 0;
2258
   parameter set_value = 1;
2259
   parameter wrap_value = 1;
2260
   parameter level1_value = 15;
2261
 
2262 29 unneback
   wire clear;
2263 30 unneback
   assign clear = 1'b0;
2264 25 unneback
   reg  [length:1] qi;
2265
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2266
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2267
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2268
   assign q_next = rew ? q_next_rew : q_next_fw;
2269
 
2270
   always @ (posedge clk or posedge rst)
2271
     if (rst)
2272
       qi <= {length{1'b0}};
2273
     else
2274
     if (cke)
2275
       qi <= q_next;
2276
 
2277
   assign q = qi;
2278
 
2279
 
2280
   always @ (posedge clk or posedge rst)
2281
     if (rst)
2282
       zq <= 1'b1;
2283
     else
2284
     if (cke)
2285
       zq <= q_next == {length{1'b0}};
2286
 
2287
    always @ (posedge clk or posedge rst)
2288
    if (rst)
2289
        level1 <= 1'b0;
2290
    else
2291
    if (cke)
2292 29 unneback
    if (clear)
2293
        level1 <= 1'b0;
2294
    else if (q_next == level1_value)
2295 25 unneback
        level1 <= 1'b1;
2296
    else if (qi == level1_value & rew)
2297
        level1 <= 1'b0;
2298
endmodule
2299 40 unneback
`endif
2300
`ifdef CNT_LFSR_ZQ
2301 25 unneback
//////////////////////////////////////////////////////////////////////
2302
////                                                              ////
2303
////  Versatile counter                                           ////
2304
////                                                              ////
2305
////  Description                                                 ////
2306
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2307
////  counter                                                     ////
2308
////                                                              ////
2309
////  To Do:                                                      ////
2310
////   - add LFSR with more taps                                  ////
2311
////                                                              ////
2312
////  Author(s):                                                  ////
2313
////      - Michael Unneback, unneback@opencores.org              ////
2314
////        ORSoC AB                                              ////
2315
////                                                              ////
2316
//////////////////////////////////////////////////////////////////////
2317
////                                                              ////
2318
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2319
////                                                              ////
2320
//// This source file may be used and distributed without         ////
2321
//// restriction provided that this copyright statement is not    ////
2322
//// removed from the file and that any derivative work contains  ////
2323
//// the original copyright notice and the associated disclaimer. ////
2324
////                                                              ////
2325
//// This source file is free software; you can redistribute it   ////
2326
//// and/or modify it under the terms of the GNU Lesser General   ////
2327
//// Public License as published by the Free Software Foundation; ////
2328
//// either version 2.1 of the License, or (at your option) any   ////
2329
//// later version.                                               ////
2330
////                                                              ////
2331
//// This source is distributed in the hope that it will be       ////
2332
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2333
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2334
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2335
//// details.                                                     ////
2336
////                                                              ////
2337
//// You should have received a copy of the GNU Lesser General    ////
2338
//// Public License along with this source; if not, download it   ////
2339
//// from http://www.opencores.org/lgpl.shtml                     ////
2340
////                                                              ////
2341
//////////////////////////////////////////////////////////////////////
2342
 
2343 6 unneback
// LFSR counter
2344
 
2345 40 unneback
`define MODULE cnt_lfsr_zq
2346
module `BASE`MODULE (
2347
`undef MODULE
2348
 zq, rst, clk);
2349
 
2350 6 unneback
   parameter length = 4;
2351
   output reg zq;
2352
   input rst;
2353
   input clk;
2354
 
2355
   parameter clear_value = 0;
2356
   parameter set_value = 1;
2357
   parameter wrap_value = 8;
2358
   parameter level1_value = 15;
2359
 
2360
   reg  [length:1] qi;
2361
   reg lfsr_fb;
2362
   wire [length:1] q_next;
2363
   reg [32:1] polynom;
2364
   integer i;
2365
 
2366
   always @ (qi)
2367
   begin
2368
        case (length)
2369
         2: polynom = 32'b11;                               // 0x3
2370
         3: polynom = 32'b110;                              // 0x6
2371
         4: polynom = 32'b1100;                             // 0xC
2372
         5: polynom = 32'b10100;                            // 0x14
2373
         6: polynom = 32'b110000;                           // 0x30
2374
         7: polynom = 32'b1100000;                          // 0x60
2375
         8: polynom = 32'b10111000;                         // 0xb8
2376
         9: polynom = 32'b100010000;                        // 0x110
2377
        10: polynom = 32'b1001000000;                       // 0x240
2378
        11: polynom = 32'b10100000000;                      // 0x500
2379
        12: polynom = 32'b100000101001;                     // 0x829
2380
        13: polynom = 32'b1000000001100;                    // 0x100C
2381
        14: polynom = 32'b10000000010101;                   // 0x2015
2382
        15: polynom = 32'b110000000000000;                  // 0x6000
2383
        16: polynom = 32'b1101000000001000;                 // 0xD008
2384
        17: polynom = 32'b10010000000000000;                // 0x12000
2385
        18: polynom = 32'b100000010000000000;               // 0x20400
2386
        19: polynom = 32'b1000000000000100011;              // 0x40023
2387 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2388 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2389
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2390
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2391
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2392
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2393
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2394
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2395
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2396
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2397
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2398
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2399
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2400
        default: polynom = 32'b0;
2401
        endcase
2402
        lfsr_fb = qi[length];
2403
        for (i=length-1; i>=1; i=i-1) begin
2404
            if (polynom[i])
2405
                lfsr_fb = lfsr_fb  ~^ qi[i];
2406
        end
2407
    end
2408
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2409
 
2410
   always @ (posedge clk or posedge rst)
2411
     if (rst)
2412
       qi <= {length{1'b0}};
2413
     else
2414
       qi <= q_next;
2415
 
2416
 
2417
 
2418
   always @ (posedge clk or posedge rst)
2419
     if (rst)
2420
       zq <= 1'b1;
2421
     else
2422
       zq <= q_next == {length{1'b0}};
2423
endmodule
2424 40 unneback
`endif
2425 75 unneback
`ifdef CNT_LFSR_CE
2426
//////////////////////////////////////////////////////////////////////
2427
////                                                              ////
2428
////  Versatile counter                                           ////
2429
////                                                              ////
2430
////  Description                                                 ////
2431
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2432
////  counter                                                     ////
2433
////                                                              ////
2434
////  To Do:                                                      ////
2435
////   - add LFSR with more taps                                  ////
2436
////                                                              ////
2437
////  Author(s):                                                  ////
2438
////      - Michael Unneback, unneback@opencores.org              ////
2439
////        ORSoC AB                                              ////
2440
////                                                              ////
2441
//////////////////////////////////////////////////////////////////////
2442
////                                                              ////
2443
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2444
////                                                              ////
2445
//// This source file may be used and distributed without         ////
2446
//// restriction provided that this copyright statement is not    ////
2447
//// removed from the file and that any derivative work contains  ////
2448
//// the original copyright notice and the associated disclaimer. ////
2449
////                                                              ////
2450
//// This source file is free software; you can redistribute it   ////
2451
//// and/or modify it under the terms of the GNU Lesser General   ////
2452
//// Public License as published by the Free Software Foundation; ////
2453
//// either version 2.1 of the License, or (at your option) any   ////
2454
//// later version.                                               ////
2455
////                                                              ////
2456
//// This source is distributed in the hope that it will be       ////
2457
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2458
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2459
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2460
//// details.                                                     ////
2461
////                                                              ////
2462
//// You should have received a copy of the GNU Lesser General    ////
2463
//// Public License along with this source; if not, download it   ////
2464
//// from http://www.opencores.org/lgpl.shtml                     ////
2465
////                                                              ////
2466
//////////////////////////////////////////////////////////////////////
2467
 
2468
// LFSR counter
2469
 
2470
`define MODULE cnt_lfsr_ce
2471
module `BASE`MODULE (
2472
`undef MODULE
2473
 cke, zq, rst, clk);
2474
 
2475
   parameter length = 4;
2476
   input cke;
2477
   output reg zq;
2478
   input rst;
2479
   input clk;
2480
 
2481
   parameter clear_value = 0;
2482
   parameter set_value = 1;
2483
   parameter wrap_value = 0;
2484
   parameter level1_value = 15;
2485
 
2486
   reg  [length:1] qi;
2487
   reg lfsr_fb;
2488
   wire [length:1] q_next;
2489
   reg [32:1] polynom;
2490
   integer i;
2491
 
2492
   always @ (qi)
2493
   begin
2494
        case (length)
2495
         2: polynom = 32'b11;                               // 0x3
2496
         3: polynom = 32'b110;                              // 0x6
2497
         4: polynom = 32'b1100;                             // 0xC
2498
         5: polynom = 32'b10100;                            // 0x14
2499
         6: polynom = 32'b110000;                           // 0x30
2500
         7: polynom = 32'b1100000;                          // 0x60
2501
         8: polynom = 32'b10111000;                         // 0xb8
2502
         9: polynom = 32'b100010000;                        // 0x110
2503
        10: polynom = 32'b1001000000;                       // 0x240
2504
        11: polynom = 32'b10100000000;                      // 0x500
2505
        12: polynom = 32'b100000101001;                     // 0x829
2506
        13: polynom = 32'b1000000001100;                    // 0x100C
2507
        14: polynom = 32'b10000000010101;                   // 0x2015
2508
        15: polynom = 32'b110000000000000;                  // 0x6000
2509
        16: polynom = 32'b1101000000001000;                 // 0xD008
2510
        17: polynom = 32'b10010000000000000;                // 0x12000
2511
        18: polynom = 32'b100000010000000000;               // 0x20400
2512
        19: polynom = 32'b1000000000000100011;              // 0x40023
2513
        20: polynom = 32'b10010000000000000000;             // 0x90000
2514
        21: polynom = 32'b101000000000000000000;            // 0x140000
2515
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2516
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2517
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2518
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2519
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2520
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2521
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2522
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2523
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2524
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2525
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2526
        default: polynom = 32'b0;
2527
        endcase
2528
        lfsr_fb = qi[length];
2529
        for (i=length-1; i>=1; i=i-1) begin
2530
            if (polynom[i])
2531
                lfsr_fb = lfsr_fb  ~^ qi[i];
2532
        end
2533
    end
2534
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2535
 
2536
   always @ (posedge clk or posedge rst)
2537
     if (rst)
2538
       qi <= {length{1'b0}};
2539
     else
2540
     if (cke)
2541
       qi <= q_next;
2542
 
2543
 
2544
 
2545
   always @ (posedge clk or posedge rst)
2546
     if (rst)
2547
       zq <= 1'b1;
2548
     else
2549
     if (cke)
2550
       zq <= q_next == {length{1'b0}};
2551
endmodule
2552
`endif
2553 40 unneback
`ifdef CNT_LFSR_CE_ZQ
2554 6 unneback
//////////////////////////////////////////////////////////////////////
2555
////                                                              ////
2556
////  Versatile counter                                           ////
2557
////                                                              ////
2558
////  Description                                                 ////
2559
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2560
////  counter                                                     ////
2561
////                                                              ////
2562
////  To Do:                                                      ////
2563
////   - add LFSR with more taps                                  ////
2564
////                                                              ////
2565
////  Author(s):                                                  ////
2566
////      - Michael Unneback, unneback@opencores.org              ////
2567
////        ORSoC AB                                              ////
2568
////                                                              ////
2569
//////////////////////////////////////////////////////////////////////
2570
////                                                              ////
2571
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2572
////                                                              ////
2573
//// This source file may be used and distributed without         ////
2574
//// restriction provided that this copyright statement is not    ////
2575
//// removed from the file and that any derivative work contains  ////
2576
//// the original copyright notice and the associated disclaimer. ////
2577
////                                                              ////
2578
//// This source file is free software; you can redistribute it   ////
2579
//// and/or modify it under the terms of the GNU Lesser General   ////
2580
//// Public License as published by the Free Software Foundation; ////
2581
//// either version 2.1 of the License, or (at your option) any   ////
2582
//// later version.                                               ////
2583
////                                                              ////
2584
//// This source is distributed in the hope that it will be       ////
2585
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2586
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2587
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2588
//// details.                                                     ////
2589
////                                                              ////
2590
//// You should have received a copy of the GNU Lesser General    ////
2591
//// Public License along with this source; if not, download it   ////
2592
//// from http://www.opencores.org/lgpl.shtml                     ////
2593
////                                                              ////
2594
//////////////////////////////////////////////////////////////////////
2595
 
2596
// LFSR counter
2597
 
2598 40 unneback
`define MODULE cnt_lfsr_ce_zq
2599
module `BASE`MODULE (
2600
`undef MODULE
2601
 cke, zq, rst, clk);
2602
 
2603 6 unneback
   parameter length = 4;
2604
   input cke;
2605
   output reg zq;
2606
   input rst;
2607
   input clk;
2608
 
2609
   parameter clear_value = 0;
2610
   parameter set_value = 1;
2611
   parameter wrap_value = 8;
2612
   parameter level1_value = 15;
2613
 
2614
   reg  [length:1] qi;
2615
   reg lfsr_fb;
2616
   wire [length:1] q_next;
2617
   reg [32:1] polynom;
2618
   integer i;
2619
 
2620
   always @ (qi)
2621
   begin
2622
        case (length)
2623
         2: polynom = 32'b11;                               // 0x3
2624
         3: polynom = 32'b110;                              // 0x6
2625
         4: polynom = 32'b1100;                             // 0xC
2626
         5: polynom = 32'b10100;                            // 0x14
2627
         6: polynom = 32'b110000;                           // 0x30
2628
         7: polynom = 32'b1100000;                          // 0x60
2629
         8: polynom = 32'b10111000;                         // 0xb8
2630
         9: polynom = 32'b100010000;                        // 0x110
2631
        10: polynom = 32'b1001000000;                       // 0x240
2632
        11: polynom = 32'b10100000000;                      // 0x500
2633
        12: polynom = 32'b100000101001;                     // 0x829
2634
        13: polynom = 32'b1000000001100;                    // 0x100C
2635
        14: polynom = 32'b10000000010101;                   // 0x2015
2636
        15: polynom = 32'b110000000000000;                  // 0x6000
2637
        16: polynom = 32'b1101000000001000;                 // 0xD008
2638
        17: polynom = 32'b10010000000000000;                // 0x12000
2639
        18: polynom = 32'b100000010000000000;               // 0x20400
2640
        19: polynom = 32'b1000000000000100011;              // 0x40023
2641 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2642 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2643
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2644
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2645
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2646
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2647
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2648
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2649
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2650
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2651
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2652
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2653
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2654
        default: polynom = 32'b0;
2655
        endcase
2656
        lfsr_fb = qi[length];
2657
        for (i=length-1; i>=1; i=i-1) begin
2658
            if (polynom[i])
2659
                lfsr_fb = lfsr_fb  ~^ qi[i];
2660
        end
2661
    end
2662
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2663
 
2664
   always @ (posedge clk or posedge rst)
2665
     if (rst)
2666
       qi <= {length{1'b0}};
2667
     else
2668
     if (cke)
2669
       qi <= q_next;
2670
 
2671
 
2672
 
2673
   always @ (posedge clk or posedge rst)
2674
     if (rst)
2675
       zq <= 1'b1;
2676
     else
2677
     if (cke)
2678
       zq <= q_next == {length{1'b0}};
2679
endmodule
2680 40 unneback
`endif
2681
`ifdef CNT_LFSR_CE_Q
2682 6 unneback
//////////////////////////////////////////////////////////////////////
2683
////                                                              ////
2684
////  Versatile counter                                           ////
2685
////                                                              ////
2686
////  Description                                                 ////
2687
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2688
////  counter                                                     ////
2689
////                                                              ////
2690
////  To Do:                                                      ////
2691
////   - add LFSR with more taps                                  ////
2692
////                                                              ////
2693
////  Author(s):                                                  ////
2694
////      - Michael Unneback, unneback@opencores.org              ////
2695
////        ORSoC AB                                              ////
2696
////                                                              ////
2697
//////////////////////////////////////////////////////////////////////
2698
////                                                              ////
2699
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2700
////                                                              ////
2701
//// This source file may be used and distributed without         ////
2702
//// restriction provided that this copyright statement is not    ////
2703
//// removed from the file and that any derivative work contains  ////
2704
//// the original copyright notice and the associated disclaimer. ////
2705
////                                                              ////
2706
//// This source file is free software; you can redistribute it   ////
2707
//// and/or modify it under the terms of the GNU Lesser General   ////
2708
//// Public License as published by the Free Software Foundation; ////
2709
//// either version 2.1 of the License, or (at your option) any   ////
2710
//// later version.                                               ////
2711
////                                                              ////
2712
//// This source is distributed in the hope that it will be       ////
2713
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2714
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2715
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2716
//// details.                                                     ////
2717
////                                                              ////
2718
//// You should have received a copy of the GNU Lesser General    ////
2719
//// Public License along with this source; if not, download it   ////
2720
//// from http://www.opencores.org/lgpl.shtml                     ////
2721
////                                                              ////
2722
//////////////////////////////////////////////////////////////////////
2723 22 unneback
 
2724
// LFSR counter
2725 27 unneback
 
2726 40 unneback
`define MODULE cnt_lfsr_ce_q
2727
module `BASE`MODULE (
2728
`undef MODULE
2729
 cke, q, rst, clk);
2730
 
2731 27 unneback
   parameter length = 4;
2732
   input cke;
2733
   output [length:1] q;
2734
   input rst;
2735
   input clk;
2736
 
2737
   parameter clear_value = 0;
2738
   parameter set_value = 1;
2739
   parameter wrap_value = 8;
2740
   parameter level1_value = 15;
2741
 
2742
   reg  [length:1] qi;
2743
   reg lfsr_fb;
2744
   wire [length:1] q_next;
2745
   reg [32:1] polynom;
2746
   integer i;
2747
 
2748
   always @ (qi)
2749
   begin
2750
        case (length)
2751
         2: polynom = 32'b11;                               // 0x3
2752
         3: polynom = 32'b110;                              // 0x6
2753
         4: polynom = 32'b1100;                             // 0xC
2754
         5: polynom = 32'b10100;                            // 0x14
2755
         6: polynom = 32'b110000;                           // 0x30
2756
         7: polynom = 32'b1100000;                          // 0x60
2757
         8: polynom = 32'b10111000;                         // 0xb8
2758
         9: polynom = 32'b100010000;                        // 0x110
2759
        10: polynom = 32'b1001000000;                       // 0x240
2760
        11: polynom = 32'b10100000000;                      // 0x500
2761
        12: polynom = 32'b100000101001;                     // 0x829
2762
        13: polynom = 32'b1000000001100;                    // 0x100C
2763
        14: polynom = 32'b10000000010101;                   // 0x2015
2764
        15: polynom = 32'b110000000000000;                  // 0x6000
2765
        16: polynom = 32'b1101000000001000;                 // 0xD008
2766
        17: polynom = 32'b10010000000000000;                // 0x12000
2767
        18: polynom = 32'b100000010000000000;               // 0x20400
2768
        19: polynom = 32'b1000000000000100011;              // 0x40023
2769 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2770 27 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2771
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2772
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2773
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2774
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2775
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2776
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2777
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2778
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2779
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2780
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2781
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2782
        default: polynom = 32'b0;
2783
        endcase
2784
        lfsr_fb = qi[length];
2785
        for (i=length-1; i>=1; i=i-1) begin
2786
            if (polynom[i])
2787
                lfsr_fb = lfsr_fb  ~^ qi[i];
2788
        end
2789
    end
2790
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2791
 
2792
   always @ (posedge clk or posedge rst)
2793
     if (rst)
2794
       qi <= {length{1'b0}};
2795
     else
2796
     if (cke)
2797
       qi <= q_next;
2798
 
2799
   assign q = qi;
2800
 
2801
endmodule
2802 40 unneback
`endif
2803
`ifdef CNT_LFSR_CE_CLEAR_Q
2804 27 unneback
//////////////////////////////////////////////////////////////////////
2805
////                                                              ////
2806
////  Versatile counter                                           ////
2807
////                                                              ////
2808
////  Description                                                 ////
2809
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2810
////  counter                                                     ////
2811
////                                                              ////
2812
////  To Do:                                                      ////
2813
////   - add LFSR with more taps                                  ////
2814
////                                                              ////
2815
////  Author(s):                                                  ////
2816
////      - Michael Unneback, unneback@opencores.org              ////
2817
////        ORSoC AB                                              ////
2818
////                                                              ////
2819
//////////////////////////////////////////////////////////////////////
2820
////                                                              ////
2821
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2822
////                                                              ////
2823
//// This source file may be used and distributed without         ////
2824
//// restriction provided that this copyright statement is not    ////
2825
//// removed from the file and that any derivative work contains  ////
2826
//// the original copyright notice and the associated disclaimer. ////
2827
////                                                              ////
2828
//// This source file is free software; you can redistribute it   ////
2829
//// and/or modify it under the terms of the GNU Lesser General   ////
2830
//// Public License as published by the Free Software Foundation; ////
2831
//// either version 2.1 of the License, or (at your option) any   ////
2832
//// later version.                                               ////
2833
////                                                              ////
2834
//// This source is distributed in the hope that it will be       ////
2835
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2836
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2837
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2838
//// details.                                                     ////
2839
////                                                              ////
2840
//// You should have received a copy of the GNU Lesser General    ////
2841
//// Public License along with this source; if not, download it   ////
2842
//// from http://www.opencores.org/lgpl.shtml                     ////
2843
////                                                              ////
2844
//////////////////////////////////////////////////////////////////////
2845
 
2846
// LFSR counter
2847
 
2848 40 unneback
`define MODULE cnt_lfsr_ce_clear_q
2849
module `BASE`MODULE (
2850
`undef MODULE
2851
 clear, cke, q, rst, clk);
2852
 
2853 27 unneback
   parameter length = 4;
2854
   input clear;
2855
   input cke;
2856
   output [length:1] q;
2857
   input rst;
2858
   input clk;
2859
 
2860
   parameter clear_value = 0;
2861
   parameter set_value = 1;
2862
   parameter wrap_value = 8;
2863
   parameter level1_value = 15;
2864
 
2865
   reg  [length:1] qi;
2866
   reg lfsr_fb;
2867
   wire [length:1] q_next;
2868
   reg [32:1] polynom;
2869
   integer i;
2870
 
2871
   always @ (qi)
2872
   begin
2873
        case (length)
2874
         2: polynom = 32'b11;                               // 0x3
2875
         3: polynom = 32'b110;                              // 0x6
2876
         4: polynom = 32'b1100;                             // 0xC
2877
         5: polynom = 32'b10100;                            // 0x14
2878
         6: polynom = 32'b110000;                           // 0x30
2879
         7: polynom = 32'b1100000;                          // 0x60
2880
         8: polynom = 32'b10111000;                         // 0xb8
2881
         9: polynom = 32'b100010000;                        // 0x110
2882
        10: polynom = 32'b1001000000;                       // 0x240
2883
        11: polynom = 32'b10100000000;                      // 0x500
2884
        12: polynom = 32'b100000101001;                     // 0x829
2885
        13: polynom = 32'b1000000001100;                    // 0x100C
2886
        14: polynom = 32'b10000000010101;                   // 0x2015
2887
        15: polynom = 32'b110000000000000;                  // 0x6000
2888
        16: polynom = 32'b1101000000001000;                 // 0xD008
2889
        17: polynom = 32'b10010000000000000;                // 0x12000
2890
        18: polynom = 32'b100000010000000000;               // 0x20400
2891
        19: polynom = 32'b1000000000000100011;              // 0x40023
2892 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2893 27 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2894
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2895
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2896
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2897
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2898
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2899
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2900
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2901
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2902
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2903
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2904
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2905
        default: polynom = 32'b0;
2906
        endcase
2907
        lfsr_fb = qi[length];
2908
        for (i=length-1; i>=1; i=i-1) begin
2909
            if (polynom[i])
2910
                lfsr_fb = lfsr_fb  ~^ qi[i];
2911
        end
2912
    end
2913
   assign q_next =  clear ? {length{1'b0}} :(qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2914
 
2915
   always @ (posedge clk or posedge rst)
2916
     if (rst)
2917
       qi <= {length{1'b0}};
2918
     else
2919
     if (cke)
2920
       qi <= q_next;
2921
 
2922
   assign q = qi;
2923
 
2924
endmodule
2925 40 unneback
`endif
2926
`ifdef CNT_LFSR_CE_Q_ZQ
2927 27 unneback
//////////////////////////////////////////////////////////////////////
2928
////                                                              ////
2929
////  Versatile counter                                           ////
2930
////                                                              ////
2931
////  Description                                                 ////
2932
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2933
////  counter                                                     ////
2934
////                                                              ////
2935
////  To Do:                                                      ////
2936
////   - add LFSR with more taps                                  ////
2937
////                                                              ////
2938
////  Author(s):                                                  ////
2939
////      - Michael Unneback, unneback@opencores.org              ////
2940
////        ORSoC AB                                              ////
2941
////                                                              ////
2942
//////////////////////////////////////////////////////////////////////
2943
////                                                              ////
2944
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2945
////                                                              ////
2946
//// This source file may be used and distributed without         ////
2947
//// restriction provided that this copyright statement is not    ////
2948
//// removed from the file and that any derivative work contains  ////
2949
//// the original copyright notice and the associated disclaimer. ////
2950
////                                                              ////
2951
//// This source file is free software; you can redistribute it   ////
2952
//// and/or modify it under the terms of the GNU Lesser General   ////
2953
//// Public License as published by the Free Software Foundation; ////
2954
//// either version 2.1 of the License, or (at your option) any   ////
2955
//// later version.                                               ////
2956
////                                                              ////
2957
//// This source is distributed in the hope that it will be       ////
2958
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2959
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2960
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2961
//// details.                                                     ////
2962
////                                                              ////
2963
//// You should have received a copy of the GNU Lesser General    ////
2964
//// Public License along with this source; if not, download it   ////
2965
//// from http://www.opencores.org/lgpl.shtml                     ////
2966
////                                                              ////
2967
//////////////////////////////////////////////////////////////////////
2968
 
2969
// LFSR counter
2970 22 unneback
 
2971 40 unneback
`define MODULE cnt_lfsr_ce_q_zq
2972
module `BASE`MODULE (
2973
`undef MODULE
2974
 cke, q, zq, rst, clk);
2975
 
2976 22 unneback
   parameter length = 4;
2977
   input cke;
2978
   output [length:1] q;
2979
   output reg zq;
2980
   input rst;
2981
   input clk;
2982
 
2983
   parameter clear_value = 0;
2984
   parameter set_value = 1;
2985
   parameter wrap_value = 8;
2986
   parameter level1_value = 15;
2987
 
2988
   reg  [length:1] qi;
2989
   reg lfsr_fb;
2990
   wire [length:1] q_next;
2991
   reg [32:1] polynom;
2992
   integer i;
2993
 
2994
   always @ (qi)
2995
   begin
2996
        case (length)
2997
         2: polynom = 32'b11;                               // 0x3
2998
         3: polynom = 32'b110;                              // 0x6
2999
         4: polynom = 32'b1100;                             // 0xC
3000
         5: polynom = 32'b10100;                            // 0x14
3001
         6: polynom = 32'b110000;                           // 0x30
3002
         7: polynom = 32'b1100000;                          // 0x60
3003
         8: polynom = 32'b10111000;                         // 0xb8
3004
         9: polynom = 32'b100010000;                        // 0x110
3005
        10: polynom = 32'b1001000000;                       // 0x240
3006
        11: polynom = 32'b10100000000;                      // 0x500
3007
        12: polynom = 32'b100000101001;                     // 0x829
3008
        13: polynom = 32'b1000000001100;                    // 0x100C
3009
        14: polynom = 32'b10000000010101;                   // 0x2015
3010
        15: polynom = 32'b110000000000000;                  // 0x6000
3011
        16: polynom = 32'b1101000000001000;                 // 0xD008
3012
        17: polynom = 32'b10010000000000000;                // 0x12000
3013
        18: polynom = 32'b100000010000000000;               // 0x20400
3014
        19: polynom = 32'b1000000000000100011;              // 0x40023
3015 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3016 22 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3017
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3018
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3019
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3020
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3021
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3022
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3023
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3024
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3025
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3026
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3027
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3028
        default: polynom = 32'b0;
3029
        endcase
3030
        lfsr_fb = qi[length];
3031
        for (i=length-1; i>=1; i=i-1) begin
3032
            if (polynom[i])
3033
                lfsr_fb = lfsr_fb  ~^ qi[i];
3034
        end
3035
    end
3036
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3037
 
3038
   always @ (posedge clk or posedge rst)
3039
     if (rst)
3040
       qi <= {length{1'b0}};
3041
     else
3042
     if (cke)
3043
       qi <= q_next;
3044
 
3045
   assign q = qi;
3046
 
3047
 
3048
   always @ (posedge clk or posedge rst)
3049
     if (rst)
3050
       zq <= 1'b1;
3051
     else
3052
     if (cke)
3053
       zq <= q_next == {length{1'b0}};
3054
endmodule
3055 40 unneback
`endif
3056
`ifdef CNT_LFSR_CE_REW_L1
3057 22 unneback
//////////////////////////////////////////////////////////////////////
3058
////                                                              ////
3059
////  Versatile counter                                           ////
3060
////                                                              ////
3061
////  Description                                                 ////
3062
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3063
////  counter                                                     ////
3064
////                                                              ////
3065
////  To Do:                                                      ////
3066
////   - add LFSR with more taps                                  ////
3067
////                                                              ////
3068
////  Author(s):                                                  ////
3069
////      - Michael Unneback, unneback@opencores.org              ////
3070
////        ORSoC AB                                              ////
3071
////                                                              ////
3072
//////////////////////////////////////////////////////////////////////
3073
////                                                              ////
3074
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3075
////                                                              ////
3076
//// This source file may be used and distributed without         ////
3077
//// restriction provided that this copyright statement is not    ////
3078
//// removed from the file and that any derivative work contains  ////
3079
//// the original copyright notice and the associated disclaimer. ////
3080
////                                                              ////
3081
//// This source file is free software; you can redistribute it   ////
3082
//// and/or modify it under the terms of the GNU Lesser General   ////
3083
//// Public License as published by the Free Software Foundation; ////
3084
//// either version 2.1 of the License, or (at your option) any   ////
3085
//// later version.                                               ////
3086
////                                                              ////
3087
//// This source is distributed in the hope that it will be       ////
3088
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3089
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3090
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3091
//// details.                                                     ////
3092
////                                                              ////
3093
//// You should have received a copy of the GNU Lesser General    ////
3094
//// Public License along with this source; if not, download it   ////
3095
//// from http://www.opencores.org/lgpl.shtml                     ////
3096
////                                                              ////
3097
//////////////////////////////////////////////////////////////////////
3098 6 unneback
 
3099
// LFSR counter
3100
 
3101 40 unneback
`define MODULE cnt_lfsr_ce_rew_l1
3102
module `BASE`MODULE (
3103
`undef MODULE
3104
 cke, rew, level1, rst, clk);
3105
 
3106 6 unneback
   parameter length = 4;
3107
   input cke;
3108
   input rew;
3109
   output reg level1;
3110
   input rst;
3111
   input clk;
3112
 
3113
   parameter clear_value = 0;
3114
   parameter set_value = 1;
3115
   parameter wrap_value = 8;
3116
   parameter level1_value = 15;
3117
 
3118 29 unneback
   wire clear;
3119 30 unneback
   assign clear = 1'b0;
3120 6 unneback
   reg  [length:1] qi;
3121
   reg lfsr_fb, lfsr_fb_rew;
3122
   wire  [length:1] q_next, q_next_fw, q_next_rew;
3123
   reg [32:1] polynom_rew;
3124
   integer j;
3125
   reg [32:1] polynom;
3126
   integer i;
3127
 
3128
   always @ (qi)
3129
   begin
3130
        case (length)
3131
         2: polynom = 32'b11;                               // 0x3
3132
         3: polynom = 32'b110;                              // 0x6
3133
         4: polynom = 32'b1100;                             // 0xC
3134
         5: polynom = 32'b10100;                            // 0x14
3135
         6: polynom = 32'b110000;                           // 0x30
3136
         7: polynom = 32'b1100000;                          // 0x60
3137
         8: polynom = 32'b10111000;                         // 0xb8
3138
         9: polynom = 32'b100010000;                        // 0x110
3139
        10: polynom = 32'b1001000000;                       // 0x240
3140
        11: polynom = 32'b10100000000;                      // 0x500
3141
        12: polynom = 32'b100000101001;                     // 0x829
3142
        13: polynom = 32'b1000000001100;                    // 0x100C
3143
        14: polynom = 32'b10000000010101;                   // 0x2015
3144
        15: polynom = 32'b110000000000000;                  // 0x6000
3145
        16: polynom = 32'b1101000000001000;                 // 0xD008
3146
        17: polynom = 32'b10010000000000000;                // 0x12000
3147
        18: polynom = 32'b100000010000000000;               // 0x20400
3148
        19: polynom = 32'b1000000000000100011;              // 0x40023
3149 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3150 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3151
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3152
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3153
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3154
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3155
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3156
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3157
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3158
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3159
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3160
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3161
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3162
        default: polynom = 32'b0;
3163
        endcase
3164
        lfsr_fb = qi[length];
3165
        for (i=length-1; i>=1; i=i-1) begin
3166
            if (polynom[i])
3167
                lfsr_fb = lfsr_fb  ~^ qi[i];
3168
        end
3169
    end
3170
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3171
   always @ (qi)
3172
   begin
3173
        case (length)
3174
         2: polynom_rew = 32'b11;
3175
         3: polynom_rew = 32'b110;
3176
         4: polynom_rew = 32'b1100;
3177
         5: polynom_rew = 32'b10100;
3178
         6: polynom_rew = 32'b110000;
3179
         7: polynom_rew = 32'b1100000;
3180
         8: polynom_rew = 32'b10111000;
3181
         9: polynom_rew = 32'b100010000;
3182
        10: polynom_rew = 32'b1001000000;
3183
        11: polynom_rew = 32'b10100000000;
3184
        12: polynom_rew = 32'b100000101001;
3185
        13: polynom_rew = 32'b1000000001100;
3186
        14: polynom_rew = 32'b10000000010101;
3187
        15: polynom_rew = 32'b110000000000000;
3188
        16: polynom_rew = 32'b1101000000001000;
3189
        17: polynom_rew = 32'b10010000000000000;
3190
        18: polynom_rew = 32'b100000010000000000;
3191
        19: polynom_rew = 32'b1000000000000100011;
3192
        20: polynom_rew = 32'b10000010000000000000;
3193
        21: polynom_rew = 32'b101000000000000000000;
3194
        22: polynom_rew = 32'b1100000000000000000000;
3195
        23: polynom_rew = 32'b10000100000000000000000;
3196
        24: polynom_rew = 32'b111000010000000000000000;
3197
        25: polynom_rew = 32'b1001000000000000000000000;
3198
        26: polynom_rew = 32'b10000000000000000000100011;
3199
        27: polynom_rew = 32'b100000000000000000000010011;
3200
        28: polynom_rew = 32'b1100100000000000000000000000;
3201
        29: polynom_rew = 32'b10100000000000000000000000000;
3202
        30: polynom_rew = 32'b100000000000000000000000101001;
3203
        31: polynom_rew = 32'b1001000000000000000000000000000;
3204
        32: polynom_rew = 32'b10000000001000000000000000000011;
3205
        default: polynom_rew = 32'b0;
3206
        endcase
3207
        // rotate left
3208
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
3209
        lfsr_fb_rew = qi[length];
3210
        for (i=length-1; i>=1; i=i-1) begin
3211
            if (polynom_rew[i])
3212
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
3213
        end
3214
    end
3215
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
3216
   assign q_next = rew ? q_next_rew : q_next_fw;
3217
 
3218
   always @ (posedge clk or posedge rst)
3219
     if (rst)
3220
       qi <= {length{1'b0}};
3221
     else
3222
     if (cke)
3223
       qi <= q_next;
3224
 
3225
 
3226
 
3227
    always @ (posedge clk or posedge rst)
3228
    if (rst)
3229
        level1 <= 1'b0;
3230
    else
3231
    if (cke)
3232 29 unneback
    if (clear)
3233
        level1 <= 1'b0;
3234
    else if (q_next == level1_value)
3235 6 unneback
        level1 <= 1'b1;
3236
    else if (qi == level1_value & rew)
3237
        level1 <= 1'b0;
3238
endmodule
3239 40 unneback
`endif
3240
`ifdef CNT_GRAY
3241 6 unneback
//////////////////////////////////////////////////////////////////////
3242
////                                                              ////
3243
////  Versatile counter                                           ////
3244
////                                                              ////
3245
////  Description                                                 ////
3246
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3247
////  counter                                                     ////
3248
////                                                              ////
3249
////  To Do:                                                      ////
3250
////   - add LFSR with more taps                                  ////
3251
////                                                              ////
3252
////  Author(s):                                                  ////
3253
////      - Michael Unneback, unneback@opencores.org              ////
3254
////        ORSoC AB                                              ////
3255
////                                                              ////
3256
//////////////////////////////////////////////////////////////////////
3257
////                                                              ////
3258
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3259
////                                                              ////
3260
//// This source file may be used and distributed without         ////
3261
//// restriction provided that this copyright statement is not    ////
3262
//// removed from the file and that any derivative work contains  ////
3263
//// the original copyright notice and the associated disclaimer. ////
3264
////                                                              ////
3265
//// This source file is free software; you can redistribute it   ////
3266
//// and/or modify it under the terms of the GNU Lesser General   ////
3267
//// Public License as published by the Free Software Foundation; ////
3268
//// either version 2.1 of the License, or (at your option) any   ////
3269
//// later version.                                               ////
3270
////                                                              ////
3271
//// This source is distributed in the hope that it will be       ////
3272
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3273
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3274
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3275
//// details.                                                     ////
3276
////                                                              ////
3277
//// You should have received a copy of the GNU Lesser General    ////
3278
//// Public License along with this source; if not, download it   ////
3279
//// from http://www.opencores.org/lgpl.shtml                     ////
3280
////                                                              ////
3281
//////////////////////////////////////////////////////////////////////
3282
 
3283
// GRAY counter
3284
 
3285 40 unneback
`define MODULE cnt_gray
3286
module `BASE`MODULE (
3287
`undef MODULE
3288
 q, rst, clk);
3289
 
3290 6 unneback
   parameter length = 4;
3291
   output reg [length:1] q;
3292
   input rst;
3293
   input clk;
3294
 
3295
   parameter clear_value = 0;
3296
   parameter set_value = 1;
3297
   parameter wrap_value = 8;
3298
   parameter level1_value = 15;
3299
 
3300
   reg  [length:1] qi;
3301
   wire [length:1] q_next;
3302
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3303
 
3304
   always @ (posedge clk or posedge rst)
3305
     if (rst)
3306
       qi <= {length{1'b0}};
3307
     else
3308
       qi <= q_next;
3309
 
3310
   always @ (posedge clk or posedge rst)
3311
     if (rst)
3312
       q <= {length{1'b0}};
3313
     else
3314
         q <= (q_next>>1) ^ q_next;
3315
 
3316
endmodule
3317 40 unneback
`endif
3318
`ifdef CNT_GRAY_CE
3319 6 unneback
//////////////////////////////////////////////////////////////////////
3320
////                                                              ////
3321
////  Versatile counter                                           ////
3322
////                                                              ////
3323
////  Description                                                 ////
3324
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3325
////  counter                                                     ////
3326
////                                                              ////
3327
////  To Do:                                                      ////
3328
////   - add LFSR with more taps                                  ////
3329
////                                                              ////
3330
////  Author(s):                                                  ////
3331
////      - Michael Unneback, unneback@opencores.org              ////
3332
////        ORSoC AB                                              ////
3333
////                                                              ////
3334
//////////////////////////////////////////////////////////////////////
3335
////                                                              ////
3336
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3337
////                                                              ////
3338
//// This source file may be used and distributed without         ////
3339
//// restriction provided that this copyright statement is not    ////
3340
//// removed from the file and that any derivative work contains  ////
3341
//// the original copyright notice and the associated disclaimer. ////
3342
////                                                              ////
3343
//// This source file is free software; you can redistribute it   ////
3344
//// and/or modify it under the terms of the GNU Lesser General   ////
3345
//// Public License as published by the Free Software Foundation; ////
3346
//// either version 2.1 of the License, or (at your option) any   ////
3347
//// later version.                                               ////
3348
////                                                              ////
3349
//// This source is distributed in the hope that it will be       ////
3350
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3351
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3352
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3353
//// details.                                                     ////
3354
////                                                              ////
3355
//// You should have received a copy of the GNU Lesser General    ////
3356
//// Public License along with this source; if not, download it   ////
3357
//// from http://www.opencores.org/lgpl.shtml                     ////
3358
////                                                              ////
3359
//////////////////////////////////////////////////////////////////////
3360
 
3361
// GRAY counter
3362
 
3363 40 unneback
`define MODULE cnt_gray_ce
3364
module `BASE`MODULE (
3365
`undef MODULE
3366
 cke, q, rst, clk);
3367
 
3368 6 unneback
   parameter length = 4;
3369
   input cke;
3370
   output reg [length:1] q;
3371
   input rst;
3372
   input clk;
3373
 
3374
   parameter clear_value = 0;
3375
   parameter set_value = 1;
3376
   parameter wrap_value = 8;
3377
   parameter level1_value = 15;
3378
 
3379
   reg  [length:1] qi;
3380
   wire [length:1] q_next;
3381
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3382
 
3383
   always @ (posedge clk or posedge rst)
3384
     if (rst)
3385
       qi <= {length{1'b0}};
3386
     else
3387
     if (cke)
3388
       qi <= q_next;
3389
 
3390
   always @ (posedge clk or posedge rst)
3391
     if (rst)
3392
       q <= {length{1'b0}};
3393
     else
3394
       if (cke)
3395
         q <= (q_next>>1) ^ q_next;
3396
 
3397
endmodule
3398 40 unneback
`endif
3399
`ifdef CNT_GRAY_CE_BIN
3400 6 unneback
//////////////////////////////////////////////////////////////////////
3401
////                                                              ////
3402
////  Versatile counter                                           ////
3403
////                                                              ////
3404
////  Description                                                 ////
3405
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3406
////  counter                                                     ////
3407
////                                                              ////
3408
////  To Do:                                                      ////
3409
////   - add LFSR with more taps                                  ////
3410
////                                                              ////
3411
////  Author(s):                                                  ////
3412
////      - Michael Unneback, unneback@opencores.org              ////
3413
////        ORSoC AB                                              ////
3414
////                                                              ////
3415
//////////////////////////////////////////////////////////////////////
3416
////                                                              ////
3417
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3418
////                                                              ////
3419
//// This source file may be used and distributed without         ////
3420
//// restriction provided that this copyright statement is not    ////
3421
//// removed from the file and that any derivative work contains  ////
3422
//// the original copyright notice and the associated disclaimer. ////
3423
////                                                              ////
3424
//// This source file is free software; you can redistribute it   ////
3425
//// and/or modify it under the terms of the GNU Lesser General   ////
3426
//// Public License as published by the Free Software Foundation; ////
3427
//// either version 2.1 of the License, or (at your option) any   ////
3428
//// later version.                                               ////
3429
////                                                              ////
3430
//// This source is distributed in the hope that it will be       ////
3431
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3432
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3433
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3434
//// details.                                                     ////
3435
////                                                              ////
3436
//// You should have received a copy of the GNU Lesser General    ////
3437
//// Public License along with this source; if not, download it   ////
3438
//// from http://www.opencores.org/lgpl.shtml                     ////
3439
////                                                              ////
3440
//////////////////////////////////////////////////////////////////////
3441
 
3442
// GRAY counter
3443
 
3444 40 unneback
`define MODULE cnt_gray_ce_bin
3445
module `BASE`MODULE (
3446
`undef MODULE
3447
 cke, q, q_bin, rst, clk);
3448
 
3449 6 unneback
   parameter length = 4;
3450
   input cke;
3451
   output reg [length:1] q;
3452
   output [length:1] q_bin;
3453
   input rst;
3454
   input clk;
3455
 
3456
   parameter clear_value = 0;
3457
   parameter set_value = 1;
3458
   parameter wrap_value = 8;
3459
   parameter level1_value = 15;
3460
 
3461
   reg  [length:1] qi;
3462
   wire [length:1] q_next;
3463
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3464
 
3465
   always @ (posedge clk or posedge rst)
3466
     if (rst)
3467
       qi <= {length{1'b0}};
3468
     else
3469
     if (cke)
3470
       qi <= q_next;
3471
 
3472
   always @ (posedge clk or posedge rst)
3473
     if (rst)
3474
       q <= {length{1'b0}};
3475
     else
3476
       if (cke)
3477
         q <= (q_next>>1) ^ q_next;
3478
 
3479
   assign q_bin = qi;
3480
 
3481
endmodule
3482 40 unneback
`endif
3483 6 unneback
//////////////////////////////////////////////////////////////////////
3484
////                                                              ////
3485
////  Versatile library, counters                                 ////
3486
////                                                              ////
3487
////  Description                                                 ////
3488
////  counters                                                    ////
3489
////                                                              ////
3490
////                                                              ////
3491
////  To Do:                                                      ////
3492
////   - add more counters                                        ////
3493
////                                                              ////
3494
////  Author(s):                                                  ////
3495
////      - Michael Unneback, unneback@opencores.org              ////
3496
////        ORSoC AB                                              ////
3497
////                                                              ////
3498
//////////////////////////////////////////////////////////////////////
3499
////                                                              ////
3500
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3501
////                                                              ////
3502
//// This source file may be used and distributed without         ////
3503
//// restriction provided that this copyright statement is not    ////
3504
//// removed from the file and that any derivative work contains  ////
3505
//// the original copyright notice and the associated disclaimer. ////
3506
////                                                              ////
3507
//// This source file is free software; you can redistribute it   ////
3508
//// and/or modify it under the terms of the GNU Lesser General   ////
3509
//// Public License as published by the Free Software Foundation; ////
3510
//// either version 2.1 of the License, or (at your option) any   ////
3511
//// later version.                                               ////
3512
////                                                              ////
3513
//// This source is distributed in the hope that it will be       ////
3514
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3515
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3516
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3517
//// details.                                                     ////
3518
////                                                              ////
3519
//// You should have received a copy of the GNU Lesser General    ////
3520
//// Public License along with this source; if not, download it   ////
3521
//// from http://www.opencores.org/lgpl.shtml                     ////
3522
////                                                              ////
3523
//////////////////////////////////////////////////////////////////////
3524
 
3525 40 unneback
`ifdef CNT_SHREG_WRAP
3526
`define MODULE cnt_shreg_wrap
3527
module `BASE`MODULE ( q, rst, clk);
3528
`undef MODULE
3529 6 unneback
 
3530
   parameter length = 4;
3531
   output reg [0:length-1] q;
3532
   input rst;
3533
   input clk;
3534
 
3535
    always @ (posedge clk or posedge rst)
3536
    if (rst)
3537
        q <= {1'b1,{length-1{1'b0}}};
3538
    else
3539
        q <= {q[length-1],q[0:length-2]};
3540
 
3541
endmodule
3542 40 unneback
`endif
3543 6 unneback
 
3544 40 unneback
`ifdef CNT_SHREG_CE_WRAP
3545
`define MODULE cnt_shreg_ce_wrap
3546
module `BASE`MODULE ( cke, q, rst, clk);
3547
`undef MODULE
3548 6 unneback
 
3549
   parameter length = 4;
3550
   input cke;
3551
   output reg [0:length-1] q;
3552
   input rst;
3553
   input clk;
3554
 
3555
    always @ (posedge clk or posedge rst)
3556
    if (rst)
3557
        q <= {1'b1,{length-1{1'b0}}};
3558
    else
3559
        if (cke)
3560
            q <= {q[length-1],q[0:length-2]};
3561
 
3562
endmodule
3563 40 unneback
`endif
3564 6 unneback
 
3565 40 unneback
`ifdef CNT_SHREG_CE_CLEAR
3566
`define MODULE cnt_shreg_ce_clear
3567
module `BASE`MODULE ( cke, clear, q, rst, clk);
3568
`undef MODULE
3569 6 unneback
 
3570
   parameter length = 4;
3571
   input cke, clear;
3572
   output reg [0:length-1] q;
3573
   input rst;
3574
   input clk;
3575
 
3576
    always @ (posedge clk or posedge rst)
3577
    if (rst)
3578
        q <= {1'b1,{length-1{1'b0}}};
3579
    else
3580
        if (cke)
3581
            if (clear)
3582
                q <= {1'b1,{length-1{1'b0}}};
3583
            else
3584
                q <= q >> 1;
3585
 
3586
endmodule
3587 40 unneback
`endif
3588 6 unneback
 
3589 40 unneback
`ifdef CNT_SHREG_CE_CLEAR_WRAP
3590
`define MODULE cnt_shreg_ce_clear_wrap
3591
module `BASE`MODULE ( cke, clear, q, rst, clk);
3592
`undef MODULE
3593 6 unneback
 
3594
   parameter length = 4;
3595
   input cke, clear;
3596
   output reg [0:length-1] q;
3597
   input rst;
3598
   input clk;
3599
 
3600
    always @ (posedge clk or posedge rst)
3601
    if (rst)
3602
        q <= {1'b1,{length-1{1'b0}}};
3603
    else
3604
        if (cke)
3605
            if (clear)
3606
                q <= {1'b1,{length-1{1'b0}}};
3607
            else
3608
            q <= {q[length-1],q[0:length-2]};
3609
 
3610
endmodule
3611 40 unneback
`endif
3612 6 unneback
//////////////////////////////////////////////////////////////////////
3613
////                                                              ////
3614
////  Versatile library, memories                                 ////
3615
////                                                              ////
3616
////  Description                                                 ////
3617
////  memories                                                    ////
3618
////                                                              ////
3619
////                                                              ////
3620
////  To Do:                                                      ////
3621
////   - add more memory types                                    ////
3622
////                                                              ////
3623
////  Author(s):                                                  ////
3624
////      - Michael Unneback, unneback@opencores.org              ////
3625
////        ORSoC AB                                              ////
3626
////                                                              ////
3627
//////////////////////////////////////////////////////////////////////
3628
////                                                              ////
3629
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3630
////                                                              ////
3631
//// This source file may be used and distributed without         ////
3632
//// restriction provided that this copyright statement is not    ////
3633
//// removed from the file and that any derivative work contains  ////
3634
//// the original copyright notice and the associated disclaimer. ////
3635
////                                                              ////
3636
//// This source file is free software; you can redistribute it   ////
3637
//// and/or modify it under the terms of the GNU Lesser General   ////
3638
//// Public License as published by the Free Software Foundation; ////
3639
//// either version 2.1 of the License, or (at your option) any   ////
3640
//// later version.                                               ////
3641
////                                                              ////
3642
//// This source is distributed in the hope that it will be       ////
3643
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3644
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3645
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3646
//// details.                                                     ////
3647
////                                                              ////
3648
//// You should have received a copy of the GNU Lesser General    ////
3649
//// Public License along with this source; if not, download it   ////
3650
//// from http://www.opencores.org/lgpl.shtml                     ////
3651
////                                                              ////
3652
//////////////////////////////////////////////////////////////////////
3653
 
3654 40 unneback
`ifdef ROM_INIT
3655 6 unneback
/// ROM
3656 40 unneback
`define MODULE rom_init
3657
module `BASE`MODULE ( adr, q, clk);
3658
`undef MODULE
3659 6 unneback
 
3660 7 unneback
   parameter data_width = 32;
3661
   parameter addr_width = 8;
3662 75 unneback
   parameter mem_size = 1<<addr_width;
3663 7 unneback
   input [(addr_width-1):0]       adr;
3664
   output reg [(data_width-1):0] q;
3665
   input                         clk;
3666 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
3667 7 unneback
   parameter memory_file = "vl_rom.vmem";
3668
   initial
3669
     begin
3670
        $readmemh(memory_file, rom);
3671
     end
3672
 
3673
   always @ (posedge clk)
3674
     q <= rom[adr];
3675 6 unneback
 
3676 7 unneback
endmodule
3677 40 unneback
`endif
3678 7 unneback
 
3679 40 unneback
`ifdef RAM
3680
`define MODULE ram
3681 6 unneback
// Single port RAM
3682 40 unneback
module `BASE`MODULE ( d, adr, we, q, clk);
3683
`undef MODULE
3684 6 unneback
 
3685
   parameter data_width = 32;
3686
   parameter addr_width = 8;
3687 75 unneback
   parameter mem_size = 1<<addr_width;
3688 6 unneback
   input [(data_width-1):0]      d;
3689
   input [(addr_width-1):0]       adr;
3690
   input                         we;
3691 7 unneback
   output reg [(data_width-1):0] q;
3692 6 unneback
   input                         clk;
3693 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0];
3694 7 unneback
   parameter init = 0;
3695
   parameter memory_file = "vl_ram.vmem";
3696
   generate if (init) begin : init_mem
3697
   initial
3698
     begin
3699
        $readmemh(memory_file, ram);
3700
     end
3701
   end
3702
   endgenerate
3703
 
3704 6 unneback
   always @ (posedge clk)
3705
   begin
3706
   if (we)
3707
     ram[adr] <= d;
3708
   q <= ram[adr];
3709
   end
3710
 
3711
endmodule
3712 40 unneback
`endif
3713 6 unneback
 
3714 40 unneback
`ifdef RAM_BE
3715
`define MODULE ram_be
3716 90 unneback
module `BASE`MODULE ( d, adr, be, re, we, q, clk);
3717 40 unneback
`undef MODULE
3718
 
3719 7 unneback
   parameter data_width = 32;
3720 72 unneback
   parameter addr_width = 6;
3721 75 unneback
   parameter mem_size = 1<<addr_width;
3722 7 unneback
   input [(data_width-1):0]      d;
3723
   input [(addr_width-1):0]       adr;
3724 73 unneback
   input [(data_width/8)-1:0]    be;
3725 90 unneback
   input                         re;
3726 7 unneback
   input                         we;
3727
   output reg [(data_width-1):0] q;
3728
   input                         clk;
3729
 
3730 85 unneback
 
3731 65 unneback
`ifdef SYSTEMVERILOG
3732 68 unneback
   logic [data_width/8-1:0][7:0] ram[0:mem_size-1];// # words = 1 << address width
3733 65 unneback
`else
3734 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
3735
    wire [data_width/8-1:0] cke;
3736 65 unneback
`endif
3737
 
3738 60 unneback
   parameter memory_init = 0;
3739 7 unneback
   parameter memory_file = "vl_ram.vmem";
3740 60 unneback
   generate if (memory_init) begin : init_mem
3741 7 unneback
   initial
3742
     begin
3743
        $readmemh(memory_file, ram);
3744
     end
3745
   end
3746
   endgenerate
3747
 
3748 60 unneback
`ifdef SYSTEMVERILOG
3749
// use a multi-dimensional packed array
3750
//to model individual bytes within the word
3751
 
3752
always_ff@(posedge clk)
3753
begin
3754
    if(we) begin // note: we should have a for statement to support any bus width
3755 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
3756
        if(be[2]) ram[adr][2] <= d[23:16];
3757
        if(be[1]) ram[adr][1] <= d[15:8];
3758
        if(be[0]) ram[adr][0] <= d[7:0];
3759 60 unneback
    end
3760 90 unneback
    if (re)
3761
        q <= ram[adr];
3762 60 unneback
end
3763
 
3764
`else
3765
 
3766 85 unneback
assign cke = {data_width/8{we}} & be;
3767 7 unneback
   genvar i;
3768 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
3769 7 unneback
      always @ (posedge clk)
3770 85 unneback
      if (cke[i])
3771 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
3772
   end
3773
   endgenerate
3774
 
3775
   always @ (posedge clk)
3776 90 unneback
    if (re)
3777 7 unneback
      q <= ram[adr];
3778
 
3779 60 unneback
`endif
3780
 
3781 85 unneback
   // Function to access RAM (for use by Verilator).
3782
   function [31:0] get_mem;
3783
      // verilator public
3784 90 unneback
      input [addr_width-1:0]             addr;
3785 85 unneback
      get_mem = ram[addr];
3786
   endfunction // get_mem
3787
 
3788
   // Function to write RAM (for use by Verilator).
3789
   function set_mem;
3790
      // verilator public
3791 90 unneback
      input [addr_width-1:0]             addr;
3792
      input [data_width-1:0]             data;
3793 85 unneback
      ram[addr] = data;
3794
   endfunction // set_mem
3795
 
3796 7 unneback
endmodule
3797 40 unneback
`endif
3798 7 unneback
 
3799 6 unneback
`ifdef ACTEL
3800 48 unneback
        // ACTEL FPGA should not use logic to handle rw collision
3801 6 unneback
        `define SYN /*synthesis syn_ramstyle = "no_rw_check"*/
3802
`else
3803
        `define SYN
3804
`endif
3805
 
3806 40 unneback
`ifdef DPRAM_1R1W
3807
`define MODULE dpram_1r1w
3808
module `BASE`MODULE ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
3809
`undef MODULE
3810 6 unneback
   parameter data_width = 32;
3811
   parameter addr_width = 8;
3812 75 unneback
   parameter mem_size = 1<<addr_width;
3813 6 unneback
   input [(data_width-1):0]      d_a;
3814
   input [(addr_width-1):0]       adr_a;
3815
   input [(addr_width-1):0]       adr_b;
3816
   input                         we_a;
3817
   output [(data_width-1):0]      q_b;
3818
   input                         clk_a, clk_b;
3819
   reg [(addr_width-1):0]         adr_b_reg;
3820 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0] `SYN;
3821 7 unneback
 
3822
   parameter init = 0;
3823
   parameter memory_file = "vl_ram.vmem";
3824
   generate if (init) begin : init_mem
3825
   initial
3826
     begin
3827
        $readmemh(memory_file, ram);
3828
     end
3829
   end
3830
   endgenerate
3831
 
3832 6 unneback
   always @ (posedge clk_a)
3833
   if (we_a)
3834
     ram[adr_a] <= d_a;
3835
   always @ (posedge clk_b)
3836
   adr_b_reg <= adr_b;
3837
   assign q_b = ram[adr_b_reg];
3838 40 unneback
 
3839 6 unneback
endmodule
3840 40 unneback
`endif
3841 6 unneback
 
3842 40 unneback
`ifdef DPRAM_2R1W
3843
`define MODULE dpram_2r1w
3844
module `BASE`MODULE ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
3845
`undef MODULE
3846
 
3847 6 unneback
   parameter data_width = 32;
3848
   parameter addr_width = 8;
3849 75 unneback
   parameter mem_size = 1<<addr_width;
3850 6 unneback
   input [(data_width-1):0]      d_a;
3851
   input [(addr_width-1):0]       adr_a;
3852
   input [(addr_width-1):0]       adr_b;
3853
   input                         we_a;
3854
   output [(data_width-1):0]      q_b;
3855
   output reg [(data_width-1):0] q_a;
3856
   input                         clk_a, clk_b;
3857
   reg [(data_width-1):0]         q_b;
3858 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0] `SYN;
3859 7 unneback
 
3860
   parameter init = 0;
3861
   parameter memory_file = "vl_ram.vmem";
3862
   generate if (init) begin : init_mem
3863
   initial
3864
     begin
3865
        $readmemh(memory_file, ram);
3866
     end
3867
   end
3868
   endgenerate
3869
 
3870 6 unneback
   always @ (posedge clk_a)
3871
     begin
3872
        q_a <= ram[adr_a];
3873
        if (we_a)
3874
             ram[adr_a] <= d_a;
3875
     end
3876
   always @ (posedge clk_b)
3877
          q_b <= ram[adr_b];
3878
endmodule
3879 40 unneback
`endif
3880 6 unneback
 
3881 40 unneback
`ifdef DPRAM_2R2W
3882
`define MODULE dpram_2r2w
3883
module `BASE`MODULE ( d_a, q_a, adr_a, we_a, clk_a, d_b, q_b, adr_b, we_b, clk_b );
3884
`undef MODULE
3885
 
3886 6 unneback
   parameter data_width = 32;
3887
   parameter addr_width = 8;
3888 75 unneback
   parameter mem_size = 1<<addr_width;
3889 6 unneback
   input [(data_width-1):0]      d_a;
3890
   input [(addr_width-1):0]       adr_a;
3891
   input [(addr_width-1):0]       adr_b;
3892
   input                         we_a;
3893
   output [(data_width-1):0]      q_b;
3894
   input [(data_width-1):0]       d_b;
3895
   output reg [(data_width-1):0] q_a;
3896
   input                         we_b;
3897
   input                         clk_a, clk_b;
3898
   reg [(data_width-1):0]         q_b;
3899 75 unneback
   reg [data_width-1:0] ram [mem_size-1:0] `SYN;
3900 7 unneback
 
3901
   parameter init = 0;
3902
   parameter memory_file = "vl_ram.vmem";
3903
   generate if (init) begin : init_mem
3904
   initial
3905
     begin
3906
        $readmemh(memory_file, ram);
3907
     end
3908
   end
3909
   endgenerate
3910
 
3911 6 unneback
   always @ (posedge clk_a)
3912
     begin
3913
        q_a <= ram[adr_a];
3914
        if (we_a)
3915
             ram[adr_a] <= d_a;
3916
     end
3917
   always @ (posedge clk_b)
3918
     begin
3919
        q_b <= ram[adr_b];
3920
        if (we_b)
3921
          ram[adr_b] <= d_b;
3922
     end
3923
endmodule
3924 40 unneback
`endif
3925 6 unneback
 
3926 83 unneback
`ifdef DPRAM_MIXED_WIDTH_2R2W
3927
`define MODULE dpram_mixed_width_2r2w
3928
module `BASE`MODULE ( d_a, q_a, adr_a, be_a, we_a, clk_a, d_b, q_b, adr_b, be_b, we_b, clk_b );
3929
`undef MODULE
3930
   parameter data_width = 32;
3931
   parameter addr_width = 8;
3932
   parameter data_width_ratio = 2;
3933
   parameter b_data_width = data_width * data_width_ratio;
3934
   parameter b_addr_width = addr_width ;
3935
endmodule
3936
`endif
3937
 
3938 75 unneback
`ifdef DPRAM_BE_2R2W
3939
`define MODULE dpram_be_2r2w
3940
module `BASE`MODULE ( d_a, q_a, adr_a, be_a, we_a, clk_a, d_b, q_b, adr_b, be_b, we_b, clk_b );
3941
`undef MODULE
3942
 
3943
   parameter a_data_width = 32;
3944
   parameter a_addr_width = 8;
3945
   parameter b_data_width = 64;
3946
   parameter b_addr_width = 7;
3947
   //parameter mem_size = (a_addr_width>b_addr_width) ? (1<<a_addr_width) : (1<<b_addr_width);
3948
   parameter mem_size = 1024;
3949
   input [(a_data_width-1):0]      d_a;
3950
   input [(a_addr_width-1):0]     adr_a;
3951
   input [(b_addr_width-1):0]     adr_b;
3952
   input [(a_data_width/4-1):0]    be_a;
3953
   input                         we_a;
3954
   output [(b_data_width-1):0]    q_b;
3955
   input [(b_data_width-1):0]     d_b;
3956
   output reg [(a_data_width-1):0] q_a;
3957
   input [(b_data_width/4-1):0]    be_b;
3958
   input                         we_b;
3959
   input                         clk_a, clk_b;
3960
   reg [(b_data_width-1):0]       q_b;
3961
 
3962
generate
3963
if (a_data_width==32 & b_data_width==64) begin : inst32to64
3964
 
3965 77 unneback
    wire [63:0] tmp;
3966 75 unneback
    `define MODULE dpram_2r2w
3967
    `BASE`MODULE
3968
    # (.data_width(8), .addr_width(b_addr_width-3))
3969
    ram0 (
3970
        .d_a(d_a[7:0]),
3971
        .q_a(tmp[7:0]),
3972
        .adr_a(adr_a[a_addr_width-3-1:0]),
3973
        .we_a(we_a & be_a[0] & !adr_a[0]),
3974
        .clk_a(clk_a),
3975
        .d_b(d_b[7:0]),
3976
        .q_b(q_b[7:0]),
3977
        .adr_b(adr_b[b_addr_width-3-1:0]),
3978
        .we_b(we_b),
3979
        .clk_b(clk_b) );
3980
    `BASE`MODULE
3981
    # (.data_width(8), .addr_width(b_addr_width-3))
3982
    ram1 (
3983
        .d_a(d_a[7:0]),
3984
        .q_a(tmp[7:0]),
3985
        .adr_a(adr_a[a_addr_width-3-1:0]),
3986
        .we_a(we_a),
3987
        .clk_a(clk_a),
3988
        .d_b(d_b[7:0]),
3989
        .q_b(q_b[7:0]),
3990
        .adr_b(adr_b[b_addr_width-3-1:0]),
3991
        .we_b(we_b),
3992
        .clk_b(clk_b) );
3993
    `BASE`MODULE
3994
    # (.data_width(8), .addr_width(b_addr_width-3))
3995
    ram2 (
3996
        .d_a(d_a[15:8]),
3997
        .q_a(tmp[7:0]),
3998
        .adr_a(adr_a[a_addr_width-3-1:0]),
3999
        .we_a(we_a),
4000
        .clk_a(clk_a),
4001
        .d_b(d_b[7:0]),
4002
        .q_b(q_b[7:0]),
4003
        .adr_b(adr_b[b_addr_width-3-1:0]),
4004
        .we_b(we_b),
4005
        .clk_b(clk_b) );
4006
    `BASE`MODULE
4007
    # (.data_width(8), .addr_width(b_addr_width-3))
4008
    ram3 (
4009
        .d_a(d_a[15:8]),
4010
        .q_a(tmp[7:0]),
4011
        .adr_a(adr_a[a_addr_width-3-1:0]),
4012
        .we_a(we_a),
4013
        .clk_a(clk_a),
4014
        .d_b(d_b[7:0]),
4015
        .q_b(q_b[7:0]),
4016
        .adr_b(adr_b[b_addr_width-3-1:0]),
4017
        .we_b(we_b),
4018
        .clk_b(clk_b) );
4019
    `BASE`MODULE
4020
    # (.data_width(8), .addr_width(b_addr_width-3))
4021
    ram4 (
4022
        .d_a(d_a[23:16]),
4023
        .q_a(tmp[7:0]),
4024
        .adr_a(adr_a[a_addr_width-3-1:0]),
4025
        .we_a(we_a),
4026
        .clk_a(clk_a),
4027
        .d_b(d_b[7:0]),
4028
        .q_b(q_b[7:0]),
4029
        .adr_b(adr_b[b_addr_width-3-1:0]),
4030
        .we_b(we_b),
4031
        .clk_b(clk_b) );
4032
    `BASE`MODULE
4033
    # (.data_width(8), .addr_width(b_addr_width-3))
4034
    ram5 (
4035
        .d_a(d_a[23:16]),
4036
        .q_a(tmp[7:0]),
4037
        .adr_a(adr_a[a_addr_width-3-1:0]),
4038
        .we_a(we_a),
4039
        .clk_a(clk_a),
4040
        .d_b(d_b[7:0]),
4041
        .q_b(q_b[7:0]),
4042
        .adr_b(adr_b[b_addr_width-3-1:0]),
4043
        .we_b(we_b),
4044
        .clk_b(clk_b) );
4045
    `BASE`MODULE
4046
    # (.data_width(8), .addr_width(b_addr_width-3))
4047
    ram6 (
4048
        .d_a(d_a[31:24]),
4049
        .q_a(tmp[7:0]),
4050
        .adr_a(adr_a[a_addr_width-3-1:0]),
4051
        .we_a(we_a),
4052
        .clk_a(clk_a),
4053
        .d_b(d_b[7:0]),
4054
        .q_b(q_b[7:0]),
4055
        .adr_b(adr_b[b_addr_width-3-1:0]),
4056
        .we_b(we_b),
4057
        .clk_b(clk_b) );
4058
    `BASE`MODULE
4059
    # (.data_width(8), .addr_width(b_addr_width-3))
4060
    ram7 (
4061
        .d_a(d_a[31:24]),
4062
        .q_a(tmp[7:0]),
4063
        .adr_a(adr_a[a_addr_width-3-1:0]),
4064
        .we_a(we_a),
4065
        .clk_a(clk_a),
4066
        .d_b(d_b[7:0]),
4067
        .q_b(q_b[7:0]),
4068
        .adr_b(adr_b[b_addr_width-3-1:0]),
4069
        .we_b(we_b),
4070
        .clk_b(clk_b) );
4071
`undef MODULE
4072
/*
4073
   reg [7:0] ram0 [mem_size/8-1:0];
4074
   wire [7:0] wea, web;
4075
   assign wea = we_a & be_a[0];
4076
   assign web = we_b & be_b[0];
4077
   always @ (posedge clk_a)
4078
    if (wea)
4079
        ram0[adr_a] <= d_a[7:0];
4080
    always @ (posedge clk_a)
4081
        q_a[7:0] <= ram0[adr_a];
4082
   always @ (posedge clk_a)
4083
    if (web)
4084
        ram0[adr_b] <= d_b[7:0];
4085
    always @ (posedge clk_b)
4086
        q_b[7:0] <= ram0[adr_b];
4087
*/
4088
end
4089
endgenerate
4090
/*
4091
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_rama
4092
      always @ (posedge clk_a)
4093
      if (we_a & be_a[i])
4094
        ram[adr_a][(i+1)*8-1:i*8] <= d_a[(i+1)*8-1:i*8];
4095
   end
4096
   endgenerate
4097
 
4098
   always @ (posedge clk_a)
4099
      q_a <= ram[adr_a];
4100
 
4101
   genvar i;
4102
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_ramb
4103
      always @ (posedge clk_a)
4104
      if (we_b & be_b[i])
4105
        ram[adr_b][(i+1)*8-1:i*8] <= d_b[(i+1)*8-1:i*8];
4106
   end
4107
   endgenerate
4108
 
4109
   always @ (posedge clk_b)
4110
      q_b <= ram[adr_b];
4111
*/
4112
/*
4113
   always @ (posedge clk_a)
4114
     begin
4115
        q_a <= ram[adr_a];
4116
        if (we_a)
4117
             ram[adr_a] <= d_a;
4118
     end
4119
   always @ (posedge clk_b)
4120
     begin
4121
        q_b <= ram[adr_b];
4122
        if (we_b)
4123
          ram[adr_b] <= d_b;
4124
     end
4125
*/
4126
endmodule
4127
`endif
4128
 
4129 6 unneback
// Content addresable memory, CAM
4130
 
4131 40 unneback
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
4132 6 unneback
// FIFO
4133 40 unneback
`define MODULE fifo_1r1w_fill_level_sync
4134
module `BASE`MODULE (
4135
`undef MODULE
4136 25 unneback
    d, wr, fifo_full,
4137
    q, rd, fifo_empty,
4138
    fill_level,
4139
    clk, rst
4140
    );
4141
 
4142
parameter data_width = 18;
4143
parameter addr_width = 4;
4144 6 unneback
 
4145 25 unneback
// write side
4146
input  [data_width-1:0] d;
4147
input                   wr;
4148
output                  fifo_full;
4149
// read side
4150
output [data_width-1:0] q;
4151
input                   rd;
4152
output                  fifo_empty;
4153
// common
4154
output [addr_width:0]   fill_level;
4155
input rst, clk;
4156
 
4157
wire [addr_width:1] wadr, radr;
4158
 
4159 40 unneback
`define MODULE cnt_bin_ce
4160
`BASE`MODULE
4161 25 unneback
    # ( .length(addr_width))
4162
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
4163 40 unneback
`BASE`MODULE
4164 25 unneback
    # (.length(addr_width))
4165
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
4166 40 unneback
`undef MODULE
4167 25 unneback
 
4168 40 unneback
`define MODULE dpram_1r1w
4169
`BASE`MODULE
4170 25 unneback
    # (.data_width(data_width), .addr_width(addr_width))
4171
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
4172 40 unneback
`undef MODULE
4173 25 unneback
 
4174 40 unneback
`define MODULE cnt_bin_ce_rew_q_zq_l1
4175
`BASE`MODULE
4176 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
4177 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
4178 40 unneback
`undef MODULE
4179 25 unneback
endmodule
4180 40 unneback
`endif
4181 25 unneback
 
4182 40 unneback
`ifdef FIFO_2R2W_SYNC_SIMPLEX
4183 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
4184
// RAM is supposed to be larger than the two FIFOs
4185
// LFSR counters used adr pointers
4186 40 unneback
`define MODULE fifo_2r2w_sync_simplex
4187
module `BASE`MODULE (
4188
`undef MODULE
4189 27 unneback
    // a side
4190
    a_d, a_wr, a_fifo_full,
4191
    a_q, a_rd, a_fifo_empty,
4192
    a_fill_level,
4193
    // b side
4194
    b_d, b_wr, b_fifo_full,
4195
    b_q, b_rd, b_fifo_empty,
4196
    b_fill_level,
4197
    // common
4198
    clk, rst
4199
    );
4200
parameter data_width = 8;
4201
parameter addr_width = 5;
4202
parameter fifo_full_level = (1<<addr_width)-1;
4203
 
4204
// a side
4205
input  [data_width-1:0] a_d;
4206
input                   a_wr;
4207
output                  a_fifo_full;
4208
output [data_width-1:0] a_q;
4209
input                   a_rd;
4210
output                  a_fifo_empty;
4211
output [addr_width-1:0] a_fill_level;
4212
 
4213
// b side
4214
input  [data_width-1:0] b_d;
4215
input                   b_wr;
4216
output                  b_fifo_full;
4217
output [data_width-1:0] b_q;
4218
input                   b_rd;
4219
output                  b_fifo_empty;
4220
output [addr_width-1:0] b_fill_level;
4221
 
4222
input                   clk;
4223
input                   rst;
4224
 
4225
// adr_gen
4226
wire [addr_width:1] a_wadr, a_radr;
4227
wire [addr_width:1] b_wadr, b_radr;
4228
// dpram
4229
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
4230
 
4231 40 unneback
`define MODULE cnt_lfsr_ce
4232
`BASE`MODULE
4233 27 unneback
    # ( .length(addr_width))
4234
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
4235
 
4236 40 unneback
`BASE`MODULE
4237 27 unneback
    # (.length(addr_width))
4238
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
4239
 
4240 40 unneback
`BASE`MODULE
4241 27 unneback
    # ( .length(addr_width))
4242
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
4243
 
4244 40 unneback
`BASE`MODULE
4245 27 unneback
    # (.length(addr_width))
4246
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
4247 40 unneback
`undef MODULE
4248 27 unneback
 
4249
// mux read or write adr to DPRAM
4250
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
4251
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
4252
 
4253 40 unneback
`define MODULE dpram_2r2w
4254
`BASE`MODULE
4255 27 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
4256
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
4257
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
4258 40 unneback
`undef MODULE
4259
 
4260
`define MODULE cnt_bin_ce_rew_zq_l1
4261
`BASE`MODULE
4262 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
4263 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));
4264
 
4265 40 unneback
`BASE`MODULE
4266 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
4267 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));
4268 40 unneback
`undef MODULE
4269 27 unneback
 
4270
endmodule
4271 40 unneback
`endif
4272 27 unneback
 
4273 40 unneback
`ifdef FIFO_CMP_ASYNC
4274
`define MODULE fifo_cmp_async
4275
module `BASE`MODULE ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
4276
`undef MODULE
4277 6 unneback
 
4278 11 unneback
   parameter addr_width = 4;
4279
   parameter N = addr_width-1;
4280 6 unneback
 
4281
   parameter Q1 = 2'b00;
4282
   parameter Q2 = 2'b01;
4283
   parameter Q3 = 2'b11;
4284
   parameter Q4 = 2'b10;
4285
 
4286
   parameter going_empty = 1'b0;
4287
   parameter going_full  = 1'b1;
4288
 
4289
   input [N:0]  wptr, rptr;
4290 14 unneback
   output       fifo_empty;
4291 6 unneback
   output       fifo_full;
4292
   input        wclk, rclk, rst;
4293
 
4294
`ifndef GENERATE_DIRECTION_AS_LATCH
4295
   wire direction;
4296
`endif
4297
`ifdef GENERATE_DIRECTION_AS_LATCH
4298
   reg direction;
4299
`endif
4300
   reg  direction_set, direction_clr;
4301
 
4302
   wire async_empty, async_full;
4303
   wire fifo_full2;
4304 14 unneback
   wire fifo_empty2;
4305 6 unneback
 
4306
   // direction_set
4307
   always @ (wptr[N:N-1] or rptr[N:N-1])
4308
     case ({wptr[N:N-1],rptr[N:N-1]})
4309
       {Q1,Q2} : direction_set <= 1'b1;
4310
       {Q2,Q3} : direction_set <= 1'b1;
4311
       {Q3,Q4} : direction_set <= 1'b1;
4312
       {Q4,Q1} : direction_set <= 1'b1;
4313
       default : direction_set <= 1'b0;
4314
     endcase
4315
 
4316
   // direction_clear
4317
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
4318
     if (rst)
4319
       direction_clr <= 1'b1;
4320
     else
4321
       case ({wptr[N:N-1],rptr[N:N-1]})
4322
         {Q2,Q1} : direction_clr <= 1'b1;
4323
         {Q3,Q2} : direction_clr <= 1'b1;
4324
         {Q4,Q3} : direction_clr <= 1'b1;
4325
         {Q1,Q4} : direction_clr <= 1'b1;
4326
         default : direction_clr <= 1'b0;
4327
       endcase
4328
 
4329 40 unneback
`define MODULE dff_sr
4330 6 unneback
`ifndef GENERATE_DIRECTION_AS_LATCH
4331 40 unneback
    `BASE`MODULE dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
4332 6 unneback
`endif
4333
 
4334
`ifdef GENERATE_DIRECTION_AS_LATCH
4335
   always @ (posedge direction_set or posedge direction_clr)
4336
     if (direction_clr)
4337
       direction <= going_empty;
4338
     else
4339
       direction <= going_full;
4340
`endif
4341
 
4342
   assign async_empty = (wptr == rptr) && (direction==going_empty);
4343
   assign async_full  = (wptr == rptr) && (direction==going_full);
4344
 
4345 40 unneback
    `BASE`MODULE dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
4346
    `BASE`MODULE dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
4347
`undef MODULE
4348 6 unneback
 
4349
/*
4350
   always @ (posedge wclk or posedge rst or posedge async_full)
4351
     if (rst)
4352
       {fifo_full, fifo_full2} <= 2'b00;
4353
     else if (async_full)
4354
       {fifo_full, fifo_full2} <= 2'b11;
4355
     else
4356
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
4357
*/
4358 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
4359 6 unneback
     if (async_empty)
4360
       {fifo_empty, fifo_empty2} <= 2'b11;
4361
     else
4362 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
4363 40 unneback
`define MODULE dff
4364
    `BASE`MODULE # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
4365
    `BASE`MODULE # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
4366
`undef MODULE
4367 27 unneback
endmodule // async_compb
4368 40 unneback
`endif
4369 6 unneback
 
4370 40 unneback
`ifdef FIFO_1R1W_ASYNC
4371
`define MODULE fifo_1r1w_async
4372
module `BASE`MODULE (
4373
`undef MODULE
4374 6 unneback
    d, wr, fifo_full, wr_clk, wr_rst,
4375
    q, rd, fifo_empty, rd_clk, rd_rst
4376
    );
4377
 
4378
parameter data_width = 18;
4379
parameter addr_width = 4;
4380
 
4381
// write side
4382
input  [data_width-1:0] d;
4383
input                   wr;
4384
output                  fifo_full;
4385
input                   wr_clk;
4386
input                   wr_rst;
4387
// read side
4388
output [data_width-1:0] q;
4389
input                   rd;
4390
output                  fifo_empty;
4391
input                   rd_clk;
4392
input                   rd_rst;
4393
 
4394
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
4395 23 unneback
 
4396 40 unneback
`define MODULE cnt_gray_ce_bin
4397
`BASE`MODULE
4398 6 unneback
    # ( .length(addr_width))
4399
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
4400
 
4401 40 unneback
`BASE`MODULE
4402 6 unneback
    # (.length(addr_width))
4403 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
4404 40 unneback
`undef MODULE
4405 6 unneback
 
4406 40 unneback
`define MODULE dpram_1r1w
4407
`BASE`MODULE
4408 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
4409
    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));
4410 40 unneback
`undef MODULE
4411 6 unneback
 
4412 40 unneback
`define MODULE fifo_cmp_async
4413
`BASE`MODULE
4414 6 unneback
    # (.addr_width(addr_width))
4415
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
4416 40 unneback
`undef MODULE
4417 6 unneback
 
4418
endmodule
4419 40 unneback
`endif
4420 6 unneback
 
4421 40 unneback
`ifdef FIFO_2R2W_ASYNC
4422
`define MODULE fifo_2r2w_async
4423
module `BASE`MODULE (
4424
`undef MODULE
4425 6 unneback
    // a side
4426
    a_d, a_wr, a_fifo_full,
4427
    a_q, a_rd, a_fifo_empty,
4428
    a_clk, a_rst,
4429
    // b side
4430
    b_d, b_wr, b_fifo_full,
4431
    b_q, b_rd, b_fifo_empty,
4432
    b_clk, b_rst
4433
    );
4434
 
4435
parameter data_width = 18;
4436
parameter addr_width = 4;
4437
 
4438
// a side
4439
input  [data_width-1:0] a_d;
4440
input                   a_wr;
4441
output                  a_fifo_full;
4442
output [data_width-1:0] a_q;
4443
input                   a_rd;
4444
output                  a_fifo_empty;
4445
input                   a_clk;
4446
input                   a_rst;
4447
 
4448
// b side
4449
input  [data_width-1:0] b_d;
4450
input                   b_wr;
4451
output                  b_fifo_full;
4452
output [data_width-1:0] b_q;
4453
input                   b_rd;
4454
output                  b_fifo_empty;
4455
input                   b_clk;
4456
input                   b_rst;
4457
 
4458 40 unneback
`define MODULE fifo_1r1w_async
4459
`BASE`MODULE # (.data_width(data_width), .addr_width(addr_width))
4460 6 unneback
vl_fifo_1r1w_async_a (
4461
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
4462
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
4463
    );
4464
 
4465 40 unneback
`BASE`MODULE # (.data_width(data_width), .addr_width(addr_width))
4466 6 unneback
vl_fifo_1r1w_async_b (
4467
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
4468
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
4469
    );
4470 40 unneback
`undef MODULE
4471
 
4472 6 unneback
endmodule
4473 40 unneback
`endif
4474 6 unneback
 
4475 40 unneback
`ifdef FIFO_2R2W_ASYNC_SIMPLEX
4476
`define MODULE fifo_2r2w_async_simplex
4477
module `BASE`MODULE (
4478
`undef MODULE
4479 6 unneback
    // a side
4480
    a_d, a_wr, a_fifo_full,
4481
    a_q, a_rd, a_fifo_empty,
4482
    a_clk, a_rst,
4483
    // b side
4484
    b_d, b_wr, b_fifo_full,
4485
    b_q, b_rd, b_fifo_empty,
4486
    b_clk, b_rst
4487
    );
4488
 
4489
parameter data_width = 18;
4490
parameter addr_width = 4;
4491
 
4492
// a side
4493
input  [data_width-1:0] a_d;
4494
input                   a_wr;
4495
output                  a_fifo_full;
4496
output [data_width-1:0] a_q;
4497
input                   a_rd;
4498
output                  a_fifo_empty;
4499
input                   a_clk;
4500
input                   a_rst;
4501
 
4502
// b side
4503
input  [data_width-1:0] b_d;
4504
input                   b_wr;
4505
output                  b_fifo_full;
4506
output [data_width-1:0] b_q;
4507
input                   b_rd;
4508
output                  b_fifo_empty;
4509
input                   b_clk;
4510
input                   b_rst;
4511
 
4512
// adr_gen
4513
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
4514
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
4515
// dpram
4516
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
4517
 
4518 40 unneback
`define MODULE cnt_gray_ce_bin
4519
`BASE`MODULE
4520 6 unneback
    # ( .length(addr_width))
4521
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
4522
 
4523 40 unneback
`BASE`MODULE
4524 6 unneback
    # (.length(addr_width))
4525
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
4526
 
4527 40 unneback
`BASE`MODULE
4528 6 unneback
    # ( .length(addr_width))
4529
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
4530
 
4531 40 unneback
`BASE`MODULE
4532 6 unneback
    # (.length(addr_width))
4533
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
4534 40 unneback
`undef MODULE
4535 6 unneback
 
4536
// mux read or write adr to DPRAM
4537
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
4538
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
4539
 
4540 40 unneback
`define MODULE dpram_2r2w
4541
`BASE`MODULE
4542 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
4543
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
4544
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
4545 40 unneback
`undef MODULE
4546 6 unneback
 
4547 40 unneback
`define MODULE fifo_cmp_async
4548
`BASE`MODULE
4549 6 unneback
    # (.addr_width(addr_width))
4550
    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) );
4551
 
4552 40 unneback
`BASE`MODULE
4553 6 unneback
    # (.addr_width(addr_width))
4554
    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) );
4555 40 unneback
`undef MODULE
4556 6 unneback
 
4557
endmodule
4558 40 unneback
`endif
4559 48 unneback
 
4560
`ifdef REG_FILE
4561
`define MODULE reg_file
4562
module `BASE`MODULE (
4563
`undef MODULE
4564
    a1, a2, a3, wd3, we3, rd1, rd2, clk
4565
);
4566
parameter data_width = 32;
4567
parameter addr_width = 5;
4568
input [addr_width-1:0] a1, a2, a3;
4569
input [data_width-1:0] wd3;
4570
input we3;
4571
output [data_width-1:0] rd1, rd2;
4572
input clk;
4573
 
4574
`ifdef ACTEL
4575
reg [data_width-1:0] wd3_reg;
4576
reg [addr_width-1:0] a1_reg, a2_reg, a3_reg;
4577
reg we3_reg;
4578
reg [data_width-1:0] ram1 [(1<<addr_width)-1:0] `SYN;
4579
reg [data_width-1:0] ram2 [(1<<addr_width)-1:0] `SYN;
4580
always @ (posedge clk or posedge rst)
4581
if (rst)
4582
    {wd3_reg, a3_reg, we3_reg} <= {(data_width+addr_width+1){1'b0}};
4583
else
4584
    {wd3_reg, a3_reg, we3_reg} <= {wd3,a3,wd3};
4585
 
4586
    always @ (negedge clk)
4587
    if (we3_reg)
4588
        ram1[a3_reg] <= wd3;
4589
    always @ (posedge clk)
4590
        a1_reg <= a1;
4591
    assign rd1 = ram1[a1_reg];
4592
 
4593
    always @ (negedge clk)
4594
    if (we3_reg)
4595
        ram2[a3_reg] <= wd3;
4596
    always @ (posedge clk)
4597
        a2_reg <= a2;
4598
    assign rd2 = ram2[a2_reg];
4599
 
4600
`else
4601
 
4602
`define MODULE dpram_1r1w
4603
`BASE`MODULE
4604
    # ( .data_width(data_width), .addr_width(addr_width))
4605
    ram1 (
4606
        .d_a(wd3),
4607
        .adr_a(a3),
4608
        .we_a(we3),
4609
        .clk_a(clk),
4610
        .q_b(rd1),
4611
        .adr_b(a1),
4612
        .clk_b(clk) );
4613
 
4614
`BASE`MODULE
4615
    # ( .data_width(data_width), .addr_width(addr_width))
4616
    ram2 (
4617
        .d_a(wd3),
4618
        .adr_a(a3),
4619
        .we_a(we3),
4620
        .clk_a(clk),
4621
        .q_b(rd2),
4622
        .adr_b(a2),
4623
        .clk_b(clk) );
4624
`undef MODULE
4625
 
4626
`endif
4627
 
4628
endmodule
4629
`endif
4630 12 unneback
//////////////////////////////////////////////////////////////////////
4631
////                                                              ////
4632
////  Versatile library, wishbone stuff                           ////
4633
////                                                              ////
4634
////  Description                                                 ////
4635
////  Wishbone compliant modules                                  ////
4636
////                                                              ////
4637
////                                                              ////
4638
////  To Do:                                                      ////
4639
////   -                                                          ////
4640
////                                                              ////
4641
////  Author(s):                                                  ////
4642
////      - Michael Unneback, unneback@opencores.org              ////
4643
////        ORSoC AB                                              ////
4644
////                                                              ////
4645
//////////////////////////////////////////////////////////////////////
4646
////                                                              ////
4647
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
4648
////                                                              ////
4649
//// This source file may be used and distributed without         ////
4650
//// restriction provided that this copyright statement is not    ////
4651
//// removed from the file and that any derivative work contains  ////
4652
//// the original copyright notice and the associated disclaimer. ////
4653
////                                                              ////
4654
//// This source file is free software; you can redistribute it   ////
4655
//// and/or modify it under the terms of the GNU Lesser General   ////
4656
//// Public License as published by the Free Software Foundation; ////
4657
//// either version 2.1 of the License, or (at your option) any   ////
4658
//// later version.                                               ////
4659
////                                                              ////
4660
//// This source is distributed in the hope that it will be       ////
4661
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
4662
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
4663
//// PURPOSE.  See the GNU Lesser General Public License for more ////
4664
//// details.                                                     ////
4665
////                                                              ////
4666
//// You should have received a copy of the GNU Lesser General    ////
4667
//// Public License along with this source; if not, download it   ////
4668
//// from http://www.opencores.org/lgpl.shtml                     ////
4669
////                                                              ////
4670
//////////////////////////////////////////////////////////////////////
4671
 
4672 75 unneback
`ifdef WB_ADR_INC
4673
// async wb3 - wb3 bridge
4674
`timescale 1ns/1ns
4675
`define MODULE wb_adr_inc
4676 85 unneback
module `BASE`MODULE ( cyc_i, stb_i, cti_i, bte_i, adr_i, we_i, ack_o, adr_o, clk, rst);
4677 75 unneback
`undef MODULE
4678 83 unneback
parameter adr_width = 10;
4679
parameter max_burst_width = 4;
4680 85 unneback
input cyc_i, stb_i, we_i;
4681 83 unneback
input [2:0] cti_i;
4682
input [1:0] bte_i;
4683
input [adr_width-1:0] adr_i;
4684
output [adr_width-1:0] adr_o;
4685
output ack_o;
4686
input clk, rst;
4687 75 unneback
 
4688 83 unneback
reg [adr_width-1:0] adr;
4689 90 unneback
wire [max_burst_width-1:0] to_adr;
4690
 
4691 83 unneback
generate
4692
if (max_burst_width==0) begin : inst_0
4693
    reg ack_o;
4694
    assign adr_o = adr_i;
4695 75 unneback
    always @ (posedge clk or posedge rst)
4696 83 unneback
    if (rst)
4697
        ack_o <= 1'b0;
4698
    else
4699
        ack_o <= cyc_i & stb_i & !ack_o;
4700
end else begin
4701
 
4702
    reg [1:0] last_cycle;
4703
    localparam idle = 2'b00;
4704
    localparam cyc  = 2'b01;
4705
    localparam ws   = 2'b10;
4706
    localparam eoc  = 2'b11;
4707
    always @ (posedge clk or posedge rst)
4708
    if (rst)
4709
        last_cycle <= idle;
4710
    else
4711
        last_cycle <= (!cyc_i) ? idle :
4712
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? eoc :
4713
                      (cyc_i & !stb_i) ? ws :
4714
                      cyc;
4715
    assign to_adr = (last_cycle==idle | last_cycle==eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
4716 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
4717
                                        (last_cycle==idle | last_cycle==eoc) ? adr_i[max_burst_width-1:0] :
4718
                                        adr[max_burst_width-1:0];
4719 90 unneback
    assign ack_o = (last_cycle==cyc | last_cycle==ws) & stb_i;
4720 83 unneback
end
4721
endgenerate
4722
 
4723
generate
4724
if (max_burst_width==2) begin : inst_2
4725
    always @ (posedge clk or posedge rst)
4726
    if (rst)
4727
        adr <= 2'h0;
4728
    else
4729
        if (cyc_i & stb_i)
4730
            adr[1:0] <= to_adr[1:0] + 2'd1;
4731 75 unneback
        else
4732 83 unneback
            adr <= to_adr[1:0];
4733
end
4734
endgenerate
4735
 
4736
generate
4737
if (max_burst_width==3) begin : inst_3
4738
    always @ (posedge clk or posedge rst)
4739
    if (rst)
4740
        adr <= 3'h0;
4741
    else
4742
        if (cyc_i & stb_i)
4743
            case (bte_i)
4744
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
4745
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
4746 75 unneback
            endcase
4747 83 unneback
        else
4748
            adr <= to_adr[2:0];
4749
end
4750
endgenerate
4751
 
4752
generate
4753
if (max_burst_width==4) begin : inst_4
4754
    always @ (posedge clk or posedge rst)
4755
    if (rst)
4756
        adr <= 4'h0;
4757
    else
4758
        if (cyc_i & stb_i)
4759
            case (bte_i)
4760
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
4761
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
4762
            default: adr[3:0] <= to_adr + 4'd1;
4763
            endcase
4764
        else
4765
            adr <= to_adr[3:0];
4766
end
4767
endgenerate
4768
 
4769
generate
4770
if (adr_width > max_burst_width) begin : pass_through
4771
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
4772
end
4773
endgenerate
4774
 
4775
endmodule
4776 75 unneback
`endif
4777
 
4778 40 unneback
`ifdef WB3WB3_BRIDGE
4779 12 unneback
// async wb3 - wb3 bridge
4780
`timescale 1ns/1ns
4781 40 unneback
`define MODULE wb3wb3_bridge
4782
module `BASE`MODULE (
4783
`undef MODULE
4784 12 unneback
        // wishbone slave side
4785
        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,
4786
        // wishbone master side
4787
        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);
4788
 
4789
input [31:0] wbs_dat_i;
4790
input [31:2] wbs_adr_i;
4791
input [3:0]  wbs_sel_i;
4792
input [1:0]  wbs_bte_i;
4793
input [2:0]  wbs_cti_i;
4794
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
4795
output [31:0] wbs_dat_o;
4796 14 unneback
output wbs_ack_o;
4797 12 unneback
input wbs_clk, wbs_rst;
4798
 
4799
output [31:0] wbm_dat_o;
4800
output reg [31:2] wbm_adr_o;
4801
output [3:0]  wbm_sel_o;
4802
output reg [1:0]  wbm_bte_o;
4803
output reg [2:0]  wbm_cti_o;
4804 14 unneback
output reg wbm_we_o;
4805
output wbm_cyc_o;
4806 12 unneback
output wbm_stb_o;
4807
input [31:0]  wbm_dat_i;
4808
input wbm_ack_i;
4809
input wbm_clk, wbm_rst;
4810
 
4811
parameter addr_width = 4;
4812
 
4813
// bte
4814
parameter linear       = 2'b00;
4815
parameter wrap4        = 2'b01;
4816
parameter wrap8        = 2'b10;
4817
parameter wrap16       = 2'b11;
4818
// cti
4819
parameter classic      = 3'b000;
4820
parameter incburst     = 3'b010;
4821
parameter endofburst   = 3'b111;
4822
 
4823
parameter wbs_adr  = 1'b0;
4824
parameter wbs_data = 1'b1;
4825
 
4826 33 unneback
parameter wbm_adr0      = 2'b00;
4827
parameter wbm_adr1      = 2'b01;
4828
parameter wbm_data      = 2'b10;
4829
parameter wbm_data_wait = 2'b11;
4830 12 unneback
 
4831
reg [1:0] wbs_bte_reg;
4832
reg wbs;
4833
wire wbs_eoc_alert, wbm_eoc_alert;
4834
reg wbs_eoc, wbm_eoc;
4835
reg [1:0] wbm;
4836
 
4837 14 unneback
wire [1:16] wbs_count, wbm_count;
4838 12 unneback
 
4839
wire [35:0] a_d, a_q, b_d, b_q;
4840
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
4841
reg a_rd_reg;
4842
wire b_rd_adr, b_rd_data;
4843 14 unneback
wire b_rd_data_reg;
4844
wire [35:0] temp;
4845 12 unneback
 
4846
`define WE 5
4847
`define BTE 4:3
4848
`define CTI 2:0
4849
 
4850
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]);
4851
always @ (posedge wbs_clk or posedge wbs_rst)
4852
if (wbs_rst)
4853
        wbs_eoc <= 1'b0;
4854
else
4855
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
4856 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
4857 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
4858
                wbs_eoc <= 1'b1;
4859
 
4860 40 unneback
`define MODULE cnt_shreg_ce_clear
4861
`BASE`MODULE # ( .length(16))
4862
`undef MODULE
4863 12 unneback
    cnt0 (
4864
        .cke(wbs_ack_o),
4865
        .clear(wbs_eoc),
4866
        .q(wbs_count),
4867
        .rst(wbs_rst),
4868
        .clk(wbs_clk));
4869
 
4870
always @ (posedge wbs_clk or posedge wbs_rst)
4871
if (wbs_rst)
4872
        wbs <= wbs_adr;
4873
else
4874 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
4875 12 unneback
                wbs <= wbs_data;
4876
        else if (wbs_eoc & wbs_ack_o)
4877
                wbs <= wbs_adr;
4878
 
4879
// wbs FIFO
4880 75 unneback
assign a_d = (wbs==wbs_adr) ? {wbs_adr_i[31:2],wbs_we_i,((wbs_cti_i==3'b111) ? {2'b00,3'b000} : {wbs_bte_i,wbs_cti_i})} : {wbs_dat_i,wbs_sel_i};
4881
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
4882 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
4883
              1'b0;
4884
assign a_rd = !a_fifo_empty;
4885
always @ (posedge wbs_clk or posedge wbs_rst)
4886
if (wbs_rst)
4887
        a_rd_reg <= 1'b0;
4888
else
4889
        a_rd_reg <= a_rd;
4890
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
4891
 
4892
assign wbs_dat_o = a_q[35:4];
4893
 
4894
always @ (posedge wbs_clk or posedge wbs_rst)
4895
if (wbs_rst)
4896 13 unneback
        wbs_bte_reg <= 2'b00;
4897 12 unneback
else
4898 13 unneback
        wbs_bte_reg <= wbs_bte_i;
4899 12 unneback
 
4900
// wbm FIFO
4901
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]);
4902
always @ (posedge wbm_clk or posedge wbm_rst)
4903
if (wbm_rst)
4904
        wbm_eoc <= 1'b0;
4905
else
4906
        if (wbm==wbm_adr0 & !b_fifo_empty)
4907
                wbm_eoc <= b_q[`BTE] == linear;
4908
        else if (wbm_eoc_alert & wbm_ack_i)
4909
                wbm_eoc <= 1'b1;
4910
 
4911
always @ (posedge wbm_clk or posedge wbm_rst)
4912
if (wbm_rst)
4913
        wbm <= wbm_adr0;
4914
else
4915 33 unneback
/*
4916 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
4917
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
4918
        (wbm==wbm_adr1 & !wbm_we_o) |
4919
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
4920
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
4921 33 unneback
*/
4922
    case (wbm)
4923
    wbm_adr0:
4924
        if (!b_fifo_empty)
4925
            wbm <= wbm_adr1;
4926
    wbm_adr1:
4927
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
4928
            wbm <= wbm_data;
4929
    wbm_data:
4930
        if (wbm_ack_i & wbm_eoc)
4931
            wbm <= wbm_adr0;
4932
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
4933
            wbm <= wbm_data_wait;
4934
    wbm_data_wait:
4935
        if (!b_fifo_empty)
4936
            wbm <= wbm_data;
4937
    endcase
4938 12 unneback
 
4939
assign b_d = {wbm_dat_i,4'b1111};
4940
assign b_wr = !wbm_we_o & wbm_ack_i;
4941
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
4942
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
4943
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
4944 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
4945 12 unneback
                   1'b0;
4946
assign b_rd = b_rd_adr | b_rd_data;
4947
 
4948 40 unneback
`define MODULE dff
4949
`BASE`MODULE dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
4950
`undef MODULE
4951
`define MODULE dff_ce
4952
`BASE`MODULE # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
4953
`undef MODULE
4954 12 unneback
 
4955
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
4956
 
4957 40 unneback
`define MODULE cnt_shreg_ce_clear
4958 42 unneback
`BASE`MODULE # ( .length(16))
4959 40 unneback
`undef MODULE
4960 12 unneback
    cnt1 (
4961
        .cke(wbm_ack_i),
4962
        .clear(wbm_eoc),
4963
        .q(wbm_count),
4964
        .rst(wbm_rst),
4965
        .clk(wbm_clk));
4966
 
4967 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
4968
assign wbm_stb_o = (wbm==wbm_data);
4969 12 unneback
 
4970
always @ (posedge wbm_clk or posedge wbm_rst)
4971
if (wbm_rst)
4972
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
4973
else begin
4974
        if (wbm==wbm_adr0 & !b_fifo_empty)
4975
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
4976
        else if (wbm_eoc_alert & wbm_ack_i)
4977
                wbm_cti_o <= endofburst;
4978
end
4979
 
4980
//async_fifo_dw_simplex_top
4981 40 unneback
`define MODULE fifo_2r2w_async_simplex
4982
`BASE`MODULE
4983
`undef MODULE
4984 12 unneback
# ( .data_width(36), .addr_width(addr_width))
4985
fifo (
4986
    // a side
4987
    .a_d(a_d),
4988
    .a_wr(a_wr),
4989
    .a_fifo_full(a_fifo_full),
4990
    .a_q(a_q),
4991
    .a_rd(a_rd),
4992
    .a_fifo_empty(a_fifo_empty),
4993
    .a_clk(wbs_clk),
4994
    .a_rst(wbs_rst),
4995
    // b side
4996
    .b_d(b_d),
4997
    .b_wr(b_wr),
4998
    .b_fifo_full(b_fifo_full),
4999
    .b_q(b_q),
5000
    .b_rd(b_rd),
5001
    .b_fifo_empty(b_fifo_empty),
5002
    .b_clk(wbm_clk),
5003
    .b_rst(wbm_rst)
5004
    );
5005
 
5006
endmodule
5007 40 unneback
`undef WE
5008
`undef BTE
5009
`undef CTI
5010
`endif
5011 17 unneback
 
5012 75 unneback
`ifdef WB3AVALON_BRIDGE
5013
`define MODULE wb3avalon_bridge
5014
module `BASE`MODULE (
5015
`undef MODULE
5016
        // wishbone slave side
5017
        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,
5018 77 unneback
        // avalon master side
5019 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
5020
 
5021 85 unneback
parameter linewrapburst = 1'b0;
5022
 
5023 75 unneback
input [31:0] wbs_dat_i;
5024
input [31:2] wbs_adr_i;
5025
input [3:0]  wbs_sel_i;
5026
input [1:0]  wbs_bte_i;
5027
input [2:0]  wbs_cti_i;
5028 83 unneback
input wbs_we_i;
5029
input wbs_cyc_i;
5030
input wbs_stb_i;
5031 75 unneback
output [31:0] wbs_dat_o;
5032
output wbs_ack_o;
5033
input wbs_clk, wbs_rst;
5034
 
5035
input [31:0] readdata;
5036
output [31:0] writedata;
5037
output [31:2] address;
5038
output [3:0]  be;
5039
output write;
5040 81 unneback
output read;
5041 75 unneback
output beginbursttransfer;
5042
output [3:0] burstcount;
5043
input readdatavalid;
5044
input waitrequest;
5045
input clk;
5046
input rst;
5047
 
5048
wire [1:0] wbm_bte_o;
5049
wire [2:0] wbm_cti_o;
5050
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
5051
reg last_cyc;
5052 79 unneback
reg [3:0] counter;
5053 82 unneback
reg read_busy;
5054 75 unneback
 
5055
always @ (posedge clk or posedge rst)
5056
if (rst)
5057
    last_cyc <= 1'b0;
5058
else
5059
    last_cyc <= wbm_cyc_o;
5060
 
5061 79 unneback
always @ (posedge clk or posedge rst)
5062
if (rst)
5063 82 unneback
    read_busy <= 1'b0;
5064 79 unneback
else
5065 82 unneback
    if (read & !waitrequest)
5066
        read_busy <= 1'b1;
5067
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
5068
        read_busy <= 1'b0;
5069
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
5070 81 unneback
 
5071 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
5072
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
5073
                    (wbm_bte_o==2'b10) ? 4'd8 :
5074 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
5075
                    4'd1;
5076 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
5077 75 unneback
 
5078 79 unneback
always @ (posedge clk or posedge rst)
5079
if (rst) begin
5080
    counter <= 4'd0;
5081
end else
5082 80 unneback
    if (wbm_we_o) begin
5083
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
5084 85 unneback
            counter <= burstcount -4'd1;
5085 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
5086
            counter <= burstcount;
5087
        end else if (!waitrequest & wbm_stb_o) begin
5088
            counter <= counter - 4'd1;
5089
        end
5090 82 unneback
    end
5091 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
5092 79 unneback
 
5093 75 unneback
`define MODULE wb3wb3_bridge
5094 77 unneback
`BASE`MODULE wbwb3inst (
5095 75 unneback
`undef MODULE
5096
    // wishbone slave side
5097
    .wbs_dat_i(wbs_dat_i),
5098
    .wbs_adr_i(wbs_adr_i),
5099
    .wbs_sel_i(wbs_sel_i),
5100
    .wbs_bte_i(wbs_bte_i),
5101
    .wbs_cti_i(wbs_cti_i),
5102
    .wbs_we_i(wbs_we_i),
5103
    .wbs_cyc_i(wbs_cyc_i),
5104
    .wbs_stb_i(wbs_stb_i),
5105
    .wbs_dat_o(wbs_dat_o),
5106
    .wbs_ack_o(wbs_ack_o),
5107
    .wbs_clk(wbs_clk),
5108
    .wbs_rst(wbs_rst),
5109
    // wishbone master side
5110
    .wbm_dat_o(writedata),
5111 78 unneback
    .wbm_adr_o(address),
5112 75 unneback
    .wbm_sel_o(be),
5113
    .wbm_bte_o(wbm_bte_o),
5114
    .wbm_cti_o(wbm_cti_o),
5115
    .wbm_we_o(wbm_we_o),
5116
    .wbm_cyc_o(wbm_cyc_o),
5117
    .wbm_stb_o(wbm_stb_o),
5118
    .wbm_dat_i(readdata),
5119
    .wbm_ack_i(wbm_ack_i),
5120
    .wbm_clk(clk),
5121
    .wbm_rst(rst));
5122
 
5123
 
5124
endmodule
5125
`endif
5126
 
5127 40 unneback
`ifdef WB3_ARBITER_TYPE1
5128
`define MODULE wb3_arbiter_type1
5129 42 unneback
module `BASE`MODULE (
5130 40 unneback
`undef MODULE
5131 39 unneback
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
5132
    wbm_dat_i, wbm_ack_i, wbm_err_i, wbm_rty_i,
5133
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
5134
    wbs_dat_o, wbs_ack_o, wbs_err_o, wbs_rty_o,
5135
    wb_clk, wb_rst
5136
);
5137
 
5138
parameter nr_of_ports = 3;
5139
parameter adr_size = 26;
5140
parameter adr_lo   = 2;
5141
parameter dat_size = 32;
5142
parameter sel_size = dat_size/8;
5143
 
5144
localparam aw = (adr_size - adr_lo) * nr_of_ports;
5145
localparam dw = dat_size * nr_of_ports;
5146
localparam sw = sel_size * nr_of_ports;
5147
localparam cw = 3 * nr_of_ports;
5148
localparam bw = 2 * nr_of_ports;
5149
 
5150
input  [dw-1:0] wbm_dat_o;
5151
input  [aw-1:0] wbm_adr_o;
5152
input  [sw-1:0] wbm_sel_o;
5153
input  [cw-1:0] wbm_cti_o;
5154
input  [bw-1:0] wbm_bte_o;
5155
input  [nr_of_ports-1:0] wbm_we_o, wbm_stb_o, wbm_cyc_o;
5156
output [dw-1:0] wbm_dat_i;
5157
output [nr_of_ports-1:0] wbm_ack_i, wbm_err_i, wbm_rty_i;
5158
 
5159
output [dat_size-1:0] wbs_dat_i;
5160
output [adr_size-1:adr_lo] wbs_adr_i;
5161
output [sel_size-1:0] wbs_sel_i;
5162
output [2:0] wbs_cti_i;
5163
output [1:0] wbs_bte_i;
5164
output wbs_we_i, wbs_stb_i, wbs_cyc_i;
5165
input  [dat_size-1:0] wbs_dat_o;
5166
input  wbs_ack_o, wbs_err_o, wbs_rty_o;
5167
 
5168
input wb_clk, wb_rst;
5169
 
5170 44 unneback
reg  [nr_of_ports-1:0] select;
5171 39 unneback
wire [nr_of_ports-1:0] state;
5172
wire [nr_of_ports-1:0] eoc; // end-of-cycle
5173
wire [nr_of_ports-1:0] sel;
5174
wire idle;
5175
 
5176
genvar i;
5177
 
5178
assign idle = !(|state);
5179
 
5180
generate
5181
if (nr_of_ports == 2) begin
5182
 
5183
    wire [2:0] wbm1_cti_o, wbm0_cti_o;
5184
 
5185
    assign {wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5186
 
5187 44 unneback
    //assign select = (idle) ? {wbm_cyc_o[1],!wbm_cyc_o[1] & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5188
 
5189
    always @ (idle or wbm_cyc_o)
5190
    if (idle)
5191
        casex (wbm_cyc_o)
5192
        2'b1x : select = 2'b10;
5193
        2'b01 : select = 2'b01;
5194
        default : select = {nr_of_ports{1'b0}};
5195
        endcase
5196
    else
5197
        select = {nr_of_ports{1'b0}};
5198
 
5199 39 unneback
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5200
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5201
 
5202
end
5203
endgenerate
5204
 
5205
generate
5206
if (nr_of_ports == 3) begin
5207
 
5208
    wire [2:0] wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5209
 
5210
    assign {wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5211
 
5212 44 unneback
    always @ (idle or wbm_cyc_o)
5213
    if (idle)
5214
        casex (wbm_cyc_o)
5215
        3'b1xx : select = 3'b100;
5216
        3'b01x : select = 3'b010;
5217
        3'b001 : select = 3'b001;
5218
        default : select = {nr_of_ports{1'b0}};
5219
        endcase
5220
    else
5221
        select = {nr_of_ports{1'b0}};
5222
 
5223
//    assign select = (idle) ? {wbm_cyc_o[2],!wbm_cyc_o[2] & wbm_cyc_o[1],wbm_cyc_o[2:1]==2'b00 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5224 39 unneback
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5225
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5226
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5227
 
5228
end
5229
endgenerate
5230
 
5231
generate
5232 44 unneback
if (nr_of_ports == 4) begin
5233
 
5234
    wire [2:0] wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5235
 
5236
    assign {wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5237
 
5238
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5239
 
5240
    always @ (idle or wbm_cyc_o)
5241
    if (idle)
5242
        casex (wbm_cyc_o)
5243
        4'b1xxx : select = 4'b1000;
5244
        4'b01xx : select = 4'b0100;
5245
        4'b001x : select = 4'b0010;
5246
        4'b0001 : select = 4'b0001;
5247
        default : select = {nr_of_ports{1'b0}};
5248
        endcase
5249
    else
5250
        select = {nr_of_ports{1'b0}};
5251
 
5252
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5253
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5254
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5255
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5256
 
5257
end
5258
endgenerate
5259
 
5260
generate
5261
if (nr_of_ports == 5) begin
5262
 
5263
    wire [2:0] wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5264
 
5265
    assign {wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5266
 
5267
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5268
 
5269
    always @ (idle or wbm_cyc_o)
5270
    if (idle)
5271
        casex (wbm_cyc_o)
5272
        5'b1xxxx : select = 5'b10000;
5273
        5'b01xxx : select = 5'b01000;
5274
        5'b001xx : select = 5'b00100;
5275
        5'b0001x : select = 5'b00010;
5276
        5'b00001 : select = 5'b00001;
5277
        default : select = {nr_of_ports{1'b0}};
5278
        endcase
5279
    else
5280
        select = {nr_of_ports{1'b0}};
5281
 
5282
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5283
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5284
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5285
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5286
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5287
 
5288
end
5289
endgenerate
5290
 
5291
generate
5292 67 unneback
if (nr_of_ports == 6) begin
5293
 
5294
    wire [2:0] wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5295
 
5296
    assign {wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5297
 
5298
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5299
 
5300
    always @ (idle or wbm_cyc_o)
5301
    if (idle)
5302
        casex (wbm_cyc_o)
5303
        6'b1xxxxx : select = 6'b100000;
5304
        6'b01xxxx : select = 6'b010000;
5305
        6'b001xxx : select = 6'b001000;
5306
        6'b0001xx : select = 6'b000100;
5307
        6'b00001x : select = 6'b000010;
5308
        6'b000001 : select = 6'b000001;
5309
        default : select = {nr_of_ports{1'b0}};
5310
        endcase
5311
    else
5312
        select = {nr_of_ports{1'b0}};
5313
 
5314
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5315
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5316
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5317
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5318
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5319
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5320
 
5321
end
5322
endgenerate
5323
 
5324
generate
5325
if (nr_of_ports == 7) begin
5326
 
5327
    wire [2:0] wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5328
 
5329
    assign {wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5330
 
5331
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5332
 
5333
    always @ (idle or wbm_cyc_o)
5334
    if (idle)
5335
        casex (wbm_cyc_o)
5336
        7'b1xxxxxx : select = 7'b1000000;
5337
        7'b01xxxxx : select = 7'b0100000;
5338
        7'b001xxxx : select = 7'b0010000;
5339
        7'b0001xxx : select = 7'b0001000;
5340
        7'b00001xx : select = 7'b0000100;
5341
        7'b000001x : select = 7'b0000010;
5342
        7'b0000001 : select = 7'b0000001;
5343
        default : select = {nr_of_ports{1'b0}};
5344
        endcase
5345
    else
5346
        select = {nr_of_ports{1'b0}};
5347
 
5348
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
5349
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5350
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5351
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5352
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5353
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5354
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5355
 
5356
end
5357
endgenerate
5358
 
5359
generate
5360
if (nr_of_ports == 8) begin
5361
 
5362
    wire [2:0] wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5363
 
5364
    assign {wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5365
 
5366
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5367
 
5368
    always @ (idle or wbm_cyc_o)
5369
    if (idle)
5370
        casex (wbm_cyc_o)
5371
        8'b1xxxxxxx : select = 8'b10000000;
5372
        8'b01xxxxxx : select = 8'b01000000;
5373
        8'b001xxxxx : select = 8'b00100000;
5374
        8'b0001xxxx : select = 8'b00010000;
5375
        8'b00001xxx : select = 8'b00001000;
5376
        8'b000001xx : select = 8'b00000100;
5377
        8'b0000001x : select = 8'b00000010;
5378
        8'b00000001 : select = 8'b00000001;
5379
        default : select = {nr_of_ports{1'b0}};
5380
        endcase
5381
    else
5382
        select = {nr_of_ports{1'b0}};
5383
 
5384
    assign eoc[7] = (wbm_ack_i[7] & (wbm7_cti_o == 3'b000 | wbm7_cti_o == 3'b111)) | !wbm_cyc_o[7];
5385
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
5386
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5387
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5388
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5389
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5390
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5391
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5392
 
5393
end
5394
endgenerate
5395
 
5396
generate
5397 63 unneback
for (i=0;i<nr_of_ports;i=i+1) begin : spr0
5398 42 unneback
`define MODULE spr
5399
    `BASE`MODULE sr0( .sp(select[i]), .r(eoc[i]), .q(state[i]), .clk(wb_clk), .rst(wb_rst));
5400
`undef MODULE
5401 39 unneback
end
5402
endgenerate
5403
 
5404
    assign sel = select | state;
5405
 
5406 40 unneback
`define MODULE mux_andor
5407
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(32)) mux0 ( .a(wbm_dat_o), .sel(sel), .dout(wbs_dat_i));
5408
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(adr_size-adr_lo)) mux1 ( .a(wbm_adr_o), .sel(sel), .dout(wbs_adr_i));
5409
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(sel_size)) mux2 ( .a(wbm_sel_o), .sel(sel), .dout(wbs_sel_i));
5410
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(3)) mux3 ( .a(wbm_cti_o), .sel(sel), .dout(wbs_cti_i));
5411
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(2)) mux4 ( .a(wbm_bte_o), .sel(sel), .dout(wbs_bte_i));
5412
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux5 ( .a(wbm_we_o), .sel(sel), .dout(wbs_we_i));
5413
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux6 ( .a(wbm_stb_o), .sel(sel), .dout(wbs_stb_i));
5414
`undef MODULE
5415 39 unneback
    assign wbs_cyc_i = |sel;
5416
 
5417
    assign wbm_dat_i = {nr_of_ports{wbs_dat_o}};
5418
    assign wbm_ack_i = {nr_of_ports{wbs_ack_o}} & sel;
5419
    assign wbm_err_i = {nr_of_ports{wbs_err_o}} & sel;
5420
    assign wbm_rty_i = {nr_of_ports{wbs_rty_o}} & sel;
5421
 
5422
endmodule
5423 40 unneback
`endif
5424 39 unneback
 
5425 60 unneback
`ifdef WB_B3_RAM_BE
5426 49 unneback
// WB RAM with byte enable
5427 59 unneback
`define MODULE wb_b3_ram_be
5428
module `BASE`MODULE (
5429
`undef MODULE
5430 69 unneback
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
5431
    wbs_dat_o, wbs_ack_o, wb_clk, wb_rst);
5432 59 unneback
 
5433 68 unneback
parameter adr_size = 16;
5434 85 unneback
parameter mem_size = 1<<adr_size;
5435 60 unneback
parameter dat_size = 32;
5436 83 unneback
parameter max_burst_width = 4;
5437 60 unneback
parameter memory_init = 1;
5438
parameter memory_file = "vl_ram.vmem";
5439 59 unneback
 
5440 85 unneback
localparam aw = (adr_size);
5441 69 unneback
localparam dw = dat_size;
5442
localparam sw = dat_size/8;
5443
localparam cw = 3;
5444
localparam bw = 2;
5445 60 unneback
 
5446 70 unneback
input [dw-1:0] wbs_dat_i;
5447
input [aw-1:0] wbs_adr_i;
5448
input [cw-1:0] wbs_cti_i;
5449
input [bw-1:0] wbs_bte_i;
5450
input [sw-1:0] wbs_sel_i;
5451
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
5452
output [dw-1:0] wbs_dat_o;
5453
output wbs_ack_o;
5454 71 unneback
input wb_clk, wb_rst;
5455 59 unneback
 
5456 83 unneback
wire [aw-1:0] adr;
5457 59 unneback
 
5458 60 unneback
`define MODULE ram_be
5459
`BASE`MODULE # (
5460
    .data_width(dat_size),
5461 83 unneback
    .addr_width(aw),
5462 69 unneback
    .mem_size(mem_size),
5463 68 unneback
    .memory_init(memory_init),
5464
    .memory_file(memory_file))
5465 60 unneback
ram0(
5466
`undef MODULE
5467
    .d(wbs_dat_i),
5468 83 unneback
    .adr(adr),
5469 60 unneback
    .be(wbs_sel_i),
5470 90 unneback
    .re(wbs_stb_i),
5471 86 unneback
    .we(wbs_we_i & wbs_ack_o),
5472 60 unneback
    .q(wbs_dat_o),
5473
    .clk(wb_clk)
5474
);
5475
 
5476 83 unneback
`define MODULE wb_adr_inc
5477
`BASE`MODULE # ( .adr_width(aw), .max_burst_width(max_burst_width)) adr_inc0 (
5478
    .cyc_i(wbs_cyc_i),
5479
    .stb_i(wbs_stb_i),
5480
    .cti_i(wbs_cti_i),
5481
    .bte_i(wbs_bte_i),
5482
    .adr_i(wbs_adr_i),
5483 85 unneback
    .we_i(wbs_we_i),
5484 83 unneback
    .ack_o(wbs_ack_o),
5485
    .adr_o(adr),
5486
    .clk(wb_clk),
5487
    .rst(wb_rst));
5488
`undef MODULE
5489 60 unneback
 
5490 59 unneback
endmodule
5491
`endif
5492
 
5493
`ifdef WB_B4_RAM_BE
5494
// WB RAM with byte enable
5495 49 unneback
`define MODULE wb_b4_ram_be
5496
module `BASE`MODULE (
5497
`undef MODULE
5498
    wb_dat_i, wb_adr_i, wb_sel_i, wb_we_i, wb_stb_i, wb_cyc_i,
5499 52 unneback
    wb_dat_o, wb_stall_o, wb_ack_o, wb_clk, wb_rst);
5500 49 unneback
 
5501
    parameter dat_width = 32;
5502
    parameter adr_width = 8;
5503
 
5504
input [dat_width-1:0] wb_dat_i;
5505
input [adr_width-1:0] wb_adr_i;
5506
input [dat_width/8-1:0] wb_sel_i;
5507
input wb_we_i, wb_stb_i, wb_cyc_i;
5508
output [dat_width-1:0] wb_dat_o;
5509 51 unneback
reg [dat_width-1:0] wb_dat_o;
5510 52 unneback
output wb_stall_o;
5511 49 unneback
output wb_ack_o;
5512
reg wb_ack_o;
5513
input wb_clk, wb_rst;
5514
 
5515 56 unneback
wire [dat_width/8-1:0] cke;
5516
 
5517 49 unneback
generate
5518
if (dat_width==32) begin
5519 51 unneback
reg [7:0] ram3 [1<<(adr_width-2)-1:0];
5520
reg [7:0] ram2 [1<<(adr_width-2)-1:0];
5521
reg [7:0] ram1 [1<<(adr_width-2)-1:0];
5522
reg [7:0] ram0 [1<<(adr_width-2)-1:0];
5523 56 unneback
assign cke = wb_sel_i & {(dat_width/8){wb_we_i}};
5524 49 unneback
    always @ (posedge wb_clk)
5525
    begin
5526 56 unneback
        if (cke[3]) ram3[wb_adr_i[adr_width-1:2]] <= wb_dat_i[31:24];
5527
        if (cke[2]) ram2[wb_adr_i[adr_width-1:2]] <= wb_dat_i[23:16];
5528
        if (cke[1]) ram1[wb_adr_i[adr_width-1:2]] <= wb_dat_i[15:8];
5529
        if (cke[0]) ram0[wb_adr_i[adr_width-1:2]] <= wb_dat_i[7:0];
5530 49 unneback
    end
5531 59 unneback
    always @ (posedge wb_clk or posedge wb_rst)
5532
    begin
5533
        if (wb_rst)
5534
            wb_dat_o <= 32'h0;
5535
        else
5536
            wb_dat_o <= {ram3[wb_adr_i[adr_width-1:2]],ram2[wb_adr_i[adr_width-1:2]],ram1[wb_adr_i[adr_width-1:2]],ram0[wb_adr_i[adr_width-1:2]]};
5537
    end
5538 49 unneback
end
5539
endgenerate
5540
 
5541 52 unneback
always @ (posedge wb_clk or posedge wb_rst)
5542 55 unneback
if (wb_rst)
5543 52 unneback
    wb_ack_o <= 1'b0;
5544
else
5545 54 unneback
    wb_ack_o <= wb_stb_i & wb_cyc_i;
5546 52 unneback
 
5547
assign wb_stall_o = 1'b0;
5548
 
5549 49 unneback
endmodule
5550
`endif
5551
 
5552 48 unneback
`ifdef WB_B4_ROM
5553
// WB ROM
5554
`define MODULE wb_b4_rom
5555
module `BASE`MODULE (
5556
`undef MODULE
5557
    wb_adr_i, wb_stb_i, wb_cyc_i,
5558
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
5559
 
5560
    parameter dat_width = 32;
5561
    parameter dat_default = 32'h15000000;
5562
    parameter adr_width = 32;
5563
 
5564
/*
5565
`ifndef ROM
5566
`define ROM "rom.v"
5567
`endif
5568
*/
5569
    input [adr_width-1:2]   wb_adr_i;
5570
    input                   wb_stb_i;
5571
    input                   wb_cyc_i;
5572
    output [dat_width-1:0]  wb_dat_o;
5573
    reg [dat_width-1:0]     wb_dat_o;
5574
    output                  wb_ack_o;
5575
    reg                     wb_ack_o;
5576
    output                  stall_o;
5577
    input                   wb_clk;
5578
    input                   wb_rst;
5579
 
5580
always @ (posedge wb_clk or posedge wb_rst)
5581
    if (wb_rst)
5582
        wb_dat_o <= {dat_width{1'b0}};
5583
    else
5584
         case (wb_adr_i[adr_width-1:2])
5585
`ifdef ROM
5586
`include `ROM
5587
`endif
5588
           default:
5589
             wb_dat_o <= dat_default;
5590
 
5591
         endcase // case (wb_adr_i)
5592
 
5593
 
5594
always @ (posedge wb_clk or posedge wb_rst)
5595
    if (wb_rst)
5596
        wb_ack_o <= 1'b0;
5597
    else
5598
        wb_ack_o <= wb_stb_i & wb_cyc_i;
5599
 
5600
assign stall_o = 1'b0;
5601
 
5602
endmodule
5603
`endif
5604
 
5605
 
5606 40 unneback
`ifdef WB_BOOT_ROM
5607 17 unneback
// WB ROM
5608 40 unneback
`define MODULE wb_boot_rom
5609
module `BASE`MODULE (
5610
`undef MODULE
5611 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
5612 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
5613 17 unneback
 
5614 18 unneback
    parameter adr_hi = 31;
5615
    parameter adr_lo = 28;
5616
    parameter adr_sel = 4'hf;
5617
    parameter addr_width = 5;
5618 33 unneback
/*
5619 17 unneback
`ifndef BOOT_ROM
5620
`define BOOT_ROM "boot_rom.v"
5621
`endif
5622 33 unneback
*/
5623 18 unneback
    input [adr_hi:2]    wb_adr_i;
5624
    input               wb_stb_i;
5625
    input               wb_cyc_i;
5626
    output [31:0]        wb_dat_o;
5627
    output              wb_ack_o;
5628
    output              hit_o;
5629
    input               wb_clk;
5630
    input               wb_rst;
5631
 
5632
    wire hit;
5633
    reg [31:0] wb_dat;
5634
    reg wb_ack;
5635
 
5636
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
5637 17 unneback
 
5638
always @ (posedge wb_clk or posedge wb_rst)
5639
    if (wb_rst)
5640 18 unneback
        wb_dat <= 32'h15000000;
5641 17 unneback
    else
5642 18 unneback
         case (wb_adr_i[addr_width-1:2])
5643 33 unneback
`ifdef BOOT_ROM
5644 17 unneback
`include `BOOT_ROM
5645 33 unneback
`endif
5646 17 unneback
           /*
5647
            // Zero r0 and jump to 0x00000100
5648 18 unneback
 
5649
            1 : wb_dat <= 32'hA8200000;
5650
            2 : wb_dat <= 32'hA8C00100;
5651
            3 : wb_dat <= 32'h44003000;
5652
            4 : wb_dat <= 32'h15000000;
5653 17 unneback
            */
5654
           default:
5655 18 unneback
             wb_dat <= 32'h00000000;
5656 17 unneback
 
5657
         endcase // case (wb_adr_i)
5658
 
5659
 
5660
always @ (posedge wb_clk or posedge wb_rst)
5661
    if (wb_rst)
5662 18 unneback
        wb_ack <= 1'b0;
5663 17 unneback
    else
5664 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
5665 17 unneback
 
5666 18 unneback
assign hit_o = hit;
5667
assign wb_dat_o = wb_dat & {32{wb_ack}};
5668
assign wb_ack_o = wb_ack;
5669
 
5670 17 unneback
endmodule
5671 40 unneback
`endif
5672 32 unneback
 
5673 40 unneback
`ifdef WB_DPRAM
5674
`define MODULE wb_dpram
5675
module `BASE`MODULE (
5676
`undef MODULE
5677 32 unneback
        // wishbone slave side a
5678
        wbsa_dat_i, wbsa_adr_i, wbsa_we_i, wbsa_cyc_i, wbsa_stb_i, wbsa_dat_o, wbsa_ack_o,
5679
        wbsa_clk, wbsa_rst,
5680
        // wishbone slave side a
5681
        wbsb_dat_i, wbsb_adr_i, wbsb_we_i, wbsb_cyc_i, wbsb_stb_i, wbsb_dat_o, wbsb_ack_o,
5682
        wbsb_clk, wbsb_rst);
5683
 
5684
parameter data_width = 32;
5685
parameter addr_width = 8;
5686
 
5687
parameter dat_o_mask_a = 1;
5688
parameter dat_o_mask_b = 1;
5689
 
5690
input [31:0] wbsa_dat_i;
5691
input [addr_width-1:2] wbsa_adr_i;
5692
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
5693
output [31:0] wbsa_dat_o;
5694
output wbsa_ack_o;
5695
input wbsa_clk, wbsa_rst;
5696
 
5697
input [31:0] wbsb_dat_i;
5698
input [addr_width-1:2] wbsb_adr_i;
5699
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
5700
output [31:0] wbsb_dat_o;
5701
output wbsb_ack_o;
5702
input wbsb_clk, wbsb_rst;
5703
 
5704
wire wbsa_dat_tmp, wbsb_dat_tmp;
5705
 
5706 40 unneback
`define MODULE dpram_2r2w
5707
`BASE`MODULE # (
5708
`undef MODULE
5709 33 unneback
    .data_width(data_width), .addr_width(addr_width) )
5710 32 unneback
dpram0(
5711
    .d_a(wbsa_dat_i),
5712
    .q_a(wbsa_dat_tmp),
5713
    .adr_a(wbsa_adr_i),
5714
    .we_a(wbsa_we_i),
5715
    .clk_a(wbsa_clk),
5716
    .d_b(wbsb_dat_i),
5717
    .q_b(wbsb_dat_tmp),
5718
    .adr_b(wbsb_adr_i),
5719
    .we_b(wbsb_we_i),
5720
    .clk_b(wbsb_clk) );
5721
 
5722 33 unneback
generate if (dat_o_mask_a==1)
5723 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp & {data_width{wbsa_ack_o}};
5724
endgenerate
5725 33 unneback
generate if (dat_o_mask_a==0)
5726 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp;
5727
endgenerate
5728
 
5729 33 unneback
generate if (dat_o_mask_b==1)
5730 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp & {data_width{wbsb_ack_o}};
5731
endgenerate
5732 33 unneback
generate if (dat_o_mask_b==0)
5733 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp;
5734
endgenerate
5735
 
5736 40 unneback
`define MODULE spr
5737
`BASE`MODULE ack_a( .sp(wbsa_cyc_i & wbsa_stb_i & !wbsa_ack_o), .r(1'b1), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
5738
`BASE`MODULE ack_b( .sp(wbsb_cyc_i & wbsb_stb_i & !wbsb_ack_o), .r(1'b1), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
5739
`undef MODULE
5740 32 unneback
 
5741
endmodule
5742 40 unneback
`endif
5743 18 unneback
//////////////////////////////////////////////////////////////////////
5744
////                                                              ////
5745
////  Arithmetic functions                                        ////
5746
////                                                              ////
5747
////  Description                                                 ////
5748
////  Arithmetic functions for ALU and DSP                        ////
5749
////                                                              ////
5750
////                                                              ////
5751
////  To Do:                                                      ////
5752
////   -                                                          ////
5753
////                                                              ////
5754
////  Author(s):                                                  ////
5755
////      - Michael Unneback, unneback@opencores.org              ////
5756
////        ORSoC AB                                              ////
5757
////                                                              ////
5758
//////////////////////////////////////////////////////////////////////
5759
////                                                              ////
5760
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
5761
////                                                              ////
5762
//// This source file may be used and distributed without         ////
5763
//// restriction provided that this copyright statement is not    ////
5764
//// removed from the file and that any derivative work contains  ////
5765
//// the original copyright notice and the associated disclaimer. ////
5766
////                                                              ////
5767
//// This source file is free software; you can redistribute it   ////
5768
//// and/or modify it under the terms of the GNU Lesser General   ////
5769
//// Public License as published by the Free Software Foundation; ////
5770
//// either version 2.1 of the License, or (at your option) any   ////
5771
//// later version.                                               ////
5772
////                                                              ////
5773
//// This source is distributed in the hope that it will be       ////
5774
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
5775
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
5776
//// PURPOSE.  See the GNU Lesser General Public License for more ////
5777
//// details.                                                     ////
5778
////                                                              ////
5779
//// You should have received a copy of the GNU Lesser General    ////
5780
//// Public License along with this source; if not, download it   ////
5781
//// from http://www.opencores.org/lgpl.shtml                     ////
5782
////                                                              ////
5783
//////////////////////////////////////////////////////////////////////
5784
 
5785 40 unneback
`ifdef MULTS
5786 18 unneback
// signed multiplication
5787 40 unneback
`define MODULE mults
5788
module `BASE`MODULE (a,b,p);
5789
`undef MODULE
5790 18 unneback
parameter operand_a_width = 18;
5791
parameter operand_b_width = 18;
5792
parameter result_hi = 35;
5793
parameter result_lo = 0;
5794
input [operand_a_width-1:0] a;
5795
input [operand_b_width-1:0] b;
5796
output [result_hi:result_lo] p;
5797
wire signed [operand_a_width-1:0] ai;
5798
wire signed [operand_b_width-1:0] bi;
5799
wire signed [operand_a_width+operand_b_width-1:0] result;
5800
 
5801
    assign ai = a;
5802
    assign bi = b;
5803
    assign result = ai * bi;
5804
    assign p = result[result_hi:result_lo];
5805
 
5806
endmodule
5807 40 unneback
`endif
5808
`ifdef MULTS18X18
5809
`define MODULE mults18x18
5810
module `BASE`MODULE (a,b,p);
5811
`undef MODULE
5812 18 unneback
input [17:0] a,b;
5813
output [35:0] p;
5814
vl_mult
5815
    # (.operand_a_width(18), .operand_b_width(18))
5816
    mult0 (.a(a), .b(b), .p(p));
5817
endmodule
5818 40 unneback
`endif
5819 18 unneback
 
5820 40 unneback
`ifdef MULT
5821
`define MODULE mult
5822 18 unneback
// unsigned multiplication
5823 40 unneback
module `BASE`MODULE (a,b,p);
5824
`undef MODULE
5825 18 unneback
parameter operand_a_width = 18;
5826
parameter operand_b_width = 18;
5827
parameter result_hi = 35;
5828
parameter result_lo = 0;
5829
input [operand_a_width-1:0] a;
5830
input [operand_b_width-1:0] b;
5831
output [result_hi:result_hi] p;
5832
 
5833
wire [operand_a_width+operand_b_width-1:0] result;
5834
 
5835
    assign result = a * b;
5836
    assign p = result[result_hi:result_lo];
5837
 
5838
endmodule
5839 40 unneback
`endif
5840 18 unneback
 
5841 40 unneback
`ifdef SHIFT_UNIT_32
5842
`define MODULE shift_unit_32
5843 18 unneback
// shift unit
5844
// supporting the following shift functions
5845
//   SLL
5846
//   SRL
5847
//   SRA
5848
`define SHIFT_UNIT_MULT # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7))
5849 40 unneback
module `BASE`MODULE( din, s, dout, opcode);
5850
`undef MODULE
5851 18 unneback
input [31:0] din; // data in operand
5852
input [4:0] s; // shift operand
5853
input [1:0] opcode;
5854
output [31:0] dout;
5855
 
5856
parameter opcode_sll = 2'b00;
5857
//parameter opcode_srl = 2'b01;
5858
parameter opcode_sra = 2'b10;
5859
//parameter opcode_ror = 2'b11;
5860
 
5861
wire sll, sra;
5862
assign sll = opcode == opcode_sll;
5863
assign sra = opcode == opcode_sra;
5864
 
5865
wire [15:1] s1;
5866
wire [3:0] sign;
5867
wire [7:0] tmp [0:3];
5868
 
5869
// first stage is multiplier based
5870
// shift operand as fractional 8.7
5871
assign s1[15] = sll & s[2:0]==3'd7;
5872
assign s1[14] = sll & s[2:0]==3'd6;
5873
assign s1[13] = sll & s[2:0]==3'd5;
5874
assign s1[12] = sll & s[2:0]==3'd4;
5875
assign s1[11] = sll & s[2:0]==3'd3;
5876
assign s1[10] = sll & s[2:0]==3'd2;
5877
assign s1[ 9] = sll & s[2:0]==3'd1;
5878
assign s1[ 8] = s[2:0]==3'd0;
5879
assign s1[ 7] = !sll & s[2:0]==3'd1;
5880
assign s1[ 6] = !sll & s[2:0]==3'd2;
5881
assign s1[ 5] = !sll & s[2:0]==3'd3;
5882
assign s1[ 4] = !sll & s[2:0]==3'd4;
5883
assign s1[ 3] = !sll & s[2:0]==3'd5;
5884
assign s1[ 2] = !sll & s[2:0]==3'd6;
5885
assign s1[ 1] = !sll & s[2:0]==3'd7;
5886
 
5887
assign sign[3] = din[31] & sra;
5888
assign sign[2] = sign[3] & (&din[31:24]);
5889
assign sign[1] = sign[2] & (&din[23:16]);
5890
assign sign[0] = sign[1] & (&din[15:8]);
5891 40 unneback
`define MODULE mults
5892
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte3 ( .a({sign[3], {8{sign[3]}},din[31:24], din[23:16]}), .b({1'b0,s1}), .p(tmp[3]));
5893
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte2 ( .a({sign[2], din[31:24]  ,din[23:16],  din[15:8]}), .b({1'b0,s1}), .p(tmp[2]));
5894
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte1 ( .a({sign[1], din[23:16]  ,din[15:8],   din[7:0]}), .b({1'b0,s1}), .p(tmp[1]));
5895
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte0 ( .a({sign[0], din[15:8]   ,din[7:0],    8'h00}),      .b({1'b0,s1}), .p(tmp[0]));
5896
`undef MODULE
5897 18 unneback
// second stage is multiplexer based
5898
// shift on byte level
5899
 
5900
// mux byte 3
5901
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
5902
                     (sll & s[4:3]==2'b01) ? tmp[2] :
5903
                     (sll & s[4:3]==2'b10) ? tmp[1] :
5904
                     (sll & s[4:3]==2'b11) ? tmp[0] :
5905
                     {8{sign[3]}};
5906
 
5907
// mux byte 2
5908
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
5909
                     (sll & s[4:3]==2'b01) ? tmp[1] :
5910
                     (sll & s[4:3]==2'b10) ? tmp[0] :
5911
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
5912
                     (s[4:3]==2'b01) ? tmp[3] :
5913
                     {8{sign[3]}};
5914
 
5915
// mux byte 1
5916
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
5917
                     (sll & s[4:3]==2'b01) ? tmp[0] :
5918
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
5919
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
5920
                     (s[4:3]==2'b01) ? tmp[2] :
5921
                     (s[4:3]==2'b10) ? tmp[3] :
5922
                     {8{sign[3]}};
5923
 
5924
// mux byte 0
5925
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
5926
                     (sll) ?  {8{1'b0}}:
5927
                     (s[4:3]==2'b01) ? tmp[1] :
5928
                     (s[4:3]==2'b10) ? tmp[2] :
5929
                     tmp[3];
5930
 
5931
endmodule
5932 40 unneback
`endif
5933 18 unneback
 
5934 40 unneback
`ifdef LOGIC_UNIT
5935 18 unneback
// logic unit
5936
// supporting the following logic functions
5937
//    a and b
5938
//    a or  b
5939
//    a xor b
5940
//    not b
5941 40 unneback
`define MODULE logic_unit
5942
module `BASE`MODULE( a, b, result, opcode);
5943
`undef MODULE
5944 18 unneback
parameter width = 32;
5945
parameter opcode_and = 2'b00;
5946
parameter opcode_or  = 2'b01;
5947
parameter opcode_xor = 2'b10;
5948
input [width-1:0] a,b;
5949
output [width-1:0] result;
5950
input [1:0] opcode;
5951
 
5952
assign result = (opcode==opcode_and) ? a & b :
5953
                (opcode==opcode_or)  ? a | b :
5954
                (opcode==opcode_xor) ? a ^ b :
5955
                b;
5956
 
5957
endmodule
5958 48 unneback
`endif
5959 18 unneback
 
5960 48 unneback
`ifdef ARITH_UNIT
5961
`define MODULE arith_unit
5962
module `BASE`MODULE ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
5963
`undef MODULE
5964 18 unneback
parameter width = 32;
5965
parameter opcode_add = 1'b0;
5966
parameter opcode_sub = 1'b1;
5967
input [width-1:0] a,b;
5968
input c_in, add_sub, sign;
5969
output [width-1:0] result;
5970
output c_out, z, ovfl;
5971
 
5972
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))};
5973
assign z = (result=={width{1'b0}});
5974
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
5975
               (~a[width-1] & ~b[width-1] &  result[width-1]);
5976
endmodule
5977 40 unneback
`endif
5978 48 unneback
 
5979
`ifdef COUNT_UNIT
5980
`define MODULE count_unit
5981
module `BASE`MODULE (din, dout, opcode);
5982
`undef MODULE
5983
parameter width = 32;
5984
input [width-1:0] din;
5985
output [width-1:0] dout;
5986
input opcode;
5987
 
5988
integer i;
5989 58 unneback
wire [width/32+4:0] ff1, fl1;
5990 48 unneback
 
5991 57 unneback
/*
5992 48 unneback
always @(din) begin
5993
    ff1 = 0; i = 0;
5994
    while (din[i] == 0 && i < width) begin // complex condition
5995
        ff1 = ff1 + 1;
5996
        i = i + 1;
5997
    end
5998
end
5999
 
6000
always @(din) begin
6001
    fl1 = width; i = width-1;
6002
    while (din[i] == 0 && i >= width) begin // complex condition
6003
        fl1 = fl1 - 1;
6004
        i = i - 1;
6005
    end
6006
end
6007 57 unneback
*/
6008 48 unneback
 
6009
generate
6010
if (width==32) begin
6011 57 unneback
 
6012
    assign ff1 = din[0] ? 6'd1 :
6013
                 din[1] ? 6'd2 :
6014
                 din[2] ? 6'd3 :
6015
                 din[3] ? 6'd4 :
6016
                 din[4] ? 6'd5 :
6017
                 din[5] ? 6'd6 :
6018
                 din[6] ? 6'd7 :
6019
                 din[7] ? 6'd8 :
6020
                 din[8] ? 6'd9 :
6021
                 din[9] ? 6'd10 :
6022
                 din[10] ? 6'd11 :
6023
                 din[11] ? 6'd12 :
6024
                 din[12] ? 6'd13 :
6025
                 din[13] ? 6'd14 :
6026
                 din[14] ? 6'd15 :
6027
                 din[15] ? 6'd16 :
6028
                 din[16] ? 6'd17 :
6029
                 din[17] ? 6'd18 :
6030
                 din[18] ? 6'd19 :
6031
                 din[19] ? 6'd20 :
6032
                 din[20] ? 6'd21 :
6033
                 din[21] ? 6'd22 :
6034
                 din[22] ? 6'd23 :
6035
                 din[23] ? 6'd24 :
6036
                 din[24] ? 6'd25 :
6037
                 din[25] ? 6'd26 :
6038
                 din[26] ? 6'd27 :
6039
                 din[27] ? 6'd28 :
6040
                 din[28] ? 6'd29 :
6041
                 din[29] ? 6'd30 :
6042
                 din[30] ? 6'd31 :
6043
                 din[31] ? 6'd32 :
6044
                 6'd0;
6045
 
6046
    assign fl1 = din[31] ? 6'd32 :
6047
                 din[30] ? 6'd31 :
6048
                 din[29] ? 6'd30 :
6049
                 din[28] ? 6'd29 :
6050
                 din[27] ? 6'd28 :
6051
                 din[26] ? 6'd27 :
6052
                 din[25] ? 6'd26 :
6053
                 din[24] ? 6'd25 :
6054
                 din[23] ? 6'd24 :
6055
                 din[22] ? 6'd23 :
6056
                 din[21] ? 6'd22 :
6057
                 din[20] ? 6'd21 :
6058
                 din[19] ? 6'd20 :
6059
                 din[18] ? 6'd19 :
6060
                 din[17] ? 6'd18 :
6061
                 din[16] ? 6'd17 :
6062
                 din[15] ? 6'd16 :
6063
                 din[14] ? 6'd15 :
6064
                 din[13] ? 6'd14 :
6065
                 din[12] ? 6'd13 :
6066
                 din[11] ? 6'd12 :
6067
                 din[10] ? 6'd11 :
6068
                 din[9] ? 6'd10 :
6069
                 din[8] ? 6'd9 :
6070
                 din[7] ? 6'd8 :
6071
                 din[6] ? 6'd7 :
6072
                 din[5] ? 6'd6 :
6073
                 din[4] ? 6'd5 :
6074
                 din[3] ? 6'd4 :
6075
                 din[2] ? 6'd3 :
6076
                 din[1] ? 6'd2 :
6077
                 din[0] ? 6'd1 :
6078
                 6'd0;
6079
 
6080
    assign dout = (!opcode) ? {{26{1'b0}}, ff1} : {{26{1'b0}}, fl1};
6081 48 unneback
end
6082
endgenerate
6083 57 unneback
 
6084 48 unneback
generate
6085
if (width==64) begin
6086 57 unneback
    assign ff1 = 7'd0;
6087
    assign fl1 = 7'd0;
6088
    assign dout = (!opcode) ? {{57{1'b0}}, ff1} : {{57{1'b0}}, fl1};
6089 48 unneback
end
6090
endgenerate
6091
 
6092
endmodule
6093
`endif
6094
 
6095
`ifdef EXT_UNIT
6096
`define MODULE ext_unit
6097
module `BASE`MODULE ( a, b, F, result, opcode);
6098
`undef MODULE
6099
parameter width = 32;
6100
input [width-1:0] a, b;
6101
input F;
6102
output reg [width-1:0] result;
6103
input [2:0] opcode;
6104
 
6105
generate
6106
if (width==32) begin
6107
always @ (a or b or F or opcode)
6108
begin
6109
    case (opcode)
6110
    3'b000: result = {{24{1'b0}},a[7:0]};
6111
    3'b001: result = {{24{a[7]}},a[7:0]};
6112
    3'b010: result = {{16{1'b0}},a[7:0]};
6113
    3'b011: result = {{16{a[15]}},a[15:0]};
6114
    3'b110: result = (F) ? a : b;
6115
    default: result = {b[15:0],16'h0000};
6116
    endcase
6117
end
6118
end
6119
endgenerate
6120
 
6121
generate
6122
if (width==64) begin
6123
always @ (a or b or F or opcode)
6124
begin
6125
    case (opcode)
6126
    3'b000: result = {{56{1'b0}},a[7:0]};
6127
    3'b001: result = {{56{a[7]}},a[7:0]};
6128
    3'b010: result = {{48{1'b0}},a[7:0]};
6129
    3'b011: result = {{48{a[15]}},a[15:0]};
6130 57 unneback
    3'b110: result = (F) ? a : b;
6131 48 unneback
    default: result = {32'h00000000,b[15:0],16'h0000};
6132
    endcase
6133
end
6134
end
6135
endgenerate
6136
endmodule
6137
`endif

powered by: WebSVN 2.1.0

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