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

Subversion Repositories versatile_library

[/] [versatile_library/] [trunk/] [rtl/] [verilog/] [versatile_library_altera.v] - Blame information for rev 86

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

powered by: WebSVN 2.1.0

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