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 139

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 98 unneback
`ifdef ACTEL
14
    // ACTEL FPGA should not use logic to handle rw collision
15
    `define SYN_NO_RW_CHECK /*synthesis syn_ramstyle = "no_rw_check"*/
16
`else
17
    `define SYN_NO_RW_CHECK
18
`endif
19
 
20 40 unneback
`ifdef ALL
21
 
22
`define GBUF
23
`define SYNC_RST
24
`define PLL
25
 
26
`define MULTS
27
`define MULTS18X18
28
`define MULT
29
`define SHIFT_UNIT_32
30
`define LOGIC_UNIT
31
 
32
`define CNT_SHREG_WRAP
33
`define CNT_SHREG_CE_WRAP
34 105 unneback
`define CNT_SHREG_CLEAR
35 40 unneback
`define CNT_SHREG_CE_CLEAR
36
`define CNT_SHREG_CE_CLEAR_WRAP
37
 
38 139 unneback
`define CNT_BIN
39
`define CNT_BIN_CE
40
`define CNT_BIN_CLEAR
41
`define CNT_BIN_CE_CLEAR
42
`define CNT_BIN_CE_CLEAR_L1_L2
43
`define CNT_BIN_CE_CLEAR_SET_REW
44
`define CNT_BIN_CE_REW_L1
45
`define CNT_BIN_CE_REW_ZQ_L1
46
`define CNT_BIN_CE_REW_Q_ZQ_L1
47
`define CNT_GRAY
48
`define CNT_GRAY_CE
49
`define CNT_GRAY_CE_BIN
50
`define CNT_LFSR_ZQ
51
`define CNT_LFSR_CE
52
`define CNT_LFSR_CE_CLEAR_Q
53
`define CNT_LFSR_CE_Q
54
`define CNT_LFSR_CE_ZQ
55
`define CNT_LFSR_CE_Q_ZQ
56
`define CNT_LFSR_CE_REW_L1
57
 
58 40 unneback
`define MUX_ANDOR
59
`define MUX2_ANDOR
60
`define MUX3_ANDOR
61
`define MUX4_ANDOR
62
`define MUX5_ANDOR
63
`define MUX6_ANDOR
64 43 unneback
`define PARITY
65 40 unneback
 
66
`define ROM_INIT
67
`define RAM
68
`define RAM_BE
69
`define DPRAM_1R1W
70
`define DPRAM_2R1W
71 100 unneback
`define DPRAM_1R2W
72 40 unneback
`define DPRAM_2R2W
73 75 unneback
`define DPRAM_BE_2R2W
74 40 unneback
`define FIFO_1R1W_FILL_LEVEL_SYNC
75
`define FIFO_2R2W_SYNC_SIMPLEX
76
`define FIFO_CMP_ASYNC
77
`define FIFO_1R1W_ASYNC
78
`define FIFO_2R2W_ASYNC
79
`define FIFO_2R2W_ASYNC_SIMPLEX
80 48 unneback
`define REG_FILE
81 40 unneback
 
82
`define DFF
83
`define DFF_ARRAY
84
`define DFF_CE
85
`define DFF_CE_CLEAR
86
`define DF_CE_SET
87
`define SPR
88
`define SRP
89
`define DFF_SR
90
`define LATCH
91
`define SHREG
92
`define SHREG_CE
93
`define DELAY
94
`define DELAY_EMPTYFLAG
95 94 unneback
`define PULSE2TOGGLE
96
`define TOGGLE2PULSE
97
`define SYNCHRONIZER
98
`define CDC
99 40 unneback
 
100 75 unneback
`define WB3AVALON_BRIDGE
101 40 unneback
`define WB3WB3_BRIDGE
102
`define WB3_ARBITER_TYPE1
103 83 unneback
`define WB_ADR_INC
104 101 unneback
`define WB_RAM
105 103 unneback
`define WB_SHADOW_RAM
106 48 unneback
`define WB_B4_ROM
107 40 unneback
`define WB_BOOT_ROM
108
`define WB_DPRAM
109 101 unneback
`define WB_CACHE
110 103 unneback
`define WB_AVALON_BRIDGE
111
`define WB_AVALON_MEM_CACHE
112 136 unneback
`define WB_SDR_SDRAM_CTRL
113 40 unneback
 
114 44 unneback
`define IO_DFF_OE
115
`define O_DFF
116 136 unneback
`define O_DDR
117
`define O_CLK
118 44 unneback
 
119 40 unneback
`endif
120
 
121 136 unneback
///////////////////////////////////////
122
// dependencies
123
///////////////////////////////////////
124
 
125 40 unneback
`ifdef PLL
126
`ifndef SYNC_RST
127
`define SYNC_RST
128
`endif
129
`endif
130
 
131
`ifdef SYNC_RST
132
`ifndef GBUF
133
`define GBUF
134
`endif
135
`endif
136
 
137 136 unneback
`ifdef WB_SDR_SDRAM_CTRL
138
`ifndef WB_SHADOW_RAM
139
`define WB_SHADOW_RAM
140
`endif
141
`ifndef WB_CACHE
142
`define WB_CACHE
143
`endif
144
`ifndef WB_SDR_SDRAM
145
`define WB_SDR_SDRAM
146
`endif
147
`ifndef IO_DFF_OE
148
`define IO_DFF_OE
149
`endif
150
`ifndef O_DFF
151
`define O_DFF
152
`endif
153
`ifndef O_CLK
154
`define O_CLK
155
`endif
156
`endif
157
 
158
`ifdef WB_SDR_SDRAM
159
`ifndef CNT_SHREG_CLEAR
160
`define CNT_SHREG_CLEAR
161
`endif
162
`ifndef CNT_LFSR_ZQ
163
`define CNT_LFSR_ZQ
164
`endif
165
`ifndef DELAY_EMPTYFLAG
166
`define DELAY_EMPTYFLAG
167
`endif
168
`endif
169
 
170 108 unneback
`ifdef WB_DPRAM
171 92 unneback
`ifndef WB_ADR_INC
172
`define WB_ADR_INC
173 40 unneback
`endif
174 92 unneback
`ifndef DPRAM_BE_2R2W
175
`define DPRAM_BE_2R2W
176 40 unneback
`endif
177
`endif
178
 
179
`ifdef WB3_ARBITER_TYPE1
180 42 unneback
`ifndef SPR
181
`define SPR
182
`endif
183 40 unneback
`ifndef MUX_ANDOR
184
`define MUX_ANDOR
185
`endif
186
`endif
187
 
188 76 unneback
`ifdef WB3AVALON_BRIDGE
189
`ifndef WB3WB3_BRIDGE
190
`define WB3WB3_BRIDGE
191
`endif
192
`endif
193
 
194 40 unneback
`ifdef WB3WB3_BRIDGE
195
`ifndef CNT_SHREG_CE_CLEAR
196
`define CNT_SHREG_CE_CLEAR
197
`endif
198
`ifndef DFF
199
`define DFF
200
`endif
201
`ifndef DFF_CE
202
`define DFF_CE
203
`endif
204
`ifndef CNT_SHREG_CE_CLEAR
205
`define CNT_SHREG_CE_CLEAR
206
`endif
207
`ifndef FIFO_2R2W_ASYNC_SIMPLEX
208
`define FIFO_2R2W_ASYNC_SIMPLEX
209
`endif
210
`endif
211
 
212 103 unneback
 
213
`ifdef WB_AVALON_MEM_CACHE
214
`ifndef WB_SHADOW_RAM
215
`define WB_SHADOW_RAM
216
`endif
217
`ifndef WB_CACHE
218
`define WB_CACHE
219
`endif
220
`ifndef WB_AVALON_BRIDGE
221
`define WB_AVALON_BRIDGE
222
`endif
223
`endif
224
 
225 101 unneback
`ifdef WB_CACHE
226 100 unneback
`ifndef RAM
227
`define RAM
228
`endif
229
`ifndef WB_ADR_INC
230
`define WB_ADR_INC
231
`endif
232
`ifndef DPRAM_1R1W
233
`define DPRAM_1R1W
234
`endif
235
`ifndef DPRAM_1R2W
236
`define DPRAM_1R2W
237
`endif
238
`ifndef DPRAM_BE_2R2W
239
`define DPRAM_BE_2R2W
240
`endif
241
`ifndef CDC
242
`define CDC
243
`endif
244 136 unneback
`ifndef O_DFF
245
`define O_DFF
246 100 unneback
`endif
247 136 unneback
`ifndef O_CLK
248
`define O_CLK
249
`endif
250
`endif
251 103 unneback
 
252
`ifdef WB_SHADOW_RAM
253 115 unneback
`ifndef WB_RAM
254
`define WB_RAM
255 103 unneback
`endif
256
`endif
257
 
258
`ifdef WB_RAM
259
`ifndef WB_ADR_INC
260
`define WB_ADR_INC
261
`endif
262 114 unneback
`ifndef RAM_BE
263
`define RAM_BE
264 103 unneback
`endif
265 114 unneback
`endif
266
 
267 40 unneback
`ifdef MULTS18X18
268
`ifndef MULTS
269
`define MULTS
270
`endif
271
`endif
272
 
273
`ifdef SHIFT_UNIT_32
274
`ifndef MULTS
275
`define MULTS
276
`endif
277
`endif
278
 
279
`ifdef MUX2_ANDOR
280
`ifndef MUX_ANDOR
281
`define MUX_ANDOR
282
`endif
283
`endif
284
 
285
`ifdef MUX3_ANDOR
286
`ifndef MUX_ANDOR
287
`define MUX_ANDOR
288
`endif
289
`endif
290
 
291
`ifdef MUX4_ANDOR
292
`ifndef MUX_ANDOR
293
`define MUX_ANDOR
294
`endif
295
`endif
296
 
297
`ifdef MUX5_ANDOR
298
`ifndef MUX_ANDOR
299
`define MUX_ANDOR
300
`endif
301
`endif
302
 
303
`ifdef MUX6_ANDOR
304
`ifndef MUX_ANDOR
305
`define MUX_ANDOR
306
`endif
307
`endif
308
 
309
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
310
`ifndef CNT_BIN_CE
311
`define CNT_BIN_CE
312
`endif
313
`ifndef DPRAM_1R1W
314
`define DPRAM_1R1W
315
`endif
316
`ifndef CNT_BIN_CE_REW_Q_ZQ_L1
317
`define CNT_BIN_CE_REW_Q_ZQ_L1
318
`endif
319
`endif
320
 
321
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
322
`ifndef CNT_LFSR_CE
323
`define CNT_LFSR_CE
324
`endif
325
`ifndef DPRAM_2R2W
326
`define DPRAM_2R2W
327
`endif
328
`ifndef CNT_BIN_CE_REW_ZQ_L1
329
`define CNT_BIN_CE_REW_ZQ_L1
330
`endif
331
`endif
332
 
333
`ifdef FIFO_2R2W_ASYNC_SIMPLEX
334
`ifndef CNT_GRAY_CE_BIN
335
`define CNT_GRAY_CE_BIN
336
`endif
337
`ifndef DPRAM_2R2W
338
`define DPRAM_2R2W
339
`endif
340
`ifndef FIFO_CMP_ASYNC
341
`define FIFO_CMP_ASYNC
342
`endif
343
`endif
344
 
345
`ifdef FIFO_2R2W_ASYNC
346
`ifndef FIFO_1R1W_ASYNC
347
`define FIFO_1R1W_ASYNC
348
`endif
349
`endif
350
 
351
`ifdef FIFO_1R1W_ASYNC
352
`ifndef CNT_GRAY_CE_BIN
353
`define CNT_GRAY_CE_BIN
354
`endif
355
`ifndef DPRAM_1R1W
356
`define DPRAM_1R1W
357
`endif
358
`ifndef FIFO_CMP_ASYNC
359
`define FIFO_CMP_ASYNC
360
`endif
361
`endif
362
 
363
`ifdef FIFO_CMP_ASYNC
364
`ifndef DFF_SR
365
`define DFF_SR
366
`endif
367
`ifndef DFF
368
`define DFF
369
`endif
370
`endif
371 48 unneback
 
372
`ifdef REG_FILE
373
`ifndef DPRAM_1R1W
374
`define DPRAM_1R1W
375
`endif
376
`endif
377 97 unneback
 
378 98 unneback
`ifdef CDC
379
`ifndef PULSE2TOGGLE
380
`define PULSE2TOGGLE
381
`endif
382
`ifndef TOGGLE2PULSE
383
`define TOGGLE2PULSE
384
`endif
385
`ifndef SYNCHRONIZER
386
`define SYNCHRONIZER
387
`endif
388
`endif
389
 
390 136 unneback
`ifdef O_CLK
391
`ifndef O_DDR
392
`define O_DDR
393
`endif
394
`endif
395
 
396 97 unneback
// size to width
397 100 unneback
`define SIZE2WIDTH_EXPR = (`SIZE2WIDTH==1) ? 0 : (`SIZE2WIDTH==2) ? 1 : (`SIZE2WIDTH==4) ? 2 : (`SIZE2WIDTH==8) ? 3 : (`SIZE2WIDTH==16) ? 4 : (`SIZE2WIDTH==32) ? 5 : (`SIZE2WIDTH==64) ? 6 : (`SIZE2WIDTH==128) ? 7 : 8;
398 62 unneback
//////////////////////////////////////////////////////////////////////
399 6 unneback
////                                                              ////
400
////  Versatile library, clock and reset                          ////
401
////                                                              ////
402
////  Description                                                 ////
403
////  Logic related to clock and reset                            ////
404
////                                                              ////
405
////                                                              ////
406
////  To Do:                                                      ////
407
////   - add more different registers                             ////
408
////                                                              ////
409
////  Author(s):                                                  ////
410
////      - Michael Unneback, unneback@opencores.org              ////
411
////        ORSoC AB                                              ////
412
////                                                              ////
413
//////////////////////////////////////////////////////////////////////
414
////                                                              ////
415
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
416
////                                                              ////
417
//// This source file may be used and distributed without         ////
418
//// restriction provided that this copyright statement is not    ////
419
//// removed from the file and that any derivative work contains  ////
420
//// the original copyright notice and the associated disclaimer. ////
421
////                                                              ////
422
//// This source file is free software; you can redistribute it   ////
423
//// and/or modify it under the terms of the GNU Lesser General   ////
424
//// Public License as published by the Free Software Foundation; ////
425
//// either version 2.1 of the License, or (at your option) any   ////
426
//// later version.                                               ////
427
////                                                              ////
428
//// This source is distributed in the hope that it will be       ////
429
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
430
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
431
//// PURPOSE.  See the GNU Lesser General Public License for more ////
432
//// details.                                                     ////
433
////                                                              ////
434
//// You should have received a copy of the GNU Lesser General    ////
435
//// Public License along with this source; if not, download it   ////
436
//// from http://www.opencores.org/lgpl.shtml                     ////
437
////                                                              ////
438
//////////////////////////////////////////////////////////////////////
439
 
440 48 unneback
`ifdef ACTEL
441
`ifdef GBUF
442
`timescale 1 ns/100 ps
443 6 unneback
// Global buffer
444
// usage:
445
// use to enable global buffers for high fan out signals such as clock and reset
446
// Version: 8.4 8.4.0.33
447
module gbuf(GL,CLK);
448
output GL;
449
input  CLK;
450
 
451
    wire GND;
452
 
453
    GND GND_1_net(.Y(GND));
454
    CLKDLY Inst1(.CLK(CLK), .GL(GL), .DLYGL0(GND), .DLYGL1(GND),
455
        .DLYGL2(GND), .DLYGL3(GND), .DLYGL4(GND)) /* synthesis black_box */;
456
 
457
endmodule
458
`timescale 1 ns/1 ns
459 40 unneback
`define MODULE gbuf
460
module `BASE`MODULE ( i, o);
461
`undef MODULE
462 6 unneback
input i;
463
output o;
464
`ifdef SIM_GBUF
465
assign o=i;
466
`else
467
gbuf gbuf_i0 ( .CLK(i), .GL(o));
468
`endif
469
endmodule
470 40 unneback
`endif
471 33 unneback
 
472 6 unneback
`else
473 33 unneback
 
474 40 unneback
`ifdef ALTERA
475
`ifdef GBUF
476 21 unneback
//altera
477 40 unneback
`define MODULE gbuf
478
module `BASE`MODULE ( i, o);
479
`undef MODULE
480 33 unneback
input i;
481
output o;
482
assign o = i;
483
endmodule
484 40 unneback
`endif
485 33 unneback
 
486 6 unneback
`else
487
 
488 40 unneback
`ifdef GBUF
489 6 unneback
`timescale 1 ns/100 ps
490 40 unneback
`define MODULE
491
module `BASE`MODULE ( i, o);
492
`undef MODULE
493 6 unneback
input i;
494
output o;
495
assign o = i;
496
endmodule
497 40 unneback
`endif
498 6 unneback
`endif // ALTERA
499
`endif //ACTEL
500
 
501 40 unneback
`ifdef SYNC_RST
502 6 unneback
// sync reset
503 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
504 6 unneback
// output active high global reset sync with two DFFs 
505
`timescale 1 ns/100 ps
506 40 unneback
`define MODULE sync_rst
507
module `BASE`MODULE ( rst_n_i, rst_o, clk);
508
`undef MODULE
509 6 unneback
input rst_n_i, clk;
510
output rst_o;
511 18 unneback
reg [1:0] tmp;
512 6 unneback
always @ (posedge clk or negedge rst_n_i)
513
if (!rst_n_i)
514 17 unneback
        tmp <= 2'b11;
