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 90

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

Line No. Rev Author Line
1 60 unneback
// default SYN_KEEP definition
2 6 unneback
//////////////////////////////////////////////////////////////////////
3
////                                                              ////
4
////  Versatile library, clock and reset                          ////
5
////                                                              ////
6
////  Description                                                 ////
7
////  Logic related to clock and reset                            ////
8
////                                                              ////
9
////                                                              ////
10
////  To Do:                                                      ////
11
////   - add more different registers                             ////
12
////                                                              ////
13
////  Author(s):                                                  ////
14
////      - Michael Unneback, unneback@opencores.org              ////
15
////        ORSoC AB                                              ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE.  See the GNU Lesser General Public License for more ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43 48 unneback
`timescale 1 ns/100 ps
44 6 unneback
// Global buffer
45
// usage:
46
// use to enable global buffers for high fan out signals such as clock and reset
47
// Version: 8.4 8.4.0.33
48
module gbuf(GL,CLK);
49
output GL;
50
input  CLK;
51
    wire GND;
52
    GND GND_1_net(.Y(GND));
53
    CLKDLY Inst1(.CLK(CLK), .GL(GL), .DLYGL0(GND), .DLYGL1(GND),
54
        .DLYGL2(GND), .DLYGL3(GND), .DLYGL4(GND)) /* synthesis black_box */;
55
endmodule
56
`timescale 1 ns/1 ns
57
module vl_gbuf ( i, o);
58
input i;
59
output o;
60
`ifdef SIM_GBUF
61
assign o=i;
62
`else
63
gbuf gbuf_i0 ( .CLK(i), .GL(o));
64
`endif
65
endmodule
66
 //ACTEL
67
// sync reset
68 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
69 6 unneback
// output active high global reset sync with two DFFs 
70
`timescale 1 ns/100 ps
71
module vl_sync_rst ( rst_n_i, rst_o, clk);
72
input rst_n_i, clk;
73
output rst_o;
74 18 unneback
reg [1:0] tmp;
75 6 unneback
always @ (posedge clk or negedge rst_n_i)
76
if (!rst_n_i)
77 17 unneback
        tmp <= 2'b11;
