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 101

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
output clk, rst;
425
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 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
1199 6 unneback
   parameter length = 4;
1200
   input cke, 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 (cke)
1209
            if (clear)
1210
                q <= {1'b1,{length-1{1'b0}}};
1211
            else
1212
                q <= q >> 1;
1213
endmodule
1214 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
1215 6 unneback
   parameter length = 4;
1216
   input cke, clear;
1217
   output reg [0:length-1] q;
1218
   input rst;
1219
   input clk;
1220
    always @ (posedge clk or posedge rst)
1221
    if (rst)
1222
        q <= {1'b1,{length-1{1'b0}}};
1223
    else
1224
        if (cke)
1225
            if (clear)
1226
                q <= {1'b1,{length-1{1'b0}}};
1227
            else
1228
            q <= {q[length-1],q[0:length-2]};
1229
endmodule
1230
//////////////////////////////////////////////////////////////////////
1231
////                                                              ////
1232
////  Versatile library, memories                                 ////
1233
////                                                              ////
1234
////  Description                                                 ////
1235
////  memories                                                    ////
1236
////                                                              ////
1237
////                                                              ////
1238
////  To Do:                                                      ////
1239
////   - add more memory types                                    ////
1240
////                                                              ////
1241
////  Author(s):                                                  ////
1242
////      - Michael Unneback, unneback@opencores.org              ////
1243
////        ORSoC AB                                              ////
1244
////                                                              ////
1245
//////////////////////////////////////////////////////////////////////
1246
////                                                              ////
1247
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1248
////                                                              ////
1249
//// This source file may be used and distributed without         ////
1250
//// restriction provided that this copyright statement is not    ////
1251
//// removed from the file and that any derivative work contains  ////
1252
//// the original copyright notice and the associated disclaimer. ////
1253
////                                                              ////
1254
//// This source file is free software; you can redistribute it   ////
1255
//// and/or modify it under the terms of the GNU Lesser General   ////
1256
//// Public License as published by the Free Software Foundation; ////
1257
//// either version 2.1 of the License, or (at your option) any   ////
1258
//// later version.                                               ////
1259
////                                                              ////
1260
//// This source is distributed in the hope that it will be       ////
1261
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1262
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1263
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1264
//// details.                                                     ////
1265
////                                                              ////
1266
//// You should have received a copy of the GNU Lesser General    ////
1267
//// Public License along with this source; if not, download it   ////
1268
//// from http://www.opencores.org/lgpl.shtml                     ////
1269
////                                                              ////
1270
//////////////////////////////////////////////////////////////////////
1271
/// ROM
1272 7 unneback
module vl_rom_init ( adr, q, clk);
1273
   parameter data_width = 32;
1274
   parameter addr_width = 8;
1275 75 unneback
   parameter mem_size = 1<<addr_width;
1276 7 unneback
   input [(addr_width-1):0]       adr;
1277
   output reg [(data_width-1):0] q;
1278
   input                         clk;
1279 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
1280 7 unneback
   parameter memory_file = "vl_rom.vmem";
1281
   initial
1282
     begin
1283
        $readmemh(memory_file, rom);
1284
     end
1285
   always @ (posedge clk)
1286
     q <= rom[adr];
1287
endmodule
1288 6 unneback
// Single port RAM
1289
module vl_ram ( d, adr, we, q, clk);
1290
   parameter data_width = 32;
1291
   parameter addr_width = 8;
1292 75 unneback
   parameter mem_size = 1<<addr_width;
1293 100 unneback
   parameter debug = 0;
1294 6 unneback
   input [(data_width-1):0]      d;
1295
   input [(addr_width-1):0]       adr;
1296
   input                         we;
1297 7 unneback
   output reg [(data_width-1):0] q;
1298 6 unneback
   input                         clk;
1299 98 unneback
   reg [data_width-1:0] ram [mem_size-1:0];
1300 100 unneback
    parameter memory_init = 0;
1301
    parameter memory_file = "vl_ram.vmem";
1302
    generate
1303
    if (memory_init == 1) begin : init_mem
1304
        initial
1305
            $readmemh(memory_file, ram);
1306
   end else if (memory_init == 2) begin : init_zero
1307
        integer k;
1308
        initial
1309
            for (k = 0; k < mem_size; k = k + 1)
1310
                ram[k] = 0;
1311 7 unneback
   end
1312
   endgenerate
1313 100 unneback
    generate
1314
    if (debug==1) begin : debug_we
1315
        always @ (posedge clk)
1316
        if (we)
1317
            $display ("Value %h written at address %h : time %t", d, adr, $time);
1318
    end
1319
    endgenerate
1320 6 unneback
   always @ (posedge clk)
1321
   begin
1322
   if (we)
1323
     ram[adr] <= d;
1324
   q <= ram[adr];
1325
   end
1326
endmodule
1327 91 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
1328 7 unneback
   parameter data_width = 32;
1329 72 unneback
   parameter addr_width = 6;
1330 75 unneback
   parameter mem_size = 1<<addr_width;
1331 7 unneback
   input [(data_width-1):0]      d;
1332
   input [(addr_width-1):0]       adr;
1333 73 unneback
   input [(data_width/8)-1:0]    be;
1334 7 unneback
   input                         we;
1335
   output reg [(data_width-1):0] q;
1336
   input                         clk;
1337 65 unneback
`ifdef SYSTEMVERILOG
1338 95 unneback
    // use a multi-dimensional packed array
1339
    //t o model individual bytes within the word
1340
    logic [data_width/8-1:0][7:0] ram [0:mem_size-1];// # words = 1 << address width
1341 65 unneback
`else
1342 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
1343
    wire [data_width/8-1:0] cke;
1344 65 unneback
`endif
1345 100 unneback
    parameter memory_init = 0;
1346
    parameter memory_file = "vl_ram.vmem";
1347
    generate
1348
    if (memory_init == 1) begin : init_mem
1349
        initial
1350
            $readmemh(memory_file, ram);
1351
    end else if (memory_init == 2) begin : init_zero
1352
        integer k;
1353
        initial
1354
            for (k = 0; k < mem_size; k = k + 1)
1355
                ram[k] = 0;
1356
    end
1357 7 unneback
   endgenerate
1358 60 unneback
`ifdef SYSTEMVERILOG
1359
always_ff@(posedge clk)
1360
begin
1361 95 unneback
    if(we) begin
1362 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
1363
        if(be[2]) ram[adr][2] <= d[23:16];
1364
        if(be[1]) ram[adr][1] <= d[15:8];
1365
        if(be[0]) ram[adr][0] <= d[7:0];
1366 60 unneback
    end
1367 90 unneback
        q <= ram[adr];
1368 60 unneback
end
1369
`else
1370 85 unneback
assign cke = {data_width/8{we}} & be;
1371 7 unneback
   genvar i;
1372 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
1373 7 unneback
      always @ (posedge clk)
1374 85 unneback
      if (cke[i])
1375 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
1376
   end
1377
   endgenerate
1378
   always @ (posedge clk)
1379
      q <= ram[adr];
1380 60 unneback
`endif
1381 93 unneback
`ifdef verilator
1382 85 unneback
   // Function to access RAM (for use by Verilator).
1383
   function [31:0] get_mem;
1384
      // verilator public
1385 90 unneback
      input [addr_width-1:0]             addr;
1386 85 unneback
      get_mem = ram[addr];
1387
   endfunction // get_mem
1388
   // Function to write RAM (for use by Verilator).
1389
   function set_mem;
1390
      // verilator public
1391 90 unneback
      input [addr_width-1:0]             addr;
1392
      input [data_width-1:0]             data;
1393 85 unneback
      ram[addr] = data;
1394
   endfunction // set_mem
1395 93 unneback
`endif
1396 7 unneback
endmodule
1397
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1398 6 unneback
   parameter data_width = 32;
1399
   parameter addr_width = 8;
1400 75 unneback
   parameter mem_size = 1<<addr_width;
1401 6 unneback
   input [(data_width-1):0]      d_a;
1402
   input [(addr_width-1):0]       adr_a;
1403
   input [(addr_width-1):0]       adr_b;
1404
   input                         we_a;
1405
   output [(data_width-1):0]      q_b;
1406
   input                         clk_a, clk_b;
1407
   reg [(addr_width-1):0]         adr_b_reg;
