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

Subversion Repositories versatile_library

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

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

Line No. Rev Author Line
1 60 unneback
// default SYN_KEEP definition
2 98 unneback
    // ACTEL FPGA should not use logic to handle rw collision
3 97 unneback
// size to width
4 6 unneback
//////////////////////////////////////////////////////////////////////
5
////                                                              ////
6
////  Versatile library, clock and reset                          ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Logic related to clock and reset                            ////
10
////                                                              ////
11
////                                                              ////
12
////  To Do:                                                      ////
13
////   - add more different registers                             ////
14
////                                                              ////
15
////  Author(s):                                                  ////
16
////      - Michael Unneback, unneback@opencores.org              ////
17
////        ORSoC AB                                              ////
18
////                                                              ////
19
//////////////////////////////////////////////////////////////////////
20
////                                                              ////
21
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
22
////                                                              ////
23
//// This source file may be used and distributed without         ////
24
//// restriction provided that this copyright statement is not    ////
25
//// removed from the file and that any derivative work contains  ////
26
//// the original copyright notice and the associated disclaimer. ////
27
////                                                              ////
28
//// This source file is free software; you can redistribute it   ////
29
//// and/or modify it under the terms of the GNU Lesser General   ////
30
//// Public License as published by the Free Software Foundation; ////
31
//// either version 2.1 of the License, or (at your option) any   ////
32
//// later version.                                               ////
33
////                                                              ////
34
//// This source is distributed in the hope that it will be       ////
35
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
36
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
37
//// PURPOSE.  See the GNU Lesser General Public License for more ////
38
//// details.                                                     ////
39
////                                                              ////
40
//// You should have received a copy of the GNU Lesser General    ////
41
//// Public License along with this source; if not, download it   ////
42
//// from http://www.opencores.org/lgpl.shtml                     ////
43
////                                                              ////
44
//////////////////////////////////////////////////////////////////////
45 48 unneback
`timescale 1 ns/100 ps
46 6 unneback
// Global buffer
47
// usage:
48
// use to enable global buffers for high fan out signals such as clock and reset
49
// Version: 8.4 8.4.0.33
50
module gbuf(GL,CLK);
51
output GL;
52
input  CLK;
53
    wire GND;
54
    GND GND_1_net(.Y(GND));
55
    CLKDLY Inst1(.CLK(CLK), .GL(GL), .DLYGL0(GND), .DLYGL1(GND),
56
        .DLYGL2(GND), .DLYGL3(GND), .DLYGL4(GND)) /* synthesis black_box */;
57
endmodule
58
`timescale 1 ns/1 ns
59
module vl_gbuf ( i, o);
60
input i;
61
output o;
62
`ifdef SIM_GBUF
63
assign o=i;
64
`else
65
gbuf gbuf_i0 ( .CLK(i), .GL(o));
66
`endif
67
endmodule
68
 //ACTEL
69
// sync reset
70 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
71 6 unneback
// output active high global reset sync with two DFFs 
72
`timescale 1 ns/100 ps
73
module vl_sync_rst ( rst_n_i, rst_o, clk);
74
input rst_n_i, clk;
75
output rst_o;
76 18 unneback
reg [1:0] tmp;
77 6 unneback
always @ (posedge clk or negedge rst_n_i)
78
if (!rst_n_i)
79 17 unneback
        tmp <= 2'b11;
