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 106

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

powered by: WebSVN 2.1.0

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