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 103

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

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

powered by: WebSVN 2.1.0

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