80 6 unneback
else
81 33 unneback
        tmp <= {1'b0,tmp[1]};
82 17 unneback
vl_gbuf buf_i0( .i(tmp[0]), .o(rst_o));
83 6 unneback
endmodule
84
// vl_pll
85 32 unneback
///////////////////////////////////////////////////////////////////////////////
86 17 unneback
`timescale 1 ps/1 ps
87 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
88
parameter index = 0;
89
parameter number_of_clk = 1;
90 17 unneback
parameter period_time_0 = 20000;
91
parameter period_time_1 = 20000;
92
parameter period_time_2 = 20000;
93
parameter lock_delay = 2000000;
94 6 unneback
input clk_i, rst_n_i;
95
output lock;
96
output reg [0:number_of_clk-1] clk_o;
97
output [0:number_of_clk-1] rst_o;
98
`ifdef SIM_PLL
99
always
100
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
101
generate if (number_of_clk > 1)
102
always
103
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
104
endgenerate
105
generate if (number_of_clk > 2)
106
always
107
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
108
endgenerate
109
genvar i;
110
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
111
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
112
end
113
endgenerate
114
assign #lock_delay lock = rst_n_i;
115
endmodule
116
`else
117
generate if (number_of_clk==1 & index==0) begin
118
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
119
end
120
endgenerate // index==0
121
generate if (number_of_clk==1 & index==1) begin
122
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
123
end
124
endgenerate // index==1
125
generate if (number_of_clk==1 & index==2) begin
126
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
127
end
128
endgenerate // index==2
129
generate if (number_of_clk==1 & index==3) begin
130
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
131
end
132
endgenerate // index==0
133
generate if (number_of_clk==2 & index==0) begin
134
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
135
end
136
endgenerate // index==0
137
generate if (number_of_clk==2 & index==1) begin
138
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
139
end
140
endgenerate // index==1
141
generate if (number_of_clk==2 & index==2) begin
142
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
143
end
144
endgenerate // index==2
145
generate if (number_of_clk==2 & index==3) begin
146
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
147
end
148
endgenerate // index==0
149
generate if (number_of_clk==3 & index==0) begin
150
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
151
end
152
endgenerate // index==0
153
generate if (number_of_clk==3 & index==1) begin
154
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
155
end
156
endgenerate // index==1
157
generate if (number_of_clk==3 & index==2) begin
158
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
159
end
160
endgenerate // index==2
161
generate if (number_of_clk==3 & index==3) begin
162
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
163
end
164
endgenerate // index==0
165
genvar i;
166
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
167 40 unneback
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
168 6 unneback
end
169
endgenerate
170
endmodule
171
`endif
172 32 unneback
///////////////////////////////////////////////////////////////////////////////
173 6 unneback
 //actel
174
//////////////////////////////////////////////////////////////////////
175
////                                                              ////
176
////  Versatile library, registers                                ////
177
////                                                              ////
178
////  Description                                                 ////
179
////  Different type of registers                                 ////
180
////                                                              ////
181
////                                                              ////
182
////  To Do:                                                      ////
183
////   - add more different registers                             ////
184
////                                                              ////
185
////  Author(s):                                                  ////
186
////      - Michael Unneback, unneback@opencores.org              ////
187
////        ORSoC AB                                              ////
188
////                                                              ////
189
//////////////////////////////////////////////////////////////////////
190
////                                                              ////
191
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
192
////                                                              ////
193
//// This source file may be used and distributed without         ////
194
//// restriction provided that this copyright statement is not    ////
195
//// removed from the file and that any derivative work contains  ////
196
//// the original copyright notice and the associated disclaimer. ////
197
////                                                              ////
198
//// This source file is free software; you can redistribute it   ////
199
//// and/or modify it under the terms of the GNU Lesser General   ////
200
//// Public License as published by the Free Software Foundation; ////
201
//// either version 2.1 of the License, or (at your option) any   ////
202
//// later version.                                               ////
203
////                                                              ////
204
//// This source is distributed in the hope that it will be       ////
205
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
206
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
207
//// PURPOSE.  See the GNU Lesser General Public License for more ////
208
//// details.                                                     ////
209
////                                                              ////
210
//// You should have received a copy of the GNU Lesser General    ////
211
//// Public License along with this source; if not, download it   ////
212
//// from http://www.opencores.org/lgpl.shtml                     ////
213
////                                                              ////
214
//////////////////////////////////////////////////////////////////////
215 18 unneback
module vl_dff ( d, q, clk, rst);
216 6 unneback
        parameter width = 1;
217
        parameter reset_value = 0;
218
        input [width-1:0] d;
219
        input clk, rst;
220
        output reg [width-1:0] q;
221
        always @ (posedge clk or posedge rst)
222
        if (rst)
223
                q <= reset_value;
224
        else
225
                q <= d;
226
endmodule
227 18 unneback
module vl_dff_array ( d, q, clk, rst);
228 6 unneback
        parameter width = 1;
229
        parameter depth = 2;
230
        parameter reset_value = 1'b0;
231
        input [width-1:0] d;
232
        input clk, rst;
233
        output [width-1:0] q;
234
        reg  [0:depth-1] q_tmp [width-1:0];
235
        integer i;
236
        always @ (posedge clk or posedge rst)
237
        if (rst) begin
238
            for (i=0;i<depth;i=i+1)
239
                q_tmp[i] <= {width{reset_value}};
240
        end else begin
241
            q_tmp[0] <= d;
242
            for (i=1;i<depth;i=i+1)
243
                q_tmp[i] <= q_tmp[i-1];
244
        end
245
    assign q = q_tmp[depth-1];
246
endmodule
247 18 unneback
module vl_dff_ce ( d, ce, q, clk, rst);
248 6 unneback
        parameter width = 1;
249
        parameter reset_value = 0;
250
        input [width-1:0] d;
251
        input ce, clk, rst;
252
        output reg [width-1:0] q;
253
        always @ (posedge clk or posedge rst)
254
        if (rst)
255
                q <= reset_value;
256
        else
257
                if (ce)
258
                        q <= d;
259
endmodule
260 18 unneback
module vl_dff_ce_clear ( d, ce, clear, q, clk, rst);
261 8 unneback
        parameter width = 1;
262
        parameter reset_value = 0;
263
        input [width-1:0] d;
264 10 unneback
        input ce, clear, clk, rst;
265 8 unneback
        output reg [width-1:0] q;
266
        always @ (posedge clk or posedge rst)
267
        if (rst)
268
            q <= reset_value;
269
        else
270
            if (ce)
271
                if (clear)
272
                    q <= {width{1'b0}};
273
                else
274
                    q <= d;
275
endmodule
276 24 unneback
module vl_dff_ce_set ( d, ce, set, q, clk, rst);
277
        parameter width = 1;
278
        parameter reset_value = 0;
279
        input [width-1:0] d;
280
        input ce, set, clk, rst;
281
        output reg [width-1:0] q;
282
        always @ (posedge clk or posedge rst)
283
        if (rst)
284
            q <= reset_value;
285
        else
286
            if (ce)
287
                if (set)
288
                    q <= {width{1'b1}};
289
                else
290
                    q <= d;
291
endmodule
292 29 unneback
module vl_spr ( sp, r, q, clk, rst);
293 64 unneback
        //parameter width = 1;
294
        parameter reset_value = 1'b0;
295 29 unneback
        input sp, r;
296
        output reg q;
297
        input clk, rst;
298
        always @ (posedge clk or posedge rst)
299
        if (rst)
300
            q <= reset_value;
301
        else
302
            if (sp)
303
                q <= 1'b1;
304
            else if (r)
305
                q <= 1'b0;
306
endmodule
307
module vl_srp ( s, rp, q, clk, rst);
308
        parameter width = 1;
309
        parameter reset_value = 0;
310
        input s, rp;
311
        output reg q;
312
        input clk, rst;
313
        always @ (posedge clk or posedge rst)
314
        if (rst)
315
            q <= reset_value;
316
        else
317
            if (rp)
318
                q <= 1'b0;
319
            else if (s)
320
                q <= 1'b1;
321
endmodule
322 18 unneback
module vl_dff_sr ( aclr, aset, clock, data, q);
323 6 unneback
    input         aclr;
324
    input         aset;
325
    input         clock;
326
    input         data;
327
    output reg    q;
328
   always @ (posedge clock or posedge aclr or posedge aset)
329
     if (aclr)
330
       q <= 1'b0;
331
     else if (aset)
332
       q <= 1'b1;
333
     else
334
       q <= data;
335
endmodule
336
// LATCH
337
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
338 40 unneback
module vl_latch ( d, le, q, clk);
339 6 unneback
input d, le;
340 48 unneback
input clk;
341
always @ (le or d)
342 60 unneback
if (le)
343 48 unneback
    d <= q;
344 6 unneback
endmodule
345 18 unneback
module vl_shreg ( d, q, clk, rst);
346 17 unneback
parameter depth = 10;
347
input d;
348
output q;
349
input clk, rst;
350
reg [1:depth] dffs;
351
always @ (posedge clk or posedge rst)
352
if (rst)
353
    dffs <= {depth{1'b0}};
354
else
355
    dffs <= {d,dffs[1:depth-1]};
356
assign q = dffs[depth];
357
endmodule
358 18 unneback
module vl_shreg_ce ( d, ce, q, clk, rst);
359 17 unneback
parameter depth = 10;
360
input d, ce;
361
output q;
362
input clk, rst;
363
reg [1:depth] dffs;
364
always @ (posedge clk or posedge rst)
365
if (rst)
366
    dffs <= {depth{1'b0}};
367
else
368
    if (ce)
369
        dffs <= {d,dffs[1:depth-1]};
370
assign q = dffs[depth];
371
endmodule
372 18 unneback
module vl_delay ( d, q, clk, rst);
373 15 unneback
parameter depth = 10;
374
input d;
375
output q;
376
input clk, rst;
377
reg [1:depth] dffs;
378
always @ (posedge clk or posedge rst)
379
if (rst)
380
    dffs <= {depth{1'b0}};
381
else
382
    dffs <= {d,dffs[1:depth-1]};
383
assign q = dffs[depth];
384
endmodule
385 18 unneback
module vl_delay_emptyflag ( d, q, emptyflag, clk, rst);
386 17 unneback
parameter depth = 10;
387
input d;
388
output q, emptyflag;
389
input clk, rst;
390
reg [1:depth] dffs;
391
always @ (posedge clk or posedge rst)
392
if (rst)
393
    dffs <= {depth{1'b0}};
394
else
395
    dffs <= {d,dffs[1:depth-1]};
396
assign q = dffs[depth];
397
assign emptyflag = !(|dffs);
398
endmodule
399 98 unneback
module vl_pulse2toggle ( pl, q, clk, rst);
400 94 unneback
input pl;
401 98 unneback
output reg q;
402 94 unneback
input clk, rst;
403
always @ (posedge clk or posedge rst)
404
if (rst)
405
    q <= 1'b0;
406
else
407
    q <= pl ^ q;
408
endmodule
409 98 unneback
module vl_toggle2pulse (d, pl, clk, rst);
410 94 unneback
input d;
411
output pl;
412
input clk, rst;
413
reg dff;
414
always @ (posedge clk or posedge rst)
415
if (rst)
416
    dff <= 1'b0;
417
else
418
    dff <= d;
419 98 unneback
assign pl = d ^ dff;
420 94 unneback
endmodule
421
module vl_synchronizer (d, q, clk, rst);
422
input d;
423
output reg q;
424 116 unneback
input clk, rst;
425 94 unneback
reg dff;
426
always @ (posedge clk or posedge rst)
427
if (rst)
428 100 unneback
    {q,dff} <= 2'b00;
429 94 unneback
else
430 100 unneback
    {q,dff} <= {dff,d};
431 94 unneback
endmodule
432 97 unneback
module vl_cdc ( start_pl, take_it_pl, take_it_grant_pl, got_it_pl, clk_src, rst_src, clk_dst, rst_dst);
433 94 unneback
input start_pl;
434
output take_it_pl;
435
input take_it_grant_pl; // note: connect to take_it_pl to generate automatic ack
436
output got_it_pl;
437
input clk_src, rst_src;
438
input clk_dst, rst_dst;
439
wire take_it_tg, take_it_tg_sync;
440
wire got_it_tg, got_it_tg_sync;
441
// src -> dst
442
vl_pulse2toggle p2t0 (
443
    .pl(start_pl),
444
    .q(take_it_tg),
445
    .clk(clk_src),
446
    .rst(rst_src));
447
vl_synchronizer sync0 (
448
    .d(take_it_tg),
449
    .q(take_it_tg_sync),
450
    .clk(clk_dst),
451
    .rst(rst_dst));
452
vl_toggle2pulse t2p0 (
453 100 unneback
    .d(take_it_tg_sync),
454 94 unneback
    .pl(take_it_pl),
455
    .clk(clk_dst),
456
    .rst(rst_dst));
457
// dst -> src
458 98 unneback
vl_pulse2toggle p2t1 (
459 94 unneback
    .pl(take_it_grant_pl),
460
    .q(got_it_tg),
461
    .clk(clk_dst),
462
    .rst(rst_dst));
463
vl_synchronizer sync1 (
464
    .d(got_it_tg),
465
    .q(got_it_tg_sync),
466
    .clk(clk_src),
467
    .rst(rst_src));
468
vl_toggle2pulse t2p1 (
469 100 unneback
    .d(got_it_tg_sync),
470 94 unneback
    .pl(got_it_pl),
471
    .clk(clk_src),
472
    .rst(rst_src));
473
endmodule
474 6 unneback
//////////////////////////////////////////////////////////////////////
475
////                                                              ////
476 18 unneback
////  Logic functions                                             ////
477
////                                                              ////
478
////  Description                                                 ////
479
////  Logic functions such as multiplexers                        ////
480
////                                                              ////
481
////                                                              ////
482
////  To Do:                                                      ////
483
////   -                                                          ////
484
////                                                              ////
485
////  Author(s):                                                  ////
486
////      - Michael Unneback, unneback@opencores.org              ////
487
////        ORSoC AB                                              ////
488
////                                                              ////
489
//////////////////////////////////////////////////////////////////////
490
////                                                              ////
491
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
492
////                                                              ////
493
//// This source file may be used and distributed without         ////
494
//// restriction provided that this copyright statement is not    ////
495
//// removed from the file and that any derivative work contains  ////
496
//// the original copyright notice and the associated disclaimer. ////
497
////                                                              ////
498
//// This source file is free software; you can redistribute it   ////
499
//// and/or modify it under the terms of the GNU Lesser General   ////
500
//// Public License as published by the Free Software Foundation; ////
501
//// either version 2.1 of the License, or (at your option) any   ////
502
//// later version.                                               ////
503
////                                                              ////
504
//// This source is distributed in the hope that it will be       ////
505
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
506
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
507
//// PURPOSE.  See the GNU Lesser General Public License for more ////
508
//// details.                                                     ////
509
////                                                              ////
510
//// You should have received a copy of the GNU Lesser General    ////
511
//// Public License along with this source; if not, download it   ////
512
//// from http://www.opencores.org/lgpl.shtml                     ////
513
////                                                              ////
514
//////////////////////////////////////////////////////////////////////
515 36 unneback
module vl_mux_andor ( a, sel, dout);
516
parameter width = 32;
517
parameter nr_of_ports = 4;
518
input [nr_of_ports*width-1:0] a;
519
input [nr_of_ports-1:0] sel;
520
output reg [width-1:0] dout;
521 38 unneback
integer i,j;
522 36 unneback
always @ (a, sel)
523
begin
524
    dout = a[width-1:0] & {width{sel[0]}};
525 42 unneback
    for (i=1;i<nr_of_ports;i=i+1)
526
        for (j=0;j<width;j=j+1)
527
            dout[j] = (a[i*width + j] & sel[i]) | dout[j];
528 36 unneback
end
529
endmodule
530 34 unneback
module vl_mux2_andor ( a1, a0, sel, dout);
531
parameter width = 32;
532 35 unneback
localparam nr_of_ports = 2;
533 34 unneback
input [width-1:0] a1, a0;
534
input [nr_of_ports-1:0] sel;
535
output [width-1:0] dout;
536 36 unneback
vl_mux_andor
537 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
538 36 unneback
    mux0( .a({a1,a0}), .sel(sel), .dout(dout));
539 34 unneback
endmodule
540
module vl_mux3_andor ( a2, a1, a0, sel, dout);
541
parameter width = 32;
542 35 unneback
localparam nr_of_ports = 3;
543 34 unneback
input [width-1:0] a2, a1, a0;
544
input [nr_of_ports-1:0] sel;
545
output [width-1:0] dout;
546 36 unneback
vl_mux_andor
547 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
548 36 unneback
    mux0( .a({a2,a1,a0}), .sel(sel), .dout(dout));
549 34 unneback
endmodule
550 18 unneback
module vl_mux4_andor ( a3, a2, a1, a0, sel, dout);
551
parameter width = 32;
552 35 unneback
localparam nr_of_ports = 4;
553 18 unneback
input [width-1:0] a3, a2, a1, a0;
554
input [nr_of_ports-1:0] sel;
555 22 unneback
output [width-1:0] dout;
556 36 unneback
vl_mux_andor
557 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
558 36 unneback
    mux0( .a({a3,a2,a1,a0}), .sel(sel), .dout(dout));
559 18 unneback
endmodule
560
module vl_mux5_andor ( a4, a3, a2, a1, a0, sel, dout);
561
parameter width = 32;
562 35 unneback
localparam nr_of_ports = 5;
563 18 unneback
input [width-1:0] a4, a3, a2, a1, a0;
564
input [nr_of_ports-1:0] sel;
565 22 unneback
output [width-1:0] dout;
566 36 unneback
vl_mux_andor
567 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
568 36 unneback
    mux0( .a({a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
569 18 unneback
endmodule
570
module vl_mux6_andor ( a5, a4, a3, a2, a1, a0, sel, dout);
571
parameter width = 32;
572 35 unneback
localparam nr_of_ports = 6;
573 18 unneback
input [width-1:0] a5, a4, a3, a2, a1, a0;
574
input [nr_of_ports-1:0] sel;
575 22 unneback
output [width-1:0] dout;
576 36 unneback
vl_mux_andor
577 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
578 36 unneback
    mux0( .a({a5,a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
579 18 unneback
endmodule
580 43 unneback
module vl_parity_generate (data, parity);
581
parameter word_size = 32;
582
parameter chunk_size = 8;
583
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
584
input [word_size-1:0] data;
585
output reg [word_size/chunk_size-1:0] parity;
586
integer i,j;
587
always @ (data)
588
for (i=0;i<word_size/chunk_size;i=i+1) begin
589
    parity[i] = parity_type;
590
    for (j=0;j<chunk_size;j=j+1) begin
591 46 unneback
        parity[i] = data[i*chunk_size+j] ^ parity[i];
592 43 unneback
    end
593
end
594
endmodule
595
module vl_parity_check( data, parity, parity_error);
596
parameter word_size = 32;
597
parameter chunk_size = 8;
598
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
599
input [word_size-1:0] data;
600
input [word_size/chunk_size-1:0] parity;
601
output parity_error;
602 44 unneback
reg [word_size/chunk_size-1:0] error_flag;
603 43 unneback
integer i,j;
604
always @ (data or parity)
605
for (i=0;i<word_size/chunk_size;i=i+1) begin
606
    error_flag[i] = parity[i] ^ parity_type;
607
    for (j=0;j<chunk_size;j=j+1) begin
608 46 unneback
        error_flag[i] = data[i*chunk_size+j] ^ error_flag[i];
609 43 unneback
    end
610
end
611
assign parity_error = |error_flag;
612
endmodule
613 18 unneback
//////////////////////////////////////////////////////////////////////
614
////                                                              ////
615 44 unneback
////  IO functions                                                ////
616
////                                                              ////
617
////  Description                                                 ////
618
////  IO functions such as IOB flip-flops                         ////
619
////                                                              ////
620
////                                                              ////
621
////  To Do:                                                      ////
622
////   -                                                          ////
623
////                                                              ////
624
////  Author(s):                                                  ////
625
////      - Michael Unneback, unneback@opencores.org              ////
626
////        ORSoC AB                                              ////
627
////                                                              ////
628
//////////////////////////////////////////////////////////////////////
629
////                                                              ////
630
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
631
////                                                              ////
632
//// This source file may be used and distributed without         ////
633
//// restriction provided that this copyright statement is not    ////
634
//// removed from the file and that any derivative work contains  ////
635
//// the original copyright notice and the associated disclaimer. ////
636
////                                                              ////
637
//// This source file is free software; you can redistribute it   ////
638
//// and/or modify it under the terms of the GNU Lesser General   ////
639
//// Public License as published by the Free Software Foundation; ////
640
//// either version 2.1 of the License, or (at your option) any   ////
641
//// later version.                                               ////
642
////                                                              ////
643
//// This source is distributed in the hope that it will be       ////
644
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
645
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
646
//// PURPOSE.  See the GNU Lesser General Public License for more ////
647
//// details.                                                     ////
648
////                                                              ////
649
//// You should have received a copy of the GNU Lesser General    ////
650
//// Public License along with this source; if not, download it   ////
651
//// from http://www.opencores.org/lgpl.shtml                     ////
652
////                                                              ////
653
//////////////////////////////////////////////////////////////////////
654 45 unneback
`timescale 1ns/1ns
655 44 unneback
module vl_o_dff (d_i, o_pad, clk, rst);
656
parameter width = 1;
657 45 unneback
parameter reset_value = {width{1'b0}};
658
input  [width-1:0]  d_i;
659 44 unneback
output [width-1:0] o_pad;
660
input clk, rst;
661
wire [width-1:0] d_i_int /*synthesis syn_keep = 1*/;
662 45 unneback
reg  [width-1:0] o_pad_int;
663 44 unneback
assign d_i_int = d_i;
664
genvar i;
665 45 unneback
generate
666 44 unneback
for (i=0;i<width;i=i+1) begin
667
    always @ (posedge clk or posedge rst)
668
    if (rst)
669 45 unneback
        o_pad_int[i] <= reset_value[i];
670 44 unneback
    else
671 45 unneback
        o_pad_int[i] <= d_i_int[i];
672
    assign #1 o_pad[i] = o_pad_int[i];
673 44 unneback
end
674
endgenerate
675
endmodule
676 45 unneback
`timescale 1ns/1ns
677 44 unneback
module vl_io_dff_oe ( d_i, d_o, oe, io_pad, clk, rst);
678
parameter width = 1;
679
input  [width-1:0] d_o;
680
output reg [width-1:0] d_i;
681
input oe;
682
inout [width-1:0] io_pad;
683
input clk, rst;
684
wire [width-1:0] oe_d /*synthesis syn_keep = 1*/;
685
reg [width-1:0] oe_q;
686
reg [width-1:0] d_o_q;
687
assign oe_d = {width{oe}};
688
genvar i;
689
generate
690
for (i=0;i<width;i=i+1) begin
691
    always @ (posedge clk or posedge rst)
692
    if (rst)
693
        oe_q[i] <= 1'b0;
694
    else
695
        oe_q[i] <= oe_d[i];
696
    always @ (posedge clk or posedge rst)
697
    if (rst)
698
        d_o_q[i] <= 1'b0;
699
    else
700
        d_o_q[i] <= d_o[i];
701
    always @ (posedge clk or posedge rst)
702
    if (rst)
703
        d_i[i] <= 1'b0;
704
    else
705
        d_i[i] <= io_pad[i];
706 45 unneback
    assign #1 io_pad[i] = (oe_q[i]) ? d_o_q[i] : 1'bz;
707 44 unneback
end
708
endgenerate
709
endmodule
710
//////////////////////////////////////////////////////////////////////
711
////                                                              ////
712 6 unneback
////  Versatile counter                                           ////
713
////                                                              ////
714
////  Description                                                 ////
715
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
716
////  counter                                                     ////
717
////                                                              ////
718
////  To Do:                                                      ////
719
////   - add LFSR with more taps                                  ////
720
////                                                              ////
721
////  Author(s):                                                  ////
722
////      - Michael Unneback, unneback@opencores.org              ////
723
////        ORSoC AB                                              ////
724
////                                                              ////
725
//////////////////////////////////////////////////////////////////////
726
////                                                              ////
727
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
728
////                                                              ////
729
//// This source file may be used and distributed without         ////
730
//// restriction provided that this copyright statement is not    ////
731
//// removed from the file and that any derivative work contains  ////
732
//// the original copyright notice and the associated disclaimer. ////
733
////                                                              ////
734
//// This source file is free software; you can redistribute it   ////
735
//// and/or modify it under the terms of the GNU Lesser General   ////
736
//// Public License as published by the Free Software Foundation; ////
737
//// either version 2.1 of the License, or (at your option) any   ////
738
//// later version.                                               ////
739
////                                                              ////
740
//// This source is distributed in the hope that it will be       ////
741
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
742
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
743
//// PURPOSE.  See the GNU Lesser General Public License for more ////
744
//// details.                                                     ////
745
////                                                              ////
746
//// You should have received a copy of the GNU Lesser General    ////
747
//// Public License along with this source; if not, download it   ////
748
//// from http://www.opencores.org/lgpl.shtml                     ////
749
////                                                              ////
750
//////////////////////////////////////////////////////////////////////
751
// binary counter
752 40 unneback
module vl_cnt_bin_ce (
753
 cke, q, rst, clk);
754 22 unneback
   parameter length = 4;
755 6 unneback
   input cke;
756
   output [length:1] q;
757
   input rst;
758
   input clk;
759
   parameter clear_value = 0;
760
   parameter set_value = 1;
761
   parameter wrap_value = 0;
762
   parameter level1_value = 15;
763
   reg  [length:1] qi;
764
   wire [length:1] q_next;
765
   assign q_next = qi + {{length-1{1'b0}},1'b1};
766
   always @ (posedge clk or posedge rst)
767
     if (rst)
768
       qi <= {length{1'b0}};
769
     else
770
     if (cke)
771
       qi <= q_next;
772
   assign q = qi;
773
endmodule
774
//////////////////////////////////////////////////////////////////////
775
////                                                              ////
776
////  Versatile counter                                           ////
777
////                                                              ////
778
////  Description                                                 ////
779
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
780
////  counter                                                     ////
781
////                                                              ////
782
////  To Do:                                                      ////
783
////   - add LFSR with more taps                                  ////
784
////                                                              ////
785
////  Author(s):                                                  ////
786
////      - Michael Unneback, unneback@opencores.org              ////
787
////        ORSoC AB                                              ////
788
////                                                              ////
789
//////////////////////////////////////////////////////////////////////
790
////                                                              ////
791
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
792
////                                                              ////
793
//// This source file may be used and distributed without         ////
794
//// restriction provided that this copyright statement is not    ////
795
//// removed from the file and that any derivative work contains  ////
796
//// the original copyright notice and the associated disclaimer. ////
797
////                                                              ////
798
//// This source file is free software; you can redistribute it   ////
799
//// and/or modify it under the terms of the GNU Lesser General   ////
800
//// Public License as published by the Free Software Foundation; ////
801
//// either version 2.1 of the License, or (at your option) any   ////
802
//// later version.                                               ////
803
////                                                              ////
804
//// This source is distributed in the hope that it will be       ////
805
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
806
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
807
//// PURPOSE.  See the GNU Lesser General Public License for more ////
808
//// details.                                                     ////
809
////                                                              ////
810
//// You should have received a copy of the GNU Lesser General    ////
811
//// Public License along with this source; if not, download it   ////
812
//// from http://www.opencores.org/lgpl.shtml                     ////
813
////                                                              ////
814
//////////////////////////////////////////////////////////////////////
815
// binary counter
816 40 unneback
module vl_cnt_bin_ce_rew_zq_l1 (
817
 cke, rew, zq, level1, rst, clk);
818 6 unneback
   parameter length = 4;
819
   input cke;
820
   input rew;
821 25 unneback
   output reg zq;
822
   output reg level1;
823
   input rst;
824
   input clk;
825
   parameter clear_value = 0;
826
   parameter set_value = 1;
827
   parameter wrap_value = 1;
828
   parameter level1_value = 15;
829 29 unneback
   wire clear;
830 30 unneback
   assign clear = 1'b0;
831 25 unneback
   reg  [length:1] qi;
832
   wire  [length:1] q_next, q_next_fw, q_next_rew;
833
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
834
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
835
   assign q_next = rew ? q_next_rew : q_next_fw;
836
   always @ (posedge clk or posedge rst)
837
     if (rst)
838
       qi <= {length{1'b0}};
839
     else
840
     if (cke)
841
       qi <= q_next;
842
   always @ (posedge clk or posedge rst)
843
     if (rst)
844
       zq <= 1'b1;
845
     else
846
     if (cke)
847
       zq <= q_next == {length{1'b0}};
848
    always @ (posedge clk or posedge rst)
849
    if (rst)
850
        level1 <= 1'b0;
851
    else
852
    if (cke)
853 29 unneback
    if (clear)
854
        level1 <= 1'b0;
855
    else if (q_next == level1_value)
856 25 unneback
        level1 <= 1'b1;
857
    else if (qi == level1_value & rew)
858
        level1 <= 1'b0;
859
endmodule
860
//////////////////////////////////////////////////////////////////////
861
////                                                              ////
862
////  Versatile counter                                           ////
863
////                                                              ////
864
////  Description                                                 ////
865
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
866
////  counter                                                     ////
867
////                                                              ////
868
////  To Do:                                                      ////
869
////   - add LFSR with more taps                                  ////
870
////                                                              ////
871
////  Author(s):                                                  ////
872
////      - Michael Unneback, unneback@opencores.org              ////
873
////        ORSoC AB                                              ////
874
////                                                              ////
875
//////////////////////////////////////////////////////////////////////
876
////                                                              ////
877
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
878
////                                                              ////
879
//// This source file may be used and distributed without         ////
880
//// restriction provided that this copyright statement is not    ////
881
//// removed from the file and that any derivative work contains  ////
882
//// the original copyright notice and the associated disclaimer. ////
883
////                                                              ////
884
//// This source file is free software; you can redistribute it   ////
885
//// and/or modify it under the terms of the GNU Lesser General   ////
886
//// Public License as published by the Free Software Foundation; ////
887
//// either version 2.1 of the License, or (at your option) any   ////
888
//// later version.                                               ////
889
////                                                              ////
890
//// This source is distributed in the hope that it will be       ////
891
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
892
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
893
//// PURPOSE.  See the GNU Lesser General Public License for more ////
894
//// details.                                                     ////
895
////                                                              ////
896
//// You should have received a copy of the GNU Lesser General    ////
897
//// Public License along with this source; if not, download it   ////
898
//// from http://www.opencores.org/lgpl.shtml                     ////
899
////                                                              ////
900
//////////////////////////////////////////////////////////////////////
901
// binary counter
902 40 unneback
module vl_cnt_bin_ce_rew_q_zq_l1 (
903
 cke, rew, q, zq, level1, rst, clk);
904 25 unneback
   parameter length = 4;
905
   input cke;
906
   input rew;
907
   output [length:1] q;
908
   output reg zq;
909
   output reg level1;
910
   input rst;
911
   input clk;
912
   parameter clear_value = 0;
913
   parameter set_value = 1;
914
   parameter wrap_value = 1;
915
   parameter level1_value = 15;
916 29 unneback
   wire clear;
917 30 unneback
   assign clear = 1'b0;
918 25 unneback
   reg  [length:1] qi;
919
   wire  [length:1] q_next, q_next_fw, q_next_rew;
920
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
921
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
922
   assign q_next = rew ? q_next_rew : q_next_fw;
923
   always @ (posedge clk or posedge rst)
924
     if (rst)
925
       qi <= {length{1'b0}};
926
     else
927
     if (cke)
928
       qi <= q_next;
929
   assign q = qi;
930
   always @ (posedge clk or posedge rst)
931
     if (rst)
932
       zq <= 1'b1;
933
     else
934
     if (cke)
935
       zq <= q_next == {length{1'b0}};
936
    always @ (posedge clk or posedge rst)
937
    if (rst)
938
        level1 <= 1'b0;
939
    else
940
    if (cke)
941 29 unneback
    if (clear)
942
        level1 <= 1'b0;
943
    else if (q_next == level1_value)
944 25 unneback
        level1 <= 1'b1;
945
    else if (qi == level1_value & rew)
946
        level1 <= 1'b0;
947
endmodule
948
//////////////////////////////////////////////////////////////////////
949
////                                                              ////
950
////  Versatile counter                                           ////
951
////                                                              ////
952
////  Description                                                 ////
953
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
954
////  counter                                                     ////
955
////                                                              ////
956
////  To Do:                                                      ////
957
////   - add LFSR with more taps                                  ////
958
////                                                              ////
959
////  Author(s):                                                  ////
960
////      - Michael Unneback, unneback@opencores.org              ////
961
////        ORSoC AB                                              ////
962
////                                                              ////
963
//////////////////////////////////////////////////////////////////////
964
////                                                              ////
965
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
966
////                                                              ////
967
//// This source file may be used and distributed without         ////
968
//// restriction provided that this copyright statement is not    ////
969
//// removed from the file and that any derivative work contains  ////
970
//// the original copyright notice and the associated disclaimer. ////
971
////                                                              ////
972
//// This source file is free software; you can redistribute it   ////
973
//// and/or modify it under the terms of the GNU Lesser General   ////
974
//// Public License as published by the Free Software Foundation; ////
975
//// either version 2.1 of the License, or (at your option) any   ////
976
//// later version.                                               ////
977
////                                                              ////
978
//// This source is distributed in the hope that it will be       ////
979
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
980
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
981
//// PURPOSE.  See the GNU Lesser General Public License for more ////
982
//// details.                                                     ////
983
////                                                              ////
984
//// You should have received a copy of the GNU Lesser General    ////
985
//// Public License along with this source; if not, download it   ////
986
//// from http://www.opencores.org/lgpl.shtml                     ////
987
////                                                              ////
988
//////////////////////////////////////////////////////////////////////
989 75 unneback
// LFSR counter
990
module vl_cnt_lfsr_ce (
991
 cke, zq, rst, clk);
992
   parameter length = 4;
993
   input cke;
994
   output reg zq;
995
   input rst;
996
   input clk;
997
   parameter clear_value = 0;
998
   parameter set_value = 1;
999
   parameter wrap_value = 0;
1000
   parameter level1_value = 15;
1001
   reg  [length:1] qi;
1002
   reg lfsr_fb;
1003
   wire [length:1] q_next;
1004
   reg [32:1] polynom;
1005
   integer i;
1006
   always @ (qi)
1007
   begin
1008
        case (length)
1009
         2: polynom = 32'b11;                               // 0x3
1010
         3: polynom = 32'b110;                              // 0x6
1011
         4: polynom = 32'b1100;                             // 0xC
1012
         5: polynom = 32'b10100;                            // 0x14
1013
         6: polynom = 32'b110000;                           // 0x30
1014
         7: polynom = 32'b1100000;                          // 0x60
1015
         8: polynom = 32'b10111000;                         // 0xb8
1016
         9: polynom = 32'b100010000;                        // 0x110
1017
        10: polynom = 32'b1001000000;                       // 0x240
1018
        11: polynom = 32'b10100000000;                      // 0x500
1019
        12: polynom = 32'b100000101001;                     // 0x829
1020
        13: polynom = 32'b1000000001100;                    // 0x100C
1021
        14: polynom = 32'b10000000010101;                   // 0x2015
1022
        15: polynom = 32'b110000000000000;                  // 0x6000
1023
        16: polynom = 32'b1101000000001000;                 // 0xD008
1024
        17: polynom = 32'b10010000000000000;                // 0x12000
1025
        18: polynom = 32'b100000010000000000;               // 0x20400
1026
        19: polynom = 32'b1000000000000100011;              // 0x40023
1027
        20: polynom = 32'b10010000000000000000;             // 0x90000
1028
        21: polynom = 32'b101000000000000000000;            // 0x140000
1029
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1030
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1031
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1032
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1033
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1034
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1035
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1036
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1037
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1038
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1039
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1040
        default: polynom = 32'b0;
1041
        endcase
1042
        lfsr_fb = qi[length];
1043
        for (i=length-1; i>=1; i=i-1) begin
1044
            if (polynom[i])
1045
                lfsr_fb = lfsr_fb  ~^ qi[i];
1046
        end
1047
    end
1048
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1049
   always @ (posedge clk or posedge rst)
1050
     if (rst)
1051
       qi <= {length{1'b0}};
1052
     else
1053
     if (cke)
1054
       qi <= q_next;
1055
   always @ (posedge clk or posedge rst)
1056
     if (rst)
1057
       zq <= 1'b1;
1058
     else
1059
     if (cke)
1060
       zq <= q_next == {length{1'b0}};
1061
endmodule
1062
//////////////////////////////////////////////////////////////////////
1063
////                                                              ////
1064
////  Versatile counter                                           ////
1065
////                                                              ////
1066
////  Description                                                 ////
1067
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1068
////  counter                                                     ////
1069
////                                                              ////
1070
////  To Do:                                                      ////
1071
////   - add LFSR with more taps                                  ////
1072
////                                                              ////
1073
////  Author(s):                                                  ////
1074
////      - Michael Unneback, unneback@opencores.org              ////
1075
////        ORSoC AB                                              ////
1076
////                                                              ////
1077
//////////////////////////////////////////////////////////////////////
1078
////                                                              ////
1079
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1080
////                                                              ////
1081
//// This source file may be used and distributed without         ////
1082
//// restriction provided that this copyright statement is not    ////
1083
//// removed from the file and that any derivative work contains  ////
1084
//// the original copyright notice and the associated disclaimer. ////
1085
////                                                              ////
1086
//// This source file is free software; you can redistribute it   ////
1087
//// and/or modify it under the terms of the GNU Lesser General   ////
1088
//// Public License as published by the Free Software Foundation; ////
1089
//// either version 2.1 of the License, or (at your option) any   ////
1090
//// later version.                                               ////
1091
////                                                              ////
1092
//// This source is distributed in the hope that it will be       ////
1093
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1094
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1095
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1096
//// details.                                                     ////
1097
////                                                              ////
1098
//// You should have received a copy of the GNU Lesser General    ////
1099
//// Public License along with this source; if not, download it   ////
1100
//// from http://www.opencores.org/lgpl.shtml                     ////
1101
////                                                              ////
1102
//////////////////////////////////////////////////////////////////////
1103 6 unneback
// GRAY counter
1104 40 unneback
module vl_cnt_gray_ce_bin (
1105
 cke, q, q_bin, rst, clk);
1106 6 unneback
   parameter length = 4;
1107
   input cke;
1108
   output reg [length:1] q;
1109
   output [length:1] q_bin;
1110
   input rst;
1111
   input clk;
1112
   parameter clear_value = 0;
1113
   parameter set_value = 1;
1114
   parameter wrap_value = 8;
1115
   parameter level1_value = 15;
1116
   reg  [length:1] qi;
1117
   wire [length:1] q_next;
1118
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1119
   always @ (posedge clk or posedge rst)
1120
     if (rst)
1121
       qi <= {length{1'b0}};
1122
     else
1123
     if (cke)
1124
       qi <= q_next;
1125
   always @ (posedge clk or posedge rst)
1126
     if (rst)
1127
       q <= {length{1'b0}};
1128
     else
1129
       if (cke)
1130
         q <= (q_next>>1) ^ q_next;
1131
   assign q_bin = qi;
1132
endmodule
1133
//////////////////////////////////////////////////////////////////////
1134
////                                                              ////
1135
////  Versatile library, counters                                 ////
1136
////                                                              ////
1137
////  Description                                                 ////
1138
////  counters                                                    ////
1139
////                                                              ////
1140
////                                                              ////
1141
////  To Do:                                                      ////
1142
////   - add more counters                                        ////
1143
////                                                              ////
1144
////  Author(s):                                                  ////
1145
////      - Michael Unneback, unneback@opencores.org              ////
1146
////        ORSoC AB                                              ////
1147
////                                                              ////
1148
//////////////////////////////////////////////////////////////////////
1149
////                                                              ////
1150
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1151
////                                                              ////
1152
//// This source file may be used and distributed without         ////
1153
//// restriction provided that this copyright statement is not    ////
1154
//// removed from the file and that any derivative work contains  ////
1155
//// the original copyright notice and the associated disclaimer. ////
1156
////                                                              ////
1157
//// This source file is free software; you can redistribute it   ////
1158
//// and/or modify it under the terms of the GNU Lesser General   ////
1159
//// Public License as published by the Free Software Foundation; ////
1160
//// either version 2.1 of the License, or (at your option) any   ////
1161
//// later version.                                               ////
1162
////                                                              ////
1163
//// This source is distributed in the hope that it will be       ////
1164
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1165
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1166
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1167
//// details.                                                     ////
1168
////                                                              ////
1169
//// You should have received a copy of the GNU Lesser General    ////
1170
//// Public License along with this source; if not, download it   ////
1171
//// from http://www.opencores.org/lgpl.shtml                     ////
1172
////                                                              ////
1173
//////////////////////////////////////////////////////////////////////
1174 18 unneback
module vl_cnt_shreg_wrap ( q, rst, clk);
1175 6 unneback
   parameter length = 4;
1176
   output reg [0:length-1] q;
1177
   input rst;
1178
   input clk;
1179
    always @ (posedge clk or posedge rst)
1180
    if (rst)
1181
        q <= {1'b1,{length-1{1'b0}}};
1182
    else
1183
        q <= {q[length-1],q[0:length-2]};
1184
endmodule
1185 18 unneback
module vl_cnt_shreg_ce_wrap ( cke, q, rst, clk);
1186 6 unneback
   parameter length = 4;
1187
   input cke;
1188
   output reg [0:length-1] q;
1189
   input rst;
1190
   input clk;
1191
    always @ (posedge clk or posedge rst)
1192
    if (rst)
1193
        q <= {1'b1,{length-1{1'b0}}};
1194
    else
1195
        if (cke)
1196
            q <= {q[length-1],q[0:length-2]};
1197
endmodule
1198 105 unneback
module vl_cnt_shreg_clear ( clear, q, rst, clk);
1199
   parameter length = 4;
1200
   input clear;
1201
   output reg [0:length-1] q;
1202
   input rst;
1203
   input clk;
1204
    always @ (posedge clk or posedge rst)
1205
    if (rst)
1206
        q <= {1'b1,{length-1{1'b0}}};
1207
    else
1208
        if (clear)
1209
            q <= {1'b1,{length-1{1'b0}}};
1210
        else
1211
            q <= q >> 1;
1212
endmodule
1213 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
1214 6 unneback
   parameter length = 4;
1215
   input cke, clear;
1216
   output reg [0:length-1] q;
1217
   input rst;
1218
   input clk;
1219
    always @ (posedge clk or posedge rst)
1220
    if (rst)
1221
        q <= {1'b1,{length-1{1'b0}}};
1222
    else
1223
        if (cke)
1224
            if (clear)
1225
                q <= {1'b1,{length-1{1'b0}}};
1226
            else
1227
                q <= q >> 1;
1228
endmodule
1229 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
1230 6 unneback
   parameter length = 4;
1231
   input cke, clear;
1232
   output reg [0:length-1] q;
1233
   input rst;
1234
   input clk;
1235
    always @ (posedge clk or posedge rst)
1236
    if (rst)
1237
        q <= {1'b1,{length-1{1'b0}}};
1238
    else
1239
        if (cke)
1240
            if (clear)
1241
                q <= {1'b1,{length-1{1'b0}}};
1242
            else
1243
            q <= {q[length-1],q[0:length-2]};
1244
endmodule
1245
//////////////////////////////////////////////////////////////////////
1246
////                                                              ////
1247
////  Versatile library, memories                                 ////
1248
////                                                              ////
1249
////  Description                                                 ////
1250
////  memories                                                    ////
1251
////                                                              ////
1252
////                                                              ////
1253
////  To Do:                                                      ////
1254
////   - add more memory types                                    ////
1255
////                                                              ////
1256
////  Author(s):                                                  ////
1257
////      - Michael Unneback, unneback@opencores.org              ////
1258
////        ORSoC AB                                              ////
1259
////                                                              ////
1260
//////////////////////////////////////////////////////////////////////
1261
////                                                              ////
1262
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1263
////                                                              ////
1264
//// This source file may be used and distributed without         ////
1265
//// restriction provided that this copyright statement is not    ////
1266
//// removed from the file and that any derivative work contains  ////
1267
//// the original copyright notice and the associated disclaimer. ////
1268
////                                                              ////
1269
//// This source file is free software; you can redistribute it   ////
1270
//// and/or modify it under the terms of the GNU Lesser General   ////
1271
//// Public License as published by the Free Software Foundation; ////
1272
//// either version 2.1 of the License, or (at your option) any   ////
1273
//// later version.                                               ////
1274
////                                                              ////
1275
//// This source is distributed in the hope that it will be       ////
1276
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1277
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1278
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1279
//// details.                                                     ////
1280
////                                                              ////
1281
//// You should have received a copy of the GNU Lesser General    ////
1282
//// Public License along with this source; if not, download it   ////
1283
//// from http://www.opencores.org/lgpl.shtml                     ////
1284
////                                                              ////
1285
//////////////////////////////////////////////////////////////////////
1286
/// ROM
1287 7 unneback
module vl_rom_init ( adr, q, clk);
1288
   parameter data_width = 32;
1289
   parameter addr_width = 8;
1290 75 unneback
   parameter mem_size = 1<<addr_width;
1291 7 unneback
   input [(addr_width-1):0]       adr;
1292
   output reg [(data_width-1):0] q;
1293
   input                         clk;
1294 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
1295 7 unneback
   parameter memory_file = "vl_rom.vmem";
1296
   initial
1297
     begin
1298
        $readmemh(memory_file, rom);
1299
     end
1300
   always @ (posedge clk)
1301
     q <= rom[adr];
1302
endmodule
1303 6 unneback
// Single port RAM
1304
module vl_ram ( d, adr, we, q, clk);
1305
   parameter data_width = 32;
1306
   parameter addr_width = 8;
1307 75 unneback
   parameter mem_size = 1<<addr_width;
1308 100 unneback
   parameter debug = 0;
1309 6 unneback
   input [(data_width-1):0]      d;
1310
   input [(addr_width-1):0]       adr;
1311
   input                         we;
1312 7 unneback
   output reg [(data_width-1):0] q;
1313 6 unneback
   input                         clk;
1314 98 unneback
   reg [data_width-1:0] ram [mem_size-1:0];
1315 100 unneback
    parameter memory_init = 0;
1316
    parameter memory_file = "vl_ram.vmem";
1317
    generate
1318
    if (memory_init == 1) begin : init_mem
1319
        initial
1320
            $readmemh(memory_file, ram);
1321
   end else if (memory_init == 2) begin : init_zero
1322
        integer k;
1323
        initial
1324
            for (k = 0; k < mem_size; k = k + 1)
1325
                ram[k] = 0;
1326 7 unneback
   end
1327
   endgenerate
1328 100 unneback
    generate
1329
    if (debug==1) begin : debug_we
1330
        always @ (posedge clk)
1331
        if (we)
1332
            $display ("Value %h written at address %h : time %t", d, adr, $time);
1333
    end
1334
    endgenerate
1335 6 unneback
   always @ (posedge clk)
1336
   begin
1337
   if (we)
1338
     ram[adr] <= d;
1339
   q <= ram[adr];
1340
   end
1341
endmodule
1342 91 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
1343 7 unneback
   parameter data_width = 32;
1344 72 unneback
   parameter addr_width = 6;
1345 75 unneback
   parameter mem_size = 1<<addr_width;
1346 7 unneback
   input [(data_width-1):0]      d;
1347
   input [(addr_width-1):0]       adr;
1348 73 unneback
   input [(data_width/8)-1:0]    be;
1349 7 unneback
   input                         we;
1350
   output reg [(data_width-1):0] q;
1351
   input                         clk;
1352 65 unneback
`ifdef SYSTEMVERILOG
1353 95 unneback
    // use a multi-dimensional packed array
1354
    //t o model individual bytes within the word
1355
    logic [data_width/8-1:0][7:0] ram [0:mem_size-1];// # words = 1 << address width
1356 65 unneback
`else
1357 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
1358
    wire [data_width/8-1:0] cke;
1359 65 unneback
`endif
1360 100 unneback
    parameter memory_init = 0;
1361
    parameter memory_file = "vl_ram.vmem";
1362
    generate
1363
    if (memory_init == 1) begin : init_mem
1364
        initial
1365
            $readmemh(memory_file, ram);
1366
    end else if (memory_init == 2) begin : init_zero
1367
        integer k;
1368
        initial
1369
            for (k = 0; k < mem_size; k = k + 1)
1370
                ram[k] = 0;
1371
    end
1372 7 unneback
   endgenerate
1373 60 unneback
`ifdef SYSTEMVERILOG
1374
always_ff@(posedge clk)
1375
begin
1376 95 unneback
    if(we) begin
1377 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
1378
        if(be[2]) ram[adr][2] <= d[23:16];
1379
        if(be[1]) ram[adr][1] <= d[15:8];
1380
        if(be[0]) ram[adr][0] <= d[7:0];
1381 60 unneback
    end
1382 90 unneback
        q <= ram[adr];
1383 60 unneback
end
1384
`else
1385 85 unneback
assign cke = {data_width/8{we}} & be;
1386 7 unneback
   genvar i;
1387 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
1388 7 unneback
      always @ (posedge clk)
1389 85 unneback
      if (cke[i])
1390 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
1391
   end
1392
   endgenerate
1393
   always @ (posedge clk)
1394
      q <= ram[adr];
1395 60 unneback
`endif
1396 93 unneback
`ifdef verilator
1397 85 unneback
   // Function to access RAM (for use by Verilator).
1398
   function [31:0] get_mem;
1399
      // verilator public
1400 90 unneback
      input [addr_width-1:0]             addr;
1401 85 unneback
      get_mem = ram[addr];
1402
   endfunction // get_mem
1403
   // Function to write RAM (for use by Verilator).
1404
   function set_mem;
1405
      // verilator public
1406 90 unneback
      input [addr_width-1:0]             addr;
1407
      input [data_width-1:0]             data;
1408 85 unneback
      ram[addr] = data;
1409
   endfunction // set_mem
1410 93 unneback
`endif
1411 7 unneback
endmodule
1412
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1413 6 unneback
   parameter data_width = 32;
1414
   parameter addr_width = 8;
1415 75 unneback
   parameter mem_size = 1<<addr_width;
1416 6 unneback
   input [(data_width-1):0]      d_a;
1417
   input [(addr_width-1):0]       adr_a;
1418
   input [(addr_width-1):0]       adr_b;
1419
   input                         we_a;
1420 118 unneback
   output reg [(data_width-1):0]          q_b;
1421 6 unneback
   input                         clk_a, clk_b;
1422 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1423 100 unneback
    parameter memory_init = 0;
1424
    parameter memory_file = "vl_ram.vmem";
1425
    parameter debug = 0;
1426
    generate
1427
    if (memory_init == 1) begin : init_mem
1428
        initial
1429
            $readmemh(memory_file, ram);
1430
    end else if (memory_init == 2) begin : init_zero
1431
        integer k;
1432
        initial
1433
            for (k = 0; k < mem_size; k = k + 1)
1434
                ram[k] = 0;
1435
    end
1436 7 unneback
   endgenerate
1437 100 unneback
    generate
1438
    if (debug==1) begin : debug_we
1439
        always @ (posedge clk_a)
1440
        if (we_a)
1441
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1442
    end
1443
    endgenerate
1444 6 unneback
   always @ (posedge clk_a)
1445
   if (we_a)
1446
     ram[adr_a] <= d_a;
1447
   always @ (posedge clk_b)
1448 118 unneback
      q_b = ram[adr_b];
1449 6 unneback
endmodule
1450 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1451 6 unneback
   parameter data_width = 32;
1452
   parameter addr_width = 8;
1453 75 unneback
   parameter mem_size = 1<<addr_width;
1454 6 unneback
   input [(data_width-1):0]      d_a;
1455
   input [(addr_width-1):0]       adr_a;
1456
   input [(addr_width-1):0]       adr_b;
1457
   input                         we_a;
1458
   output [(data_width-1):0]      q_b;
1459
   output reg [(data_width-1):0] q_a;
1460
   input                         clk_a, clk_b;
1461
   reg [(data_width-1):0]         q_b;
1462 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1463 100 unneback
    parameter memory_init = 0;
1464
    parameter memory_file = "vl_ram.vmem";
1465
    parameter debug = 0;
1466
    generate
1467
    if (memory_init == 1) begin : init_mem
1468
        initial
1469
            $readmemh(memory_file, ram);
1470
    end else if (memory_init == 2) begin : init_zero
1471
        integer k;
1472
        initial
1473
            for (k = 0; k < mem_size; k = k + 1)
1474
                ram[k] = 0;
1475
    end
1476 7 unneback
   endgenerate
1477 100 unneback
    generate
1478
    if (debug==1) begin : debug_we
1479
        always @ (posedge clk_a)
1480
        if (we_a)
1481
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1482
    end
1483
    endgenerate
1484 6 unneback
   always @ (posedge clk_a)
1485
     begin
1486
        q_a <= ram[adr_a];
1487
        if (we_a)
1488
             ram[adr_a] <= d_a;
1489
     end
1490
   always @ (posedge clk_b)
1491
          q_b <= ram[adr_b];
1492
endmodule
1493 100 unneback
module vl_dpram_1r2w ( d_a, q_a, adr_a, we_a, clk_a, d_b, adr_b, we_b, clk_b );
1494
   parameter data_width = 32;
1495
   parameter addr_width = 8;
1496
   parameter mem_size = 1<<addr_width;
1497
   input [(data_width-1):0]      d_a;
1498
   input [(addr_width-1):0]       adr_a;
1499
   input [(addr_width-1):0]       adr_b;
1500
   input                         we_a;
1501
   input [(data_width-1):0]       d_b;
1502
   output reg [(data_width-1):0] q_a;
1503
   input                         we_b;
1504
   input                         clk_a, clk_b;
1505
   reg [(data_width-1):0]         q_b;
1506 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1507 100 unneback
    parameter memory_init = 0;
1508
    parameter memory_file = "vl_ram.vmem";
1509
    parameter debug = 0;
1510
    generate
1511
    if (memory_init == 1) begin : init_mem
1512
        initial
1513
            $readmemh(memory_file, ram);
1514
    end else if (memory_init == 2) begin : init_zero
1515
        integer k;
1516
        initial
1517
            for (k = 0; k < mem_size; k = k + 1)
1518
                ram[k] = 0;
1519
    end
1520
   endgenerate
1521
    generate
1522
    if (debug==1) begin : debug_we
1523
        always @ (posedge clk_a)
1524
        if (we_a)
1525
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1526
        always @ (posedge clk_b)
1527
        if (we_b)
1528
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1529
    end
1530
    endgenerate
1531
   always @ (posedge clk_a)
1532
     begin
1533
        q_a <= ram[adr_a];
1534
        if (we_a)
1535
             ram[adr_a] <= d_a;
1536
     end
1537
   always @ (posedge clk_b)
1538
     begin
1539
        if (we_b)
1540
          ram[adr_b] <= d_b;
1541
     end
1542
endmodule
1543 7 unneback
module vl_dpram_2r2w ( d_a, q_a, adr_a, we_a, clk_a, d_b, q_b, adr_b, we_b, clk_b );
1544 6 unneback
   parameter data_width = 32;
1545
   parameter addr_width = 8;
1546 75 unneback
   parameter mem_size = 1<<addr_width;
1547 6 unneback
   input [(data_width-1):0]      d_a;
1548
   input [(addr_width-1):0]       adr_a;
1549
   input [(addr_width-1):0]       adr_b;
1550
   input                         we_a;
1551
   output [(data_width-1):0]      q_b;
1552
   input [(data_width-1):0]       d_b;
1553
   output reg [(data_width-1):0] q_a;
1554
   input                         we_b;
1555
   input                         clk_a, clk_b;
1556
   reg [(data_width-1):0]         q_b;
1557 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1558 100 unneback
    parameter memory_init = 0;
1559
    parameter memory_file = "vl_ram.vmem";
1560
    parameter debug = 0;
1561
    generate
1562
    if (memory_init) begin : init_mem
1563
        initial
1564
            $readmemh(memory_file, ram);
1565
    end else if (memory_init == 2) begin : init_zero
1566
        integer k;
1567
        initial
1568
            for (k = 0; k < mem_size; k = k + 1)
1569
                ram[k] = 0;
1570
    end
1571 7 unneback
   endgenerate
1572 100 unneback
    generate
1573
    if (debug==1) begin : debug_we
1574
        always @ (posedge clk_a)
1575
        if (we_a)
1576
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1577
        always @ (posedge clk_b)
1578
        if (we_b)
1579
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1580
    end
1581
    endgenerate
1582 6 unneback
   always @ (posedge clk_a)
1583
     begin
1584
        q_a <= ram[adr_a];
1585
        if (we_a)
1586
             ram[adr_a] <= d_a;
1587
     end
1588
   always @ (posedge clk_b)
1589
     begin
1590
        q_b <= ram[adr_b];
1591
        if (we_b)
1592
          ram[adr_b] <= d_b;
1593
     end
1594
endmodule
1595 92 unneback
module vl_dpram_be_2r2w ( d_a, q_a, adr_a, be_a, we_a, clk_a, d_b, q_b, adr_b, be_b, we_b, clk_b );
1596 75 unneback
   parameter a_data_width = 32;
1597
   parameter a_addr_width = 8;
1598 95 unneback
   parameter b_data_width = 64; //a_data_width;
1599 91 unneback
   localparam b_addr_width = a_data_width * a_addr_width / b_data_width;
1600 95 unneback
   localparam ratio = (a_addr_width>b_addr_width) ? (a_addr_width/b_addr_width) : (b_addr_width/a_addr_width);
1601
   parameter mem_size = (a_addr_width>b_addr_width) ? (1<<b_addr_width) : (1<<a_addr_width);
1602 100 unneback
   parameter memory_init = 0;
1603 95 unneback
   parameter memory_file = "vl_ram.vmem";
1604 100 unneback
   parameter debug = 0;
1605 75 unneback
   input [(a_data_width-1):0]      d_a;
1606 91 unneback
   input [(a_addr_width-1):0]       adr_a;
1607
   input [(a_data_width/8-1):0]    be_a;
1608
   input                           we_a;
1609 75 unneback
   output reg [(a_data_width-1):0] q_a;
1610 91 unneback
   input [(b_data_width-1):0]       d_b;
1611
   input [(b_addr_width-1):0]       adr_b;
1612 92 unneback
   input [(b_data_width/8-1):0]    be_b;
1613
   input                           we_b;
1614
   output reg [(b_data_width-1):0]          q_b;
1615 91 unneback
   input                           clk_a, clk_b;
1616 100 unneback
    generate
1617
    if (debug==1) begin : debug_we
1618
        always @ (posedge clk_a)
1619
        if (we_a)
1620
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1621
        always @ (posedge clk_b)
1622
        if (we_b)
1623
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1624
    end
1625
    endgenerate
1626 91 unneback
`ifdef SYSTEMVERILOG
1627
// use a multi-dimensional packed array
1628
//to model individual bytes within the word
1629 75 unneback
generate
1630 91 unneback
if (a_data_width==32 & b_data_width==32) begin : dpram_3232
1631 98 unneback
    logic [0:3][7:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1632 95 unneback
    initial
1633 100 unneback
        if (memory_init==1)
1634 95 unneback
            $readmemh(memory_file, ram);
1635 100 unneback
    integer k;
1636
    initial
1637
        if (memory_init==2)
1638
            for (k = 0; k < mem_size; k = k + 1)
1639
                ram[k] = 0;
1640 91 unneback
    always_ff@(posedge clk_a)
1641
    begin
1642
        if(we_a) begin
1643 100 unneback
            if(be_a[3]) ram[adr_a][0] <= d_a[31:24];
1644
            if(be_a[2]) ram[adr_a][1] <= d_a[23:16];
1645
            if(be_a[1]) ram[adr_a][2] <= d_a[15:8];
1646
            if(be_a[0]) ram[adr_a][3] <= d_a[7:0];
1647 91 unneback
        end
1648
    end
1649 92 unneback
    always@(posedge clk_a)
1650
        q_a = ram[adr_a];
1651 91 unneback
    always_ff@(posedge clk_b)
1652 92 unneback
    begin
1653
        if(we_b) begin
1654 100 unneback
            if(be_b[3]) ram[adr_b][0] <= d_b[31:24];
1655
            if(be_b[2]) ram[adr_b][1] <= d_b[23:16];
1656
            if(be_b[1]) ram[adr_b][2] <= d_b[15:8];
1657
            if(be_b[0]) ram[adr_b][3] <= d_b[7:0];
1658 92 unneback
        end
1659
    end
1660
    always@(posedge clk_b)
1661
        q_b = ram[adr_b];
1662 75 unneback
end
1663
endgenerate
1664 95 unneback
generate
1665
if (a_data_width==64 & b_data_width==64) begin : dpram_6464
1666 98 unneback
    logic [0:7][7:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1667 95 unneback
    initial
1668 100 unneback
        if (memory_init==1)
1669 95 unneback
            $readmemh(memory_file, ram);
1670 100 unneback
    integer k;
1671
    initial
1672
        if (memory_init==2)
1673
            for (k = 0; k < mem_size; k = k + 1)
1674
                ram[k] = 0;
1675 95 unneback
    always_ff@(posedge clk_a)
1676
    begin
1677
        if(we_a) begin
1678
            if(be_a[7]) ram[adr_a][7] <= d_a[63:56];
1679
            if(be_a[6]) ram[adr_a][6] <= d_a[55:48];
1680
            if(be_a[5]) ram[adr_a][5] <= d_a[47:40];
1681
            if(be_a[4]) ram[adr_a][4] <= d_a[39:32];
1682
            if(be_a[3]) ram[adr_a][3] <= d_a[31:24];
1683
            if(be_a[2]) ram[adr_a][2] <= d_a[23:16];
1684
            if(be_a[1]) ram[adr_a][1] <= d_a[15:8];
1685
            if(be_a[0]) ram[adr_a][0] <= d_a[7:0];
1686
        end
1687
    end
1688
    always@(posedge clk_a)
1689
        q_a = ram[adr_a];
1690
    always_ff@(posedge clk_b)
1691
    begin
1692
        if(we_b) begin
1693
            if(be_b[7]) ram[adr_b][7] <= d_b[63:56];
1694
            if(be_b[6]) ram[adr_b][6] <= d_b[55:48];
1695
            if(be_b[5]) ram[adr_b][5] <= d_b[47:40];
1696
            if(be_b[4]) ram[adr_b][4] <= d_b[39:32];
1697
            if(be_b[3]) ram[adr_b][3] <= d_b[31:24];
1698
            if(be_b[2]) ram[adr_b][2] <= d_b[23:16];
1699
            if(be_b[1]) ram[adr_b][1] <= d_b[15:8];
1700
            if(be_b[0]) ram[adr_b][0] <= d_b[7:0];
1701
        end
1702
    end
1703
    always@(posedge clk_b)
1704
        q_b = ram[adr_b];
1705
end
1706
endgenerate
1707
generate
1708
if (a_data_width==32 & b_data_width==16) begin : dpram_3216
1709
logic [31:0] temp;
1710 111 unneback
vl_dpram_be_2r2w # (.a_data_width(64), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
1711 95 unneback
dpram6464 (
1712
    .d_a(d_a),
1713
    .q_a(q_a),
1714
    .adr_a(adr_a),
1715
    .be_a(be_a),
1716
    .we_a(we_a),
1717
    .clk_a(clk_a),
1718
    .d_b({d_b,d_b}),
1719
    .q_b(temp),
1720
    .adr_b(adr_b),
1721
    .be_b({be_b,be_b} & {{2{adr_b[0]}},{2{!adr_b[0]}}}),
1722
    .we_b(we_b),
1723
    .clk_b(clk_b)
1724
);
1725 100 unneback
always @ (adr_b[0] or temp)
1726 95 unneback
    if (adr_b[0])
1727
        q_b = temp[31:16];
1728
    else
1729
        q_b = temp[15:0];
1730
end
1731
endgenerate
1732
generate
1733
if (a_data_width==32 & b_data_width==64) begin : dpram_3264
1734
logic [63:0] temp;
1735 111 unneback
vl_dpram_be_2r2w # (.a_data_width(64), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
1736 95 unneback
dpram6464 (
1737
    .d_a({d_a,d_a}),
1738
    .q_a(temp),
1739
    .adr_a(adr_a[a_addr_width-1:1]),
1740
    .be_a({be_a,be_a} & {{4{adr_a[0]}},{4{!adr_a[0]}}}),
1741
    .we_a(we_a),
1742
    .clk_a(clk_a),
1743
    .d_b(d_b),
1744
    .q_b(q_b),
1745
    .adr_b(adr_b),
1746
    .be_b(be_b),
1747
    .we_b(we_b),
1748
    .clk_b(clk_b)
1749
);
1750 100 unneback
always @ (adr_a[0] or temp)
1751 95 unneback
    if (adr_a[0])
1752
        q_a = temp[63:32];
1753
    else
1754
        q_a = temp[31:0];
1755
end
1756
endgenerate
1757 91 unneback
`else
1758 92 unneback
    // This modules requires SystemVerilog
1759 98 unneback
    // at this point anyway
1760 91 unneback
`endif
1761 75 unneback
endmodule
1762 6 unneback
// FIFO
1763 25 unneback
module vl_fifo_1r1w_fill_level_sync (
1764
    d, wr, fifo_full,
1765
    q, rd, fifo_empty,
1766
    fill_level,
1767
    clk, rst
1768
    );
1769
parameter data_width = 18;
1770
parameter addr_width = 4;
1771
// write side
1772
input  [data_width-1:0] d;
1773
input                   wr;
1774
output                  fifo_full;
1775
// read side
1776
output [data_width-1:0] q;
1777
input                   rd;
1778
output                  fifo_empty;
1779
// common
1780
output [addr_width:0]   fill_level;
1781
input rst, clk;
1782
wire [addr_width:1] wadr, radr;
1783
vl_cnt_bin_ce
1784
    # ( .length(addr_width))
1785
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
1786
vl_cnt_bin_ce
1787
    # (.length(addr_width))
1788
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
1789
vl_dpram_1r1w
1790
    # (.data_width(data_width), .addr_width(addr_width))
1791
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
1792 31 unneback
vl_cnt_bin_ce_rew_q_zq_l1
1793 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
1794 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
1795
endmodule
1796 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
1797
// RAM is supposed to be larger than the two FIFOs
1798
// LFSR counters used adr pointers
1799
module vl_fifo_2r2w_sync_simplex (
1800
    // a side
1801
    a_d, a_wr, a_fifo_full,
1802
    a_q, a_rd, a_fifo_empty,
1803
    a_fill_level,
1804
    // b side
1805
    b_d, b_wr, b_fifo_full,
1806
    b_q, b_rd, b_fifo_empty,
1807
    b_fill_level,
1808
    // common
1809
    clk, rst
1810
    );
1811
parameter data_width = 8;
1812
parameter addr_width = 5;
1813
parameter fifo_full_level = (1<<addr_width)-1;
1814
// a side
1815
input  [data_width-1:0] a_d;
1816
input                   a_wr;
1817
output                  a_fifo_full;
1818
output [data_width-1:0] a_q;
1819
input                   a_rd;
1820
output                  a_fifo_empty;
1821
output [addr_width-1:0] a_fill_level;
1822
// b side
1823
input  [data_width-1:0] b_d;
1824
input                   b_wr;
1825
output                  b_fifo_full;
1826
output [data_width-1:0] b_q;
1827
input                   b_rd;
1828
output                  b_fifo_empty;
1829
output [addr_width-1:0] b_fill_level;
1830
input                   clk;
1831
input                   rst;
1832
// adr_gen
1833
wire [addr_width:1] a_wadr, a_radr;
1834
wire [addr_width:1] b_wadr, b_radr;
1835
// dpram
1836
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
1837
vl_cnt_lfsr_ce
1838
    # ( .length(addr_width))
1839
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
1840
vl_cnt_lfsr_ce
1841
    # (.length(addr_width))
1842
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
1843
vl_cnt_lfsr_ce
1844
    # ( .length(addr_width))
1845
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
1846
vl_cnt_lfsr_ce
1847
    # (.length(addr_width))
1848
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
1849
// mux read or write adr to DPRAM
1850
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
1851
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
1852
vl_dpram_2r2w
1853
    # (.data_width(data_width), .addr_width(addr_width+1))
1854
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
1855
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
1856
vl_cnt_bin_ce_rew_zq_l1
1857 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1858 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));
1859
vl_cnt_bin_ce_rew_zq_l1
1860 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1861 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));
1862
endmodule
1863 6 unneback
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
1864 11 unneback
   parameter addr_width = 4;
1865
   parameter N = addr_width-1;
1866 6 unneback
   parameter Q1 = 2'b00;
1867
   parameter Q2 = 2'b01;
1868
   parameter Q3 = 2'b11;
1869
   parameter Q4 = 2'b10;
1870
   parameter going_empty = 1'b0;
1871
   parameter going_full  = 1'b1;
1872
   input [N:0]  wptr, rptr;
1873 14 unneback
   output       fifo_empty;
1874 6 unneback
   output       fifo_full;
1875
   input        wclk, rclk, rst;
1876
   wire direction;
1877
   reg  direction_set, direction_clr;
1878
   wire async_empty, async_full;
1879
   wire fifo_full2;
1880 14 unneback
   wire fifo_empty2;
1881 6 unneback
   // direction_set
1882
   always @ (wptr[N:N-1] or rptr[N:N-1])
1883
     case ({wptr[N:N-1],rptr[N:N-1]})
1884
       {Q1,Q2} : direction_set <= 1'b1;
1885
       {Q2,Q3} : direction_set <= 1'b1;
1886
       {Q3,Q4} : direction_set <= 1'b1;
1887
       {Q4,Q1} : direction_set <= 1'b1;
1888
       default : direction_set <= 1'b0;
1889
     endcase
1890
   // direction_clear
1891
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
1892
     if (rst)
1893
       direction_clr <= 1'b1;
1894
     else
1895
       case ({wptr[N:N-1],rptr[N:N-1]})
1896
         {Q2,Q1} : direction_clr <= 1'b1;
1897
         {Q3,Q2} : direction_clr <= 1'b1;
1898
         {Q4,Q3} : direction_clr <= 1'b1;
1899
         {Q1,Q4} : direction_clr <= 1'b1;
1900
         default : direction_clr <= 1'b0;
1901
       endcase
1902 18 unneback
    vl_dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
1903 6 unneback
   assign async_empty = (wptr == rptr) && (direction==going_empty);
1904
   assign async_full  = (wptr == rptr) && (direction==going_full);
1905 18 unneback
    vl_dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
1906
    vl_dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
1907 6 unneback
/*
1908
   always @ (posedge wclk or posedge rst or posedge async_full)
1909
     if (rst)
1910
       {fifo_full, fifo_full2} <= 2'b00;
1911
     else if (async_full)
1912
       {fifo_full, fifo_full2} <= 2'b11;
1913
     else
1914
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
1915
*/
1916 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
1917 6 unneback
     if (async_empty)
1918
       {fifo_empty, fifo_empty2} <= 2'b11;
1919
     else
1920 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
1921 18 unneback
    vl_dff # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
1922
    vl_dff # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
1923 27 unneback
endmodule // async_compb
1924 6 unneback
module vl_fifo_1r1w_async (
1925
    d, wr, fifo_full, wr_clk, wr_rst,
1926
    q, rd, fifo_empty, rd_clk, rd_rst
1927
    );
1928
parameter data_width = 18;
1929
parameter addr_width = 4;
1930
// write side
1931
input  [data_width-1:0] d;
1932
input                   wr;
1933
output                  fifo_full;
1934
input                   wr_clk;
1935
input                   wr_rst;
1936
// read side
1937
output [data_width-1:0] q;
1938
input                   rd;
1939
output                  fifo_empty;
1940
input                   rd_clk;
1941
input                   rd_rst;
1942
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
1943 18 unneback
vl_cnt_gray_ce_bin
1944 6 unneback
    # ( .length(addr_width))
1945
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
1946 18 unneback
vl_cnt_gray_ce_bin
1947 6 unneback
    # (.length(addr_width))
1948 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
1949 7 unneback
vl_dpram_1r1w
1950 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
1951
    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));
1952
vl_fifo_cmp_async
1953
    # (.addr_width(addr_width))
1954
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
1955
endmodule
1956 8 unneback
module vl_fifo_2r2w_async (
1957 6 unneback
    // a side
1958
    a_d, a_wr, a_fifo_full,
1959
    a_q, a_rd, a_fifo_empty,
1960
    a_clk, a_rst,
1961
    // b side
1962
    b_d, b_wr, b_fifo_full,
1963
    b_q, b_rd, b_fifo_empty,
1964
    b_clk, b_rst
1965
    );
1966
parameter data_width = 18;
1967
parameter addr_width = 4;
1968
// a side
1969
input  [data_width-1:0] a_d;
1970
input                   a_wr;
1971
output                  a_fifo_full;
1972
output [data_width-1:0] a_q;
1973
input                   a_rd;
1974
output                  a_fifo_empty;
1975
input                   a_clk;
1976
input                   a_rst;
1977
// b side
1978
input  [data_width-1:0] b_d;
1979
input                   b_wr;
1980
output                  b_fifo_full;
1981
output [data_width-1:0] b_q;
1982
input                   b_rd;
1983
output                  b_fifo_empty;
1984
input                   b_clk;
1985
input                   b_rst;
1986
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1987
vl_fifo_1r1w_async_a (
1988
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
1989
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
1990
    );
1991
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1992
vl_fifo_1r1w_async_b (
1993
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
1994
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
1995
    );
1996
endmodule
1997 8 unneback
module vl_fifo_2r2w_async_simplex (
1998 6 unneback
    // a side
1999
    a_d, a_wr, a_fifo_full,
2000
    a_q, a_rd, a_fifo_empty,
2001
    a_clk, a_rst,
2002
    // b side
2003
    b_d, b_wr, b_fifo_full,
2004
    b_q, b_rd, b_fifo_empty,
2005
    b_clk, b_rst
2006
    );
2007
parameter data_width = 18;
2008
parameter addr_width = 4;
2009
// a side
2010
input  [data_width-1:0] a_d;
2011
input                   a_wr;
2012
output                  a_fifo_full;
2013
output [data_width-1:0] a_q;
2014
input                   a_rd;
2015
output                  a_fifo_empty;
2016
input                   a_clk;
2017
input                   a_rst;
2018
// b side
2019
input  [data_width-1:0] b_d;
2020
input                   b_wr;
2021
output                  b_fifo_full;
2022
output [data_width-1:0] b_q;
2023
input                   b_rd;
2024
output                  b_fifo_empty;
2025
input                   b_clk;
2026
input                   b_rst;
2027
// adr_gen
2028
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
2029
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
2030
// dpram
2031
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
2032 18 unneback
vl_cnt_gray_ce_bin
2033 6 unneback
    # ( .length(addr_width))
2034
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
2035 18 unneback
vl_cnt_gray_ce_bin
2036 6 unneback
    # (.length(addr_width))
2037
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
2038 18 unneback
vl_cnt_gray_ce_bin
2039 6 unneback
    # ( .length(addr_width))
2040
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
2041 18 unneback
vl_cnt_gray_ce_bin
2042 6 unneback
    # (.length(addr_width))
2043
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
2044
// mux read or write adr to DPRAM
2045
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
2046
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
2047 11 unneback
vl_dpram_2r2w
2048 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
2049
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
2050
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
2051 11 unneback
vl_fifo_cmp_async
2052 6 unneback
    # (.addr_width(addr_width))
2053
    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) );
2054 11 unneback
vl_fifo_cmp_async
2055 6 unneback
    # (.addr_width(addr_width))
2056
    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) );
2057
endmodule
2058 48 unneback
module vl_reg_file (
2059
    a1, a2, a3, wd3, we3, rd1, rd2, clk
2060
);
2061
parameter data_width = 32;
2062
parameter addr_width = 5;
2063
input [addr_width-1:0] a1, a2, a3;
2064
input [data_width-1:0] wd3;
2065
input we3;
2066
output [data_width-1:0] rd1, rd2;
2067
input clk;
2068
reg [data_width-1:0] wd3_reg;
2069
reg [addr_width-1:0] a1_reg, a2_reg, a3_reg;
2070
reg we3_reg;
2071
reg [data_width-1:0] ram1 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
2072
reg [data_width-1:0] ram2 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
2073
always @ (posedge clk or posedge rst)
2074
if (rst)
2075
    {wd3_reg, a3_reg, we3_reg} <= {(data_width+addr_width+1){1'b0}};
2076
else
2077
    {wd3_reg, a3_reg, we3_reg} <= {wd3,a3,wd3};
2078
    always @ (negedge clk)
2079
    if (we3_reg)
2080
        ram1[a3_reg] <= wd3;
2081
    always @ (posedge clk)
2082
        a1_reg <= a1;
2083
    assign rd1 = ram1[a1_reg];
2084
    always @ (negedge clk)
2085
    if (we3_reg)
2086
        ram2[a3_reg] <= wd3;
2087
    always @ (posedge clk)
2088
        a2_reg <= a2;
2089
    assign rd2 = ram2[a2_reg];
2090
endmodule
2091 12 unneback
//////////////////////////////////////////////////////////////////////
2092
////                                                              ////
2093
////  Versatile library, wishbone stuff                           ////
2094
////                                                              ////
2095
////  Description                                                 ////
2096
////  Wishbone compliant modules                                  ////
2097
////                                                              ////
2098
////                                                              ////
2099
////  To Do:                                                      ////
2100
////   -                                                          ////
2101
////                                                              ////
2102
////  Author(s):                                                  ////
2103
////      - Michael Unneback, unneback@opencores.org              ////
2104
////        ORSoC AB                                              ////
2105
////                                                              ////
2106
//////////////////////////////////////////////////////////////////////
2107
////                                                              ////
2108
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2109
////                                                              ////
2110
//// This source file may be used and distributed without         ////
2111
//// restriction provided that this copyright statement is not    ////
2112
//// removed from the file and that any derivative work contains  ////
2113
//// the original copyright notice and the associated disclaimer. ////
2114
////                                                              ////
2115
//// This source file is free software; you can redistribute it   ////
2116
//// and/or modify it under the terms of the GNU Lesser General   ////
2117
//// Public License as published by the Free Software Foundation; ////
2118
//// either version 2.1 of the License, or (at your option) any   ////
2119
//// later version.                                               ////
2120
////                                                              ////
2121
//// This source is distributed in the hope that it will be       ////
2122
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2123
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2124
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2125
//// details.                                                     ////
2126
////                                                              ////
2127
//// You should have received a copy of the GNU Lesser General    ////
2128
//// Public License along with this source; if not, download it   ////
2129
//// from http://www.opencores.org/lgpl.shtml                     ////
2130
////                                                              ////
2131
//////////////////////////////////////////////////////////////////////
2132
`timescale 1ns/1ns
2133 85 unneback
module vl_wb_adr_inc ( cyc_i, stb_i, cti_i, bte_i, adr_i, we_i, ack_o, adr_o, clk, rst);
2134 83 unneback
parameter adr_width = 10;
2135
parameter max_burst_width = 4;
2136 85 unneback
input cyc_i, stb_i, we_i;
2137 83 unneback
input [2:0] cti_i;
2138
input [1:0] bte_i;
2139
input [adr_width-1:0] adr_i;
2140
output [adr_width-1:0] adr_o;
2141
output ack_o;
2142
input clk, rst;
2143
reg [adr_width-1:0] adr;
2144 90 unneback
wire [max_burst_width-1:0] to_adr;
2145 91 unneback
reg [max_burst_width-1:0] last_adr;
2146 92 unneback
reg last_cycle;
2147
localparam idle_or_eoc = 1'b0;
2148
localparam cyc_or_ws   = 1'b1;
2149 91 unneback
always @ (posedge clk or posedge rst)
2150
if (rst)
2151
    last_adr <= {max_burst_width{1'b0}};
2152
else
2153
    if (stb_i)
2154 92 unneback
        last_adr <=adr_o[max_burst_width-1:0];
2155 83 unneback
generate
2156
if (max_burst_width==0) begin : inst_0
2157 97 unneback
        reg ack_o;
2158
        assign adr_o = adr_i;
2159
        always @ (posedge clk or posedge rst)
2160
        if (rst)
2161
            ack_o <= 1'b0;
2162
        else
2163
            ack_o <= cyc_i & stb_i & !ack_o;
2164 83 unneback
end else begin
2165
    always @ (posedge clk or posedge rst)
2166
    if (rst)
2167 92 unneback
        last_cycle <= idle_or_eoc;
2168 83 unneback
    else
2169 92 unneback
        last_cycle <= (!cyc_i) ? idle_or_eoc : //idle
2170
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? idle_or_eoc : // eoc
2171
                      (cyc_i & !stb_i) ? cyc_or_ws : //ws
2172
                      cyc_or_ws; // cyc
2173
    assign to_adr = (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
2174 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
2175 91 unneback
                                        (!stb_i) ? last_adr :
2176 92 unneback
                                        (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] :
2177 85 unneback
                                        adr[max_burst_width-1:0];
2178 92 unneback
    assign ack_o = (last_cycle==cyc_or_ws) & stb_i;
2179 83 unneback
end
2180
endgenerate
2181
generate
2182
if (max_burst_width==2) begin : inst_2
2183
    always @ (posedge clk or posedge rst)
2184
    if (rst)
2185
        adr <= 2'h0;
2186
    else
2187
        if (cyc_i & stb_i)
2188
            adr[1:0] <= to_adr[1:0] + 2'd1;
2189
        else
2190
            adr <= to_adr[1:0];
2191
end
2192
endgenerate
2193
generate
2194
if (max_burst_width==3) begin : inst_3
2195
    always @ (posedge clk or posedge rst)
2196
    if (rst)
2197
        adr <= 3'h0;
2198
    else
2199
        if (cyc_i & stb_i)
2200
            case (bte_i)
2201
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
2202
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
2203
            endcase
2204
        else
2205
            adr <= to_adr[2:0];
2206
end
2207
endgenerate
2208
generate
2209
if (max_burst_width==4) begin : inst_4
2210
    always @ (posedge clk or posedge rst)
2211
    if (rst)
2212
        adr <= 4'h0;
2213
    else
2214 91 unneback
        if (stb_i) // | (!stb_i & last_cycle!=ws)) // for !stb_i restart with adr_i +1, only inc once
2215 83 unneback
            case (bte_i)
2216
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
2217
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
2218
            default: adr[3:0] <= to_adr + 4'd1;
2219
            endcase
2220
        else
2221
            adr <= to_adr[3:0];
2222
end
2223
endgenerate
2224
generate
2225
if (adr_width > max_burst_width) begin : pass_through
2226
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
2227
end
2228
endgenerate
2229
endmodule
2230
// async wb3 - wb3 bridge
2231
`timescale 1ns/1ns
2232 18 unneback
module vl_wb3wb3_bridge (
2233 12 unneback
        // wishbone slave side
2234
        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,
2235
        // wishbone master side
2236
        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);
2237 95 unneback
parameter style = "FIFO"; // valid: simple, FIFO
2238
parameter addr_width = 4;
2239 12 unneback
input [31:0] wbs_dat_i;
2240
input [31:2] wbs_adr_i;
2241
input [3:0]  wbs_sel_i;
2242
input [1:0]  wbs_bte_i;
2243
input [2:0]  wbs_cti_i;
2244
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
2245
output [31:0] wbs_dat_o;
2246 14 unneback
output wbs_ack_o;
2247 12 unneback
input wbs_clk, wbs_rst;
2248
output [31:0] wbm_dat_o;
2249
output reg [31:2] wbm_adr_o;
2250
output [3:0]  wbm_sel_o;
2251
output reg [1:0]  wbm_bte_o;
2252
output reg [2:0]  wbm_cti_o;
2253 14 unneback
output reg wbm_we_o;
2254
output wbm_cyc_o;
2255 12 unneback
output wbm_stb_o;
2256
input [31:0]  wbm_dat_i;
2257
input wbm_ack_i;
2258
input wbm_clk, wbm_rst;
2259
// bte
2260
parameter linear       = 2'b00;
2261
parameter wrap4        = 2'b01;
2262
parameter wrap8        = 2'b10;
2263
parameter wrap16       = 2'b11;
2264
// cti
2265
parameter classic      = 3'b000;
2266
parameter incburst     = 3'b010;
2267
parameter endofburst   = 3'b111;
2268 95 unneback
localparam wbs_adr  = 1'b0;
2269
localparam wbs_data = 1'b1;
2270
localparam wbm_adr0      = 2'b00;
2271
localparam wbm_adr1      = 2'b01;
2272
localparam wbm_data      = 2'b10;
2273
localparam wbm_data_wait = 2'b11;
2274 12 unneback
reg [1:0] wbs_bte_reg;
2275
reg wbs;
2276
wire wbs_eoc_alert, wbm_eoc_alert;
2277
reg wbs_eoc, wbm_eoc;
2278
reg [1:0] wbm;
2279 14 unneback
wire [1:16] wbs_count, wbm_count;
2280 12 unneback
wire [35:0] a_d, a_q, b_d, b_q;
2281
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
2282
reg a_rd_reg;
2283
wire b_rd_adr, b_rd_data;
2284 14 unneback
wire b_rd_data_reg;
2285
wire [35:0] temp;
2286 12 unneback
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]);
2287
always @ (posedge wbs_clk or posedge wbs_rst)
2288
if (wbs_rst)
2289
        wbs_eoc <= 1'b0;
2290
else
2291
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
2292 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
2293 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
2294
                wbs_eoc <= 1'b1;
2295 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
2296 12 unneback
    cnt0 (
2297
        .cke(wbs_ack_o),
2298
        .clear(wbs_eoc),
2299
        .q(wbs_count),
2300
        .rst(wbs_rst),
2301
        .clk(wbs_clk));
2302
always @ (posedge wbs_clk or posedge wbs_rst)
2303
if (wbs_rst)
2304
        wbs <= wbs_adr;
2305
else
2306 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
2307 12 unneback
                wbs <= wbs_data;
2308
        else if (wbs_eoc & wbs_ack_o)
2309
                wbs <= wbs_adr;
2310
// wbs FIFO
2311 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};
2312
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
2313 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
2314
              1'b0;
2315
assign a_rd = !a_fifo_empty;
2316
always @ (posedge wbs_clk or posedge wbs_rst)
2317
if (wbs_rst)
2318
        a_rd_reg <= 1'b0;
2319
else
2320
        a_rd_reg <= a_rd;
2321
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
2322
assign wbs_dat_o = a_q[35:4];
2323
always @ (posedge wbs_clk or posedge wbs_rst)
2324
if (wbs_rst)
2325 13 unneback
        wbs_bte_reg <= 2'b00;
2326 12 unneback
else
2327 13 unneback
        wbs_bte_reg <= wbs_bte_i;
2328 12 unneback
// wbm FIFO
2329
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]);
2330
always @ (posedge wbm_clk or posedge wbm_rst)
2331
if (wbm_rst)
2332
        wbm_eoc <= 1'b0;
2333
else
2334
        if (wbm==wbm_adr0 & !b_fifo_empty)
2335
                wbm_eoc <= b_q[4:3] == linear;
2336
        else if (wbm_eoc_alert & wbm_ack_i)
2337
                wbm_eoc <= 1'b1;
2338
always @ (posedge wbm_clk or posedge wbm_rst)
2339
if (wbm_rst)
2340
        wbm <= wbm_adr0;
2341
else
2342 33 unneback
/*
2343 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
2344
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
2345
        (wbm==wbm_adr1 & !wbm_we_o) |
2346
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
2347
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
2348 33 unneback
*/
2349
    case (wbm)
2350
    wbm_adr0:
2351
        if (!b_fifo_empty)
2352
            wbm <= wbm_adr1;
2353
    wbm_adr1:
2354
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
2355
            wbm <= wbm_data;
2356
    wbm_data:
2357
        if (wbm_ack_i & wbm_eoc)
2358
            wbm <= wbm_adr0;
2359
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
2360
            wbm <= wbm_data_wait;
2361
    wbm_data_wait:
2362
        if (!b_fifo_empty)
2363
            wbm <= wbm_data;