78 6 unneback
else
79 33 unneback
        tmp <= {1'b0,tmp[1]};
80 17 unneback
vl_gbuf buf_i0( .i(tmp[0]), .o(rst_o));
81 6 unneback
endmodule
82
// vl_pll
83 32 unneback
///////////////////////////////////////////////////////////////////////////////
84 17 unneback
`timescale 1 ps/1 ps
85 6 unneback
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
86
parameter index = 0;
87
parameter number_of_clk = 1;
88 17 unneback
parameter period_time_0 = 20000;
89
parameter period_time_1 = 20000;
90
parameter period_time_2 = 20000;
91
parameter lock_delay = 2000000;
92 6 unneback
input clk_i, rst_n_i;
93
output lock;
94
output reg [0:number_of_clk-1] clk_o;
95
output [0:number_of_clk-1] rst_o;
96
`ifdef SIM_PLL
97
always
98
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
99
generate if (number_of_clk > 1)
100
always
101
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
102
endgenerate
103
generate if (number_of_clk > 2)
104
always
105
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
106
endgenerate
107
genvar i;
108
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
109
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
110
end
111
endgenerate
112
assign #lock_delay lock = rst_n_i;
113
endmodule
114
`else
115
generate if (number_of_clk==1 & index==0) begin
116
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
117
end
118
endgenerate // index==0
119
generate if (number_of_clk==1 & index==1) begin
120
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
121
end
122
endgenerate // index==1
123
generate if (number_of_clk==1 & index==2) begin
124
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
125
end
126
endgenerate // index==2
127
generate if (number_of_clk==1 & index==3) begin
128
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]));
129
end
130
endgenerate // index==0
131
generate if (number_of_clk==2 & index==0) begin
132
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
133
end
134
endgenerate // index==0
135
generate if (number_of_clk==2 & index==1) begin
136
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
137
end
138
endgenerate // index==1
139
generate if (number_of_clk==2 & index==2) begin
140
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
141
end
142
endgenerate // index==2
143
generate if (number_of_clk==2 & index==3) begin
144
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]));
145
end
146
endgenerate // index==0
147
generate if (number_of_clk==3 & index==0) begin
148
        pll0 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
149
end
150
endgenerate // index==0
151
generate if (number_of_clk==3 & index==1) begin
152
        pll1 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
153
end
154
endgenerate // index==1
155
generate if (number_of_clk==3 & index==2) begin
156
        pll2 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
157
end
158
endgenerate // index==2
159
generate if (number_of_clk==3 & index==3) begin
160
        pll3 pll_i0 (.POWERDOWN(1'b1), .CLKA(clk_i), .LOCK(lock), .GLA(clk_o[0]), .GLB(clk_o[1]), .GLC(clk_o[2]));
161
end
162
endgenerate // index==0
163
genvar i;
164
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
165 40 unneback
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o), .clk(clk_o[i]));
166 6 unneback
end
167
endgenerate
168
endmodule
169
`endif
170 32 unneback
///////////////////////////////////////////////////////////////////////////////
171 6 unneback
 //actel
172
//////////////////////////////////////////////////////////////////////
173
////                                                              ////
174
////  Versatile library, registers                                ////
175
////                                                              ////
176
////  Description                                                 ////
177
////  Different type of registers                                 ////
178
////                                                              ////
179
////                                                              ////
180
////  To Do:                                                      ////
181
////   - add more different registers                             ////
182
////                                                              ////
183
////  Author(s):                                                  ////
184
////      - Michael Unneback, unneback@opencores.org              ////
185
////        ORSoC AB                                              ////
186
////                                                              ////
187
//////////////////////////////////////////////////////////////////////
188
////                                                              ////
189
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
190
////                                                              ////
191
//// This source file may be used and distributed without         ////
192
//// restriction provided that this copyright statement is not    ////
193
//// removed from the file and that any derivative work contains  ////
194
//// the original copyright notice and the associated disclaimer. ////
195
////                                                              ////
196
//// This source file is free software; you can redistribute it   ////
197
//// and/or modify it under the terms of the GNU Lesser General   ////
198
//// Public License as published by the Free Software Foundation; ////
199
//// either version 2.1 of the License, or (at your option) any   ////
200
//// later version.                                               ////
201
////                                                              ////
202
//// This source is distributed in the hope that it will be       ////
203
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
204
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
205
//// PURPOSE.  See the GNU Lesser General Public License for more ////
206
//// details.                                                     ////
207
////                                                              ////
208
//// You should have received a copy of the GNU Lesser General    ////
209
//// Public License along with this source; if not, download it   ////
210
//// from http://www.opencores.org/lgpl.shtml                     ////
211
////                                                              ////
212
//////////////////////////////////////////////////////////////////////
213 18 unneback
module vl_dff ( d, q, clk, rst);
214 6 unneback
        parameter width = 1;
215
        parameter reset_value = 0;
216
        input [width-1:0] d;
217
        input clk, rst;
218
        output reg [width-1:0] q;
219
        always @ (posedge clk or posedge rst)
220
        if (rst)
221
                q <= reset_value;
222
        else
223
                q <= d;
224
endmodule
225 18 unneback
module vl_dff_array ( d, q, clk, rst);
226 6 unneback
        parameter width = 1;
227
        parameter depth = 2;
228
        parameter reset_value = 1'b0;
229
        input [width-1:0] d;
230
        input clk, rst;
231
        output [width-1:0] q;
232
        reg  [0:depth-1] q_tmp [width-1:0];
233
        integer i;
234
        always @ (posedge clk or posedge rst)
235
        if (rst) begin
236
            for (i=0;i<depth;i=i+1)
237
                q_tmp[i] <= {width{reset_value}};
238
        end else begin
239
            q_tmp[0] <= d;
240
            for (i=1;i<depth;i=i+1)
241
                q_tmp[i] <= q_tmp[i-1];
242
        end
243
    assign q = q_tmp[depth-1];
244
endmodule
245 18 unneback
module vl_dff_ce ( d, ce, q, clk, rst);
246 6 unneback
        parameter width = 1;
247
        parameter reset_value = 0;
248
        input [width-1:0] d;
249
        input ce, clk, rst;
250
        output reg [width-1:0] q;
251
        always @ (posedge clk or posedge rst)
252
        if (rst)
253
                q <= reset_value;
254
        else
255
                if (ce)
256
                        q <= d;
257
endmodule
258 18 unneback
module vl_dff_ce_clear ( d, ce, clear, q, clk, rst);
259 8 unneback
        parameter width = 1;
260
        parameter reset_value = 0;
261
        input [width-1:0] d;
262 10 unneback
        input ce, clear, clk, rst;
263 8 unneback
        output reg [width-1:0] q;
264
        always @ (posedge clk or posedge rst)
265
        if (rst)
266
            q <= reset_value;
267
        else
268
            if (ce)
269
                if (clear)
270
                    q <= {width{1'b0}};
271
                else
272
                    q <= d;
273
endmodule
274 24 unneback
module vl_dff_ce_set ( d, ce, set, q, clk, rst);
275
        parameter width = 1;
276
        parameter reset_value = 0;
277
        input [width-1:0] d;
278
        input ce, set, clk, rst;
279
        output reg [width-1:0] q;
280
        always @ (posedge clk or posedge rst)
281
        if (rst)
282
            q <= reset_value;
283
        else
284
            if (ce)
285
                if (set)
286
                    q <= {width{1'b1}};
287
                else
288
                    q <= d;
289
endmodule
290 29 unneback
module vl_spr ( sp, r, q, clk, rst);
291 64 unneback
        //parameter width = 1;
292
        parameter reset_value = 1'b0;
293 29 unneback
        input sp, r;
294
        output reg q;
295
        input clk, rst;
296
        always @ (posedge clk or posedge rst)
297
        if (rst)
298
            q <= reset_value;
299
        else
300
            if (sp)
301
                q <= 1'b1;
302
            else if (r)
303
                q <= 1'b0;
304
endmodule
305
module vl_srp ( s, rp, q, clk, rst);
306
        parameter width = 1;
307
        parameter reset_value = 0;
308
        input s, rp;
309
        output reg q;
310
        input clk, rst;
311
        always @ (posedge clk or posedge rst)
312
        if (rst)
313
            q <= reset_value;
314
        else
315
            if (rp)
316
                q <= 1'b0;
317
            else if (s)
318
                q <= 1'b1;
319
endmodule
320 18 unneback
module vl_dff_sr ( aclr, aset, clock, data, q);
321 6 unneback
    input         aclr;
322
    input         aset;
323
    input         clock;
324
    input         data;
325
    output reg    q;
326
   always @ (posedge clock or posedge aclr or posedge aset)
327
     if (aclr)
328
       q <= 1'b0;
329
     else if (aset)
330
       q <= 1'b1;
331
     else
332
       q <= data;
333
endmodule
334
// LATCH
335
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
336 40 unneback
module vl_latch ( d, le, q, clk);
337 6 unneback
input d, le;
338 48 unneback
input clk;
339
always @ (le or d)
340 60 unneback
if (le)
341 48 unneback
    d <= q;
342 6 unneback
endmodule
343 18 unneback
module vl_shreg ( d, q, clk, rst);
344 17 unneback
parameter depth = 10;
345
input d;
346
output q;
347
input clk, rst;
348
reg [1:depth] dffs;
349
always @ (posedge clk or posedge rst)
350
if (rst)
351
    dffs <= {depth{1'b0}};
352
else
353
    dffs <= {d,dffs[1:depth-1]};
354
assign q = dffs[depth];
355
endmodule
356 18 unneback
module vl_shreg_ce ( d, ce, q, clk, rst);
357 17 unneback
parameter depth = 10;
358
input d, ce;
359
output q;
360
input clk, rst;
361
reg [1:depth] dffs;
362
always @ (posedge clk or posedge rst)
363
if (rst)
364
    dffs <= {depth{1'b0}};
365
else
366
    if (ce)
367
        dffs <= {d,dffs[1:depth-1]};
368
assign q = dffs[depth];
369
endmodule
370 18 unneback
module vl_delay ( d, q, clk, rst);
371 15 unneback
parameter depth = 10;
372
input d;
373
output q;
374
input clk, rst;
375
reg [1:depth] dffs;
376
always @ (posedge clk or posedge rst)
377
if (rst)
378
    dffs <= {depth{1'b0}};
379
else
380
    dffs <= {d,dffs[1:depth-1]};
381
assign q = dffs[depth];
382
endmodule
383 18 unneback
module vl_delay_emptyflag ( d, q, emptyflag, clk, rst);
384 17 unneback
parameter depth = 10;
385
input d;
386
output q, emptyflag;
387
input clk, rst;
388
reg [1:depth] dffs;
389
always @ (posedge clk or posedge rst)
390
if (rst)
391
    dffs <= {depth{1'b0}};
392
else
393
    dffs <= {d,dffs[1:depth-1]};
394
assign q = dffs[depth];
395
assign emptyflag = !(|dffs);
396
endmodule
397 6 unneback
//////////////////////////////////////////////////////////////////////
398
////                                                              ////
399 18 unneback
////  Logic functions                                             ////
400
////                                                              ////
401
////  Description                                                 ////
402
////  Logic functions such as multiplexers                        ////
403
////                                                              ////
404
////                                                              ////
405
////  To Do:                                                      ////
406
////   -                                                          ////
407
////                                                              ////
408
////  Author(s):                                                  ////
409
////      - Michael Unneback, unneback@opencores.org              ////
410
////        ORSoC AB                                              ////
411
////                                                              ////
412
//////////////////////////////////////////////////////////////////////
413
////                                                              ////
414
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
415
////                                                              ////
416
//// This source file may be used and distributed without         ////
417
//// restriction provided that this copyright statement is not    ////
418
//// removed from the file and that any derivative work contains  ////
419
//// the original copyright notice and the associated disclaimer. ////
420
////                                                              ////
421
//// This source file is free software; you can redistribute it   ////
422
//// and/or modify it under the terms of the GNU Lesser General   ////
423
//// Public License as published by the Free Software Foundation; ////
424
//// either version 2.1 of the License, or (at your option) any   ////
425
//// later version.                                               ////
426
////                                                              ////
427
//// This source is distributed in the hope that it will be       ////
428
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
429
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
430
//// PURPOSE.  See the GNU Lesser General Public License for more ////
431
//// details.                                                     ////
432
////                                                              ////
433
//// You should have received a copy of the GNU Lesser General    ////
434
//// Public License along with this source; if not, download it   ////
435
//// from http://www.opencores.org/lgpl.shtml                     ////
436
////                                                              ////
437
//////////////////////////////////////////////////////////////////////
438 36 unneback
module vl_mux_andor ( a, sel, dout);
439
parameter width = 32;
440
parameter nr_of_ports = 4;
441
input [nr_of_ports*width-1:0] a;
442
input [nr_of_ports-1:0] sel;
443
output reg [width-1:0] dout;
444 38 unneback
integer i,j;
445 36 unneback
always @ (a, sel)
446
begin
447
    dout = a[width-1:0] & {width{sel[0]}};
448 42 unneback
    for (i=1;i<nr_of_ports;i=i+1)
449
        for (j=0;j<width;j=j+1)
450
            dout[j] = (a[i*width + j] & sel[i]) | dout[j];
451 36 unneback
end
452
endmodule
453 34 unneback
module vl_mux2_andor ( a1, a0, sel, dout);
454
parameter width = 32;
455 35 unneback
localparam nr_of_ports = 2;
456 34 unneback
input [width-1:0] a1, a0;
457
input [nr_of_ports-1:0] sel;
458
output [width-1:0] dout;
459 36 unneback
vl_mux_andor
460 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
461 36 unneback
    mux0( .a({a1,a0}), .sel(sel), .dout(dout));
462 34 unneback
endmodule
463
module vl_mux3_andor ( a2, a1, a0, sel, dout);
464
parameter width = 32;
465 35 unneback
localparam nr_of_ports = 3;
466 34 unneback
input [width-1:0] a2, a1, a0;
467
input [nr_of_ports-1:0] sel;
468
output [width-1:0] dout;
469 36 unneback
vl_mux_andor
470 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
471 36 unneback
    mux0( .a({a2,a1,a0}), .sel(sel), .dout(dout));
472 34 unneback
endmodule
473 18 unneback
module vl_mux4_andor ( a3, a2, a1, a0, sel, dout);
474
parameter width = 32;
475 35 unneback
localparam nr_of_ports = 4;
476 18 unneback
input [width-1:0] a3, a2, a1, a0;
477
input [nr_of_ports-1:0] sel;
478 22 unneback
output [width-1:0] dout;
479 36 unneback
vl_mux_andor
480 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
481 36 unneback
    mux0( .a({a3,a2,a1,a0}), .sel(sel), .dout(dout));
482 18 unneback
endmodule
483
module vl_mux5_andor ( a4, a3, a2, a1, a0, sel, dout);
484
parameter width = 32;
485 35 unneback
localparam nr_of_ports = 5;
486 18 unneback
input [width-1:0] a4, a3, a2, a1, a0;
487
input [nr_of_ports-1:0] sel;
488 22 unneback
output [width-1:0] dout;
489 36 unneback
vl_mux_andor
490 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
491 36 unneback
    mux0( .a({a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
492 18 unneback
endmodule
493
module vl_mux6_andor ( a5, a4, a3, a2, a1, a0, sel, dout);
494
parameter width = 32;
495 35 unneback
localparam nr_of_ports = 6;
496 18 unneback
input [width-1:0] a5, a4, a3, a2, a1, a0;
497
input [nr_of_ports-1:0] sel;
498 22 unneback
output [width-1:0] dout;
499 36 unneback
vl_mux_andor
500 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
501 36 unneback
    mux0( .a({a5,a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
502 18 unneback
endmodule
503 43 unneback
module vl_parity_generate (data, parity);
504
parameter word_size = 32;
505
parameter chunk_size = 8;
506
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
507
input [word_size-1:0] data;
508
output reg [word_size/chunk_size-1:0] parity;
509
integer i,j;
510
always @ (data)
511
for (i=0;i<word_size/chunk_size;i=i+1) begin
512
    parity[i] = parity_type;
513
    for (j=0;j<chunk_size;j=j+1) begin
514 46 unneback
        parity[i] = data[i*chunk_size+j] ^ parity[i];
515 43 unneback
    end
516
end
517
endmodule
518
module vl_parity_check( data, parity, parity_error);
519
parameter word_size = 32;
520
parameter chunk_size = 8;
521
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
522
input [word_size-1:0] data;
523
input [word_size/chunk_size-1:0] parity;
524
output parity_error;
525 44 unneback
reg [word_size/chunk_size-1:0] error_flag;
526 43 unneback
integer i,j;
527
always @ (data or parity)
528
for (i=0;i<word_size/chunk_size;i=i+1) begin
529
    error_flag[i] = parity[i] ^ parity_type;
530
    for (j=0;j<chunk_size;j=j+1) begin
531 46 unneback
        error_flag[i] = data[i*chunk_size+j] ^ error_flag[i];
532 43 unneback
    end
533
end
534
assign parity_error = |error_flag;
535
endmodule
536 18 unneback
//////////////////////////////////////////////////////////////////////
537
////                                                              ////
538 44 unneback
////  IO functions                                                ////
539
////                                                              ////
540
////  Description                                                 ////
541
////  IO functions such as IOB flip-flops                         ////
542
////                                                              ////
543
////                                                              ////
544
////  To Do:                                                      ////
545
////   -                                                          ////
546
////                                                              ////
547
////  Author(s):                                                  ////
548
////      - Michael Unneback, unneback@opencores.org              ////
549
////        ORSoC AB                                              ////
550
////                                                              ////
551
//////////////////////////////////////////////////////////////////////
552
////                                                              ////
553
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
554
////                                                              ////
555
//// This source file may be used and distributed without         ////
556
//// restriction provided that this copyright statement is not    ////
557
//// removed from the file and that any derivative work contains  ////
558
//// the original copyright notice and the associated disclaimer. ////
559
////                                                              ////
560
//// This source file is free software; you can redistribute it   ////
561
//// and/or modify it under the terms of the GNU Lesser General   ////
562
//// Public License as published by the Free Software Foundation; ////
563
//// either version 2.1 of the License, or (at your option) any   ////
564
//// later version.                                               ////
565
////                                                              ////
566
//// This source is distributed in the hope that it will be       ////
567
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
568
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
569
//// PURPOSE.  See the GNU Lesser General Public License for more ////
570
//// details.                                                     ////
571
////                                                              ////
572
//// You should have received a copy of the GNU Lesser General    ////
573
//// Public License along with this source; if not, download it   ////
574
//// from http://www.opencores.org/lgpl.shtml                     ////
575
////                                                              ////
576
//////////////////////////////////////////////////////////////////////
577 45 unneback
`timescale 1ns/1ns
578 44 unneback
module vl_o_dff (d_i, o_pad, clk, rst);
579
parameter width = 1;
580 45 unneback
parameter reset_value = {width{1'b0}};
581
input  [width-1:0]  d_i;
582 44 unneback
output [width-1:0] o_pad;
583
input clk, rst;
584
wire [width-1:0] d_i_int /*synthesis syn_keep = 1*/;
585 45 unneback
reg  [width-1:0] o_pad_int;
586 44 unneback
assign d_i_int = d_i;
587
genvar i;
588 45 unneback
generate
589 44 unneback
for (i=0;i<width;i=i+1) begin
590
    always @ (posedge clk or posedge rst)
591
    if (rst)
592 45 unneback
        o_pad_int[i] <= reset_value[i];
593 44 unneback
    else
594 45 unneback
        o_pad_int[i] <= d_i_int[i];
595
    assign #1 o_pad[i] = o_pad_int[i];
596 44 unneback
end
597
endgenerate
598
endmodule
599 45 unneback
`timescale 1ns/1ns
600 44 unneback
module vl_io_dff_oe ( d_i, d_o, oe, io_pad, clk, rst);
601
parameter width = 1;
602
input  [width-1:0] d_o;
603
output reg [width-1:0] d_i;
604
input oe;
605
inout [width-1:0] io_pad;
606
input clk, rst;
607
wire [width-1:0] oe_d /*synthesis syn_keep = 1*/;
608
reg [width-1:0] oe_q;
609
reg [width-1:0] d_o_q;
610
assign oe_d = {width{oe}};
611
genvar i;
612
generate
613
for (i=0;i<width;i=i+1) begin
614
    always @ (posedge clk or posedge rst)
615
    if (rst)
616
        oe_q[i] <= 1'b0;
617
    else
618
        oe_q[i] <= oe_d[i];
619
    always @ (posedge clk or posedge rst)
620
    if (rst)
621
        d_o_q[i] <= 1'b0;
622
    else
623
        d_o_q[i] <= d_o[i];
624
    always @ (posedge clk or posedge rst)
625
    if (rst)
626
        d_i[i] <= 1'b0;
627
    else
628
        d_i[i] <= io_pad[i];
629 45 unneback
    assign #1 io_pad[i] = (oe_q[i]) ? d_o_q[i] : 1'bz;
630 44 unneback
end
631
endgenerate
632
endmodule
633
//////////////////////////////////////////////////////////////////////
634
////                                                              ////
635 6 unneback
////  Versatile counter                                           ////
636
////                                                              ////
637
////  Description                                                 ////
638
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
639
////  counter                                                     ////
640
////                                                              ////
641
////  To Do:                                                      ////
642
////   - add LFSR with more taps                                  ////
643
////                                                              ////
644
////  Author(s):                                                  ////
645
////      - Michael Unneback, unneback@opencores.org              ////
646
////        ORSoC AB                                              ////
647
////                                                              ////
648
//////////////////////////////////////////////////////////////////////
649
////                                                              ////
650
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
651
////                                                              ////
652
//// This source file may be used and distributed without         ////
653
//// restriction provided that this copyright statement is not    ////
654
//// removed from the file and that any derivative work contains  ////
655
//// the original copyright notice and the associated disclaimer. ////
656
////                                                              ////
657
//// This source file is free software; you can redistribute it   ////
658
//// and/or modify it under the terms of the GNU Lesser General   ////
659
//// Public License as published by the Free Software Foundation; ////
660
//// either version 2.1 of the License, or (at your option) any   ////
661
//// later version.                                               ////
662
////                                                              ////
663
//// This source is distributed in the hope that it will be       ////
664
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
665
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
666
//// PURPOSE.  See the GNU Lesser General Public License for more ////
667
//// details.                                                     ////
668
////                                                              ////
669
//// You should have received a copy of the GNU Lesser General    ////
670
//// Public License along with this source; if not, download it   ////
671
//// from http://www.opencores.org/lgpl.shtml                     ////
672
////                                                              ////
673
//////////////////////////////////////////////////////////////////////
674
// binary counter
675 40 unneback
module vl_cnt_bin_ce (
676
 cke, q, rst, clk);
677 22 unneback
   parameter length = 4;
678 6 unneback
   input cke;
679
   output [length:1] q;
680
   input rst;
681
   input clk;
682
   parameter clear_value = 0;
683
   parameter set_value = 1;
684
   parameter wrap_value = 0;
685
   parameter level1_value = 15;
686
   reg  [length:1] qi;
687
   wire [length:1] q_next;
688
   assign q_next = qi + {{length-1{1'b0}},1'b1};
689
   always @ (posedge clk or posedge rst)
690
     if (rst)
691
       qi <= {length{1'b0}};
692
     else
693
     if (cke)
694
       qi <= q_next;
695
   assign q = qi;
696
endmodule
697
//////////////////////////////////////////////////////////////////////
698
////                                                              ////
699
////  Versatile counter                                           ////
700
////                                                              ////
701
////  Description                                                 ////
702
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
703
////  counter                                                     ////
704
////                                                              ////
705
////  To Do:                                                      ////
706
////   - add LFSR with more taps                                  ////
707
////                                                              ////
708
////  Author(s):                                                  ////
709
////      - Michael Unneback, unneback@opencores.org              ////
710
////        ORSoC AB                                              ////
711
////                                                              ////
712
//////////////////////////////////////////////////////////////////////
713
////                                                              ////
714
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
715
////                                                              ////
716
//// This source file may be used and distributed without         ////
717
//// restriction provided that this copyright statement is not    ////
718
//// removed from the file and that any derivative work contains  ////
719
//// the original copyright notice and the associated disclaimer. ////
720
////                                                              ////
721
//// This source file is free software; you can redistribute it   ////
722
//// and/or modify it under the terms of the GNU Lesser General   ////
723
//// Public License as published by the Free Software Foundation; ////
724
//// either version 2.1 of the License, or (at your option) any   ////
725
//// later version.                                               ////
726
////                                                              ////
727
//// This source is distributed in the hope that it will be       ////
728
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
729
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
730
//// PURPOSE.  See the GNU Lesser General Public License for more ////
731
//// details.                                                     ////
732
////                                                              ////
733
//// You should have received a copy of the GNU Lesser General    ////
734
//// Public License along with this source; if not, download it   ////
735
//// from http://www.opencores.org/lgpl.shtml                     ////
736
////                                                              ////
737
//////////////////////////////////////////////////////////////////////
738
// binary counter
739 40 unneback
module vl_cnt_bin_ce_rew_zq_l1 (
740
 cke, rew, zq, level1, rst, clk);
741 6 unneback
   parameter length = 4;
742
   input cke;
743
   input rew;
744 25 unneback
   output reg zq;
745
   output reg level1;
746
   input rst;
747
   input clk;
748
   parameter clear_value = 0;
749
   parameter set_value = 1;
750
   parameter wrap_value = 1;
751
   parameter level1_value = 15;
752 29 unneback
   wire clear;
753 30 unneback
   assign clear = 1'b0;
754 25 unneback
   reg  [length:1] qi;
755
   wire  [length:1] q_next, q_next_fw, q_next_rew;
756
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
757
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
758
   assign q_next = rew ? q_next_rew : q_next_fw;
759
   always @ (posedge clk or posedge rst)
760
     if (rst)
761
       qi <= {length{1'b0}};
762
     else
763
     if (cke)
764
       qi <= q_next;
765
   always @ (posedge clk or posedge rst)
766
     if (rst)
767
       zq <= 1'b1;
768
     else
769
     if (cke)
770
       zq <= q_next == {length{1'b0}};
771
    always @ (posedge clk or posedge rst)
772
    if (rst)
773
        level1 <= 1'b0;
774
    else
775
    if (cke)
776 29 unneback
    if (clear)
777
        level1 <= 1'b0;
778
    else if (q_next == level1_value)
779 25 unneback
        level1 <= 1'b1;
780
    else if (qi == level1_value & rew)
781
        level1 <= 1'b0;
782
endmodule
783
//////////////////////////////////////////////////////////////////////
784
////                                                              ////
785
////  Versatile counter                                           ////
786
////                                                              ////
787
////  Description                                                 ////
788
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
789
////  counter                                                     ////
790
////                                                              ////
791
////  To Do:                                                      ////
792
////   - add LFSR with more taps                                  ////
793
////                                                              ////
794
////  Author(s):                                                  ////
795
////      - Michael Unneback, unneback@opencores.org              ////
796
////        ORSoC AB                                              ////
797
////                                                              ////
798
//////////////////////////////////////////////////////////////////////
799
////                                                              ////
800
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
801
////                                                              ////
802
//// This source file may be used and distributed without         ////
803
//// restriction provided that this copyright statement is not    ////
804
//// removed from the file and that any derivative work contains  ////
805
//// the original copyright notice and the associated disclaimer. ////
806
////                                                              ////
807
//// This source file is free software; you can redistribute it   ////
808
//// and/or modify it under the terms of the GNU Lesser General   ////
809
//// Public License as published by the Free Software Foundation; ////
810
//// either version 2.1 of the License, or (at your option) any   ////
811
//// later version.                                               ////
812
////                                                              ////
813
//// This source is distributed in the hope that it will be       ////
814
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
815
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
816
//// PURPOSE.  See the GNU Lesser General Public License for more ////
817
//// details.                                                     ////
818
////                                                              ////
819
//// You should have received a copy of the GNU Lesser General    ////
820
//// Public License along with this source; if not, download it   ////
821
//// from http://www.opencores.org/lgpl.shtml                     ////
822
////                                                              ////
823
//////////////////////////////////////////////////////////////////////
824
// binary counter
825 40 unneback
module vl_cnt_bin_ce_rew_q_zq_l1 (
826
 cke, rew, q, zq, level1, rst, clk);
827 25 unneback
   parameter length = 4;
828
   input cke;
829
   input rew;
830
   output [length:1] q;
831
   output reg zq;
832
   output reg level1;
833
   input rst;
834
   input clk;
835
   parameter clear_value = 0;
836
   parameter set_value = 1;
837
   parameter wrap_value = 1;
838
   parameter level1_value = 15;
839 29 unneback
   wire clear;
840 30 unneback
   assign clear = 1'b0;
841 25 unneback
   reg  [length:1] qi;
842
   wire  [length:1] q_next, q_next_fw, q_next_rew;
843
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
844
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
845
   assign q_next = rew ? q_next_rew : q_next_fw;
846
   always @ (posedge clk or posedge rst)
847
     if (rst)
848
       qi <= {length{1'b0}};
849
     else
850
     if (cke)
851
       qi <= q_next;
852
   assign q = qi;
853
   always @ (posedge clk or posedge rst)
854
     if (rst)
855
       zq <= 1'b1;
856
     else
857
     if (cke)
858
       zq <= q_next == {length{1'b0}};
859
    always @ (posedge clk or posedge rst)
860
    if (rst)
861
        level1 <= 1'b0;
862
    else
863
    if (cke)
864 29 unneback
    if (clear)
865
        level1 <= 1'b0;
866
    else if (q_next == level1_value)
867 25 unneback
        level1 <= 1'b1;
868
    else if (qi == level1_value & rew)
869
        level1 <= 1'b0;
870
endmodule
871
//////////////////////////////////////////////////////////////////////
872
////                                                              ////
873
////  Versatile counter                                           ////
874
////                                                              ////
875
////  Description                                                 ////
876
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
877
////  counter                                                     ////
878
////                                                              ////
879
////  To Do:                                                      ////
880
////   - add LFSR with more taps                                  ////
881
////                                                              ////
882
////  Author(s):                                                  ////
883
////      - Michael Unneback, unneback@opencores.org              ////
884
////        ORSoC AB                                              ////
885
////                                                              ////
886
//////////////////////////////////////////////////////////////////////
887
////                                                              ////
888
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
889
////                                                              ////
890
//// This source file may be used and distributed without         ////
891
//// restriction provided that this copyright statement is not    ////
892
//// removed from the file and that any derivative work contains  ////
893
//// the original copyright notice and the associated disclaimer. ////
894
////                                                              ////
895
//// This source file is free software; you can redistribute it   ////
896
//// and/or modify it under the terms of the GNU Lesser General   ////
897
//// Public License as published by the Free Software Foundation; ////
898
//// either version 2.1 of the License, or (at your option) any   ////
899
//// later version.                                               ////
900
////                                                              ////
901
//// This source is distributed in the hope that it will be       ////
902
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
903
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
904
//// PURPOSE.  See the GNU Lesser General Public License for more ////
905
//// details.                                                     ////
906
////                                                              ////
907
//// You should have received a copy of the GNU Lesser General    ////
908
//// Public License along with this source; if not, download it   ////
909
//// from http://www.opencores.org/lgpl.shtml                     ////
910
////                                                              ////
911
//////////////////////////////////////////////////////////////////////
912 75 unneback
// LFSR counter
913
module vl_cnt_lfsr_ce (
914
 cke, zq, rst, clk);
915
   parameter length = 4;
916
   input cke;
917
   output reg zq;
918
   input rst;
919
   input clk;
920
   parameter clear_value = 0;
921
   parameter set_value = 1;
922
   parameter wrap_value = 0;
923
   parameter level1_value = 15;
924
   reg  [length:1] qi;
925
   reg lfsr_fb;
926
   wire [length:1] q_next;
927
   reg [32:1] polynom;
928
   integer i;
929
   always @ (qi)
930
   begin
931
        case (length)
932
         2: polynom = 32'b11;                               // 0x3
933
         3: polynom = 32'b110;                              // 0x6
934
         4: polynom = 32'b1100;                             // 0xC
935
         5: polynom = 32'b10100;                            // 0x14
936
         6: polynom = 32'b110000;                           // 0x30
937
         7: polynom = 32'b1100000;                          // 0x60
938
         8: polynom = 32'b10111000;                         // 0xb8
939
         9: polynom = 32'b100010000;                        // 0x110
940
        10: polynom = 32'b1001000000;                       // 0x240
941
        11: polynom = 32'b10100000000;                      // 0x500
942
        12: polynom = 32'b100000101001;                     // 0x829
943
        13: polynom = 32'b1000000001100;                    // 0x100C
944
        14: polynom = 32'b10000000010101;                   // 0x2015
945
        15: polynom = 32'b110000000000000;                  // 0x6000
946
        16: polynom = 32'b1101000000001000;                 // 0xD008
947
        17: polynom = 32'b10010000000000000;                // 0x12000
948
        18: polynom = 32'b100000010000000000;               // 0x20400
949
        19: polynom = 32'b1000000000000100011;              // 0x40023
950
        20: polynom = 32'b10010000000000000000;             // 0x90000
951
        21: polynom = 32'b101000000000000000000;            // 0x140000
952
        22: polynom = 32'b1100000000000000000000;           // 0x300000
953
        23: polynom = 32'b10000100000000000000000;          // 0x420000
954
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
955
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
956
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
957
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
958
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
959
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
960
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
961
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
962
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
963
        default: polynom = 32'b0;
964
        endcase
965
        lfsr_fb = qi[length];
966
        for (i=length-1; i>=1; i=i-1) begin
967
            if (polynom[i])
968
                lfsr_fb = lfsr_fb  ~^ qi[i];
969
        end
970
    end
971
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
972
   always @ (posedge clk or posedge rst)
973
     if (rst)
974
       qi <= {length{1'b0}};
975
     else
976
     if (cke)
977
       qi <= q_next;
978
   always @ (posedge clk or posedge rst)
979
     if (rst)
980
       zq <= 1'b1;
981
     else
982
     if (cke)
983
       zq <= q_next == {length{1'b0}};
984
endmodule
985
//////////////////////////////////////////////////////////////////////
986
////                                                              ////
987
////  Versatile counter                                           ////
988
////                                                              ////
989
////  Description                                                 ////
990
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
991
////  counter                                                     ////
992
////                                                              ////
993
////  To Do:                                                      ////
994
////   - add LFSR with more taps                                  ////
995
////                                                              ////
996
////  Author(s):                                                  ////
997
////      - Michael Unneback, unneback@opencores.org              ////
998
////        ORSoC AB                                              ////
999
////                                                              ////
1000
//////////////////////////////////////////////////////////////////////
1001
////                                                              ////
1002
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1003
////                                                              ////
1004
//// This source file may be used and distributed without         ////
1005
//// restriction provided that this copyright statement is not    ////
1006
//// removed from the file and that any derivative work contains  ////
1007
//// the original copyright notice and the associated disclaimer. ////
1008
////                                                              ////
1009
//// This source file is free software; you can redistribute it   ////
1010
//// and/or modify it under the terms of the GNU Lesser General   ////
1011
//// Public License as published by the Free Software Foundation; ////
1012
//// either version 2.1 of the License, or (at your option) any   ////
1013
//// later version.                                               ////
1014
////                                                              ////
1015
//// This source is distributed in the hope that it will be       ////
1016
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1017
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1018
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1019
//// details.                                                     ////
1020
////                                                              ////
1021
//// You should have received a copy of the GNU Lesser General    ////
1022
//// Public License along with this source; if not, download it   ////
1023
//// from http://www.opencores.org/lgpl.shtml                     ////
1024
////                                                              ////
1025
//////////////////////////////////////////////////////////////////////
1026 6 unneback
// GRAY counter
1027 40 unneback
module vl_cnt_gray_ce_bin (
1028
 cke, q, q_bin, rst, clk);
1029 6 unneback
   parameter length = 4;
1030
   input cke;
1031
   output reg [length:1] q;
1032
   output [length:1] q_bin;
1033
   input rst;
1034
   input clk;
1035
   parameter clear_value = 0;
1036
   parameter set_value = 1;
1037
   parameter wrap_value = 8;
1038
   parameter level1_value = 15;
1039
   reg  [length:1] qi;
1040
   wire [length:1] q_next;
1041
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1042
   always @ (posedge clk or posedge rst)
1043
     if (rst)
1044
       qi <= {length{1'b0}};
1045
     else
1046
     if (cke)
1047
       qi <= q_next;
1048
   always @ (posedge clk or posedge rst)
1049
     if (rst)
1050
       q <= {length{1'b0}};
1051
     else
1052
       if (cke)
1053
         q <= (q_next>>1) ^ q_next;
1054
   assign q_bin = qi;
1055
endmodule
1056
//////////////////////////////////////////////////////////////////////
1057
////                                                              ////
1058
////  Versatile library, counters                                 ////
1059
////                                                              ////
1060
////  Description                                                 ////
1061
////  counters                                                    ////
1062
////                                                              ////
1063
////                                                              ////
1064
////  To Do:                                                      ////
1065
////   - add more counters                                        ////
1066
////                                                              ////
1067
////  Author(s):                                                  ////
1068
////      - Michael Unneback, unneback@opencores.org              ////
1069
////        ORSoC AB                                              ////
1070
////                                                              ////
1071
//////////////////////////////////////////////////////////////////////
1072
////                                                              ////
1073
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1074
////                                                              ////
1075
//// This source file may be used and distributed without         ////
1076
//// restriction provided that this copyright statement is not    ////
1077
//// removed from the file and that any derivative work contains  ////
1078
//// the original copyright notice and the associated disclaimer. ////
1079
////                                                              ////
1080
//// This source file is free software; you can redistribute it   ////
1081
//// and/or modify it under the terms of the GNU Lesser General   ////
1082
//// Public License as published by the Free Software Foundation; ////
1083
//// either version 2.1 of the License, or (at your option) any   ////
1084
//// later version.                                               ////
1085
////                                                              ////
1086
//// This source is distributed in the hope that it will be       ////
1087
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1088
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1089
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1090
//// details.                                                     ////
1091
////                                                              ////
1092
//// You should have received a copy of the GNU Lesser General    ////
1093
//// Public License along with this source; if not, download it   ////
1094
//// from http://www.opencores.org/lgpl.shtml                     ////
1095
////                                                              ////
1096
//////////////////////////////////////////////////////////////////////
1097 18 unneback
module vl_cnt_shreg_wrap ( q, rst, clk);
1098 6 unneback
   parameter length = 4;
1099
   output reg [0:length-1] q;
1100
   input rst;
1101
   input clk;
1102
    always @ (posedge clk or posedge rst)
1103
    if (rst)
1104
        q <= {1'b1,{length-1{1'b0}}};
1105
    else
1106
        q <= {q[length-1],q[0:length-2]};
1107
endmodule
1108 18 unneback
module vl_cnt_shreg_ce_wrap ( cke, q, rst, clk);
1109 6 unneback
   parameter length = 4;
1110
   input cke;
1111
   output reg [0:length-1] q;
1112
   input rst;
1113
   input clk;
1114
    always @ (posedge clk or posedge rst)
1115
    if (rst)
1116
        q <= {1'b1,{length-1{1'b0}}};
1117
    else
1118
        if (cke)
1119
            q <= {q[length-1],q[0:length-2]};
1120
endmodule
1121 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
1122 6 unneback
   parameter length = 4;
1123
   input cke, clear;
1124
   output reg [0:length-1] q;
1125
   input rst;
1126
   input clk;
1127
    always @ (posedge clk or posedge rst)
1128
    if (rst)
1129
        q <= {1'b1,{length-1{1'b0}}};
1130
    else
1131
        if (cke)
1132
            if (clear)
1133
                q <= {1'b1,{length-1{1'b0}}};
1134
            else
1135
                q <= q >> 1;
1136
endmodule
1137 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
1138 6 unneback
   parameter length = 4;
1139
   input cke, clear;
1140
   output reg [0:length-1] q;
1141
   input rst;
1142
   input clk;
1143
    always @ (posedge clk or posedge rst)
1144
    if (rst)
1145
        q <= {1'b1,{length-1{1'b0}}};
1146
    else
1147
        if (cke)
1148
            if (clear)
1149
                q <= {1'b1,{length-1{1'b0}}};
1150
            else
1151
            q <= {q[length-1],q[0:length-2]};
1152
endmodule
1153
//////////////////////////////////////////////////////////////////////
1154
////                                                              ////
1155
////  Versatile library, memories                                 ////
1156
////                                                              ////
1157
////  Description                                                 ////
1158
////  memories                                                    ////
1159
////                                                              ////
1160
////                                                              ////
1161
////  To Do:                                                      ////
1162
////   - add more memory types                                    ////
1163
////                                                              ////
1164
////  Author(s):                                                  ////
1165
////      - Michael Unneback, unneback@opencores.org              ////
1166
////        ORSoC AB                                              ////
1167
////                                                              ////
1168
//////////////////////////////////////////////////////////////////////
1169
////                                                              ////
1170
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1171
////                                                              ////
1172
//// This source file may be used and distributed without         ////
1173
//// restriction provided that this copyright statement is not    ////
1174
//// removed from the file and that any derivative work contains  ////
1175
//// the original copyright notice and the associated disclaimer. ////
1176
////                                                              ////
1177
//// This source file is free software; you can redistribute it   ////
1178
//// and/or modify it under the terms of the GNU Lesser General   ////
1179
//// Public License as published by the Free Software Foundation; ////
1180
//// either version 2.1 of the License, or (at your option) any   ////
1181
//// later version.                                               ////
1182
////                                                              ////
1183
//// This source is distributed in the hope that it will be       ////
1184
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1185
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1186
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1187
//// details.                                                     ////
1188
////                                                              ////
1189
//// You should have received a copy of the GNU Lesser General    ////
1190
//// Public License along with this source; if not, download it   ////
1191
//// from http://www.opencores.org/lgpl.shtml                     ////
1192
////                                                              ////
1193
//////////////////////////////////////////////////////////////////////
1194
/// ROM
1195 7 unneback
module vl_rom_init ( adr, q, clk);
1196
   parameter data_width = 32;
1197
   parameter addr_width = 8;
1198 75 unneback
   parameter mem_size = 1<<addr_width;
1199 7 unneback
   input [(addr_width-1):0]       adr;
1200
   output reg [(data_width-1):0] q;
1201
   input                         clk;
1202 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
1203 7 unneback
   parameter memory_file = "vl_rom.vmem";
1204
   initial
1205
     begin
1206
        $readmemh(memory_file, rom);
1207
     end
1208
   always @ (posedge clk)
1209
     q <= rom[adr];
1210
endmodule
1211 6 unneback
// Single port RAM
1212
module vl_ram ( d, adr, we, q, clk);
1213
   parameter data_width = 32;
1214
   parameter addr_width = 8;
1215 75 unneback
   parameter mem_size = 1<<addr_width;
1216 6 unneback
   input [(data_width-1):0]      d;
1217
   input [(addr_width-1):0]       adr;
1218
   input                         we;
1219 7 unneback
   output reg [(data_width-1):0] q;
1220 6 unneback
   input                         clk;
1221 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0];
1222 7 unneback
   parameter init = 0;
1223
   parameter memory_file = "vl_ram.vmem";
1224
   generate if (init) begin : init_mem
1225
   initial
1226
     begin
1227
        $readmemh(memory_file, ram);
1228
     end
1229
   end
1230
   endgenerate
1231 6 unneback
   always @ (posedge clk)
1232
   begin
1233
   if (we)
1234
     ram[adr] <= d;
1235
   q <= ram[adr];
1236
   end
1237
endmodule
1238 90 unneback
module vl_ram_be ( d, adr, be, re, we, q, clk);
1239 7 unneback
   parameter data_width = 32;
1240 72 unneback
   parameter addr_width = 6;
1241 75 unneback
   parameter mem_size = 1<<addr_width;
1242 7 unneback
   input [(data_width-1):0]      d;
1243
   input [(addr_width-1):0]       adr;
1244 73 unneback
   input [(data_width/8)-1:0]    be;
1245 90 unneback
   input                         re;
1246 7 unneback
   input                         we;
1247
   output reg [(data_width-1):0] q;
1248
   input                         clk;
1249 65 unneback
`ifdef SYSTEMVERILOG
1250 68 unneback
   logic [data_width/8-1:0][7:0] ram[0:mem_size-1];// # words = 1 << address width
1251 65 unneback
`else
1252 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
1253
    wire [data_width/8-1:0] cke;
1254 65 unneback
`endif
1255 60 unneback
   parameter memory_init = 0;
1256 7 unneback
   parameter memory_file = "vl_ram.vmem";
1257 60 unneback
   generate if (memory_init) begin : init_mem
1258 7 unneback
   initial
1259
     begin
1260
        $readmemh(memory_file, ram);
1261
     end
1262
   end
1263
   endgenerate
1264 60 unneback
`ifdef SYSTEMVERILOG
1265
// use a multi-dimensional packed array
1266
//to model individual bytes within the word
1267
always_ff@(posedge clk)
1268
begin
1269
    if(we) begin // note: we should have a for statement to support any bus width
1270 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
1271
        if(be[2]) ram[adr][2] <= d[23:16];
1272
        if(be[1]) ram[adr][1] <= d[15:8];
1273
        if(be[0]) ram[adr][0] <= d[7:0];
1274 60 unneback
    end
1275 90 unneback
    if (re)
1276
        q <= ram[adr];
1277 60 unneback
end
1278
`else
1279 85 unneback
assign cke = {data_width/8{we}} & be;
1280 7 unneback
   genvar i;
1281 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
1282 7 unneback
      always @ (posedge clk)
1283 85 unneback
      if (cke[i])
1284 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
1285
   end
1286
   endgenerate
1287
   always @ (posedge clk)
1288 90 unneback
    if (re)
1289 7 unneback
      q <= ram[adr];
1290 60 unneback
`endif
1291 85 unneback
   // Function to access RAM (for use by Verilator).
1292
   function [31:0] get_mem;
1293
      // verilator public
1294 90 unneback
      input [addr_width-1:0]             addr;
1295 85 unneback
      get_mem = ram[addr];
1296
   endfunction // get_mem
1297
   // Function to write RAM (for use by Verilator).
1298
   function set_mem;
1299
      // verilator public
1300 90 unneback
      input [addr_width-1:0]             addr;
1301
      input [data_width-1:0]             data;
1302 85 unneback
      ram[addr] = data;
1303
   endfunction // set_mem
1304 7 unneback
endmodule
1305 48 unneback
        // ACTEL FPGA should not use logic to handle rw collision
1306 7 unneback
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1307 6 unneback
   parameter data_width = 32;
1308
   parameter addr_width = 8;
1309 75 unneback
   parameter mem_size = 1<<addr_width;
1310 6 unneback
   input [(data_width-1):0]      d_a;
1311
   input [(addr_width-1):0]       adr_a;
1312
   input [(addr_width-1):0]       adr_b;
1313
   input                         we_a;
1314
   output [(data_width-1):0]      q_b;
1315
   input                         clk_a, clk_b;
1316
   reg [(addr_width-1):0]         adr_b_reg;
1317 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1318 7 unneback
   parameter init = 0;
1319
   parameter memory_file = "vl_ram.vmem";
1320
   generate if (init) begin : init_mem
1321
   initial
1322
     begin
1323
        $readmemh(memory_file, ram);
1324
     end
1325
   end
1326
   endgenerate
1327 6 unneback
   always @ (posedge clk_a)
1328
   if (we_a)
1329
     ram[adr_a] <= d_a;
1330
   always @ (posedge clk_b)
1331
   adr_b_reg <= adr_b;
1332
   assign q_b = ram[adr_b_reg];
1333
endmodule
1334 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
1335 6 unneback
   parameter data_width = 32;
1336
   parameter addr_width = 8;
1337 75 unneback
   parameter mem_size = 1<<addr_width;
1338 6 unneback
   input [(data_width-1):0]      d_a;
1339
   input [(addr_width-1):0]       adr_a;
1340
   input [(addr_width-1):0]       adr_b;
1341
   input                         we_a;
1342
   output [(data_width-1):0]      q_b;
1343
   output reg [(data_width-1):0] q_a;
1344
   input                         clk_a, clk_b;
1345
   reg [(data_width-1):0]         q_b;
1346 75 unneback
   reg [data_width-1:0] ram [mem_szie-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1347 7 unneback
   parameter init = 0;
1348
   parameter memory_file = "vl_ram.vmem";
1349
   generate if (init) begin : init_mem
1350
   initial
1351
     begin
1352
        $readmemh(memory_file, ram);
1353
     end
1354
   end
1355
   endgenerate
1356 6 unneback
   always @ (posedge clk_a)
1357
     begin
1358
        q_a <= ram[adr_a];
1359
        if (we_a)
1360
             ram[adr_a] <= d_a;
1361
     end
1362
   always @ (posedge clk_b)
1363
          q_b <= ram[adr_b];
1364
endmodule
1365 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 );
1366 6 unneback
   parameter data_width = 32;
1367
   parameter addr_width = 8;
1368 75 unneback
   parameter mem_size = 1<<addr_width;
1369 6 unneback
   input [(data_width-1):0]      d_a;
1370
   input [(addr_width-1):0]       adr_a;
1371
   input [(addr_width-1):0]       adr_b;
1372
   input                         we_a;
1373
   output [(data_width-1):0]      q_b;
1374
   input [(data_width-1):0]       d_b;
1375
   output reg [(data_width-1):0] q_a;
1376
   input                         we_b;
1377
   input                         clk_a, clk_b;
1378
   reg [(data_width-1):0]         q_b;
1379 75 unneback
   reg [data_width-1:0] ram [mem_size-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1380 7 unneback
   parameter init = 0;
1381
   parameter memory_file = "vl_ram.vmem";
1382
   generate if (init) begin : init_mem
1383
   initial
1384
     begin
1385
        $readmemh(memory_file, ram);
1386
     end
1387
   end
1388
   endgenerate
1389 6 unneback
   always @ (posedge clk_a)
1390
     begin
1391
        q_a <= ram[adr_a];
1392
        if (we_a)
1393
             ram[adr_a] <= d_a;
1394
     end
1395
   always @ (posedge clk_b)
1396
     begin
1397
        q_b <= ram[adr_b];
1398
        if (we_b)
1399
          ram[adr_b] <= d_b;
1400
     end
1401
endmodule
1402 75 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 );
1403
   parameter a_data_width = 32;
1404
   parameter a_addr_width = 8;
1405
   parameter b_data_width = 64;
1406
   parameter b_addr_width = 7;
1407
   //parameter mem_size = (a_addr_width>b_addr_width) ? (1<<a_addr_width) : (1<<b_addr_width);
1408
   parameter mem_size = 1024;
1409
   input [(a_data_width-1):0]      d_a;
1410
   input [(a_addr_width-1):0]     adr_a;
1411
   input [(b_addr_width-1):0]     adr_b;
1412
   input [(a_data_width/4-1):0]    be_a;
1413
   input                         we_a;
1414
   output [(b_data_width-1):0]    q_b;
1415
   input [(b_data_width-1):0]     d_b;
1416
   output reg [(a_data_width-1):0] q_a;
1417
   input [(b_data_width/4-1):0]    be_b;
1418
   input                         we_b;
1419
   input                         clk_a, clk_b;
1420
   reg [(b_data_width-1):0]       q_b;
1421
generate
1422
if (a_data_width==32 & b_data_width==64) begin : inst32to64
1423 77 unneback
    wire [63:0] tmp;
1424 75 unneback
    vl_dpram_2r2w
1425
    # (.data_width(8), .addr_width(b_addr_width-3))
1426
    ram0 (
1427
        .d_a(d_a[7:0]),
1428
        .q_a(tmp[7:0]),
1429
        .adr_a(adr_a[a_addr_width-3-1:0]),
1430
        .we_a(we_a & be_a[0] & !adr_a[0]),
1431
        .clk_a(clk_a),
1432
        .d_b(d_b[7:0]),
1433
        .q_b(q_b[7:0]),
1434
        .adr_b(adr_b[b_addr_width-3-1:0]),
1435
        .we_b(we_b),
1436
        .clk_b(clk_b) );
1437
    vl_dpram_2r2w
1438
    # (.data_width(8), .addr_width(b_addr_width-3))
1439
    ram1 (
1440
        .d_a(d_a[7:0]),
1441
        .q_a(tmp[7:0]),
1442
        .adr_a(adr_a[a_addr_width-3-1:0]),
1443
        .we_a(we_a),
1444
        .clk_a(clk_a),
1445
        .d_b(d_b[7:0]),
1446
        .q_b(q_b[7:0]),
1447
        .adr_b(adr_b[b_addr_width-3-1:0]),
1448
        .we_b(we_b),
1449
        .clk_b(clk_b) );
1450
    vl_dpram_2r2w
1451
    # (.data_width(8), .addr_width(b_addr_width-3))
1452
    ram2 (
1453
        .d_a(d_a[15:8]),
1454
        .q_a(tmp[7:0]),
1455
        .adr_a(adr_a[a_addr_width-3-1:0]),
1456
        .we_a(we_a),
1457
        .clk_a(clk_a),
1458
        .d_b(d_b[7:0]),
1459
        .q_b(q_b[7:0]),
1460
        .adr_b(adr_b[b_addr_width-3-1:0]),
1461
        .we_b(we_b),
1462
        .clk_b(clk_b) );
1463
    vl_dpram_2r2w
1464
    # (.data_width(8), .addr_width(b_addr_width-3))
1465
    ram3 (
1466
        .d_a(d_a[15:8]),
1467
        .q_a(tmp[7:0]),
1468
        .adr_a(adr_a[a_addr_width-3-1:0]),
1469
        .we_a(we_a),
1470
        .clk_a(clk_a),
1471
        .d_b(d_b[7:0]),
1472
        .q_b(q_b[7:0]),
1473
        .adr_b(adr_b[b_addr_width-3-1:0]),
1474
        .we_b(we_b),
1475
        .clk_b(clk_b) );
1476
    vl_dpram_2r2w
1477
    # (.data_width(8), .addr_width(b_addr_width-3))
1478
    ram4 (
1479
        .d_a(d_a[23:16]),
1480
        .q_a(tmp[7:0]),
1481
        .adr_a(adr_a[a_addr_width-3-1:0]),
1482
        .we_a(we_a),
1483
        .clk_a(clk_a),
1484
        .d_b(d_b[7:0]),
1485
        .q_b(q_b[7:0]),
1486
        .adr_b(adr_b[b_addr_width-3-1:0]),
1487
        .we_b(we_b),
1488
        .clk_b(clk_b) );
1489
    vl_dpram_2r2w
1490
    # (.data_width(8), .addr_width(b_addr_width-3))
1491
    ram5 (
1492
        .d_a(d_a[23:16]),
1493
        .q_a(tmp[7:0]),
1494
        .adr_a(adr_a[a_addr_width-3-1:0]),
1495
        .we_a(we_a),
1496
        .clk_a(clk_a),
1497
        .d_b(d_b[7:0]),
1498
        .q_b(q_b[7:0]),
1499
        .adr_b(adr_b[b_addr_width-3-1:0]),
1500
        .we_b(we_b),
1501
        .clk_b(clk_b) );
1502
    vl_dpram_2r2w
1503
    # (.data_width(8), .addr_width(b_addr_width-3))
1504
    ram6 (
1505
        .d_a(d_a[31:24]),
1506
        .q_a(tmp[7:0]),
1507
        .adr_a(adr_a[a_addr_width-3-1:0]),
1508
        .we_a(we_a),
1509
        .clk_a(clk_a),
1510
        .d_b(d_b[7:0]),
1511
        .q_b(q_b[7:0]),
1512
        .adr_b(adr_b[b_addr_width-3-1:0]),
1513
        .we_b(we_b),
1514
        .clk_b(clk_b) );
1515
    vl_dpram_2r2w
1516
    # (.data_width(8), .addr_width(b_addr_width-3))
1517
    ram7 (
1518
        .d_a(d_a[31:24]),
1519
        .q_a(tmp[7:0]),
1520
        .adr_a(adr_a[a_addr_width-3-1:0]),
1521
        .we_a(we_a),
1522
        .clk_a(clk_a),
1523
        .d_b(d_b[7:0]),
1524
        .q_b(q_b[7:0]),
1525
        .adr_b(adr_b[b_addr_width-3-1:0]),
1526
        .we_b(we_b),
1527
        .clk_b(clk_b) );
1528
/*
1529
   reg [7:0] ram0 [mem_size/8-1:0];
1530
   wire [7:0] wea, web;
1531
   assign wea = we_a & be_a[0];
1532
   assign web = we_b & be_b[0];
1533
   always @ (posedge clk_a)
1534
    if (wea)
1535
        ram0[adr_a] <= d_a[7:0];
1536
    always @ (posedge clk_a)
1537
        q_a[7:0] <= ram0[adr_a];
1538
   always @ (posedge clk_a)
1539
    if (web)
1540
        ram0[adr_b] <= d_b[7:0];
1541
    always @ (posedge clk_b)
1542
        q_b[7:0] <= ram0[adr_b];
1543
*/
1544
end
1545
endgenerate
1546
/*
1547
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_rama
1548
      always @ (posedge clk_a)
1549
      if (we_a & be_a[i])
1550
        ram[adr_a][(i+1)*8-1:i*8] <= d_a[(i+1)*8-1:i*8];
1551
   end
1552
   endgenerate
1553
   always @ (posedge clk_a)
1554
      q_a <= ram[adr_a];
1555
   genvar i;
1556
   generate for (i=0;i<addr_width/4;i=i+1) begin : be_ramb
1557
      always @ (posedge clk_a)
1558
      if (we_b & be_b[i])
1559
        ram[adr_b][(i+1)*8-1:i*8] <= d_b[(i+1)*8-1:i*8];
1560
   end
1561
   endgenerate
1562
   always @ (posedge clk_b)
1563
      q_b <= ram[adr_b];
1564
*/
1565
/*
1566
   always @ (posedge clk_a)
1567
     begin
1568
        q_a <= ram[adr_a];
1569
        if (we_a)
1570
             ram[adr_a] <= d_a;
1571
     end
1572
   always @ (posedge clk_b)
1573
     begin
1574
        q_b <= ram[adr_b];
1575
        if (we_b)
1576
          ram[adr_b] <= d_b;
1577
     end
1578
*/
1579
endmodule
1580 6 unneback
// Content addresable memory, CAM
1581
// FIFO
1582 25 unneback
module vl_fifo_1r1w_fill_level_sync (
1583
    d, wr, fifo_full,
1584
    q, rd, fifo_empty,
1585
    fill_level,
1586
    clk, rst
1587
    );
1588
parameter data_width = 18;
1589
parameter addr_width = 4;
1590
// write side
1591
input  [data_width-1:0] d;
1592
input                   wr;
1593
output                  fifo_full;
1594
// read side
1595
output [data_width-1:0] q;
1596
input                   rd;
1597
output                  fifo_empty;
1598
// common
1599
output [addr_width:0]   fill_level;
1600
input rst, clk;
1601
wire [addr_width:1] wadr, radr;
1602
vl_cnt_bin_ce
1603
    # ( .length(addr_width))
1604
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
1605
vl_cnt_bin_ce
1606
    # (.length(addr_width))
1607
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
1608
vl_dpram_1r1w
1609
    # (.data_width(data_width), .addr_width(addr_width))
1610
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
1611 31 unneback
vl_cnt_bin_ce_rew_q_zq_l1
1612 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
1613 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
1614
endmodule
1615 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
1616
// RAM is supposed to be larger than the two FIFOs
1617
// LFSR counters used adr pointers
1618
module vl_fifo_2r2w_sync_simplex (
1619
    // a side
1620
    a_d, a_wr, a_fifo_full,
1621
    a_q, a_rd, a_fifo_empty,
1622
    a_fill_level,
1623
    // b side
1624
    b_d, b_wr, b_fifo_full,
1625
    b_q, b_rd, b_fifo_empty,
1626
    b_fill_level,
1627
    // common
1628
    clk, rst
1629
    );
1630
parameter data_width = 8;
1631
parameter addr_width = 5;
1632
parameter fifo_full_level = (1<<addr_width)-1;
1633
// a side
1634
input  [data_width-1:0] a_d;
1635
input                   a_wr;
1636
output                  a_fifo_full;
1637
output [data_width-1:0] a_q;
1638
input                   a_rd;
1639
output                  a_fifo_empty;
1640
output [addr_width-1:0] a_fill_level;
1641
// b side
1642
input  [data_width-1:0] b_d;
1643
input                   b_wr;
1644
output                  b_fifo_full;
1645
output [data_width-1:0] b_q;
1646
input                   b_rd;
1647
output                  b_fifo_empty;
1648
output [addr_width-1:0] b_fill_level;
1649
input                   clk;
1650
input                   rst;
1651
// adr_gen
1652
wire [addr_width:1] a_wadr, a_radr;
1653
wire [addr_width:1] b_wadr, b_radr;
1654
// dpram
1655
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
1656
vl_cnt_lfsr_ce
1657
    # ( .length(addr_width))
1658
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
1659
vl_cnt_lfsr_ce
1660
    # (.length(addr_width))
1661
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
1662
vl_cnt_lfsr_ce
1663
    # ( .length(addr_width))
1664
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
1665
vl_cnt_lfsr_ce
1666
    # (.length(addr_width))
1667
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
1668
// mux read or write adr to DPRAM
1669
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
1670
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
1671
vl_dpram_2r2w
1672
    # (.data_width(data_width), .addr_width(addr_width+1))
1673
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
1674
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
1675
vl_cnt_bin_ce_rew_zq_l1
1676 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1677 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));
1678
vl_cnt_bin_ce_rew_zq_l1
1679 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
1680 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));
1681
endmodule
1682 6 unneback
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
1683 11 unneback
   parameter addr_width = 4;
1684
   parameter N = addr_width-1;
1685 6 unneback
   parameter Q1 = 2'b00;
1686
   parameter Q2 = 2'b01;
1687
   parameter Q3 = 2'b11;
1688
   parameter Q4 = 2'b10;
1689
   parameter going_empty = 1'b0;
1690
   parameter going_full  = 1'b1;
1691
   input [N:0]  wptr, rptr;
1692 14 unneback
   output       fifo_empty;
1693 6 unneback
   output       fifo_full;
1694
   input        wclk, rclk, rst;
1695
   wire direction;
1696
   reg  direction_set, direction_clr;
1697
   wire async_empty, async_full;
1698
   wire fifo_full2;
1699 14 unneback
   wire fifo_empty2;
1700 6 unneback
   // direction_set
1701
   always @ (wptr[N:N-1] or rptr[N:N-1])
1702
     case ({wptr[N:N-1],rptr[N:N-1]})
1703
       {Q1,Q2} : direction_set <= 1'b1;
1704
       {Q2,Q3} : direction_set <= 1'b1;
1705
       {Q3,Q4} : direction_set <= 1'b1;
1706
       {Q4,Q1} : direction_set <= 1'b1;
1707
       default : direction_set <= 1'b0;
1708
     endcase
1709
   // direction_clear
1710
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
1711
     if (rst)
1712
       direction_clr <= 1'b1;
1713
     else
1714
       case ({wptr[N:N-1],rptr[N:N-1]})
1715
         {Q2,Q1} : direction_clr <= 1'b1;
1716
         {Q3,Q2} : direction_clr <= 1'b1;
1717
         {Q4,Q3} : direction_clr <= 1'b1;
1718
         {Q1,Q4} : direction_clr <= 1'b1;
1719
         default : direction_clr <= 1'b0;
1720
       endcase
1721 18 unneback
    vl_dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
1722 6 unneback
   assign async_empty = (wptr == rptr) && (direction==going_empty);
1723
   assign async_full  = (wptr == rptr) && (direction==going_full);
1724 18 unneback
    vl_dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
1725
    vl_dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
1726 6 unneback
/*
1727
   always @ (posedge wclk or posedge rst or posedge async_full)
1728
     if (rst)
1729
       {fifo_full, fifo_full2} <= 2'b00;
1730
     else if (async_full)
1731
       {fifo_full, fifo_full2} <= 2'b11;
1732
     else
1733
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
1734
*/
1735 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
1736 6 unneback
     if (async_empty)
1737
       {fifo_empty, fifo_empty2} <= 2'b11;
1738
     else
1739 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
1740 18 unneback
    vl_dff # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
1741
    vl_dff # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
1742 27 unneback
endmodule // async_compb
1743 6 unneback
module vl_fifo_1r1w_async (
1744
    d, wr, fifo_full, wr_clk, wr_rst,
1745
    q, rd, fifo_empty, rd_clk, rd_rst
1746
    );
1747
parameter data_width = 18;
1748
parameter addr_width = 4;
1749
// write side
1750
input  [data_width-1:0] d;
1751
input                   wr;
1752
output                  fifo_full;
1753
input                   wr_clk;
1754
input                   wr_rst;
1755
// read side
1756
output [data_width-1:0] q;
1757
input                   rd;
1758
output                  fifo_empty;
1759
input                   rd_clk;
1760
input                   rd_rst;
1761
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
1762 18 unneback
vl_cnt_gray_ce_bin
1763 6 unneback
    # ( .length(addr_width))
1764
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
1765 18 unneback
vl_cnt_gray_ce_bin
1766 6 unneback
    # (.length(addr_width))
1767 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
1768 7 unneback
vl_dpram_1r1w
1769 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
1770
    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));
1771
vl_fifo_cmp_async
1772
    # (.addr_width(addr_width))
1773
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
1774
endmodule
1775 8 unneback
module vl_fifo_2r2w_async (
1776 6 unneback
    // a side
1777
    a_d, a_wr, a_fifo_full,
1778
    a_q, a_rd, a_fifo_empty,
1779
    a_clk, a_rst,
1780
    // b side
1781
    b_d, b_wr, b_fifo_full,
1782
    b_q, b_rd, b_fifo_empty,
1783
    b_clk, b_rst
1784
    );
1785
parameter data_width = 18;
1786
parameter addr_width = 4;
1787
// a side
1788
input  [data_width-1:0] a_d;
1789
input                   a_wr;
1790
output                  a_fifo_full;
1791
output [data_width-1:0] a_q;
1792
input                   a_rd;
1793
output                  a_fifo_empty;
1794
input                   a_clk;
1795
input                   a_rst;
1796
// b side
1797
input  [data_width-1:0] b_d;
1798
input                   b_wr;
1799
output                  b_fifo_full;
1800
output [data_width-1:0] b_q;
1801
input                   b_rd;
1802
output                  b_fifo_empty;
1803
input                   b_clk;
1804
input                   b_rst;
1805
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1806
vl_fifo_1r1w_async_a (
1807
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
1808
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
1809
    );
1810
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
1811
vl_fifo_1r1w_async_b (
1812
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
1813
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
1814
    );
1815
endmodule
1816 8 unneback
module vl_fifo_2r2w_async_simplex (
1817 6 unneback
    // a side
1818
    a_d, a_wr, a_fifo_full,
1819
    a_q, a_rd, a_fifo_empty,
1820
    a_clk, a_rst,
1821
    // b side
1822
    b_d, b_wr, b_fifo_full,
1823
    b_q, b_rd, b_fifo_empty,
1824
    b_clk, b_rst
1825
    );
1826
parameter data_width = 18;
1827
parameter addr_width = 4;
1828
// a side
1829
input  [data_width-1:0] a_d;
1830
input                   a_wr;
1831
output                  a_fifo_full;
1832
output [data_width-1:0] a_q;
1833
input                   a_rd;
1834
output                  a_fifo_empty;
1835
input                   a_clk;
1836
input                   a_rst;
1837
// b side
1838
input  [data_width-1:0] b_d;
1839
input                   b_wr;
1840
output                  b_fifo_full;
1841
output [data_width-1:0] b_q;
1842
input                   b_rd;
1843
output                  b_fifo_empty;
1844
input                   b_clk;
1845
input                   b_rst;
1846
// adr_gen
1847
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
1848
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
1849
// dpram
1850
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
1851 18 unneback
vl_cnt_gray_ce_bin
1852 6 unneback
    # ( .length(addr_width))
1853
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
1854 18 unneback
vl_cnt_gray_ce_bin
1855 6 unneback
    # (.length(addr_width))
1856
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
1857 18 unneback
vl_cnt_gray_ce_bin
1858 6 unneback
    # ( .length(addr_width))
1859
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
1860 18 unneback
vl_cnt_gray_ce_bin
1861 6 unneback
    # (.length(addr_width))
1862
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
1863
// mux read or write adr to DPRAM
1864
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
1865
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
1866 11 unneback
vl_dpram_2r2w
1867 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
1868
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
1869
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
1870 11 unneback
vl_fifo_cmp_async
1871 6 unneback
    # (.addr_width(addr_width))
1872
    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) );
1873 11 unneback
vl_fifo_cmp_async
1874 6 unneback
    # (.addr_width(addr_width))
1875
    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) );
1876
endmodule
1877 48 unneback
module vl_reg_file (
1878
    a1, a2, a3, wd3, we3, rd1, rd2, clk
1879
);
1880
parameter data_width = 32;
1881
parameter addr_width = 5;
1882
input [addr_width-1:0] a1, a2, a3;
1883
input [data_width-1:0] wd3;
1884
input we3;
1885
output [data_width-1:0] rd1, rd2;
1886
input clk;
1887
reg [data_width-1:0] wd3_reg;
1888
reg [addr_width-1:0] a1_reg, a2_reg, a3_reg;
1889
reg we3_reg;
1890
reg [data_width-1:0] ram1 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1891
reg [data_width-1:0] ram2 [(1<<addr_width)-1:0] /*synthesis syn_ramstyle = "no_rw_check"*/;
1892
always @ (posedge clk or posedge rst)
1893
if (rst)
1894
    {wd3_reg, a3_reg, we3_reg} <= {(data_width+addr_width+1){1'b0}};
1895
else
1896
    {wd3_reg, a3_reg, we3_reg} <= {wd3,a3,wd3};
1897
    always @ (negedge clk)
1898
    if (we3_reg)
1899
        ram1[a3_reg] <= wd3;
1900
    always @ (posedge clk)
1901
        a1_reg <= a1;
1902
    assign rd1 = ram1[a1_reg];
1903
    always @ (negedge clk)
1904
    if (we3_reg)
1905
        ram2[a3_reg] <= wd3;
1906
    always @ (posedge clk)
1907
        a2_reg <= a2;
1908
    assign rd2 = ram2[a2_reg];
1909
endmodule
1910 12 unneback
//////////////////////////////////////////////////////////////////////
1911
////                                                              ////
1912
////  Versatile library, wishbone stuff                           ////
1913
////                                                              ////
1914
////  Description                                                 ////
1915
////  Wishbone compliant modules                                  ////
1916
////                                                              ////
1917
////                                                              ////
1918
////  To Do:                                                      ////
1919
////   -                                                          ////
1920
////                                                              ////
1921
////  Author(s):                                                  ////
1922
////      - Michael Unneback, unneback@opencores.org              ////
1923
////        ORSoC AB                                              ////
1924
////                                                              ////
1925
//////////////////////////////////////////////////////////////////////
1926
////                                                              ////
1927
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
1928
////                                                              ////
1929
//// This source file may be used and distributed without         ////
1930
//// restriction provided that this copyright statement is not    ////
1931
//// removed from the file and that any derivative work contains  ////
1932
//// the original copyright notice and the associated disclaimer. ////
1933
////                                                              ////
1934
//// This source file is free software; you can redistribute it   ////
1935
//// and/or modify it under the terms of the GNU Lesser General   ////
1936
//// Public License as published by the Free Software Foundation; ////
1937
//// either version 2.1 of the License, or (at your option) any   ////
1938
//// later version.                                               ////
1939
////                                                              ////
1940
//// This source is distributed in the hope that it will be       ////
1941
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1942
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1943
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1944
//// details.                                                     ////
1945
////                                                              ////
1946
//// You should have received a copy of the GNU Lesser General    ////
1947
//// Public License along with this source; if not, download it   ////
1948
//// from http://www.opencores.org/lgpl.shtml                     ////
1949
////                                                              ////
1950
//////////////////////////////////////////////////////////////////////
1951
// async wb3 - wb3 bridge
1952
`timescale 1ns/1ns
1953 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);
1954 83 unneback
parameter adr_width = 10;
1955
parameter max_burst_width = 4;
1956 85 unneback
input cyc_i, stb_i, we_i;
1957 83 unneback
input [2:0] cti_i;
1958
input [1:0] bte_i;
1959
input [adr_width-1:0] adr_i;
1960
output [adr_width-1:0] adr_o;
1961
output ack_o;
1962
input clk, rst;
1963
reg [adr_width-1:0] adr;
1964 90 unneback
wire [max_burst_width-1:0] to_adr;
1965 83 unneback
generate
1966
if (max_burst_width==0) begin : inst_0
1967
    reg ack_o;
1968
    assign adr_o = adr_i;
1969
    always @ (posedge clk or posedge rst)
1970
    if (rst)
1971
        ack_o <= 1'b0;
1972
    else
1973
        ack_o <= cyc_i & stb_i & !ack_o;
1974
end else begin
1975
    reg [1:0] last_cycle;
1976
    localparam idle = 2'b00;
1977
    localparam cyc  = 2'b01;
1978
    localparam ws   = 2'b10;
1979
    localparam eoc  = 2'b11;
1980
    always @ (posedge clk or posedge rst)
1981
    if (rst)
1982
        last_cycle <= idle;
1983
    else
1984
        last_cycle <= (!cyc_i) ? idle :
1985
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? eoc :
1986
                      (cyc_i & !stb_i) ? ws :
1987
                      cyc;
1988
    assign to_adr = (last_cycle==idle | last_cycle==eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
1989 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
1990
                                        (last_cycle==idle | last_cycle==eoc) ? adr_i[max_burst_width-1:0] :
1991
                                        adr[max_burst_width-1:0];
1992 90 unneback
    assign ack_o = (last_cycle==cyc | last_cycle==ws) & stb_i;
1993 83 unneback
end
1994
endgenerate
1995
generate
1996
if (max_burst_width==2) begin : inst_2
1997
    always @ (posedge clk or posedge rst)
1998
    if (rst)
1999
        adr <= 2'h0;
2000
    else
2001
        if (cyc_i & stb_i)
2002
            adr[1:0] <= to_adr[1:0] + 2'd1;
2003
        else
2004
            adr <= to_adr[1:0];
2005
end
2006
endgenerate
2007
generate
2008
if (max_burst_width==3) begin : inst_3
2009
    always @ (posedge clk or posedge rst)
2010
    if (rst)
2011
        adr <= 3'h0;
2012
    else
2013
        if (cyc_i & stb_i)
2014
            case (bte_i)
2015
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
2016
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
2017
            endcase
2018
        else
2019
            adr <= to_adr[2:0];
2020
end
2021
endgenerate
2022
generate
2023
if (max_burst_width==4) begin : inst_4
2024
    always @ (posedge clk or posedge rst)
2025
    if (rst)
2026
        adr <= 4'h0;
2027
    else
2028
        if (cyc_i & stb_i)
2029
            case (bte_i)
2030
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
2031
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
2032
            default: adr[3:0] <= to_adr + 4'd1;
2033
            endcase
2034
        else
2035
            adr <= to_adr[3:0];
2036
end
2037
endgenerate
2038
generate
2039
if (adr_width > max_burst_width) begin : pass_through
2040
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
2041
end
2042
endgenerate
2043
endmodule
2044
// async wb3 - wb3 bridge
2045
`timescale 1ns/1ns
2046 18 unneback
module vl_wb3wb3_bridge (
2047 12 unneback
        // wishbone slave side
2048
        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,
2049
        // wishbone master side
2050
        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);
2051
input [31:0] wbs_dat_i;
2052
input [31:2] wbs_adr_i;
2053
input [3:0]  wbs_sel_i;
2054
input [1:0]  wbs_bte_i;
2055
input [2:0]  wbs_cti_i;
2056
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
2057
output [31:0] wbs_dat_o;
2058 14 unneback
output wbs_ack_o;
2059 12 unneback
input wbs_clk, wbs_rst;
2060
output [31:0] wbm_dat_o;
2061
output reg [31:2] wbm_adr_o;
2062
output [3:0]  wbm_sel_o;
2063
output reg [1:0]  wbm_bte_o;
2064
output reg [2:0]  wbm_cti_o;
2065 14 unneback
output reg wbm_we_o;
2066
output wbm_cyc_o;
2067 12 unneback
output wbm_stb_o;
2068
input [31:0]  wbm_dat_i;
2069
input wbm_ack_i;
2070
input wbm_clk, wbm_rst;
2071
parameter addr_width = 4;
2072
// bte
2073
parameter linear       = 2'b00;
2074
parameter wrap4        = 2'b01;
2075
parameter wrap8        = 2'b10;
2076
parameter wrap16       = 2'b11;
2077
// cti
2078
parameter classic      = 3'b000;
2079
parameter incburst     = 3'b010;
2080
parameter endofburst   = 3'b111;
2081
parameter wbs_adr  = 1'b0;
2082
parameter wbs_data = 1'b1;
2083 33 unneback
parameter wbm_adr0      = 2'b00;
2084
parameter wbm_adr1      = 2'b01;
2085
parameter wbm_data      = 2'b10;
2086
parameter wbm_data_wait = 2'b11;
2087 12 unneback
reg [1:0] wbs_bte_reg;
2088
reg wbs;
2089
wire wbs_eoc_alert, wbm_eoc_alert;
2090
reg wbs_eoc, wbm_eoc;
2091
reg [1:0] wbm;
2092 14 unneback
wire [1:16] wbs_count, wbm_count;
2093 12 unneback
wire [35:0] a_d, a_q, b_d, b_q;
2094
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
2095
reg a_rd_reg;
2096
wire b_rd_adr, b_rd_data;
2097 14 unneback
wire b_rd_data_reg;
2098
wire [35:0] temp;
2099 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]);
2100
always @ (posedge wbs_clk or posedge wbs_rst)
2101
if (wbs_rst)
2102
        wbs_eoc <= 1'b0;
2103
else
2104
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
2105 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
2106 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
2107
                wbs_eoc <= 1'b1;
2108 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
2109 12 unneback
    cnt0 (
2110
        .cke(wbs_ack_o),
2111
        .clear(wbs_eoc),
2112
        .q(wbs_count),
2113
        .rst(wbs_rst),
2114
        .clk(wbs_clk));
2115
always @ (posedge wbs_clk or posedge wbs_rst)
2116
if (wbs_rst)
2117
        wbs <= wbs_adr;
2118
else
2119 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
2120 12 unneback
                wbs <= wbs_data;
2121
        else if (wbs_eoc & wbs_ack_o)
2122
                wbs <= wbs_adr;
2123
// wbs FIFO
2124 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};
2125
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
2126 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
2127
              1'b0;
2128
assign a_rd = !a_fifo_empty;
2129
always @ (posedge wbs_clk or posedge wbs_rst)
2130
if (wbs_rst)
2131
        a_rd_reg <= 1'b0;
2132
else
2133
        a_rd_reg <= a_rd;
2134
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
2135
assign wbs_dat_o = a_q[35:4];
2136
always @ (posedge wbs_clk or posedge wbs_rst)
2137
if (wbs_rst)
2138 13 unneback
        wbs_bte_reg <= 2'b00;
2139 12 unneback
else
2140 13 unneback
        wbs_bte_reg <= wbs_bte_i;
2141 12 unneback
// wbm FIFO
2142
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]);
2143
always @ (posedge wbm_clk or posedge wbm_rst)
2144
if (wbm_rst)
2145
        wbm_eoc <= 1'b0;
2146
else
2147
        if (wbm==wbm_adr0 & !b_fifo_empty)
2148
                wbm_eoc <= b_q[4:3] == linear;
2149
        else if (wbm_eoc_alert & wbm_ack_i)
2150
                wbm_eoc <= 1'b1;
2151
always @ (posedge wbm_clk or posedge wbm_rst)
2152
if (wbm_rst)
2153
        wbm <= wbm_adr0;
2154
else
2155 33 unneback
/*
2156 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
2157
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
2158
        (wbm==wbm_adr1 & !wbm_we_o) |
2159
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
2160
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
2161 33 unneback
*/
2162
    case (wbm)
2163
    wbm_adr0:
2164
        if (!b_fifo_empty)
2165
            wbm <= wbm_adr1;
2166
    wbm_adr1:
2167
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
2168
            wbm <= wbm_data;
2169
    wbm_data:
2170
        if (wbm_ack_i & wbm_eoc)
2171
            wbm <= wbm_adr0;
2172
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
2173
            wbm <= wbm_data_wait;
2174
    wbm_data_wait:
2175
        if (!b_fifo_empty)
2176
            wbm <= wbm_data;
2177
    endcase
2178 12 unneback
assign b_d = {wbm_dat_i,4'b1111};
2179
assign b_wr = !wbm_we_o & wbm_ack_i;
2180
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
2181
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
2182
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
2183 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
2184 12 unneback
                   1'b0;
2185
assign b_rd = b_rd_adr | b_rd_data;
2186 18 unneback
vl_dff dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
2187
vl_dff_ce # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
2188 12 unneback
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
2189 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
2190 12 unneback
    cnt1 (
2191
        .cke(wbm_ack_i),
2192
        .clear(wbm_eoc),
2193
        .q(wbm_count),
2194
        .rst(wbm_rst),
2195
        .clk(wbm_clk));
2196 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
2197
assign wbm_stb_o = (wbm==wbm_data);
2198 12 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
2199
if (wbm_rst)
2200
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
2201
else begin
2202
        if (wbm==wbm_adr0 & !b_fifo_empty)
2203
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
2204
        else if (wbm_eoc_alert & wbm_ack_i)
2205
                wbm_cti_o <= endofburst;
2206
end
2207
//async_fifo_dw_simplex_top
2208
vl_fifo_2r2w_async_simplex
2209
# ( .data_width(36), .addr_width(addr_width))
2210
fifo (
2211
    // a side
2212
    .a_d(a_d),
2213
    .a_wr(a_wr),
2214
    .a_fifo_full(a_fifo_full),
2215
    .a_q(a_q),
2216
    .a_rd(a_rd),
2217
    .a_fifo_empty(a_fifo_empty),
2218
    .a_clk(wbs_clk),
2219
    .a_rst(wbs_rst),
2220
    // b side
2221
    .b_d(b_d),
2222
    .b_wr(b_wr),
2223
    .b_fifo_full(b_fifo_full),
2224
    .b_q(b_q),
2225
    .b_rd(b_rd),
2226
    .b_fifo_empty(b_fifo_empty),
2227
    .b_clk(wbm_clk),
2228
    .b_rst(wbm_rst)
2229
    );
2230
endmodule
2231 75 unneback
module vl_wb3avalon_bridge (
2232
        // wishbone slave side
2233
        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,
2234 77 unneback
        // avalon master side
2235 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
2236 85 unneback
parameter linewrapburst = 1'b0;
2237 75 unneback
input [31:0] wbs_dat_i;
2238
input [31:2] wbs_adr_i;
2239
input [3:0]  wbs_sel_i;
2240
input [1:0]  wbs_bte_i;
2241
input [2:0]  wbs_cti_i;
2242 83 unneback
input wbs_we_i;
2243
input wbs_cyc_i;
2244
input wbs_stb_i;
2245 75 unneback
output [31:0] wbs_dat_o;
2246
output wbs_ack_o;
2247
input wbs_clk, wbs_rst;
2248
input [31:0] readdata;
2249
output [31:0] writedata;
2250
output [31:2] address;
2251
output [3:0]  be;
2252
output write;
2253 81 unneback
output read;
2254 75 unneback
output beginbursttransfer;
2255
output [3:0] burstcount;
2256
input readdatavalid;
2257
input waitrequest;
2258
input clk;
2259
input rst;
2260
wire [1:0] wbm_bte_o;
2261
wire [2:0] wbm_cti_o;
2262
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
2263
reg last_cyc;
2264 79 unneback
reg [3:0] counter;
2265 82 unneback
reg read_busy;
2266 75 unneback
always @ (posedge clk or posedge rst)
2267
if (rst)
2268
    last_cyc <= 1'b0;
2269
else
2270
    last_cyc <= wbm_cyc_o;
2271 79 unneback
always @ (posedge clk or posedge rst)
2272
if (rst)
2273 82 unneback
    read_busy <= 1'b0;
2274 79 unneback
else
2275 82 unneback
    if (read & !waitrequest)
2276
        read_busy <= 1'b1;
2277
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
2278
        read_busy <= 1'b0;
2279
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
2280 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
2281
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
2282
                    (wbm_bte_o==2'b10) ? 4'd8 :
2283 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
2284
                    4'd1;
2285 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
2286 79 unneback
always @ (posedge clk or posedge rst)
2287
if (rst) begin
2288
    counter <= 4'd0;
2289
end else
2290 80 unneback
    if (wbm_we_o) begin
2291
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
2292 85 unneback
            counter <= burstcount -4'd1;
2293 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
2294
            counter <= burstcount;
2295
        end else if (!waitrequest & wbm_stb_o) begin
2296
            counter <= counter - 4'd1;
2297
        end
2298 82 unneback
    end
2299 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
2300 77 unneback
vl_wb3wb3_bridge wbwb3inst (
2301 75 unneback
    // wishbone slave side
2302
    .wbs_dat_i(wbs_dat_i),
2303
    .wbs_adr_i(wbs_adr_i),
2304
    .wbs_sel_i(wbs_sel_i),
2305
    .wbs_bte_i(wbs_bte_i),
2306
    .wbs_cti_i(wbs_cti_i),
2307
    .wbs_we_i(wbs_we_i),
2308
    .wbs_cyc_i(wbs_cyc_i),
2309
    .wbs_stb_i(wbs_stb_i),
2310
    .wbs_dat_o(wbs_dat_o),
2311
    .wbs_ack_o(wbs_ack_o),
2312
    .wbs_clk(wbs_clk),
2313
    .wbs_rst(wbs_rst),
2314
    // wishbone master side
2315
    .wbm_dat_o(writedata),
2316 78 unneback
    .wbm_adr_o(address),
2317 75 unneback
    .wbm_sel_o(be),
2318
    .wbm_bte_o(wbm_bte_o),
2319
    .wbm_cti_o(wbm_cti_o),
2320
    .wbm_we_o(wbm_we_o),
2321
    .wbm_cyc_o(wbm_cyc_o),
2322
    .wbm_stb_o(wbm_stb_o),
2323
    .wbm_dat_i(readdata),
2324
    .wbm_ack_i(wbm_ack_i),
2325
    .wbm_clk(clk),
2326
    .wbm_rst(rst));
2327
endmodule
2328 39 unneback
module vl_wb3_arbiter_type1 (
2329
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
2330
    wbm_dat_i, wbm_ack_i, wbm_err_i, wbm_rty_i,
2331
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
2332
    wbs_dat_o, wbs_ack_o, wbs_err_o, wbs_rty_o,
2333
    wb_clk, wb_rst
2334
);
2335
parameter nr_of_ports = 3;
2336
parameter adr_size = 26;
2337
parameter adr_lo   = 2;
2338
parameter dat_size = 32;
2339
parameter sel_size = dat_size/8;
2340
localparam aw = (adr_size - adr_lo) * nr_of_ports;
2341
localparam dw = dat_size * nr_of_ports;
2342
localparam sw = sel_size * nr_of_ports;
2343
localparam cw = 3 * nr_of_ports;
2344
localparam bw = 2 * nr_of_ports;
2345
input  [dw-1:0] wbm_dat_o;
2346
input  [aw-1:0] wbm_adr_o;
2347
input  [sw-1:0] wbm_sel_o;
2348
input  [cw-1:0] wbm_cti_o;
2349
input  [bw-1:0] wbm_bte_o;
2350
input  [nr_of_ports-1:0] wbm_we_o, wbm_stb_o, wbm_cyc_o;
2351
output [dw-1:0] wbm_dat_i;
2352
output [nr_of_ports-1:0] wbm_ack_i, wbm_err_i, wbm_rty_i;
2353
output [dat_size-1:0] wbs_dat_i;
2354
output [adr_size-1:adr_lo] wbs_adr_i;
2355
output [sel_size-1:0] wbs_sel_i;
2356
output [2:0] wbs_cti_i;
2357
output [1:0] wbs_bte_i;
2358
output wbs_we_i, wbs_stb_i, wbs_cyc_i;
2359
input  [dat_size-1:0] wbs_dat_o;
2360
input  wbs_ack_o, wbs_err_o, wbs_rty_o;
2361
input wb_clk, wb_rst;
2362 44 unneback
reg  [nr_of_ports-1:0] select;
2363 39 unneback
wire [nr_of_ports-1:0] state;
2364
wire [nr_of_ports-1:0] eoc; // end-of-cycle
2365
wire [nr_of_ports-1:0] sel;
2366
wire idle;
2367
genvar i;
2368
assign idle = !(|state);
2369
generate
2370
if (nr_of_ports == 2) begin
2371
    wire [2:0] wbm1_cti_o, wbm0_cti_o;
2372
    assign {wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2373 44 unneback
    //assign select = (idle) ? {wbm_cyc_o[1],!wbm_cyc_o[1] & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2374
    always @ (idle or wbm_cyc_o)
2375
    if (idle)
2376
        casex (wbm_cyc_o)
2377
        2'b1x : select = 2'b10;
2378
        2'b01 : select = 2'b01;
2379
        default : select = {nr_of_ports{1'b0}};
2380
        endcase
2381
    else
2382
        select = {nr_of_ports{1'b0}};
2383 39 unneback
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2384
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2385
end
2386
endgenerate
2387
generate
2388
if (nr_of_ports == 3) begin
2389
    wire [2:0] wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2390
    assign {wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2391 44 unneback
    always @ (idle or wbm_cyc_o)
2392
    if (idle)
2393
        casex (wbm_cyc_o)
2394
        3'b1xx : select = 3'b100;
2395
        3'b01x : select = 3'b010;
2396
        3'b001 : select = 3'b001;
2397
        default : select = {nr_of_ports{1'b0}};
2398
        endcase
2399
    else
2400
        select = {nr_of_ports{1'b0}};
2401
//    assign select = (idle) ? {wbm_cyc_o[2],!wbm_cyc_o[2] & wbm_cyc_o[1],wbm_cyc_o[2:1]==2'b00 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2402 39 unneback
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2403
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2404
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2405
end
2406
endgenerate
2407
generate
2408 44 unneback
if (nr_of_ports == 4) begin
2409
    wire [2:0] wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2410
    assign {wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2411
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2412
    always @ (idle or wbm_cyc_o)
2413
    if (idle)
2414
        casex (wbm_cyc_o)
2415
        4'b1xxx : select = 4'b1000;
2416
        4'b01xx : select = 4'b0100;
2417
        4'b001x : select = 4'b0010;
2418
        4'b0001 : select = 4'b0001;
2419
        default : select = {nr_of_ports{1'b0}};
2420
        endcase
2421
    else
2422
        select = {nr_of_ports{1'b0}};
2423
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2424
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2425
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2426
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2427
end
2428
endgenerate
2429
generate
2430
if (nr_of_ports == 5) begin
2431
    wire [2:0] wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2432
    assign {wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2433
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2434
    always @ (idle or wbm_cyc_o)
2435
    if (idle)
2436
        casex (wbm_cyc_o)
2437
        5'b1xxxx : select = 5'b10000;
2438
        5'b01xxx : select = 5'b01000;
2439
        5'b001xx : select = 5'b00100;
2440
        5'b0001x : select = 5'b00010;
2441
        5'b00001 : select = 5'b00001;
2442
        default : select = {nr_of_ports{1'b0}};
2443
        endcase
2444
    else
2445
        select = {nr_of_ports{1'b0}};
2446
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2447
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2448
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2449
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2450
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2451
end
2452
endgenerate
2453
generate
2454 67 unneback
if (nr_of_ports == 6) begin
2455
    wire [2:0] wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2456
    assign {wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2457
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2458
    always @ (idle or wbm_cyc_o)
2459
    if (idle)
2460
        casex (wbm_cyc_o)
2461
        6'b1xxxxx : select = 6'b100000;
2462
        6'b01xxxx : select = 6'b010000;
2463
        6'b001xxx : select = 6'b001000;
2464
        6'b0001xx : select = 6'b000100;
2465
        6'b00001x : select = 6'b000010;
2466
        6'b000001 : select = 6'b000001;
2467
        default : select = {nr_of_ports{1'b0}};
2468
        endcase
2469
    else
2470
        select = {nr_of_ports{1'b0}};
2471
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2472
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2473
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2474
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2475
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2476
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2477
end
2478
endgenerate
2479
generate
2480
if (nr_of_ports == 7) begin
2481
    wire [2:0] wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2482
    assign {wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2483
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2484
    always @ (idle or wbm_cyc_o)
2485
    if (idle)
2486
        casex (wbm_cyc_o)
2487
        7'b1xxxxxx : select = 7'b1000000;
2488
        7'b01xxxxx : select = 7'b0100000;
2489
        7'b001xxxx : select = 7'b0010000;
2490
        7'b0001xxx : select = 7'b0001000;
2491
        7'b00001xx : select = 7'b0000100;
2492
        7'b000001x : select = 7'b0000010;
2493
        7'b0000001 : select = 7'b0000001;
2494
        default : select = {nr_of_ports{1'b0}};
2495
        endcase
2496
    else
2497
        select = {nr_of_ports{1'b0}};
2498
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
2499
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2500
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2501
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2502
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2503
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2504
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2505
end
2506
endgenerate
2507
generate
2508
if (nr_of_ports == 8) begin
2509
    wire [2:0] wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
2510
    assign {wbm7_cti_o, wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
2511
    //assign select = (idle) ? {wbm_cyc_o[3],!wbm_cyc_o[3] & wbm_cyc_o[2],wbm_cyc_o[3:2]==2'b00 & wbm_cyc_o[1],wbm_cyc_o[3:1]==3'b000 & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
2512
    always @ (idle or wbm_cyc_o)
2513
    if (idle)
2514
        casex (wbm_cyc_o)
2515
        8'b1xxxxxxx : select = 8'b10000000;
2516
        8'b01xxxxxx : select = 8'b01000000;
2517
        8'b001xxxxx : select = 8'b00100000;
2518
        8'b0001xxxx : select = 8'b00010000;
2519
        8'b00001xxx : select = 8'b00001000;
2520
        8'b000001xx : select = 8'b00000100;
2521
        8'b0000001x : select = 8'b00000010;
2522
        8'b00000001 : select = 8'b00000001;
2523
        default : select = {nr_of_ports{1'b0}};
2524
        endcase
2525
    else
2526
        select = {nr_of_ports{1'b0}};
2527
    assign eoc[7] = (wbm_ack_i[7] & (wbm7_cti_o == 3'b000 | wbm7_cti_o == 3'b111)) | !wbm_cyc_o[7];
2528
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
2529
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
2530
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
2531
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
2532
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
2533
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
2534
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
2535
end
2536
endgenerate
2537
generate
2538 63 unneback
for (i=0;i<nr_of_ports;i=i+1) begin : spr0
2539 39 unneback
    vl_spr sr0( .sp(select[i]), .r(eoc[i]), .q(state[i]), .clk(wb_clk), .rst(wb_rst));
2540
end
2541
endgenerate
2542
    assign sel = select | state;
2543
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(32)) mux0 ( .a(wbm_dat_o), .sel(sel), .dout(wbs_dat_i));
2544
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(adr_size-adr_lo)) mux1 ( .a(wbm_adr_o), .sel(sel), .dout(wbs_adr_i));
2545
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(sel_size)) mux2 ( .a(wbm_sel_o), .sel(sel), .dout(wbs_sel_i));
2546
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(3)) mux3 ( .a(wbm_cti_o), .sel(sel), .dout(wbs_cti_i));
2547
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(2)) mux4 ( .a(wbm_bte_o), .sel(sel), .dout(wbs_bte_i));
2548
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(1)) mux5 ( .a(wbm_we_o), .sel(sel), .dout(wbs_we_i));
2549
    vl_mux_andor # ( .nr_of_ports(nr_of_ports), .width(1)) mux6 ( .a(wbm_stb_o), .sel(sel), .dout(wbs_stb_i));
2550
    assign wbs_cyc_i = |sel;
2551
    assign wbm_dat_i = {nr_of_ports{wbs_dat_o}};
2552
    assign wbm_ack_i = {nr_of_ports{wbs_ack_o}} & sel;
2553
    assign wbm_err_i = {nr_of_ports{wbs_err_o}} & sel;
2554
    assign wbm_rty_i = {nr_of_ports{wbs_rty_o}} & sel;
2555
endmodule
2556 49 unneback
// WB RAM with byte enable
2557 59 unneback
module vl_wb_b3_ram_be (
2558 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,
2559
    wbs_dat_o, wbs_ack_o, wb_clk, wb_rst);
2560 68 unneback
parameter adr_size = 16;
2561 85 unneback
parameter mem_size = 1<<adr_size;
2562 60 unneback
parameter dat_size = 32;
2563 83 unneback
parameter max_burst_width = 4;
2564 60 unneback
parameter memory_init = 1;
2565
parameter memory_file = "vl_ram.vmem";
2566 85 unneback
localparam aw = (adr_size);
2567 69 unneback
localparam dw = dat_size;
2568
localparam sw = dat_size/8;
2569
localparam cw = 3;
2570
localparam bw = 2;
2571 70 unneback
input [dw-1:0] wbs_dat_i;
2572
input [aw-1:0] wbs_adr_i;
2573
input [cw-1:0] wbs_cti_i;
2574
input [bw-1:0] wbs_bte_i;
2575
input [sw-1:0] wbs_sel_i;
2576
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
2577
output [dw-1:0] wbs_dat_o;
2578
output wbs_ack_o;
2579 71 unneback
input wb_clk, wb_rst;
2580 83 unneback
wire [aw-1:0] adr;
2581 60 unneback
vl_ram_be # (
2582
    .data_width(dat_size),
2583 83 unneback
    .addr_width(aw),
2584 69 unneback
    .mem_size(mem_size),
2585 68 unneback
    .memory_init(memory_init),
2586
    .memory_file(memory_file))
2587 60 unneback
ram0(
2588
    .d(wbs_dat_i),
2589 83 unneback
    .adr(adr),
2590 60 unneback
    .be(wbs_sel_i),
2591 90 unneback
    .re(wbs_stb_i),
2592 86 unneback
    .we(wbs_we_i & wbs_ack_o),
2593 60 unneback
    .q(wbs_dat_o),
2594
    .clk(wb_clk)
2595
);
2596 83 unneback
vl_wb_adr_inc # ( .adr_width(aw), .max_burst_width(max_burst_width)) adr_inc0 (
2597
    .cyc_i(wbs_cyc_i),
2598
    .stb_i(wbs_stb_i),
2599
    .cti_i(wbs_cti_i),
2600
    .bte_i(wbs_bte_i),
2601
    .adr_i(wbs_adr_i),
2602 85 unneback
    .we_i(wbs_we_i),
2603 83 unneback
    .ack_o(wbs_ack_o),
2604
    .adr_o(adr),
2605
    .clk(wb_clk),
2606
    .rst(wb_rst));
2607 59 unneback
endmodule
2608
// WB RAM with byte enable
2609 49 unneback
module vl_wb_b4_ram_be (
2610
    wb_dat_i, wb_adr_i, wb_sel_i, wb_we_i, wb_stb_i, wb_cyc_i,
2611 52 unneback
    wb_dat_o, wb_stall_o, wb_ack_o, wb_clk, wb_rst);
2612 49 unneback
    parameter dat_width = 32;
2613
    parameter adr_width = 8;
2614
input [dat_width-1:0] wb_dat_i;
2615
input [adr_width-1:0] wb_adr_i;
2616
input [dat_width/8-1:0] wb_sel_i;
2617
input wb_we_i, wb_stb_i, wb_cyc_i;
2618
output [dat_width-1:0] wb_dat_o;
2619 51 unneback
reg [dat_width-1:0] wb_dat_o;
2620 52 unneback
output wb_stall_o;
2621 49 unneback
output wb_ack_o;
2622
reg wb_ack_o;
2623
input wb_clk, wb_rst;
2624 56 unneback
wire [dat_width/8-1:0] cke;
2625 49 unneback
generate
2626
if (dat_width==32) begin
2627 51 unneback
reg [7:0] ram3 [1<<(adr_width-2)-1:0];
2628
reg [7:0] ram2 [1<<(adr_width-2)-1:0];
2629
reg [7:0] ram1 [1<<(adr_width-2)-1:0];
2630
reg [7:0] ram0 [1<<(adr_width-2)-1:0];
2631 56 unneback
assign cke = wb_sel_i & {(dat_width/8){wb_we_i}};
2632 49 unneback
    always @ (posedge wb_clk)
2633
    begin
2634 56 unneback
        if (cke[3]) ram3[wb_adr_i[adr_width-1:2]] <= wb_dat_i[31:24];
2635
        if (cke[2]) ram2[wb_adr_i[adr_width-1:2]] <= wb_dat_i[23:16];
2636
        if (cke[1]) ram1[wb_adr_i[adr_width-1:2]] <= wb_dat_i[15:8];
2637
        if (cke[0]) ram0[wb_adr_i[adr_width-1:2]] <= wb_dat_i[7:0];
2638 49 unneback
    end
2639 59 unneback
    always @ (posedge wb_clk or posedge wb_rst)
2640
    begin
2641
        if (wb_rst)
2642
            wb_dat_o <= 32'h0;
2643
        else
2644
            wb_dat_o <= {ram3[wb_adr_i[adr_width-1:2]],ram2[wb_adr_i[adr_width-1:2]],ram1[wb_adr_i[adr_width-1:2]],ram0[wb_adr_i[adr_width-1:2]]};
2645
    end
2646 49 unneback
end
2647
endgenerate
2648 52 unneback
always @ (posedge wb_clk or posedge wb_rst)
2649 55 unneback
if (wb_rst)
2650 52 unneback
    wb_ack_o <= 1'b0;
2651
else
2652 54 unneback
    wb_ack_o <= wb_stb_i & wb_cyc_i;
2653 52 unneback
assign wb_stall_o = 1'b0;
2654 49 unneback
endmodule
2655 17 unneback
// WB ROM
2656 48 unneback
module vl_wb_b4_rom (
2657
    wb_adr_i, wb_stb_i, wb_cyc_i,
2658
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
2659
    parameter dat_width = 32;
2660
    parameter dat_default = 32'h15000000;
2661
    parameter adr_width = 32;
2662
/*
2663
`ifndef ROM
2664
`define ROM "rom.v"
2665
`endif
2666
*/
2667
    input [adr_width-1:2]   wb_adr_i;
2668
    input                   wb_stb_i;
2669
    input                   wb_cyc_i;
2670
    output [dat_width-1:0]  wb_dat_o;
2671
    reg [dat_width-1:0]     wb_dat_o;
2672
    output                  wb_ack_o;
2673
    reg                     wb_ack_o;
2674
    output                  stall_o;
2675
    input                   wb_clk;
2676
    input                   wb_rst;
2677
always @ (posedge wb_clk or posedge wb_rst)
2678
    if (wb_rst)
2679
        wb_dat_o <= {dat_width{1'b0}};
2680
    else
2681
         case (wb_adr_i[adr_width-1:2])
2682
`ifdef ROM
2683
`include `ROM
2684
`endif
2685
           default:
2686
             wb_dat_o <= dat_default;
2687
         endcase // case (wb_adr_i)
2688
always @ (posedge wb_clk or posedge wb_rst)
2689
    if (wb_rst)
2690
        wb_ack_o <= 1'b0;
2691
    else
2692
        wb_ack_o <= wb_stb_i & wb_cyc_i;
2693
assign stall_o = 1'b0;
2694
endmodule
2695
// WB ROM
2696 18 unneback
module vl_wb_boot_rom (
2697 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
2698 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
2699
    parameter adr_hi = 31;
2700
    parameter adr_lo = 28;
2701
    parameter adr_sel = 4'hf;
2702
    parameter addr_width = 5;
2703 33 unneback
/*
2704 17 unneback
`ifndef BOOT_ROM
2705
`define BOOT_ROM "boot_rom.v"
2706
`endif
2707 33 unneback
*/
2708 18 unneback
    input [adr_hi:2]    wb_adr_i;
2709
    input               wb_stb_i;
2710
    input               wb_cyc_i;
2711
    output [31:0]        wb_dat_o;
2712
    output              wb_ack_o;
2713
    output              hit_o;
2714
    input               wb_clk;
2715
    input               wb_rst;
2716
    wire hit;
2717
    reg [31:0] wb_dat;
2718
    reg wb_ack;
2719
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
2720 17 unneback
always @ (posedge wb_clk or posedge wb_rst)
2721
    if (wb_rst)
2722 18 unneback
        wb_dat <= 32'h15000000;
2723 17 unneback
    else
2724 18 unneback
         case (wb_adr_i[addr_width-1:2])
2725 33 unneback
`ifdef BOOT_ROM
2726 17 unneback
`include `BOOT_ROM
2727 33 unneback
`endif
2728 17 unneback
           /*
2729
            // Zero r0 and jump to 0x00000100
2730 18 unneback
 
2731
            1 : wb_dat <= 32'hA8200000;
2732
            2 : wb_dat <= 32'hA8C00100;
2733
            3 : wb_dat <= 32'h44003000;
2734
            4 : wb_dat <= 32'h15000000;
2735 17 unneback
            */
2736
           default:
2737 18 unneback
             wb_dat <= 32'h00000000;
2738 17 unneback
         endcase // case (wb_adr_i)
2739
always @ (posedge wb_clk or posedge wb_rst)
2740
    if (wb_rst)
2741 18 unneback
        wb_ack <= 1'b0;
2742 17 unneback
    else
2743 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
2744
assign hit_o = hit;
2745
assign wb_dat_o = wb_dat & {32{wb_ack}};
2746
assign wb_ack_o = wb_ack;
2747 17 unneback
endmodule
2748 32 unneback
module vl_wb_dpram (
2749
        // wishbone slave side a
2750
        wbsa_dat_i, wbsa_adr_i, wbsa_we_i, wbsa_cyc_i, wbsa_stb_i, wbsa_dat_o, wbsa_ack_o,
2751
        wbsa_clk, wbsa_rst,
2752
        // wishbone slave side a
2753
        wbsb_dat_i, wbsb_adr_i, wbsb_we_i, wbsb_cyc_i, wbsb_stb_i, wbsb_dat_o, wbsb_ack_o,
2754
        wbsb_clk, wbsb_rst);
2755
parameter data_width = 32;
2756
parameter addr_width = 8;
2757
parameter dat_o_mask_a = 1;
2758
parameter dat_o_mask_b = 1;
2759
input [31:0] wbsa_dat_i;
2760
input [addr_width-1:2] wbsa_adr_i;
2761
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
2762
output [31:0] wbsa_dat_o;
2763
output wbsa_ack_o;
2764
input wbsa_clk, wbsa_rst;
2765
input [31:0] wbsb_dat_i;
2766
input [addr_width-1:2] wbsb_adr_i;
2767
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
2768
output [31:0] wbsb_dat_o;
2769
output wbsb_ack_o;
2770
input wbsb_clk, wbsb_rst;
2771
wire wbsa_dat_tmp, wbsb_dat_tmp;
2772
vl_dpram_2r2w # (
2773 33 unneback
    .data_width(data_width), .addr_width(addr_width) )
2774 32 unneback
dpram0(
2775
    .d_a(wbsa_dat_i),
2776
    .q_a(wbsa_dat_tmp),
2777
    .adr_a(wbsa_adr_i),
2778
    .we_a(wbsa_we_i),
2779
    .clk_a(wbsa_clk),
2780
    .d_b(wbsb_dat_i),
2781
    .q_b(wbsb_dat_tmp),
2782
    .adr_b(wbsb_adr_i),
2783
    .we_b(wbsb_we_i),
2784
    .clk_b(wbsb_clk) );
2785 33 unneback
generate if (dat_o_mask_a==1)
2786 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp & {data_width{wbsa_ack_o}};
2787
endgenerate
2788 33 unneback
generate if (dat_o_mask_a==0)
2789 32 unneback
    assign wbsa_dat_o = wbsa_dat_tmp;
2790
endgenerate
2791 33 unneback
generate if (dat_o_mask_b==1)
2792 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp & {data_width{wbsb_ack_o}};
2793
endgenerate
2794 33 unneback
generate if (dat_o_mask_b==0)
2795 32 unneback
    assign wbsb_dat_o = wbsb_dat_tmp;
2796
endgenerate
2797
vl_spr ack_a( .sp(wbsa_cyc_i & wbsa_stb_i & !wbsa_ack_o), .r(1'b1), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
2798
vl_spr ack_b( .sp(wbsb_cyc_i & wbsb_stb_i & !wbsb_ack_o), .r(1'b1), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
2799
endmodule
2800 18 unneback
//////////////////////////////////////////////////////////////////////
2801
////                                                              ////
2802
////  Arithmetic functions                                        ////
2803
////                                                              ////
2804
////  Description                                                 ////
2805
////  Arithmetic functions for ALU and DSP                        ////
2806
////                                                              ////
2807
////                                                              ////
2808
////  To Do:                                                      ////
2809
////   -                                                          ////
2810
////                                                              ////
2811
////  Author(s):                                                  ////
2812
////      - Michael Unneback, unneback@opencores.org              ////
2813
////        ORSoC AB                                              ////
2814
////                                                              ////
2815
//////////////////////////////////////////////////////////////////////
2816
////                                                              ////
2817
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2818
////                                                              ////
2819
//// This source file may be used and distributed without         ////
2820
//// restriction provided that this copyright statement is not    ////
2821
//// removed from the file and that any derivative work contains  ////
2822
//// the original copyright notice and the associated disclaimer. ////
2823
////                                                              ////
2824
//// This source file is free software; you can redistribute it   ////
2825
//// and/or modify it under the terms of the GNU Lesser General   ////
2826
//// Public License as published by the Free Software Foundation; ////
2827
//// either version 2.1 of the License, or (at your option) any   ////
2828
//// later version.                                               ////
2829
////                                                              ////
2830
//// This source is distributed in the hope that it will be       ////
2831
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2832
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2833
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2834
//// details.                                                     ////
2835
////                                                              ////
2836
//// You should have received a copy of the GNU Lesser General    ////
2837
//// Public License along with this source; if not, download it   ////
2838
//// from http://www.opencores.org/lgpl.shtml                     ////
2839
////                                                              ////
2840
//////////////////////////////////////////////////////////////////////
2841
// signed multiplication
2842
module vl_mults (a,b,p);
2843
parameter operand_a_width = 18;
2844
parameter operand_b_width = 18;
2845
parameter result_hi = 35;
2846
parameter result_lo = 0;
2847
input [operand_a_width-1:0] a;
2848
input [operand_b_width-1:0] b;
2849
output [result_hi:result_lo] p;
2850
wire signed [operand_a_width-1:0] ai;
2851
wire signed [operand_b_width-1:0] bi;
2852
wire signed [operand_a_width+operand_b_width-1:0] result;
2853
    assign ai = a;
2854
    assign bi = b;
2855
    assign result = ai * bi;
2856
    assign p = result[result_hi:result_lo];
2857
endmodule
2858
module vl_mults18x18 (a,b,p);
2859
input [17:0] a,b;
2860
output [35:0] p;
2861
vl_mult
2862
    # (.operand_a_width(18), .operand_b_width(18))
2863
    mult0 (.a(a), .b(b), .p(p));
2864
endmodule
2865
// unsigned multiplication
2866
module vl_mult (a,b,p);
2867
parameter operand_a_width = 18;
2868
parameter operand_b_width = 18;
2869
parameter result_hi = 35;
2870
parameter result_lo = 0;
2871
input [operand_a_width-1:0] a;
2872
input [operand_b_width-1:0] b;
2873
output [result_hi:result_hi] p;
2874
wire [operand_a_width+operand_b_width-1:0] result;
2875
    assign result = a * b;
2876
    assign p = result[result_hi:result_lo];
2877
endmodule
2878
// shift unit
2879
// supporting the following shift functions
2880
//   SLL
2881
//   SRL
2882
//   SRA
2883
module vl_shift_unit_32( din, s, dout, opcode);
2884
input [31:0] din; // data in operand
2885
input [4:0] s; // shift operand
2886
input [1:0] opcode;
2887
output [31:0] dout;
2888
parameter opcode_sll = 2'b00;
2889
//parameter opcode_srl = 2'b01;
2890
parameter opcode_sra = 2'b10;
2891
//parameter opcode_ror = 2'b11;
2892
wire sll, sra;
2893
assign sll = opcode == opcode_sll;
2894
assign sra = opcode == opcode_sra;
2895
wire [15:1] s1;
2896
wire [3:0] sign;
2897
wire [7:0] tmp [0:3];
2898
// first stage is multiplier based
2899
// shift operand as fractional 8.7
2900
assign s1[15] = sll & s[2:0]==3'd7;
2901
assign s1[14] = sll & s[2:0]==3'd6;
2902
assign s1[13] = sll & s[2:0]==3'd5;
2903
assign s1[12] = sll & s[2:0]==3'd4;
2904
assign s1[11] = sll & s[2:0]==3'd3;
2905
assign s1[10] = sll & s[2:0]==3'd2;
2906
assign s1[ 9] = sll & s[2:0]==3'd1;
2907
assign s1[ 8] = s[2:0]==3'd0;
2908
assign s1[ 7] = !sll & s[2:0]==3'd1;
2909
assign s1[ 6] = !sll & s[2:0]==3'd2;
2910
assign s1[ 5] = !sll & s[2:0]==3'd3;
2911
assign s1[ 4] = !sll & s[2:0]==3'd4;
2912
assign s1[ 3] = !sll & s[2:0]==3'd5;
2913
assign s1[ 2] = !sll & s[2:0]==3'd6;
2914
assign s1[ 1] = !sll & s[2:0]==3'd7;
2915
assign sign[3] = din[31] & sra;
2916
assign sign[2] = sign[3] & (&din[31:24]);
2917
assign sign[1] = sign[2] & (&din[23:16]);
2918
assign sign[0] = sign[1] & (&din[15:8]);
2919
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]));
2920
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]));
2921
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]));
2922
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]));
2923
// second stage is multiplexer based
2924
// shift on byte level
2925
// mux byte 3
2926
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
2927
                     (sll & s[4:3]==2'b01) ? tmp[2] :
2928
                     (sll & s[4:3]==2'b10) ? tmp[1] :
2929
                     (sll & s[4:3]==2'b11) ? tmp[0] :
2930
                     {8{sign[3]}};
2931
// mux byte 2
2932
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
2933
                     (sll & s[4:3]==2'b01) ? tmp[1] :
2934
                     (sll & s[4:3]==2'b10) ? tmp[0] :
2935
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
2936
                     (s[4:3]==2'b01) ? tmp[3] :
2937
                     {8{sign[3]}};
2938
// mux byte 1
2939
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
2940
                     (sll & s[4:3]==2'b01) ? tmp[0] :
2941
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
2942
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
2943
                     (s[4:3]==2'b01) ? tmp[2] :
2944
                     (s[4:3]==2'b10) ? tmp[3] :
2945
                     {8{sign[3]}};
2946
// mux byte 0
2947
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
2948
                     (sll) ?  {8{1'b0}}:
2949
                     (s[4:3]==2'b01) ? tmp[1] :
2950
                     (s[4:3]==2'b10) ? tmp[2] :
2951
                     tmp[3];
2952
endmodule
2953
// logic unit
2954
// supporting the following logic functions
2955
//    a and b
2956
//    a or  b
2957
//    a xor b
2958
//    not b
2959
module vl_logic_unit( a, b, result, opcode);
2960
parameter width = 32;
2961
parameter opcode_and = 2'b00;
2962
parameter opcode_or  = 2'b01;
2963
parameter opcode_xor = 2'b10;
2964
input [width-1:0] a,b;
2965
output [width-1:0] result;
2966
input [1:0] opcode;
2967
assign result = (opcode==opcode_and) ? a & b :
2968
                (opcode==opcode_or)  ? a | b :
2969
                (opcode==opcode_xor) ? a ^ b :
2970
                b;
2971
endmodule

powered by: WebSVN 2.1.0

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