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 94

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

powered by: WebSVN 2.1.0

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