1408 100 unneback
   reg [data_width-1:0] ram [mem_size-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1409
    parameter memory_init = 0;
1410
    parameter memory_file = "vl_ram.vmem";
1411
    parameter debug = 0;
1412
    generate
1413
    if (memory_init == 1) begin : init_mem
1414
        initial
1415
            $readmemh(memory_file, ram);
1416
    end else if (memory_init == 2) begin : init_zero
1417
        integer k;
1418
        initial
1419
            for (k = 0; k < mem_size; k = k + 1)
1420
                ram[k] = 0;
1421
    end
1422 7 unneback
   endgenerate
1423 100 unneback
    generate
1424
    if (debug==1) begin : debug_we
1425
        always @ (posedge clk_a)
1426
        if (we_a)
1427
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1428
    end
1429
    endgenerate
1430 6 unneback
   always @ (posedge clk_a)
1431
   if (we_a)
1432
     ram[adr_a] <= d_a;
1433
   always @ (posedge clk_b)
1434
   adr_b_reg <= adr_b;
1435
   assign q_b = ram[adr_b_reg];
1436
endmodule
1437 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1438 6 unneback
   parameter data_width = 32;
1439
   parameter addr_width = 8;
1440 75 unneback
   parameter mem_size = 1<<addr_width;
1441 6 unneback
   input [(data_width-1):0]      d_a;
1442
   input [(addr_width-1):0]       adr_a;
1443
   input [(addr_width-1):0]       adr_b;
1444
   input                         we_a;
1445
   output [(data_width-1):0]      q_b;
1446
   output reg [(data_width-1):0] q_a;
1447
   input                         clk_a, clk_b;
1448
   reg [(data_width-1):0]         q_b;
1449 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1450 100 unneback
    parameter memory_init = 0;
1451
    parameter memory_file = "vl_ram.vmem";
1452
    parameter debug = 0;
1453
    generate
1454
    if (memory_init == 1) begin : init_mem
1455
        initial
1456
            $readmemh(memory_file, ram);
1457
    end else if (memory_init == 2) begin : init_zero
1458
        integer k;
1459
        initial
1460
            for (k = 0; k < mem_size; k = k + 1)
1461
                ram[k] = 0;
1462
    end
1463 7 unneback
   endgenerate
1464 100 unneback
    generate
1465
    if (debug==1) begin : debug_we
1466
        always @ (posedge clk_a)
1467
        if (we_a)
1468
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1469
    end
1470
    endgenerate
1471 6 unneback
   always @ (posedge clk_a)
1472
     begin
1473
        q_a <= ram[adr_a];
1474
        if (we_a)
1475
             ram[adr_a] <= d_a;
1476
     end
1477
   always @ (posedge clk_b)
1478
          q_b <= ram[adr_b];
1479
endmodule
1480 100 unneback
module vl_dpram_1r2w ( d_a, q_a, adr_a, we_a, clk_a, d_b, adr_b, we_b, clk_b );
1481
   parameter data_width = 32;
1482
   parameter addr_width = 8;
1483
   parameter mem_size = 1<<addr_width;
1484
   input [(data_width-1):0]      d_a;
1485
   input [(addr_width-1):0]       adr_a;
1486
   input [(addr_width-1):0]       adr_b;
1487
   input                         we_a;
1488
   input [(data_width-1):0]       d_b;
1489
   output reg [(data_width-1):0] q_a;
1490
   input                         we_b;
1491
   input                         clk_a, clk_b;
1492
   reg [(data_width-1):0]         q_b;
1493
   reg [data_width-1:0] ram [mem_size-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1494
    parameter memory_init = 0;
1495
    parameter memory_file = "vl_ram.vmem";
1496
    parameter debug = 0;
1497
    generate
1498
    if (memory_init == 1) begin : init_mem
1499
        initial
1500
            $readmemh(memory_file, ram);
1501
    end else if (memory_init == 2) begin : init_zero
1502
        integer k;
1503
        initial
1504
            for (k = 0; k < mem_size; k = k + 1)
1505
                ram[k] = 0;
1506
    end
1507
   endgenerate
1508
    generate
1509
    if (debug==1) begin : debug_we
1510
        always @ (posedge clk_a)
1511
        if (we_a)
1512
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1513
        always @ (posedge clk_b)
1514
        if (we_b)
1515
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1516
    end
1517
    endgenerate
1518
   always @ (posedge clk_a)
1519
     begin
1520
        q_a <= ram[adr_a];
1521
        if (we_a)
1522
             ram[adr_a] <= d_a;
1523
     end
1524
   always @ (posedge clk_b)
1525
     begin
1526
        if (we_b)
1527
          ram[adr_b] <= d_b;
1528
     end
1529
endmodule
1530 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 );
1531 6 unneback
   parameter data_width = 32;
1532
   parameter addr_width = 8;
1533 75 unneback
   parameter mem_size = 1<<addr_width;
1534 6 unneback
   input [(data_width-1):0]      d_a;
1535
   input [(addr_width-1):0]       adr_a;
1536
   input [(addr_width-1):0]       adr_b;
1537
   input                         we_a;
1538
   output [(data_width-1):0]      q_b;
1539
   input [(data_width-1):0]       d_b;
1540
   output reg [(data_width-1):0] q_a;
1541
   input                         we_b;
1542
   input                         clk_a, clk_b;
1543
   reg [(data_width-1):0]         q_b;
1544 75 unneback
   reg [data_width-1:0] ram [mem_size-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1545 100 unneback
    parameter memory_init = 0;
1546
    parameter memory_file = "vl_ram.vmem";
1547
    parameter debug = 0;
1548
    generate
1549
    if (memory_init) begin : init_mem
1550
        initial
1551
            $readmemh(memory_file, ram);
1552
    end else if (memory_init == 2) begin : init_zero
1553
        integer k;
1554
        initial
1555
            for (k = 0; k < mem_size; k = k + 1)
1556
                ram[k] = 0;
1557
    end
1558 7 unneback
   endgenerate
1559 100 unneback
    generate
1560
    if (debug==1) begin : debug_we
1561
        always @ (posedge clk_a)
1562
        if (we_a)
1563
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1564
        always @ (posedge clk_b)
1565
        if (we_b)
1566
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1567
    end
1568
    endgenerate
1569 6 unneback
   always @ (posedge clk_a)
1570
     begin
1571
        q_a <= ram[adr_a];
1572
        if (we_a)
1573
             ram[adr_a] <= d_a;
1574
     end
1575
   always @ (posedge clk_b)
1576
     begin
1577
        q_b <= ram[adr_b];
1578
        if (we_b)
1579
          ram[adr_b] <= d_b;
1580
     end
1581
endmodule
1582 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 );
1583 75 unneback
   parameter a_data_width = 32;
1584
   parameter a_addr_width = 8;
1585 95 unneback
   parameter b_data_width = 64; //a_data_width;
1586 91 unneback
   localparam b_addr_width = a_data_width * a_addr_width / b_data_width;
1587 95 unneback
   localparam ratio = (a_addr_width>b_addr_width) ? (a_addr_width/b_addr_width) : (b_addr_width/a_addr_width);
1588
   parameter mem_size = (a_addr_width>b_addr_width) ? (1<<b_addr_width) : (1<<a_addr_width);
1589 100 unneback
   parameter memory_init = 0;
1590 95 unneback
   parameter memory_file = "vl_ram.vmem";
1591 100 unneback
   parameter debug = 0;
1592 75 unneback
   input [(a_data_width-1):0]      d_a;
1593 91 unneback
   input [(a_addr_width-1):0]       adr_a;
1594
   input [(a_data_width/8-1):0]    be_a;
1595
   input                           we_a;
1596 75 unneback
   output reg [(a_data_width-1):0] q_a;
1597 91 unneback
   input [(b_data_width-1):0]       d_b;
1598
   input [(b_addr_width-1):0]       adr_b;
1599 92 unneback
   input [(b_data_width/8-1):0]    be_b;
1600
   input                           we_b;
1601
   output reg [(b_data_width-1):0]          q_b;
1602 91 unneback
   input                           clk_a, clk_b;
1603 100 unneback
    generate
1604
    if (debug==1) begin : debug_we
1605
        always @ (posedge clk_a)
1606
        if (we_a)
1607
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
1608
        always @ (posedge clk_b)
1609
        if (we_b)
1610
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
1611
    end
1612
    endgenerate
1613 91 unneback
`ifdef SYSTEMVERILOG
1614
// use a multi-dimensional packed array
1615
//to model individual bytes within the word
1616 75 unneback
generate
1617 91 unneback
if (a_data_width==32 & b_data_width==32) begin : dpram_3232
1618 98 unneback
    logic [0:3][7:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1619 95 unneback
    initial
1620 100 unneback
        if (memory_init==1)
1621 95 unneback
            $readmemh(memory_file, ram);
1622 100 unneback
    integer k;
1623
    initial
1624
        if (memory_init==2)
1625
            for (k = 0; k < mem_size; k = k + 1)
1626
                ram[k] = 0;
1627 91 unneback
    always_ff@(posedge clk_a)
1628
    begin
1629
        if(we_a) begin
1630 100 unneback
            if(be_a[3]) ram[adr_a][0] <= d_a[31:24];
1631
            if(be_a[2]) ram[adr_a][1] <= d_a[23:16];
1632
            if(be_a[1]) ram[adr_a][2] <= d_a[15:8];
1633
            if(be_a[0]) ram[adr_a][3] <= d_a[7:0];
1634 91 unneback
        end
1635
    end
1636 92 unneback
    always@(posedge clk_a)
1637
        q_a = ram[adr_a];
1638 91 unneback
    always_ff@(posedge clk_b)
1639 92 unneback
    begin
1640
        if(we_b) begin
1641 100 unneback
            if(be_b[3]) ram[adr_b][0] <= d_b[31:24];
1642
            if(be_b[2]) ram[adr_b][1] <= d_b[23:16];
1643
            if(be_b[1]) ram[adr_b][2] <= d_b[15:8];
1644
            if(be_b[0]) ram[adr_b][3] <= d_b[7:0];
1645 92 unneback
        end
1646
    end
1647
    always@(posedge clk_b)
1648
        q_b = ram[adr_b];
1649 75 unneback
end
1650
endgenerate
1651 95 unneback
generate
1652
if (a_data_width==64 & b_data_width==64) begin : dpram_6464
1653 98 unneback
    logic [0:7][7:0] ram [0:mem_size-1] /*synthesis syn_ramstyle = "no_rw_check"*/;
1654 95 unneback
    initial
1655 100 unneback
        if (memory_init==1)
1656 95 unneback
            $readmemh(memory_file, ram);
1657 100 unneback
    integer k;
1658
    initial
1659
        if (memory_init==2)
1660
            for (k = 0; k < mem_size; k = k + 1)
1661
                ram[k] = 0;
1662 95 unneback
    always_ff@(posedge clk_a)
1663
    begin
1664
        if(we_a) begin
1665
            if(be_a[7]) ram[adr_a][7] <= d_a[63:56];
1666
            if(be_a[6]) ram[adr_a][6] <= d_a[55:48];
1667
            if(be_a[5]) ram[adr_a][5] <= d_a[47:40];
1668
            if(be_a[4]) ram[adr_a][4] <= d_a[39:32];
1669
            if(be_a[3]) ram[adr_a][3] <= d_a[31:24];
1670
            if(be_a[2]) ram[adr_a][2] <= d_a[23:16];
1671
            if(be_a[1]) ram[adr_a][1] <= d_a[15:8];
1672
            if(be_a[0]) ram[adr_a][0] <= d_a[7:0];
1673
        end
1674
    end
1675
    always@(posedge clk_a)
1676
        q_a = ram[adr_a];
1677
    always_ff@(posedge clk_b)
1678
    begin
1679
        if(we_b) begin
1680
            if(be_b[7]) ram[adr_b][7] <= d_b[63:56];
1681
            if(be_b[6]) ram[adr_b][6] <= d_b[55:48];
1682
            if(be_b[5]) ram[adr_b][5] <= d_b[47:40];
1683
            if(be_b[4]) ram[adr_b][4] <= d_b[39:32];
1684
            if(be_b[3]) ram[adr_b][3] <= d_b[31:24];
1685
            if(be_b[2]) ram[adr_b][2] <= d_b[23:16];
1686
            if(be_b[1]) ram[adr_b][1] <= d_b[15:8];
1687
            if(be_b[0]) ram[adr_b][0] <= d_b[7:0];
1688
        end
1689
    end
1690
    always@(posedge clk_b)
1691
        q_b = ram[adr_b];
1692
end
1693
endgenerate
1694
generate
1695
if (a_data_width==32 & b_data_width==16) begin : dpram_3216
1696
logic [31:0] temp;
1697 100 unneback
vl_dpram_be_2r2w # (.a_data_width(64), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .init(memory_init), .memory_file(memory_file))
1698 95 unneback
dpram6464 (
1699
    .d_a(d_a),
1700
    .q_a(q_a),
1701
    .adr_a(adr_a),
1702
    .be_a(be_a),
1703
    .we_a(we_a),
1704
    .clk_a(clk_a),
1705
    .d_b({d_b,d_b}),
1706
    .q_b(temp),
1707
    .adr_b(adr_b),
1708
    .be_b({be_b,be_b} & {{2{adr_b[0]}},{2{!adr_b[0]}}}),
1709
    .we_b(we_b),
1710
    .clk_b(clk_b)
1711
);
1712 100 unneback
always @ (adr_b[0] or temp)
1713 95 unneback
    if (adr_b[0])
1714
        q_b = temp[31:16];
1715
    else
1716
        q_b = temp[15:0];
1717
end
1718
endgenerate
1719
generate
1720
if (a_data_width==32 & b_data_width==64) begin : dpram_3264
1721
logic [63:0] temp;
1722 100 unneback
vl_dpram_be_2r2w # (.a_data_width(64), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .init(memory_init), .memory_file(memory_file))
1723 95 unneback
dpram6464 (
1724
    .d_a({d_a,d_a}),
1725
    .q_a(temp),
1726
    .adr_a(adr_a[a_addr_width-1:1]),
1727
    .be_a({be_a,be_a} & {{4{adr_a[0]}},{4{!adr_a[0]}}}),
1728
    .we_a(we_a),
1729
    .clk_a(clk_a),
1730
    .d_b(d_b),
1731
    .q_b(q_b),
1732
    .adr_b(adr_b),
1733
    .be_b(be_b),
1734
    .we_b(we_b),
1735
    .clk_b(clk_b)
1736
);
1737 100 unneback
always @ (adr_a[0] or temp)
1738 95 unneback
    if (adr_a[0])
1739
        q_a = temp[63:32];
1740
    else
1741
        q_a = temp[31:0];
1742
end
1743
endgenerate
1744 91 unneback
`else
1745 92 unneback
    // This modules requires SystemVerilog
1746 98 unneback
    // at this point anyway
1747 91 unneback
`endif
1748 75 unneback
endmodule
1749 6 unneback
// FIFO
1750 25 unneback
module vl_fifo_1r1w_fill_level_sync (
1751
    d, wr, fifo_full,
1752
    q, rd, fifo_empty,
1753
    fill_level,
1754
    clk, rst
1755
    );
1756
parameter data_width = 18;
1757
parameter addr_width = 4;
1758
// write side
1759
input  [data_width-1:0] d;
1760
input                   wr;
1761
output                  fifo_full;
1762
// read side
1763
output [data_width-1:0] q;
1764
input                   rd;
1765
output                  fifo_empty;
1766
// common
1767
output [addr_width:0]   fill_level;
1768
input rst, clk;
1769
wire [addr_width:1] wadr, radr;
1770
vl_cnt_bin_ce
1771
    # ( .length(addr_width))
1772
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
1773
vl_cnt_bin_ce
1774
    # (.length(addr_width))
1775
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
1776
vl_dpram_1r1w
1777
    # (.data_width(data_width), .addr_width(addr_width))
1778
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
1779 31 unneback
vl_cnt_bin_ce_rew_q_zq_l1
1780 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
1781 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
1782
endmodule
1783 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
1784
// RAM is supposed to be larger than the two FIFOs
1785
// LFSR counters used adr pointers
1786
module vl_fifo_2r2w_sync_simplex (
1787
    // a side
1788
    a_d, a_wr, a_fifo_full,
1789
    a_q, a_rd, a_fifo_empty,
1790
    a_fill_level,
1791
    // b side
1792
    b_d, b_wr, b_fifo_full,
1793
    b_q, b_rd, b_fifo_empty,
1794
    b_fill_level,
1795
    // common
1796
    clk, rst
1797
    );
1798
parameter data_width = 8;
1799
parameter addr_width = 5;
1800
parameter fifo_full_level = (1<<addr_width)-1;
1801
// a side
1802
input  [data_width-1:0] a_d;
1803
input                   a_wr;
1804
output                  a_fifo_full;
1805
output [data_width-1:0] a_q;
1806
input                   a_rd;
1807
output                  a_fifo_empty;
1808
output [addr_width-1:0] a_fill_level;
1809
// b side
1810
input  [data_width-1:0] b_d;
1811
input                   b_wr;
1812
output                  b_fifo_full;
1813
output [data_width-1:0] b_q;
1814
input                   b_rd;
1815
output                  b_fifo_empty;
1816
output [addr_width-1:0] b_fill_level;
1817
input                   clk;
1818
input                   rst;
1819
// adr_gen
1820
wire [addr_width:1] a_wadr, a_radr;
1821
wire [addr_width:1] b_wadr, b_radr;
1822
// dpram
1823
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
1824
vl_cnt_lfsr_ce
1825
    # ( .length(addr_width))
1826
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
1827
vl_cnt_lfsr_ce
1828
    # (.length(addr_width))
1829
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
1830
vl_cnt_lfsr_ce
1831
    # ( .length(addr_width))
1832
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
1833
vl_cnt_lfsr_ce
1834
    # (.length(addr_width))
1835
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
1836
// mux read or write adr to DPRAM
1837
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
1838
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
1839
vl_dpram_2r2w
1840
    # (.data_width(data_width), .addr_width(addr_width+1))
1841
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
1842
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
1843
vl_cnt_bin_ce_rew_zq_l1
1844 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1845 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));
1846
vl_cnt_bin_ce_rew_zq_l1
1847 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1848 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));
1849
endmodule
1850 6 unneback
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
1851 11 unneback
   parameter addr_width = 4;
1852
   parameter N = addr_width-1;
1853 6 unneback
   parameter Q1 = 2'b00;
1854
   parameter Q2 = 2'b01;
1855
   parameter Q3 = 2'b11;
1856
   parameter Q4 = 2'b10;
1857
   parameter going_empty = 1'b0;
1858
   parameter going_full  = 1'b1;
1859
   input [N:0]  wptr, rptr;
1860 14 unneback
   output       fifo_empty;
1861 6 unneback
   output       fifo_full;
1862
   input        wclk, rclk, rst;
1863
   wire direction;
1864
   reg  direction_set, direction_clr;
1865
   wire async_empty, async_full;
1866
   wire fifo_full2;
1867 14 unneback
   wire fifo_empty2;
1868 6 unneback
   // direction_set
1869
   always @ (wptr[N:N-1] or rptr[N:N-1])
1870
     case ({wptr[N:N-1],rptr[N:N-1]})
1871
       {Q1,Q2} : direction_set <= 1'b1;
1872
       {Q2,Q3} : direction_set <= 1'b1;
1873
       {Q3,Q4} : direction_set <= 1'b1;
1874
       {Q4,Q1} : direction_set <= 1'b1;
1875
       default : direction_set <= 1'b0;
1876
     endcase
1877
   // direction_clear
1878
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
1879
     if (rst)
1880
       direction_clr <= 1'b1;
1881
     else
1882
       case ({wptr[N:N-1],rptr[N:N-1]})
1883
         {Q2,Q1} : direction_clr <= 1'b1;
1884
         {Q3,Q2} : direction_clr <= 1'b1;
1885
         {Q4,Q3} : direction_clr <= 1'b1;
1886
         {Q1,Q4} : direction_clr <= 1'b1;
1887
         default : direction_clr <= 1'b0;
1888
       endcase
1889 18 unneback
    vl_dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
1890 6 unneback
   assign async_empty = (wptr == rptr) && (direction==going_empty);
1891
   assign async_full  = (wptr == rptr) && (direction==going_full);
1892 18 unneback
    vl_dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
1893
    vl_dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
1894 6 unneback
/*
1895
   always @ (posedge wclk or posedge rst or posedge async_full)
1896
     if (rst)
1897
       {fifo_full, fifo_full2} <= 2'b00;
1898
     else if (async_full)
1899
       {fifo_full, fifo_full2} <= 2'b11;
1900
     else
1901
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
1902
*/
1903 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
1904 6 unneback
     if (async_empty)
1905
       {fifo_empty, fifo_empty2} <= 2'b11;
1906
     else
1907 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
1908 18 unneback
    vl_dff # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
1909
    vl_dff # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
1910 27 unneback
endmodule // async_compb
1911 6 unneback
module vl_fifo_1r1w_async (
1912
    d, wr, fifo_full, wr_clk, wr_rst,
1913
    q, rd, fifo_empty, rd_clk, rd_rst
1914
    );
1915
parameter data_width = 18;
1916
parameter addr_width = 4;
1917
// write side
1918
input  [data_width-1:0] d;
1919
input                   wr;
1920
output                  fifo_full;
1921
input                   wr_clk;
1922
input                   wr_rst;
1923
// read side
1924
output [data_width-1:0] q;
1925
input                   rd;
1926
output                  fifo_empty;
1927
input                   rd_clk;
1928
input                   rd_rst;
1929
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
1930 18 unneback
vl_cnt_gray_ce_bin
1931 6 unneback
    # ( .length(addr_width))
1932
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
1933 18 unneback
vl_cnt_gray_ce_bin
1934 6 unneback
    # (.length(addr_width))
1935 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
1936 7 unneback
vl_dpram_1r1w
1937 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
1938
    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));
1939
vl_fifo_cmp_async
1940
    # (.addr_width(addr_width))
1941
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
1942
endmodule
1943 8 unneback
module vl_fifo_2r2w_async (
1944 6 unneback
    // a side
1945
    a_d, a_wr, a_fifo_full,
1946
    a_q, a_rd, a_fifo_empty,
1947
    a_clk, a_rst,
1948
    // b side
1949
    b_d, b_wr, b_fifo_full,
1950
    b_q, b_rd, b_fifo_empty,
1951
    b_clk, b_rst
1952
    );
1953
parameter data_width = 18;
1954
parameter addr_width = 4;
1955
// a side
1956
input  [data_width-1:0] a_d;
1957
input                   a_wr;
1958
output                  a_fifo_full;
1959
output [data_width-1:0] a_q;
1960
input                   a_rd;
1961
output                  a_fifo_empty;
1962
input                   a_clk;
1963
input                   a_rst;
1964
// b side
1965
input  [data_width-1:0] b_d;
1966
input                   b_wr;
1967
output                  b_fifo_full;
1968
output [data_width-1:0] b_q;
1969
input                   b_rd;
1970
output                  b_fifo_empty;
1971
input                   b_clk;
1972
input                   b_rst;
1973
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1974
vl_fifo_1r1w_async_a (
1975
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
1976
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
1977
    );
1978
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1979
vl_fifo_1r1w_async_b (
1980
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
1981
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
1982
    );
1983
endmodule
1984 8 unneback
module vl_fifo_2r2w_async_simplex (
1985 6 unneback
    // a side
1986
    a_d, a_wr, a_fifo_full,
1987
    a_q, a_rd, a_fifo_empty,
1988
    a_clk, a_rst,
1989
    // b side
1990
    b_d, b_wr, b_fifo_full,
1991
    b_q, b_rd, b_fifo_empty,
1992
    b_clk, b_rst
1993
    );
1994
parameter data_width = 18;
1995
parameter addr_width = 4;
1996
// a side
1997
input  [data_width-1:0] a_d;
1998
input                   a_wr;
1999
output                  a_fifo_full;
2000
output [data_width-1:0] a_q;
2001
input                   a_rd;
2002
output                  a_fifo_empty;
2003
input                   a_clk;
2004
input                   a_rst;
2005
// b side
2006
input  [data_width-1:0] b_d;
2007
input                   b_wr;
2008
output                  b_fifo_full;
2009
output [data_width-1:0] b_q;
2010
input                   b_rd;
2011
output                  b_fifo_empty;
2012
input                   b_clk;
2013
input                   b_rst;
2014
// adr_gen
2015
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
2016
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
2017
// dpram
2018
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
2019 18 unneback
vl_cnt_gray_ce_bin
2020 6 unneback
    # ( .length(addr_width))
2021
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
2022 18 unneback
vl_cnt_gray_ce_bin
2023 6 unneback
    # (.length(addr_width))
2024
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
2025 18 unneback
vl_cnt_gray_ce_bin
2026 6 unneback
    # ( .length(addr_width))
2027
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
2028 18 unneback
vl_cnt_gray_ce_bin
2029 6 unneback
    # (.length(addr_width))
2030
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
2031
// mux read or write adr to DPRAM
2032
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
2033
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
2034 11 unneback
vl_dpram_2r2w
2035 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
2036
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
2037
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
2038 11 unneback
vl_fifo_cmp_async
2039 6 unneback
    # (.addr_width(addr_width))
2040
    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) );
2041 11 unneback
vl_fifo_cmp_async
2042 6 unneback
    # (.addr_width(addr_width))
2043
    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) );
2044
endmodule
2045 48 unneback
module vl_reg_file (
2046
    a1, a2, a3, wd3, we3, rd1, rd2, clk
2047
);
2048
parameter data_width = 32;
2049
parameter addr_width = 5;
2050
input [addr_width-1:0] a1, a2, a3;
2051
input [data_width-1:0] wd3;
2052
input we3;
2053
output [data_width-1:0] rd1, rd2;
2054
input clk;
2055
reg [data_width-1:0] wd3_reg;
2056
reg [addr_width-1:0] a1_reg, a2_reg, a3_reg;
2057
reg we3_reg;
2058
reg [data_width-1:0] ram1 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
2059
reg [data_width-1:0] ram2 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
2060
always @ (posedge clk or posedge rst)
2061
if (rst)
2062
    {wd3_reg, a3_reg, we3_reg} <= {(data_width+addr_width+1){1'b0}};
2063
else
2064
    {wd3_reg, a3_reg, we3_reg} <= {wd3,a3,wd3};
2065
    always @ (negedge clk)
2066
    if (we3_reg)
2067
        ram1[a3_reg] <= wd3;
2068
    always @ (posedge clk)
2069
        a1_reg <= a1;
2070
    assign rd1 = ram1[a1_reg];
2071
    always @ (negedge clk)
2072
    if (we3_reg)
2073
        ram2[a3_reg] <= wd3;
2074
    always @ (posedge clk)
2075
        a2_reg <= a2;
2076
    assign rd2 = ram2[a2_reg];
2077
endmodule
2078 12 unneback
//////////////////////////////////////////////////////////////////////
2079
////                                                              ////
2080
////  Versatile library, wishbone stuff                           ////
2081
////                                                              ////
2082
////  Description                                                 ////
2083
////  Wishbone compliant modules                                  ////
2084
////                                                              ////
2085
////                                                              ////
2086
////  To Do:                                                      ////
2087
////   -                                                          ////
2088
////                                                              ////
2089
////  Author(s):                                                  ////
2090
////      - Michael Unneback, unneback@opencores.org              ////
2091
////        ORSoC AB                                              ////
2092
////                                                              ////
2093
//////////////////////////////////////////////////////////////////////
2094
////                                                              ////
2095
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2096
////                                                              ////
2097
//// This source file may be used and distributed without         ////
2098
//// restriction provided that this copyright statement is not    ////
2099
//// removed from the file and that any derivative work contains  ////
2100
//// the original copyright notice and the associated disclaimer. ////
2101
////                                                              ////
2102
//// This source file is free software; you can redistribute it   ////
2103
//// and/or modify it under the terms of the GNU Lesser General   ////
2104
//// Public License as published by the Free Software Foundation; ////
2105
//// either version 2.1 of the License, or (at your option) any   ////
2106
//// later version.                                               ////
2107
////                                                              ////
2108
//// This source is distributed in the hope that it will be       ////
2109
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2110
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2111
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2112
//// details.                                                     ////
2113
////                                                              ////
2114
//// You should have received a copy of the GNU Lesser General    ////
2115
//// Public License along with this source; if not, download it   ////
2116
//// from http://www.opencores.org/lgpl.shtml                     ////
2117
////                                                              ////
2118
//////////////////////////////////////////////////////////////////////
2119
`timescale 1ns/1ns
2120 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);
2121 83 unneback
parameter adr_width = 10;
2122
parameter max_burst_width = 4;
2123 85 unneback
input cyc_i, stb_i, we_i;
2124 83 unneback
input [2:0] cti_i;
2125
input [1:0] bte_i;
2126
input [adr_width-1:0] adr_i;
2127
output [adr_width-1:0] adr_o;
2128
output ack_o;
2129
input clk, rst;
2130
reg [adr_width-1:0] adr;
2131 90 unneback
wire [max_burst_width-1:0] to_adr;
2132 91 unneback
reg [max_burst_width-1:0] last_adr;
2133 92 unneback
reg last_cycle;
2134
localparam idle_or_eoc = 1'b0;
2135
localparam cyc_or_ws   = 1'b1;
2136 91 unneback
always @ (posedge clk or posedge rst)
2137
if (rst)
2138
    last_adr <= {max_burst_width{1'b0}};
2139
else
2140
    if (stb_i)
2141 92 unneback
        last_adr <=adr_o[max_burst_width-1:0];
2142 83 unneback
generate
2143
if (max_burst_width==0) begin : inst_0
2144 97 unneback
        reg ack_o;
2145
        assign adr_o = adr_i;
2146
        always @ (posedge clk or posedge rst)
2147
        if (rst)
2148
            ack_o <= 1'b0;
2149
        else
2150
            ack_o <= cyc_i & stb_i & !ack_o;
2151 83 unneback
end else begin
2152
    always @ (posedge clk or posedge rst)
2153
    if (rst)
2154 92 unneback
        last_cycle <= idle_or_eoc;
2155 83 unneback
    else
2156 92 unneback
        last_cycle <= (!cyc_i) ? idle_or_eoc : //idle
2157
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? idle_or_eoc : // eoc
2158
                      (cyc_i & !stb_i) ? cyc_or_ws : //ws
2159
                      cyc_or_ws; // cyc
2160
    assign to_adr = (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
2161 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
2162 91 unneback
                                        (!stb_i) ? last_adr :
2163 92 unneback
                                        (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] :
2164 85 unneback
                                        adr[max_burst_width-1:0];
2165 92 unneback
    assign ack_o = (last_cycle==cyc_or_ws) & stb_i;
2166 83 unneback
end
2167
endgenerate
2168
generate
2169
if (max_burst_width==2) begin : inst_2
2170
    always @ (posedge clk or posedge rst)
2171
    if (rst)
2172
        adr <= 2'h0;
2173
    else
2174
        if (cyc_i & stb_i)
2175
            adr[1:0] <= to_adr[1:0] + 2'd1;
2176
        else
2177
            adr <= to_adr[1:0];
2178
end
2179
endgenerate
2180
generate
2181
if (max_burst_width==3) begin : inst_3
2182
    always @ (posedge clk or posedge rst)
2183
    if (rst)
2184
        adr <= 3'h0;
2185
    else
2186
        if (cyc_i & stb_i)
2187
            case (bte_i)
2188
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
2189
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
2190
            endcase
2191
        else
2192
            adr <= to_adr[2:0];
2193
end
2194
endgenerate
2195
generate
2196
if (max_burst_width==4) begin : inst_4
2197
    always @ (posedge clk or posedge rst)
2198
    if (rst)
2199
        adr <= 4'h0;
2200
    else
2201 91 unneback
        if (stb_i) // | (!stb_i & last_cycle!=ws)) // for !stb_i restart with adr_i +1, only inc once
2202 83 unneback
            case (bte_i)
2203
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
2204
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
2205
            default: adr[3:0] <= to_adr + 4'd1;
2206
            endcase
2207
        else
2208
            adr <= to_adr[3:0];
2209
end
2210
endgenerate
2211
generate
2212
if (adr_width > max_burst_width) begin : pass_through
2213
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
2214
end
2215
endgenerate
2216
endmodule
2217
// async wb3 - wb3 bridge
2218
`timescale 1ns/1ns
2219 18 unneback
module vl_wb3wb3_bridge (
2220 12 unneback
        // wishbone slave side
2221
        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,
2222
        // wishbone master side
2223
        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);
2224 95 unneback
parameter style = "FIFO"; // valid: simple, FIFO
2225
parameter addr_width = 4;
2226 12 unneback
input [31:0] wbs_dat_i;
2227
input [31:2] wbs_adr_i;
2228
input [3:0]  wbs_sel_i;
2229
input [1:0]  wbs_bte_i;
2230
input [2:0]  wbs_cti_i;
2231
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
2232
output [31:0] wbs_dat_o;
2233 14 unneback
output wbs_ack_o;
2234 12 unneback
input wbs_clk, wbs_rst;
2235
output [31:0] wbm_dat_o;
2236
output reg [31:2] wbm_adr_o;
2237
output [3:0]  wbm_sel_o;
2238
output reg [1:0]  wbm_bte_o;
2239
output reg [2:0]  wbm_cti_o;
2240 14 unneback
output reg wbm_we_o;
2241
output wbm_cyc_o;
2242 12 unneback
output wbm_stb_o;
2243
input [31:0]  wbm_dat_i;
2244
input wbm_ack_i;
2245
input wbm_clk, wbm_rst;
2246
// bte
2247
parameter linear       = 2'b00;
2248
parameter wrap4        = 2'b01;
2249
parameter wrap8        = 2'b10;
2250
parameter wrap16       = 2'b11;
2251
// cti
2252
parameter classic      = 3'b000;
2253
parameter incburst     = 3'b010;
2254
parameter endofburst   = 3'b111;
2255 95 unneback
localparam wbs_adr  = 1'b0;
2256
localparam wbs_data = 1'b1;
2257
localparam wbm_adr0      = 2'b00;
2258
localparam wbm_adr1      = 2'b01;
2259
localparam wbm_data      = 2'b10;
2260
localparam wbm_data_wait = 2'b11;
2261 12 unneback
reg [1:0] wbs_bte_reg;
2262
reg wbs;
2263
wire wbs_eoc_alert, wbm_eoc_alert;
2264
reg wbs_eoc, wbm_eoc;
2265
reg [1:0] wbm;
2266 14 unneback
wire [1:16] wbs_count, wbm_count;
2267 12 unneback
wire [35:0] a_d, a_q, b_d, b_q;
2268
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
2269
reg a_rd_reg;
2270
wire b_rd_adr, b_rd_data;
2271 14 unneback
wire b_rd_data_reg;
2272
wire [35:0] temp;
2273 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]);
2274
always @ (posedge wbs_clk or posedge wbs_rst)
2275
if (wbs_rst)
2276
        wbs_eoc <= 1'b0;
2277
else
2278
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
2279 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
2280 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
2281
                wbs_eoc <= 1'b1;
2282 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
2283 12 unneback
    cnt0 (
2284
        .cke(wbs_ack_o),
2285
        .clear(wbs_eoc),
2286
        .q(wbs_count),
2287
        .rst(wbs_rst),
2288
        .clk(wbs_clk));
2289
always @ (posedge wbs_clk or posedge wbs_rst)
2290
if (wbs_rst)
2291
        wbs <= wbs_adr;
2292
else
2293 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
2294 12 unneback
                wbs <= wbs_data;
2295
        else if (wbs_eoc & wbs_ack_o)
2296
                wbs <= wbs_adr;
2297
// wbs FIFO
2298 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};
2299
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
2300 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
2301
              1'b0;
2302
assign a_rd = !a_fifo_empty;
2303
always @ (posedge wbs_clk or posedge wbs_rst)
2304
if (wbs_rst)
2305
        a_rd_reg <= 1'b0;
2306
else
2307
        a_rd_reg <= a_rd;
2308
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
2309
assign wbs_dat_o = a_q[35:4];
2310
always @ (posedge wbs_clk or posedge wbs_rst)
2311
if (wbs_rst)
2312 13 unneback
        wbs_bte_reg <= 2'b00;
2313 12 unneback
else
2314 13 unneback
        wbs_bte_reg <= wbs_bte_i;
2315 12 unneback
// wbm FIFO
2316
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]);
2317
always @ (posedge wbm_clk or posedge wbm_rst)
2318
if (wbm_rst)
2319
        wbm_eoc <= 1'b0;
2320
else
2321
        if (wbm==wbm_adr0 & !b_fifo_empty)
2322
                wbm_eoc <= b_q[4:3] == linear;
2323
        else if (wbm_eoc_alert & wbm_ack_i)
2324
                wbm_eoc <= 1'b1;
2325
always @ (posedge wbm_clk or posedge wbm_rst)
2326
if (wbm_rst)
2327
        wbm <= wbm_adr0;
2328
else
2329 33 unneback
/*
2330 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
2331
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
2332
        (wbm==wbm_adr1 & !wbm_we_o) |
2333
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
2334
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
2335 33 unneback
*/
2336
    case (wbm)
2337
    wbm_adr0:
2338
        if (!b_fifo_empty)
2339
            wbm <= wbm_adr1;
2340
    wbm_adr1:
2341
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
2342
            wbm <= wbm_data;
2343
    wbm_data:
2344
        if (wbm_ack_i & wbm_eoc)
2345
            wbm <= wbm_adr0;
2346
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
2347
            wbm <= wbm_data_wait;
2348
    wbm_data_wait:
2349
        if (!b_fifo_empty)
2350
            wbm <= wbm_data;
2351
    endcase
2352 12 unneback
assign b_d = {wbm_dat_i,4'b1111};
2353
assign b_wr = !wbm_we_o & wbm_ack_i;
2354
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
2355
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
2356
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
2357 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
2358 12 unneback
                   1'b0;
2359
assign b_rd = b_rd_adr | b_rd_data;
2360 18 unneback
vl_dff dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
2361
vl_dff_ce # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
2362 12 unneback
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
2363 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
2364 12 unneback
    cnt1 (
2365
        .cke(wbm_ack_i),
2366
        .clear(wbm_eoc),
2367
        .q(wbm_count),
2368
        .rst(wbm_rst),
2369
        .clk(wbm_clk));
2370 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
2371
assign wbm_stb_o = (wbm==wbm_data);
2372 12 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
2373
if (wbm_rst)
2374
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
2375
else begin
2376
        if (wbm==wbm_adr0 & !b_fifo_empty)
2377
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
2378
        else if (wbm_eoc_alert & wbm_ack_i)
2379
                wbm_cti_o <= endofburst;
2380
end
2381
//async_fifo_dw_simplex_top
2382
vl_fifo_2r2w_async_simplex
2383
# ( .data_width(36), .addr_width(addr_width))
2384
fifo (
2385
    // a side
2386
    .a_d(a_d),
2387
    .a_wr(a_wr),
2388
    .a_fifo_full(a_fifo_full),
2389
    .a_q(a_q),
2390
    .a_rd(a_rd),
2391
    .a_fifo_empty(a_fifo_empty),
2392
    .a_clk(wbs_clk),
2393
    .a_rst(wbs_rst),
2394
    // b side
2395
    .b_d(b_d),
2396
    .b_wr(b_wr),
2397
    .b_fifo_full(b_fifo_full),
2398
    .b_q(b_q),
2399
    .b_rd(b_rd),
2400
    .b_fifo_empty(b_fifo_empty),
2401
    .b_clk(wbm_clk),
2402
    .b_rst(wbm_rst)
2403
    );
2404
endmodule
2405 75 unneback
module vl_wb3avalon_bridge (
2406
        // wishbone slave side
2407
        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,
2408 77 unneback
        // avalon master side
2409 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
2410 85 unneback
parameter linewrapburst = 1'b0;
2411 75 unneback
input [31:0] wbs_dat_i;
2412
input [31:2] wbs_adr_i;
2413
input [3:0]  wbs_sel_i;
2414
input [1:0]  wbs_bte_i;
2415
input [2:0]  wbs_cti_i;
2416 83 unneback
input wbs_we_i;
2417
input wbs_cyc_i;
2418
input wbs_stb_i;
2419 75 unneback
output [31:0] wbs_dat_o;
2420
output wbs_ack_o;
2421
input wbs_clk, wbs_rst;
2422
input [31:0] readdata;
2423
output [31:0] writedata;
2424
output [31:2] address;
2425
output [3:0]  be;
2426
output write;
2427 81 unneback
output read;
2428 75 unneback
output beginbursttransfer;
2429
output [3:0] burstcount;
2430
input readdatavalid;
2431
input waitrequest;
2432
input clk;
2433
input rst;
2434
wire [1:0] wbm_bte_o;
2435
wire [2:0] wbm_cti_o;
2436
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
2437
reg last_cyc;
2438 79 unneback
reg [3:0] counter;
2439 82 unneback
reg read_busy;
2440 75 unneback
always @ (posedge clk or posedge rst)
2441
if (rst)
2442
    last_cyc <= 1'b0;
2443
else
2444
    last_cyc <= wbm_cyc_o;
2445 79 unneback
always @ (posedge clk or posedge rst)
2446
if (rst)
2447 82 unneback
    read_busy <= 1'b0;
2448 79 unneback
else
2449 82 unneback
    if (read & !waitrequest)
2450
        read_busy <= 1'b1;
2451
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
2452
        read_busy <= 1'b0;
2453
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
2454 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
2455
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
2456
                    (wbm_bte_o==2'b10) ? 4'd8 :
2457 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
2458
                    4'd1;
2459 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
2460 79 unneback
always @ (posedge clk or posedge rst)
2461
if (rst) begin
2462
    counter <= 4'd0;
2463
end else
2464 80 unneback
    if (wbm_we_o) begin
2465
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
2466 85 unneback
            counter <= burstcount -4'd1;
2467 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
2468
            counter <= burstcount;
2469
        end else if (!waitrequest & wbm_stb_o) begin
2470
            counter <= counter - 4'd1;
2471
        end
2472 82 unneback
    end
2473 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
2474 77 unneback
vl_wb3wb3_bridge wbwb3inst (
2475 75 unneback
    // wishbone slave side
2476
    .wbs_dat_i(wbs_dat_i),
2477
    .wbs_adr_i(wbs_adr_i),
2478
    .wbs_sel_i(wbs_sel_i),
2479
    .wbs_bte_i(wbs_bte_i),
2480
    .wbs_cti_i(wbs_cti_i),
2481
    .wbs_we_i(wbs_we_i),
2482
    .wbs_cyc_i(wbs_cyc_i),
2483
    .wbs_stb_i(wbs_stb_i),
2484
    .wbs_dat_o(wbs_dat_o),
2485
    .wbs_ack_o(wbs_ack_o),
2486
    .wbs_clk(wbs_clk),
2487
    .wbs_rst(wbs_rst),
2488
    // wishbone master side
2489
    .wbm_dat_o(writedata),
2490 78 unneback
    .wbm_adr_o(address),
2491 75 unneback
    .wbm_sel_o(be),
2492
    .wbm_bte_o(wbm_bte_o),
2493
    .wbm_cti_o(wbm_cti_o),
2494
    .wbm_we_o(wbm_we_o),
2495
    .wbm_cyc_o(wbm_cyc_o),
2496
    .wbm_stb_o(wbm_stb_o),
2497
    .wbm_dat_i(readdata),
2498
    .wbm_ack_i(wbm_ack_i),
2499
    .wbm_clk(clk),
2500
    .wbm_rst(rst));
2501
endmodule
2502 39 unneback
module vl_wb3_arbiter_type1 (
2503
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
2504
    wbm_dat_i, wbm_ack_i, wbm_err_i, wbm_rty_i,
2505
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
2506
    wbs_dat_o, wbs_ack_o, wbs_err_o, wbs_rty_o,
2507
    wb_clk, wb_rst
2508
);
2509
parameter nr_of_ports = 3;
2510
parameter adr_size = 26;
2511
parameter adr_lo   = 2;
2512
parameter dat_size = 32;
2513
parameter sel_size = dat_size/8;
2514
localparam aw = (adr_size - adr_lo) * nr_of_ports;
2515
localparam dw = dat_size * nr_of_ports;
2516
localparam sw = sel_size * nr_of_ports;
2517
localparam cw = 3 * nr_of_ports;
2518
localparam bw = 2 * nr_of_ports;
2519
input  [dw-1:0] wbm_dat_o;
2520
input  [aw-1:0] wbm_adr_o;
2521
input  [sw-1:0] wbm_sel_o;
2522
input  [cw-1:0] wbm_cti_o;
2523
input  [bw-1:0] wbm_bte_o;
2524
input  [nr_of_ports-1:0] wbm_we_o, wbm_stb_o, wbm_cyc_o;
2525
output [dw-1:0] wbm_dat_i;
2526
output [nr_of_ports-1:0] wbm_ack_i, wbm_err_i, wbm_rty_i;
2527
output [dat_size-1:0] wbs_dat_i;
2528
output [adr_size-1:adr_lo] wbs_adr_i;
2529
output [sel_size-1:0] wbs_sel_i;
2530
output [2:0] wbs_cti_i;
2531
output [1:0] wbs_bte_i;
2532
output wbs_we_i, wbs_stb_i, wbs_cyc_i;
2533
input  [dat_size-1:0] wbs_dat_o;
2534
input  wbs_ack_o, wbs_err_o, wbs_rty_o;
2535
input wb_clk, wb_rst;
2536 44 unneback
reg  [nr_of_ports-1:0] select;
2537 39 unneback
wire [nr_of_ports-1:0] state;
2538
wire [nr_of_ports-1:0] eoc; // end-of-cycle
2539
wire [nr_of_ports-1:0] sel;
2540
wire idle;
2541
genvar i;
2542
assign idle = !(|state);
2543
generate
2544
if (nr_of_ports == 2) begin
2545
    wire [2:0] wbm1_cti_o, wbm0_cti_o;
2546
    assign {wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2547 44 unneback
    //assign select = (idle) ? {wbm_cyc_o[1],!wbm_cyc_o[1] & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2548
    always @ (idle or wbm_cyc_o)
2549
    if (idle)
2550
        casex (wbm_cyc_o)
2551
        2'b1x : select = 2'b10;
2552
        2'b01 : select = 2'b01;
2553
        default : select = {nr_of_ports{1'b0}};
2554
        endcase
2555
    else
2556
        select = {nr_of_ports{1'b0}};
2557 39 unneback
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2558
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2559
end
2560
endgenerate
2561
generate
2562
if (nr_of_ports == 3) begin
2563
    wire [2:0] wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2564
    assign {wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2565 44 unneback
    always @ (idle or wbm_cyc_o)
2566
    if (idle)
2567
        casex (wbm_cyc_o)
2568
        3'b1xx : select = 3'b100;
2569
        3'b01x : select = 3'b010;
2570
        3'b001 : select = 3'b001;
2571
        default : select = {nr_of_ports{1'b0}};
2572
        endcase
2573
    else
2574
        select = {nr_of_ports{1'b0}};
2575
//    assign select = (idle) ? {wbm_cyc_o[2],!wbm_cyc_o[2] & wbm_cyc_o[1],wbm_cyc_o[2:1]==2'b00 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2576 39 unneback
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2577
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2578
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2579
end
2580
endgenerate
2581
generate
2582 44 unneback
if (nr_of_ports == 4) begin
2583
    wire [2:0] wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2584
    assign {wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2585
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2586
    always @ (idle or wbm_cyc_o)
2587
    if (idle)
2588
        casex (wbm_cyc_o)
2589
        4'b1xxx : select = 4'b1000;
2590
        4'b01xx : select = 4'b0100;
2591
        4'b001x : select = 4'b0010;
2592
        4'b0001 : select = 4'b0001;
2593
        default : select = {nr_of_ports{1'b0}};
2594
        endcase
2595
    else
2596
        select = {nr_of_ports{1'b0}};
2597
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2598
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2599
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2600
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2601
end
2602
endgenerate
2603
generate
2604
if (nr_of_ports == 5) begin
2605
    wire [2:0] wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2606
    assign {wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2607
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2608
    always @ (idle or wbm_cyc_o)
2609
    if (idle)
2610
        casex (wbm_cyc_o)
2611
        5'b1xxxx : select = 5'b10000;
2612
        5'b01xxx : select = 5'b01000;
2613
        5'b001xx : select = 5'b00100;
2614
        5'b0001x : select = 5'b00010;
2615
        5'b00001 : select = 5'b00001;
2616
        default : select = {nr_of_ports{1'b0}};
2617
        endcase
2618
    else
2619
        select = {nr_of_ports{1'b0}};
2620
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2621
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2622
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2623
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2624
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2625
end
2626
endgenerate
2627
generate
2628 67 unneback
if (nr_of_ports == 6) begin
2629
    wire [2:0] wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2630
    assign {wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2631
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2632
    always @ (idle or wbm_cyc_o)
2633
    if (idle)
2634
        casex (wbm_cyc_o)
2635
        6'b1xxxxx : select = 6'b100000;
2636
        6'b01xxxx : select = 6'b010000;
2637
        6'b001xxx : select = 6'b001000;
2638
        6'b0001xx : select = 6'b000100;
2639
        6'b00001x : select = 6'b000010;
2640
        6'b000001 : select = 6'b000001;
2641
        default : select = {nr_of_ports{1'b0}};
2642
        endcase
2643
    else
2644
        select = {nr_of_ports{1'b0}};
2645
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2646
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2647
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2648
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2649
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2650
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2651
end
2652
endgenerate
2653
generate
2654
if (nr_of_ports == 7) begin
2655
    wire [2:0] wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2656
    assign {wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2657
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2658
    always @ (idle or wbm_cyc_o)
2659
    if (idle)
2660
        casex (wbm_cyc_o)
2661
        7'b1xxxxxx : select = 7'b1000000;
2662
        7'b01xxxxx : select = 7'b0100000;
2663
        7'b001xxxx : select = 7'b0010000;
2664
        7'b0001xxx : select = 7'b0001000;
2665
        7'b00001xx : select = 7'b0000100;
2666
        7'b000001x : select = 7'b0000010;
2667
        7'b0000001 : select = 7'b0000001;
2668
        default : select = {nr_of_ports{1'b0}};
2669
        endcase
2670
    else
2671
        select = {nr_of_ports{1'b0}};
2672
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
2673
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2674
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2675
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2676
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2677
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2678
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2679
end
2680
endgenerate
2681
generate
2682
if (nr_of_ports == 8) begin
2683
    wire [2:0] wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2684
    assign {wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2685
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2686
    always @ (idle or wbm_cyc_o)
2687
    if (idle)
2688
        casex (wbm_cyc_o)
2689
        8'b1xxxxxxx : select = 8'b10000000;
2690
        8'b01xxxxxx : select = 8'b01000000;
2691
        8'b001xxxxx : select = 8'b00100000;
2692
        8'b0001xxxx : select = 8'b00010000;
2693
        8'b00001xxx : select = 8'b00001000;
2694
        8'b000001xx : select = 8'b00000100;
2695
        8'b0000001x : select = 8'b00000010;
2696
        8'b00000001 : select = 8'b00000001;
2697
        default : select = {nr_of_ports{1'b0}};
2698
        endcase
2699
    else
2700
        select = {nr_of_ports{1'b0}};
2701
    assign eoc[7] = (wbm_ack_i[7] & (wbm7_cti_o == 3'b000 | wbm7_cti_o == 3'b111)) | !wbm_cyc_o[7];
2702
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
2703
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2704
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2705
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2706
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2707
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2708
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2709
end
2710
endgenerate
2711
generate
2712 63 unneback
for (i=0;i<nr_of_ports;i=i+1) begin : spr0
2713 39 unneback
    vl_spr sr0( .sp(select[i]), .r(eoc[i]), .q(state[i]), .clk(wb_clk), .rst(wb_rst));
2714
end
2715
endgenerate
2716
    assign sel = select | state;
2717
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(32)) mux0 ( .a(wbm_dat_o), .sel(sel), .dout(wbs_dat_i));
2718
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(adr_size-adr_lo)) mux1 ( .a(wbm_adr_o), .sel(sel), .dout(wbs_adr_i));
2719
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(sel_size)) mux2 ( .a(wbm_sel_o), .sel(sel), .dout(wbs_sel_i));
2720
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(3)) mux3 ( .a(wbm_cti_o), .sel(sel), .dout(wbs_cti_i));
2721
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(2)) mux4 ( .a(wbm_bte_o), .sel(sel), .dout(wbs_bte_i));
2722
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(1)) mux5 ( .a(wbm_we_o), .sel(sel), .dout(wbs_we_i));
2723
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(1)) mux6 ( .a(wbm_stb_o), .sel(sel), .dout(wbs_stb_i));
2724
    assign wbs_cyc_i = |sel;
2725
    assign wbm_dat_i = {nr_of_ports{wbs_dat_o}};
2726
    assign wbm_ack_i = {nr_of_ports{wbs_ack_o}} & sel;
2727
    assign wbm_err_i = {nr_of_ports{wbs_err_o}} & sel;
2728
    assign wbm_rty_i = {nr_of_ports{wbs_rty_o}} & sel;
2729
endmodule
2730 49 unneback
// WB RAM with byte enable
2731 101 unneback
module vl_wb_ram (
2732 69 unneback
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
2733 101 unneback
    wbs_dat_o, wbs_ack_o, wbs_stall_o, wb_clk, wb_rst);
2734
parameter adr_width = 16;
2735
parameter mem_size = 1<<adr_width;
2736
parameter dat_width = 32;
2737
parameter max_burst_width = 4; // only used for B3
2738
parameter mode = "B3"; // valid options: B3, B4
2739 60 unneback
parameter memory_init = 1;
2740
parameter memory_file = "vl_ram.vmem";
2741 101 unneback
input [dat_width-1:0] wbs_dat_i;
2742
input [adr_width-1:0] wbs_adr_i;
2743
input [2:0] wbs_cti_i;
2744
input [1:0] wbs_bte_i;
2745
input [dat_width/8-1:0] wbs_sel_i;
2746 70 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
2747 101 unneback
output [dat_width-1:0] wbs_dat_o;
2748 70 unneback
output wbs_ack_o;
2749 101 unneback
output wbs_stall_o;
2750 71 unneback
input wb_clk, wb_rst;
2751 101 unneback
wire [adr_width-1:0] adr;
2752
wire we;
2753
generate
2754
if (mode=="B3") begin : B3_inst
2755
vl_wb_adr_inc # ( .adr_width(adr_width), .max_burst_width(max_burst_width)) adr_inc0 (
2756 83 unneback
    .cyc_i(wbs_cyc_i),
2757
    .stb_i(wbs_stb_i),
2758
    .cti_i(wbs_cti_i),
2759
    .bte_i(wbs_bte_i),
2760
    .adr_i(wbs_adr_i),
2761 85 unneback
    .we_i(wbs_we_i),
2762 83 unneback
    .ack_o(wbs_ack_o),
2763
    .adr_o(adr),
2764
    .clk(wb_clk),
2765
    .rst(wb_rst));
2766 101 unneback
assign we = wbs_we_i & wbs_ack_o;
2767
end else if (mode=="B4") begin : B4_inst
2768
reg wbs_ack_o_reg;
2769
always @ (posedge wb_clk or posedge wb_rst)
2770
    if (wb_rst)
2771
        wbs_ack_o_reg <= 1'b0;
2772
    else
2773
        wbs_ack_o_reg <= wbs_stb_i & wbs_cyc_i;
2774
assign wbs_ack_o = wbs_ack_o_reg;
2775
assign wbs_stall_o = 1'b0;
2776
assign adr = wbs_adr_i;
2777
assign we = wbs_we_i & wbs_cyc_i & wbs_stb_i;
2778
end
2779
endgenerate
2780 100 unneback
vl_ram_be # (
2781
    .data_width(dat_width),
2782
    .addr_width(adr_width),
2783
    .mem_size(mem_size),
2784
    .memory_init(memory_init),
2785
    .memory_file(memory_file))
2786
ram0(
2787 101 unneback
    .d(wbs_dat_i),
2788
    .adr(adr),
2789
    .be(wbs_sel_i),
2790
    .we(we),
2791
    .q(wbs_dat_o),
2792 100 unneback
    .clk(wb_clk)
2793
);
2794 49 unneback
endmodule
2795 17 unneback
// WB ROM
2796 48 unneback
module vl_wb_b4_rom (
2797
    wb_adr_i, wb_stb_i, wb_cyc_i,
2798
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
2799
    parameter dat_width = 32;
2800
    parameter dat_default = 32'h15000000;
2801
    parameter adr_width = 32;
2802
/*
2803
`ifndef ROM
2804
`define ROM "rom.v"
2805
`endif
2806
*/
2807
    input [adr_width-1:2]   wb_adr_i;
2808
    input                   wb_stb_i;
2809
    input                   wb_cyc_i;
2810
    output [dat_width-1:0]  wb_dat_o;
2811
    reg [dat_width-1:0]     wb_dat_o;
2812
    output                  wb_ack_o;
2813
    reg                     wb_ack_o;
2814
    output                  stall_o;
2815
    input                   wb_clk;
2816
    input                   wb_rst;
2817
always @ (posedge wb_clk or posedge wb_rst)
2818
    if (wb_rst)
2819
        wb_dat_o <= {dat_width{1'b0}};
2820
    else
2821
         case (wb_adr_i[adr_width-1:2])
2822
`ifdef ROM
2823
`include `ROM
2824
`endif
2825
           default:
2826
             wb_dat_o <= dat_default;
2827
         endcase // case (wb_adr_i)
2828
always @ (posedge wb_clk or posedge wb_rst)
2829
    if (wb_rst)
2830
        wb_ack_o <= 1'b0;
2831
    else
2832
        wb_ack_o <= wb_stb_i & wb_cyc_i;
2833
assign stall_o = 1'b0;
2834
endmodule
2835
// WB ROM
2836 18 unneback
module vl_wb_boot_rom (
2837 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
2838 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
2839
    parameter adr_hi = 31;
2840
    parameter adr_lo = 28;
2841
    parameter adr_sel = 4'hf;
2842
    parameter addr_width = 5;
2843 33 unneback
/*
2844 17 unneback
`ifndef BOOT_ROM
2845
`define BOOT_ROM "boot_rom.v"
2846
`endif
2847 33 unneback
*/
2848 18 unneback
    input [adr_hi:2]    wb_adr_i;
2849
    input               wb_stb_i;
2850
    input               wb_cyc_i;
2851
    output [31:0]        wb_dat_o;
2852
    output              wb_ack_o;
2853
    output              hit_o;
2854
    input               wb_clk;
2855
    input               wb_rst;
2856
    wire hit;
2857
    reg [31:0] wb_dat;
2858
    reg wb_ack;
2859
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
2860 17 unneback
always @ (posedge wb_clk or posedge wb_rst)
2861
    if (wb_rst)
2862 18 unneback
        wb_dat <= 32'h15000000;
2863 17 unneback
    else
2864 18 unneback
         case (wb_adr_i[addr_width-1:2])
2865 33 unneback
`ifdef BOOT_ROM
2866 17 unneback
`include `BOOT_ROM
2867 33 unneback
`endif
2868 17 unneback
           /*
2869
            // Zero r0 and jump to 0x00000100
2870 18 unneback
 
2871
            1 : wb_dat <= 32'hA8200000;
2872
            2 : wb_dat <= 32'hA8C00100;
2873
            3 : wb_dat <= 32'h44003000;
2874
            4 : wb_dat <= 32'h15000000;
2875 17 unneback
            */
2876
           default:
2877 18 unneback
             wb_dat <= 32'h00000000;
2878 17 unneback
         endcase // case (wb_adr_i)
2879
always @ (posedge wb_clk or posedge wb_rst)
2880
    if (wb_rst)
2881 18 unneback
        wb_ack <= 1'b0;
2882 17 unneback
    else
2883 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
2884
assign hit_o = hit;
2885
assign wb_dat_o = wb_dat & {32{wb_ack}};
2886
assign wb_ack_o = wb_ack;
2887 17 unneback
endmodule
2888 101 unneback
module vl_wb_cache (
2889 98 unneback
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i, wbs_dat_o, wbs_ack_o, wbs_clk, wbs_rst,
2890
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o, wbm_dat_i, wbm_ack_i, wbm_stall_i, wbm_clk, wbm_rst
2891 97 unneback
);
2892
parameter dw_s = 32;
2893
parameter aw_s = 24;
2894
parameter dw_m = dw_s;
2895 100 unneback
localparam aw_m = dw_s * aw_s / dw_m;
2896
parameter wbs_max_burst_width = 4;
2897 97 unneback
parameter async = 1; // wbs_clk != wbm_clk
2898
parameter nr_of_ways = 1;
2899
parameter aw_offset = 4; // 4 => 16 words per cache line
2900
parameter aw_slot = 10;
2901 100 unneback
parameter valid_mem = 0;
2902
parameter debug = 0;
2903
localparam aw_b_offset = aw_offset * dw_s / dw_m;
2904 98 unneback
localparam aw_tag = aw_s - aw_slot - aw_offset;
2905 97 unneback
parameter wbm_burst_size = 4; // valid options 4,8,16
2906 98 unneback
localparam bte = (wbm_burst_size==4) ? 2'b01 : (wbm_burst_size==8) ? 2'b10 : 2'b11;
2907 100 unneback
localparam wbm_burst_width = (wbm_burst_size==1) ? 0 : (wbm_burst_size==2) ? 1 : (wbm_burst_size==4) ? 2 : (wbm_burst_size==8) ? 3 : (wbm_burst_size==16) ? 4 : (wbm_burst_size==32) ? 5 : (wbm_burst_size==64) ? 6 : (wbm_burst_size==128) ? 7 : 8;
2908 97 unneback
localparam nr_of_wbm_burst = ((1<<aw_offset)/wbm_burst_size) * dw_s / dw_m;
2909 100 unneback
localparam nr_of_wbm_burst_width = (nr_of_wbm_burst==1) ? 0 : (nr_of_wbm_burst==2) ? 1 : (nr_of_wbm_burst==4) ? 2 : (nr_of_wbm_burst==8) ? 3 : (nr_of_wbm_burst==16) ? 4 : (nr_of_wbm_burst==32) ? 5 : (nr_of_wbm_burst==64) ? 6 : (nr_of_wbm_burst==128) ? 7 : 8;
2910 97 unneback
input [dw_s-1:0] wbs_dat_i;
2911
input [aw_s-1:0] wbs_adr_i; // dont include a1,a0
2912 98 unneback
input [dw_s/8-1:0] wbs_sel_i;
2913 97 unneback
input [2:0] wbs_cti_i;
2914
input [1:0] wbs_bte_i;
2915 98 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
2916 97 unneback
output [dw_s-1:0] wbs_dat_o;
2917
output wbs_ack_o;
2918
input wbs_clk, wbs_rst;
2919
output [dw_m-1:0] wbm_dat_o;
2920
output [aw_m-1:0] wbm_adr_o;
2921
output [dw_m/8-1:0] wbm_sel_o;
2922
output [2:0] wbm_cti_o;
2923
output [1:0] wbm_bte_o;
2924 98 unneback
output wbm_stb_o, wbm_cyc_o, wbm_we_o;
2925 97 unneback
input [dw_m-1:0] wbm_dat_i;
2926
input wbm_ack_i;
2927
input wbm_stall_i;
2928
input wbm_clk, wbm_rst;
2929 100 unneback
wire valid, dirty, hit;
2930 97 unneback
wire [aw_tag-1:0] tag;
2931
wire tag_mem_we;
2932
wire [aw_tag-1:0] wbs_adr_tag;
2933
wire [aw_slot-1:0] wbs_adr_slot;
2934 98 unneback
wire [aw_offset-1:0] wbs_adr_word;
2935
wire [aw_s-1:0] wbs_adr;
2936 97 unneback
reg [1:0] state;
2937
localparam idle = 2'h0;
2938
localparam rdwr = 2'h1;
2939
localparam push = 2'h2;
2940
localparam pull = 2'h3;
2941
wire eoc;
2942
// cdc
2943
wire done, mem_alert, mem_done;
2944 98 unneback
// wbm side
2945
reg [aw_m-1:0] wbm_radr;
2946
reg [aw_m-1:0] wbm_wadr;
2947 100 unneback
wire [aw_slot-1:0] wbm_adr;
2948 98 unneback
wire wbm_radr_cke, wbm_wadr_cke;
2949 100 unneback
reg [2:0] phase;
2950
// phase = {we,stb,cyc}
2951
localparam wbm_wait     = 3'b000;
2952
localparam wbm_wr       = 3'b111;
2953
localparam wbm_wr_drain = 3'b101;
2954
localparam wbm_rd       = 3'b011;
2955
localparam wbm_rd_drain = 3'b001;
2956 97 unneback
assign {wbs_adr_tag, wbs_adr_slot, wbs_adr_word} = wbs_adr_i;
2957
assign eoc = (wbs_cti_i==3'b000 | wbs_cti_i==3'b111) & wbs_ack_o;
2958 100 unneback
generate
2959
if (valid_mem==0) begin : no_valid_mem
2960
assign valid = 1'b1;
2961
end else begin : valid_mem_inst
2962
vl_dpram_1r1w
2963
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
2964
    valid_mem ( .d_a(1'b1), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
2965
                .q_b(valid), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
2966
end
2967
endgenerate
2968
vl_dpram_1r1w
2969
    # ( .data_width(aw_tag), .addr_width(aw_slot), .memory_init(2), .debug(debug))
2970
    tag_mem ( .d_a(wbs_adr_tag), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
2971
              .q_b(tag), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
2972
assign hit = wbs_adr_tag == tag;
2973
vl_dpram_1r2w
2974
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
2975
    dirty_mem (
2976
        .d_a(1'b1), .q_a(dirty), .adr_a(wbs_adr_slot), .we_a(wbs_cyc_i & wbs_we_i & wbs_ack_o), .clk_a(wbs_clk),
2977
        .d_b(1'b0), .adr_b(wbs_adr_slot), .we_b(mem_done), .clk_b(wbm_clk));
2978
vl_wb_adr_inc # ( .adr_width(aw_s), .max_burst_width(wbs_max_burst_width)) adr_inc0 (
2979
    .cyc_i(wbs_cyc_i & (state==rdwr) & hit & valid),
2980
    .stb_i(wbs_stb_i & (state==rdwr) & hit & valid), // throttle depending on valid
2981 97 unneback
    .cti_i(wbs_cti_i),
2982
    .bte_i(wbs_bte_i),
2983
    .adr_i(wbs_adr_i),
2984
    .we_i (wbs_we_i),
2985
    .ack_o(wbs_ack_o),
2986
    .adr_o(wbs_adr),
2987 100 unneback
    .clk(wbs_clk),
2988
    .rst(wbs_rst));
2989 97 unneback
vl_dpram_be_2r2w
2990 100 unneback
    # ( .a_data_width(dw_s), .a_addr_width(aw_slot+aw_offset), .b_data_width(dw_m), .debug(debug))
2991
    cache_mem ( .d_a(wbs_dat_i), .adr_a(wbs_adr[aw_slot+aw_offset-1:0]),   .be_a(wbs_sel_i), .we_a(wbs_cyc_i &  wbs_we_i & wbs_ack_o), .q_a(wbs_dat_o), .clk_a(wbs_clk),
2992
                .d_b(wbm_dat_i), .adr_b(wbm_adr_o[aw_slot+aw_offset-1:0]), .be_b(wbm_sel_o), .we_b(wbm_cyc_o & !wbm_we_o & wbs_ack_i), .q_b(wbm_dat_o), .clk_b(wbm_clk));
2993
//                .d_b(wbm_dat_i), .adr_b(wbm_adr), .be_b(wbm_sel_o), .we_b(wbm_cyc_o & !wbm_we_o & wbs_ack_i), .q_b(wbm_dat_o), .clk_b(wbm_clk));
2994 97 unneback
always @ (posedge wbs_clk or posedge wbs_rst)
2995
if (wbs_rst)
2996 98 unneback
    state <= idle;
2997 97 unneback
else
2998
    case (state)
2999
    idle:
3000
        if (wbs_cyc_i)
3001
            state <= rdwr;
3002
    rdwr:
3003 100 unneback
        casex ({valid, hit, dirty, eoc})
3004
        4'b0xxx: state <= pull;
3005
        4'b11x1: state <= idle;
3006
        4'b101x: state <= push;
3007
        4'b100x: state <= pull;
3008
        endcase
3009 97 unneback
    push:
3010
        if (done)
3011
            state <= rdwr;
3012
    pull:
3013
        if (done)
3014
            state <= rdwr;
3015
    default: state <= idle;
3016
    endcase
3017
// cdc
3018
generate
3019
if (async==1) begin : cdc0
3020 100 unneback
vl_cdc cdc0 ( .start_pl(state==rdwr & (!valid | !hit)), .take_it_pl(mem_alert), .take_it_grant_pl(mem_done), .got_it_pl(done), .clk_src(wbs_clk), .rst_src(wbs_rst), .clk_dst(wbm_clk), .rst_dst(wbm_rst));
3021 97 unneback
end
3022
else begin : nocdc
3023 100 unneback
    assign mem_alert = state==rdwr & (!valid | !hit);
3024 97 unneback
    assign done = mem_done;
3025
end
3026
endgenerate
3027
// FSM generating a number of burts 4 cycles
3028
// actual number depends on data width ratio
3029
// nr_of_wbm_burst
3030 101 unneback
reg [nr_of_wbm_burst_width+wbm_burst_width-1:0]       cnt_rw, cnt_ack;
3031 97 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
3032
if (wbm_rst)
3033 100 unneback
    cnt_rw <= {wbm_burst_width{1'b0}};
3034 97 unneback
else
3035 100 unneback
    if (wbm_cyc_o & wbm_stb_o & !wbm_stall_i)
3036
        cnt_rw <= cnt_rw + 1;
3037 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
3038
if (wbm_rst)
3039 100 unneback
    cnt_ack <= {wbm_burst_width{1'b0}};
3040 98 unneback
else
3041 100 unneback
    if (wbm_ack_i)
3042
        cnt_ack <= cnt_ack + 1;
3043
generate
3044 101 unneback
if (nr_of_wbm_burst==1) begin : one_burst
3045 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
3046
if (wbm_rst)
3047
    phase <= wbm_wait;
3048
else
3049
    case (phase)
3050
    wbm_wait:
3051
        if (mem_alert)
3052 100 unneback
            if (state==push)
3053
                phase <= wbm_wr;
3054
            else
3055
                phase <= wbm_rd;
3056 98 unneback
    wbm_wr:
3057 100 unneback
        if (&cnt_rw)
3058
            phase <= wbm_wr_drain;
3059
    wbm_wr_drain:
3060
        if (&cnt_ack)
3061 98 unneback
            phase <= wbm_rd;
3062
    wbm_rd:
3063 100 unneback
        if (&cnt_rw)
3064
            phase <= wbm_rd_drain;
3065
    wbm_rd_drain:
3066
        if (&cnt_ack)
3067
            phase <= wbm_wait;
3068 98 unneback
    default: phase <= wbm_wait;
3069
    endcase
3070 100 unneback
end else begin : multiple_burst
3071 101 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
3072
if (wbm_rst)
3073
    phase <= wbm_wait;
3074
else
3075
    case (phase)
3076
    wbm_wait:
3077
        if (mem_alert)
3078
            if (state==push)
3079
                phase <= wbm_wr;
3080
            else
3081
                phase <= wbm_rd;
3082
    wbm_wr:
3083
        if (&cnt_rw[wbm_burst_width-1:0])
3084
            phase <= wbm_wr_drain;
3085
    wbm_wr_drain:
3086
        if (&cnt_ack)
3087
            phase <= wbm_rd;
3088
        else if (&cnt_ack[wbm_burst_width-1:0])
3089
            phase <= wbm_wr;
3090
    wbm_rd:
3091
        if (&cnt_rw[wbm_burst_width-1:0])
3092
            phase <= wbm_rd_drain;
3093
    wbm_rd_drain:
3094
        if (&cnt_ack)
3095
            phase <= wbm_wait;
3096
        else if (&cnt_ack[wbm_burst_width-1:0])
3097
            phase <= wbm_rd;
3098
    default: phase <= wbm_wait;
3099
    endcase
3100 100 unneback
end
3101
endgenerate
3102 101 unneback
assign mem_done = phase==wbm_rd_drain & (&cnt_ack) & wbm_ack_i;
3103 100 unneback
assign wbm_adr_o = (phase[2]) ? {tag, wbs_adr_slot, cnt_rw} : {wbs_adr_tag, wbs_adr_slot, cnt_rw};
3104
assign wbm_adr   = (phase[2]) ? {wbs_adr_slot, cnt_rw} : {wbs_adr_slot, cnt_rw};
3105
assign wbm_sel_o = {dw_m/8{1'b1}};
3106
assign wbm_cti_o = (&cnt_rw | !wbm_stb_o) ? 3'b111 : 3'b010;
3107 98 unneback
assign wbm_bte_o = bte;
3108 100 unneback
assign {wbm_we_o, wbm_stb_o, wbm_cyc_o}  = phase;
3109 97 unneback
endmodule
3110 18 unneback
//////////////////////////////////////////////////////////////////////
3111
////                                                              ////
3112
////  Arithmetic functions                                        ////
3113
////                                                              ////
3114
////  Description                                                 ////
3115
////  Arithmetic functions for ALU and DSP                        ////
3116
////                                                              ////
3117
////                                                              ////
3118
////  To Do:                                                      ////
3119
////   -                                                          ////
3120
////                                                              ////
3121
////  Author(s):                                                  ////
3122
////      - Michael Unneback, unneback@opencores.org              ////
3123
////        ORSoC AB                                              ////
3124
////                                                              ////
3125
//////////////////////////////////////////////////////////////////////
3126
////                                                              ////
3127
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3128
////                                                              ////
3129
//// This source file may be used and distributed without         ////
3130
//// restriction provided that this copyright statement is not    ////
3131
//// removed from the file and that any derivative work contains  ////
3132
//// the original copyright notice and the associated disclaimer. ////
3133
////                                                              ////
3134
//// This source file is free software; you can redistribute it   ////
3135
//// and/or modify it under the terms of the GNU Lesser General   ////
3136
//// Public License as published by the Free Software Foundation; ////
3137
//// either version 2.1 of the License, or (at your option) any   ////
3138
//// later version.                                               ////
3139
////                                                              ////
3140
//// This source is distributed in the hope that it will be       ////
3141
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3142
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3143
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3144
//// details.                                                     ////
3145
////                                                              ////
3146
//// You should have received a copy of the GNU Lesser General    ////
3147
//// Public License along with this source; if not, download it   ////
3148
//// from http://www.opencores.org/lgpl.shtml                     ////
3149
////                                                              ////
3150
//////////////////////////////////////////////////////////////////////
3151
// signed multiplication
3152
module vl_mults (a,b,p);
3153
parameter operand_a_width = 18;
3154
parameter operand_b_width = 18;
3155
parameter result_hi = 35;
3156
parameter result_lo = 0;
3157
input [operand_a_width-1:0] a;
3158
input [operand_b_width-1:0] b;
3159
output [result_hi:result_lo] p;
3160
wire signed [operand_a_width-1:0] ai;
3161
wire signed [operand_b_width-1:0] bi;
3162
wire signed [operand_a_width+operand_b_width-1:0] result;
3163
    assign ai = a;
3164
    assign bi = b;
3165
    assign result = ai * bi;
3166
    assign p = result[result_hi:result_lo];
3167
endmodule
3168
module vl_mults18x18 (a,b,p);
3169
input [17:0] a,b;
3170
output [35:0] p;
3171
vl_mult
3172
    # (.operand_a_width(18), .operand_b_width(18))
3173
    mult0 (.a(a), .b(b), .p(p));
3174
endmodule
3175
// unsigned multiplication
3176
module vl_mult (a,b,p);
3177
parameter operand_a_width = 18;
3178
parameter operand_b_width = 18;
3179
parameter result_hi = 35;
3180
parameter result_lo = 0;
3181
input [operand_a_width-1:0] a;
3182
input [operand_b_width-1:0] b;
3183
output [result_hi:result_hi] p;
3184
wire [operand_a_width+operand_b_width-1:0] result;
3185
    assign result = a * b;
3186
    assign p = result[result_hi:result_lo];
3187
endmodule
3188
// shift unit
3189
// supporting the following shift functions
3190
//   SLL
3191
//   SRL
3192
//   SRA
3193
module vl_shift_unit_32( din, s, dout, opcode);
3194
input [31:0] din; // data in operand
3195
input [4:0] s; // shift operand
3196
input [1:0] opcode;
3197
output [31:0] dout;
3198
parameter opcode_sll = 2'b00;
3199
//parameter opcode_srl = 2'b01;
3200
parameter opcode_sra = 2'b10;
3201
//parameter opcode_ror = 2'b11;
3202
wire sll, sra;
3203
assign sll = opcode == opcode_sll;
3204
assign sra = opcode == opcode_sra;
3205
wire [15:1] s1;
3206
wire [3:0] sign;
3207
wire [7:0] tmp [0:3];
3208
// first stage is multiplier based
3209
// shift operand as fractional 8.7
3210
assign s1[15] = sll & s[2:0]==3'd7;
3211
assign s1[14] = sll & s[2:0]==3'd6;
3212
assign s1[13] = sll & s[2:0]==3'd5;
3213
assign s1[12] = sll & s[2:0]==3'd4;
3214
assign s1[11] = sll & s[2:0]==3'd3;
3215
assign s1[10] = sll & s[2:0]==3'd2;
3216
assign s1[ 9] = sll & s[2:0]==3'd1;
3217
assign s1[ 8] = s[2:0]==3'd0;
3218
assign s1[ 7] = !sll & s[2:0]==3'd1;
3219
assign s1[ 6] = !sll & s[2:0]==3'd2;
3220
assign s1[ 5] = !sll & s[2:0]==3'd3;
3221
assign s1[ 4] = !sll & s[2:0]==3'd4;
3222
assign s1[ 3] = !sll & s[2:0]==3'd5;
3223
assign s1[ 2] = !sll & s[2:0]==3'd6;
3224
assign s1[ 1] = !sll & s[2:0]==3'd7;
3225
assign sign[3] = din[31] & sra;
3226
assign sign[2] = sign[3] & (&din[31:24]);
3227
assign sign[1] = sign[2] & (&din[23:16]);
3228
assign sign[0] = sign[1] & (&din[15:8]);
3229
vl_mults # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7)) mult_byte3 ( .a({sign[3], {8{sign[3]}},din[31:24], din[23:16]}), .b({1'b0,s1}), .p(tmp[3]));
3230
vl_mults # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7)) mult_byte2 ( .a({sign[2], din[31:24]  ,din[23:16],  din[15:8]}), .b({1'b0,s1}), .p(tmp[2]));
3231
vl_mults # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7)) mult_byte1 ( .a({sign[1], din[23:16]  ,din[15:8],   din[7:0]}), .b({1'b0,s1}), .p(tmp[1]));
3232
vl_mults # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7)) mult_byte0 ( .a({sign[0], din[15:8]   ,din[7:0],    8'h00}),      .b({1'b0,s1}), .p(tmp[0]));
3233
// second stage is multiplexer based
3234
// shift on byte level
3235
// mux byte 3
3236
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
3237
                     (sll & s[4:3]==2'b01) ? tmp[2] :
3238
                     (sll & s[4:3]==2'b10) ? tmp[1] :
3239
                     (sll & s[4:3]==2'b11) ? tmp[0] :
3240
                     {8{sign[3]}};
3241
// mux byte 2
3242
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
3243
                     (sll & s[4:3]==2'b01) ? tmp[1] :
3244
                     (sll & s[4:3]==2'b10) ? tmp[0] :
3245
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
3246
                     (s[4:3]==2'b01) ? tmp[3] :
3247
                     {8{sign[3]}};
3248
// mux byte 1
3249
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
3250
                     (sll & s[4:3]==2'b01) ? tmp[0] :
3251
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
3252
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
3253
                     (s[4:3]==2'b01) ? tmp[2] :
3254
                     (s[4:3]==2'b10) ? tmp[3] :
3255
                     {8{sign[3]}};
3256
// mux byte 0
3257
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
3258
                     (sll) ?  {8{1'b0}}:
3259
                     (s[4:3]==2'b01) ? tmp[1] :
3260
                     (s[4:3]==2'b10) ? tmp[2] :
3261
                     tmp[3];
3262
endmodule
3263
// logic unit
3264
// supporting the following logic functions
3265
//    a and b
3266
//    a or  b
3267
//    a xor b
3268
//    not b
3269
module vl_logic_unit( a, b, result, opcode);
3270
parameter width = 32;
3271
parameter opcode_and = 2'b00;
3272
parameter opcode_or  = 2'b01;
3273
parameter opcode_xor = 2'b10;
3274
input [width-1:0] a,b;
3275
output [width-1:0] result;
3276
input [1:0] opcode;
3277
assign result = (opcode==opcode_and) ? a & b :
3278
                (opcode==opcode_or)  ? a | b :
3279
                (opcode==opcode_xor) ? a ^ b :
3280
                b;
3281
endmodule

powered by: WebSVN 2.1.0

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