515 6 unneback
else
516 33 unneback
        tmp <= {1'b0,tmp[1]};
517 40 unneback
`define MODULE gbuf
518
`BASE`MODULE buf_i0( .i(tmp[0]), .o(rst_o));
519
`undef MODULE
520 6 unneback
endmodule
521 40 unneback
`endif
522 6 unneback
 
523 40 unneback
`ifdef PLL
524 6 unneback
// vl_pll
525
`ifdef ACTEL
526 32 unneback
///////////////////////////////////////////////////////////////////////////////
527 17 unneback
`timescale 1 ps/1 ps
528 40 unneback
`define MODULE pll
529
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
530
`undef MODULE
531 6 unneback
parameter index = 0;
532
parameter number_of_clk = 1;
533 17 unneback
parameter period_time_0 = 20000;
534
parameter period_time_1 = 20000;
535
parameter period_time_2 = 20000;
536
parameter lock_delay = 2000000;
537 6 unneback
input clk_i, rst_n_i;
538
output lock;
539
output reg [0:number_of_clk-1] clk_o;
540
output [0:number_of_clk-1] rst_o;
541
 
542
`ifdef SIM_PLL
543
 
544
always
545
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
546
 
547
generate if (number_of_clk > 1)
548
always
549
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
550
endgenerate
551
 
552
generate if (number_of_clk > 2)
553
always
554
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
555
endgenerate
556
 
557
genvar i;
558
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
559
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
560
end
561
endgenerate
562
 
563
assign #lock_delay lock = rst_n_i;
564
 
565
endmodule
566
`else
567
generate if (number_of_clk==1 & index==0) begin
568
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
569
end
570
endgenerate // index==0
571
generate if (number_of_clk==1 & index==1) begin
572
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
573
end
574
endgenerate // index==1
575
generate if (number_of_clk==1 & index==2) begin
576
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
577
end
578
endgenerate // index==2
579
generate if (number_of_clk==1 & index==3) begin
580
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
581
end
582
endgenerate // index==0
583
 
584
generate if (number_of_clk==2 & index==0) begin
585
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
586
end
587
endgenerate // index==0
588
generate if (number_of_clk==2 & index==1) begin
589
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
590
end
591
endgenerate // index==1
592
generate if (number_of_clk==2 & index==2) begin
593
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
594
end
595
endgenerate // index==2
596
generate if (number_of_clk==2 & index==3) begin
597
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
598
end
599
endgenerate // index==0
600
 
601
generate if (number_of_clk==3 & index==0) begin
602
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
603
end
604
endgenerate // index==0
605
generate if (number_of_clk==3 & index==1) begin
606
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
607
end
608
endgenerate // index==1
609
generate if (number_of_clk==3 & index==2) begin
610
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
611
end
612
endgenerate // index==2
613
generate if (number_of_clk==3 & index==3) begin
614
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
615
end
616
endgenerate // index==0
617
 
618
genvar i;
619
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
620 40 unneback
`define MODULE sync_rst
621
        `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
622
`undef MODULE
623 6 unneback
end
624
endgenerate
625
endmodule
626
`endif
627 32 unneback
///////////////////////////////////////////////////////////////////////////////
628 6 unneback
 
629
`else
630
 
631 32 unneback
///////////////////////////////////////////////////////////////////////////////
632 6 unneback
`ifdef ALTERA
633
 
634 32 unneback
`timescale 1 ps/1 ps
635 40 unneback
`define MODULE pll
636
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
637
`undef MODULE
638 32 unneback
parameter index = 0;
639
parameter number_of_clk = 1;
640
parameter period_time_0 = 20000;
641
parameter period_time_1 = 20000;
642
parameter period_time_2 = 20000;
643
parameter period_time_3 = 20000;
644
parameter period_time_4 = 20000;
645
parameter lock_delay = 2000000;
646
input clk_i, rst_n_i;
647
output lock;
648
output reg [0:number_of_clk-1] clk_o;
649
output [0:number_of_clk-1] rst_o;
650
 
651
`ifdef SIM_PLL
652
 
653
always
654
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
655
 
656
generate if (number_of_clk > 1)
657
always
658
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
659
endgenerate
660
 
661
generate if (number_of_clk > 2)
662
always
663
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
664
endgenerate
665
 
666 33 unneback
generate if (number_of_clk > 3)
667 32 unneback
always
668
     #((period_time_3)/2) clk_o[3] <=  (!rst_n_i) ? 0 : ~clk_o[3];
669
endgenerate
670
 
671 33 unneback
generate if (number_of_clk > 4)
672 32 unneback
always
673
     #((period_time_4)/2) clk_o[4] <=  (!rst_n_i) ? 0 : ~clk_o[4];
674
endgenerate
675
 
676
genvar i;
677
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
678
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
679
end
680
endgenerate
681
 
682 33 unneback
//assign #lock_delay lock = rst_n_i;
683
assign lock = rst_n_i;
684 32 unneback
 
685
endmodule
686 6 unneback
`else
687
 
688 33 unneback
`ifdef VL_PLL0
689
`ifdef VL_PLL0_CLK1
690
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
691
`endif
692
`ifdef VL_PLL0_CLK2
693
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
694
`endif
695
`ifdef VL_PLL0_CLK3
696
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
697
`endif
698
`ifdef VL_PLL0_CLK4
699
    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]));
700
`endif
701
`ifdef VL_PLL0_CLK5
702
    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]));
703
`endif
704
`endif
705 32 unneback
 
706 33 unneback
`ifdef VL_PLL1
707
`ifdef VL_PLL1_CLK1
708
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
709
`endif
710
`ifdef VL_PLL1_CLK2
711
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
712
`endif
713
`ifdef VL_PLL1_CLK3
714
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
715
`endif
716
`ifdef VL_PLL1_CLK4
717
    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]));
718
`endif
719
`ifdef VL_PLL1_CLK5
720
    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]));
721
`endif
722
`endif
723 32 unneback
 
724 33 unneback
`ifdef VL_PLL2
725
`ifdef VL_PLL2_CLK1
726
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
727
`endif
728
`ifdef VL_PLL2_CLK2
729
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
730
`endif
731
`ifdef VL_PLL2_CLK3
732
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
733
`endif
734
`ifdef VL_PLL2_CLK4
735
    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]));
736
`endif
737
`ifdef VL_PLL2_CLK5
738
    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]));
739
`endif
740
`endif
741 32 unneback
 
742 33 unneback
`ifdef VL_PLL3
743
`ifdef VL_PLL3_CLK1
744
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
745
`endif
746
`ifdef VL_PLL3_CLK2
747
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
748
`endif
749
`ifdef VL_PLL3_CLK3
750
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]), .c2(clk_o[2]));
751
`endif
752
`ifdef VL_PLL3_CLK4
753
    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]));
754
`endif
755
`ifdef VL_PLL3_CLK5
756
    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]));
757
`endif
758
`endif
759 32 unneback
 
760
genvar i;
761
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
762 40 unneback
`define MODULE sync_rst
763
        `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
764
`undef MODULE
765 32 unneback
end
766
endgenerate
767
endmodule
768
`endif
769
///////////////////////////////////////////////////////////////////////////////
770
 
771
`else
772
 
773 6 unneback
// generic PLL
774 17 unneback
`timescale 1 ps/1 ps
775 40 unneback
`define MODULE pll
776
module `BASE`MODULE ( clk_i, rst_n_i, lock, clk_o, rst_o);
777
`undef MODULE
778 6 unneback
parameter index = 0;
779
parameter number_of_clk = 1;
780 17 unneback
parameter period_time_0 = 20000;
781
parameter period_time_1 = 20000;
782
parameter period_time_2 = 20000;
783 6 unneback
parameter lock_delay = 2000;
784
input clk_i, rst_n_i;
785
output lock;
786
output reg [0:number_of_clk-1] clk_o;
787
output [0:number_of_clk-1] rst_o;
788
 
789
always
790
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
791
 
792
generate if (number_of_clk > 1)
793
always
794
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
795
endgenerate
796
 
797
generate if (number_of_clk > 2)
798
always
799
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
800
endgenerate
801
 
802
genvar i;
803
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
804 40 unneback
`define MODULE sync_rst
805
     `BASE`MODULE rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
806
`undef MODULE
807 6 unneback
end
808
endgenerate
809
 
810
assign #lock_delay lock = rst_n_i;
811
 
812
endmodule
813
 
814
`endif //altera
815 17 unneback
`endif //actel
816 40 unneback
`undef MODULE
817
`endif//////////////////////////////////////////////////////////////////////
818 6 unneback
////                                                              ////
819
////  Versatile library, registers                                ////
820
////                                                              ////
821
////  Description                                                 ////
822
////  Different type of registers                                 ////
823
////                                                              ////
824
////                                                              ////
825
////  To Do:                                                      ////
826
////   - add more different registers                             ////
827
////                                                              ////
828
////  Author(s):                                                  ////
829
////      - Michael Unneback, unneback@opencores.org              ////
830
////        ORSoC AB                                              ////
831
////                                                              ////
832
//////////////////////////////////////////////////////////////////////
833
////                                                              ////
834
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
835
////                                                              ////
836
//// This source file may be used and distributed without         ////
837
//// restriction provided that this copyright statement is not    ////
838
//// removed from the file and that any derivative work contains  ////
839
//// the original copyright notice and the associated disclaimer. ////
840
////                                                              ////
841
//// This source file is free software; you can redistribute it   ////
842
//// and/or modify it under the terms of the GNU Lesser General   ////
843
//// Public License as published by the Free Software Foundation; ////
844
//// either version 2.1 of the License, or (at your option) any   ////
845
//// later version.                                               ////
846
////                                                              ////
847
//// This source is distributed in the hope that it will be       ////
848
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
849
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
850
//// PURPOSE.  See the GNU Lesser General Public License for more ////
851
//// details.                                                     ////
852
////                                                              ////
853
//// You should have received a copy of the GNU Lesser General    ////
854
//// Public License along with this source; if not, download it   ////
855
//// from http://www.opencores.org/lgpl.shtml                     ////
856
////                                                              ////
857
//////////////////////////////////////////////////////////////////////
858
 
859 40 unneback
`ifdef DFF
860
`define MODULE dff
861
module `BASE`MODULE ( d, q, clk, rst);
862
`undef MODULE
863 6 unneback
        parameter width = 1;
864 139 unneback
        parameter reset_value = {width{1'b0}};
865 6 unneback
 
866
        input [width-1:0] d;
867
        input clk, rst;
868
        output reg [width-1:0] q;
869
 
870
        always @ (posedge clk or posedge rst)
871
        if (rst)
872
                q <= reset_value;
873
        else
874
                q <= d;
875
 
876
endmodule
877 40 unneback
`endif
878 6 unneback
 
879 40 unneback
`ifdef DFF_ARRAY
880
`define MODULE dff_array
881
module `BASE`MODULE ( d, q, clk, rst);
882
`undef MODULE
883 6 unneback
 
884
        parameter width = 1;
885
        parameter depth = 2;
886
        parameter reset_value = 1'b0;
887
 
888
        input [width-1:0] d;
889
        input clk, rst;
890
        output [width-1:0] q;
891
        reg  [0:depth-1] q_tmp [width-1:0];
892
        integer i;
893
        always @ (posedge clk or posedge rst)
894
        if (rst) begin
895
            for (i=0;i<depth;i=i+1)
896
                q_tmp[i] <= {width{reset_value}};
897
        end else begin
898
            q_tmp[0] <= d;
899
            for (i=1;i<depth;i=i+1)
900
                q_tmp[i] <= q_tmp[i-1];
901
        end
902
 
903
    assign q = q_tmp[depth-1];
904
 
905
endmodule
906 40 unneback
`endif
907 6 unneback
 
908 40 unneback
`ifdef DFF_CE
909
`define MODULE dff_ce
910
module `BASE`MODULE ( d, ce, q, clk, rst);
911
`undef MODULE
912 6 unneback
 
913
        parameter width = 1;
914 139 unneback
        parameter reset_value = {width{1'b0}};
915 6 unneback
 
916
        input [width-1:0] d;
917
        input ce, clk, rst;
918
        output reg [width-1:0] q;
919
 
920
        always @ (posedge clk or posedge rst)
921
        if (rst)
922
                q <= reset_value;
923
        else
924
                if (ce)
925
                        q <= d;
926
 
927
endmodule
928 40 unneback
`endif
929 6 unneback
 
930 40 unneback
`ifdef DFF_CE_CLEAR
931
`define MODULE dff_ce_clear
932
module `BASE`MODULE ( d, ce, clear, q, clk, rst);
933
`undef MODULE
934 8 unneback
 
935
        parameter width = 1;
936 139 unneback
        parameter reset_value = {width{1'b0}};
937 8 unneback
 
938
        input [width-1:0] d;
939 10 unneback
        input ce, clear, clk, rst;
940 8 unneback
        output reg [width-1:0] q;
941
 
942
        always @ (posedge clk or posedge rst)
943
        if (rst)
944
            q <= reset_value;
945
        else
946
            if (ce)
947
                if (clear)
948
                    q <= {width{1'b0}};
949
                else
950
                    q <= d;
951
 
952
endmodule
953 40 unneback
`endif
954 8 unneback
 
955 40 unneback
`ifdef DF_CE_SET
956
`define MODULE dff_ce_set
957
module `BASE`MODULE ( d, ce, set, q, clk, rst);
958
`undef MODULE
959 24 unneback
 
960
        parameter width = 1;
961 139 unneback
        parameter reset_value = {width{1'b0}};
962 24 unneback
 
963
        input [width-1:0] d;
964
        input ce, set, clk, rst;
965
        output reg [width-1:0] q;
966
 
967
        always @ (posedge clk or posedge rst)
968
        if (rst)
969
            q <= reset_value;
970
        else
971
            if (ce)
972
                if (set)
973
                    q <= {width{1'b1}};
974
                else
975
                    q <= d;
976
 
977
endmodule
978 40 unneback
`endif
979 24 unneback
 
980 40 unneback
`ifdef SPR
981
`define MODULE spr
982
module `BASE`MODULE ( sp, r, q, clk, rst);
983
`undef MODULE
984
 
985 64 unneback
        //parameter width = 1;
986
        parameter reset_value = 1'b0;
987 29 unneback
 
988
        input sp, r;
989
        output reg q;
990
        input clk, rst;
991
 
992
        always @ (posedge clk or posedge rst)
993
        if (rst)
994
            q <= reset_value;
995
        else
996
            if (sp)
997
                q <= 1'b1;
998
            else if (r)
999
                q <= 1'b0;
1000
 
1001
endmodule
1002 40 unneback
`endif
1003 29 unneback
 
1004 40 unneback
`ifdef SRP
1005
`define MODULE srp
1006
module `BASE`MODULE ( s, rp, q, clk, rst);
1007
`undef MODULE
1008
 
1009 29 unneback
        parameter width = 1;
1010
        parameter reset_value = 0;
1011
 
1012
        input s, rp;
1013
        output reg q;
1014
        input clk, rst;
1015
 
1016
        always @ (posedge clk or posedge rst)
1017
        if (rst)
1018
            q <= reset_value;
1019
        else
1020
            if (rp)
1021
                q <= 1'b0;
1022
            else if (s)
1023
                q <= 1'b1;
1024
 
1025
endmodule
1026 40 unneback
`endif
1027 29 unneback
 
1028 40 unneback
`ifdef ALTERA
1029 29 unneback
 
1030 40 unneback
`ifdef DFF_SR
1031 6 unneback
// megafunction wizard: %LPM_FF%
1032
// GENERATION: STANDARD
1033
// VERSION: WM1.0
1034
// MODULE: lpm_ff 
1035
 
1036
// ============================================================
1037
// File Name: dff_sr.v
1038
// Megafunction Name(s):
1039
//                      lpm_ff
1040
//
1041
// Simulation Library Files(s):
1042
//                      lpm
1043
// ============================================================
1044
// ************************************************************
1045
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
1046
//
1047
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
1048
// ************************************************************
1049
 
1050
 
1051
//Copyright (C) 1991-2010 Altera Corporation
1052
//Your use of Altera Corporation's design tools, logic functions 
1053
//and other software and tools, and its AMPP partner logic 
1054
//functions, and any output files from any of the foregoing 
1055
//(including device programming or simulation files), and any 
1056
//associated documentation or information are expressly subject 
1057
//to the terms and conditions of the Altera Program License 
1058
//Subscription Agreement, Altera MegaCore Function License 
1059
//Agreement, or other applicable license agreement, including, 
1060
//without limitation, that your use is for the sole purpose of 
1061
//programming logic devices manufactured by Altera and sold by 
1062
//Altera or its authorized distributors.  Please refer to the 
1063
//applicable agreement for further details.
1064
 
1065
 
1066
// synopsys translate_off
1067
`timescale 1 ps / 1 ps
1068
// synopsys translate_on
1069 40 unneback
`define MODULE dff_sr
1070
module `BASE`MODULE (
1071
`undef MODULE
1072
 
1073 6 unneback
        aclr,
1074
        aset,
1075
        clock,
1076
        data,
1077
        q);
1078
 
1079
        input     aclr;
1080
        input     aset;
1081
        input     clock;
1082
        input     data;
1083
        output    q;
1084
 
1085
        wire [0:0] sub_wire0;
1086
        wire [0:0] sub_wire1 = sub_wire0[0:0];
1087
        wire  q = sub_wire1;
1088
        wire  sub_wire2 = data;
1089
        wire  sub_wire3 = sub_wire2;
1090
 
1091
        lpm_ff  lpm_ff_component (
1092
                                .aclr (aclr),
1093
                                .clock (clock),
1094
                                .data (sub_wire3),
1095
                                .aset (aset),
1096
                                .q (sub_wire0)
1097
                                // synopsys translate_off
1098
                                ,
1099
                                .aload (),
1100
                                .enable (),
1101
                                .sclr (),
1102
                                .sload (),
1103
                                .sset ()
1104
                                // synopsys translate_on
1105
                                );
1106
        defparam
1107
                lpm_ff_component.lpm_fftype = "DFF",
1108
                lpm_ff_component.lpm_type = "LPM_FF",
1109
                lpm_ff_component.lpm_width = 1;
1110
 
1111
 
1112
endmodule
1113
 
1114
// ============================================================
1115
// CNX file retrieval info
1116
// ============================================================
1117
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
1118
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
1119
// Retrieval info: PRIVATE: ASET NUMERIC "1"
1120
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
1121
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
1122
// Retrieval info: PRIVATE: DFF NUMERIC "1"
1123
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
1124
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
1125
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
1126
// Retrieval info: PRIVATE: SSET NUMERIC "0"
1127
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
1128
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
1129
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
1130
// Retrieval info: PRIVATE: nBit NUMERIC "1"
1131
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
1132
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
1133
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
1134
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
1135
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
1136
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
1137
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
1138
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
1139
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
1140
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
1141
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
1142
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
1143
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
1144
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
1145
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
1146
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
1147
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
1148
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
1149
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
1150
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
1151
// Retrieval info: LIB_FILE: lpm
1152 40 unneback
`endif
1153 6 unneback
 
1154
`else
1155
 
1156 40 unneback
`ifdef DFF_SR
1157
`define MODULE dff_sr
1158
module `BASE`MODULE ( aclr, aset, clock, data, q);
1159
`undef MODULE
1160 6 unneback
 
1161
    input         aclr;
1162
    input         aset;
1163
    input         clock;
1164
    input         data;
1165
    output reg    q;
1166
 
1167
   always @ (posedge clock or posedge aclr or posedge aset)
1168
     if (aclr)
1169
       q <= 1'b0;
1170
     else if (aset)
1171
       q <= 1'b1;
1172
     else
1173
       q <= data;
1174
 
1175
endmodule
1176 40 unneback
`endif
1177 6 unneback
 
1178
`endif
1179
 
1180
// LATCH
1181
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
1182
`ifdef ALTERA
1183 40 unneback
 
1184
`ifdef LATCH
1185
`define MODULE latch
1186
module `BASE`MODULE ( d, le, q, clk);
1187
`undef MODULE
1188 6 unneback
input d, le;
1189
output q;
1190
input clk;
1191
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
1192
endmodule
1193 40 unneback
`endif
1194
 
1195 6 unneback
`else
1196 40 unneback
 
1197
`ifdef LATCH
1198
`define MODULE latch
1199
module `BASE`MODULE ( d, le, q, clk);
1200
`undef MODULE
1201 6 unneback
input d, le;
1202 48 unneback
input clk;
1203
always @ (le or d)
1204 60 unneback
if (le)
1205 48 unneback
    d <= q;
1206 6 unneback
endmodule
1207 15 unneback
`endif
1208
 
1209 40 unneback
`endif
1210
 
1211
`ifdef SHREG
1212
`define MODULE shreg
1213
module `BASE`MODULE ( d, q, clk, rst);
1214
`undef MODULE
1215
 
1216 17 unneback
parameter depth = 10;
1217
input d;
1218
output q;
1219
input clk, rst;
1220
 
1221
reg [1:depth] dffs;
1222
 
1223
always @ (posedge clk or posedge rst)
1224
if (rst)
1225
    dffs <= {depth{1'b0}};
1226
else
1227
    dffs <= {d,dffs[1:depth-1]};
1228
assign q = dffs[depth];
1229
endmodule
1230 40 unneback
`endif
1231 17 unneback
 
1232 40 unneback
`ifdef SHREG_CE
1233
`define MODULE shreg_ce
1234
module `BASE`MODULE ( d, ce, q, clk, rst);
1235
`undef MODULE
1236 17 unneback
parameter depth = 10;
1237
input d, ce;
1238
output q;
1239
input clk, rst;
1240
 
1241
reg [1:depth] dffs;
1242
 
1243
always @ (posedge clk or posedge rst)
1244
if (rst)
1245
    dffs <= {depth{1'b0}};
1246
else
1247
    if (ce)
1248
        dffs <= {d,dffs[1:depth-1]};
1249
assign q = dffs[depth];
1250
endmodule
1251 40 unneback
`endif
1252 17 unneback
 
1253 40 unneback
`ifdef DELAY
1254
`define MODULE delay
1255
module `BASE`MODULE ( d, q, clk, rst);
1256
`undef MODULE
1257 15 unneback
parameter depth = 10;
1258
input d;
1259
output q;
1260
input clk, rst;
1261
 
1262
reg [1:depth] dffs;
1263
 
1264
always @ (posedge clk or posedge rst)
1265
if (rst)
1266
    dffs <= {depth{1'b0}};
1267
else
1268
    dffs <= {d,dffs[1:depth-1]};
1269
assign q = dffs[depth];
1270 17 unneback
endmodule
1271 40 unneback
`endif
1272 17 unneback
 
1273 40 unneback
`ifdef DELAY_EMPTYFLAG
1274
`define MODULE delay_emptyflag
1275 41 unneback
module `BASE`MODULE ( d, q, emptyflag, clk, rst);
1276 40 unneback
`undef MODULE
1277 17 unneback
parameter depth = 10;
1278
input d;
1279
output q, emptyflag;
1280
input clk, rst;
1281
 
1282
reg [1:depth] dffs;
1283
 
1284
always @ (posedge clk or posedge rst)
1285
if (rst)
1286
    dffs <= {depth{1'b0}};
1287
else
1288
    dffs <= {d,dffs[1:depth-1]};
1289
assign q = dffs[depth];
1290
assign emptyflag = !(|dffs);
1291
endmodule
1292 40 unneback
`endif
1293 75 unneback
 
1294 94 unneback
`ifdef PULSE2TOGGLE
1295 98 unneback
`define MODULE pulse2toggle
1296
module `BASE`MODULE ( pl, q, clk, rst);
1297 75 unneback
`undef MODULE
1298 94 unneback
input pl;
1299 98 unneback
output reg q;
1300 94 unneback
input clk, rst;
1301
always @ (posedge clk or posedge rst)
1302 75 unneback
if (rst)
1303 94 unneback
    q <= 1'b0;
1304 75 unneback
else
1305 94 unneback
    q <= pl ^ q;
1306
endmodule
1307
`endif
1308 75 unneback
 
1309 94 unneback
`ifdef TOGGLE2PULSE
1310 98 unneback
`define MODULE toggle2pulse
1311 94 unneback
module `BASE`MODULE (d, pl, clk, rst);
1312 97 unneback
`undef MODULE
1313 94 unneback
input d;
1314
output pl;
1315
input clk, rst;
1316
reg dff;
1317
always @ (posedge clk or posedge rst)
1318
if (rst)
1319
    dff <= 1'b0;
1320 75 unneback
else
1321 94 unneback
    dff <= d;
1322 98 unneback
assign pl = d ^ dff;
1323 94 unneback
endmodule
1324
`endif
1325 75 unneback
 
1326 94 unneback
`ifdef SYNCHRONIZER
1327
`define MODULE synchronizer
1328
module `BASE`MODULE (d, q, clk, rst);
1329
`undef MODULE
1330
input d;
1331
output reg q;
1332 116 unneback
input clk, rst;
1333 94 unneback
reg dff;
1334
always @ (posedge clk or posedge rst)
1335
if (rst)
1336 100 unneback
    {q,dff} <= 2'b00;
1337 75 unneback
else
1338 100 unneback
    {q,dff} <= {dff,d};
1339 94 unneback
endmodule
1340
`endif
1341 75 unneback
 
1342 94 unneback
`ifdef CDC
1343
`define MODULE cdc
1344 97 unneback
module `BASE`MODULE ( start_pl, take_it_pl, take_it_grant_pl, got_it_pl, clk_src, rst_src, clk_dst, rst_dst);
1345 94 unneback
`undef MODULE
1346
input start_pl;
1347
output take_it_pl;
1348
input take_it_grant_pl; // note: connect to take_it_pl to generate automatic ack
1349
output got_it_pl;
1350
input clk_src, rst_src;
1351
input clk_dst, rst_dst;
1352
wire take_it_tg, take_it_tg_sync;
1353
wire got_it_tg, got_it_tg_sync;
1354
// src -> dst
1355
`define MODULE pulse2toggle
1356
`BASE`MODULE p2t0 (
1357
`undef MODULE
1358
    .pl(start_pl),
1359
    .q(take_it_tg),
1360
    .clk(clk_src),
1361
    .rst(rst_src));
1362 75 unneback
 
1363 94 unneback
`define MODULE synchronizer
1364
`BASE`MODULE sync0 (
1365
`undef MODULE
1366
    .d(take_it_tg),
1367
    .q(take_it_tg_sync),
1368
    .clk(clk_dst),
1369
    .rst(rst_dst));
1370
 
1371
`define MODULE toggle2pulse
1372
`BASE`MODULE t2p0 (
1373
`undef MODULE
1374 100 unneback
    .d(take_it_tg_sync),
1375 94 unneback
    .pl(take_it_pl),
1376
    .clk(clk_dst),
1377
    .rst(rst_dst));
1378
 
1379
// dst -> src
1380
`define MODULE pulse2toggle
1381 98 unneback
`BASE`MODULE p2t1 (
1382 94 unneback
`undef MODULE
1383
    .pl(take_it_grant_pl),
1384
    .q(got_it_tg),
1385
    .clk(clk_dst),
1386
    .rst(rst_dst));
1387
 
1388
`define MODULE synchronizer
1389
`BASE`MODULE sync1 (
1390
`undef MODULE
1391
    .d(got_it_tg),
1392
    .q(got_it_tg_sync),
1393
    .clk(clk_src),
1394
    .rst(rst_src));
1395
 
1396
`define MODULE toggle2pulse
1397
`BASE`MODULE t2p1 (
1398
`undef MODULE
1399 100 unneback
    .d(got_it_tg_sync),
1400 94 unneback
    .pl(got_it_pl),
1401
    .clk(clk_src),
1402
    .rst(rst_src));
1403
 
1404 75 unneback
endmodule
1405
`endif
1406 17 unneback
//////////////////////////////////////////////////////////////////////
1407 6 unneback
////                                                              ////
1408 18 unneback
////  Logic functions                                             ////
1409
////                                                              ////
1410
////  Description                                                 ////
1411
////  Logic functions such as multiplexers                        ////
1412
////                                                              ////
1413
////                                                              ////
1414
////  To Do:                                                      ////
1415
////   -                                                          ////
1416
////                                                              ////
1417
////  Author(s):                                                  ////
1418
////      - Michael Unneback, unneback@opencores.org              ////
1419
////        ORSoC AB                                              ////
1420
////                                                              ////
1421
//////////////////////////////////////////////////////////////////////
1422
////                                                              ////
1423
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1424
////                                                              ////
1425
//// This source file may be used and distributed without         ////
1426
//// restriction provided that this copyright statement is not    ////
1427
//// removed from the file and that any derivative work contains  ////
1428
//// the original copyright notice and the associated disclaimer. ////
1429
////                                                              ////
1430
//// This source file is free software; you can redistribute it   ////
1431
//// and/or modify it under the terms of the GNU Lesser General   ////
1432
//// Public License as published by the Free Software Foundation; ////
1433
//// either version 2.1 of the License, or (at your option) any   ////
1434
//// later version.                                               ////
1435
////                                                              ////
1436
//// This source is distributed in the hope that it will be       ////
1437
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1438
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1439
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1440
//// details.                                                     ////
1441
////                                                              ////
1442
//// You should have received a copy of the GNU Lesser General    ////
1443
//// Public License along with this source; if not, download it   ////
1444
//// from http://www.opencores.org/lgpl.shtml                     ////
1445
////                                                              ////
1446
//////////////////////////////////////////////////////////////////////
1447 40 unneback
`ifdef MUX_ANDOR
1448
`define MODULE mux_andor
1449
module `BASE`MODULE ( a, sel, dout);
1450
`undef MODULE
1451 36 unneback
 
1452
parameter width = 32;
1453
parameter nr_of_ports = 4;
1454
 
1455
input [nr_of_ports*width-1:0] a;
1456
input [nr_of_ports-1:0] sel;
1457
output reg [width-1:0] dout;
1458
 
1459 38 unneback
integer i,j;
1460
 
1461 36 unneback
always @ (a, sel)
1462
begin
1463
    dout = a[width-1:0] & {width{sel[0]}};
1464 42 unneback
    for (i=1;i<nr_of_ports;i=i+1)
1465
        for (j=0;j<width;j=j+1)
1466
            dout[j] = (a[i*width + j] & sel[i]) | dout[j];
1467 36 unneback
end
1468
 
1469
endmodule
1470 40 unneback
`endif
1471 36 unneback
 
1472 40 unneback
`ifdef MUX2_ANDOR
1473
`define MODULE mux2_andor
1474
module `BASE`MODULE ( a1, a0, sel, dout);
1475
`undef MODULE
1476 18 unneback
 
1477 34 unneback
parameter width = 32;
1478 35 unneback
localparam nr_of_ports = 2;
1479 34 unneback
input [width-1:0] a1, a0;
1480
input [nr_of_ports-1:0] sel;
1481
output [width-1:0] dout;
1482
 
1483 40 unneback
`define MODULE mux_andor
1484
`BASE`MODULE
1485 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1486 36 unneback
    mux0( .a({a1,a0}), .sel(sel), .dout(dout));
1487 40 unneback
`undef MODULE
1488
 
1489 34 unneback
endmodule
1490 40 unneback
`endif
1491 34 unneback
 
1492 40 unneback
`ifdef MUX3_ANDOR
1493
`define MODULE mux3_andor
1494
module `BASE`MODULE ( a2, a1, a0, sel, dout);
1495
`undef MODULE
1496 34 unneback
 
1497
parameter width = 32;
1498 35 unneback
localparam nr_of_ports = 3;
1499 34 unneback
input [width-1:0] a2, a1, a0;
1500
input [nr_of_ports-1:0] sel;
1501
output [width-1:0] dout;
1502
 
1503 40 unneback
`define MODULE mux_andor
1504
`BASE`MODULE
1505 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1506 36 unneback
    mux0( .a({a2,a1,a0}), .sel(sel), .dout(dout));
1507 40 unneback
`undef MODULE
1508 34 unneback
endmodule
1509 40 unneback
`endif
1510 34 unneback
 
1511 40 unneback
`ifdef MUX4_ANDOR
1512
`define MODULE mux4_andor
1513
module `BASE`MODULE ( a3, a2, a1, a0, sel, dout);
1514
`undef MODULE
1515 18 unneback
 
1516
parameter width = 32;
1517 35 unneback
localparam nr_of_ports = 4;
1518 18 unneback
input [width-1:0] a3, a2, a1, a0;
1519
input [nr_of_ports-1:0] sel;
1520 22 unneback
output [width-1:0] dout;
1521 18 unneback
 
1522 40 unneback
`define MODULE mux_andor
1523
`BASE`MODULE
1524 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1525 36 unneback
    mux0( .a({a3,a2,a1,a0}), .sel(sel), .dout(dout));
1526 40 unneback
`undef MODULE
1527 18 unneback
 
1528
endmodule
1529 40 unneback
`endif
1530 18 unneback
 
1531 40 unneback
`ifdef MUX5_ANDOR
1532
`define MODULE mux5_andor
1533
module `BASE`MODULE ( a4, a3, a2, a1, a0, sel, dout);
1534
`undef MODULE
1535 18 unneback
 
1536
parameter width = 32;
1537 35 unneback
localparam nr_of_ports = 5;
1538 18 unneback
input [width-1:0] a4, a3, a2, a1, a0;
1539
input [nr_of_ports-1:0] sel;
1540 22 unneback
output [width-1:0] dout;
1541 18 unneback
 
1542 40 unneback
`define MODULE mux_andor
1543
`BASE`MODULE
1544 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1545 36 unneback
    mux0( .a({a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
1546 40 unneback
`undef MODULE
1547 18 unneback
 
1548
endmodule
1549 40 unneback
`endif
1550 18 unneback
 
1551 40 unneback
`ifdef MUX6_ANDOR
1552
`define MODULE mux6_andor
1553
module `BASE`MODULE ( a5, a4, a3, a2, a1, a0, sel, dout);
1554
`undef MODULE
1555 18 unneback
 
1556
parameter width = 32;
1557 35 unneback
localparam nr_of_ports = 6;
1558 18 unneback
input [width-1:0] a5, a4, a3, a2, a1, a0;
1559
input [nr_of_ports-1:0] sel;
1560 22 unneback
output [width-1:0] dout;
1561 18 unneback
 
1562 40 unneback
`define MODULE mux_andor
1563
`BASE`MODULE
1564 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
1565 36 unneback
    mux0( .a({a5,a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
1566 40 unneback
`undef MODULE
1567 18 unneback
 
1568
endmodule
1569 40 unneback
`endif
1570 43 unneback
 
1571
`ifdef PARITY
1572
 
1573
`define MODULE parity_generate
1574
module `BASE`MODULE (data, parity);
1575
`undef MODULE
1576
parameter word_size = 32;
1577
parameter chunk_size = 8;
1578
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
1579
input [word_size-1:0] data;
1580
output reg [word_size/chunk_size-1:0] parity;
1581
integer i,j;
1582
always @ (data)
1583
for (i=0;i<word_size/chunk_size;i=i+1) begin
1584
    parity[i] = parity_type;
1585
    for (j=0;j<chunk_size;j=j+1) begin
1586 46 unneback
        parity[i] = data[i*chunk_size+j] ^ parity[i];
1587 43 unneback
    end
1588
end
1589
endmodule
1590
 
1591
`define MODULE parity_check
1592
module `BASE`MODULE( data, parity, parity_error);
1593
`undef MODULE
1594
parameter word_size = 32;
1595
parameter chunk_size = 8;
1596
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
1597
input [word_size-1:0] data;
1598
input [word_size/chunk_size-1:0] parity;
1599
output parity_error;
1600 44 unneback
reg [word_size/chunk_size-1:0] error_flag;
1601 43 unneback
integer i,j;
1602
always @ (data or parity)
1603
for (i=0;i<word_size/chunk_size;i=i+1) begin
1604
    error_flag[i] = parity[i] ^ parity_type;
1605
    for (j=0;j<chunk_size;j=j+1) begin
1606 46 unneback
        error_flag[i] = data[i*chunk_size+j] ^ error_flag[i];
1607 43 unneback
    end
1608
end
1609
assign parity_error = |error_flag;
1610
endmodule
1611
 
1612 44 unneback
`endif//////////////////////////////////////////////////////////////////////
1613
////                                                              ////
1614
////  IO functions                                                ////
1615
////                                                              ////
1616
////  Description                                                 ////
1617
////  IO functions such as IOB flip-flops                         ////
1618
////                                                              ////
1619
////                                                              ////
1620
////  To Do:                                                      ////
1621
////   -                                                          ////
1622
////                                                              ////
1623
////  Author(s):                                                  ////
1624
////      - Michael Unneback, unneback@opencores.org              ////
1625
////        ORSoC AB                                              ////
1626
////                                                              ////
1627 18 unneback
//////////////////////////////////////////////////////////////////////
1628
////                                                              ////
1629 44 unneback
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1630
////                                                              ////
1631
//// This source file may be used and distributed without         ////
1632
//// restriction provided that this copyright statement is not    ////
1633
//// removed from the file and that any derivative work contains  ////
1634
//// the original copyright notice and the associated disclaimer. ////
1635
////                                                              ////
1636
//// This source file is free software; you can redistribute it   ////
1637
//// and/or modify it under the terms of the GNU Lesser General   ////
1638
//// Public License as published by the Free Software Foundation; ////
1639
//// either version 2.1 of the License, or (at your option) any   ////
1640
//// later version.                                               ////
1641
////                                                              ////
1642
//// This source is distributed in the hope that it will be       ////
1643
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1644
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1645
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1646
//// details.                                                     ////
1647
////                                                              ////
1648
//// You should have received a copy of the GNU Lesser General    ////
1649
//// Public License along with this source; if not, download it   ////
1650
//// from http://www.opencores.org/lgpl.shtml                     ////
1651
////                                                              ////
1652
//////////////////////////////////////////////////////////////////////
1653 136 unneback
`ifdef O_DFF
1654 45 unneback
`timescale 1ns/1ns
1655 44 unneback
`define MODULE o_dff
1656
module `BASE`MODULE (d_i, o_pad, clk, rst);
1657
`undef MODULE
1658
parameter width = 1;
1659 45 unneback
parameter reset_value = {width{1'b0}};
1660
input  [width-1:0]  d_i;
1661 44 unneback
output [width-1:0] o_pad;
1662
input clk, rst;
1663
wire [width-1:0] d_i_int `SYN_KEEP;
1664 45 unneback
reg  [width-1:0] o_pad_int;
1665 44 unneback
assign d_i_int = d_i;
1666
genvar i;
1667 45 unneback
generate
1668 136 unneback
for (i=0;i<width;i=i+1) begin : dffs
1669 44 unneback
    always @ (posedge clk or posedge rst)
1670
    if (rst)
1671 45 unneback
        o_pad_int[i] <= reset_value[i];
1672 44 unneback
    else
1673 45 unneback
        o_pad_int[i] <= d_i_int[i];
1674
    assign #1 o_pad[i] = o_pad_int[i];
1675 44 unneback
end
1676
endgenerate
1677
endmodule
1678
`endif
1679
 
1680 136 unneback
`ifdef IO_DFF_OE
1681 45 unneback
`timescale 1ns/1ns
1682 44 unneback
`define MODULE io_dff_oe
1683
module `BASE`MODULE ( d_i, d_o, oe, io_pad, clk, rst);
1684
`undef MODULE
1685
parameter width = 1;
1686
input  [width-1:0] d_o;
1687
output reg [width-1:0] d_i;
1688
input oe;
1689
inout [width-1:0] io_pad;
1690
input clk, rst;
1691
wire [width-1:0] oe_d `SYN_KEEP;
1692
reg [width-1:0] oe_q;
1693
reg [width-1:0] d_o_q;
1694
assign oe_d = {width{oe}};
1695
genvar i;
1696
generate
1697 136 unneback
for (i=0;i<width;i=i+1) begin : dffs
1698 44 unneback
    always @ (posedge clk or posedge rst)
1699
    if (rst)
1700
        oe_q[i] <= 1'b0;
1701
    else
1702
        oe_q[i] <= oe_d[i];
1703
    always @ (posedge clk or posedge rst)
1704
    if (rst)
1705
        d_o_q[i] <= 1'b0;
1706
    else
1707
        d_o_q[i] <= d_o[i];
1708
    always @ (posedge clk or posedge rst)
1709
    if (rst)
1710
        d_i[i] <= 1'b0;
1711
    else
1712
        d_i[i] <= io_pad[i];
1713 45 unneback
    assign #1 io_pad[i] = (oe_q[i]) ? d_o_q[i] : 1'bz;
1714 44 unneback
end
1715
endgenerate
1716
endmodule
1717
`endif
1718 136 unneback
 
1719
`ifdef O_DDR
1720
`ifdef ALTERA
1721
`define MODULE o_ddr
1722
module `BASE`MODULE (d_h_i, d_l_i, o_pad, clk, rst);
1723
`undef MODULE
1724
parameter width = 1;
1725
input  [width-1:0] d_h_i, d_l_i;
1726
output [width-1:0] o_pad;
1727
input clk, rst;
1728
genvar i;
1729
generate
1730
for (i=0;i<width;i=i+1) begin : ddr
1731
    ddio_out ddio_out0( .aclr(rst), .datain_h(d_h_i[i]), .datain_l(d_l_i[i]), .outclock(clk), .dataout(o_pad[i]) );
1732
end
1733
endgenerate
1734
endmodule
1735
`else
1736
`define MODULE o_ddr
1737
module `BASE`MODULE (d_h_i, d_l_i, o_pad, clk, rst);
1738
`undef MODULE
1739
parameter width = 1;
1740
input  [width-1:0] d_h_i, d_l_i;
1741
output [width-1:0] o_pad;
1742
input clk, rst;
1743
reg [width-1:0] ff1;
1744
reg [width-1:0] ff2;
1745
genvar i;
1746
generate
1747
for (i=0;i<width;i=i+1) begin : ddr
1748
    always @ (posedge clk or posedge rst)
1749
    if (rst)
1750
        ff1[i] <= 1'b0;
1751
    else
1752
        ff1[i] <= d_h_i[i];
1753
    always @ (posedge clk or posedge rst)
1754
    if (rst)
1755
        ff2[i] <= 1'b0;
1756
    else
1757
        ff2[i] <= d_l_i[i];
1758
    assign o_pad = (clk) ? ff1 : ff2;
1759
end
1760
endgenerate
1761
endmodule
1762
`endif
1763
`endif
1764
 
1765
`ifdef O_CLK
1766
`define MODULE o_clk
1767
module `BASE`MODULE ( clk_o_pad, clk, rst);
1768
`undef MODULE
1769
input clk, rst;
1770
output clk_o_pad;
1771
`define MODULE o_ddr
1772
`BASE`MODULE o_ddr0( .d_h_i(1'b1), .d_l_i(1'b0), .o_pad(clk_o_pad), .clk(clk), .rst(rst));
1773
`undef MODULE
1774
endmodule
1775
`endif`ifdef CNT_BIN
1776 44 unneback
//////////////////////////////////////////////////////////////////////
1777
////                                                              ////
1778 6 unneback
////  Versatile counter                                           ////
1779
////                                                              ////
1780
////  Description                                                 ////
1781
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1782
////  counter                                                     ////
1783
////                                                              ////
1784
////  To Do:                                                      ////
1785
////   - add LFSR with more taps                                  ////
1786
////                                                              ////
1787
////  Author(s):                                                  ////
1788
////      - Michael Unneback, unneback@opencores.org              ////
1789
////        ORSoC AB                                              ////
1790
////                                                              ////
1791
//////////////////////////////////////////////////////////////////////
1792
////                                                              ////
1793
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1794
////                                                              ////
1795
//// This source file may be used and distributed without         ////
1796
//// restriction provided that this copyright statement is not    ////
1797
//// removed from the file and that any derivative work contains  ////
1798
//// the original copyright notice and the associated disclaimer. ////
1799
////                                                              ////
1800
//// This source file is free software; you can redistribute it   ////
1801
//// and/or modify it under the terms of the GNU Lesser General   ////
1802
//// Public License as published by the Free Software Foundation; ////
1803
//// either version 2.1 of the License, or (at your option) any   ////
1804
//// later version.                                               ////
1805
////                                                              ////
1806
//// This source is distributed in the hope that it will be       ////
1807
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1808
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1809
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1810
//// details.                                                     ////
1811
////                                                              ////
1812
//// You should have received a copy of the GNU Lesser General    ////
1813
//// Public License along with this source; if not, download it   ////
1814
//// from http://www.opencores.org/lgpl.shtml                     ////
1815
////                                                              ////
1816
//////////////////////////////////////////////////////////////////////
1817
 
1818
// binary counter
1819 22 unneback
 
1820 40 unneback
`define MODULE cnt_bin
1821
module `BASE`MODULE (
1822
`undef MODULE
1823
 q, rst, clk);
1824
 
1825 22 unneback
   parameter length = 4;
1826
   output [length:1] q;
1827
   input rst;
1828
   input clk;
1829
 
1830
   parameter clear_value = 0;
1831
   parameter set_value = 1;
1832
   parameter wrap_value = 0;
1833
   parameter level1_value = 15;
1834
 
1835
   reg  [length:1] qi;
1836
   wire [length:1] q_next;
1837
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1838
 
1839
   always @ (posedge clk or posedge rst)
1840
     if (rst)
1841
       qi <= {length{1'b0}};
1842
     else
1843
       qi <= q_next;
1844
 
1845
   assign q = qi;
1846
 
1847
endmodule
1848 40 unneback
`endif
1849
`ifdef CNT_BIN_CLEAR
1850 22 unneback
//////////////////////////////////////////////////////////////////////
1851
////                                                              ////
1852
////  Versatile counter                                           ////
1853
////                                                              ////
1854
////  Description                                                 ////
1855
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1856
////  counter                                                     ////
1857
////                                                              ////
1858
////  To Do:                                                      ////
1859
////   - add LFSR with more taps                                  ////
1860
////                                                              ////
1861
////  Author(s):                                                  ////
1862
////      - Michael Unneback, unneback@opencores.org              ////
1863
////        ORSoC AB                                              ////
1864
////                                                              ////
1865
//////////////////////////////////////////////////////////////////////
1866
////                                                              ////
1867
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1868
////                                                              ////
1869
//// This source file may be used and distributed without         ////
1870
//// restriction provided that this copyright statement is not    ////
1871
//// removed from the file and that any derivative work contains  ////
1872
//// the original copyright notice and the associated disclaimer. ////
1873
////                                                              ////
1874
//// This source file is free software; you can redistribute it   ////
1875
//// and/or modify it under the terms of the GNU Lesser General   ////
1876
//// Public License as published by the Free Software Foundation; ////
1877
//// either version 2.1 of the License, or (at your option) any   ////
1878
//// later version.                                               ////
1879
////                                                              ////
1880
//// This source is distributed in the hope that it will be       ////
1881
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1882
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1883
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1884
//// details.                                                     ////
1885
////                                                              ////
1886
//// You should have received a copy of the GNU Lesser General    ////
1887
//// Public License along with this source; if not, download it   ////
1888
//// from http://www.opencores.org/lgpl.shtml                     ////
1889
////                                                              ////
1890
//////////////////////////////////////////////////////////////////////
1891
 
1892
// binary counter
1893
 
1894 40 unneback
`define MODULE cnt_bin_clear
1895
module `BASE`MODULE (
1896
`undef MODULE
1897
 clear, q, rst, clk);
1898
 
1899 22 unneback
   parameter length = 4;
1900
   input clear;
1901
   output [length:1] q;
1902
   input rst;
1903
   input clk;
1904
 
1905
   parameter clear_value = 0;
1906
   parameter set_value = 1;
1907
   parameter wrap_value = 0;
1908
   parameter level1_value = 15;
1909
 
1910
   reg  [length:1] qi;
1911
   wire [length:1] q_next;
1912
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1913
 
1914
   always @ (posedge clk or posedge rst)
1915
     if (rst)
1916
       qi <= {length{1'b0}};
1917
     else
1918
       qi <= q_next;
1919
 
1920
   assign q = qi;
1921
 
1922
endmodule
1923 40 unneback
`endif
1924
`ifdef CNT_BIN_CE
1925 22 unneback
//////////////////////////////////////////////////////////////////////
1926
////                                                              ////
1927
////  Versatile counter                                           ////
1928
////                                                              ////
1929
////  Description                                                 ////
1930
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1931
////  counter                                                     ////
1932
////                                                              ////
1933
////  To Do:                                                      ////
1934
////   - add LFSR with more taps                                  ////
1935
////                                                              ////
1936
////  Author(s):                                                  ////
1937
////      - Michael Unneback, unneback@opencores.org              ////
1938
////        ORSoC AB                                              ////
1939
////                                                              ////
1940
//////////////////////////////////////////////////////////////////////
1941
////                                                              ////
1942
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1943
////                                                              ////
1944
//// This source file may be used and distributed without         ////
1945
//// restriction provided that this copyright statement is not    ////
1946
//// removed from the file and that any derivative work contains  ////
1947
//// the original copyright notice and the associated disclaimer. ////
1948
////                                                              ////
1949
//// This source file is free software; you can redistribute it   ////
1950
//// and/or modify it under the terms of the GNU Lesser General   ////
1951
//// Public License as published by the Free Software Foundation; ////
1952
//// either version 2.1 of the License, or (at your option) any   ////
1953
//// later version.                                               ////
1954
////                                                              ////
1955
//// This source is distributed in the hope that it will be       ////
1956
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1957
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1958
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1959
//// details.                                                     ////
1960
////                                                              ////
1961
//// You should have received a copy of the GNU Lesser General    ////
1962
//// Public License along with this source; if not, download it   ////
1963
//// from http://www.opencores.org/lgpl.shtml                     ////
1964
////                                                              ////
1965
//////////////////////////////////////////////////////////////////////
1966
 
1967
// binary counter
1968 6 unneback
 
1969 40 unneback
`define MODULE cnt_bin_ce
1970
module `BASE`MODULE (
1971
`undef MODULE
1972
 cke, q, rst, clk);
1973
 
1974 6 unneback
   parameter length = 4;
1975
   input cke;
1976
   output [length:1] q;
1977
   input rst;
1978
   input clk;
1979
 
1980
   parameter clear_value = 0;
1981
   parameter set_value = 1;
1982
   parameter wrap_value = 0;
1983
   parameter level1_value = 15;
1984
 
1985
   reg  [length:1] qi;
1986
   wire [length:1] q_next;
1987
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1988
 
1989
   always @ (posedge clk or posedge rst)
1990
     if (rst)
1991
       qi <= {length{1'b0}};
1992
     else
1993
     if (cke)
1994
       qi <= q_next;
1995
 
1996
   assign q = qi;
1997
 
1998
endmodule
1999 40 unneback
`endif
2000
`ifdef CNT_BIN_CE_CLEAR
2001 6 unneback
//////////////////////////////////////////////////////////////////////
2002
////                                                              ////
2003
////  Versatile counter                                           ////
2004
////                                                              ////
2005
////  Description                                                 ////
2006
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2007
////  counter                                                     ////
2008
////                                                              ////
2009
////  To Do:                                                      ////
2010
////   - add LFSR with more taps                                  ////
2011
////                                                              ////
2012
////  Author(s):                                                  ////
2013
////      - Michael Unneback, unneback@opencores.org              ////
2014
////        ORSoC AB                                              ////
2015
////                                                              ////
2016
//////////////////////////////////////////////////////////////////////
2017
////                                                              ////
2018
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2019
////                                                              ////
2020
//// This source file may be used and distributed without         ////
2021
//// restriction provided that this copyright statement is not    ////
2022
//// removed from the file and that any derivative work contains  ////
2023
//// the original copyright notice and the associated disclaimer. ////
2024
////                                                              ////
2025
//// This source file is free software; you can redistribute it   ////
2026
//// and/or modify it under the terms of the GNU Lesser General   ////
2027
//// Public License as published by the Free Software Foundation; ////
2028
//// either version 2.1 of the License, or (at your option) any   ////
2029
//// later version.                                               ////
2030
////                                                              ////
2031
//// This source is distributed in the hope that it will be       ////
2032
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2033
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2034
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2035
//// details.                                                     ////
2036
////                                                              ////
2037
//// You should have received a copy of the GNU Lesser General    ////
2038
//// Public License along with this source; if not, download it   ////
2039
//// from http://www.opencores.org/lgpl.shtml                     ////
2040
////                                                              ////
2041
//////////////////////////////////////////////////////////////////////
2042
 
2043
// binary counter
2044
 
2045 40 unneback
`define MODULE cnt_bin_ce_clear
2046
module `BASE`MODULE (
2047
`undef MODULE
2048
 clear, cke, q, rst, clk);
2049
 
2050 6 unneback
   parameter length = 4;
2051
   input clear;
2052
   input cke;
2053
   output [length:1] q;
2054
   input rst;
2055
   input clk;
2056
 
2057
   parameter clear_value = 0;
2058
   parameter set_value = 1;
2059
   parameter wrap_value = 0;
2060
   parameter level1_value = 15;
2061
 
2062
   reg  [length:1] qi;
2063
   wire [length:1] q_next;
2064
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
2065
 
2066
   always @ (posedge clk or posedge rst)
2067
     if (rst)
2068
       qi <= {length{1'b0}};
2069
     else
2070
     if (cke)
2071
       qi <= q_next;
2072
 
2073
   assign q = qi;
2074
 
2075
endmodule
2076 40 unneback
`endif
2077
`ifdef CNT_BIN_CE_CLEAR_L1_L2
2078 6 unneback
//////////////////////////////////////////////////////////////////////
2079
////                                                              ////
2080
////  Versatile counter                                           ////
2081
////                                                              ////
2082
////  Description                                                 ////
2083
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2084
////  counter                                                     ////
2085
////                                                              ////
2086
////  To Do:                                                      ////
2087
////   - add LFSR with more taps                                  ////
2088
////                                                              ////
2089
////  Author(s):                                                  ////
2090
////      - Michael Unneback, unneback@opencores.org              ////
2091
////        ORSoC AB                                              ////
2092
////                                                              ////
2093
//////////////////////////////////////////////////////////////////////
2094
////                                                              ////
2095
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2096
////                                                              ////
2097
//// This source file may be used and distributed without         ////
2098
//// restriction provided that this copyright statement is not    ////
2099
//// removed from the file and that any derivative work contains  ////
2100
//// the original copyright notice and the associated disclaimer. ////
2101
////                                                              ////
2102
//// This source file is free software; you can redistribute it   ////
2103
//// and/or modify it under the terms of the GNU Lesser General   ////
2104
//// Public License as published by the Free Software Foundation; ////
2105
//// either version 2.1 of the License, or (at your option) any   ////
2106
//// later version.                                               ////
2107
////                                                              ////
2108
//// This source is distributed in the hope that it will be       ////
2109
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2110
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2111
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2112
//// details.                                                     ////
2113
////                                                              ////
2114
//// You should have received a copy of the GNU Lesser General    ////
2115
//// Public License along with this source; if not, download it   ////
2116
//// from http://www.opencores.org/lgpl.shtml                     ////
2117
////                                                              ////
2118
//////////////////////////////////////////////////////////////////////
2119
 
2120
// binary counter
2121 29 unneback
 
2122 40 unneback
`define MODULE cnt_bin_ce_clear_l1_l2
2123
module `BASE`MODULE (
2124
`undef MODULE
2125
 clear, cke, q, level1, level2, rst, clk);
2126
 
2127 29 unneback
   parameter length = 4;
2128
   input clear;
2129
   input cke;
2130
   output [length:1] q;
2131
   output reg level1;
2132
   output reg level2;
2133
   input rst;
2134
   input clk;
2135
 
2136
   parameter clear_value = 0;
2137
   parameter set_value = 1;
2138 30 unneback
   parameter wrap_value = 15;
2139
   parameter level1_value = 8;
2140
   parameter level2_value = 15;
2141 29 unneback
 
2142
   wire rew;
2143 30 unneback
   assign rew = 1'b0;
2144 29 unneback
   reg  [length:1] qi;
2145
   wire [length:1] q_next;
2146
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
2147
 
2148
   always @ (posedge clk or posedge rst)
2149
     if (rst)
2150
       qi <= {length{1'b0}};
2151
     else
2152
     if (cke)
2153
       qi <= q_next;
2154
 
2155
   assign q = qi;
2156
 
2157
 
2158
    always @ (posedge clk or posedge rst)
2159
    if (rst)
2160
        level1 <= 1'b0;
2161
    else
2162
    if (cke)
2163
    if (clear)
2164
        level1 <= 1'b0;
2165
    else if (q_next == level1_value)
2166
        level1 <= 1'b1;
2167
    else if (qi == level1_value & rew)
2168
        level1 <= 1'b0;
2169
 
2170
    always @ (posedge clk or posedge rst)
2171
    if (rst)
2172
        level2 <= 1'b0;
2173
    else
2174
    if (cke)
2175
    if (clear)
2176
        level2 <= 1'b0;
2177
    else if (q_next == level2_value)
2178
        level2 <= 1'b1;
2179
    else if (qi == level2_value & rew)
2180
        level2 <= 1'b0;
2181
endmodule
2182 40 unneback
`endif
2183
`ifdef CNT_BIN_CE_CLEAR_SET_REW
2184 29 unneback
//////////////////////////////////////////////////////////////////////
2185
////                                                              ////
2186
////  Versatile counter                                           ////
2187
////                                                              ////
2188
////  Description                                                 ////
2189
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2190
////  counter                                                     ////
2191
////                                                              ////
2192
////  To Do:                                                      ////
2193
////   - add LFSR with more taps                                  ////
2194
////                                                              ////
2195
////  Author(s):                                                  ////
2196
////      - Michael Unneback, unneback@opencores.org              ////
2197
////        ORSoC AB                                              ////
2198
////                                                              ////
2199
//////////////////////////////////////////////////////////////////////
2200
////                                                              ////
2201
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2202
////                                                              ////
2203
//// This source file may be used and distributed without         ////
2204
//// restriction provided that this copyright statement is not    ////
2205
//// removed from the file and that any derivative work contains  ////
2206
//// the original copyright notice and the associated disclaimer. ////
2207
////                                                              ////
2208
//// This source file is free software; you can redistribute it   ////
2209
//// and/or modify it under the terms of the GNU Lesser General   ////
2210
//// Public License as published by the Free Software Foundation; ////
2211
//// either version 2.1 of the License, or (at your option) any   ////
2212
//// later version.                                               ////
2213
////                                                              ////
2214
//// This source is distributed in the hope that it will be       ////
2215
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2216
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2217
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2218
//// details.                                                     ////
2219
////                                                              ////
2220
//// You should have received a copy of the GNU Lesser General    ////
2221
//// Public License along with this source; if not, download it   ////
2222
//// from http://www.opencores.org/lgpl.shtml                     ////
2223
////                                                              ////
2224
//////////////////////////////////////////////////////////////////////
2225
 
2226
// binary counter
2227 6 unneback
 
2228 40 unneback
`define MODULE cnt_bin_ce_clear_set_rew
2229
module `BASE`MODULE (
2230
`undef MODULE
2231
 clear, set, cke, rew, q, rst, clk);
2232
 
2233 6 unneback
   parameter length = 4;
2234
   input clear;
2235
   input set;
2236
   input cke;
2237
   input rew;
2238
   output [length:1] q;
2239
   input rst;
2240
   input clk;
2241
 
2242
   parameter clear_value = 0;
2243
   parameter set_value = 1;
2244
   parameter wrap_value = 0;
2245
   parameter level1_value = 15;
2246
 
2247
   reg  [length:1] qi;
2248
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2249
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
2250
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
2251
   assign q_next = rew ? q_next_rew : q_next_fw;
2252
 
2253
   always @ (posedge clk or posedge rst)
2254
     if (rst)
2255
       qi <= {length{1'b0}};
2256
     else
2257
     if (cke)
2258
       qi <= q_next;
2259
 
2260
   assign q = qi;
2261
 
2262
endmodule
2263 40 unneback
`endif
2264
`ifdef CNT_BIN_CE_REW_L1
2265 6 unneback
//////////////////////////////////////////////////////////////////////
2266
////                                                              ////
2267
////  Versatile counter                                           ////
2268
////                                                              ////
2269
////  Description                                                 ////
2270
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2271
////  counter                                                     ////
2272
////                                                              ////
2273
////  To Do:                                                      ////
2274
////   - add LFSR with more taps                                  ////
2275
////                                                              ////
2276
////  Author(s):                                                  ////
2277
////      - Michael Unneback, unneback@opencores.org              ////
2278
////        ORSoC AB                                              ////
2279
////                                                              ////
2280
//////////////////////////////////////////////////////////////////////
2281
////                                                              ////
2282
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2283
////                                                              ////
2284
//// This source file may be used and distributed without         ////
2285
//// restriction provided that this copyright statement is not    ////
2286
//// removed from the file and that any derivative work contains  ////
2287
//// the original copyright notice and the associated disclaimer. ////
2288
////                                                              ////
2289
//// This source file is free software; you can redistribute it   ////
2290
//// and/or modify it under the terms of the GNU Lesser General   ////
2291
//// Public License as published by the Free Software Foundation; ////
2292
//// either version 2.1 of the License, or (at your option) any   ////
2293
//// later version.                                               ////
2294
////                                                              ////
2295
//// This source is distributed in the hope that it will be       ////
2296
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2297
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2298
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2299
//// details.                                                     ////
2300
////                                                              ////
2301
//// You should have received a copy of the GNU Lesser General    ////
2302
//// Public License along with this source; if not, download it   ////
2303
//// from http://www.opencores.org/lgpl.shtml                     ////
2304
////                                                              ////
2305
//////////////////////////////////////////////////////////////////////
2306
 
2307
// binary counter
2308
 
2309 40 unneback
`define MODULE cnt_bin_ce_rew_l1
2310
module `BASE`MODULE (
2311
`undef MODULE
2312
 cke, rew, level1, rst, clk);
2313
 
2314 6 unneback
   parameter length = 4;
2315
   input cke;
2316
   input rew;
2317
   output reg level1;
2318
   input rst;
2319
   input clk;
2320
 
2321
   parameter clear_value = 0;
2322
   parameter set_value = 1;
2323
   parameter wrap_value = 1;
2324
   parameter level1_value = 15;
2325
 
2326 29 unneback
   wire clear;
2327 30 unneback
   assign clear = 1'b0;
2328 6 unneback
   reg  [length:1] qi;
2329
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2330
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2331
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2332
   assign q_next = rew ? q_next_rew : q_next_fw;
2333
 
2334
   always @ (posedge clk or posedge rst)
2335
     if (rst)
2336
       qi <= {length{1'b0}};
2337
     else
2338
     if (cke)
2339
       qi <= q_next;
2340
 
2341
 
2342
 
2343
    always @ (posedge clk or posedge rst)
2344
    if (rst)
2345
        level1 <= 1'b0;
2346
    else
2347
    if (cke)
2348 29 unneback
    if (clear)
2349
        level1 <= 1'b0;
2350
    else if (q_next == level1_value)
2351 6 unneback
        level1 <= 1'b1;
2352
    else if (qi == level1_value & rew)
2353
        level1 <= 1'b0;
2354
endmodule
2355 40 unneback
`endif
2356
`ifdef CNT_BIN_CE_REW_ZQ_L1
2357 6 unneback
//////////////////////////////////////////////////////////////////////
2358
////                                                              ////
2359
////  Versatile counter                                           ////
2360
////                                                              ////
2361
////  Description                                                 ////
2362
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2363
////  counter                                                     ////
2364
////                                                              ////
2365
////  To Do:                                                      ////
2366
////   - add LFSR with more taps                                  ////
2367
////                                                              ////
2368
////  Author(s):                                                  ////
2369
////      - Michael Unneback, unneback@opencores.org              ////
2370
////        ORSoC AB                                              ////
2371
////                                                              ////
2372
//////////////////////////////////////////////////////////////////////
2373
////                                                              ////
2374
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2375
////                                                              ////
2376
//// This source file may be used and distributed without         ////
2377
//// restriction provided that this copyright statement is not    ////
2378
//// removed from the file and that any derivative work contains  ////
2379
//// the original copyright notice and the associated disclaimer. ////
2380
////                                                              ////
2381
//// This source file is free software; you can redistribute it   ////
2382
//// and/or modify it under the terms of the GNU Lesser General   ////
2383
//// Public License as published by the Free Software Foundation; ////
2384
//// either version 2.1 of the License, or (at your option) any   ////
2385
//// later version.                                               ////
2386
////                                                              ////
2387
//// This source is distributed in the hope that it will be       ////
2388
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2389
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2390
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2391
//// details.                                                     ////
2392
////                                                              ////
2393
//// You should have received a copy of the GNU Lesser General    ////
2394
//// Public License along with this source; if not, download it   ////
2395
//// from http://www.opencores.org/lgpl.shtml                     ////
2396
////                                                              ////
2397
//////////////////////////////////////////////////////////////////////
2398
 
2399 25 unneback
// binary counter
2400
 
2401 40 unneback
`define MODULE cnt_bin_ce_rew_zq_l1
2402
module `BASE`MODULE (
2403
`undef MODULE
2404
 cke, rew, zq, level1, rst, clk);
2405
 
2406 25 unneback
   parameter length = 4;
2407
   input cke;
2408
   input rew;
2409
   output reg zq;
2410
   output reg level1;
2411
   input rst;
2412
   input clk;
2413
 
2414
   parameter clear_value = 0;
2415
   parameter set_value = 1;
2416
   parameter wrap_value = 1;
2417
   parameter level1_value = 15;
2418
 
2419 29 unneback
   wire clear;
2420 30 unneback
   assign clear = 1'b0;
2421 25 unneback
   reg  [length:1] qi;
2422
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2423
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2424
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2425
   assign q_next = rew ? q_next_rew : q_next_fw;
2426
 
2427
   always @ (posedge clk or posedge rst)
2428
     if (rst)
2429
       qi <= {length{1'b0}};
2430
     else
2431
     if (cke)
2432
       qi <= q_next;
2433
 
2434
 
2435
 
2436
   always @ (posedge clk or posedge rst)
2437
     if (rst)
2438
       zq <= 1'b1;
2439
     else
2440
     if (cke)
2441
       zq <= q_next == {length{1'b0}};
2442
 
2443
    always @ (posedge clk or posedge rst)
2444
    if (rst)
2445
        level1 <= 1'b0;
2446
    else
2447
    if (cke)
2448 29 unneback
    if (clear)
2449
        level1 <= 1'b0;
2450
    else if (q_next == level1_value)
2451 25 unneback
        level1 <= 1'b1;
2452
    else if (qi == level1_value & rew)
2453
        level1 <= 1'b0;
2454
endmodule
2455 40 unneback
`endif
2456
`ifdef CNT_BIN_CE_REW_Q_ZQ_L1
2457 25 unneback
//////////////////////////////////////////////////////////////////////
2458
////                                                              ////
2459
////  Versatile counter                                           ////
2460
////                                                              ////
2461
////  Description                                                 ////
2462
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2463
////  counter                                                     ////
2464
////                                                              ////
2465
////  To Do:                                                      ////
2466
////   - add LFSR with more taps                                  ////
2467
////                                                              ////
2468
////  Author(s):                                                  ////
2469
////      - Michael Unneback, unneback@opencores.org              ////
2470
////        ORSoC AB                                              ////
2471
////                                                              ////
2472
//////////////////////////////////////////////////////////////////////
2473
////                                                              ////
2474
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2475
////                                                              ////
2476
//// This source file may be used and distributed without         ////
2477
//// restriction provided that this copyright statement is not    ////
2478
//// removed from the file and that any derivative work contains  ////
2479
//// the original copyright notice and the associated disclaimer. ////
2480
////                                                              ////
2481
//// This source file is free software; you can redistribute it   ////
2482
//// and/or modify it under the terms of the GNU Lesser General   ////
2483
//// Public License as published by the Free Software Foundation; ////
2484
//// either version 2.1 of the License, or (at your option) any   ////
2485
//// later version.                                               ////
2486
////                                                              ////
2487
//// This source is distributed in the hope that it will be       ////
2488
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2489
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2490
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2491
//// details.                                                     ////
2492
////                                                              ////
2493
//// You should have received a copy of the GNU Lesser General    ////
2494
//// Public License along with this source; if not, download it   ////
2495
//// from http://www.opencores.org/lgpl.shtml                     ////
2496
////                                                              ////
2497
//////////////////////////////////////////////////////////////////////
2498
 
2499
// binary counter
2500
 
2501 40 unneback
`define MODULE cnt_bin_ce_rew_q_zq_l1
2502
module `BASE`MODULE (
2503
`undef MODULE
2504
 cke, rew, q, zq, level1, rst, clk);
2505
 
2506 25 unneback
   parameter length = 4;
2507
   input cke;
2508
   input rew;
2509
   output [length:1] q;
2510
   output reg zq;
2511
   output reg level1;
2512
   input rst;
2513
   input clk;
2514
 
2515
   parameter clear_value = 0;
2516
   parameter set_value = 1;
2517
   parameter wrap_value = 1;
2518
   parameter level1_value = 15;
2519
 
2520 29 unneback
   wire clear;
2521 30 unneback
   assign clear = 1'b0;
2522 25 unneback
   reg  [length:1] qi;
2523
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2524
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
2525
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
2526
   assign q_next = rew ? q_next_rew : q_next_fw;
2527
 
2528
   always @ (posedge clk or posedge rst)
2529
     if (rst)
2530
       qi <= {length{1'b0}};
2531
     else
2532
     if (cke)
2533
       qi <= q_next;
2534
 
2535
   assign q = qi;
2536
 
2537
 
2538
   always @ (posedge clk or posedge rst)
2539
     if (rst)
2540
       zq <= 1'b1;
2541
     else
2542
     if (cke)
2543
       zq <= q_next == {length{1'b0}};
2544
 
2545
    always @ (posedge clk or posedge rst)
2546
    if (rst)
2547
        level1 <= 1'b0;
2548
    else
2549
    if (cke)
2550 29 unneback
    if (clear)
2551
        level1 <= 1'b0;
2552
    else if (q_next == level1_value)
2553 25 unneback
        level1 <= 1'b1;
2554
    else if (qi == level1_value & rew)
2555
        level1 <= 1'b0;
2556
endmodule
2557 40 unneback
`endif
2558
`ifdef CNT_LFSR_ZQ
2559 25 unneback
//////////////////////////////////////////////////////////////////////
2560
////                                                              ////
2561
////  Versatile counter                                           ////
2562
////                                                              ////
2563
////  Description                                                 ////
2564
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2565
////  counter                                                     ////
2566
////                                                              ////
2567
////  To Do:                                                      ////
2568
////   - add LFSR with more taps                                  ////
2569
////                                                              ////
2570
////  Author(s):                                                  ////
2571
////      - Michael Unneback, unneback@opencores.org              ////
2572
////        ORSoC AB                                              ////
2573
////                                                              ////
2574
//////////////////////////////////////////////////////////////////////
2575
////                                                              ////
2576
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2577
////                                                              ////
2578
//// This source file may be used and distributed without         ////
2579
//// restriction provided that this copyright statement is not    ////
2580
//// removed from the file and that any derivative work contains  ////
2581
//// the original copyright notice and the associated disclaimer. ////
2582
////                                                              ////
2583
//// This source file is free software; you can redistribute it   ////
2584
//// and/or modify it under the terms of the GNU Lesser General   ////
2585
//// Public License as published by the Free Software Foundation; ////
2586
//// either version 2.1 of the License, or (at your option) any   ////
2587
//// later version.                                               ////
2588
////                                                              ////
2589
//// This source is distributed in the hope that it will be       ////
2590
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2591
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2592
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2593
//// details.                                                     ////
2594
////                                                              ////
2595
//// You should have received a copy of the GNU Lesser General    ////
2596
//// Public License along with this source; if not, download it   ////
2597
//// from http://www.opencores.org/lgpl.shtml                     ////
2598
////                                                              ////
2599
//////////////////////////////////////////////////////////////////////
2600
 
2601 6 unneback
// LFSR counter
2602
 
2603 40 unneback
`define MODULE cnt_lfsr_zq
2604
module `BASE`MODULE (
2605
`undef MODULE
2606
 zq, rst, clk);
2607
 
2608 6 unneback
   parameter length = 4;
2609
   output reg zq;
2610
   input rst;
2611
   input clk;
2612
 
2613
   parameter clear_value = 0;
2614
   parameter set_value = 1;
2615
   parameter wrap_value = 8;
2616
   parameter level1_value = 15;
2617
 
2618
   reg  [length:1] qi;
2619
   reg lfsr_fb;
2620
   wire [length:1] q_next;
2621
   reg [32:1] polynom;
2622
   integer i;
2623
 
2624
   always @ (qi)
2625
   begin
2626
        case (length)
2627
         2: polynom = 32'b11;                               // 0x3
2628
         3: polynom = 32'b110;                              // 0x6
2629
         4: polynom = 32'b1100;                             // 0xC
2630
         5: polynom = 32'b10100;                            // 0x14
2631
         6: polynom = 32'b110000;                           // 0x30
2632
         7: polynom = 32'b1100000;                          // 0x60
2633
         8: polynom = 32'b10111000;                         // 0xb8
2634
         9: polynom = 32'b100010000;                        // 0x110
2635
        10: polynom = 32'b1001000000;                       // 0x240
2636
        11: polynom = 32'b10100000000;                      // 0x500
2637
        12: polynom = 32'b100000101001;                     // 0x829
2638
        13: polynom = 32'b1000000001100;                    // 0x100C
2639
        14: polynom = 32'b10000000010101;                   // 0x2015
2640
        15: polynom = 32'b110000000000000;                  // 0x6000
2641
        16: polynom = 32'b1101000000001000;                 // 0xD008
2642
        17: polynom = 32'b10010000000000000;                // 0x12000
2643
        18: polynom = 32'b100000010000000000;               // 0x20400
2644
        19: polynom = 32'b1000000000000100011;              // 0x40023
2645 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2646 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2647
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2648
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2649
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2650
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2651
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2652
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2653
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2654
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2655
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2656
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2657
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2658
        default: polynom = 32'b0;
2659
        endcase
2660
        lfsr_fb = qi[length];
2661
        for (i=length-1; i>=1; i=i-1) begin
2662
            if (polynom[i])
2663
                lfsr_fb = lfsr_fb  ~^ qi[i];
2664
        end
2665
    end
2666
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2667
 
2668
   always @ (posedge clk or posedge rst)
2669
     if (rst)
2670
       qi <= {length{1'b0}};
2671
     else
2672
       qi <= q_next;
2673
 
2674
 
2675
 
2676
   always @ (posedge clk or posedge rst)
2677
     if (rst)
2678
       zq <= 1'b1;
2679
     else
2680
       zq <= q_next == {length{1'b0}};
2681
endmodule
2682 40 unneback
`endif
2683 75 unneback
`ifdef CNT_LFSR_CE
2684
//////////////////////////////////////////////////////////////////////
2685
////                                                              ////
2686
////  Versatile counter                                           ////
2687
////                                                              ////
2688
////  Description                                                 ////
2689
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2690
////  counter                                                     ////
2691
////                                                              ////
2692
////  To Do:                                                      ////
2693
////   - add LFSR with more taps                                  ////
2694
////                                                              ////
2695
////  Author(s):                                                  ////
2696
////      - Michael Unneback, unneback@opencores.org              ////
2697
////        ORSoC AB                                              ////
2698
////                                                              ////
2699
//////////////////////////////////////////////////////////////////////
2700
////                                                              ////
2701
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2702
////                                                              ////
2703
//// This source file may be used and distributed without         ////
2704
//// restriction provided that this copyright statement is not    ////
2705
//// removed from the file and that any derivative work contains  ////
2706
//// the original copyright notice and the associated disclaimer. ////
2707
////                                                              ////
2708
//// This source file is free software; you can redistribute it   ////
2709
//// and/or modify it under the terms of the GNU Lesser General   ////
2710
//// Public License as published by the Free Software Foundation; ////
2711
//// either version 2.1 of the License, or (at your option) any   ////
2712
//// later version.                                               ////
2713
////                                                              ////
2714
//// This source is distributed in the hope that it will be       ////
2715
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2716
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2717
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2718
//// details.                                                     ////
2719
////                                                              ////
2720
//// You should have received a copy of the GNU Lesser General    ////
2721
//// Public License along with this source; if not, download it   ////
2722
//// from http://www.opencores.org/lgpl.shtml                     ////
2723
////                                                              ////
2724
//////////////////////////////////////////////////////////////////////
2725
 
2726
// LFSR counter
2727
 
2728
`define MODULE cnt_lfsr_ce
2729
module `BASE`MODULE (
2730
`undef MODULE
2731
 cke, zq, rst, clk);
2732
 
2733
   parameter length = 4;
2734
   input cke;
2735
   output reg zq;
2736
   input rst;
2737
   input clk;
2738
 
2739
   parameter clear_value = 0;
2740
   parameter set_value = 1;
2741
   parameter wrap_value = 0;
2742
   parameter level1_value = 15;
2743
 
2744
   reg  [length:1] qi;
2745
   reg lfsr_fb;
2746
   wire [length:1] q_next;
2747
   reg [32:1] polynom;
2748
   integer i;
2749
 
2750
   always @ (qi)
2751
   begin
2752
        case (length)
2753
         2: polynom = 32'b11;                               // 0x3
2754
         3: polynom = 32'b110;                              // 0x6
2755
         4: polynom = 32'b1100;                             // 0xC
2756
         5: polynom = 32'b10100;                            // 0x14
2757
         6: polynom = 32'b110000;                           // 0x30
2758
         7: polynom = 32'b1100000;                          // 0x60
2759
         8: polynom = 32'b10111000;                         // 0xb8
2760
         9: polynom = 32'b100010000;                        // 0x110
2761
        10: polynom = 32'b1001000000;                       // 0x240
2762
        11: polynom = 32'b10100000000;                      // 0x500
2763
        12: polynom = 32'b100000101001;                     // 0x829
2764
        13: polynom = 32'b1000000001100;                    // 0x100C
2765
        14: polynom = 32'b10000000010101;                   // 0x2015
2766
        15: polynom = 32'b110000000000000;                  // 0x6000
2767
        16: polynom = 32'b1101000000001000;                 // 0xD008
2768
        17: polynom = 32'b10010000000000000;                // 0x12000
2769
        18: polynom = 32'b100000010000000000;               // 0x20400
2770
        19: polynom = 32'b1000000000000100011;              // 0x40023
2771
        20: polynom = 32'b10010000000000000000;             // 0x90000
2772
        21: polynom = 32'b101000000000000000000;            // 0x140000
2773
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2774
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2775
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2776
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2777
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2778
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2779
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2780
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2781
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2782
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2783
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2784
        default: polynom = 32'b0;
2785
        endcase
2786
        lfsr_fb = qi[length];
2787
        for (i=length-1; i>=1; i=i-1) begin
2788
            if (polynom[i])
2789
                lfsr_fb = lfsr_fb  ~^ qi[i];
2790
        end
2791
    end
2792
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2793
 
2794
   always @ (posedge clk or posedge rst)
2795
     if (rst)
2796
       qi <= {length{1'b0}};
2797
     else
2798
     if (cke)
2799
       qi <= q_next;
2800
 
2801
 
2802
 
2803
   always @ (posedge clk or posedge rst)
2804
     if (rst)
2805
       zq <= 1'b1;
2806
     else
2807
     if (cke)
2808
       zq <= q_next == {length{1'b0}};
2809
endmodule
2810
`endif
2811 40 unneback
`ifdef CNT_LFSR_CE_ZQ
2812 6 unneback
//////////////////////////////////////////////////////////////////////
2813
////                                                              ////
2814
////  Versatile counter                                           ////
2815
////                                                              ////
2816
////  Description                                                 ////
2817
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2818
////  counter                                                     ////
2819
////                                                              ////
2820
////  To Do:                                                      ////
2821
////   - add LFSR with more taps                                  ////
2822
////                                                              ////
2823
////  Author(s):                                                  ////
2824
////      - Michael Unneback, unneback@opencores.org              ////
2825
////        ORSoC AB                                              ////
2826
////                                                              ////
2827
//////////////////////////////////////////////////////////////////////
2828
////                                                              ////
2829
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2830
////                                                              ////
2831
//// This source file may be used and distributed without         ////
2832
//// restriction provided that this copyright statement is not    ////
2833
//// removed from the file and that any derivative work contains  ////
2834
//// the original copyright notice and the associated disclaimer. ////
2835
////                                                              ////
2836
//// This source file is free software; you can redistribute it   ////
2837
//// and/or modify it under the terms of the GNU Lesser General   ////
2838
//// Public License as published by the Free Software Foundation; ////
2839
//// either version 2.1 of the License, or (at your option) any   ////
2840
//// later version.                                               ////
2841
////                                                              ////
2842
//// This source is distributed in the hope that it will be       ////
2843
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2844
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2845
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2846
//// details.                                                     ////
2847
////                                                              ////
2848
//// You should have received a copy of the GNU Lesser General    ////
2849
//// Public License along with this source; if not, download it   ////
2850
//// from http://www.opencores.org/lgpl.shtml                     ////
2851
////                                                              ////
2852
//////////////////////////////////////////////////////////////////////
2853
 
2854
// LFSR counter
2855
 
2856 40 unneback
`define MODULE cnt_lfsr_ce_zq
2857
module `BASE`MODULE (
2858
`undef MODULE
2859
 cke, zq, rst, clk);
2860
 
2861 6 unneback
   parameter length = 4;
2862
   input cke;
2863
   output reg zq;
2864
   input rst;
2865
   input clk;
2866
 
2867
   parameter clear_value = 0;
2868
   parameter set_value = 1;
2869
   parameter wrap_value = 8;
2870
   parameter level1_value = 15;
2871
 
2872
   reg  [length:1] qi;
2873
   reg lfsr_fb;
2874
   wire [length:1] q_next;
2875
   reg [32:1] polynom;
2876
   integer i;
2877
 
2878
   always @ (qi)
2879
   begin
2880
        case (length)
2881
         2: polynom = 32'b11;                               // 0x3
2882
         3: polynom = 32'b110;                              // 0x6
2883
         4: polynom = 32'b1100;                             // 0xC
2884
         5: polynom = 32'b10100;                            // 0x14
2885
         6: polynom = 32'b110000;                           // 0x30
2886
         7: polynom = 32'b1100000;                          // 0x60
2887
         8: polynom = 32'b10111000;                         // 0xb8
2888
         9: polynom = 32'b100010000;                        // 0x110
2889
        10: polynom = 32'b1001000000;                       // 0x240
2890
        11: polynom = 32'b10100000000;                      // 0x500
2891
        12: polynom = 32'b100000101001;                     // 0x829
2892
        13: polynom = 32'b1000000001100;                    // 0x100C
2893
        14: polynom = 32'b10000000010101;                   // 0x2015
2894
        15: polynom = 32'b110000000000000;                  // 0x6000
2895
        16: polynom = 32'b1101000000001000;                 // 0xD008
2896
        17: polynom = 32'b10010000000000000;                // 0x12000
2897
        18: polynom = 32'b100000010000000000;               // 0x20400
2898
        19: polynom = 32'b1000000000000100011;              // 0x40023
2899 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
2900 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
2901
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2902
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2903
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2904
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2905
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2906
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2907
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2908
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2909
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2910
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2911
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2912
        default: polynom = 32'b0;
2913
        endcase
2914
        lfsr_fb = qi[length];
2915
        for (i=length-1; i>=1; i=i-1) begin
2916
            if (polynom[i])
2917
                lfsr_fb = lfsr_fb  ~^ qi[i];
2918
        end
2919
    end
2920
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2921
 
2922
   always @ (posedge clk or posedge rst)
2923
     if (rst)
2924
       qi <= {length{1'b0}};
2925
     else
2926
     if (cke)
2927
       qi <= q_next;
2928
 
2929
 
2930
 
2931
   always @ (posedge clk or posedge rst)
2932
     if (rst)
2933
       zq <= 1'b1;
2934
     else
2935
     if (cke)
2936
       zq <= q_next == {length{1'b0}};
2937
endmodule
2938 40 unneback
`endif
2939
`ifdef CNT_LFSR_CE_Q
2940 6 unneback
//////////////////////////////////////////////////////////////////////
2941
////                                                              ////
2942
////  Versatile counter                                           ////
2943
////                                                              ////
2944
////  Description                                                 ////
2945
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2946
////  counter                                                     ////
2947
////                                                              ////
2948
////  To Do:                                                      ////
2949
////   - add LFSR with more taps                                  ////
2950
////                                                              ////
2951
////  Author(s):                                                  ////
2952
////      - Michael Unneback, unneback@opencores.org              ////
2953
////        ORSoC AB                                              ////
2954
////                                                              ////
2955
//////////////////////////////////////////////////////////////////////
2956
////                                                              ////
2957
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2958
////                                                              ////
2959
//// This source file may be used and distributed without         ////
2960
//// restriction provided that this copyright statement is not    ////
2961
//// removed from the file and that any derivative work contains  ////
2962
//// the original copyright notice and the associated disclaimer. ////
2963
////                                                              ////
2964
//// This source file is free software; you can redistribute it   ////
2965
//// and/or modify it under the terms of the GNU Lesser General   ////
2966
//// Public License as published by the Free Software Foundation; ////
2967
//// either version 2.1 of the License, or (at your option) any   ////
2968
//// later version.                                               ////
2969
////                                                              ////
2970
//// This source is distributed in the hope that it will be       ////
2971
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2972
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2973
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2974
//// details.                                                     ////
2975
////                                                              ////
2976
//// You should have received a copy of the GNU Lesser General    ////
2977
//// Public License along with this source; if not, download it   ////
2978
//// from http://www.opencores.org/lgpl.shtml                     ////
2979
////                                                              ////
2980
//////////////////////////////////////////////////////////////////////
2981 22 unneback
 
2982
// LFSR counter
2983 27 unneback
 
2984 40 unneback
`define MODULE cnt_lfsr_ce_q
2985
module `BASE`MODULE (
2986
`undef MODULE
2987
 cke, q, rst, clk);
2988
 
2989 27 unneback
   parameter length = 4;
2990
   input cke;
2991
   output [length:1] q;
2992
   input rst;
2993
   input clk;
2994
 
2995
   parameter clear_value = 0;
2996
   parameter set_value = 1;
2997
   parameter wrap_value = 8;
2998
   parameter level1_value = 15;
2999
 
3000
   reg  [length:1] qi;
3001
   reg lfsr_fb;
3002
   wire [length:1] q_next;
3003
   reg [32:1] polynom;
3004
   integer i;
3005
 
3006
   always @ (qi)
3007
   begin
3008
        case (length)
3009
         2: polynom = 32'b11;                               // 0x3
3010
         3: polynom = 32'b110;                              // 0x6
3011
         4: polynom = 32'b1100;                             // 0xC
3012
         5: polynom = 32'b10100;                            // 0x14
3013
         6: polynom = 32'b110000;                           // 0x30
3014
         7: polynom = 32'b1100000;                          // 0x60
3015
         8: polynom = 32'b10111000;                         // 0xb8
3016
         9: polynom = 32'b100010000;                        // 0x110
3017
        10: polynom = 32'b1001000000;                       // 0x240
3018
        11: polynom = 32'b10100000000;                      // 0x500
3019
        12: polynom = 32'b100000101001;                     // 0x829
3020
        13: polynom = 32'b1000000001100;                    // 0x100C
3021
        14: polynom = 32'b10000000010101;                   // 0x2015
3022
        15: polynom = 32'b110000000000000;                  // 0x6000
3023
        16: polynom = 32'b1101000000001000;                 // 0xD008
3024
        17: polynom = 32'b10010000000000000;                // 0x12000
3025
        18: polynom = 32'b100000010000000000;               // 0x20400
3026
        19: polynom = 32'b1000000000000100011;              // 0x40023
3027 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3028 27 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3029
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3030
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3031
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3032
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3033
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3034
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3035
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3036
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3037
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3038
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3039
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3040
        default: polynom = 32'b0;
3041
        endcase
3042
        lfsr_fb = qi[length];
3043
        for (i=length-1; i>=1; i=i-1) begin
3044
            if (polynom[i])
3045
                lfsr_fb = lfsr_fb  ~^ qi[i];
3046
        end
3047
    end
3048
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3049
 
3050
   always @ (posedge clk or posedge rst)
3051
     if (rst)
3052
       qi <= {length{1'b0}};
3053
     else
3054
     if (cke)
3055
       qi <= q_next;
3056
 
3057
   assign q = qi;
3058
 
3059
endmodule
3060 40 unneback
`endif
3061
`ifdef CNT_LFSR_CE_CLEAR_Q
3062 27 unneback
//////////////////////////////////////////////////////////////////////
3063
////                                                              ////
3064
////  Versatile counter                                           ////
3065
////                                                              ////
3066
////  Description                                                 ////
3067
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3068
////  counter                                                     ////
3069
////                                                              ////
3070
////  To Do:                                                      ////
3071
////   - add LFSR with more taps                                  ////
3072
////                                                              ////
3073
////  Author(s):                                                  ////
3074
////      - Michael Unneback, unneback@opencores.org              ////
3075
////        ORSoC AB                                              ////
3076
////                                                              ////
3077
//////////////////////////////////////////////////////////////////////
3078
////                                                              ////
3079
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3080
////                                                              ////
3081
//// This source file may be used and distributed without         ////
3082
//// restriction provided that this copyright statement is not    ////
3083
//// removed from the file and that any derivative work contains  ////
3084
//// the original copyright notice and the associated disclaimer. ////
3085
////                                                              ////
3086
//// This source file is free software; you can redistribute it   ////
3087
//// and/or modify it under the terms of the GNU Lesser General   ////
3088
//// Public License as published by the Free Software Foundation; ////
3089
//// either version 2.1 of the License, or (at your option) any   ////
3090
//// later version.                                               ////
3091
////                                                              ////
3092
//// This source is distributed in the hope that it will be       ////
3093
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3094
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3095
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3096
//// details.                                                     ////
3097
////                                                              ////
3098
//// You should have received a copy of the GNU Lesser General    ////
3099
//// Public License along with this source; if not, download it   ////
3100
//// from http://www.opencores.org/lgpl.shtml                     ////
3101
////                                                              ////
3102
//////////////////////////////////////////////////////////////////////
3103
 
3104
// LFSR counter
3105
 
3106 40 unneback
`define MODULE cnt_lfsr_ce_clear_q
3107
module `BASE`MODULE (
3108
`undef MODULE
3109
 clear, cke, q, rst, clk);
3110
 
3111 27 unneback
   parameter length = 4;
3112
   input clear;
3113
   input cke;
3114
   output [length:1] q;
3115
   input rst;
3116
   input clk;
3117
 
3118
   parameter clear_value = 0;
3119
   parameter set_value = 1;
3120
   parameter wrap_value = 8;
3121
   parameter level1_value = 15;
3122
 
3123
   reg  [length:1] qi;
3124
   reg lfsr_fb;
3125
   wire [length:1] q_next;
3126
   reg [32:1] polynom;
3127
   integer i;
3128
 
3129
   always @ (qi)
3130
   begin
3131
        case (length)
3132
         2: polynom = 32'b11;                               // 0x3
3133
         3: polynom = 32'b110;                              // 0x6
3134
         4: polynom = 32'b1100;                             // 0xC
3135
         5: polynom = 32'b10100;                            // 0x14
3136
         6: polynom = 32'b110000;                           // 0x30
3137
         7: polynom = 32'b1100000;                          // 0x60
3138
         8: polynom = 32'b10111000;                         // 0xb8
3139
         9: polynom = 32'b100010000;                        // 0x110
3140
        10: polynom = 32'b1001000000;                       // 0x240
3141
        11: polynom = 32'b10100000000;                      // 0x500
3142
        12: polynom = 32'b100000101001;                     // 0x829
3143
        13: polynom = 32'b1000000001100;                    // 0x100C
3144
        14: polynom = 32'b10000000010101;                   // 0x2015
3145
        15: polynom = 32'b110000000000000;                  // 0x6000
3146
        16: polynom = 32'b1101000000001000;                 // 0xD008
3147
        17: polynom = 32'b10010000000000000;                // 0x12000
3148
        18: polynom = 32'b100000010000000000;               // 0x20400
3149
        19: polynom = 32'b1000000000000100011;              // 0x40023
3150 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3151 27 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3152
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3153
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3154
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3155
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3156
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3157
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3158
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3159
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3160
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3161
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3162
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3163
        default: polynom = 32'b0;
3164
        endcase
3165
        lfsr_fb = qi[length];
3166
        for (i=length-1; i>=1; i=i-1) begin
3167
            if (polynom[i])
3168
                lfsr_fb = lfsr_fb  ~^ qi[i];
3169
        end
3170
    end
3171
   assign q_next =  clear ? {length{1'b0}} :(qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3172
 
3173
   always @ (posedge clk or posedge rst)
3174
     if (rst)
3175
       qi <= {length{1'b0}};
3176
     else
3177
     if (cke)
3178
       qi <= q_next;
3179
 
3180
   assign q = qi;
3181
 
3182
endmodule
3183 40 unneback
`endif
3184
`ifdef CNT_LFSR_CE_Q_ZQ
3185 27 unneback
//////////////////////////////////////////////////////////////////////
3186
////                                                              ////
3187
////  Versatile counter                                           ////
3188
////                                                              ////
3189
////  Description                                                 ////
3190
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3191
////  counter                                                     ////
3192
////                                                              ////
3193
////  To Do:                                                      ////
3194
////   - add LFSR with more taps                                  ////
3195
////                                                              ////
3196
////  Author(s):                                                  ////
3197
////      - Michael Unneback, unneback@opencores.org              ////
3198
////        ORSoC AB                                              ////
3199
////                                                              ////
3200
//////////////////////////////////////////////////////////////////////
3201
////                                                              ////
3202
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3203
////                                                              ////
3204
//// This source file may be used and distributed without         ////
3205
//// restriction provided that this copyright statement is not    ////
3206
//// removed from the file and that any derivative work contains  ////
3207
//// the original copyright notice and the associated disclaimer. ////
3208
////                                                              ////
3209
//// This source file is free software; you can redistribute it   ////
3210
//// and/or modify it under the terms of the GNU Lesser General   ////
3211
//// Public License as published by the Free Software Foundation; ////
3212
//// either version 2.1 of the License, or (at your option) any   ////
3213
//// later version.                                               ////
3214
////                                                              ////
3215
//// This source is distributed in the hope that it will be       ////
3216
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3217
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3218
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3219
//// details.                                                     ////
3220
////                                                              ////
3221
//// You should have received a copy of the GNU Lesser General    ////
3222
//// Public License along with this source; if not, download it   ////
3223
//// from http://www.opencores.org/lgpl.shtml                     ////
3224
////                                                              ////
3225
//////////////////////////////////////////////////////////////////////
3226
 
3227
// LFSR counter
3228 22 unneback
 
3229 40 unneback
`define MODULE cnt_lfsr_ce_q_zq
3230
module `BASE`MODULE (
3231
`undef MODULE
3232
 cke, q, zq, rst, clk);
3233
 
3234 22 unneback
   parameter length = 4;
3235
   input cke;
3236
   output [length:1] q;
3237
   output reg zq;
3238
   input rst;
3239
   input clk;
3240
 
3241
   parameter clear_value = 0;
3242
   parameter set_value = 1;
3243
   parameter wrap_value = 8;
3244
   parameter level1_value = 15;
3245
 
3246
   reg  [length:1] qi;
3247
   reg lfsr_fb;
3248
   wire [length:1] q_next;
3249
   reg [32:1] polynom;
3250
   integer i;
3251
 
3252
   always @ (qi)
3253
   begin
3254
        case (length)
3255
         2: polynom = 32'b11;                               // 0x3
3256
         3: polynom = 32'b110;                              // 0x6
3257
         4: polynom = 32'b1100;                             // 0xC
3258
         5: polynom = 32'b10100;                            // 0x14
3259
         6: polynom = 32'b110000;                           // 0x30
3260
         7: polynom = 32'b1100000;                          // 0x60
3261
         8: polynom = 32'b10111000;                         // 0xb8
3262
         9: polynom = 32'b100010000;                        // 0x110
3263
        10: polynom = 32'b1001000000;                       // 0x240
3264
        11: polynom = 32'b10100000000;                      // 0x500
3265
        12: polynom = 32'b100000101001;                     // 0x829
3266
        13: polynom = 32'b1000000001100;                    // 0x100C
3267
        14: polynom = 32'b10000000010101;                   // 0x2015
3268
        15: polynom = 32'b110000000000000;                  // 0x6000
3269
        16: polynom = 32'b1101000000001000;                 // 0xD008
3270
        17: polynom = 32'b10010000000000000;                // 0x12000
3271
        18: polynom = 32'b100000010000000000;               // 0x20400
3272
        19: polynom = 32'b1000000000000100011;              // 0x40023
3273 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3274 22 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3275
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3276
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3277
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3278
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3279
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3280
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3281
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3282
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3283
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3284
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3285
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3286
        default: polynom = 32'b0;
3287
        endcase
3288
        lfsr_fb = qi[length];
3289
        for (i=length-1; i>=1; i=i-1) begin
3290
            if (polynom[i])
3291
                lfsr_fb = lfsr_fb  ~^ qi[i];
3292
        end
3293
    end
3294
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3295
 
3296
   always @ (posedge clk or posedge rst)
3297
     if (rst)
3298
       qi <= {length{1'b0}};
3299
     else
3300
     if (cke)
3301
       qi <= q_next;
3302
 
3303
   assign q = qi;
3304
 
3305
 
3306
   always @ (posedge clk or posedge rst)
3307
     if (rst)
3308
       zq <= 1'b1;
3309
     else
3310
     if (cke)
3311
       zq <= q_next == {length{1'b0}};
3312
endmodule
3313 40 unneback
`endif
3314
`ifdef CNT_LFSR_CE_REW_L1
3315 22 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 6 unneback
 
3357
// LFSR counter
3358
 
3359 40 unneback
`define MODULE cnt_lfsr_ce_rew_l1
3360
module `BASE`MODULE (
3361
`undef MODULE
3362
 cke, rew, level1, rst, clk);
3363
 
3364 6 unneback
   parameter length = 4;
3365
   input cke;
3366
   input rew;
3367
   output reg level1;
3368
   input rst;
3369
   input clk;
3370
 
3371
   parameter clear_value = 0;
3372
   parameter set_value = 1;
3373
   parameter wrap_value = 8;
3374
   parameter level1_value = 15;
3375
 
3376 29 unneback
   wire clear;
3377 30 unneback
   assign clear = 1'b0;
3378 6 unneback
   reg  [length:1] qi;
3379
   reg lfsr_fb, lfsr_fb_rew;
3380
   wire  [length:1] q_next, q_next_fw, q_next_rew;
3381
   reg [32:1] polynom_rew;
3382
   integer j;
3383
   reg [32:1] polynom;
3384
   integer i;
3385
 
3386
   always @ (qi)
3387
   begin
3388
        case (length)
3389
         2: polynom = 32'b11;                               // 0x3
3390
         3: polynom = 32'b110;                              // 0x6
3391
         4: polynom = 32'b1100;                             // 0xC
3392
         5: polynom = 32'b10100;                            // 0x14
3393
         6: polynom = 32'b110000;                           // 0x30
3394
         7: polynom = 32'b1100000;                          // 0x60
3395
         8: polynom = 32'b10111000;                         // 0xb8
3396
         9: polynom = 32'b100010000;                        // 0x110
3397
        10: polynom = 32'b1001000000;                       // 0x240
3398
        11: polynom = 32'b10100000000;                      // 0x500
3399
        12: polynom = 32'b100000101001;                     // 0x829
3400
        13: polynom = 32'b1000000001100;                    // 0x100C
3401
        14: polynom = 32'b10000000010101;                   // 0x2015
3402
        15: polynom = 32'b110000000000000;                  // 0x6000
3403
        16: polynom = 32'b1101000000001000;                 // 0xD008
3404
        17: polynom = 32'b10010000000000000;                // 0x12000
3405
        18: polynom = 32'b100000010000000000;               // 0x20400
3406
        19: polynom = 32'b1000000000000100011;              // 0x40023
3407 37 unneback
        20: polynom = 32'b10010000000000000000;             // 0x90000
3408 6 unneback
        21: polynom = 32'b101000000000000000000;            // 0x140000
3409
        22: polynom = 32'b1100000000000000000000;           // 0x300000
3410
        23: polynom = 32'b10000100000000000000000;          // 0x420000
3411
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
3412
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
3413
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
3414
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
3415
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
3416
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
3417
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
3418
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
3419
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
3420
        default: polynom = 32'b0;
3421
        endcase
3422
        lfsr_fb = qi[length];
3423
        for (i=length-1; i>=1; i=i-1) begin
3424
            if (polynom[i])
3425
                lfsr_fb = lfsr_fb  ~^ qi[i];
3426
        end
3427
    end
3428
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
3429
   always @ (qi)
3430
   begin
3431
        case (length)
3432
         2: polynom_rew = 32'b11;
3433
         3: polynom_rew = 32'b110;
3434
         4: polynom_rew = 32'b1100;
3435
         5: polynom_rew = 32'b10100;
3436
         6: polynom_rew = 32'b110000;
3437
         7: polynom_rew = 32'b1100000;
3438
         8: polynom_rew = 32'b10111000;
3439
         9: polynom_rew = 32'b100010000;
3440
        10: polynom_rew = 32'b1001000000;
3441
        11: polynom_rew = 32'b10100000000;
3442
        12: polynom_rew = 32'b100000101001;
3443
        13: polynom_rew = 32'b1000000001100;
3444
        14: polynom_rew = 32'b10000000010101;
3445
        15: polynom_rew = 32'b110000000000000;
3446
        16: polynom_rew = 32'b1101000000001000;
3447
        17: polynom_rew = 32'b10010000000000000;
3448
        18: polynom_rew = 32'b100000010000000000;
3449
        19: polynom_rew = 32'b1000000000000100011;
3450
        20: polynom_rew = 32'b10000010000000000000;
3451
        21: polynom_rew = 32'b101000000000000000000;
3452
        22: polynom_rew = 32'b1100000000000000000000;
3453
        23: polynom_rew = 32'b10000100000000000000000;
3454
        24: polynom_rew = 32'b111000010000000000000000;
3455
        25: polynom_rew = 32'b1001000000000000000000000;
3456
        26: polynom_rew = 32'b10000000000000000000100011;
3457
        27: polynom_rew = 32'b100000000000000000000010011;
3458
        28: polynom_rew = 32'b1100100000000000000000000000;
3459
        29: polynom_rew = 32'b10100000000000000000000000000;
3460
        30: polynom_rew = 32'b100000000000000000000000101001;
3461
        31: polynom_rew = 32'b1001000000000000000000000000000;
3462
        32: polynom_rew = 32'b10000000001000000000000000000011;
3463
        default: polynom_rew = 32'b0;
3464
        endcase
3465
        // rotate left
3466
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
3467
        lfsr_fb_rew = qi[length];
3468
        for (i=length-1; i>=1; i=i-1) begin
3469
            if (polynom_rew[i])
3470
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
3471
        end
3472
    end
3473
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
3474
   assign q_next = rew ? q_next_rew : q_next_fw;
3475
 
3476
   always @ (posedge clk or posedge rst)
3477
     if (rst)
3478
       qi <= {length{1'b0}};
3479
     else
3480
     if (cke)
3481
       qi <= q_next;
3482
 
3483
 
3484
 
3485
    always @ (posedge clk or posedge rst)
3486
    if (rst)
3487
        level1 <= 1'b0;
3488
    else
3489
    if (cke)
3490 29 unneback
    if (clear)
3491
        level1 <= 1'b0;
3492
    else if (q_next == level1_value)
3493 6 unneback
        level1 <= 1'b1;
3494
    else if (qi == level1_value & rew)
3495
        level1 <= 1'b0;
3496
endmodule
3497 40 unneback
`endif
3498
`ifdef CNT_GRAY
3499 6 unneback
//////////////////////////////////////////////////////////////////////
3500
////                                                              ////
3501
////  Versatile counter                                           ////
3502
////                                                              ////
3503
////  Description                                                 ////
3504
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3505
////  counter                                                     ////
3506
////                                                              ////
3507
////  To Do:                                                      ////
3508
////   - add LFSR with more taps                                  ////
3509
////                                                              ////
3510
////  Author(s):                                                  ////
3511
////      - Michael Unneback, unneback@opencores.org              ////
3512
////        ORSoC AB                                              ////
3513
////                                                              ////
3514
//////////////////////////////////////////////////////////////////////
3515
////                                                              ////
3516
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3517
////                                                              ////
3518
//// This source file may be used and distributed without         ////
3519
//// restriction provided that this copyright statement is not    ////
3520
//// removed from the file and that any derivative work contains  ////
3521
//// the original copyright notice and the associated disclaimer. ////
3522
////                                                              ////
3523
//// This source file is free software; you can redistribute it   ////
3524
//// and/or modify it under the terms of the GNU Lesser General   ////
3525
//// Public License as published by the Free Software Foundation; ////
3526
//// either version 2.1 of the License, or (at your option) any   ////
3527
//// later version.                                               ////
3528
////                                                              ////
3529
//// This source is distributed in the hope that it will be       ////
3530
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3531
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3532
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3533
//// details.                                                     ////
3534
////                                                              ////
3535
//// You should have received a copy of the GNU Lesser General    ////
3536
//// Public License along with this source; if not, download it   ////
3537
//// from http://www.opencores.org/lgpl.shtml                     ////
3538
////                                                              ////
3539
//////////////////////////////////////////////////////////////////////
3540
 
3541
// GRAY counter
3542
 
3543 40 unneback
`define MODULE cnt_gray
3544
module `BASE`MODULE (
3545
`undef MODULE
3546
 q, rst, clk);
3547
 
3548 6 unneback
   parameter length = 4;
3549
   output reg [length:1] q;
3550
   input rst;
3551
   input clk;
3552
 
3553
   parameter clear_value = 0;
3554
   parameter set_value = 1;
3555
   parameter wrap_value = 8;
3556
   parameter level1_value = 15;
3557
 
3558
   reg  [length:1] qi;
3559
   wire [length:1] q_next;
3560
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3561
 
3562
   always @ (posedge clk or posedge rst)
3563
     if (rst)
3564
       qi <= {length{1'b0}};
3565
     else
3566
       qi <= q_next;
3567
 
3568
   always @ (posedge clk or posedge rst)
3569
     if (rst)
3570
       q <= {length{1'b0}};
3571
     else
3572
         q <= (q_next>>1) ^ q_next;
3573
 
3574
endmodule
3575 40 unneback
`endif
3576
`ifdef CNT_GRAY_CE
3577 6 unneback
//////////////////////////////////////////////////////////////////////
3578
////                                                              ////
3579
////  Versatile counter                                           ////
3580
////                                                              ////
3581
////  Description                                                 ////
3582
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3583
////  counter                                                     ////
3584
////                                                              ////
3585
////  To Do:                                                      ////
3586
////   - add LFSR with more taps                                  ////
3587
////                                                              ////
3588
////  Author(s):                                                  ////
3589
////      - Michael Unneback, unneback@opencores.org              ////
3590
////        ORSoC AB                                              ////
3591
////                                                              ////
3592
//////////////////////////////////////////////////////////////////////
3593
////                                                              ////
3594
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3595
////                                                              ////
3596
//// This source file may be used and distributed without         ////
3597
//// restriction provided that this copyright statement is not    ////
3598
//// removed from the file and that any derivative work contains  ////
3599
//// the original copyright notice and the associated disclaimer. ////
3600
////                                                              ////
3601
//// This source file is free software; you can redistribute it   ////
3602
//// and/or modify it under the terms of the GNU Lesser General   ////
3603
//// Public License as published by the Free Software Foundation; ////
3604
//// either version 2.1 of the License, or (at your option) any   ////
3605
//// later version.                                               ////
3606
////                                                              ////
3607
//// This source is distributed in the hope that it will be       ////
3608
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3609
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3610
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3611
//// details.                                                     ////
3612
////                                                              ////
3613
//// You should have received a copy of the GNU Lesser General    ////
3614
//// Public License along with this source; if not, download it   ////
3615
//// from http://www.opencores.org/lgpl.shtml                     ////
3616
////                                                              ////
3617
//////////////////////////////////////////////////////////////////////
3618
 
3619
// GRAY counter
3620
 
3621 40 unneback
`define MODULE cnt_gray_ce
3622
module `BASE`MODULE (
3623
`undef MODULE
3624
 cke, q, rst, clk);
3625
 
3626 6 unneback
   parameter length = 4;
3627
   input cke;
3628
   output reg [length:1] q;
3629
   input rst;
3630
   input clk;
3631
 
3632
   parameter clear_value = 0;
3633
   parameter set_value = 1;
3634
   parameter wrap_value = 8;
3635
   parameter level1_value = 15;
3636
 
3637
   reg  [length:1] qi;
3638
   wire [length:1] q_next;
3639
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3640
 
3641
   always @ (posedge clk or posedge rst)
3642
     if (rst)
3643
       qi <= {length{1'b0}};
3644
     else
3645
     if (cke)
3646
       qi <= q_next;
3647
 
3648
   always @ (posedge clk or posedge rst)
3649
     if (rst)
3650
       q <= {length{1'b0}};
3651
     else
3652
       if (cke)
3653
         q <= (q_next>>1) ^ q_next;
3654
 
3655
endmodule
3656 40 unneback
`endif
3657
`ifdef CNT_GRAY_CE_BIN
3658 6 unneback
//////////////////////////////////////////////////////////////////////
3659
////                                                              ////
3660
////  Versatile counter                                           ////
3661
////                                                              ////
3662
////  Description                                                 ////
3663
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
3664
////  counter                                                     ////
3665
////                                                              ////
3666
////  To Do:                                                      ////
3667
////   - add LFSR with more taps                                  ////
3668
////                                                              ////
3669
////  Author(s):                                                  ////
3670
////      - Michael Unneback, unneback@opencores.org              ////
3671
////        ORSoC AB                                              ////
3672
////                                                              ////
3673
//////////////////////////////////////////////////////////////////////
3674
////                                                              ////
3675
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
3676
////                                                              ////
3677
//// This source file may be used and distributed without         ////
3678
//// restriction provided that this copyright statement is not    ////
3679
//// removed from the file and that any derivative work contains  ////
3680
//// the original copyright notice and the associated disclaimer. ////
3681
////                                                              ////
3682
//// This source file is free software; you can redistribute it   ////
3683
//// and/or modify it under the terms of the GNU Lesser General   ////
3684
//// Public License as published by the Free Software Foundation; ////
3685
//// either version 2.1 of the License, or (at your option) any   ////
3686
//// later version.                                               ////
3687
////                                                              ////
3688
//// This source is distributed in the hope that it will be       ////
3689
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3690
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3691
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3692
//// details.                                                     ////
3693
////                                                              ////
3694
//// You should have received a copy of the GNU Lesser General    ////
3695
//// Public License along with this source; if not, download it   ////
3696
//// from http://www.opencores.org/lgpl.shtml                     ////
3697
////                                                              ////
3698
//////////////////////////////////////////////////////////////////////
3699
 
3700
// GRAY counter
3701
 
3702 40 unneback
`define MODULE cnt_gray_ce_bin
3703
module `BASE`MODULE (
3704
`undef MODULE
3705
 cke, q, q_bin, rst, clk);
3706
 
3707 6 unneback
   parameter length = 4;
3708
   input cke;
3709
   output reg [length:1] q;
3710
   output [length:1] q_bin;
3711
   input rst;
3712
   input clk;
3713
 
3714
   parameter clear_value = 0;
3715
   parameter set_value = 1;
3716
   parameter wrap_value = 8;
3717
   parameter level1_value = 15;
3718
 
3719
   reg  [length:1] qi;
3720
   wire [length:1] q_next;
3721
   assign q_next = qi + {{length-1{1'b0}},1'b1};
3722
 
3723
   always @ (posedge clk or posedge rst)
3724
     if (rst)
3725
       qi <= {length{1'b0}};
3726
     else
3727
     if (cke)
3728
       qi <= q_next;
3729
 
3730
   always @ (posedge clk or posedge rst)
3731
     if (rst)
3732
       q <= {length{1'b0}};
3733
     else
3734
       if (cke)
3735
         q <= (q_next>>1) ^ q_next;
3736
 
3737
   assign q_bin = qi;
3738
 
3739
endmodule
3740 40 unneback
`endif
3741 6 unneback
//////////////////////////////////////////////////////////////////////
3742
////                                                              ////
3743
////  Versatile library, counters                                 ////
3744
////                                                              ////
3745
////  Description                                                 ////
3746
////  counters                                                    ////
3747
////                                                              ////
3748
////                                                              ////
3749
////  To Do:                                                      ////
3750
////   - add more counters                                        ////
3751
////                                                              ////
3752
////  Author(s):                                                  ////
3753
////      - Michael Unneback, unneback@opencores.org              ////
3754
////        ORSoC AB                                              ////
3755
////                                                              ////
3756
//////////////////////////////////////////////////////////////////////
3757
////                                                              ////
3758
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3759
////                                                              ////
3760
//// This source file may be used and distributed without         ////
3761
//// restriction provided that this copyright statement is not    ////
3762
//// removed from the file and that any derivative work contains  ////
3763
//// the original copyright notice and the associated disclaimer. ////
3764
////                                                              ////
3765
//// This source file is free software; you can redistribute it   ////
3766
//// and/or modify it under the terms of the GNU Lesser General   ////
3767
//// Public License as published by the Free Software Foundation; ////
3768
//// either version 2.1 of the License, or (at your option) any   ////
3769
//// later version.                                               ////
3770
////                                                              ////
3771
//// This source is distributed in the hope that it will be       ////
3772
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3773
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3774
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3775
//// details.                                                     ////
3776
////                                                              ////
3777
//// You should have received a copy of the GNU Lesser General    ////
3778
//// Public License along with this source; if not, download it   ////
3779
//// from http://www.opencores.org/lgpl.shtml                     ////
3780
////                                                              ////
3781
//////////////////////////////////////////////////////////////////////
3782
 
3783 40 unneback
`ifdef CNT_SHREG_WRAP
3784
`define MODULE cnt_shreg_wrap
3785
module `BASE`MODULE ( q, rst, clk);
3786
`undef MODULE
3787 6 unneback
 
3788
   parameter length = 4;
3789
   output reg [0:length-1] q;
3790
   input rst;
3791
   input clk;
3792
 
3793
    always @ (posedge clk or posedge rst)
3794
    if (rst)
3795
        q <= {1'b1,{length-1{1'b0}}};
3796
    else
3797
        q <= {q[length-1],q[0:length-2]};
3798
 
3799
endmodule
3800 40 unneback
`endif
3801 6 unneback
 
3802 40 unneback
`ifdef CNT_SHREG_CE_WRAP
3803
`define MODULE cnt_shreg_ce_wrap
3804
module `BASE`MODULE ( cke, q, rst, clk);
3805
`undef MODULE
3806 6 unneback
 
3807
   parameter length = 4;
3808
   input cke;
3809
   output reg [0:length-1] q;
3810
   input rst;
3811
   input clk;
3812
 
3813
    always @ (posedge clk or posedge rst)
3814
    if (rst)
3815
        q <= {1'b1,{length-1{1'b0}}};
3816
    else
3817
        if (cke)
3818
            q <= {q[length-1],q[0:length-2]};
3819
 
3820
endmodule
3821 40 unneback
`endif
3822 6 unneback
 
3823 105 unneback
`ifdef CNT_SHREG_CLEAR
3824
`define MODULE cnt_shreg_clear
3825
module `BASE`MODULE ( clear, q, rst, clk);
3826
`undef MODULE
3827
 
3828
   parameter length = 4;
3829
   input clear;
3830
   output reg [0:length-1] q;
3831
   input rst;
3832
   input clk;
3833
 
3834
    always @ (posedge clk or posedge rst)
3835
    if (rst)
3836
        q <= {1'b1,{length-1{1'b0}}};
3837
    else
3838
        if (clear)
3839
            q <= {1'b1,{length-1{1'b0}}};
3840
        else
3841
            q <= q >> 1;
3842
 
3843
endmodule
3844
`endif
3845
 
3846 40 unneback
`ifdef CNT_SHREG_CE_CLEAR
3847
`define MODULE cnt_shreg_ce_clear
3848
module `BASE`MODULE ( cke, clear, q, rst, clk);
3849
`undef MODULE
3850 6 unneback
 
3851
   parameter length = 4;
3852
   input cke, clear;
3853
   output reg [0:length-1] q;
3854
   input rst;
3855
   input clk;
3856
 
3857
    always @ (posedge clk or posedge rst)
3858
    if (rst)
3859
        q <= {1'b1,{length-1{1'b0}}};
3860
    else
3861
        if (cke)
3862
            if (clear)
3863
                q <= {1'b1,{length-1{1'b0}}};
3864
            else
3865
                q <= q >> 1;
3866
 
3867
endmodule
3868 40 unneback
`endif
3869 6 unneback
 
3870 40 unneback
`ifdef CNT_SHREG_CE_CLEAR_WRAP
3871
`define MODULE cnt_shreg_ce_clear_wrap
3872
module `BASE`MODULE ( cke, clear, q, rst, clk);
3873
`undef MODULE
3874 6 unneback
 
3875
   parameter length = 4;
3876
   input cke, clear;
3877
   output reg [0:length-1] q;
3878
   input rst;
3879
   input clk;
3880
 
3881
    always @ (posedge clk or posedge rst)
3882
    if (rst)
3883
        q <= {1'b1,{length-1{1'b0}}};
3884
    else
3885
        if (cke)
3886
            if (clear)
3887
                q <= {1'b1,{length-1{1'b0}}};
3888
            else
3889
            q <= {q[length-1],q[0:length-2]};
3890
 
3891
endmodule
3892 40 unneback
`endif
3893 6 unneback
//////////////////////////////////////////////////////////////////////
3894
////                                                              ////
3895
////  Versatile library, memories                                 ////
3896
////                                                              ////
3897
////  Description                                                 ////
3898
////  memories                                                    ////
3899
////                                                              ////
3900
////                                                              ////
3901
////  To Do:                                                      ////
3902
////   - add more memory types                                    ////
3903
////                                                              ////
3904
////  Author(s):                                                  ////
3905
////      - Michael Unneback, unneback@opencores.org              ////
3906
////        ORSoC AB                                              ////
3907
////                                                              ////
3908
//////////////////////////////////////////////////////////////////////
3909
////                                                              ////
3910
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3911
////                                                              ////
3912
//// This source file may be used and distributed without         ////
3913
//// restriction provided that this copyright statement is not    ////
3914
//// removed from the file and that any derivative work contains  ////
3915
//// the original copyright notice and the associated disclaimer. ////
3916
////                                                              ////
3917
//// This source file is free software; you can redistribute it   ////
3918
//// and/or modify it under the terms of the GNU Lesser General   ////
3919
//// Public License as published by the Free Software Foundation; ////
3920
//// either version 2.1 of the License, or (at your option) any   ////
3921
//// later version.                                               ////
3922
////                                                              ////
3923
//// This source is distributed in the hope that it will be       ////
3924
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3925
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3926
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3927
//// details.                                                     ////
3928
////                                                              ////
3929
//// You should have received a copy of the GNU Lesser General    ////
3930
//// Public License along with this source; if not, download it   ////
3931
//// from http://www.opencores.org/lgpl.shtml                     ////
3932
////                                                              ////
3933
//////////////////////////////////////////////////////////////////////
3934
 
3935 40 unneback
`ifdef ROM_INIT
3936 6 unneback
/// ROM
3937 40 unneback
`define MODULE rom_init
3938
module `BASE`MODULE ( adr, q, clk);
3939
`undef MODULE
3940 6 unneback
 
3941 7 unneback
   parameter data_width = 32;
3942
   parameter addr_width = 8;
3943 75 unneback
   parameter mem_size = 1<<addr_width;
3944 7 unneback
   input [(addr_width-1):0]       adr;
3945
   output reg [(data_width-1):0] q;
3946
   input                         clk;
3947 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
3948 7 unneback
   parameter memory_file = "vl_rom.vmem";
3949
   initial
3950
     begin
3951
        $readmemh(memory_file, rom);
3952
     end
3953
 
3954
   always @ (posedge clk)
3955
     q <= rom[adr];
3956 6 unneback
 
3957 7 unneback
endmodule
3958 40 unneback
`endif
3959 7 unneback
 
3960 40 unneback
`ifdef RAM
3961
`define MODULE ram
3962 6 unneback
// Single port RAM
3963 40 unneback
module `BASE`MODULE ( d, adr, we, q, clk);
3964
`undef MODULE
3965 6 unneback
 
3966
   parameter data_width = 32;
3967
   parameter addr_width = 8;
3968 75 unneback
   parameter mem_size = 1<<addr_width;
3969 100 unneback
   parameter debug = 0;
3970 6 unneback
   input [(data_width-1):0]      d;
3971
   input [(addr_width-1):0]       adr;
3972
   input                         we;
3973 7 unneback
   output reg [(data_width-1):0] q;
3974 6 unneback
   input                         clk;
3975 98 unneback
   reg [data_width-1:0] ram [mem_size-1:0];
3976 100 unneback
 
3977
    parameter memory_init = 0;
3978
    parameter memory_file = "vl_ram.vmem";
3979
    generate
3980
    if (memory_init == 1) begin : init_mem
3981
        initial
3982
            $readmemh(memory_file, ram);
3983
   end else if (memory_init == 2) begin : init_zero
3984
        integer k;
3985
        initial
3986
            for (k = 0; k < mem_size; k = k + 1)
3987
                ram[k] = 0;
3988 7 unneback
   end
3989
   endgenerate
3990
 
3991 100 unneback
    generate
3992
    if (debug==1) begin : debug_we
3993
        always @ (posedge clk)
3994
        if (we)
3995
            $display ("Value %h written at address %h : time %t", d, adr, $time);
3996
 
3997
    end
3998
    endgenerate
3999
 
4000 6 unneback
   always @ (posedge clk)
4001
   begin
4002
   if (we)
4003
     ram[adr] <= d;
4004
   q <= ram[adr];
4005
   end
4006
 
4007
endmodule
4008 40 unneback
`endif
4009 6 unneback
 
4010 40 unneback
`ifdef RAM_BE
4011
`define MODULE ram_be
4012 91 unneback
module `BASE`MODULE ( d, adr, be, we, q, clk);
4013 40 unneback
`undef MODULE
4014
 
4015 7 unneback
   parameter data_width = 32;
4016 72 unneback
   parameter addr_width = 6;
4017 75 unneback
   parameter mem_size = 1<<addr_width;
4018 7 unneback
   input [(data_width-1):0]      d;
4019
   input [(addr_width-1):0]       adr;
4020 73 unneback
   input [(data_width/8)-1:0]    be;
4021 7 unneback
   input                         we;
4022
   output reg [(data_width-1):0] q;
4023
   input                         clk;
4024
 
4025 85 unneback
 
4026 65 unneback
`ifdef SYSTEMVERILOG
4027 95 unneback
    // use a multi-dimensional packed array
4028
    //t o model individual bytes within the word
4029
    logic [data_width/8-1:0][7:0] ram [0:mem_size-1];// # words = 1 << address width
4030 65 unneback
`else
4031 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
4032
    wire [data_width/8-1:0] cke;
4033 65 unneback
`endif
4034
 
4035 100 unneback
    parameter memory_init = 0;
4036
    parameter memory_file = "vl_ram.vmem";
4037
    generate
4038
    if (memory_init == 1) begin : init_mem
4039
        initial
4040
            $readmemh(memory_file, ram);
4041
    end else if (memory_init == 2) begin : init_zero
4042
        integer k;
4043
        initial
4044
            for (k = 0; k < mem_size; k = k + 1)
4045
                ram[k] = 0;
4046
    end
4047 7 unneback
   endgenerate
4048
 
4049 60 unneback
`ifdef SYSTEMVERILOG
4050
 
4051
always_ff@(posedge clk)
4052
begin
4053 95 unneback
    if(we) begin
4054 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
4055
        if(be[2]) ram[adr][2] <= d[23:16];
4056
        if(be[1]) ram[adr][1] <= d[15:8];
4057
        if(be[0]) ram[adr][0] <= d[7:0];
4058 60 unneback
    end
4059 90 unneback
        q <= ram[adr];
4060 60 unneback
end
4061
 
4062
`else
4063
 
4064 85 unneback
assign cke = {data_width/8{we}} & be;
4065 7 unneback
   genvar i;
4066 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
4067 7 unneback
      always @ (posedge clk)
4068 85 unneback
      if (cke[i])
4069 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
4070
   end
4071
   endgenerate
4072
 
4073
   always @ (posedge clk)
4074
      q <= ram[adr];
4075
 
4076 60 unneback
`endif
4077
 
4078 93 unneback
`ifdef verilator
4079 85 unneback
   // Function to access RAM (for use by Verilator).
4080
   function [31:0] get_mem;
4081
      // verilator public
4082 90 unneback
      input [addr_width-1:0]             addr;
4083 85 unneback
      get_mem = ram[addr];
4084
   endfunction // get_mem
4085
 
4086
   // Function to write RAM (for use by Verilator).
4087
   function set_mem;
4088
      // verilator public
4089 90 unneback
      input [addr_width-1:0]             addr;
4090
      input [data_width-1:0]             data;
4091 85 unneback
      ram[addr] = data;
4092
   endfunction // set_mem
4093 93 unneback
`endif
4094 85 unneback
 
4095 7 unneback
endmodule
4096 40 unneback
`endif
4097 7 unneback
 
4098 40 unneback
`ifdef DPRAM_1R1W
4099
`define MODULE dpram_1r1w
4100
module `BASE`MODULE ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
4101
`undef MODULE
4102 6 unneback
   parameter data_width = 32;
4103
   parameter addr_width = 8;
4104 75 unneback
   parameter mem_size = 1<<addr_width;
4105 6 unneback
   input [(data_width-1):0]      d_a;
4106
   input [(addr_width-1):0]       adr_a;
4107
   input [(addr_width-1):0]       adr_b;
4108
   input                         we_a;
4109 118 unneback
   output reg [(data_width-1):0]          q_b;
4110 6 unneback
   input                         clk_a, clk_b;
4111 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4112 7 unneback
 
4113 100 unneback
    parameter memory_init = 0;
4114
    parameter memory_file = "vl_ram.vmem";
4115
    parameter debug = 0;
4116
 
4117
    generate
4118
    if (memory_init == 1) begin : init_mem
4119
        initial
4120
            $readmemh(memory_file, ram);
4121
    end else if (memory_init == 2) begin : init_zero
4122
        integer k;
4123
        initial
4124
            for (k = 0; k < mem_size; k = k + 1)
4125
                ram[k] = 0;
4126
    end
4127 7 unneback
   endgenerate
4128
 
4129 100 unneback
    generate
4130
    if (debug==1) begin : debug_we
4131
        always @ (posedge clk_a)
4132
        if (we_a)
4133
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
4134
 
4135
    end
4136
    endgenerate
4137
 
4138 6 unneback
   always @ (posedge clk_a)
4139
   if (we_a)
4140
     ram[adr_a] <= d_a;
4141 118 unneback
 
4142 6 unneback
   always @ (posedge clk_b)
4143 118 unneback
      q_b = ram[adr_b];
4144 40 unneback
 
4145 6 unneback
endmodule
4146 40 unneback
`endif
4147 6 unneback
 
4148 40 unneback
`ifdef DPRAM_2R1W
4149
`define MODULE dpram_2r1w
4150
module `BASE`MODULE ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
4151
`undef MODULE
4152
 
4153 6 unneback
   parameter data_width = 32;
4154
   parameter addr_width = 8;
4155 75 unneback
   parameter mem_size = 1<<addr_width;
4156 6 unneback
   input [(data_width-1):0]      d_a;
4157
   input [(addr_width-1):0]       adr_a;
4158
   input [(addr_width-1):0]       adr_b;
4159
   input                         we_a;
4160
   output [(data_width-1):0]      q_b;
4161
   output reg [(data_width-1):0] q_a;
4162
   input                         clk_a, clk_b;
4163
   reg [(data_width-1):0]         q_b;
4164 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4165 7 unneback
 
4166 100 unneback
    parameter memory_init = 0;
4167
    parameter memory_file = "vl_ram.vmem";
4168
    parameter debug = 0;
4169
 
4170
    generate
4171
    if (memory_init == 1) begin : init_mem
4172
        initial
4173
            $readmemh(memory_file, ram);
4174
    end else if (memory_init == 2) begin : init_zero
4175
        integer k;
4176
        initial
4177
            for (k = 0; k < mem_size; k = k + 1)
4178
                ram[k] = 0;
4179
    end
4180 7 unneback
   endgenerate
4181
 
4182 100 unneback
    generate
4183
    if (debug==1) begin : debug_we
4184
        always @ (posedge clk_a)
4185
        if (we_a)
4186
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
4187
 
4188
    end
4189
    endgenerate
4190
 
4191 6 unneback
   always @ (posedge clk_a)
4192
     begin
4193
        q_a <= ram[adr_a];
4194
        if (we_a)
4195
             ram[adr_a] <= d_a;
4196
     end
4197
   always @ (posedge clk_b)
4198
          q_b <= ram[adr_b];
4199
endmodule
4200 40 unneback
`endif
4201 6 unneback
 
4202 100 unneback
`ifdef DPRAM_1R2W
4203
`define MODULE dpram_1r2w
4204
module `BASE`MODULE ( d_a, q_a, adr_a, we_a, clk_a, d_b, adr_b, we_b, clk_b );
4205
`undef MODULE
4206
 
4207
   parameter data_width = 32;
4208
   parameter addr_width = 8;
4209
   parameter mem_size = 1<<addr_width;
4210
   input [(data_width-1):0]      d_a;
4211
   input [(addr_width-1):0]       adr_a;
4212
   input [(addr_width-1):0]       adr_b;
4213
   input                         we_a;
4214
   input [(data_width-1):0]       d_b;
4215
   output reg [(data_width-1):0] q_a;
4216
   input                         we_b;
4217
   input                         clk_a, clk_b;
4218
   reg [(data_width-1):0]         q_b;
4219 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4220 100 unneback
 
4221
    parameter memory_init = 0;
4222
    parameter memory_file = "vl_ram.vmem";
4223
    parameter debug = 0;
4224
 
4225
    generate
4226
    if (memory_init == 1) begin : init_mem
4227
        initial
4228
            $readmemh(memory_file, ram);
4229
    end else if (memory_init == 2) begin : init_zero
4230
        integer k;
4231
        initial
4232
            for (k = 0; k < mem_size; k = k + 1)
4233
                ram[k] = 0;
4234
    end
4235
   endgenerate
4236
 
4237
    generate
4238
    if (debug==1) begin : debug_we
4239
        always @ (posedge clk_a)
4240
        if (we_a)
4241
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
4242
        always @ (posedge clk_b)
4243
        if (we_b)
4244
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
4245
    end
4246
    endgenerate
4247
 
4248
   always @ (posedge clk_a)
4249
     begin
4250
        q_a <= ram[adr_a];
4251
        if (we_a)
4252
             ram[adr_a] <= d_a;
4253
     end
4254
   always @ (posedge clk_b)
4255
     begin
4256
        if (we_b)
4257
          ram[adr_b] <= d_b;
4258
     end
4259
endmodule
4260
`endif
4261
 
4262 40 unneback
`ifdef DPRAM_2R2W
4263
`define MODULE dpram_2r2w
4264
module `BASE`MODULE ( d_a, q_a, adr_a, we_a, clk_a, d_b, q_b, adr_b, we_b, clk_b );
4265
`undef MODULE
4266
 
4267 6 unneback
   parameter data_width = 32;
4268
   parameter addr_width = 8;
4269 75 unneback
   parameter mem_size = 1<<addr_width;
4270 6 unneback
   input [(data_width-1):0]      d_a;
4271
   input [(addr_width-1):0]       adr_a;
4272
   input [(addr_width-1):0]       adr_b;
4273
   input                         we_a;
4274
   output [(data_width-1):0]      q_b;
4275
   input [(data_width-1):0]       d_b;
4276
   output reg [(data_width-1):0] q_a;
4277
   input                         we_b;
4278
   input                         clk_a, clk_b;
4279
   reg [(data_width-1):0]         q_b;
4280 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4281 7 unneback
 
4282 100 unneback
    parameter memory_init = 0;
4283
    parameter memory_file = "vl_ram.vmem";
4284
    parameter debug = 0;
4285
 
4286
    generate
4287
    if (memory_init) begin : init_mem
4288
        initial
4289
            $readmemh(memory_file, ram);
4290
    end else if (memory_init == 2) begin : init_zero
4291
        integer k;
4292
        initial
4293
            for (k = 0; k < mem_size; k = k + 1)
4294
                ram[k] = 0;
4295
    end
4296 7 unneback
   endgenerate
4297
 
4298 100 unneback
    generate
4299
    if (debug==1) begin : debug_we
4300
        always @ (posedge clk_a)
4301
        if (we_a)
4302
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
4303
        always @ (posedge clk_b)
4304
        if (we_b)
4305
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
4306
    end
4307
    endgenerate
4308
 
4309 6 unneback
   always @ (posedge clk_a)
4310
     begin
4311
        q_a <= ram[adr_a];
4312
        if (we_a)
4313
             ram[adr_a] <= d_a;
4314
     end
4315
   always @ (posedge clk_b)
4316
     begin
4317
        q_b <= ram[adr_b];
4318
        if (we_b)
4319
          ram[adr_b] <= d_b;
4320
     end
4321
endmodule
4322 40 unneback
`endif
4323 6 unneback
 
4324 83 unneback
 
4325 75 unneback
`ifdef DPRAM_BE_2R2W
4326
`define MODULE dpram_be_2r2w
4327 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 );
4328 75 unneback
`undef MODULE
4329
 
4330
   parameter a_data_width = 32;
4331
   parameter a_addr_width = 8;
4332 95 unneback
   parameter b_data_width = 64; //a_data_width;
4333 124 unneback
   //localparam b_addr_width = a_data_width * a_addr_width / b_data_width;
4334
   localparam b_addr_width =
4335 125 unneback
        (a_data_width==b_data_width) ? a_addr_width :
4336
        (a_data_width==b_data_width*2) ? a_addr_width+1 :
4337
        (a_data_width==b_data_width*4) ? a_addr_width+2 :
4338
        (a_data_width==b_data_width*8) ? a_addr_width+3 :
4339
        (a_data_width==b_data_width*16) ? a_addr_width+4 :
4340
        (a_data_width==b_data_width*32) ? a_addr_width+5 :
4341
        (a_data_width==b_data_width/2) ? a_addr_width-1 :
4342
        (a_data_width==b_data_width/4) ? a_addr_width-2 :
4343
        (a_data_width==b_data_width/8) ? a_addr_width-3 :
4344
        (a_data_width==b_data_width/16) ? a_addr_width-4 :
4345
        (a_data_width==b_data_width/32) ? a_addr_width-5 : 0;
4346 124 unneback
 
4347 95 unneback
   localparam ratio = (a_addr_width>b_addr_width) ? (a_addr_width/b_addr_width) : (b_addr_width/a_addr_width);
4348
   parameter mem_size = (a_addr_width>b_addr_width) ? (1<<b_addr_width) : (1<<a_addr_width);
4349 91 unneback
 
4350 100 unneback
   parameter memory_init = 0;
4351 95 unneback
   parameter memory_file = "vl_ram.vmem";
4352 100 unneback
   parameter debug = 0;
4353 95 unneback
 
4354 75 unneback
   input [(a_data_width-1):0]      d_a;
4355 91 unneback
   input [(a_addr_width-1):0]       adr_a;
4356
   input [(a_data_width/8-1):0]    be_a;
4357
   input                           we_a;
4358 75 unneback
   output reg [(a_data_width-1):0] q_a;
4359 91 unneback
   input [(b_data_width-1):0]       d_b;
4360
   input [(b_addr_width-1):0]       adr_b;
4361 92 unneback
   input [(b_data_width/8-1):0]    be_b;
4362
   input                           we_b;
4363
   output reg [(b_data_width-1):0]          q_b;
4364 91 unneback
   input                           clk_a, clk_b;
4365 75 unneback
 
4366 100 unneback
    generate
4367
    if (debug==1) begin : debug_we
4368
        always @ (posedge clk_a)
4369
        if (we_a)
4370
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
4371
        always @ (posedge clk_b)
4372
        if (we_b)
4373
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
4374
    end
4375
    endgenerate
4376
 
4377
 
4378 91 unneback
`ifdef SYSTEMVERILOG
4379
// use a multi-dimensional packed array
4380
//to model individual bytes within the word
4381
 
4382 75 unneback
generate
4383 91 unneback
if (a_data_width==32 & b_data_width==32) begin : dpram_3232
4384 75 unneback
 
4385 98 unneback
    logic [0:3][7:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4386 95 unneback
 
4387
    initial
4388 100 unneback
        if (memory_init==1)
4389 95 unneback
            $readmemh(memory_file, ram);
4390 100 unneback
 
4391
    integer k;
4392
    initial
4393
        if (memory_init==2)
4394
            for (k = 0; k < mem_size; k = k + 1)
4395
                ram[k] = 0;
4396 91 unneback
 
4397
    always_ff@(posedge clk_a)
4398
    begin
4399
        if(we_a) begin
4400 100 unneback
            if(be_a[3]) ram[adr_a][0] <= d_a[31:24];
4401
            if(be_a[2]) ram[adr_a][1] <= d_a[23:16];
4402
            if(be_a[1]) ram[adr_a][2] <= d_a[15:8];
4403
            if(be_a[0]) ram[adr_a][3] <= d_a[7:0];
4404 91 unneback
        end
4405
    end
4406
 
4407 92 unneback
    always@(posedge clk_a)
4408
        q_a = ram[adr_a];
4409 91 unneback
 
4410
    always_ff@(posedge clk_b)
4411 92 unneback
    begin
4412
        if(we_b) begin
4413 100 unneback
            if(be_b[3]) ram[adr_b][0] <= d_b[31:24];
4414
            if(be_b[2]) ram[adr_b][1] <= d_b[23:16];
4415
            if(be_b[1]) ram[adr_b][2] <= d_b[15:8];
4416
            if(be_b[0]) ram[adr_b][3] <= d_b[7:0];
4417 92 unneback
        end
4418
    end
4419 91 unneback
 
4420 92 unneback
    always@(posedge clk_b)
4421
        q_b = ram[adr_b];
4422 91 unneback
 
4423 75 unneback
end
4424
endgenerate
4425
 
4426 95 unneback
generate
4427
if (a_data_width==64 & b_data_width==64) begin : dpram_6464
4428
 
4429 98 unneback
    logic [0:7][7:0] ram [0:mem_size-1] `SYN_NO_RW_CHECK;
4430 95 unneback
 
4431
    initial
4432 100 unneback
        if (memory_init==1)
4433 95 unneback
            $readmemh(memory_file, ram);
4434 100 unneback
 
4435
    integer k;
4436
    initial
4437
        if (memory_init==2)
4438
            for (k = 0; k < mem_size; k = k + 1)
4439
                ram[k] = 0;
4440 95 unneback
 
4441
    always_ff@(posedge clk_a)
4442
    begin
4443
        if(we_a) begin
4444
            if(be_a[7]) ram[adr_a][7] <= d_a[63:56];
4445
            if(be_a[6]) ram[adr_a][6] <= d_a[55:48];
4446
            if(be_a[5]) ram[adr_a][5] <= d_a[47:40];
4447
            if(be_a[4]) ram[adr_a][4] <= d_a[39:32];
4448
            if(be_a[3]) ram[adr_a][3] <= d_a[31:24];
4449
            if(be_a[2]) ram[adr_a][2] <= d_a[23:16];
4450
            if(be_a[1]) ram[adr_a][1] <= d_a[15:8];
4451
            if(be_a[0]) ram[adr_a][0] <= d_a[7:0];
4452
        end
4453
    end
4454
 
4455
    always@(posedge clk_a)
4456
        q_a = ram[adr_a];
4457
 
4458
    always_ff@(posedge clk_b)
4459
    begin
4460
        if(we_b) begin
4461
            if(be_b[7]) ram[adr_b][7] <= d_b[63:56];
4462
            if(be_b[6]) ram[adr_b][6] <= d_b[55:48];
4463
            if(be_b[5]) ram[adr_b][5] <= d_b[47:40];
4464
            if(be_b[4]) ram[adr_b][4] <= d_b[39:32];
4465
            if(be_b[3]) ram[adr_b][3] <= d_b[31:24];
4466
            if(be_b[2]) ram[adr_b][2] <= d_b[23:16];
4467
            if(be_b[1]) ram[adr_b][1] <= d_b[15:8];
4468
            if(be_b[0]) ram[adr_b][0] <= d_b[7:0];
4469
        end
4470
    end
4471
 
4472
    always@(posedge clk_b)
4473
        q_b = ram[adr_b];
4474
 
4475
end
4476
endgenerate
4477
 
4478
generate
4479
if (a_data_width==32 & b_data_width==16) begin : dpram_3216
4480
logic [31:0] temp;
4481
`define MODULE dpram_be_2r2w
4482 128 unneback
`BASE`MODULE # (.a_data_width(32), .b_data_width(32), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
4483 95 unneback
`undef MODULE
4484 128 unneback
dpram3232 (
4485 95 unneback
    .d_a(d_a),
4486
    .q_a(q_a),
4487
    .adr_a(adr_a),
4488
    .be_a(be_a),
4489
    .we_a(we_a),
4490
    .clk_a(clk_a),
4491
    .d_b({d_b,d_b}),
4492
    .q_b(temp),
4493 128 unneback
    .adr_b(adr_b[b_addr_width-1:1]),
4494 137 unneback
    .be_b({be_b,be_b} & {{2{!adr_b[0]}},{2{adr_b[0]}}}),
4495 95 unneback
    .we_b(we_b),
4496
    .clk_b(clk_b)
4497
);
4498
 
4499 100 unneback
always @ (adr_b[0] or temp)
4500 95 unneback
    if (adr_b[0])
4501
        q_b = temp[31:16];
4502
    else
4503
        q_b = temp[15:0];
4504
 
4505
end
4506
endgenerate
4507
 
4508
generate
4509
if (a_data_width==32 & b_data_width==64) begin : dpram_3264
4510
logic [63:0] temp;
4511
`define MODULE dpram_be_2r2w
4512 128 unneback
`BASE`MODULE # (.a_data_width(32), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
4513 95 unneback
`undef MODULE
4514
dpram6464 (
4515
    .d_a({d_a,d_a}),
4516
    .q_a(temp),
4517
    .adr_a(adr_a[a_addr_width-1:1]),
4518
    .be_a({be_a,be_a} & {{4{adr_a[0]}},{4{!adr_a[0]}}}),
4519
    .we_a(we_a),
4520
    .clk_a(clk_a),
4521
    .d_b(d_b),
4522
    .q_b(q_b),
4523
    .adr_b(adr_b),
4524
    .be_b(be_b),
4525
    .we_b(we_b),
4526
    .clk_b(clk_b)
4527
);
4528
 
4529 100 unneback
always @ (adr_a[0] or temp)
4530 95 unneback
    if (adr_a[0])
4531
        q_a = temp[63:32];
4532
    else
4533
        q_a = temp[31:0];
4534
 
4535
end
4536
endgenerate
4537
 
4538 91 unneback
`else
4539 92 unneback
    // This modules requires SystemVerilog
4540 98 unneback
    // at this point anyway
4541 91 unneback
`endif
4542 75 unneback
endmodule
4543
`endif
4544
 
4545 91 unneback
`ifdef CAM
4546 6 unneback
// Content addresable memory, CAM
4547 91 unneback
`endif
4548 6 unneback
 
4549 40 unneback
`ifdef FIFO_1R1W_FILL_LEVEL_SYNC
4550 6 unneback
// FIFO
4551 40 unneback
`define MODULE fifo_1r1w_fill_level_sync
4552
module `BASE`MODULE (
4553
`undef MODULE
4554 25 unneback
    d, wr, fifo_full,
4555
    q, rd, fifo_empty,
4556
    fill_level,
4557
    clk, rst
4558
    );
4559
 
4560
parameter data_width = 18;
4561
parameter addr_width = 4;
4562 6 unneback
 
4563 25 unneback
// write side
4564
input  [data_width-1:0] d;
4565
input                   wr;
4566
output                  fifo_full;
4567
// read side
4568
output [data_width-1:0] q;
4569
input                   rd;
4570
output                  fifo_empty;
4571
// common
4572
output [addr_width:0]   fill_level;
4573
input rst, clk;
4574
 
4575
wire [addr_width:1] wadr, radr;
4576
 
4577 40 unneback
`define MODULE cnt_bin_ce
4578
`BASE`MODULE
4579 25 unneback
    # ( .length(addr_width))
4580
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
4581 40 unneback
`BASE`MODULE
4582 25 unneback
    # (.length(addr_width))
4583
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
4584 40 unneback
`undef MODULE
4585 25 unneback
 
4586 40 unneback
`define MODULE dpram_1r1w
4587
`BASE`MODULE
4588 25 unneback
    # (.data_width(data_width), .addr_width(addr_width))
4589
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
4590 40 unneback
`undef MODULE
4591 25 unneback
 
4592 40 unneback
`define MODULE cnt_bin_ce_rew_q_zq_l1
4593
`BASE`MODULE
4594 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
4595 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
4596 40 unneback
`undef MODULE
4597 25 unneback
endmodule
4598 40 unneback
`endif
4599 25 unneback
 
4600 40 unneback
`ifdef FIFO_2R2W_SYNC_SIMPLEX
4601 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
4602
// RAM is supposed to be larger than the two FIFOs
4603
// LFSR counters used adr pointers
4604 40 unneback
`define MODULE fifo_2r2w_sync_simplex
4605
module `BASE`MODULE (
4606
`undef MODULE
4607 27 unneback
    // a side
4608
    a_d, a_wr, a_fifo_full,
4609
    a_q, a_rd, a_fifo_empty,
4610
    a_fill_level,
4611
    // b side
4612
    b_d, b_wr, b_fifo_full,
4613
    b_q, b_rd, b_fifo_empty,
4614
    b_fill_level,
4615
    // common
4616
    clk, rst
4617
    );
4618
parameter data_width = 8;
4619
parameter addr_width = 5;
4620
parameter fifo_full_level = (1<<addr_width)-1;
4621
 
4622
// a side
4623
input  [data_width-1:0] a_d;
4624
input                   a_wr;
4625
output                  a_fifo_full;
4626
output [data_width-1:0] a_q;
4627
input                   a_rd;
4628
output                  a_fifo_empty;
4629
output [addr_width-1:0] a_fill_level;
4630
 
4631
// b side
4632
input  [data_width-1:0] b_d;
4633
input                   b_wr;
4634
output                  b_fifo_full;
4635
output [data_width-1:0] b_q;
4636
input                   b_rd;
4637
output                  b_fifo_empty;
4638
output [addr_width-1:0] b_fill_level;
4639
 
4640
input                   clk;
4641
input                   rst;
4642
 
4643
// adr_gen
4644
wire [addr_width:1] a_wadr, a_radr;
4645
wire [addr_width:1] b_wadr, b_radr;
4646
// dpram
4647
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
4648
 
4649 40 unneback
`define MODULE cnt_lfsr_ce
4650
`BASE`MODULE
4651 27 unneback
    # ( .length(addr_width))
4652
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
4653
 
4654 40 unneback
`BASE`MODULE
4655 27 unneback
    # (.length(addr_width))
4656
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
4657
 
4658 40 unneback
`BASE`MODULE
4659 27 unneback
    # ( .length(addr_width))
4660
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
4661
 
4662 40 unneback
`BASE`MODULE
4663 27 unneback
    # (.length(addr_width))
4664
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
4665 40 unneback
`undef MODULE
4666 27 unneback
 
4667
// mux read or write adr to DPRAM
4668
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
4669
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
4670
 
4671 40 unneback
`define MODULE dpram_2r2w
4672
`BASE`MODULE
4673 27 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
4674
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
4675
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
4676 40 unneback
`undef MODULE
4677
 
4678
`define MODULE cnt_bin_ce_rew_zq_l1
4679
`BASE`MODULE
4680 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
4681 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));
4682
 
4683 40 unneback
`BASE`MODULE
4684 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
4685 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));
4686 40 unneback
`undef MODULE
4687 27 unneback
 
4688
endmodule
4689 40 unneback
`endif
4690 27 unneback
 
4691 40 unneback
`ifdef FIFO_CMP_ASYNC
4692
`define MODULE fifo_cmp_async
4693
module `BASE`MODULE ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
4694
`undef MODULE
4695 6 unneback
 
4696 11 unneback
   parameter addr_width = 4;
4697
   parameter N = addr_width-1;
4698 6 unneback
 
4699
   parameter Q1 = 2'b00;
4700
   parameter Q2 = 2'b01;
4701
   parameter Q3 = 2'b11;
4702
   parameter Q4 = 2'b10;
4703
 
4704
   parameter going_empty = 1'b0;
4705
   parameter going_full  = 1'b1;
4706
 
4707
   input [N:0]  wptr, rptr;
4708 14 unneback
   output       fifo_empty;
4709 6 unneback
   output       fifo_full;
4710
   input        wclk, rclk, rst;
4711
 
4712
`ifndef GENERATE_DIRECTION_AS_LATCH
4713
   wire direction;
4714
`endif
4715
`ifdef GENERATE_DIRECTION_AS_LATCH
4716
   reg direction;
4717
`endif
4718
   reg  direction_set, direction_clr;
4719
 
4720
   wire async_empty, async_full;
4721
   wire fifo_full2;
4722 14 unneback
   wire fifo_empty2;
4723 6 unneback
 
4724
   // direction_set
4725
   always @ (wptr[N:N-1] or rptr[N:N-1])
4726
     case ({wptr[N:N-1],rptr[N:N-1]})
4727
       {Q1,Q2} : direction_set <= 1'b1;
4728
       {Q2,Q3} : direction_set <= 1'b1;
4729
       {Q3,Q4} : direction_set <= 1'b1;
4730
       {Q4,Q1} : direction_set <= 1'b1;
4731
       default : direction_set <= 1'b0;
4732
     endcase
4733
 
4734
   // direction_clear
4735
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
4736
     if (rst)
4737
       direction_clr <= 1'b1;
4738
     else
4739
       case ({wptr[N:N-1],rptr[N:N-1]})
4740
         {Q2,Q1} : direction_clr <= 1'b1;
4741
         {Q3,Q2} : direction_clr <= 1'b1;
4742
         {Q4,Q3} : direction_clr <= 1'b1;
4743
         {Q1,Q4} : direction_clr <= 1'b1;
4744
         default : direction_clr <= 1'b0;
4745
       endcase
4746
 
4747 40 unneback
`define MODULE dff_sr
4748 6 unneback
`ifndef GENERATE_DIRECTION_AS_LATCH
4749 40 unneback
    `BASE`MODULE dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
4750 6 unneback
`endif
4751
 
4752
`ifdef GENERATE_DIRECTION_AS_LATCH
4753
   always @ (posedge direction_set or posedge direction_clr)
4754
     if (direction_clr)
4755
       direction <= going_empty;
4756
     else
4757
       direction <= going_full;
4758
`endif
4759
 
4760
   assign async_empty = (wptr == rptr) && (direction==going_empty);
4761
   assign async_full  = (wptr == rptr) && (direction==going_full);
4762
 
4763 40 unneback
    `BASE`MODULE dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
4764
    `BASE`MODULE dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
4765
`undef MODULE
4766 6 unneback
 
4767
/*
4768
   always @ (posedge wclk or posedge rst or posedge async_full)
4769
     if (rst)
4770
       {fifo_full, fifo_full2} <= 2'b00;
4771
     else if (async_full)
4772
       {fifo_full, fifo_full2} <= 2'b11;
4773
     else
4774
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
4775
*/
4776 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
4777 6 unneback
     if (async_empty)
4778
       {fifo_empty, fifo_empty2} <= 2'b11;
4779
     else
4780 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
4781 40 unneback
`define MODULE dff
4782
    `BASE`MODULE # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
4783
    `BASE`MODULE # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
4784
`undef MODULE
4785 27 unneback
endmodule // async_compb
4786 40 unneback
`endif
4787 6 unneback
 
4788 40 unneback
`ifdef FIFO_1R1W_ASYNC
4789
`define MODULE fifo_1r1w_async
4790
module `BASE`MODULE (
4791
`undef MODULE
4792 6 unneback
    d, wr, fifo_full, wr_clk, wr_rst,
4793
    q, rd, fifo_empty, rd_clk, rd_rst
4794
    );
4795
 
4796
parameter data_width = 18;
4797
parameter addr_width = 4;
4798
 
4799
// write side
4800
input  [data_width-1:0] d;
4801
input                   wr;
4802
output                  fifo_full;
4803
input                   wr_clk;
4804
input                   wr_rst;
4805
// read side
4806
output [data_width-1:0] q;
4807
input                   rd;
4808
output                  fifo_empty;
4809
input                   rd_clk;
4810
input                   rd_rst;
4811
 
4812
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
4813 23 unneback
 
4814 40 unneback
`define MODULE cnt_gray_ce_bin
4815
`BASE`MODULE
4816 6 unneback
    # ( .length(addr_width))
4817
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
4818
 
4819 40 unneback
`BASE`MODULE
4820 6 unneback
    # (.length(addr_width))
4821 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
4822 40 unneback
`undef MODULE
4823 6 unneback
 
4824 40 unneback
`define MODULE dpram_1r1w
4825
`BASE`MODULE
4826 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
4827
    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));
4828 40 unneback
`undef MODULE
4829 6 unneback
 
4830 40 unneback
`define MODULE fifo_cmp_async
4831
`BASE`MODULE
4832 6 unneback
    # (.addr_width(addr_width))
4833
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
4834 40 unneback
`undef MODULE
4835 6 unneback
 
4836
endmodule
4837 40 unneback
`endif
4838 6 unneback
 
4839 40 unneback
`ifdef FIFO_2R2W_ASYNC
4840
`define MODULE fifo_2r2w_async
4841
module `BASE`MODULE (
4842
`undef MODULE
4843 6 unneback
    // a side
4844
    a_d, a_wr, a_fifo_full,
4845
    a_q, a_rd, a_fifo_empty,
4846
    a_clk, a_rst,
4847
    // b side
4848
    b_d, b_wr, b_fifo_full,
4849
    b_q, b_rd, b_fifo_empty,
4850
    b_clk, b_rst
4851
    );
4852
 
4853
parameter data_width = 18;
4854
parameter addr_width = 4;
4855
 
4856
// a side
4857
input  [data_width-1:0] a_d;
4858
input                   a_wr;
4859
output                  a_fifo_full;
4860
output [data_width-1:0] a_q;
4861
input                   a_rd;
4862
output                  a_fifo_empty;
4863
input                   a_clk;
4864
input                   a_rst;
4865
 
4866
// b side
4867
input  [data_width-1:0] b_d;
4868
input                   b_wr;
4869
output                  b_fifo_full;
4870
output [data_width-1:0] b_q;
4871
input                   b_rd;
4872
output                  b_fifo_empty;
4873
input                   b_clk;
4874
input                   b_rst;
4875
 
4876 40 unneback
`define MODULE fifo_1r1w_async
4877
`BASE`MODULE # (.data_width(data_width), .addr_width(addr_width))
4878 6 unneback
vl_fifo_1r1w_async_a (
4879
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
4880
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
4881
    );
4882
 
4883 40 unneback
`BASE`MODULE # (.data_width(data_width), .addr_width(addr_width))
4884 6 unneback
vl_fifo_1r1w_async_b (
4885
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
4886
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
4887
    );
4888 40 unneback
`undef MODULE
4889
 
4890 6 unneback
endmodule
4891 40 unneback
`endif
4892 6 unneback
 
4893 40 unneback
`ifdef FIFO_2R2W_ASYNC_SIMPLEX
4894
`define MODULE fifo_2r2w_async_simplex
4895
module `BASE`MODULE (
4896
`undef MODULE
4897 6 unneback
    // a side
4898
    a_d, a_wr, a_fifo_full,
4899
    a_q, a_rd, a_fifo_empty,
4900
    a_clk, a_rst,
4901
    // b side
4902
    b_d, b_wr, b_fifo_full,
4903
    b_q, b_rd, b_fifo_empty,
4904
    b_clk, b_rst
4905
    );
4906
 
4907
parameter data_width = 18;
4908
parameter addr_width = 4;
4909
 
4910
// a side
4911
input  [data_width-1:0] a_d;
4912
input                   a_wr;
4913
output                  a_fifo_full;
4914
output [data_width-1:0] a_q;
4915
input                   a_rd;
4916
output                  a_fifo_empty;
4917
input                   a_clk;
4918
input                   a_rst;
4919
 
4920
// b side
4921
input  [data_width-1:0] b_d;
4922
input                   b_wr;
4923
output                  b_fifo_full;
4924
output [data_width-1:0] b_q;
4925
input                   b_rd;
4926
output                  b_fifo_empty;
4927
input                   b_clk;
4928
input                   b_rst;
4929
 
4930
// adr_gen
4931
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
4932
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
4933
// dpram
4934
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
4935
 
4936 40 unneback
`define MODULE cnt_gray_ce_bin
4937
`BASE`MODULE
4938 6 unneback
    # ( .length(addr_width))
4939
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
4940
 
4941 40 unneback
`BASE`MODULE
4942 6 unneback
    # (.length(addr_width))
4943
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
4944
 
4945 40 unneback
`BASE`MODULE
4946 6 unneback
    # ( .length(addr_width))
4947
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
4948
 
4949 40 unneback
`BASE`MODULE
4950 6 unneback
    # (.length(addr_width))
4951
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
4952 40 unneback
`undef MODULE
4953 6 unneback
 
4954
// mux read or write adr to DPRAM
4955
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
4956
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
4957
 
4958 40 unneback
`define MODULE dpram_2r2w
4959
`BASE`MODULE
4960 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
4961
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
4962
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
4963 40 unneback
`undef MODULE
4964 6 unneback
 
4965 40 unneback
`define MODULE fifo_cmp_async
4966
`BASE`MODULE
4967 6 unneback
    # (.addr_width(addr_width))
4968
    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) );
4969
 
4970 40 unneback
`BASE`MODULE
4971 6 unneback
    # (.addr_width(addr_width))
4972
    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) );
4973 40 unneback
`undef MODULE
4974 6 unneback
 
4975
endmodule
4976 40 unneback
`endif
4977 48 unneback
 
4978
`ifdef REG_FILE
4979
`define MODULE reg_file
4980
module `BASE`MODULE (
4981
`undef MODULE
4982
    a1, a2, a3, wd3, we3, rd1, rd2, clk
4983
);
4984
parameter data_width = 32;
4985
parameter addr_width = 5;
4986
input [addr_width-1:0] a1, a2, a3;
4987
input [data_width-1:0] wd3;
4988
input we3;
4989
output [data_width-1:0] rd1, rd2;
4990
input clk;
4991
 
4992
`ifdef ACTEL
4993
reg [data_width-1:0] wd3_reg;
4994
reg [addr_width-1:0] a1_reg, a2_reg, a3_reg;
4995
reg we3_reg;
4996 98 unneback
reg [data_width-1:0] ram1 [(1<<addr_width)-1:0] `SYN_NO_RW_CHECK;
4997
reg [data_width-1:0] ram2 [(1<<addr_width)-1:0] `SYN_NO_RW_CHECK;
4998 48 unneback
always @ (posedge clk or posedge rst)
4999
if (rst)
5000
    {wd3_reg, a3_reg, we3_reg} <= {(data_width+addr_width+1){1'b0}};
5001
else
5002
    {wd3_reg, a3_reg, we3_reg} <= {wd3,a3,wd3};
5003
 
5004
    always @ (negedge clk)
5005
    if (we3_reg)
5006
        ram1[a3_reg] <= wd3;
5007
    always @ (posedge clk)
5008
        a1_reg <= a1;
5009
    assign rd1 = ram1[a1_reg];
5010
 
5011
    always @ (negedge clk)
5012
    if (we3_reg)
5013
        ram2[a3_reg] <= wd3;
5014
    always @ (posedge clk)
5015
        a2_reg <= a2;
5016
    assign rd2 = ram2[a2_reg];
5017
 
5018
`else
5019
 
5020
`define MODULE dpram_1r1w
5021
`BASE`MODULE
5022
    # ( .data_width(data_width), .addr_width(addr_width))
5023
    ram1 (
5024
        .d_a(wd3),
5025
        .adr_a(a3),
5026
        .we_a(we3),
5027
        .clk_a(clk),
5028
        .q_b(rd1),
5029
        .adr_b(a1),
5030
        .clk_b(clk) );
5031
 
5032
`BASE`MODULE
5033
    # ( .data_width(data_width), .addr_width(addr_width))
5034
    ram2 (
5035
        .d_a(wd3),
5036
        .adr_a(a3),
5037
        .we_a(we3),
5038
        .clk_a(clk),
5039
        .q_b(rd2),
5040
        .adr_b(a2),
5041
        .clk_b(clk) );
5042
`undef MODULE
5043
 
5044
`endif
5045
 
5046
endmodule
5047
`endif
5048 12 unneback
//////////////////////////////////////////////////////////////////////
5049
////                                                              ////
5050
////  Versatile library, wishbone stuff                           ////
5051
////                                                              ////
5052
////  Description                                                 ////
5053
////  Wishbone compliant modules                                  ////
5054
////                                                              ////
5055
////                                                              ////
5056
////  To Do:                                                      ////
5057
////   -                                                          ////
5058
////                                                              ////
5059
////  Author(s):                                                  ////
5060
////      - Michael Unneback, unneback@opencores.org              ////
5061
////        ORSoC AB                                              ////
5062
////                                                              ////
5063
//////////////////////////////////////////////////////////////////////
5064
////                                                              ////
5065
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
5066
////                                                              ////
5067
//// This source file may be used and distributed without         ////
5068
//// restriction provided that this copyright statement is not    ////
5069
//// removed from the file and that any derivative work contains  ////
5070
//// the original copyright notice and the associated disclaimer. ////
5071
////                                                              ////
5072
//// This source file is free software; you can redistribute it   ////
5073
//// and/or modify it under the terms of the GNU Lesser General   ////
5074
//// Public License as published by the Free Software Foundation; ////
5075
//// either version 2.1 of the License, or (at your option) any   ////
5076
//// later version.                                               ////
5077
////                                                              ////
5078
//// This source is distributed in the hope that it will be       ////
5079
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
5080
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
5081
//// PURPOSE.  See the GNU Lesser General Public License for more ////
5082
//// details.                                                     ////
5083
////                                                              ////
5084
//// You should have received a copy of the GNU Lesser General    ////
5085
//// Public License along with this source; if not, download it   ////
5086
//// from http://www.opencores.org/lgpl.shtml                     ////
5087
////                                                              ////
5088
//////////////////////////////////////////////////////////////////////
5089
 
5090 75 unneback
`ifdef WB_ADR_INC
5091
`timescale 1ns/1ns
5092
`define MODULE wb_adr_inc
5093 85 unneback
module `BASE`MODULE ( cyc_i, stb_i, cti_i, bte_i, adr_i, we_i, ack_o, adr_o, clk, rst);
5094 75 unneback
`undef MODULE
5095 83 unneback
parameter adr_width = 10;
5096
parameter max_burst_width = 4;
5097 85 unneback
input cyc_i, stb_i, we_i;
5098 83 unneback
input [2:0] cti_i;
5099
input [1:0] bte_i;
5100
input [adr_width-1:0] adr_i;
5101
output [adr_width-1:0] adr_o;
5102
output ack_o;
5103
input clk, rst;
5104 75 unneback
 
5105 83 unneback
reg [adr_width-1:0] adr;
5106 90 unneback
wire [max_burst_width-1:0] to_adr;
5107 91 unneback
reg [max_burst_width-1:0] last_adr;
5108 92 unneback
reg last_cycle;
5109
localparam idle_or_eoc = 1'b0;
5110
localparam cyc_or_ws   = 1'b1;
5111 90 unneback
 
5112 91 unneback
always @ (posedge clk or posedge rst)
5113
if (rst)
5114
    last_adr <= {max_burst_width{1'b0}};
5115
else
5116
    if (stb_i)
5117 92 unneback
        last_adr <=adr_o[max_burst_width-1:0];
5118 91 unneback
 
5119 83 unneback
generate
5120
if (max_burst_width==0) begin : inst_0
5121 97 unneback
 
5122
        reg ack_o;
5123
        assign adr_o = adr_i;
5124
        always @ (posedge clk or posedge rst)
5125
        if (rst)
5126
            ack_o <= 1'b0;
5127
        else
5128
            ack_o <= cyc_i & stb_i & !ack_o;
5129
 
5130 83 unneback
end else begin
5131
 
5132
    always @ (posedge clk or posedge rst)
5133
    if (rst)
5134 92 unneback
        last_cycle <= idle_or_eoc;
5135 83 unneback
    else
5136 92 unneback
        last_cycle <= (!cyc_i) ? idle_or_eoc : //idle
5137
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? idle_or_eoc : // eoc
5138
                      (cyc_i & !stb_i) ? cyc_or_ws : //ws
5139
                      cyc_or_ws; // cyc
5140
    assign to_adr = (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
5141 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
5142 91 unneback
                                        (!stb_i) ? last_adr :
5143 92 unneback
                                        (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] :
5144 85 unneback
                                        adr[max_burst_width-1:0];
5145 92 unneback
    assign ack_o = (last_cycle==cyc_or_ws) & stb_i;
5146 97 unneback
 
5147 83 unneback
end
5148
endgenerate
5149
 
5150
generate
5151
if (max_burst_width==2) begin : inst_2
5152
    always @ (posedge clk or posedge rst)
5153
    if (rst)
5154
        adr <= 2'h0;
5155
    else
5156
        if (cyc_i & stb_i)
5157
            adr[1:0] <= to_adr[1:0] + 2'd1;
5158 75 unneback
        else
5159 83 unneback
            adr <= to_adr[1:0];
5160
end
5161
endgenerate
5162
 
5163
generate
5164
if (max_burst_width==3) begin : inst_3
5165
    always @ (posedge clk or posedge rst)
5166
    if (rst)
5167
        adr <= 3'h0;
5168
    else
5169
        if (cyc_i & stb_i)
5170
            case (bte_i)
5171
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
5172
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
5173 75 unneback
            endcase
5174 83 unneback
        else
5175
            adr <= to_adr[2:0];
5176
end
5177
endgenerate
5178
 
5179
generate
5180
if (max_burst_width==4) begin : inst_4
5181
    always @ (posedge clk or posedge rst)
5182
    if (rst)
5183
        adr <= 4'h0;
5184
    else
5185 91 unneback
        if (stb_i) // | (!stb_i & last_cycle!=ws)) // for !stb_i restart with adr_i +1, only inc once
5186 83 unneback
            case (bte_i)
5187
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
5188
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
5189
            default: adr[3:0] <= to_adr + 4'd1;
5190
            endcase
5191
        else
5192
            adr <= to_adr[3:0];
5193
end
5194
endgenerate
5195
 
5196
generate
5197
if (adr_width > max_burst_width) begin : pass_through
5198
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
5199
end
5200
endgenerate
5201
 
5202
endmodule
5203 75 unneback
`endif
5204
 
5205 105 unneback
`ifdef WB_B4_EOC
5206
`define MODULE wb_b4_eoc
5207
module `BASE`MODULE ( cyc_i, stb_i, stall_o, ack_o, busy, eoc, clk, rst);
5208
`undef MODULE
5209
input cyc_i, stb_i, ack_o;
5210
output busy, eoc;
5211
input clk, rst;
5212
 
5213
`define MODULE cnt_bin_ce_rew_zq_l1
5214
`BASE`MODULE # ( .length(4), level1_value(1))
5215
cnt0 (
5216
    .cke(), .rew(), .zq(), .level1(), .rst(), clk);
5217
`undef MODULE
5218
 
5219
endmodule
5220
`endif
5221
 
5222 40 unneback
`ifdef WB3WB3_BRIDGE
5223 12 unneback
// async wb3 - wb3 bridge
5224
`timescale 1ns/1ns
5225 40 unneback
`define MODULE wb3wb3_bridge
5226
module `BASE`MODULE (
5227
`undef MODULE
5228 12 unneback
        // wishbone slave side
5229
        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,
5230
        // wishbone master side
5231
        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);
5232
 
5233 95 unneback
parameter style = "FIFO"; // valid: simple, FIFO
5234
parameter addr_width = 4;
5235
 
5236 12 unneback
input [31:0] wbs_dat_i;
5237
input [31:2] wbs_adr_i;
5238
input [3:0]  wbs_sel_i;
5239
input [1:0]  wbs_bte_i;
5240
input [2:0]  wbs_cti_i;
5241
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
5242
output [31:0] wbs_dat_o;
5243 14 unneback
output wbs_ack_o;
5244 12 unneback
input wbs_clk, wbs_rst;
5245
 
5246
output [31:0] wbm_dat_o;
5247
output reg [31:2] wbm_adr_o;
5248
output [3:0]  wbm_sel_o;
5249
output reg [1:0]  wbm_bte_o;
5250
output reg [2:0]  wbm_cti_o;
5251 14 unneback
output reg wbm_we_o;
5252
output wbm_cyc_o;
5253 12 unneback
output wbm_stb_o;
5254
input [31:0]  wbm_dat_i;
5255
input wbm_ack_i;
5256
input wbm_clk, wbm_rst;
5257
 
5258
// bte
5259
parameter linear       = 2'b00;
5260
parameter wrap4        = 2'b01;
5261
parameter wrap8        = 2'b10;
5262
parameter wrap16       = 2'b11;
5263
// cti
5264
parameter classic      = 3'b000;
5265
parameter incburst     = 3'b010;
5266
parameter endofburst   = 3'b111;
5267
 
5268 95 unneback
localparam wbs_adr  = 1'b0;
5269
localparam wbs_data = 1'b1;
5270 12 unneback
 
5271 95 unneback
localparam wbm_adr0      = 2'b00;
5272
localparam wbm_adr1      = 2'b01;
5273
localparam wbm_data      = 2'b10;
5274
localparam wbm_data_wait = 2'b11;
5275 12 unneback
 
5276
reg [1:0] wbs_bte_reg;
5277
reg wbs;
5278
wire wbs_eoc_alert, wbm_eoc_alert;
5279
reg wbs_eoc, wbm_eoc;
5280
reg [1:0] wbm;
5281
 
5282 14 unneback
wire [1:16] wbs_count, wbm_count;
5283 12 unneback
 
5284
wire [35:0] a_d, a_q, b_d, b_q;
5285
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
5286
reg a_rd_reg;
5287
wire b_rd_adr, b_rd_data;
5288 14 unneback
wire b_rd_data_reg;
5289
wire [35:0] temp;
5290 12 unneback
 
5291
`define WE 5
5292
`define BTE 4:3
5293
`define CTI 2:0
5294
 
5295
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]);
5296
always @ (posedge wbs_clk or posedge wbs_rst)
5297
if (wbs_rst)
5298
        wbs_eoc <= 1'b0;
5299
else
5300
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
5301 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
5302 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
5303
                wbs_eoc <= 1'b1;
5304
 
5305 40 unneback
`define MODULE cnt_shreg_ce_clear
5306
`BASE`MODULE # ( .length(16))
5307
`undef MODULE
5308 12 unneback
    cnt0 (
5309
        .cke(wbs_ack_o),
5310
        .clear(wbs_eoc),
5311
        .q(wbs_count),
5312
        .rst(wbs_rst),
5313
        .clk(wbs_clk));
5314
 
5315
always @ (posedge wbs_clk or posedge wbs_rst)
5316
if (wbs_rst)
5317
        wbs <= wbs_adr;
5318
else
5319 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
5320 12 unneback
                wbs <= wbs_data;
5321
        else if (wbs_eoc & wbs_ack_o)
5322
                wbs <= wbs_adr;
5323
 
5324
// wbs FIFO
5325 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};
5326
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
5327 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
5328
              1'b0;
5329
assign a_rd = !a_fifo_empty;
5330
always @ (posedge wbs_clk or posedge wbs_rst)
5331
if (wbs_rst)
5332
        a_rd_reg <= 1'b0;
5333
else
5334
        a_rd_reg <= a_rd;
5335
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
5336
 
5337
assign wbs_dat_o = a_q[35:4];
5338
 
5339
always @ (posedge wbs_clk or posedge wbs_rst)
5340
if (wbs_rst)
5341 13 unneback
        wbs_bte_reg <= 2'b00;
5342 12 unneback
else
5343 13 unneback
        wbs_bte_reg <= wbs_bte_i;
5344 12 unneback
 
5345
// wbm FIFO
5346
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]);
5347
always @ (posedge wbm_clk or posedge wbm_rst)
5348
if (wbm_rst)
5349
        wbm_eoc <= 1'b0;
5350
else
5351
        if (wbm==wbm_adr0 & !b_fifo_empty)
5352
                wbm_eoc <= b_q[`BTE] == linear;
5353
        else if (wbm_eoc_alert & wbm_ack_i)
5354
                wbm_eoc <= 1'b1;
5355
 
5356
always @ (posedge wbm_clk or posedge wbm_rst)
5357
if (wbm_rst)
5358
        wbm <= wbm_adr0;
5359
else
5360 33 unneback
/*
5361 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
5362
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
5363
        (wbm==wbm_adr1 & !wbm_we_o) |
5364
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
5365
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
5366 33 unneback
*/
5367
    case (wbm)
5368
    wbm_adr0:
5369
        if (!b_fifo_empty)
5370
            wbm <= wbm_adr1;
5371
    wbm_adr1:
5372
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
5373
            wbm <= wbm_data;
5374
    wbm_data:
5375
        if (wbm_ack_i & wbm_eoc)
5376
            wbm <= wbm_adr0;
5377
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
5378
            wbm <= wbm_data_wait;
5379
    wbm_data_wait:
5380
        if (!b_fifo_empty)
5381
            wbm <= wbm_data;
5382
    endcase
5383 12 unneback
 
5384
assign b_d = {wbm_dat_i,4'b1111};
5385
assign b_wr = !wbm_we_o & wbm_ack_i;
5386
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
5387
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
5388
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
5389 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
5390 12 unneback
                   1'b0;
5391
assign b_rd = b_rd_adr | b_rd_data;
5392
 
5393 40 unneback
`define MODULE dff
5394
`BASE`MODULE dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
5395
`undef MODULE
5396
`define MODULE dff_ce
5397
`BASE`MODULE # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
5398
`undef MODULE
5399 12 unneback
 
5400
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
5401
 
5402 40 unneback
`define MODULE cnt_shreg_ce_clear
5403 42 unneback
`BASE`MODULE # ( .length(16))
5404 40 unneback
`undef MODULE
5405 12 unneback
    cnt1 (
5406
        .cke(wbm_ack_i),
5407
        .clear(wbm_eoc),
5408
        .q(wbm_count),
5409
        .rst(wbm_rst),
5410
        .clk(wbm_clk));
5411
 
5412 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
5413
assign wbm_stb_o = (wbm==wbm_data);
5414 12 unneback
 
5415
always @ (posedge wbm_clk or posedge wbm_rst)
5416
if (wbm_rst)
5417
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
5418
else begin
5419
        if (wbm==wbm_adr0 & !b_fifo_empty)
5420
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
5421
        else if (wbm_eoc_alert & wbm_ack_i)
5422
                wbm_cti_o <= endofburst;
5423
end
5424
 
5425
//async_fifo_dw_simplex_top
5426 40 unneback
`define MODULE fifo_2r2w_async_simplex
5427
`BASE`MODULE
5428
`undef MODULE
5429 12 unneback
# ( .data_width(36), .addr_width(addr_width))
5430
fifo (
5431
    // a side
5432
    .a_d(a_d),
5433
    .a_wr(a_wr),
5434
    .a_fifo_full(a_fifo_full),
5435
    .a_q(a_q),
5436
    .a_rd(a_rd),
5437
    .a_fifo_empty(a_fifo_empty),
5438
    .a_clk(wbs_clk),
5439
    .a_rst(wbs_rst),
5440
    // b side
5441
    .b_d(b_d),
5442
    .b_wr(b_wr),
5443
    .b_fifo_full(b_fifo_full),
5444
    .b_q(b_q),
5445
    .b_rd(b_rd),
5446
    .b_fifo_empty(b_fifo_empty),
5447
    .b_clk(wbm_clk),
5448
    .b_rst(wbm_rst)
5449
    );
5450
 
5451
endmodule
5452 40 unneback
`undef WE
5453
`undef BTE
5454
`undef CTI
5455
`endif
5456 17 unneback
 
5457 75 unneback
`ifdef WB3AVALON_BRIDGE
5458
`define MODULE wb3avalon_bridge
5459
module `BASE`MODULE (
5460
`undef MODULE
5461
        // wishbone slave side
5462
        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,
5463 77 unneback
        // avalon master side
5464 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
5465
 
5466 85 unneback
parameter linewrapburst = 1'b0;
5467
 
5468 75 unneback
input [31:0] wbs_dat_i;
5469
input [31:2] wbs_adr_i;
5470
input [3:0]  wbs_sel_i;
5471
input [1:0]  wbs_bte_i;
5472
input [2:0]  wbs_cti_i;
5473 83 unneback
input wbs_we_i;
5474
input wbs_cyc_i;
5475
input wbs_stb_i;
5476 75 unneback
output [31:0] wbs_dat_o;
5477
output wbs_ack_o;
5478
input wbs_clk, wbs_rst;
5479
 
5480
input [31:0] readdata;
5481
output [31:0] writedata;
5482
output [31:2] address;
5483
output [3:0]  be;
5484
output write;
5485 81 unneback
output read;
5486 75 unneback
output beginbursttransfer;
5487
output [3:0] burstcount;
5488
input readdatavalid;
5489
input waitrequest;
5490
input clk;
5491
input rst;
5492
 
5493
wire [1:0] wbm_bte_o;
5494
wire [2:0] wbm_cti_o;
5495
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
5496
reg last_cyc;
5497 79 unneback
reg [3:0] counter;
5498 82 unneback
reg read_busy;
5499 75 unneback
 
5500
always @ (posedge clk or posedge rst)
5501
if (rst)
5502
    last_cyc <= 1'b0;
5503
else
5504
    last_cyc <= wbm_cyc_o;
5505
 
5506 79 unneback
always @ (posedge clk or posedge rst)
5507
if (rst)
5508 82 unneback
    read_busy <= 1'b0;
5509 79 unneback
else
5510 82 unneback
    if (read & !waitrequest)
5511
        read_busy <= 1'b1;
5512
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
5513
        read_busy <= 1'b0;
5514
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
5515 81 unneback
 
5516 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
5517
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
5518
                    (wbm_bte_o==2'b10) ? 4'd8 :
5519 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
5520
                    4'd1;
5521 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
5522 75 unneback
 
5523 79 unneback
always @ (posedge clk or posedge rst)
5524
if (rst) begin
5525
    counter <= 4'd0;
5526
end else
5527 80 unneback
    if (wbm_we_o) begin
5528
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
5529 85 unneback
            counter <= burstcount -4'd1;
5530 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
5531
            counter <= burstcount;
5532
        end else if (!waitrequest & wbm_stb_o) begin
5533
            counter <= counter - 4'd1;
5534
        end
5535 82 unneback
    end
5536 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
5537 79 unneback
 
5538 75 unneback
`define MODULE wb3wb3_bridge
5539 77 unneback
`BASE`MODULE wbwb3inst (
5540 75 unneback
`undef MODULE
5541
    // wishbone slave side
5542
    .wbs_dat_i(wbs_dat_i),
5543
    .wbs_adr_i(wbs_adr_i),
5544
    .wbs_sel_i(wbs_sel_i),
5545
    .wbs_bte_i(wbs_bte_i),
5546
    .wbs_cti_i(wbs_cti_i),
5547
    .wbs_we_i(wbs_we_i),
5548
    .wbs_cyc_i(wbs_cyc_i),
5549
    .wbs_stb_i(wbs_stb_i),
5550
    .wbs_dat_o(wbs_dat_o),
5551
    .wbs_ack_o(wbs_ack_o),
5552
    .wbs_clk(wbs_clk),
5553
    .wbs_rst(wbs_rst),
5554
    // wishbone master side
5555
    .wbm_dat_o(writedata),
5556 78 unneback
    .wbm_adr_o(address),
5557 75 unneback
    .wbm_sel_o(be),
5558
    .wbm_bte_o(wbm_bte_o),
5559
    .wbm_cti_o(wbm_cti_o),
5560
    .wbm_we_o(wbm_we_o),
5561
    .wbm_cyc_o(wbm_cyc_o),
5562
    .wbm_stb_o(wbm_stb_o),
5563
    .wbm_dat_i(readdata),
5564
    .wbm_ack_i(wbm_ack_i),
5565
    .wbm_clk(clk),
5566
    .wbm_rst(rst));
5567
 
5568
 
5569
endmodule
5570
`endif
5571
 
5572 105 unneback
`ifdef WB_ARBITER
5573
`define MODULE wb_arbiter
5574 42 unneback
module `BASE`MODULE (
5575 40 unneback
`undef MODULE
5576 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,
5577 105 unneback
    wbm_dat_i, wbm_stall_i, wbm_ack_i, wbm_err_i, wbm_rty_i,
5578 39 unneback
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
5579 105 unneback
    wbs_dat_o, wbs_stall_o, wbs_ack_o, wbs_err_o, wbs_rty_o,
5580 39 unneback
    wb_clk, wb_rst
5581
);
5582
 
5583
parameter nr_of_ports = 3;
5584
parameter adr_size = 26;
5585
parameter adr_lo   = 2;
5586
parameter dat_size = 32;
5587
parameter sel_size = dat_size/8;
5588
 
5589
localparam aw = (adr_size - adr_lo) * nr_of_ports;
5590
localparam dw = dat_size * nr_of_ports;
5591
localparam sw = sel_size * nr_of_ports;
5592
localparam cw = 3 * nr_of_ports;
5593
localparam bw = 2 * nr_of_ports;
5594
 
5595
input  [dw-1:0] wbm_dat_o;
5596
input  [aw-1:0] wbm_adr_o;
5597
input  [sw-1:0] wbm_sel_o;
5598
input  [cw-1:0] wbm_cti_o;
5599
input  [bw-1:0] wbm_bte_o;
5600
input  [nr_of_ports-1:0] wbm_we_o, wbm_stb_o, wbm_cyc_o;
5601
output [dw-1:0] wbm_dat_i;
5602 105 unneback
output [nr_of_ports-1:0] wbm_stall_o, wbm_ack_i, wbm_err_i, wbm_rty_i;
5603 39 unneback
 
5604
output [dat_size-1:0] wbs_dat_i;
5605
output [adr_size-1:adr_lo] wbs_adr_i;
5606
output [sel_size-1:0] wbs_sel_i;
5607
output [2:0] wbs_cti_i;
5608
output [1:0] wbs_bte_i;
5609
output wbs_we_i, wbs_stb_i, wbs_cyc_i;
5610
input  [dat_size-1:0] wbs_dat_o;
5611 105 unneback
input  wbs_stall_o, wbs_ack_o, wbs_err_o, wbs_rty_o;
5612 39 unneback
 
5613
input wb_clk, wb_rst;
5614
 
5615 44 unneback
reg  [nr_of_ports-1:0] select;
5616 39 unneback
wire [nr_of_ports-1:0] state;
5617
wire [nr_of_ports-1:0] eoc; // end-of-cycle
5618
wire [nr_of_ports-1:0] sel;
5619
wire idle;
5620
 
5621
genvar i;
5622
 
5623
assign idle = !(|state);
5624
 
5625
generate
5626
if (nr_of_ports == 2) begin
5627
 
5628
    wire [2:0] wbm1_cti_o, wbm0_cti_o;
5629
 
5630
    assign {wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5631
 
5632 44 unneback
    //assign select = (idle) ? {wbm_cyc_o[1],!wbm_cyc_o[1] & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
5633
 
5634
    always @ (idle or wbm_cyc_o)
5635
    if (idle)
5636
        casex (wbm_cyc_o)
5637
        2'b1x : select = 2'b10;
5638
        2'b01 : select = 2'b01;
5639
        default : select = {nr_of_ports{1'b0}};
5640
        endcase
5641
    else
5642
        select = {nr_of_ports{1'b0}};
5643
 
5644 39 unneback
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5645
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5646
 
5647
end
5648
endgenerate
5649
 
5650
generate
5651
if (nr_of_ports == 3) begin
5652
 
5653
    wire [2:0] wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5654
 
5655
    assign {wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5656
 
5657 44 unneback
    always @ (idle or wbm_cyc_o)
5658
    if (idle)
5659
        casex (wbm_cyc_o)
5660
        3'b1xx : select = 3'b100;
5661
        3'b01x : select = 3'b010;
5662
        3'b001 : select = 3'b001;
5663
        default : select = {nr_of_ports{1'b0}};
5664
        endcase
5665
    else
5666
        select = {nr_of_ports{1'b0}};
5667
 
5668
//    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}};
5669 39 unneback
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5670
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5671
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5672
 
5673
end
5674
endgenerate
5675
 
5676
generate
5677 44 unneback
if (nr_of_ports == 4) begin
5678
 
5679
    wire [2:0] wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5680
 
5681
    assign {wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5682
 
5683
    //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}};
5684
 
5685
    always @ (idle or wbm_cyc_o)
5686
    if (idle)
5687
        casex (wbm_cyc_o)
5688
        4'b1xxx : select = 4'b1000;
5689
        4'b01xx : select = 4'b0100;
5690
        4'b001x : select = 4'b0010;
5691
        4'b0001 : select = 4'b0001;
5692
        default : select = {nr_of_ports{1'b0}};
5693
        endcase
5694
    else
5695
        select = {nr_of_ports{1'b0}};
5696
 
5697
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5698
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5699
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5700
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5701
 
5702
end
5703
endgenerate
5704
 
5705
generate
5706
if (nr_of_ports == 5) begin
5707
 
5708
    wire [2:0] wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5709
 
5710
    assign {wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5711
 
5712
    //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}};
5713
 
5714
    always @ (idle or wbm_cyc_o)
5715
    if (idle)
5716
        casex (wbm_cyc_o)
5717
        5'b1xxxx : select = 5'b10000;
5718
        5'b01xxx : select = 5'b01000;
5719
        5'b001xx : select = 5'b00100;
5720
        5'b0001x : select = 5'b00010;
5721
        5'b00001 : select = 5'b00001;
5722
        default : select = {nr_of_ports{1'b0}};
5723
        endcase
5724
    else
5725
        select = {nr_of_ports{1'b0}};
5726
 
5727
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5728
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5729
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5730
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5731
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5732
 
5733
end
5734
endgenerate
5735
 
5736
generate
5737 67 unneback
if (nr_of_ports == 6) begin
5738
 
5739
    wire [2:0] wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5740
 
5741
    assign {wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
5742
 
5743
    //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}};
5744
 
5745
    always @ (idle or wbm_cyc_o)
5746
    if (idle)
5747
        casex (wbm_cyc_o)
5748
        6'b1xxxxx : select = 6'b100000;
5749
        6'b01xxxx : select = 6'b010000;
5750
        6'b001xxx : select = 6'b001000;
5751
        6'b0001xx : select = 6'b000100;
5752
        6'b00001x : select = 6'b000010;
5753
        6'b000001 : select = 6'b000001;
5754
        default : select = {nr_of_ports{1'b0}};
5755
        endcase
5756
    else
5757
        select = {nr_of_ports{1'b0}};
5758
 
5759
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5760
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5761
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5762
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5763
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5764
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5765
 
5766
end
5767
endgenerate
5768
 
5769
generate
5770
if (nr_of_ports == 7) begin
5771
 
5772
    wire [2:0] wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
5773
 
5774
    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;
5775
 
5776
    //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}};
5777
 
5778
    always @ (idle or wbm_cyc_o)
5779
    if (idle)
5780
        casex (wbm_cyc_o)
5781
        7'b1xxxxxx : select = 7'b1000000;
5782
        7'b01xxxxx : select = 7'b0100000;
5783
        7'b001xxxx : select = 7'b0010000;
5784
        7'b0001xxx : select = 7'b0001000;
5785
        7'b00001xx : select = 7'b0000100;
5786
        7'b000001x : select = 7'b0000010;
5787
        7'b0000001 : select = 7'b0000001;
5788
        default : select = {nr_of_ports{1'b0}};
5789
        endcase
5790
    else
5791
        select = {nr_of_ports{1'b0}};
5792
 
5793
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
5794
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5795
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5796
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5797
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5798
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5799
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5800
 
5801
end
5802
endgenerate
5803
 
5804
generate
5805
if (nr_of_ports == 8) begin
5806
 
5807
    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;
5808
 
5809
    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;
5810
 
5811
    //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}};
5812
 
5813
    always @ (idle or wbm_cyc_o)
5814
    if (idle)
5815
        casex (wbm_cyc_o)
5816
        8'b1xxxxxxx : select = 8'b10000000;
5817
        8'b01xxxxxx : select = 8'b01000000;
5818
        8'b001xxxxx : select = 8'b00100000;
5819
        8'b0001xxxx : select = 8'b00010000;
5820
        8'b00001xxx : select = 8'b00001000;
5821
        8'b000001xx : select = 8'b00000100;
5822
        8'b0000001x : select = 8'b00000010;
5823
        8'b00000001 : select = 8'b00000001;
5824
        default : select = {nr_of_ports{1'b0}};
5825
        endcase
5826
    else
5827
        select = {nr_of_ports{1'b0}};
5828
 
5829
    assign eoc[7] = (wbm_ack_i[7] & (wbm7_cti_o == 3'b000 | wbm7_cti_o == 3'b111)) | !wbm_cyc_o[7];
5830
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
5831
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
5832
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
5833
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
5834
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
5835
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
5836
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
5837
 
5838
end
5839
endgenerate
5840
 
5841
generate
5842 63 unneback
for (i=0;i<nr_of_ports;i=i+1) begin : spr0
5843 42 unneback
`define MODULE spr
5844
    `BASE`MODULE sr0( .sp(select[i]), .r(eoc[i]), .q(state[i]), .clk(wb_clk), .rst(wb_rst));
5845
`undef MODULE
5846 39 unneback
end
5847
endgenerate
5848
 
5849
    assign sel = select | state;
5850
 
5851 40 unneback
`define MODULE mux_andor
5852
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(32)) mux0 ( .a(wbm_dat_o), .sel(sel), .dout(wbs_dat_i));
5853
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(adr_size-adr_lo)) mux1 ( .a(wbm_adr_o), .sel(sel), .dout(wbs_adr_i));
5854
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(sel_size)) mux2 ( .a(wbm_sel_o), .sel(sel), .dout(wbs_sel_i));
5855
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(3)) mux3 ( .a(wbm_cti_o), .sel(sel), .dout(wbs_cti_i));
5856
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(2)) mux4 ( .a(wbm_bte_o), .sel(sel), .dout(wbs_bte_i));
5857
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux5 ( .a(wbm_we_o), .sel(sel), .dout(wbs_we_i));
5858
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux6 ( .a(wbm_stb_o), .sel(sel), .dout(wbs_stb_i));
5859
`undef MODULE
5860 39 unneback
    assign wbs_cyc_i = |sel;
5861
 
5862
    assign wbm_dat_i = {nr_of_ports{wbs_dat_o}};
5863
    assign wbm_ack_i = {nr_of_ports{wbs_ack_o}} & sel;
5864
    assign wbm_err_i = {nr_of_ports{wbs_err_o}} & sel;
5865
    assign wbm_rty_i = {nr_of_ports{wbs_rty_o}} & sel;
5866
 
5867
endmodule
5868 40 unneback
`endif
5869 39 unneback
 
5870 101 unneback
`ifdef WB_RAM
5871 49 unneback
// WB RAM with byte enable
5872 101 unneback
`define MODULE wb_ram
5873 59 unneback
module `BASE`MODULE (
5874
`undef MODULE
5875 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,
5876 101 unneback
    wbs_dat_o, wbs_ack_o, wbs_stall_o, wb_clk, wb_rst);
5877 59 unneback
 
5878 101 unneback
parameter adr_width = 16;
5879
parameter mem_size = 1<<adr_width;
5880
parameter dat_width = 32;
5881
parameter max_burst_width = 4; // only used for B3
5882
parameter mode = "B3"; // valid options: B3, B4
5883 60 unneback
parameter memory_init = 1;
5884
parameter memory_file = "vl_ram.vmem";
5885 59 unneback
 
5886 101 unneback
input [dat_width-1:0] wbs_dat_i;
5887
input [adr_width-1:0] wbs_adr_i;
5888
input [2:0] wbs_cti_i;
5889
input [1:0] wbs_bte_i;
5890
input [dat_width/8-1:0] wbs_sel_i;
5891 70 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
5892 101 unneback
output [dat_width-1:0] wbs_dat_o;
5893 70 unneback
output wbs_ack_o;
5894 101 unneback
output wbs_stall_o;
5895 71 unneback
input wb_clk, wb_rst;
5896 59 unneback
 
5897 101 unneback
wire [adr_width-1:0] adr;
5898
wire we;
5899 59 unneback
 
5900 101 unneback
generate
5901
if (mode=="B3") begin : B3_inst
5902 83 unneback
`define MODULE wb_adr_inc
5903 101 unneback
`BASE`MODULE # ( .adr_width(adr_width), .max_burst_width(max_burst_width)) adr_inc0 (
5904 83 unneback
    .cyc_i(wbs_cyc_i),
5905
    .stb_i(wbs_stb_i),
5906
    .cti_i(wbs_cti_i),
5907
    .bte_i(wbs_bte_i),
5908
    .adr_i(wbs_adr_i),
5909 85 unneback
    .we_i(wbs_we_i),
5910 83 unneback
    .ack_o(wbs_ack_o),
5911
    .adr_o(adr),
5912
    .clk(wb_clk),
5913
    .rst(wb_rst));
5914
`undef MODULE
5915 101 unneback
assign we = wbs_we_i & wbs_ack_o;
5916
end else if (mode=="B4") begin : B4_inst
5917
reg wbs_ack_o_reg;
5918
always @ (posedge wb_clk or posedge wb_rst)
5919
    if (wb_rst)
5920
        wbs_ack_o_reg <= 1'b0;
5921
    else
5922
        wbs_ack_o_reg <= wbs_stb_i & wbs_cyc_i;
5923
assign wbs_ack_o = wbs_ack_o_reg;
5924
assign wbs_stall_o = 1'b0;
5925
assign adr = wbs_adr_i;
5926
assign we = wbs_we_i & wbs_cyc_i & wbs_stb_i;
5927
end
5928
endgenerate
5929 60 unneback
 
5930 100 unneback
`define MODULE ram_be
5931
`BASE`MODULE # (
5932
    .data_width(dat_width),
5933
    .addr_width(adr_width),
5934
    .mem_size(mem_size),
5935
    .memory_init(memory_init),
5936
    .memory_file(memory_file))
5937
ram0(
5938
`undef MODULE
5939 101 unneback
    .d(wbs_dat_i),
5940
    .adr(adr),
5941
    .be(wbs_sel_i),
5942
    .we(we),
5943
    .q(wbs_dat_o),
5944 100 unneback
    .clk(wb_clk)
5945
);
5946 49 unneback
 
5947
endmodule
5948
`endif
5949
 
5950 103 unneback
`ifdef WB_SHADOW_RAM
5951
// A wishbone compliant RAM module that can be placed in front of other memory controllers
5952
`define MODULE wb_shadow_ram
5953
module `BASE`MODULE (
5954
`undef MODULE
5955
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
5956
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
5957
    wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
5958
    wbm_dat_i, wbm_ack_i, wbm_stall_i,
5959
    wb_clk, wb_rst);
5960
 
5961
parameter dat_width = 32;
5962
parameter mode = "B4";
5963
parameter max_burst_width = 4; // only used for B3
5964
 
5965
parameter shadow_mem_adr_width = 10;
5966
parameter shadow_mem_size = 1024;
5967
parameter shadow_mem_init = 2;
5968
parameter shadow_mem_file = "vl_ram.v";
5969
 
5970
parameter main_mem_adr_width = 24;
5971
 
5972
input [dat_width-1:0] wbs_dat_i;
5973
input [main_mem_adr_width-1:0] wbs_adr_i;
5974
input [2:0] wbs_cti_i;
5975
input [1:0] wbs_bte_i;
5976
input [dat_width/8-1:0] wbs_sel_i;
5977
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
5978
output [dat_width-1:0] wbs_dat_o;
5979
output wbs_ack_o;
5980
output wbs_stall_o;
5981
 
5982
output [dat_width-1:0] wbm_dat_o;
5983
output [main_mem_adr_width-1:0] wbm_adr_o;
5984
output [2:0] wbm_cti_o;
5985
output [1:0] wbm_bte_o;
5986
output [dat_width/8-1:0] wbm_sel_o;
5987
output wbm_we_o, wbm_stb_o, wbm_cyc_o;
5988
input [dat_width-1:0] wbm_dat_i;
5989
input wbm_ack_i, wbm_stall_i;
5990
 
5991
input wb_clk, wb_rst;
5992
 
5993
generate
5994
if (shadow_mem_size>0) begin : shadow_ram_inst
5995
 
5996
wire cyc;
5997
wire [dat_width-1:0] dat;
5998
wire stall, ack;
5999
 
6000
assign cyc = wbs_cyc_i & (wbs_adr_i<=shadow_mem_size);
6001
`define MODULE wb_ram
6002
`BASE`MODULE # (
6003
    .dat_width(dat_width),
6004
    .adr_width(shadow_mem_adr_width),
6005
    .mem_size(shadow_mem_size),
6006
    .memory_init(shadow_mem_init),
6007 117 unneback
    .memory_file(shadow_mem_file),
6008 103 unneback
    .mode(mode))
6009
shadow_mem0 (
6010
    .wbs_dat_i(wbs_dat_i),
6011
    .wbs_adr_i(wbs_adr_i[shadow_mem_adr_width-1:0]),
6012
    .wbs_sel_i(wbs_sel_i),
6013
    .wbs_we_i (wbs_we_i),
6014
    .wbs_bte_i(wbs_bte_i),
6015
    .wbs_cti_i(wbs_cti_i),
6016
    .wbs_stb_i(wbs_stb_i),
6017
    .wbs_cyc_i(cyc),
6018
    .wbs_dat_o(dat),
6019
    .wbs_stall_o(stall),
6020
    .wbs_ack_o(ack),
6021
    .wb_clk(wb_clk),
6022
    .wb_rst(wb_rst));
6023
`undef MODULE
6024
 
6025
assign {wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o} =
6026
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i};
6027
assign wbm_cyc_o = wbs_cyc_i & (wbs_adr_i>shadow_mem_size);
6028
 
6029
assign wbs_dat_o = (dat & {dat_width{cyc}}) | (wbm_dat_i & {dat_width{wbm_cyc_o}});
6030
assign wbs_ack_o = (ack & cyc) | (wbm_ack_i & wbm_cyc_o);
6031
assign wbs_stall_o = (stall & cyc) | (wbm_stall_i & wbm_cyc_o);
6032
 
6033
end else begin : no_shadow_ram_inst
6034
 
6035
assign {wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o, wbm_cyc_o} =
6036
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i};
6037
assign {wbs_dat_o, wbs_ack_o, wbs_stall_o} = {wbm_dat_i, wbm_ack_i, wbm_stall_i};
6038
 
6039
end
6040
endgenerate
6041
 
6042
endmodule
6043
`endif
6044
 
6045 48 unneback
`ifdef WB_B4_ROM
6046
// WB ROM
6047
`define MODULE wb_b4_rom
6048
module `BASE`MODULE (
6049
`undef MODULE
6050
    wb_adr_i, wb_stb_i, wb_cyc_i,
6051
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
6052
 
6053
    parameter dat_width = 32;
6054
    parameter dat_default = 32'h15000000;
6055
    parameter adr_width = 32;
6056
 
6057
/*
6058
`ifndef ROM
6059
`define ROM "rom.v"
6060
`endif
6061
*/
6062
    input [adr_width-1:2]   wb_adr_i;
6063
    input                   wb_stb_i;
6064
    input                   wb_cyc_i;
6065
    output [dat_width-1:0]  wb_dat_o;
6066
    reg [dat_width-1:0]     wb_dat_o;
6067
    output                  wb_ack_o;
6068
    reg                     wb_ack_o;
6069
    output                  stall_o;
6070
    input                   wb_clk;
6071
    input                   wb_rst;
6072
 
6073
always @ (posedge wb_clk or posedge wb_rst)
6074
    if (wb_rst)
6075
        wb_dat_o <= {dat_width{1'b0}};
6076
    else
6077
         case (wb_adr_i[adr_width-1:2])
6078
`ifdef ROM
6079
`include `ROM
6080
`endif
6081
           default:
6082
             wb_dat_o <= dat_default;
6083
 
6084
         endcase // case (wb_adr_i)
6085
 
6086
 
6087
always @ (posedge wb_clk or posedge wb_rst)
6088
    if (wb_rst)
6089
        wb_ack_o <= 1'b0;
6090
    else
6091
        wb_ack_o <= wb_stb_i & wb_cyc_i;
6092
 
6093
assign stall_o = 1'b0;
6094
 
6095
endmodule
6096
`endif
6097
 
6098
 
6099 40 unneback
`ifdef WB_BOOT_ROM
6100 17 unneback
// WB ROM
6101 40 unneback
`define MODULE wb_boot_rom
6102
module `BASE`MODULE (
6103
`undef MODULE
6104 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
6105 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
6106 17 unneback
 
6107 18 unneback
    parameter adr_hi = 31;
6108
    parameter adr_lo = 28;
6109
    parameter adr_sel = 4'hf;
6110
    parameter addr_width = 5;
6111 33 unneback
/*
6112 17 unneback
`ifndef BOOT_ROM
6113
`define BOOT_ROM "boot_rom.v"
6114
`endif
6115 33 unneback
*/
6116 18 unneback
    input [adr_hi:2]    wb_adr_i;
6117
    input               wb_stb_i;
6118
    input               wb_cyc_i;
6119
    output [31:0]        wb_dat_o;
6120
    output              wb_ack_o;
6121
    output              hit_o;
6122
    input               wb_clk;
6123
    input               wb_rst;
6124
 
6125
    wire hit;
6126
    reg [31:0] wb_dat;
6127
    reg wb_ack;
6128
 
6129
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
6130 17 unneback
 
6131
always @ (posedge wb_clk or posedge wb_rst)
6132
    if (wb_rst)
6133 18 unneback
        wb_dat <= 32'h15000000;
6134 17 unneback
    else
6135 18 unneback
         case (wb_adr_i[addr_width-1:2])
6136 33 unneback
`ifdef BOOT_ROM
6137 17 unneback
`include `BOOT_ROM
6138 33 unneback
`endif
6139 17 unneback
           /*
6140
            // Zero r0 and jump to 0x00000100
6141 18 unneback
 
6142
            1 : wb_dat <= 32'hA8200000;
6143
            2 : wb_dat <= 32'hA8C00100;
6144
            3 : wb_dat <= 32'h44003000;
6145
            4 : wb_dat <= 32'h15000000;
6146 17 unneback
            */
6147
           default:
6148 18 unneback
             wb_dat <= 32'h00000000;
6149 17 unneback
 
6150
         endcase // case (wb_adr_i)
6151
 
6152
 
6153
always @ (posedge wb_clk or posedge wb_rst)
6154
    if (wb_rst)
6155 18 unneback
        wb_ack <= 1'b0;
6156 17 unneback
    else
6157 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
6158 17 unneback
 
6159 18 unneback
assign hit_o = hit;
6160
assign wb_dat_o = wb_dat & {32{wb_ack}};
6161
assign wb_ack_o = wb_ack;
6162
 
6163 17 unneback
endmodule
6164 40 unneback
`endif
6165 32 unneback
 
6166 106 unneback
`ifdef WB_DPRAM
6167
`define MODULE wb_dpram
6168 40 unneback
module `BASE`MODULE (
6169
`undef MODULE
6170 32 unneback
        // wishbone slave side a
6171 106 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, wbsa_stall_o,
6172 32 unneback
        wbsa_clk, wbsa_rst,
6173 92 unneback
        // wishbone slave side b
6174 106 unneback
        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, wbsb_stall_o,
6175 32 unneback
        wbsb_clk, wbsb_rst);
6176
 
6177 92 unneback
parameter data_width_a = 32;
6178
parameter data_width_b = data_width_a;
6179
parameter addr_width_a = 8;
6180
localparam addr_width_b = data_width_a * addr_width_a / data_width_b;
6181 101 unneback
parameter mem_size = (addr_width_a>addr_width_b) ? (1<<addr_width_a) : (1<<addr_width_b);
6182 92 unneback
parameter max_burst_width_a = 4;
6183
parameter max_burst_width_b = max_burst_width_a;
6184 101 unneback
parameter mode = "B3";
6185 109 unneback
parameter memory_init = 0;
6186
parameter memory_file = "vl_ram.v";
6187 92 unneback
input [data_width_a-1:0] wbsa_dat_i;
6188
input [addr_width_a-1:0] wbsa_adr_i;
6189
input [data_width_a/8-1:0] wbsa_sel_i;
6190
input [2:0] wbsa_cti_i;
6191
input [1:0] wbsa_bte_i;
6192 32 unneback
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
6193 92 unneback
output [data_width_a-1:0] wbsa_dat_o;
6194 109 unneback
output wbsa_ack_o;
6195 106 unneback
output wbsa_stall_o;
6196 32 unneback
input wbsa_clk, wbsa_rst;
6197
 
6198 92 unneback
input [data_width_b-1:0] wbsb_dat_i;
6199
input [addr_width_b-1:0] wbsb_adr_i;
6200
input [data_width_b/8-1:0] wbsb_sel_i;
6201
input [2:0] wbsb_cti_i;
6202
input [1:0] wbsb_bte_i;
6203 32 unneback
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
6204 92 unneback
output [data_width_b-1:0] wbsb_dat_o;
6205 109 unneback
output wbsb_ack_o;
6206 106 unneback
output wbsb_stall_o;
6207 32 unneback
input wbsb_clk, wbsb_rst;
6208
 
6209 92 unneback
wire [addr_width_a-1:0] adr_a;
6210
wire [addr_width_b-1:0] adr_b;
6211 101 unneback
wire we_a, we_b;
6212
generate
6213
if (mode=="B3") begin : b3_inst
6214 92 unneback
`define MODULE wb_adr_inc
6215
`BASE`MODULE # ( .adr_width(addr_width_a), .max_burst_width(max_burst_width_a)) adr_inc0 (
6216
    .cyc_i(wbsa_cyc_i),
6217
    .stb_i(wbsa_stb_i),
6218
    .cti_i(wbsa_cti_i),
6219
    .bte_i(wbsa_bte_i),
6220
    .adr_i(wbsa_adr_i),
6221
    .we_i(wbsa_we_i),
6222
    .ack_o(wbsa_ack_o),
6223
    .adr_o(adr_a),
6224
    .clk(wbsa_clk),
6225
    .rst(wbsa_rst));
6226 101 unneback
assign we_a = wbsa_we_i & wbsa_ack_o;
6227 92 unneback
`BASE`MODULE # ( .adr_width(addr_width_b), .max_burst_width(max_burst_width_b)) adr_inc1 (
6228
    .cyc_i(wbsb_cyc_i),
6229
    .stb_i(wbsb_stb_i),
6230
    .cti_i(wbsb_cti_i),
6231
    .bte_i(wbsb_bte_i),
6232
    .adr_i(wbsb_adr_i),
6233
    .we_i(wbsb_we_i),
6234
    .ack_o(wbsb_ack_o),
6235
    .adr_o(adr_b),
6236
    .clk(wbsb_clk),
6237
    .rst(wbsb_rst));
6238 40 unneback
`undef MODULE
6239 101 unneback
assign we_b = wbsb_we_i & wbsb_ack_o;
6240
end else if (mode=="B4") begin : b4_inst
6241 109 unneback
`define MODULE dff
6242
`BASE`MODULE dffacka ( .d(wbsa_stb_i & wbsa_cyc_i), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
6243 101 unneback
assign wbsa_stall_o = 1'b0;
6244
assign we_a = wbsa_we_i & wbsa_cyc_i & wbsa_stb_i;
6245 109 unneback
`BASE`MODULE dffackb ( .d(wbsb_stb_i & wbsb_cyc_i), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
6246
`undef MODULE
6247 101 unneback
assign wbsb_stall_o = 1'b0;
6248
assign we_b = wbsb_we_i & wbsb_cyc_i & wbsb_stb_i;
6249
end
6250
endgenerate
6251 92 unneback
 
6252
`define MODULE dpram_be_2r2w
6253 109 unneback
`BASE`MODULE # ( .a_data_width(data_width_a), .a_addr_width(addr_width_a), .mem_size(mem_size),
6254 110 unneback
                 .b_data_width(data_width_b),
6255 109 unneback
                 .memory_init(memory_init), .memory_file(memory_file))
6256 92 unneback
`undef MODULE
6257
ram_i (
6258 32 unneback
    .d_a(wbsa_dat_i),
6259 92 unneback
    .q_a(wbsa_dat_o),
6260
    .adr_a(adr_a),
6261
    .be_a(wbsa_sel_i),
6262 101 unneback
    .we_a(we_a),
6263 32 unneback
    .clk_a(wbsa_clk),
6264
    .d_b(wbsb_dat_i),
6265 92 unneback
    .q_b(wbsb_dat_o),
6266
    .adr_b(adr_b),
6267
    .be_b(wbsb_sel_i),
6268 101 unneback
    .we_b(we_b),
6269 32 unneback
    .clk_b(wbsb_clk) );
6270
 
6271
endmodule
6272 40 unneback
`endif
6273 94 unneback
 
6274 101 unneback
`ifdef WB_CACHE
6275
`define MODULE wb_cache
6276 97 unneback
module `BASE`MODULE (
6277 103 unneback
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i, wbs_dat_o, wbs_ack_o, wbs_stall_o, wbs_clk, wbs_rst,
6278 98 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, wbm_dat_i, wbm_ack_i, wbm_stall_i, wbm_clk, wbm_rst
6279 97 unneback
);
6280
`undef MODULE
6281
 
6282
parameter dw_s = 32;
6283
parameter aw_s = 24;
6284
parameter dw_m = dw_s;
6285 124 unneback
//localparam aw_m = dw_s * aw_s / dw_m;
6286
localparam aw_m =
6287 126 unneback
        (dw_s==dw_m) ? aw_s :
6288
        (dw_s==dw_m*2) ? aw_s+1 :
6289
        (dw_s==dw_m*4) ? aw_s+2 :
6290
        (dw_s==dw_m*8) ? aw_s+3 :
6291
        (dw_s==dw_m*16) ? aw_s+4 :
6292
        (dw_s==dw_m*32) ? aw_s+5 :
6293
        (dw_s==dw_m/2) ? aw_s-1 :
6294 127 unneback
        (dw_s==dw_m/4) ? aw_s-2 :
6295 126 unneback
        (dw_s==dw_m/8) ? aw_s-3 :
6296
        (dw_s==dw_m/16) ? aw_s-4 :
6297
        (dw_s==dw_m/32) ? aw_s-5 : 0;
6298 124 unneback
 
6299 100 unneback
parameter wbs_max_burst_width = 4;
6300 103 unneback
parameter wbs_mode = "B3";
6301 97 unneback
 
6302
parameter async = 1; // wbs_clk != wbm_clk
6303
 
6304
parameter nr_of_ways = 1;
6305
parameter aw_offset = 4; // 4 => 16 words per cache line
6306
parameter aw_slot = 10;
6307 100 unneback
 
6308
parameter valid_mem = 0;
6309
parameter debug = 0;
6310
 
6311
localparam aw_b_offset = aw_offset * dw_s / dw_m;
6312 98 unneback
localparam aw_tag = aw_s - aw_slot - aw_offset;
6313 97 unneback
parameter wbm_burst_size = 4; // valid options 4,8,16
6314 98 unneback
localparam bte = (wbm_burst_size==4) ? 2'b01 : (wbm_burst_size==8) ? 2'b10 : 2'b11;
6315 97 unneback
`define SIZE2WIDTH wbm_burst_size
6316
localparam wbm_burst_width `SIZE2WIDTH_EXPR
6317
`undef SIZE2WIDTH
6318
localparam nr_of_wbm_burst = ((1<<aw_offset)/wbm_burst_size) * dw_s / dw_m;
6319
`define SIZE2WIDTH nr_of_wbm_burst
6320
localparam nr_of_wbm_burst_width `SIZE2WIDTH_EXPR
6321
`undef SIZE2WIDTH
6322 100 unneback
 
6323 97 unneback
input [dw_s-1:0] wbs_dat_i;
6324
input [aw_s-1:0] wbs_adr_i; // dont include a1,a0
6325 98 unneback
input [dw_s/8-1:0] wbs_sel_i;
6326 97 unneback
input [2:0] wbs_cti_i;
6327
input [1:0] wbs_bte_i;
6328 98 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
6329 97 unneback
output [dw_s-1:0] wbs_dat_o;
6330
output wbs_ack_o;
6331 103 unneback
output wbs_stall_o;
6332 97 unneback
input wbs_clk, wbs_rst;
6333
 
6334
output [dw_m-1:0] wbm_dat_o;
6335
output [aw_m-1:0] wbm_adr_o;
6336
output [dw_m/8-1:0] wbm_sel_o;
6337
output [2:0] wbm_cti_o;
6338
output [1:0] wbm_bte_o;
6339 98 unneback
output wbm_stb_o, wbm_cyc_o, wbm_we_o;
6340 97 unneback
input [dw_m-1:0] wbm_dat_i;
6341
input wbm_ack_i;
6342
input wbm_stall_i;
6343
input wbm_clk, wbm_rst;
6344
 
6345 100 unneback
wire valid, dirty, hit;
6346 97 unneback
wire [aw_tag-1:0] tag;
6347
wire tag_mem_we;
6348
wire [aw_tag-1:0] wbs_adr_tag;
6349
wire [aw_slot-1:0] wbs_adr_slot;
6350 98 unneback
wire [aw_offset-1:0] wbs_adr_word;
6351
wire [aw_s-1:0] wbs_adr;
6352 97 unneback
 
6353
reg [1:0] state;
6354
localparam idle = 2'h0;
6355
localparam rdwr = 2'h1;
6356
localparam push = 2'h2;
6357
localparam pull = 2'h3;
6358
wire eoc;
6359 103 unneback
wire we;
6360 97 unneback
 
6361
// cdc
6362
wire done, mem_alert, mem_done;
6363
 
6364 98 unneback
// wbm side
6365
reg [aw_m-1:0] wbm_radr;
6366
reg [aw_m-1:0] wbm_wadr;
6367 137 unneback
//wire [aw_slot-1:0] wbm_adr;
6368
wire [aw_m-1:0] wbm_adr;
6369 98 unneback
wire wbm_radr_cke, wbm_wadr_cke;
6370
 
6371 100 unneback
reg [2:0] phase;
6372
// phase = {we,stb,cyc}
6373
localparam wbm_wait     = 3'b000;
6374
localparam wbm_wr       = 3'b111;
6375
localparam wbm_wr_drain = 3'b101;
6376
localparam wbm_rd       = 3'b011;
6377
localparam wbm_rd_drain = 3'b001;
6378 98 unneback
 
6379 97 unneback
assign {wbs_adr_tag, wbs_adr_slot, wbs_adr_word} = wbs_adr_i;
6380
 
6381 100 unneback
generate
6382
if (valid_mem==0) begin : no_valid_mem
6383
assign valid = 1'b1;
6384
end else begin : valid_mem_inst
6385
`define MODULE dpram_1r1w
6386 97 unneback
`BASE`MODULE
6387 100 unneback
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
6388
    valid_mem ( .d_a(1'b1), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
6389
                .q_b(valid), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
6390 97 unneback
`undef MODULE
6391 100 unneback
end
6392
endgenerate
6393 97 unneback
 
6394 100 unneback
`define MODULE dpram_1r1w
6395
`BASE`MODULE
6396
    # ( .data_width(aw_tag), .addr_width(aw_slot), .memory_init(2), .debug(debug))
6397
    tag_mem ( .d_a(wbs_adr_tag), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
6398
              .q_b(tag), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
6399
assign hit = wbs_adr_tag == tag;
6400
`undef MODULE
6401
 
6402
`define MODULE dpram_1r2w
6403
`BASE`MODULE
6404
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
6405
    dirty_mem (
6406
        .d_a(1'b1), .q_a(dirty), .adr_a(wbs_adr_slot), .we_a(wbs_cyc_i & wbs_we_i & wbs_ack_o), .clk_a(wbs_clk),
6407
        .d_b(1'b0), .adr_b(wbs_adr_slot), .we_b(mem_done), .clk_b(wbm_clk));
6408
`undef MODULE
6409
 
6410 103 unneback
generate
6411
if (wbs_mode=="B3") begin : inst_b3
6412 97 unneback
`define MODULE wb_adr_inc
6413 100 unneback
`BASE`MODULE # ( .adr_width(aw_s), .max_burst_width(wbs_max_burst_width)) adr_inc0 (
6414
    .cyc_i(wbs_cyc_i & (state==rdwr) & hit & valid),
6415
    .stb_i(wbs_stb_i & (state==rdwr) & hit & valid), // throttle depending on valid
6416 97 unneback
    .cti_i(wbs_cti_i),
6417
    .bte_i(wbs_bte_i),
6418
    .adr_i(wbs_adr_i),
6419
    .we_i (wbs_we_i),
6420
    .ack_o(wbs_ack_o),
6421
    .adr_o(wbs_adr),
6422 100 unneback
    .clk(wbs_clk),
6423
    .rst(wbs_rst));
6424 97 unneback
`undef MODULE
6425 103 unneback
assign eoc = (wbs_cti_i==3'b000 | wbs_cti_i==3'b111) & wbs_ack_o;
6426
assign we = wbs_cyc_i &  wbs_we_i & wbs_ack_o;
6427
end else if (wbs_mode=="B4") begin : inst_b4
6428
end
6429 97 unneback
 
6430 103 unneback
endgenerate
6431 131 unneback
localparam cache_mem_b_aw =
6432
    (dw_s==dw_m) ? aw_slot+aw_offset :
6433 133 unneback
    (dw_s==dw_m/2) ? aw_slot+aw_offset-1 :
6434
    (dw_s==dw_m/4) ? aw_slot+aw_offset-2 :
6435
    (dw_s==dw_m/8) ? aw_slot+aw_offset-3 :
6436
    (dw_s==dw_m/16) ? aw_slot+aw_offset-4 :
6437
    (dw_s==dw_m*2) ? aw_slot+aw_offset+1 :
6438
    (dw_s==dw_m*4) ? aw_slot+aw_offset+2 :
6439
    (dw_s==dw_m*8) ? aw_slot+aw_offset+3 :
6440
    (dw_s==dw_m*16) ? aw_slot+aw_offset+4 : 0;
6441 103 unneback
 
6442 97 unneback
`define MODULE dpram_be_2r2w
6443
`BASE`MODULE
6444 100 unneback
    # ( .a_data_width(dw_s), .a_addr_width(aw_slot+aw_offset), .b_data_width(dw_m), .debug(debug))
6445 103 unneback
    cache_mem ( .d_a(wbs_dat_i), .adr_a(wbs_adr[aw_slot+aw_offset-1:0]),   .be_a(wbs_sel_i), .we_a(we), .q_a(wbs_dat_o), .clk_a(wbs_clk),
6446 136 unneback
                .d_b(wbm_dat_i), .adr_b(wbm_adr[cache_mem_b_aw-1:0]), .be_b(wbm_sel_o), .we_b(wbm_cyc_o & !wbm_we_o & wbm_ack_i), .q_b(wbm_dat_o), .clk_b(wbm_clk));
6447 97 unneback
`undef MODULE
6448
 
6449
always @ (posedge wbs_clk or posedge wbs_rst)
6450
if (wbs_rst)
6451 98 unneback
    state <= idle;
6452 97 unneback
else
6453
    case (state)
6454
    idle:
6455
        if (wbs_cyc_i)
6456
            state <= rdwr;
6457
    rdwr:
6458 100 unneback
        casex ({valid, hit, dirty, eoc})
6459
        4'b0xxx: state <= pull;
6460
        4'b11x1: state <= idle;
6461
        4'b101x: state <= push;
6462
        4'b100x: state <= pull;
6463
        endcase
6464 97 unneback
    push:
6465
        if (done)
6466
            state <= rdwr;
6467
    pull:
6468
        if (done)
6469
            state <= rdwr;
6470
    default: state <= idle;
6471
    endcase
6472
 
6473
// cdc
6474
generate
6475
if (async==1) begin : cdc0
6476
`define MODULE cdc
6477 100 unneback
`BASE`MODULE cdc0 ( .start_pl(state==rdwr & (!valid | !hit)), .take_it_pl(mem_alert), .take_it_grant_pl(mem_done), .got_it_pl(done), .clk_src(wbs_clk), .rst_src(wbs_rst), .clk_dst(wbm_clk), .rst_dst(wbm_rst));
6478 97 unneback
`undef MODULE
6479
end
6480
else begin : nocdc
6481 100 unneback
    assign mem_alert = state==rdwr & (!valid | !hit);
6482 97 unneback
    assign done = mem_done;
6483
end
6484
endgenerate
6485
 
6486 136 unneback
// FSM generating a number of bursts 4 cycles
6487 97 unneback
// actual number depends on data width ratio
6488
// nr_of_wbm_burst
6489 101 unneback
reg [nr_of_wbm_burst_width+wbm_burst_width-1:0]       cnt_rw, cnt_ack;
6490 97 unneback
 
6491
always @ (posedge wbm_clk or posedge wbm_rst)
6492
if (wbm_rst)
6493 100 unneback
    cnt_rw <= {wbm_burst_width{1'b0}};
6494 97 unneback
else
6495 100 unneback
    if (wbm_cyc_o & wbm_stb_o & !wbm_stall_i)
6496
        cnt_rw <= cnt_rw + 1;
6497 97 unneback
 
6498 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
6499
if (wbm_rst)
6500 100 unneback
    cnt_ack <= {wbm_burst_width{1'b0}};
6501 98 unneback
else
6502 100 unneback
    if (wbm_ack_i)
6503
        cnt_ack <= cnt_ack + 1;
6504 97 unneback
 
6505 100 unneback
generate
6506 101 unneback
if (nr_of_wbm_burst==1) begin : one_burst
6507 100 unneback
 
6508 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
6509
if (wbm_rst)
6510
    phase <= wbm_wait;
6511
else
6512
    case (phase)
6513
    wbm_wait:
6514
        if (mem_alert)
6515 100 unneback
            if (state==push)
6516
                phase <= wbm_wr;
6517
            else
6518
                phase <= wbm_rd;
6519 98 unneback
    wbm_wr:
6520 100 unneback
        if (&cnt_rw)
6521
            phase <= wbm_wr_drain;
6522
    wbm_wr_drain:
6523
        if (&cnt_ack)
6524 98 unneback
            phase <= wbm_rd;
6525
    wbm_rd:
6526 100 unneback
        if (&cnt_rw)
6527
            phase <= wbm_rd_drain;
6528
    wbm_rd_drain:
6529
        if (&cnt_ack)
6530
            phase <= wbm_wait;
6531 98 unneback
    default: phase <= wbm_wait;
6532
    endcase
6533
 
6534 100 unneback
end else begin : multiple_burst
6535
 
6536 101 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
6537
if (wbm_rst)
6538
    phase <= wbm_wait;
6539
else
6540
    case (phase)
6541
    wbm_wait:
6542
        if (mem_alert)
6543
            if (state==push)
6544
                phase <= wbm_wr;
6545
            else
6546
                phase <= wbm_rd;
6547
    wbm_wr:
6548
        if (&cnt_rw[wbm_burst_width-1:0])
6549
            phase <= wbm_wr_drain;
6550
    wbm_wr_drain:
6551
        if (&cnt_ack)
6552
            phase <= wbm_rd;
6553
        else if (&cnt_ack[wbm_burst_width-1:0])
6554
            phase <= wbm_wr;
6555
    wbm_rd:
6556
        if (&cnt_rw[wbm_burst_width-1:0])
6557
            phase <= wbm_rd_drain;
6558
    wbm_rd_drain:
6559
        if (&cnt_ack)
6560
            phase <= wbm_wait;
6561
        else if (&cnt_ack[wbm_burst_width-1:0])
6562
            phase <= wbm_rd;
6563
    default: phase <= wbm_wait;
6564
    endcase
6565 100 unneback
 
6566 101 unneback
 
6567 100 unneback
end
6568
endgenerate
6569
 
6570 101 unneback
assign mem_done = phase==wbm_rd_drain & (&cnt_ack) & wbm_ack_i;
6571 100 unneback
 
6572
assign wbm_adr_o = (phase[2]) ? {tag, wbs_adr_slot, cnt_rw} : {wbs_adr_tag, wbs_adr_slot, cnt_rw};
6573 137 unneback
assign wbm_adr   = (phase[2]) ? {wbs_adr_slot, cnt_rw} : {wbs_adr_slot, cnt_ack};
6574 100 unneback
assign wbm_sel_o = {dw_m/8{1'b1}};
6575
assign wbm_cti_o = (&cnt_rw | !wbm_stb_o) ? 3'b111 : 3'b010;
6576 98 unneback
assign wbm_bte_o = bte;
6577 100 unneback
assign {wbm_we_o, wbm_stb_o, wbm_cyc_o}  = phase;
6578 98 unneback
 
6579 97 unneback
endmodule
6580
`endif
6581 103 unneback
 
6582
`ifdef WB_AVALON_BRIDGE
6583
// Wishbone to avalon bridge supporting one type of burst transfer only
6584
// intended use is together with cache above
6585
// WB B4 -> pipelined avalon
6586
`define MODULE wb_avalon_bridge
6587
module `BASE`MODULE (
6588
`undef MODULE
6589
        // wishbone slave side
6590
        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_stall_o,
6591
        // avalon master side
6592
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer,
6593 136 unneback
        init_done,
6594 103 unneback
        // common
6595
        clk, rst);
6596
 
6597
parameter adr_width = 30;
6598
parameter dat_width = 32;
6599
parameter burst_size = 4;
6600
 
6601
input [dat_width-1:0] wbs_dat_i;
6602
input [adr_width-1:0] wbs_adr_i;
6603
input [dat_width/8-1:0]  wbs_sel_i;
6604
input [1:0]  wbs_bte_i;
6605
input [2:0]  wbs_cti_i;
6606
input wbs_we_i;
6607
input wbs_cyc_i;
6608
input wbs_stb_i;
6609 130 unneback
output [dat_width-1:0] wbs_dat_o;
6610 103 unneback
output wbs_ack_o;
6611
output wbs_stall_o;
6612
 
6613
input [dat_width-1:0] readdata;
6614
input readdatavalid;
6615
output [dat_width-1:0] writedata;
6616
output [adr_width-1:0] address;
6617
output [dat_width/8-1:0]  be;
6618
output write;
6619
output read;
6620
output beginbursttransfer;
6621
output [3:0] burstcount;
6622
input waitrequest;
6623 136 unneback
input init_done;
6624 103 unneback
input clk, rst;
6625
 
6626 136 unneback
// cnt1 - initiated read or writes
6627
// cnt2 - # of read or writes in pipeline
6628
reg [3:0] cnt1;
6629
reg [3:0] cnt2;
6630 103 unneback
 
6631 136 unneback
reg next_state, state;
6632
localparam s0 = 1'b0;
6633
localparam s1 = 1'b1;
6634
 
6635
wire eoc;
6636
 
6637
always @ *
6638
begin
6639
    case (state)
6640
    s0: if (init_done & wbs_cyc_i) next_state <= s1;
6641
    s1:
6642
    default: next_state <= state;
6643
    end
6644
end
6645
 
6646 103 unneback
always @ (posedge clk or posedge rst)
6647
if (rst)
6648 136 unneback
    state <= s0;
6649 103 unneback
else
6650 136 unneback
    state <= next_state;
6651 103 unneback
 
6652 136 unneback
assign eoc = state==s1 & !(read | write) & (& !waitrequest & cnt2=;
6653
always @ (posedge clk or posedge rst)
6654
if (rst)
6655
    cnt1 <= 4'h0;
6656
else
6657
    if (read & !waitrequest & init_done)
6658
        cnt1 <= burst_size - 1;
6659
    else if (write & !waitrequest & init_done)
6660
        cnt1 <= cnt1 + 4'h1;
6661
    else if (next_state==idle)
6662
        cnt1 <= 4'h0;
6663
 
6664
always @ (posedge clk or posedge rst)
6665
if (rst)
6666
    cnt2 <= 4'h0;
6667
else
6668
    if (read & !waitrequest & init_done)
6669
        cnt2 <= burst_size - 1;
6670
    else if (write & !waitrequest & init_done & )
6671
        cnt2 <= cnt1 + 4'h1;
6672
    else if (next_state==idle)
6673
        cnt2 <= 4'h0;
6674
 
6675 103 unneback
reg wr_ack;
6676
always @ (posedge clk or posedge rst)
6677
if (rst)
6678
    wr_ack <= 1'b0;
6679
else
6680
    wr_ack <=  (wbs_we_i & wbs_cyc_i & wbs_stb_i & !wbs_stall_o);
6681
 
6682
// to avalon
6683
assign writedata = wbs_dat_i;
6684
assign address = wbs_adr_i;
6685
assign be = wbs_sel_i;
6686 136 unneback
assign write = cnt!=4'h0 & wbs_cyc_i &  wbs_we_i;
6687
assign read  = cnt!=4'h0 & wbs_cyc_i & !wbs_we_i;
6688
assign beginbursttransfer = state==s0 & next_state==s1;
6689 103 unneback
assign burstcount = burst_size;
6690
 
6691
// to wishbone
6692
assign wbs_dat_o = readdata;
6693
assign wbs_ack_o = wr_ack | readdatavalid;
6694
assign wbs_stall_o = waitrequest;
6695
 
6696
endmodule
6697
`endif
6698
 
6699
`ifdef WB_AVALON_MEM_CACHE
6700
`define MODULE wb_avalon_mem_cache
6701
module `BASE`MODULE (
6702
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i, wbs_dat_o, wbs_ack_o, wbs_stall_o, wbs_clk, wbs_rst,
6703
    readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst
6704
);
6705
`undef MODULE
6706
 
6707
// wishbone
6708
parameter wb_dat_width = 32;
6709
parameter wb_adr_width = 22;
6710
parameter wb_max_burst_width = 4;
6711
parameter wb_mode = "B4";
6712
// avalon
6713
parameter avalon_dat_width = 32;
6714 121 unneback
//localparam avalon_adr_width = wb_dat_width * wb_adr_width / avalon_dat_width;
6715 122 unneback
localparam avalon_adr_width =
6716
        (wb_dat_width==avalon_dat_width) ? wb_adr_width :
6717
        (wb_dat_width==avalon_dat_width*2) ? wb_adr_width+1 :
6718
        (wb_dat_width==avalon_dat_width*4) ? wb_adr_width+2 :
6719
        (wb_dat_width==avalon_dat_width*8) ? wb_adr_width+3 :
6720
        (wb_dat_width==avalon_dat_width*16) ? wb_adr_width+4 :
6721
        (wb_dat_width==avalon_dat_width*32) ? wb_adr_width+5 :
6722
        (wb_dat_width==avalon_dat_width/2) ? wb_adr_width-1 :
6723
        (wb_dat_width==avalon_dat_width/4) ? wb_adr_width-2 :
6724
        (wb_dat_width==avalon_dat_width/8) ? wb_adr_width-3 :
6725
        (wb_dat_width==avalon_dat_width/16) ? wb_adr_width-4 :
6726 123 unneback
        (wb_dat_width==avalon_dat_width/32) ? wb_adr_width-5 : 0;
6727 103 unneback
parameter avalon_burst_size = 4;
6728
// cache
6729
parameter async = 1;
6730
parameter nr_of_ways = 1;
6731
parameter aw_offset = 4;
6732
parameter aw_slot = 10;
6733
parameter valid_mem = 1;
6734
// shadow RAM
6735
parameter shadow_ram = 0;
6736
parameter shadow_ram_adr_width = 10;
6737
parameter shadow_ram_size = 1024;
6738
parameter shadow_ram_init = 2; // 0: no init, 1: from file, 2: with zero
6739
parameter shadow_ram_file = "vl_ram.v";
6740
 
6741
input [wb_dat_width-1:0] wbs_dat_i;
6742
input [wb_adr_width-1:0] wbs_adr_i; // dont include a1,a0
6743
input [wb_dat_width/8-1:0] wbs_sel_i;
6744
input [2:0] wbs_cti_i;
6745
input [1:0] wbs_bte_i;
6746
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
6747
output [wb_dat_width-1:0] wbs_dat_o;
6748
output wbs_ack_o;
6749
output wbs_stall_o;
6750
input wbs_clk, wbs_rst;
6751
 
6752
input [avalon_dat_width-1:0] readdata;
6753
input readdatavalid;
6754
output [avalon_dat_width-1:0] writedata;
6755
output [avalon_adr_width-1:0] address;
6756
output [avalon_dat_width/8-1:0]  be;
6757
output write;
6758
output read;
6759
output beginbursttransfer;
6760
output [3:0] burstcount;
6761
input waitrequest;
6762
input clk, rst;
6763
 
6764
`define DAT_WIDTH wb_dat_width
6765
`define ADR_WIDTH wb_adr_width
6766
`define WB wb1
6767
`include "wb_wires.v"
6768 129 unneback
`undef DAT_WIDTH
6769
`undef ADR_WIDTH
6770
`define DAT_WIDTH avalon_dat_width
6771
`define ADR_WIDTH avalon_adr_width
6772 103 unneback
`define WB wb2
6773
`include "wb_wires.v"
6774
`undef DAT_WIDTH
6775
`undef ADR_WIDTH
6776
 
6777
`define MODULE wb_shadow_ram
6778
`BASE`MODULE # ( .dat_width(wb_dat_width), .mode(wb_mode), .max_burst_width(wb_max_burst_width),
6779 120 unneback
                 .shadow_mem_adr_width(shadow_ram_adr_width), .shadow_mem_size(shadow_ram_size), .shadow_mem_init(shadow_ram_init), .shadow_mem_file(shadow_ram_file),
6780 103 unneback
                 .main_mem_adr_width(wb_adr_width))
6781
shadow_ram0 (
6782
    .wbs_dat_i(wbs_dat_i), .wbs_adr_i(wbs_adr_i), .wbs_cti_i(wbs_cti_i), .wbs_bte_i(wbs_bte_i), .wbs_sel_i(wbs_sel_i), .wbs_we_i(wbs_we_i), .wbs_stb_i(wbs_stb_i), .wbs_cyc_i(wbs_cyc_i),
6783
    .wbs_dat_o(wbs_dat_o), .wbs_ack_o(wbs_ack_o), .wbs_stall_o(wbs_stall_o),
6784
    .wbm_dat_o(wb1_dat_o), .wbm_adr_o(wb1_adr_o), .wbm_cti_o(wb1_cti_o), .wbm_bte_o(wb1_bte_o), .wbm_sel_o(wb1_sel_o), .wbm_we_o(wb1_we_o), .wbm_stb_o(wb1_stb_o), .wbm_cyc_o(wb1_cyc_o),
6785
    .wbm_dat_i(wb1_dat_i), .wbm_ack_i(wb1_ack_i), .wbm_stall_i(wb1_stall_i),
6786
    .wb_clk(wbs_clk), .wb_rst(wbs_rst));
6787
`undef MODULE
6788
 
6789
`define MODULE wb_cache
6790
`BASE`MODULE
6791
# ( .dw_s(wb_dat_width), .aw_s(wb_adr_width), .dw_m(avalon_dat_width), .wbs_mode(wb_mode), .wbs_max_burst_width(wb_max_burst_width), .async(async), .nr_of_ways(nr_of_ways), .aw_offset(aw_offset), .aw_slot(aw_slot), .valid_mem(valid_mem))
6792
cache0 (
6793
    .wbs_dat_i(wb1_dat_o), .wbs_adr_i(wb1_adr_o), .wbs_sel_i(wb1_sel_o), .wbs_cti_i(wb1_cti_o), .wbs_bte_i(wb1_bte_o), .wbs_we_i(wb1_we_o), .wbs_stb_i(wb1_stb_o), .wbs_cyc_i(wb1_cyc_o),
6794
    .wbs_dat_o(wb1_dat_i), .wbs_ack_o(wb1_ack_i), .wbs_stall_o(wb1_stall_i), .wbs_clk(wbs_clk), .wbs_rst(wbs_rst),
6795
    .wbm_dat_o(wb2_dat_o), .wbm_adr_o(wb2_adr_o), .wbm_sel_o(wb2_sel_o), .wbm_cti_o(wb2_cti_o), .wbm_bte_o(wb2_bte_o), .wbm_we_o(wb2_we_o), .wbm_stb_o(wb2_stb_o), .wbm_cyc_o(wb2_cyc_o),
6796
    .wbm_dat_i(wb2_dat_i), .wbm_ack_i(wb2_ack_i), .wbm_stall_i(wb2_stall_i), .wbm_clk(clk), .wbm_rst(rst));
6797
`undef MODULE
6798
 
6799
`define MODULE wb_avalon_bridge
6800
`BASE`MODULE # ( .adr_width(avalon_adr_width), .dat_width(avalon_dat_width), .burst_size(avalon_burst_size))
6801
bridge0 (
6802
        // wishbone slave side
6803
        .wbs_dat_i(wb2_dat_o), .wbs_adr_i(wb2_adr_o), .wbs_sel_i(wb2_sel_o), .wbs_bte_i(wb2_bte_o), .wbs_cti_i(wb2_cti_o), .wbs_we_i(wb2_we_o), .wbs_cyc_i(wb2_cyc_o), .wbs_stb_i(wb2_stb_o),
6804
        .wbs_dat_o(wb2_dat_i), .wbs_ack_o(wb2_ack_i), .wbs_stall_o(wb2_stall_i),
6805
        // avalon master side
6806
        .readdata(readdata), .readdatavalid(readdatavalid), .address(address), .read(read), .be(be), .write(write), .burstcount(burstcount), .writedata(writedata), .waitrequest(waitrequest), .beginbursttransfer(beginbursttransfer),
6807
        // common
6808
        .clk(clk), .rst(rst));
6809
`undef MODULE
6810
 
6811
endmodule
6812
`endif
6813 105 unneback
 
6814
`ifdef WB_SDR_SDRAM
6815
`define MODULE wb_sdr_sdram
6816
module `BASE`MODULE (
6817
`undef MODULE
6818
    // wisbone i/f
6819 136 unneback
    dat_i, adr_i, sel_i, we_i, cyc_i, stb_i, dat_o, ack_o, stall_o,
6820 105 unneback
    // SDR SDRAM
6821
    ba, a, cmd, cke, cs_n, dqm, dq_i, dq_o, dq_oe,
6822
    // system
6823
    clk, rst);
6824
 
6825
    // external data bus size
6826
    parameter dat_size = 16;
6827
    // memory geometry parameters
6828 136 unneback
    parameter ba_size  = 2;
6829
    parameter row_size = 13;
6830
    parameter col_size = 9;
6831 105 unneback
    parameter cl = 2;
6832
    // memory timing parameters
6833
    parameter tRFC = 9;
6834
    parameter tRP  = 2;
6835
    parameter tRCD = 2;
6836
    parameter tMRD = 2;
6837
 
6838
    // LMR
6839
    // [12:10] reserved
6840
    // [9]     WB, write burst; 0 - programmed burst length, 1 - single location
6841
    // [8:7]   OP Mode, 2'b00
6842
    // [6:4]   CAS Latency; 3'b010 - 2, 3'b011 - 3
6843
    // [3]     BT, Burst Type; 1'b0 - sequential, 1'b1 - interleaved
6844
    // [2:0]   Burst length; 3'b000 - 1, 3'b001 - 2, 3'b010 - 4, 3'b011 - 8, 3'b111 - full page
6845
    localparam init_wb = 1'b1;
6846
    localparam init_cl = (cl==2) ? 3'b010 : 3'b011;
6847
    localparam init_bt = 1'b0;
6848
    localparam init_bl = 3'b000;
6849
 
6850 136 unneback
    input [dat_size-1:0] dat_i;
6851 105 unneback
    input [ba_size+col_size+row_size-1:0] adr_i;
6852
    input [dat_size/8-1:0] sel_i;
6853
    input we_i, cyc_i, stb_i;
6854
    output [dat_size-1:0] dat_o;
6855
    output ack_o;
6856
    output reg stall_o;
6857
 
6858
    output [ba_size-1:0]    ba;
6859
    output reg [12:0]   a;
6860
    output reg [2:0]    cmd; // {ras,cas,we}
6861
    output cke, cs_n;
6862
    output reg [dat_size/8-1:0]    dqm;
6863
    output [dat_size-1:0]       dq_o;
6864
    output reg          dq_oe;
6865
    input  [dat_size-1:0]       dq_i;
6866
 
6867
    input clk, rst;
6868
 
6869
    wire [ba_size-1:0]   bank;
6870
    wire [row_size-1:0] row;
6871
    wire [col_size-1:0] col;
6872
    wire [0:31]  shreg;
6873
    wire                ref_cnt_zero;
6874
    reg                 refresh_req;
6875
 
6876
    wire ack_rd, rd_ack_emptyflag;
6877
    wire ack_wr;
6878
 
6879
    // to keep track of open rows per bank
6880
    reg [row_size-1:0]   open_row[0:3];
6881
    reg [0:3]            open_ba;
6882
    reg                 current_bank_closed, current_row_open;
6883
 
6884
    parameter rfr_length = 10;
6885
    parameter rfr_wrap_value = 1010;
6886
 
6887
    parameter [2:0] cmd_nop = 3'b111,
6888
                    cmd_act = 3'b011,
6889
                    cmd_rd  = 3'b101,
6890
                    cmd_wr  = 3'b100,
6891
                    cmd_pch = 3'b010,
6892
                    cmd_rfr = 3'b001,
6893
                    cmd_lmr = 3'b000;
6894
 
6895
// ctrl FSM
6896
`define FSM_INIT 3'b000
6897
`define FSM_IDLE 3'b001
6898
`define FSM_RFR  3'b010
6899
`define FSM_ADR  3'b011
6900
`define FSM_PCH  3'b100
6901
`define FSM_ACT  3'b101
6902
`define FSM_RW   3'b111
6903
 
6904
    assign cke = 1'b1;
6905
    assign cs_n = 1'b0;
6906
 
6907
    reg [2:0] state, next;
6908
 
6909
    function [12:0] a10_fix;
6910
        input [col_size-1:0] a;
6911
        integer i;
6912
    begin
6913
        for (i=0;i<13;i=i+1) begin
6914
            if (i<10)
6915
              if (i<col_size)
6916
                a10_fix[i] = a[i];
6917
              else
6918
                a10_fix[i] = 1'b0;
6919
            else if (i==10)
6920
              a10_fix[i] = 1'b0;
6921
            else
6922
              if (i<col_size)
6923
                a10_fix[i] = a[i-1];
6924
              else
6925
                a10_fix[i] = 1'b0;
6926
        end
6927
    end
6928
    endfunction
6929
 
6930
    assign {bank,row,col} = adr_i;
6931
 
6932
    always @ (posedge clk or posedge rst)
6933
    if (rst)
6934
       state <= `FSM_INIT;
6935
    else
6936
       state <= next;
6937
 
6938
    always @*
6939
    begin
6940
        next = state;
6941
        case (state)
6942
        `FSM_INIT:
6943
            if (shreg[3+tRP+tRFC+tRFC+tMRD]) next = `FSM_IDLE;
6944
        `FSM_IDLE:
6945
            if (refresh_req) next = `FSM_RFR;
6946
            else if (cyc_i & stb_i & rd_ack_emptyflag) next = `FSM_ADR;
6947
        `FSM_RFR:
6948
            if (shreg[tRP+tRFC-2]) next = `FSM_IDLE; // take away two cycles because no cmd will be issued in idle and adr
6949
        `FSM_ADR:
6950
            if (current_bank_closed) next = `FSM_ACT;
6951
            else if (current_row_open) next = `FSM_RW;
6952
            else next = `FSM_PCH;
6953
        `FSM_PCH:
6954
            if (shreg[tRP]) next = `FSM_ACT;
6955
        `FSM_ACT:
6956
            if (shreg[tRCD]) next = `FSM_RW;
6957
        `FSM_RW:
6958
            if (!stb_i) next = `FSM_IDLE;
6959
        endcase
6960
    end
6961
 
6962
    // counter
6963 136 unneback
`define MODULE cnt_shreg_clear
6964
    `BASE`MODULE # ( .length(32))
6965 105 unneback
`undef MODULE
6966
        cnt0 (
6967
            .clear(state!=next),
6968
            .q(shreg),
6969
            .rst(rst),
6970
            .clk(clk));
6971
 
6972
    // ba, a, cmd
6973
    // outputs dependent on state vector
6974
    always @ (*)
6975
        begin
6976
            {a,cmd} = {13'd0,cmd_nop};
6977
            dqm = 2'b11;
6978
            dq_oe = 1'b0;
6979
            stall_o = 1'b1;
6980
            case (state)
6981
            `FSM_INIT:
6982
                if (shreg[3]) begin
6983
                    {a,cmd} = {13'b0010000000000, cmd_pch};
6984
                end else if (shreg[3+tRP] | shreg[3+tRP+tRFC])
6985
                    {a,cmd} = {13'd0, cmd_rfr};
6986
                else if (shreg[3+tRP+tRFC+tRFC])
6987
                    {a,cmd} = {3'b000,init_wb,2'b00,init_cl,init_bt,init_bl,cmd_lmr};
6988
            `FSM_RFR:
6989
                if (shreg[0])
6990
                    {a,cmd} = {13'b0010000000000, cmd_pch};
6991
                else if (shreg[tRP])
6992
                    {a,cmd} = {13'd0, cmd_rfr};
6993
            `FSM_PCH:
6994
                if (shreg[0])
6995
                    {a,cmd} = {13'd0,cmd_pch};
6996
            `FSM_ACT:
6997
                if (shreg[0])
6998
                    {a[row_size-1:0],cmd} = {row,cmd_act};
6999
            `FSM_RW:
7000
                begin
7001
                    if (we_i)
7002
                        cmd = cmd_wr;
7003
                    else
7004
                        cmd = cmd_rd;
7005
                    if (we_i)
7006
                        dqm = ~sel_i;
7007
                    else
7008
                        dqm = 2'b00;
7009
                    if (we_i)
7010
                        dq_oe = 1'b1;
7011
                    a = a10_fix(col);
7012 136 unneback
                    stall_o = 1'b0;
7013 105 unneback
                end
7014
            endcase
7015
        end
7016
 
7017
    assign ba = bank;
7018
 
7019
    // precharge individual bank A10=0
7020
    // precharge all bank A10=1
7021
    genvar i;
7022
    generate
7023 136 unneback
    for (i=0;i<2<<ba_size-1;i=i+1) begin : open_ba_logic
7024 105 unneback
 
7025
        always @ (posedge clk or posedge rst)
7026
        if (rst)
7027
            {open_ba[i],open_row[i]} <= {1'b0,{row_size{1'b0}}};
7028
        else
7029
            if (cmd==cmd_pch & (a[10] | bank==i))
7030
                open_ba[i] <= 1'b0;
7031
            else if (cmd==cmd_act & bank==i)
7032
                {open_ba[i],open_row[i]} <= {1'b1,row};
7033
 
7034
    end
7035
    endgenerate
7036
 
7037
    // bank and row open ?
7038
    always @ (posedge clk or posedge rst)
7039
    if (rst)
7040
       {current_bank_closed, current_row_open} <= {1'b1, 1'b0};
7041
    else
7042
       {current_bank_closed, current_row_open} <= {!(open_ba[bank]), open_row[bank]==row};
7043
 
7044
    // refresh counter
7045
`define MODULE cnt_lfsr_zq
7046 136 unneback
    `BASE`MODULE # ( .length(rfr_length), .wrap_value (rfr_wrap_value)) ref_counter0( .zq(ref_cnt_zero), .rst(rst), .clk(clk));
7047 105 unneback
`undef MODULE
7048
 
7049
    always @ (posedge clk or posedge rst)
7050
    if (rst)
7051
        refresh_req <= 1'b0;
7052
    else
7053
        if (ref_cnt_zero)
7054
            refresh_req <= 1'b1;
7055
        else if (state==`FSM_RFR)
7056
            refresh_req <= 1'b0;
7057
 
7058
    assign dat_o = dq_i;
7059
 
7060 136 unneback
    assign ack_wr = (state==`FSM_RW & we_i);
7061 105 unneback
`define MODULE delay_emptyflag
7062 136 unneback
    `BASE`MODULE # ( .depth(cl+2)) delay0 ( .d(state==`FSM_RW & stb_i & !we_i), .q(ack_rd), .emptyflag(rd_ack_emptyflag), .clk(clk), .rst(rst));
7063 105 unneback
`undef MODULE
7064
    assign ack_o = ack_rd | ack_wr;
7065
 
7066
    assign dq_o = dat_i;
7067
 
7068
endmodule
7069
`endif
7070 136 unneback
 
7071
`ifdef WB_SDR_SDRAM_CTRL
7072
`define MODULE wb_sdr_sdram_ctrl
7073
module `BASE`MODULE (
7074
    // WB i/f
7075
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
7076
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
7077
    // SDR SDRAM
7078
    mem_ba, mem_a, mem_cmd, mem_cke, mem_cs_n, mem_dqm, mem_dq_i, mem_dq_o, mem_dq_oe,
7079
    // system
7080
    wb_clk, wb_rst, mem_clk, mem_rst);
7081
`undef MODULE
7082
 
7083
    // WB slave
7084
    parameter wbs_dat_width = 32;
7085
    parameter wbs_adr_width = 24;
7086
    parameter wbs_mode = "B3";
7087
    parameter wbs_max_burst_width = 4;
7088
 
7089
    // Shadow RAM
7090
    parameter shadow_mem_adr_width = 10;
7091
    parameter shadow_mem_size = 1024;
7092
    parameter shadow_mem_init = 2;
7093
    parameter shadow_mem_file = "vl_ram.v";
7094
 
7095
    // Cache
7096
    parameter cache_async = 1; // wbs_clk != wbm_clk
7097
    parameter cache_nr_of_ways = 1;
7098
    parameter cache_aw_offset = 4; // 4 => 16 words per cache line
7099
    parameter cache_aw_slot = 10;
7100
    parameter cache_valid_mem = 0;
7101
    parameter cache_debug = 0;
7102
 
7103
    // SDRAM parameters
7104
    parameter mem_dat_size = 16;
7105
    parameter mem_ba_size  = 2;
7106
    parameter mem_row_size = 13;
7107
    parameter mem_col_size = 9;
7108
    parameter mem_cl = 2;
7109
    parameter mem_tRFC = 9;
7110
    parameter mem_tRP  = 2;
7111
    parameter mem_tRCD = 2;
7112
    parameter mem_tMRD = 2;
7113
    parameter mem_rfr_length = 10;
7114
    parameter mem_rfr_wrap_value = 1010;
7115
 
7116
    input [wbs_dat_width-1:0] wbs_dat_i;
7117
    input [wbs_adr_width-1:0] wbs_adr_i;
7118
    input [2:0] wbs_cti_i;
7119
    input [1:0] wbs_bte_i;
7120
    input [wbs_dat_width/8-1:0] wbs_sel_i;
7121
    input wbs_we_i, wbs_stb_i, wbs_cyc_i;
7122
    output [wbs_dat_width-1:0] wbs_dat_o;
7123
    output wbs_ack_o;
7124
    output wbs_stall_o;
7125
 
7126
    output [mem_ba_size-1:0]    mem_ba;
7127
    output reg [12:0]           mem_a;
7128
    output reg [2:0]            mem_cmd; // {ras,cas,we}
7129
    output                      mem_cke, mem_cs_n;
7130
    output reg [mem_dat_size/8-1:0] mem_dqm;
7131
    output [mem_dat_size-1:0]       mem_dq_o;
7132
    output reg                  mem_dq_oe;
7133
    input  [mem_dat_size-1:0]       mem_dq_i;
7134
 
7135
    input wb_clk, wb_rst, mem_clk, mem_rst;
7136
 
7137
    // wbm1
7138
    wire [wbs_dat_width-1:0] wbm1_dat_o;
7139
    wire [wbs_adr_width-1:0] wbm1_adr_o;
7140
    wire [2:0] wbm1_cti_o;
7141
    wire [1:0] wbm1_bte_o;
7142
    wire [wbs_dat_width/8-1:0] wbm1_sel_o;
7143
    wire wbm1_we_o, wbm1_stb_o, wbm1_cyc_o;
7144
    wire [wbs_dat_width-1:0] wbm1_dat_i;
7145
    wire wbm1_ack_i, wbm1_stall_i;
7146
    // wbm2
7147
    wire [mem_dat_size-1:0] wbm2_dat_o;
7148
    wire [mem_ba_size+mem_row_size+mem_col_size-1:0] wbm2_adr_o;
7149
    wire [2:0] wbm2_cti_o;
7150
    wire [1:0] wbm2_bte_o;
7151
    wire [mem_dat_size/8-1:0] wbm2_sel_o;
7152
    wire wbm2_we_o, wbm2_stb_o, wbm2_cyc_o;
7153
    wire [mem_dat_size-1:0] wbm2_dat_i;
7154
    wire wbm2_ack_i, wbm2_stall_i;
7155
 
7156
`define MODULE wb_shadow_ram
7157
`BASE`MODULE # (
7158
    .shadow_mem_adr_width(shadow_mem_adr_width), .shadow_mem_size(shadow_mem_size), .shadow_mem_init(shadow_mem_init), .shadow_mem_file(shadow_mem_file), .main_mem_adr_width(wbs_adr_width), .dat_width(wbs_dat_width), .mode(wbs_mode), .max_burst_width(wbs_max_burst_width) )
7159
shadow_ram0 (
7160
    .wbs_dat_i(wbs_dat_i),
7161
    .wbs_adr_i(wbs_adr_i),
7162
    .wbs_cti_i(wbs_cti_i),
7163
    .wbs_bte_i(wbs_bte_i),
7164
    .wbs_sel_i(wbs_sel_i),
7165
    .wbs_we_i (wbs_we_i),
7166
    .wbs_stb_i(wbs_stb_i),
7167
    .wbs_cyc_i(wbs_cyc_i),
7168
    .wbs_dat_o(wbs_dat_o),
7169
    .wbs_ack_o(wbs_ack_o),
7170
    .wbs_stall_o(wbs_stall_o),
7171
    .wbm_dat_o(wbm1_dat_o),
7172
    .wbm_adr_o(wbm1_adr_o),
7173
    .wbm_cti_o(wbm1_cti_o),
7174
    .wbm_bte_o(wbm1_bte_o),
7175
    .wbm_sel_o(wbm1_sel_o),
7176
    .wbm_we_o(wbm1_we_o),
7177
    .wbm_stb_o(wbm1_stb_o),
7178
    .wbm_cyc_o(wbm1_cyc_o),
7179
    .wbm_dat_i(wbm1_dat_i),
7180
    .wbm_ack_i(wbm1_ack_i),
7181
    .wbm_stall_i(wbm1_stall_i),
7182
    .wb_clk(wb_clk),
7183
    .wb_rst(wb_rst) );
7184
`undef MODULE
7185
 
7186
`define MODULE wb_cache
7187
`BASE`MODULE # (
7188
    .dw_s(wbs_dat_width), .aw_s(wbs_adr_width), .dw_m(mem_dat_size), .wbs_max_burst_width(cache_aw_offset), .wbs_mode(wbs_mode), .async(cache_async), .nr_of_ways(cache_nr_of_ways), .aw_offset(cache_aw_offset), .aw_slot(cache_aw_slot), .valid_mem(cache_valid_mem) )
7189
cache0 (
7190
    .wbs_dat_i(wbm1_dat_o),
7191
    .wbs_adr_i(wbm1_adr_o),
7192
    .wbs_sel_i(wbm1_sel_o),
7193
    .wbs_cti_i(wbm1_cti_o),
7194
    .wbs_bte_i(wbm1_bte_o),
7195
    .wbs_we_i (wbm1_we_o),
7196
    .wbs_stb_i(wbm1_stb_o),
7197
    .wbs_cyc_i(wbm1_cyc_o),
7198
    .wbs_dat_o(wbm1_dat_i),
7199
    .wbs_ack_o(wbm1_ack_i),
7200
    .wbs_stall_o(wbm1_stall_i),
7201
    .wbs_clk(wb_clk),
7202
    .wbs_rst(wb_rst),
7203
    .wbm_dat_o(wbm2_dat_o),
7204
    .wbm_adr_o(wbm2_adr_o),
7205
    .wbm_sel_o(wbm2_sel_o),
7206
    .wbm_cti_o(wbm2_cti_o),
7207
    .wbm_bte_o(wbm2_bte_o),
7208
    .wbm_we_o (wbm2_we_o),
7209
    .wbm_stb_o(wbm2_stb_o),
7210
    .wbm_cyc_o(wbm2_cyc_o),
7211
    .wbm_dat_i(wbm2_dat_i),
7212
    .wbm_ack_i(wbm2_ack_i),
7213
    .wbm_stall_i(wbm2_stall_i),
7214
    .wbm_clk(mem_clk),
7215
    .wbm_rst(mem_rst) );
7216
`undef MODULE
7217
 
7218
`define MODULE wb_sdr_sdram
7219
`BASE`MODULE # (
7220
    .dat_size(mem_dat_size), .ba_size(mem_ba_size), .row_size(mem_row_size), .col_size(mem_col_size), .cl(mem_cl), .tRFC(mem_tRFC), .tRP(mem_tRP), .tRCD(mem_tRCD), .tMRD(mem_tMRD), .rfr_length(mem_rfr_length), .rfr_wrap_value(mem_rfr_wrap_value) )
7221
ctrl0(
7222
    // wisbone i/f
7223
    .dat_i(wbm2_dat_o),
7224
    .adr_i(wbm2_adr_o),
7225
    .sel_i(wbm2_sel_o),
7226
    .we_i (wbm2_we_o),
7227
    .cyc_i(wbm2_cyc_o),
7228
    .stb_i(wbm2_stb_o),
7229
    .dat_o(wbm2_dat_i),
7230
    .ack_o(wbm2_ack_i),
7231
    .stall_o(wbm2_stall_i),
7232
    // SDR SDRAM
7233
    .ba(mem_ba),
7234
    .a(mem_a),
7235
    .cmd(mem_cmd),
7236
    .cke(mem_cke),
7237
    .cs_n(mem_cs_n),
7238
    .dqm(mem_dqm),
7239
    .dq_i(mem_dq_i),
7240
    .dq_o(mem_dq_o),
7241
    .dq_oe(mem_dq_oe),
7242
    // system
7243
    .clk(mem_clk),
7244
    .rst(mem_rst) );
7245
`undef MODULE
7246
 
7247
endmodule
7248
`endif
7249 18 unneback
//////////////////////////////////////////////////////////////////////
7250
////                                                              ////
7251
////  Arithmetic functions                                        ////
7252
////                                                              ////
7253
////  Description                                                 ////
7254
////  Arithmetic functions for ALU and DSP                        ////
7255
////                                                              ////
7256
////                                                              ////
7257
////  To Do:                                                      ////
7258
////   -                                                          ////
7259
////                                                              ////
7260
////  Author(s):                                                  ////
7261
////      - Michael Unneback, unneback@opencores.org              ////
7262
////        ORSoC AB                                              ////
7263
////                                                              ////
7264
//////////////////////////////////////////////////////////////////////
7265
////                                                              ////
7266
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
7267
////                                                              ////
7268
//// This source file may be used and distributed without         ////
7269
//// restriction provided that this copyright statement is not    ////
7270
//// removed from the file and that any derivative work contains  ////
7271
//// the original copyright notice and the associated disclaimer. ////
7272
////                                                              ////
7273
//// This source file is free software; you can redistribute it   ////
7274
//// and/or modify it under the terms of the GNU Lesser General   ////
7275
//// Public License as published by the Free Software Foundation; ////
7276
//// either version 2.1 of the License, or (at your option) any   ////
7277
//// later version.                                               ////
7278
////                                                              ////
7279
//// This source is distributed in the hope that it will be       ////
7280
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
7281
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
7282
//// PURPOSE.  See the GNU Lesser General Public License for more ////
7283
//// details.                                                     ////
7284
////                                                              ////
7285
//// You should have received a copy of the GNU Lesser General    ////
7286
//// Public License along with this source; if not, download it   ////
7287
//// from http://www.opencores.org/lgpl.shtml                     ////
7288
////                                                              ////
7289
//////////////////////////////////////////////////////////////////////
7290
 
7291 40 unneback
`ifdef MULTS
7292 18 unneback
// signed multiplication
7293 40 unneback
`define MODULE mults
7294
module `BASE`MODULE (a,b,p);
7295
`undef MODULE
7296 18 unneback
parameter operand_a_width = 18;
7297
parameter operand_b_width = 18;
7298
parameter result_hi = 35;
7299
parameter result_lo = 0;
7300
input [operand_a_width-1:0] a;
7301
input [operand_b_width-1:0] b;
7302
output [result_hi:result_lo] p;
7303
wire signed [operand_a_width-1:0] ai;
7304
wire signed [operand_b_width-1:0] bi;
7305
wire signed [operand_a_width+operand_b_width-1:0] result;
7306
 
7307
    assign ai = a;
7308
    assign bi = b;
7309
    assign result = ai * bi;
7310
    assign p = result[result_hi:result_lo];
7311
 
7312
endmodule
7313 40 unneback
`endif
7314
`ifdef MULTS18X18
7315
`define MODULE mults18x18
7316
module `BASE`MODULE (a,b,p);
7317
`undef MODULE
7318 18 unneback
input [17:0] a,b;
7319
output [35:0] p;
7320
vl_mult
7321
    # (.operand_a_width(18), .operand_b_width(18))
7322
    mult0 (.a(a), .b(b), .p(p));
7323
endmodule
7324 40 unneback
`endif
7325 18 unneback
 
7326 40 unneback
`ifdef MULT
7327
`define MODULE mult
7328 18 unneback
// unsigned multiplication
7329 40 unneback
module `BASE`MODULE (a,b,p);
7330
`undef MODULE
7331 18 unneback
parameter operand_a_width = 18;
7332
parameter operand_b_width = 18;
7333
parameter result_hi = 35;
7334
parameter result_lo = 0;
7335
input [operand_a_width-1:0] a;
7336
input [operand_b_width-1:0] b;
7337
output [result_hi:result_hi] p;
7338
 
7339
wire [operand_a_width+operand_b_width-1:0] result;
7340
 
7341
    assign result = a * b;
7342
    assign p = result[result_hi:result_lo];
7343
 
7344
endmodule
7345 40 unneback
`endif
7346 18 unneback
 
7347 40 unneback
`ifdef SHIFT_UNIT_32
7348
`define MODULE shift_unit_32
7349 18 unneback
// shift unit
7350
// supporting the following shift functions
7351
//   SLL
7352
//   SRL
7353
//   SRA
7354
`define SHIFT_UNIT_MULT # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7))
7355 40 unneback
module `BASE`MODULE( din, s, dout, opcode);
7356
`undef MODULE
7357 18 unneback
input [31:0] din; // data in operand
7358
input [4:0] s; // shift operand
7359
input [1:0] opcode;
7360
output [31:0] dout;
7361
 
7362
parameter opcode_sll = 2'b00;
7363
//parameter opcode_srl = 2'b01;
7364
parameter opcode_sra = 2'b10;
7365
//parameter opcode_ror = 2'b11;
7366
 
7367
wire sll, sra;
7368
assign sll = opcode == opcode_sll;
7369
assign sra = opcode == opcode_sra;
7370
 
7371
wire [15:1] s1;
7372
wire [3:0] sign;
7373
wire [7:0] tmp [0:3];
7374
 
7375
// first stage is multiplier based
7376
// shift operand as fractional 8.7
7377
assign s1[15] = sll & s[2:0]==3'd7;
7378
assign s1[14] = sll & s[2:0]==3'd6;
7379
assign s1[13] = sll & s[2:0]==3'd5;
7380
assign s1[12] = sll & s[2:0]==3'd4;
7381
assign s1[11] = sll & s[2:0]==3'd3;
7382
assign s1[10] = sll & s[2:0]==3'd2;
7383
assign s1[ 9] = sll & s[2:0]==3'd1;
7384
assign s1[ 8] = s[2:0]==3'd0;
7385
assign s1[ 7] = !sll & s[2:0]==3'd1;
7386
assign s1[ 6] = !sll & s[2:0]==3'd2;
7387
assign s1[ 5] = !sll & s[2:0]==3'd3;
7388
assign s1[ 4] = !sll & s[2:0]==3'd4;
7389
assign s1[ 3] = !sll & s[2:0]==3'd5;
7390
assign s1[ 2] = !sll & s[2:0]==3'd6;
7391
assign s1[ 1] = !sll & s[2:0]==3'd7;
7392
 
7393
assign sign[3] = din[31] & sra;
7394
assign sign[2] = sign[3] & (&din[31:24]);
7395
assign sign[1] = sign[2] & (&din[23:16]);
7396
assign sign[0] = sign[1] & (&din[15:8]);
7397 40 unneback
`define MODULE mults
7398
`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]));
7399
`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]));
7400
`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]));
7401
`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]));
7402
`undef MODULE
7403 18 unneback
// second stage is multiplexer based
7404
// shift on byte level
7405
 
7406
// mux byte 3
7407
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
7408
                     (sll & s[4:3]==2'b01) ? tmp[2] :
7409
                     (sll & s[4:3]==2'b10) ? tmp[1] :
7410
                     (sll & s[4:3]==2'b11) ? tmp[0] :
7411
                     {8{sign[3]}};
7412
 
7413
// mux byte 2
7414
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
7415
                     (sll & s[4:3]==2'b01) ? tmp[1] :
7416
                     (sll & s[4:3]==2'b10) ? tmp[0] :
7417
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
7418
                     (s[4:3]==2'b01) ? tmp[3] :
7419
                     {8{sign[3]}};
7420
 
7421
// mux byte 1
7422
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
7423
                     (sll & s[4:3]==2'b01) ? tmp[0] :
7424
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
7425
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
7426
                     (s[4:3]==2'b01) ? tmp[2] :
7427
                     (s[4:3]==2'b10) ? tmp[3] :
7428
                     {8{sign[3]}};
7429
 
7430
// mux byte 0
7431
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
7432
                     (sll) ?  {8{1'b0}}:
7433
                     (s[4:3]==2'b01) ? tmp[1] :
7434
                     (s[4:3]==2'b10) ? tmp[2] :
7435
                     tmp[3];
7436
 
7437
endmodule
7438 40 unneback
`endif
7439 18 unneback
 
7440 40 unneback
`ifdef LOGIC_UNIT
7441 18 unneback
// logic unit
7442
// supporting the following logic functions
7443
//    a and b
7444
//    a or  b
7445
//    a xor b
7446
//    not b
7447 40 unneback
`define MODULE logic_unit
7448
module `BASE`MODULE( a, b, result, opcode);
7449
`undef MODULE
7450 18 unneback
parameter width = 32;
7451
parameter opcode_and = 2'b00;
7452
parameter opcode_or  = 2'b01;
7453
parameter opcode_xor = 2'b10;
7454
input [width-1:0] a,b;
7455
output [width-1:0] result;
7456
input [1:0] opcode;
7457
 
7458
assign result = (opcode==opcode_and) ? a & b :
7459
                (opcode==opcode_or)  ? a | b :
7460
                (opcode==opcode_xor) ? a ^ b :
7461
                b;
7462
 
7463
endmodule
7464 48 unneback
`endif
7465 18 unneback
 
7466 48 unneback
`ifdef ARITH_UNIT
7467
`define MODULE arith_unit
7468
module `BASE`MODULE ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
7469
`undef MODULE
7470 18 unneback
parameter width = 32;
7471
parameter opcode_add = 1'b0;
7472
parameter opcode_sub = 1'b1;
7473
input [width-1:0] a,b;
7474
input c_in, add_sub, sign;
7475
output [width-1:0] result;
7476
output c_out, z, ovfl;
7477
 
7478
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))};
7479
assign z = (result=={width{1'b0}});
7480
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
7481
               (~a[width-1] & ~b[width-1] &  result[width-1]);
7482
endmodule
7483 40 unneback
`endif
7484 48 unneback
 
7485
`ifdef COUNT_UNIT
7486
`define MODULE count_unit
7487
module `BASE`MODULE (din, dout, opcode);
7488
`undef MODULE
7489
parameter width = 32;
7490
input [width-1:0] din;
7491
output [width-1:0] dout;
7492
input opcode;
7493
 
7494
integer i;
7495 58 unneback
wire [width/32+4:0] ff1, fl1;
7496 48 unneback
 
7497 57 unneback
/*
7498 48 unneback
always @(din) begin
7499
    ff1 = 0; i = 0;
7500
    while (din[i] == 0 && i < width) begin // complex condition
7501
        ff1 = ff1 + 1;
7502
        i = i + 1;
7503
    end
7504
end
7505
 
7506
always @(din) begin
7507
    fl1 = width; i = width-1;
7508
    while (din[i] == 0 && i >= width) begin // complex condition
7509
        fl1 = fl1 - 1;
7510
        i = i - 1;
7511
    end
7512
end
7513 57 unneback
*/
7514 48 unneback
 
7515
generate
7516
if (width==32) begin
7517 57 unneback
 
7518
    assign ff1 = din[0] ? 6'd1 :
7519
                 din[1] ? 6'd2 :
7520
                 din[2] ? 6'd3 :
7521
                 din[3] ? 6'd4 :
7522
                 din[4] ? 6'd5 :
7523
                 din[5] ? 6'd6 :
7524
                 din[6] ? 6'd7 :
7525
                 din[7] ? 6'd8 :
7526
                 din[8] ? 6'd9 :
7527
                 din[9] ? 6'd10 :
7528
                 din[10] ? 6'd11 :
7529
                 din[11] ? 6'd12 :
7530
                 din[12] ? 6'd13 :
7531
                 din[13] ? 6'd14 :
7532
                 din[14] ? 6'd15 :
7533
                 din[15] ? 6'd16 :
7534
                 din[16] ? 6'd17 :
7535
                 din[17] ? 6'd18 :
7536
                 din[18] ? 6'd19 :
7537
                 din[19] ? 6'd20 :
7538
                 din[20] ? 6'd21 :
7539
                 din[21] ? 6'd22 :
7540
                 din[22] ? 6'd23 :
7541
                 din[23] ? 6'd24 :
7542
                 din[24] ? 6'd25 :
7543
                 din[25] ? 6'd26 :
7544
                 din[26] ? 6'd27 :
7545
                 din[27] ? 6'd28 :
7546
                 din[28] ? 6'd29 :
7547
                 din[29] ? 6'd30 :
7548
                 din[30] ? 6'd31 :
7549
                 din[31] ? 6'd32 :
7550
                 6'd0;
7551
 
7552
    assign fl1 = din[31] ? 6'd32 :
7553
                 din[30] ? 6'd31 :
7554
                 din[29] ? 6'd30 :
7555
                 din[28] ? 6'd29 :
7556
                 din[27] ? 6'd28 :
7557
                 din[26] ? 6'd27 :
7558
                 din[25] ? 6'd26 :
7559
                 din[24] ? 6'd25 :
7560
                 din[23] ? 6'd24 :
7561
                 din[22] ? 6'd23 :
7562
                 din[21] ? 6'd22 :
7563
                 din[20] ? 6'd21 :
7564
                 din[19] ? 6'd20 :
7565
                 din[18] ? 6'd19 :
7566
                 din[17] ? 6'd18 :
7567
                 din[16] ? 6'd17 :
7568
                 din[15] ? 6'd16 :
7569
                 din[14] ? 6'd15 :
7570
                 din[13] ? 6'd14 :
7571
                 din[12] ? 6'd13 :
7572
                 din[11] ? 6'd12 :
7573
                 din[10] ? 6'd11 :
7574
                 din[9] ? 6'd10 :
7575
                 din[8] ? 6'd9 :
7576
                 din[7] ? 6'd8 :
7577
                 din[6] ? 6'd7 :
7578
                 din[5] ? 6'd6 :
7579
                 din[4] ? 6'd5 :
7580
                 din[3] ? 6'd4 :
7581
                 din[2] ? 6'd3 :
7582
                 din[1] ? 6'd2 :
7583
                 din[0] ? 6'd1 :
7584
                 6'd0;
7585
 
7586
    assign dout = (!opcode) ? {{26{1'b0}}, ff1} : {{26{1'b0}}, fl1};
7587 48 unneback
end
7588
endgenerate
7589 57 unneback
 
7590 48 unneback
generate
7591
if (width==64) begin
7592 57 unneback
    assign ff1 = 7'd0;
7593
    assign fl1 = 7'd0;
7594
    assign dout = (!opcode) ? {{57{1'b0}}, ff1} : {{57{1'b0}}, fl1};
7595 48 unneback
end
7596
endgenerate
7597
 
7598
endmodule
7599
`endif
7600
 
7601
`ifdef EXT_UNIT
7602
`define MODULE ext_unit
7603
module `BASE`MODULE ( a, b, F, result, opcode);
7604
`undef MODULE
7605
parameter width = 32;
7606
input [width-1:0] a, b;
7607
input F;
7608
output reg [width-1:0] result;
7609
input [2:0] opcode;
7610
 
7611
generate
7612
if (width==32) begin
7613
always @ (a or b or F or opcode)
7614
begin
7615
    case (opcode)
7616
    3'b000: result = {{24{1'b0}},a[7:0]};
7617
    3'b001: result = {{24{a[7]}},a[7:0]};
7618
    3'b010: result = {{16{1'b0}},a[7:0]};
7619
    3'b011: result = {{16{a[15]}},a[15:0]};
7620
    3'b110: result = (F) ? a : b;
7621
    default: result = {b[15:0],16'h0000};
7622
    endcase
7623
end
7624
end
7625
endgenerate
7626
 
7627
generate
7628
if (width==64) begin
7629
always @ (a or b or F or opcode)
7630
begin
7631
    case (opcode)
7632
    3'b000: result = {{56{1'b0}},a[7:0]};
7633
    3'b001: result = {{56{a[7]}},a[7:0]};
7634
    3'b010: result = {{48{1'b0}},a[7:0]};
7635
    3'b011: result = {{48{a[15]}},a[15:0]};
7636 57 unneback
    3'b110: result = (F) ? a : b;
7637 48 unneback
    default: result = {32'h00000000,b[15:0],16'h0000};
7638
    endcase
7639
end
7640
end
7641
endgenerate
7642
endmodule
7643
`endif

powered by: WebSVN 2.1.0

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