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 139

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

Line No. Rev Author Line
1 60 unneback
// default SYN_KEEP definition
2 136 unneback
///////////////////////////////////////
3
// dependencies
4
///////////////////////////////////////
5 97 unneback
// size to width
6 6 unneback
//////////////////////////////////////////////////////////////////////
7
////                                                              ////
8
////  Versatile library, clock and reset                          ////
9
////                                                              ////
10
////  Description                                                 ////
11
////  Logic related to clock and reset                            ////
12
////                                                              ////
13
////                                                              ////
14
////  To Do:                                                      ////
15
////   - add more different registers                             ////
16
////                                                              ////
17
////  Author(s):                                                  ////
18
////      - Michael Unneback, unneback@opencores.org              ////
19
////        ORSoC AB                                              ////
20
////                                                              ////
21
//////////////////////////////////////////////////////////////////////
22
////                                                              ////
23
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
24
////                                                              ////
25
//// This source file may be used and distributed without         ////
26
//// restriction provided that this copyright statement is not    ////
27
//// removed from the file and that any derivative work contains  ////
28
//// the original copyright notice and the associated disclaimer. ////
29
////                                                              ////
30
//// This source file is free software; you can redistribute it   ////
31
//// and/or modify it under the terms of the GNU Lesser General   ////
32
//// Public License as published by the Free Software Foundation; ////
33
//// either version 2.1 of the License, or (at your option) any   ////
34
//// later version.                                               ////
35
////                                                              ////
36
//// This source is distributed in the hope that it will be       ////
37
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
38
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
39
//// PURPOSE.  See the GNU Lesser General Public License for more ////
40
//// details.                                                     ////
41
////                                                              ////
42
//// You should have received a copy of the GNU Lesser General    ////
43
//// Public License along with this source; if not, download it   ////
44
//// from http://www.opencores.org/lgpl.shtml                     ////
45
////                                                              ////
46
//////////////////////////////////////////////////////////////////////
47 21 unneback
//altera
48 33 unneback
module vl_gbuf ( i, o);
49
input i;
50
output o;
51
assign o = i;
52
endmodule
53 6 unneback
 // ALTERA
54
 //ACTEL
55
// sync reset
56 17 unneback
// input active lo async reset, normally from external reset generator and/or switch
57 6 unneback
// output active high global reset sync with two DFFs 
58
`timescale 1 ns/100 ps
59
module vl_sync_rst ( rst_n_i, rst_o, clk);
60
input rst_n_i, clk;
61
output rst_o;
62 18 unneback
reg [1:0] tmp;
63 6 unneback
always @ (posedge clk or negedge rst_n_i)
64
if (!rst_n_i)
65 17 unneback
        tmp <= 2'b11;
66 6 unneback
else
67 33 unneback
        tmp <= {1'b0,tmp[1]};
68 17 unneback
vl_gbuf buf_i0( .i(tmp[0]), .o(rst_o));
69 6 unneback
endmodule
70
// vl_pll
71 32 unneback
///////////////////////////////////////////////////////////////////////////////
72
`timescale 1 ps/1 ps
73
module vl_pll ( clk_i, rst_n_i, lock, clk_o, rst_o);
74
parameter index = 0;
75
parameter number_of_clk = 1;
76
parameter period_time_0 = 20000;
77
parameter period_time_1 = 20000;
78
parameter period_time_2 = 20000;
79
parameter period_time_3 = 20000;
80
parameter period_time_4 = 20000;
81
parameter lock_delay = 2000000;
82
input clk_i, rst_n_i;
83
output lock;
84
output reg [0:number_of_clk-1] clk_o;
85
output [0:number_of_clk-1] rst_o;
86 33 unneback
`ifdef SIM_PLL
87 32 unneback
always
88
     #((period_time_0)/2) clk_o[0] <=  (!rst_n_i) ? 0 : ~clk_o[0];
89
generate if (number_of_clk > 1)
90
always
91
     #((period_time_1)/2) clk_o[1] <=  (!rst_n_i) ? 0 : ~clk_o[1];
92
endgenerate
93
generate if (number_of_clk > 2)
94
always
95
     #((period_time_2)/2) clk_o[2] <=  (!rst_n_i) ? 0 : ~clk_o[2];
96
endgenerate
97 33 unneback
generate if (number_of_clk > 3)
98 32 unneback
always
99
     #((period_time_3)/2) clk_o[3] <=  (!rst_n_i) ? 0 : ~clk_o[3];
100
endgenerate
101 33 unneback
generate if (number_of_clk > 4)
102 32 unneback
always
103
     #((period_time_4)/2) clk_o[4] <=  (!rst_n_i) ? 0 : ~clk_o[4];
104
endgenerate
105
genvar i;
106
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
107
     vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
108
end
109
endgenerate
110 33 unneback
//assign #lock_delay lock = rst_n_i;
111
assign lock = rst_n_i;
112 32 unneback
endmodule
113 33 unneback
`else
114
`ifdef VL_PLL0
115
`ifdef VL_PLL0_CLK1
116
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
117
`endif
118
`ifdef VL_PLL0_CLK2
119
    pll0 pll0_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
120
`endif
121
`ifdef VL_PLL0_CLK3
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]));
123
`endif
124
`ifdef VL_PLL0_CLK4
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]));
126
`endif
127
`ifdef VL_PLL0_CLK5
128
    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]));
129
`endif
130
`endif
131
`ifdef VL_PLL1
132
`ifdef VL_PLL1_CLK1
133
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
134
`endif
135
`ifdef VL_PLL1_CLK2
136
    pll1 pll1_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
137
`endif
138
`ifdef VL_PLL1_CLK3
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]));
140
`endif
141
`ifdef VL_PLL1_CLK4
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]));
143
`endif
144
`ifdef VL_PLL1_CLK5
145
    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]));
146
`endif
147
`endif
148
`ifdef VL_PLL2
149
`ifdef VL_PLL2_CLK1
150
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
151
`endif
152
`ifdef VL_PLL2_CLK2
153
    pll2 pll2_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
154
`endif
155
`ifdef VL_PLL2_CLK3
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]));
157
`endif
158
`ifdef VL_PLL2_CLK4
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]));
160
`endif
161
`ifdef VL_PLL2_CLK5
162
    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]));
163
`endif
164
`endif
165
`ifdef VL_PLL3
166
`ifdef VL_PLL3_CLK1
167
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]));
168
`endif
169
`ifdef VL_PLL3_CLK2
170
    pll3 pll3_i0 (.areset(rst_n_i), .inclk0(clk_i), .locked(lock), .c0(clk_o[0]), .c1(clk_o[1]));
171
`endif
172
`ifdef VL_PLL3_CLK3
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]));
174
`endif
175
`ifdef VL_PLL3_CLK4
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]));
177
`endif
178
`ifdef VL_PLL3_CLK5
179
    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]));
180
`endif
181
`endif
182 32 unneback
genvar i;
183
generate for (i=0;i<number_of_clk;i=i+1) begin: clock
184 40 unneback
        vl_sync_rst rst_i0 ( .rst_n_i(rst_n_i | lock), .rst_o(rst_o[i]), .clk(clk_o[i]));
185 32 unneback
end
186
endgenerate
187
endmodule
188 33 unneback
`endif
189 32 unneback
///////////////////////////////////////////////////////////////////////////////
190 6 unneback
 //altera
191
 //actel
192
//////////////////////////////////////////////////////////////////////
193
////                                                              ////
194
////  Versatile library, registers                                ////
195
////                                                              ////
196
////  Description                                                 ////
197
////  Different type of registers                                 ////
198
////                                                              ////
199
////                                                              ////
200
////  To Do:                                                      ////
201
////   - add more different registers                             ////
202
////                                                              ////
203
////  Author(s):                                                  ////
204
////      - Michael Unneback, unneback@opencores.org              ////
205
////        ORSoC AB                                              ////
206
////                                                              ////
207
//////////////////////////////////////////////////////////////////////
208
////                                                              ////
209
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
210
////                                                              ////
211
//// This source file may be used and distributed without         ////
212
//// restriction provided that this copyright statement is not    ////
213
//// removed from the file and that any derivative work contains  ////
214
//// the original copyright notice and the associated disclaimer. ////
215
////                                                              ////
216
//// This source file is free software; you can redistribute it   ////
217
//// and/or modify it under the terms of the GNU Lesser General   ////
218
//// Public License as published by the Free Software Foundation; ////
219
//// either version 2.1 of the License, or (at your option) any   ////
220
//// later version.                                               ////
221
////                                                              ////
222
//// This source is distributed in the hope that it will be       ////
223
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
224
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
225
//// PURPOSE.  See the GNU Lesser General Public License for more ////
226
//// details.                                                     ////
227
////                                                              ////
228
//// You should have received a copy of the GNU Lesser General    ////
229
//// Public License along with this source; if not, download it   ////
230
//// from http://www.opencores.org/lgpl.shtml                     ////
231
////                                                              ////
232
//////////////////////////////////////////////////////////////////////
233 18 unneback
module vl_dff ( d, q, clk, rst);
234 6 unneback
        parameter width = 1;
235 139 unneback
        parameter reset_value = {width{1'b0}};
236 6 unneback
        input [width-1:0] d;
237
        input clk, rst;
238
        output reg [width-1:0] q;
239
        always @ (posedge clk or posedge rst)
240
        if (rst)
241
                q <= reset_value;
242
        else
243
                q <= d;
244
endmodule
245 18 unneback
module vl_dff_array ( d, q, clk, rst);
246 6 unneback
        parameter width = 1;
247
        parameter depth = 2;
248
        parameter reset_value = 1'b0;
249
        input [width-1:0] d;
250
        input clk, rst;
251
        output [width-1:0] q;
252
        reg  [0:depth-1] q_tmp [width-1:0];
253
        integer i;
254
        always @ (posedge clk or posedge rst)
255
        if (rst) begin
256
            for (i=0;i<depth;i=i+1)
257
                q_tmp[i] <= {width{reset_value}};
258
        end else begin
259
            q_tmp[0] <= d;
260
            for (i=1;i<depth;i=i+1)
261
                q_tmp[i] <= q_tmp[i-1];
262
        end
263
    assign q = q_tmp[depth-1];
264
endmodule
265 18 unneback
module vl_dff_ce ( d, ce, q, clk, rst);
266 6 unneback
        parameter width = 1;
267 139 unneback
        parameter reset_value = {width{1'b0}};
268 6 unneback
        input [width-1:0] d;
269
        input ce, clk, rst;
270
        output reg [width-1:0] q;
271
        always @ (posedge clk or posedge rst)
272
        if (rst)
273
                q <= reset_value;
274
        else
275
                if (ce)
276
                        q <= d;
277
endmodule
278 18 unneback
module vl_dff_ce_clear ( d, ce, clear, q, clk, rst);
279 8 unneback
        parameter width = 1;
280 139 unneback
        parameter reset_value = {width{1'b0}};
281 8 unneback
        input [width-1:0] d;
282 10 unneback
        input ce, clear, clk, rst;
283 8 unneback
        output reg [width-1:0] q;
284
        always @ (posedge clk or posedge rst)
285
        if (rst)
286
            q <= reset_value;
287
        else
288
            if (ce)
289
                if (clear)
290
                    q <= {width{1'b0}};
291
                else
292
                    q <= d;
293
endmodule
294 24 unneback
module vl_dff_ce_set ( d, ce, set, q, clk, rst);
295
        parameter width = 1;
296 139 unneback
        parameter reset_value = {width{1'b0}};
297 24 unneback
        input [width-1:0] d;
298
        input ce, set, clk, rst;
299
        output reg [width-1:0] q;
300
        always @ (posedge clk or posedge rst)
301
        if (rst)
302
            q <= reset_value;
303
        else
304
            if (ce)
305
                if (set)
306
                    q <= {width{1'b1}};
307
                else
308
                    q <= d;
309
endmodule
310 29 unneback
module vl_spr ( sp, r, q, clk, rst);
311 64 unneback
        //parameter width = 1;
312
        parameter reset_value = 1'b0;
313 29 unneback
        input sp, r;
314
        output reg q;
315
        input clk, rst;
316
        always @ (posedge clk or posedge rst)
317
        if (rst)
318
            q <= reset_value;
319
        else
320
            if (sp)
321
                q <= 1'b1;
322
            else if (r)
323
                q <= 1'b0;
324
endmodule
325
module vl_srp ( s, rp, q, clk, rst);
326
        parameter width = 1;
327
        parameter reset_value = 0;
328
        input s, rp;
329
        output reg q;
330
        input clk, rst;
331
        always @ (posedge clk or posedge rst)
332
        if (rst)
333
            q <= reset_value;
334
        else
335
            if (rp)
336
                q <= 1'b0;
337
            else if (s)
338
                q <= 1'b1;
339
endmodule
340 6 unneback
// megafunction wizard: %LPM_FF%
341
// GENERATION: STANDARD
342
// VERSION: WM1.0
343
// MODULE: lpm_ff 
344
// ============================================================
345
// File Name: dff_sr.v
346
// Megafunction Name(s):
347
//                      lpm_ff
348
//
349
// Simulation Library Files(s):
350
//                      lpm
351
// ============================================================
352
// ************************************************************
353
// THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE!
354
//
355
// 9.1 Build 304 01/25/2010 SP 1 SJ Full Version
356
// ************************************************************
357
//Copyright (C) 1991-2010 Altera Corporation
358
//Your use of Altera Corporation's design tools, logic functions 
359
//and other software and tools, and its AMPP partner logic 
360
//functions, and any output files from any of the foregoing 
361
//(including device programming or simulation files), and any 
362
//associated documentation or information are expressly subject 
363
//to the terms and conditions of the Altera Program License 
364
//Subscription Agreement, Altera MegaCore Function License 
365
//Agreement, or other applicable license agreement, including, 
366
//without limitation, that your use is for the sole purpose of 
367
//programming logic devices manufactured by Altera and sold by 
368
//Altera or its authorized distributors.  Please refer to the 
369
//applicable agreement for further details.
370
// synopsys translate_off
371
`timescale 1 ps / 1 ps
372
// synopsys translate_on
373 18 unneback
module vl_dff_sr (
374 6 unneback
        aclr,
375
        aset,
376
        clock,
377
        data,
378
        q);
379
        input     aclr;
380
        input     aset;
381
        input     clock;
382
        input     data;
383
        output    q;
384
        wire [0:0] sub_wire0;
385
        wire [0:0] sub_wire1 = sub_wire0[0:0];
386
        wire  q = sub_wire1;
387
        wire  sub_wire2 = data;
388
        wire  sub_wire3 = sub_wire2;
389
        lpm_ff  lpm_ff_component (
390
                                .aclr (aclr),
391
                                .clock (clock),
392
                                .data (sub_wire3),
393
                                .aset (aset),
394
                                .q (sub_wire0)
395
                                // synopsys translate_off
396
                                ,
397
                                .aload (),
398
                                .enable (),
399
                                .sclr (),
400
                                .sload (),
401
                                .sset ()
402
                                // synopsys translate_on
403
                                );
404
        defparam
405
                lpm_ff_component.lpm_fftype = "DFF",
406
                lpm_ff_component.lpm_type = "LPM_FF",
407
                lpm_ff_component.lpm_width = 1;
408
endmodule
409
// ============================================================
410
// CNX file retrieval info
411
// ============================================================
412
// Retrieval info: PRIVATE: ACLR NUMERIC "1"
413
// Retrieval info: PRIVATE: ALOAD NUMERIC "0"
414
// Retrieval info: PRIVATE: ASET NUMERIC "1"
415
// Retrieval info: PRIVATE: ASET_ALL1 NUMERIC "1"
416
// Retrieval info: PRIVATE: CLK_EN NUMERIC "0"
417
// Retrieval info: PRIVATE: DFF NUMERIC "1"
418
// Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone IV E"
419
// Retrieval info: PRIVATE: SCLR NUMERIC "0"
420
// Retrieval info: PRIVATE: SLOAD NUMERIC "0"
421
// Retrieval info: PRIVATE: SSET NUMERIC "0"
422
// Retrieval info: PRIVATE: SSET_ALL1 NUMERIC "1"
423
// Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0"
424
// Retrieval info: PRIVATE: UseTFFdataPort NUMERIC "0"
425
// Retrieval info: PRIVATE: nBit NUMERIC "1"
426
// Retrieval info: CONSTANT: LPM_FFTYPE STRING "DFF"
427
// Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_FF"
428
// Retrieval info: CONSTANT: LPM_WIDTH NUMERIC "1"
429
// Retrieval info: USED_PORT: aclr 0 0 0 0 INPUT NODEFVAL aclr
430
// Retrieval info: USED_PORT: aset 0 0 0 0 INPUT NODEFVAL aset
431
// Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL clock
432
// Retrieval info: USED_PORT: data 0 0 0 0 INPUT NODEFVAL data
433
// Retrieval info: USED_PORT: q 0 0 0 0 OUTPUT NODEFVAL q
434
// Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0
435
// Retrieval info: CONNECT: q 0 0 0 0 @q 0 0 1 0
436
// Retrieval info: CONNECT: @aclr 0 0 0 0 aclr 0 0 0 0
437
// Retrieval info: CONNECT: @aset 0 0 0 0 aset 0 0 0 0
438
// Retrieval info: CONNECT: @data 0 0 1 0 data 0 0 0 0
439
// Retrieval info: LIBRARY: lpm lpm.lpm_components.all
440
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.v TRUE
441
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.inc FALSE
442
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.cmp FALSE
443
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr.bsf FALSE
444
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_inst.v FALSE
445
// Retrieval info: GEN_FILE: TYPE_NORMAL dff_sr_bb.v FALSE
446
// Retrieval info: LIB_FILE: lpm
447
// LATCH
448
// For targtes not supporting LATCH use dff_sr with clk=1 and data=1
449 18 unneback
module vl_latch ( d, le, q, clk);
450 6 unneback
input d, le;
451
output q;
452
input clk;
453
dff_sr i0 (.aclr(), .aset(), .clock(1'b1), .data(1'b1), .q(q));
454
endmodule
455 18 unneback
module vl_shreg ( d, q, clk, rst);
456 17 unneback
parameter depth = 10;
457
input d;
458
output q;
459
input clk, rst;
460
reg [1:depth] dffs;
461
always @ (posedge clk or posedge rst)
462
if (rst)
463
    dffs <= {depth{1'b0}};
464
else
465
    dffs <= {d,dffs[1:depth-1]};
466
assign q = dffs[depth];
467
endmodule
468 18 unneback
module vl_shreg_ce ( d, ce, q, clk, rst);
469 17 unneback
parameter depth = 10;
470
input d, ce;
471
output q;
472
input clk, rst;
473
reg [1:depth] dffs;
474
always @ (posedge clk or posedge rst)
475
if (rst)
476
    dffs <= {depth{1'b0}};
477
else
478
    if (ce)
479
        dffs <= {d,dffs[1:depth-1]};
480
assign q = dffs[depth];
481
endmodule
482 18 unneback
module vl_delay ( d, q, clk, rst);
483 15 unneback
parameter depth = 10;
484
input d;
485
output q;
486
input clk, rst;
487
reg [1:depth] dffs;
488
always @ (posedge clk or posedge rst)
489
if (rst)
490
    dffs <= {depth{1'b0}};
491
else
492
    dffs <= {d,dffs[1:depth-1]};
493
assign q = dffs[depth];
494
endmodule
495 18 unneback
module vl_delay_emptyflag ( d, q, emptyflag, clk, rst);
496 17 unneback
parameter depth = 10;
497
input d;
498
output q, emptyflag;
499
input clk, rst;
500
reg [1:depth] dffs;
501
always @ (posedge clk or posedge rst)
502
if (rst)
503
    dffs <= {depth{1'b0}};
504
else
505
    dffs <= {d,dffs[1:depth-1]};
506
assign q = dffs[depth];
507
assign emptyflag = !(|dffs);
508
endmodule
509 98 unneback
module vl_pulse2toggle ( pl, q, clk, rst);
510 94 unneback
input pl;
511 98 unneback
output reg q;
512 94 unneback
input clk, rst;
513
always @ (posedge clk or posedge rst)
514
if (rst)
515
    q <= 1'b0;
516
else
517
    q <= pl ^ q;
518
endmodule
519 98 unneback
module vl_toggle2pulse (d, pl, clk, rst);
520 94 unneback
input d;
521
output pl;
522
input clk, rst;
523
reg dff;
524
always @ (posedge clk or posedge rst)
525
if (rst)
526
    dff <= 1'b0;
527
else
528
    dff <= d;
529 98 unneback
assign pl = d ^ dff;
530 94 unneback
endmodule
531
module vl_synchronizer (d, q, clk, rst);
532
input d;
533
output reg q;
534 116 unneback
input clk, rst;
535 94 unneback
reg dff;
536
always @ (posedge clk or posedge rst)
537
if (rst)
538 100 unneback
    {q,dff} <= 2'b00;
539 94 unneback
else
540 100 unneback
    {q,dff} <= {dff,d};
541 94 unneback
endmodule
542 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);
543 94 unneback
input start_pl;
544
output take_it_pl;
545
input take_it_grant_pl; // note: connect to take_it_pl to generate automatic ack
546
output got_it_pl;
547
input clk_src, rst_src;
548
input clk_dst, rst_dst;
549
wire take_it_tg, take_it_tg_sync;
550
wire got_it_tg, got_it_tg_sync;
551
// src -> dst
552
vl_pulse2toggle p2t0 (
553
    .pl(start_pl),
554
    .q(take_it_tg),
555
    .clk(clk_src),
556
    .rst(rst_src));
557
vl_synchronizer sync0 (
558
    .d(take_it_tg),
559
    .q(take_it_tg_sync),
560
    .clk(clk_dst),
561
    .rst(rst_dst));
562
vl_toggle2pulse t2p0 (
563 100 unneback
    .d(take_it_tg_sync),
564 94 unneback
    .pl(take_it_pl),
565
    .clk(clk_dst),
566
    .rst(rst_dst));
567
// dst -> src
568 98 unneback
vl_pulse2toggle p2t1 (
569 94 unneback
    .pl(take_it_grant_pl),
570
    .q(got_it_tg),
571
    .clk(clk_dst),
572
    .rst(rst_dst));
573
vl_synchronizer sync1 (
574
    .d(got_it_tg),
575
    .q(got_it_tg_sync),
576
    .clk(clk_src),
577
    .rst(rst_src));
578
vl_toggle2pulse t2p1 (
579 100 unneback
    .d(got_it_tg_sync),
580 94 unneback
    .pl(got_it_pl),
581
    .clk(clk_src),
582
    .rst(rst_src));
583
endmodule
584 6 unneback
//////////////////////////////////////////////////////////////////////
585
////                                                              ////
586 18 unneback
////  Logic functions                                             ////
587
////                                                              ////
588
////  Description                                                 ////
589
////  Logic functions such as multiplexers                        ////
590
////                                                              ////
591
////                                                              ////
592
////  To Do:                                                      ////
593
////   -                                                          ////
594
////                                                              ////
595
////  Author(s):                                                  ////
596
////      - Michael Unneback, unneback@opencores.org              ////
597
////        ORSoC AB                                              ////
598
////                                                              ////
599
//////////////////////////////////////////////////////////////////////
600
////                                                              ////
601
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
602
////                                                              ////
603
//// This source file may be used and distributed without         ////
604
//// restriction provided that this copyright statement is not    ////
605
//// removed from the file and that any derivative work contains  ////
606
//// the original copyright notice and the associated disclaimer. ////
607
////                                                              ////
608
//// This source file is free software; you can redistribute it   ////
609
//// and/or modify it under the terms of the GNU Lesser General   ////
610
//// Public License as published by the Free Software Foundation; ////
611
//// either version 2.1 of the License, or (at your option) any   ////
612
//// later version.                                               ////
613
////                                                              ////
614
//// This source is distributed in the hope that it will be       ////
615
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
616
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
617
//// PURPOSE.  See the GNU Lesser General Public License for more ////
618
//// details.                                                     ////
619
////                                                              ////
620
//// You should have received a copy of the GNU Lesser General    ////
621
//// Public License along with this source; if not, download it   ////
622
//// from http://www.opencores.org/lgpl.shtml                     ////
623
////                                                              ////
624
//////////////////////////////////////////////////////////////////////
625 36 unneback
module vl_mux_andor ( a, sel, dout);
626
parameter width = 32;
627
parameter nr_of_ports = 4;
628
input [nr_of_ports*width-1:0] a;
629
input [nr_of_ports-1:0] sel;
630
output reg [width-1:0] dout;
631 38 unneback
integer i,j;
632 36 unneback
always @ (a, sel)
633
begin
634
    dout = a[width-1:0] & {width{sel[0]}};
635 42 unneback
    for (i=1;i<nr_of_ports;i=i+1)
636
        for (j=0;j<width;j=j+1)
637
            dout[j] = (a[i*width + j] & sel[i]) | dout[j];
638 36 unneback
end
639
endmodule
640 34 unneback
module vl_mux2_andor ( a1, a0, sel, dout);
641
parameter width = 32;
642 35 unneback
localparam nr_of_ports = 2;
643 34 unneback
input [width-1:0] a1, a0;
644
input [nr_of_ports-1:0] sel;
645
output [width-1:0] dout;
646 36 unneback
vl_mux_andor
647 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
648 36 unneback
    mux0( .a({a1,a0}), .sel(sel), .dout(dout));
649 34 unneback
endmodule
650
module vl_mux3_andor ( a2, a1, a0, sel, dout);
651
parameter width = 32;
652 35 unneback
localparam nr_of_ports = 3;
653 34 unneback
input [width-1:0] a2, a1, a0;
654
input [nr_of_ports-1:0] sel;
655
output [width-1:0] dout;
656 36 unneback
vl_mux_andor
657 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
658 36 unneback
    mux0( .a({a2,a1,a0}), .sel(sel), .dout(dout));
659 34 unneback
endmodule
660 18 unneback
module vl_mux4_andor ( a3, a2, a1, a0, sel, dout);
661
parameter width = 32;
662 35 unneback
localparam nr_of_ports = 4;
663 18 unneback
input [width-1:0] a3, a2, a1, a0;
664
input [nr_of_ports-1:0] sel;
665 22 unneback
output [width-1:0] dout;
666 36 unneback
vl_mux_andor
667 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
668 36 unneback
    mux0( .a({a3,a2,a1,a0}), .sel(sel), .dout(dout));
669 18 unneback
endmodule
670
module vl_mux5_andor ( a4, a3, a2, a1, a0, sel, dout);
671
parameter width = 32;
672 35 unneback
localparam nr_of_ports = 5;
673 18 unneback
input [width-1:0] a4, a3, a2, a1, a0;
674
input [nr_of_ports-1:0] sel;
675 22 unneback
output [width-1:0] dout;
676 36 unneback
vl_mux_andor
677 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
678 36 unneback
    mux0( .a({a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
679 18 unneback
endmodule
680
module vl_mux6_andor ( a5, a4, a3, a2, a1, a0, sel, dout);
681
parameter width = 32;
682 35 unneback
localparam nr_of_ports = 6;
683 18 unneback
input [width-1:0] a5, a4, a3, a2, a1, a0;
684
input [nr_of_ports-1:0] sel;
685 22 unneback
output [width-1:0] dout;
686 36 unneback
vl_mux_andor
687 38 unneback
    # ( .width(width), .nr_of_ports(nr_of_ports))
688 36 unneback
    mux0( .a({a5,a4,a3,a2,a1,a0}), .sel(sel), .dout(dout));
689 18 unneback
endmodule
690 43 unneback
module vl_parity_generate (data, parity);
691
parameter word_size = 32;
692
parameter chunk_size = 8;
693
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
694
input [word_size-1:0] data;
695
output reg [word_size/chunk_size-1:0] parity;
696
integer i,j;
697
always @ (data)
698
for (i=0;i<word_size/chunk_size;i=i+1) begin
699
    parity[i] = parity_type;
700
    for (j=0;j<chunk_size;j=j+1) begin
701 46 unneback
        parity[i] = data[i*chunk_size+j] ^ parity[i];
702 43 unneback
    end
703
end
704
endmodule
705
module vl_parity_check( data, parity, parity_error);
706
parameter word_size = 32;
707
parameter chunk_size = 8;
708
parameter parity_type = 1'b0; // 0 - even, 1 - odd parity
709
input [word_size-1:0] data;
710
input [word_size/chunk_size-1:0] parity;
711
output parity_error;
712 44 unneback
reg [word_size/chunk_size-1:0] error_flag;
713 43 unneback
integer i,j;
714
always @ (data or parity)
715
for (i=0;i<word_size/chunk_size;i=i+1) begin
716
    error_flag[i] = parity[i] ^ parity_type;
717
    for (j=0;j<chunk_size;j=j+1) begin
718 46 unneback
        error_flag[i] = data[i*chunk_size+j] ^ error_flag[i];
719 43 unneback
    end
720
end
721
assign parity_error = |error_flag;
722
endmodule
723 18 unneback
//////////////////////////////////////////////////////////////////////
724
////                                                              ////
725 44 unneback
////  IO functions                                                ////
726
////                                                              ////
727
////  Description                                                 ////
728
////  IO functions such as IOB flip-flops                         ////
729
////                                                              ////
730
////                                                              ////
731
////  To Do:                                                      ////
732
////   -                                                          ////
733
////                                                              ////
734
////  Author(s):                                                  ////
735
////      - Michael Unneback, unneback@opencores.org              ////
736
////        ORSoC AB                                              ////
737
////                                                              ////
738
//////////////////////////////////////////////////////////////////////
739
////                                                              ////
740
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
741
////                                                              ////
742
//// This source file may be used and distributed without         ////
743
//// restriction provided that this copyright statement is not    ////
744
//// removed from the file and that any derivative work contains  ////
745
//// the original copyright notice and the associated disclaimer. ////
746
////                                                              ////
747
//// This source file is free software; you can redistribute it   ////
748
//// and/or modify it under the terms of the GNU Lesser General   ////
749
//// Public License as published by the Free Software Foundation; ////
750
//// either version 2.1 of the License, or (at your option) any   ////
751
//// later version.                                               ////
752
////                                                              ////
753
//// This source is distributed in the hope that it will be       ////
754
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
755
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
756
//// PURPOSE.  See the GNU Lesser General Public License for more ////
757
//// details.                                                     ////
758
////                                                              ////
759
//// You should have received a copy of the GNU Lesser General    ////
760
//// Public License along with this source; if not, download it   ////
761
//// from http://www.opencores.org/lgpl.shtml                     ////
762
////                                                              ////
763
//////////////////////////////////////////////////////////////////////
764 45 unneback
`timescale 1ns/1ns
765 44 unneback
module vl_o_dff (d_i, o_pad, clk, rst);
766
parameter width = 1;
767 45 unneback
parameter reset_value = {width{1'b0}};
768
input  [width-1:0]  d_i;
769 44 unneback
output [width-1:0] o_pad;
770
input clk, rst;
771 60 unneback
wire [width-1:0] d_i_int /*synthesis syn_keep = 1*/;
772 45 unneback
reg  [width-1:0] o_pad_int;
773 44 unneback
assign d_i_int = d_i;
774
genvar i;
775 45 unneback
generate
776 136 unneback
for (i=0;i<width;i=i+1) begin : dffs
777 44 unneback
    always @ (posedge clk or posedge rst)
778
    if (rst)
779 45 unneback
        o_pad_int[i] <= reset_value[i];
780 44 unneback
    else
781 45 unneback
        o_pad_int[i] <= d_i_int[i];
782
    assign #1 o_pad[i] = o_pad_int[i];
783 44 unneback
end
784
endgenerate
785
endmodule
786 45 unneback
`timescale 1ns/1ns
787 44 unneback
module vl_io_dff_oe ( d_i, d_o, oe, io_pad, clk, rst);
788
parameter width = 1;
789
input  [width-1:0] d_o;
790
output reg [width-1:0] d_i;
791
input oe;
792
inout [width-1:0] io_pad;
793
input clk, rst;
794 60 unneback
wire [width-1:0] oe_d /*synthesis syn_keep = 1*/;
795 44 unneback
reg [width-1:0] oe_q;
796
reg [width-1:0] d_o_q;
797
assign oe_d = {width{oe}};
798
genvar i;
799
generate
800 136 unneback
for (i=0;i<width;i=i+1) begin : dffs
801 44 unneback
    always @ (posedge clk or posedge rst)
802
    if (rst)
803
        oe_q[i] <= 1'b0;
804
    else
805
        oe_q[i] <= oe_d[i];
806
    always @ (posedge clk or posedge rst)
807
    if (rst)
808
        d_o_q[i] <= 1'b0;
809
    else
810
        d_o_q[i] <= d_o[i];
811
    always @ (posedge clk or posedge rst)
812
    if (rst)
813
        d_i[i] <= 1'b0;
814
    else
815
        d_i[i] <= io_pad[i];
816 45 unneback
    assign #1 io_pad[i] = (oe_q[i]) ? d_o_q[i] : 1'bz;
817 44 unneback
end
818
endgenerate
819
endmodule
820 136 unneback
module vl_o_ddr (d_h_i, d_l_i, o_pad, clk, rst);
821
parameter width = 1;
822
input  [width-1:0] d_h_i, d_l_i;
823
output [width-1:0] o_pad;
824
input clk, rst;
825
genvar i;
826
generate
827
for (i=0;i<width;i=i+1) begin : ddr
828
    ddio_out ddio_out0( .aclr(rst), .datain_h(d_h_i[i]), .datain_l(d_l_i[i]), .outclock(clk), .dataout(o_pad[i]) );
829
end
830
endgenerate
831
endmodule
832
module vl_o_clk ( clk_o_pad, clk, rst);
833
input clk, rst;
834
output clk_o_pad;
835
vl_o_ddr o_ddr0( .d_h_i(1'b1), .d_l_i(1'b0), .o_pad(clk_o_pad), .clk(clk), .rst(rst));
836
endmodule
837 44 unneback
//////////////////////////////////////////////////////////////////////
838
////                                                              ////
839 6 unneback
////  Versatile counter                                           ////
840
////                                                              ////
841
////  Description                                                 ////
842
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
843
////  counter                                                     ////
844
////                                                              ////
845
////  To Do:                                                      ////
846
////   - add LFSR with more taps                                  ////
847
////                                                              ////
848
////  Author(s):                                                  ////
849
////      - Michael Unneback, unneback@opencores.org              ////
850
////        ORSoC AB                                              ////
851
////                                                              ////
852
//////////////////////////////////////////////////////////////////////
853
////                                                              ////
854
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
855
////                                                              ////
856
//// This source file may be used and distributed without         ////
857
//// restriction provided that this copyright statement is not    ////
858
//// removed from the file and that any derivative work contains  ////
859
//// the original copyright notice and the associated disclaimer. ////
860
////                                                              ////
861
//// This source file is free software; you can redistribute it   ////
862
//// and/or modify it under the terms of the GNU Lesser General   ////
863
//// Public License as published by the Free Software Foundation; ////
864
//// either version 2.1 of the License, or (at your option) any   ////
865
//// later version.                                               ////
866
////                                                              ////
867
//// This source is distributed in the hope that it will be       ////
868
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
869
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
870
//// PURPOSE.  See the GNU Lesser General Public License for more ////
871
//// details.                                                     ////
872
////                                                              ////
873
//// You should have received a copy of the GNU Lesser General    ////
874
//// Public License along with this source; if not, download it   ////
875
//// from http://www.opencores.org/lgpl.shtml                     ////
876
////                                                              ////
877
//////////////////////////////////////////////////////////////////////
878
// binary counter
879 139 unneback
module vl_cnt_bin (
880
 q, rst, clk);
881
   parameter length = 4;
882
   output [length:1] q;
883
   input rst;
884
   input clk;
885
   parameter clear_value = 0;
886
   parameter set_value = 1;
887
   parameter wrap_value = 0;
888
   parameter level1_value = 15;
889
   reg  [length:1] qi;
890
   wire [length:1] q_next;
891
   assign q_next = qi + {{length-1{1'b0}},1'b1};
892
   always @ (posedge clk or posedge rst)
893
     if (rst)
894
       qi <= {length{1'b0}};
895
     else
896
       qi <= q_next;
897
   assign q = qi;
898
endmodule
899
//////////////////////////////////////////////////////////////////////
900
////                                                              ////
901
////  Versatile counter                                           ////
902
////                                                              ////
903
////  Description                                                 ////
904
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
905
////  counter                                                     ////
906
////                                                              ////
907
////  To Do:                                                      ////
908
////   - add LFSR with more taps                                  ////
909
////                                                              ////
910
////  Author(s):                                                  ////
911
////      - Michael Unneback, unneback@opencores.org              ////
912
////        ORSoC AB                                              ////
913
////                                                              ////
914
//////////////////////////////////////////////////////////////////////
915
////                                                              ////
916
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
917
////                                                              ////
918
//// This source file may be used and distributed without         ////
919
//// restriction provided that this copyright statement is not    ////
920
//// removed from the file and that any derivative work contains  ////
921
//// the original copyright notice and the associated disclaimer. ////
922
////                                                              ////
923
//// This source file is free software; you can redistribute it   ////
924
//// and/or modify it under the terms of the GNU Lesser General   ////
925
//// Public License as published by the Free Software Foundation; ////
926
//// either version 2.1 of the License, or (at your option) any   ////
927
//// later version.                                               ////
928
////                                                              ////
929
//// This source is distributed in the hope that it will be       ////
930
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
931
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
932
//// PURPOSE.  See the GNU Lesser General Public License for more ////
933
//// details.                                                     ////
934
////                                                              ////
935
//// You should have received a copy of the GNU Lesser General    ////
936
//// Public License along with this source; if not, download it   ////
937
//// from http://www.opencores.org/lgpl.shtml                     ////
938
////                                                              ////
939
//////////////////////////////////////////////////////////////////////
940
// binary counter
941
module vl_cnt_bin_clear (
942
 clear, q, rst, clk);
943
   parameter length = 4;
944
   input clear;
945
   output [length:1] q;
946
   input rst;
947
   input clk;
948
   parameter clear_value = 0;
949
   parameter set_value = 1;
950
   parameter wrap_value = 0;
951
   parameter level1_value = 15;
952
   reg  [length:1] qi;
953
   wire [length:1] q_next;
954
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
955
   always @ (posedge clk or posedge rst)
956
     if (rst)
957
       qi <= {length{1'b0}};
958
     else
959
       qi <= q_next;
960
   assign q = qi;
961
endmodule
962
//////////////////////////////////////////////////////////////////////
963
////                                                              ////
964
////  Versatile counter                                           ////
965
////                                                              ////
966
////  Description                                                 ////
967
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
968
////  counter                                                     ////
969
////                                                              ////
970
////  To Do:                                                      ////
971
////   - add LFSR with more taps                                  ////
972
////                                                              ////
973
////  Author(s):                                                  ////
974
////      - Michael Unneback, unneback@opencores.org              ////
975
////        ORSoC AB                                              ////
976
////                                                              ////
977
//////////////////////////////////////////////////////////////////////
978
////                                                              ////
979
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
980
////                                                              ////
981
//// This source file may be used and distributed without         ////
982
//// restriction provided that this copyright statement is not    ////
983
//// removed from the file and that any derivative work contains  ////
984
//// the original copyright notice and the associated disclaimer. ////
985
////                                                              ////
986
//// This source file is free software; you can redistribute it   ////
987
//// and/or modify it under the terms of the GNU Lesser General   ////
988
//// Public License as published by the Free Software Foundation; ////
989
//// either version 2.1 of the License, or (at your option) any   ////
990
//// later version.                                               ////
991
////                                                              ////
992
//// This source is distributed in the hope that it will be       ////
993
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
994
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
995
//// PURPOSE.  See the GNU Lesser General Public License for more ////
996
//// details.                                                     ////
997
////                                                              ////
998
//// You should have received a copy of the GNU Lesser General    ////
999
//// Public License along with this source; if not, download it   ////
1000
//// from http://www.opencores.org/lgpl.shtml                     ////
1001
////                                                              ////
1002
//////////////////////////////////////////////////////////////////////
1003
// binary counter
1004 40 unneback
module vl_cnt_bin_ce (
1005
 cke, q, rst, clk);
1006 22 unneback
   parameter length = 4;
1007 6 unneback
   input cke;
1008
   output [length:1] q;
1009
   input rst;
1010
   input clk;
1011
   parameter clear_value = 0;
1012
   parameter set_value = 1;
1013
   parameter wrap_value = 0;
1014
   parameter level1_value = 15;
1015
   reg  [length:1] qi;
1016
   wire [length:1] q_next;
1017
   assign q_next = qi + {{length-1{1'b0}},1'b1};
1018
   always @ (posedge clk or posedge rst)
1019
     if (rst)
1020
       qi <= {length{1'b0}};
1021
     else
1022
     if (cke)
1023
       qi <= q_next;
1024
   assign q = qi;
1025
endmodule
1026
//////////////////////////////////////////////////////////////////////
1027
////                                                              ////
1028
////  Versatile counter                                           ////
1029
////                                                              ////
1030
////  Description                                                 ////
1031
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1032
////  counter                                                     ////
1033
////                                                              ////
1034
////  To Do:                                                      ////
1035
////   - add LFSR with more taps                                  ////
1036
////                                                              ////
1037
////  Author(s):                                                  ////
1038
////      - Michael Unneback, unneback@opencores.org              ////
1039
////        ORSoC AB                                              ////
1040
////                                                              ////
1041
//////////////////////////////////////////////////////////////////////
1042
////                                                              ////
1043
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1044
////                                                              ////
1045
//// This source file may be used and distributed without         ////
1046
//// restriction provided that this copyright statement is not    ////
1047
//// removed from the file and that any derivative work contains  ////
1048
//// the original copyright notice and the associated disclaimer. ////
1049
////                                                              ////
1050
//// This source file is free software; you can redistribute it   ////
1051
//// and/or modify it under the terms of the GNU Lesser General   ////
1052
//// Public License as published by the Free Software Foundation; ////
1053
//// either version 2.1 of the License, or (at your option) any   ////
1054
//// later version.                                               ////
1055
////                                                              ////
1056
//// This source is distributed in the hope that it will be       ////
1057
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1058
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1059
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1060
//// details.                                                     ////
1061
////                                                              ////
1062
//// You should have received a copy of the GNU Lesser General    ////
1063
//// Public License along with this source; if not, download it   ////
1064
//// from http://www.opencores.org/lgpl.shtml                     ////
1065
////                                                              ////
1066
//////////////////////////////////////////////////////////////////////
1067
// binary counter
1068 139 unneback
module vl_cnt_bin_ce_clear (
1069
 clear, cke, q, rst, clk);
1070
   parameter length = 4;
1071
   input clear;
1072
   input cke;
1073
   output [length:1] q;
1074
   input rst;
1075
   input clk;
1076
   parameter clear_value = 0;
1077
   parameter set_value = 1;
1078
   parameter wrap_value = 0;
1079
   parameter level1_value = 15;
1080
   reg  [length:1] qi;
1081
   wire [length:1] q_next;
1082
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1083
   always @ (posedge clk or posedge rst)
1084
     if (rst)
1085
       qi <= {length{1'b0}};
1086
     else
1087
     if (cke)
1088
       qi <= q_next;
1089
   assign q = qi;
1090
endmodule
1091
//////////////////////////////////////////////////////////////////////
1092
////                                                              ////
1093
////  Versatile counter                                           ////
1094
////                                                              ////
1095
////  Description                                                 ////
1096
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1097
////  counter                                                     ////
1098
////                                                              ////
1099
////  To Do:                                                      ////
1100
////   - add LFSR with more taps                                  ////
1101
////                                                              ////
1102
////  Author(s):                                                  ////
1103
////      - Michael Unneback, unneback@opencores.org              ////
1104
////        ORSoC AB                                              ////
1105
////                                                              ////
1106
//////////////////////////////////////////////////////////////////////
1107
////                                                              ////
1108
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1109
////                                                              ////
1110
//// This source file may be used and distributed without         ////
1111
//// restriction provided that this copyright statement is not    ////
1112
//// removed from the file and that any derivative work contains  ////
1113
//// the original copyright notice and the associated disclaimer. ////
1114
////                                                              ////
1115
//// This source file is free software; you can redistribute it   ////
1116
//// and/or modify it under the terms of the GNU Lesser General   ////
1117
//// Public License as published by the Free Software Foundation; ////
1118
//// either version 2.1 of the License, or (at your option) any   ////
1119
//// later version.                                               ////
1120
////                                                              ////
1121
//// This source is distributed in the hope that it will be       ////
1122
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1123
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1124
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1125
//// details.                                                     ////
1126
////                                                              ////
1127
//// You should have received a copy of the GNU Lesser General    ////
1128
//// Public License along with this source; if not, download it   ////
1129
//// from http://www.opencores.org/lgpl.shtml                     ////
1130
////                                                              ////
1131
//////////////////////////////////////////////////////////////////////
1132
// binary counter
1133
module vl_cnt_bin_ce_clear_l1_l2 (
1134
 clear, cke, q, level1, level2, rst, clk);
1135
   parameter length = 4;
1136
   input clear;
1137
   input cke;
1138
   output [length:1] q;
1139
   output reg level1;
1140
   output reg level2;
1141
   input rst;
1142
   input clk;
1143
   parameter clear_value = 0;
1144
   parameter set_value = 1;
1145
   parameter wrap_value = 15;
1146
   parameter level1_value = 8;
1147
   parameter level2_value = 15;
1148
   wire rew;
1149
   assign rew = 1'b0;
1150
   reg  [length:1] qi;
1151
   wire [length:1] q_next;
1152
   assign q_next =  clear ? {length{1'b0}} :qi + {{length-1{1'b0}},1'b1};
1153
   always @ (posedge clk or posedge rst)
1154
     if (rst)
1155
       qi <= {length{1'b0}};
1156
     else
1157
     if (cke)
1158
       qi <= q_next;
1159
   assign q = qi;
1160
    always @ (posedge clk or posedge rst)
1161
    if (rst)
1162
        level1 <= 1'b0;
1163
    else
1164
    if (cke)
1165
    if (clear)
1166
        level1 <= 1'b0;
1167
    else if (q_next == level1_value)
1168
        level1 <= 1'b1;
1169
    else if (qi == level1_value & rew)
1170
        level1 <= 1'b0;
1171
    always @ (posedge clk or posedge rst)
1172
    if (rst)
1173
        level2 <= 1'b0;
1174
    else
1175
    if (cke)
1176
    if (clear)
1177
        level2 <= 1'b0;
1178
    else if (q_next == level2_value)
1179
        level2 <= 1'b1;
1180
    else if (qi == level2_value & rew)
1181
        level2 <= 1'b0;
1182
endmodule
1183
//////////////////////////////////////////////////////////////////////
1184
////                                                              ////
1185
////  Versatile counter                                           ////
1186
////                                                              ////
1187
////  Description                                                 ////
1188
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1189
////  counter                                                     ////
1190
////                                                              ////
1191
////  To Do:                                                      ////
1192
////   - add LFSR with more taps                                  ////
1193
////                                                              ////
1194
////  Author(s):                                                  ////
1195
////      - Michael Unneback, unneback@opencores.org              ////
1196
////        ORSoC AB                                              ////
1197
////                                                              ////
1198
//////////////////////////////////////////////////////////////////////
1199
////                                                              ////
1200
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1201
////                                                              ////
1202
//// This source file may be used and distributed without         ////
1203
//// restriction provided that this copyright statement is not    ////
1204
//// removed from the file and that any derivative work contains  ////
1205
//// the original copyright notice and the associated disclaimer. ////
1206
////                                                              ////
1207
//// This source file is free software; you can redistribute it   ////
1208
//// and/or modify it under the terms of the GNU Lesser General   ////
1209
//// Public License as published by the Free Software Foundation; ////
1210
//// either version 2.1 of the License, or (at your option) any   ////
1211
//// later version.                                               ////
1212
////                                                              ////
1213
//// This source is distributed in the hope that it will be       ////
1214
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1215
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1216
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1217
//// details.                                                     ////
1218
////                                                              ////
1219
//// You should have received a copy of the GNU Lesser General    ////
1220
//// Public License along with this source; if not, download it   ////
1221
//// from http://www.opencores.org/lgpl.shtml                     ////
1222
////                                                              ////
1223
//////////////////////////////////////////////////////////////////////
1224
// binary counter
1225
module vl_cnt_bin_ce_clear_set_rew (
1226
 clear, set, cke, rew, q, rst, clk);
1227
   parameter length = 4;
1228
   input clear;
1229
   input set;
1230
   input cke;
1231
   input rew;
1232
   output [length:1] q;
1233
   input rst;
1234
   input clk;
1235
   parameter clear_value = 0;
1236
   parameter set_value = 1;
1237
   parameter wrap_value = 0;
1238
   parameter level1_value = 15;
1239
   reg  [length:1] qi;
1240
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1241
   assign q_next_fw  =  clear ? {length{1'b0}} : set ? set_value :qi + {{length-1{1'b0}},1'b1};
1242
   assign q_next_rew =  clear ? clear_value : set ? set_value :qi - {{length-1{1'b0}},1'b1};
1243
   assign q_next = rew ? q_next_rew : q_next_fw;
1244
   always @ (posedge clk or posedge rst)
1245
     if (rst)
1246
       qi <= {length{1'b0}};
1247
     else
1248
     if (cke)
1249
       qi <= q_next;
1250
   assign q = qi;
1251
endmodule
1252
//////////////////////////////////////////////////////////////////////
1253
////                                                              ////
1254
////  Versatile counter                                           ////
1255
////                                                              ////
1256
////  Description                                                 ////
1257
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1258
////  counter                                                     ////
1259
////                                                              ////
1260
////  To Do:                                                      ////
1261
////   - add LFSR with more taps                                  ////
1262
////                                                              ////
1263
////  Author(s):                                                  ////
1264
////      - Michael Unneback, unneback@opencores.org              ////
1265
////        ORSoC AB                                              ////
1266
////                                                              ////
1267
//////////////////////////////////////////////////////////////////////
1268
////                                                              ////
1269
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1270
////                                                              ////
1271
//// This source file may be used and distributed without         ////
1272
//// restriction provided that this copyright statement is not    ////
1273
//// removed from the file and that any derivative work contains  ////
1274
//// the original copyright notice and the associated disclaimer. ////
1275
////                                                              ////
1276
//// This source file is free software; you can redistribute it   ////
1277
//// and/or modify it under the terms of the GNU Lesser General   ////
1278
//// Public License as published by the Free Software Foundation; ////
1279
//// either version 2.1 of the License, or (at your option) any   ////
1280
//// later version.                                               ////
1281
////                                                              ////
1282
//// This source is distributed in the hope that it will be       ////
1283
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1284
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1285
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1286
//// details.                                                     ////
1287
////                                                              ////
1288
//// You should have received a copy of the GNU Lesser General    ////
1289
//// Public License along with this source; if not, download it   ////
1290
//// from http://www.opencores.org/lgpl.shtml                     ////
1291
////                                                              ////
1292
//////////////////////////////////////////////////////////////////////
1293
// binary counter
1294
module vl_cnt_bin_ce_rew_l1 (
1295
 cke, rew, level1, rst, clk);
1296
   parameter length = 4;
1297
   input cke;
1298
   input rew;
1299
   output reg level1;
1300
   input rst;
1301
   input clk;
1302
   parameter clear_value = 0;
1303
   parameter set_value = 1;
1304
   parameter wrap_value = 1;
1305
   parameter level1_value = 15;
1306
   wire clear;
1307
   assign clear = 1'b0;
1308
   reg  [length:1] qi;
1309
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1310
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1311
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1312
   assign q_next = rew ? q_next_rew : q_next_fw;
1313
   always @ (posedge clk or posedge rst)
1314
     if (rst)
1315
       qi <= {length{1'b0}};
1316
     else
1317
     if (cke)
1318
       qi <= q_next;
1319
    always @ (posedge clk or posedge rst)
1320
    if (rst)
1321
        level1 <= 1'b0;
1322
    else
1323
    if (cke)
1324
    if (clear)
1325
        level1 <= 1'b0;
1326
    else if (q_next == level1_value)
1327
        level1 <= 1'b1;
1328
    else if (qi == level1_value & rew)
1329
        level1 <= 1'b0;
1330
endmodule
1331
//////////////////////////////////////////////////////////////////////
1332
////                                                              ////
1333
////  Versatile counter                                           ////
1334
////                                                              ////
1335
////  Description                                                 ////
1336
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1337
////  counter                                                     ////
1338
////                                                              ////
1339
////  To Do:                                                      ////
1340
////   - add LFSR with more taps                                  ////
1341
////                                                              ////
1342
////  Author(s):                                                  ////
1343
////      - Michael Unneback, unneback@opencores.org              ////
1344
////        ORSoC AB                                              ////
1345
////                                                              ////
1346
//////////////////////////////////////////////////////////////////////
1347
////                                                              ////
1348
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1349
////                                                              ////
1350
//// This source file may be used and distributed without         ////
1351
//// restriction provided that this copyright statement is not    ////
1352
//// removed from the file and that any derivative work contains  ////
1353
//// the original copyright notice and the associated disclaimer. ////
1354
////                                                              ////
1355
//// This source file is free software; you can redistribute it   ////
1356
//// and/or modify it under the terms of the GNU Lesser General   ////
1357
//// Public License as published by the Free Software Foundation; ////
1358
//// either version 2.1 of the License, or (at your option) any   ////
1359
//// later version.                                               ////
1360
////                                                              ////
1361
//// This source is distributed in the hope that it will be       ////
1362
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1363
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1364
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1365
//// details.                                                     ////
1366
////                                                              ////
1367
//// You should have received a copy of the GNU Lesser General    ////
1368
//// Public License along with this source; if not, download it   ////
1369
//// from http://www.opencores.org/lgpl.shtml                     ////
1370
////                                                              ////
1371
//////////////////////////////////////////////////////////////////////
1372
// binary counter
1373 40 unneback
module vl_cnt_bin_ce_rew_zq_l1 (
1374
 cke, rew, zq, level1, rst, clk);
1375 6 unneback
   parameter length = 4;
1376
   input cke;
1377
   input rew;
1378 25 unneback
   output reg zq;
1379
   output reg level1;
1380
   input rst;
1381
   input clk;
1382
   parameter clear_value = 0;
1383
   parameter set_value = 1;
1384
   parameter wrap_value = 1;
1385
   parameter level1_value = 15;
1386 29 unneback
   wire clear;
1387 30 unneback
   assign clear = 1'b0;
1388 25 unneback
   reg  [length:1] qi;
1389
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1390
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1391
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1392
   assign q_next = rew ? q_next_rew : q_next_fw;
1393
   always @ (posedge clk or posedge rst)
1394
     if (rst)
1395
       qi <= {length{1'b0}};
1396
     else
1397
     if (cke)
1398
       qi <= q_next;
1399
   always @ (posedge clk or posedge rst)
1400
     if (rst)
1401
       zq <= 1'b1;
1402
     else
1403
     if (cke)
1404
       zq <= q_next == {length{1'b0}};
1405
    always @ (posedge clk or posedge rst)
1406
    if (rst)
1407
        level1 <= 1'b0;
1408
    else
1409
    if (cke)
1410 29 unneback
    if (clear)
1411
        level1 <= 1'b0;
1412
    else if (q_next == level1_value)
1413 25 unneback
        level1 <= 1'b1;
1414
    else if (qi == level1_value & rew)
1415
        level1 <= 1'b0;
1416
endmodule
1417
//////////////////////////////////////////////////////////////////////
1418
////                                                              ////
1419
////  Versatile counter                                           ////
1420
////                                                              ////
1421
////  Description                                                 ////
1422
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1423
////  counter                                                     ////
1424
////                                                              ////
1425
////  To Do:                                                      ////
1426
////   - add LFSR with more taps                                  ////
1427
////                                                              ////
1428
////  Author(s):                                                  ////
1429
////      - Michael Unneback, unneback@opencores.org              ////
1430
////        ORSoC AB                                              ////
1431
////                                                              ////
1432
//////////////////////////////////////////////////////////////////////
1433
////                                                              ////
1434
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1435
////                                                              ////
1436
//// This source file may be used and distributed without         ////
1437
//// restriction provided that this copyright statement is not    ////
1438
//// removed from the file and that any derivative work contains  ////
1439
//// the original copyright notice and the associated disclaimer. ////
1440
////                                                              ////
1441
//// This source file is free software; you can redistribute it   ////
1442
//// and/or modify it under the terms of the GNU Lesser General   ////
1443
//// Public License as published by the Free Software Foundation; ////
1444
//// either version 2.1 of the License, or (at your option) any   ////
1445
//// later version.                                               ////
1446
////                                                              ////
1447
//// This source is distributed in the hope that it will be       ////
1448
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1449
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1450
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1451
//// details.                                                     ////
1452
////                                                              ////
1453
//// You should have received a copy of the GNU Lesser General    ////
1454
//// Public License along with this source; if not, download it   ////
1455
//// from http://www.opencores.org/lgpl.shtml                     ////
1456
////                                                              ////
1457
//////////////////////////////////////////////////////////////////////
1458
// binary counter
1459 40 unneback
module vl_cnt_bin_ce_rew_q_zq_l1 (
1460
 cke, rew, q, zq, level1, rst, clk);
1461 25 unneback
   parameter length = 4;
1462
   input cke;
1463
   input rew;
1464
   output [length:1] q;
1465
   output reg zq;
1466
   output reg level1;
1467
   input rst;
1468
   input clk;
1469
   parameter clear_value = 0;
1470
   parameter set_value = 1;
1471
   parameter wrap_value = 1;
1472
   parameter level1_value = 15;
1473 29 unneback
   wire clear;
1474 30 unneback
   assign clear = 1'b0;
1475 25 unneback
   reg  [length:1] qi;
1476
   wire  [length:1] q_next, q_next_fw, q_next_rew;
1477
   assign q_next_fw  = qi + {{length-1{1'b0}},1'b1};
1478
   assign q_next_rew = qi - {{length-1{1'b0}},1'b1};
1479
   assign q_next = rew ? q_next_rew : q_next_fw;
1480
   always @ (posedge clk or posedge rst)
1481
     if (rst)
1482
       qi <= {length{1'b0}};
1483
     else
1484
     if (cke)
1485
       qi <= q_next;
1486
   assign q = qi;
1487
   always @ (posedge clk or posedge rst)
1488
     if (rst)
1489
       zq <= 1'b1;
1490
     else
1491
     if (cke)
1492
       zq <= q_next == {length{1'b0}};
1493
    always @ (posedge clk or posedge rst)
1494
    if (rst)
1495
        level1 <= 1'b0;
1496
    else
1497
    if (cke)
1498 29 unneback
    if (clear)
1499
        level1 <= 1'b0;
1500
    else if (q_next == level1_value)
1501 25 unneback
        level1 <= 1'b1;
1502
    else if (qi == level1_value & rew)
1503
        level1 <= 1'b0;
1504
endmodule
1505
//////////////////////////////////////////////////////////////////////
1506
////                                                              ////
1507
////  Versatile counter                                           ////
1508
////                                                              ////
1509
////  Description                                                 ////
1510
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1511
////  counter                                                     ////
1512
////                                                              ////
1513
////  To Do:                                                      ////
1514
////   - add LFSR with more taps                                  ////
1515
////                                                              ////
1516
////  Author(s):                                                  ////
1517
////      - Michael Unneback, unneback@opencores.org              ////
1518
////        ORSoC AB                                              ////
1519
////                                                              ////
1520
//////////////////////////////////////////////////////////////////////
1521
////                                                              ////
1522
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1523
////                                                              ////
1524
//// This source file may be used and distributed without         ////
1525
//// restriction provided that this copyright statement is not    ////
1526
//// removed from the file and that any derivative work contains  ////
1527
//// the original copyright notice and the associated disclaimer. ////
1528
////                                                              ////
1529
//// This source file is free software; you can redistribute it   ////
1530
//// and/or modify it under the terms of the GNU Lesser General   ////
1531
//// Public License as published by the Free Software Foundation; ////
1532
//// either version 2.1 of the License, or (at your option) any   ////
1533
//// later version.                                               ////
1534
////                                                              ////
1535
//// This source is distributed in the hope that it will be       ////
1536
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1537
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1538
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1539
//// details.                                                     ////
1540
////                                                              ////
1541
//// You should have received a copy of the GNU Lesser General    ////
1542
//// Public License along with this source; if not, download it   ////
1543
//// from http://www.opencores.org/lgpl.shtml                     ////
1544
////                                                              ////
1545
//////////////////////////////////////////////////////////////////////
1546 75 unneback
// LFSR counter
1547 136 unneback
module vl_cnt_lfsr_zq (
1548
 zq, rst, clk);
1549
   parameter length = 4;
1550
   output reg zq;
1551
   input rst;
1552
   input clk;
1553
   parameter clear_value = 0;
1554
   parameter set_value = 1;
1555
   parameter wrap_value = 8;
1556
   parameter level1_value = 15;
1557
   reg  [length:1] qi;
1558
   reg lfsr_fb;
1559
   wire [length:1] q_next;
1560
   reg [32:1] polynom;
1561
   integer i;
1562
   always @ (qi)
1563
   begin
1564
        case (length)
1565
         2: polynom = 32'b11;                               // 0x3
1566
         3: polynom = 32'b110;                              // 0x6
1567
         4: polynom = 32'b1100;                             // 0xC
1568
         5: polynom = 32'b10100;                            // 0x14
1569
         6: polynom = 32'b110000;                           // 0x30
1570
         7: polynom = 32'b1100000;                          // 0x60
1571
         8: polynom = 32'b10111000;                         // 0xb8
1572
         9: polynom = 32'b100010000;                        // 0x110
1573
        10: polynom = 32'b1001000000;                       // 0x240
1574
        11: polynom = 32'b10100000000;                      // 0x500
1575
        12: polynom = 32'b100000101001;                     // 0x829
1576
        13: polynom = 32'b1000000001100;                    // 0x100C
1577
        14: polynom = 32'b10000000010101;                   // 0x2015
1578
        15: polynom = 32'b110000000000000;                  // 0x6000
1579
        16: polynom = 32'b1101000000001000;                 // 0xD008
1580
        17: polynom = 32'b10010000000000000;                // 0x12000
1581
        18: polynom = 32'b100000010000000000;               // 0x20400
1582
        19: polynom = 32'b1000000000000100011;              // 0x40023
1583
        20: polynom = 32'b10010000000000000000;             // 0x90000
1584
        21: polynom = 32'b101000000000000000000;            // 0x140000
1585
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1586
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1587
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1588
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1589
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1590
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1591
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1592
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1593
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1594
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1595
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1596
        default: polynom = 32'b0;
1597
        endcase
1598
        lfsr_fb = qi[length];
1599
        for (i=length-1; i>=1; i=i-1) begin
1600
            if (polynom[i])
1601
                lfsr_fb = lfsr_fb  ~^ qi[i];
1602
        end
1603
    end
1604
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1605
   always @ (posedge clk or posedge rst)
1606
     if (rst)
1607
       qi <= {length{1'b0}};
1608
     else
1609
       qi <= q_next;
1610
   always @ (posedge clk or posedge rst)
1611
     if (rst)
1612
       zq <= 1'b1;
1613
     else
1614
       zq <= q_next == {length{1'b0}};
1615
endmodule
1616
//////////////////////////////////////////////////////////////////////
1617
////                                                              ////
1618
////  Versatile counter                                           ////
1619
////                                                              ////
1620
////  Description                                                 ////
1621
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1622
////  counter                                                     ////
1623
////                                                              ////
1624
////  To Do:                                                      ////
1625
////   - add LFSR with more taps                                  ////
1626
////                                                              ////
1627
////  Author(s):                                                  ////
1628
////      - Michael Unneback, unneback@opencores.org              ////
1629
////        ORSoC AB                                              ////
1630
////                                                              ////
1631
//////////////////////////////////////////////////////////////////////
1632
////                                                              ////
1633
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1634
////                                                              ////
1635
//// This source file may be used and distributed without         ////
1636
//// restriction provided that this copyright statement is not    ////
1637
//// removed from the file and that any derivative work contains  ////
1638
//// the original copyright notice and the associated disclaimer. ////
1639
////                                                              ////
1640
//// This source file is free software; you can redistribute it   ////
1641
//// and/or modify it under the terms of the GNU Lesser General   ////
1642
//// Public License as published by the Free Software Foundation; ////
1643
//// either version 2.1 of the License, or (at your option) any   ////
1644
//// later version.                                               ////
1645
////                                                              ////
1646
//// This source is distributed in the hope that it will be       ////
1647
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1648
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1649
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1650
//// details.                                                     ////
1651
////                                                              ////
1652
//// You should have received a copy of the GNU Lesser General    ////
1653
//// Public License along with this source; if not, download it   ////
1654
//// from http://www.opencores.org/lgpl.shtml                     ////
1655
////                                                              ////
1656
//////////////////////////////////////////////////////////////////////
1657
// LFSR counter
1658 75 unneback
module vl_cnt_lfsr_ce (
1659
 cke, zq, rst, clk);
1660
   parameter length = 4;
1661
   input cke;
1662
   output reg zq;
1663
   input rst;
1664
   input clk;
1665
   parameter clear_value = 0;
1666
   parameter set_value = 1;
1667
   parameter wrap_value = 0;
1668
   parameter level1_value = 15;
1669
   reg  [length:1] qi;
1670
   reg lfsr_fb;
1671
   wire [length:1] q_next;
1672
   reg [32:1] polynom;
1673
   integer i;
1674
   always @ (qi)
1675
   begin
1676
        case (length)
1677
         2: polynom = 32'b11;                               // 0x3
1678
         3: polynom = 32'b110;                              // 0x6
1679
         4: polynom = 32'b1100;                             // 0xC
1680
         5: polynom = 32'b10100;                            // 0x14
1681
         6: polynom = 32'b110000;                           // 0x30
1682
         7: polynom = 32'b1100000;                          // 0x60
1683
         8: polynom = 32'b10111000;                         // 0xb8
1684
         9: polynom = 32'b100010000;                        // 0x110
1685
        10: polynom = 32'b1001000000;                       // 0x240
1686
        11: polynom = 32'b10100000000;                      // 0x500
1687
        12: polynom = 32'b100000101001;                     // 0x829
1688
        13: polynom = 32'b1000000001100;                    // 0x100C
1689
        14: polynom = 32'b10000000010101;                   // 0x2015
1690
        15: polynom = 32'b110000000000000;                  // 0x6000
1691
        16: polynom = 32'b1101000000001000;                 // 0xD008
1692
        17: polynom = 32'b10010000000000000;                // 0x12000
1693
        18: polynom = 32'b100000010000000000;               // 0x20400
1694
        19: polynom = 32'b1000000000000100011;              // 0x40023
1695
        20: polynom = 32'b10010000000000000000;             // 0x90000
1696
        21: polynom = 32'b101000000000000000000;            // 0x140000
1697
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1698
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1699
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1700
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1701
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1702
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1703
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1704
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1705
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1706
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1707
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1708
        default: polynom = 32'b0;
1709
        endcase
1710
        lfsr_fb = qi[length];
1711
        for (i=length-1; i>=1; i=i-1) begin
1712
            if (polynom[i])
1713
                lfsr_fb = lfsr_fb  ~^ qi[i];
1714
        end
1715
    end
1716
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1717
   always @ (posedge clk or posedge rst)
1718
     if (rst)
1719
       qi <= {length{1'b0}};
1720
     else
1721
     if (cke)
1722
       qi <= q_next;
1723
   always @ (posedge clk or posedge rst)
1724
     if (rst)
1725
       zq <= 1'b1;
1726
     else
1727
     if (cke)
1728
       zq <= q_next == {length{1'b0}};
1729
endmodule
1730
//////////////////////////////////////////////////////////////////////
1731
////                                                              ////
1732
////  Versatile counter                                           ////
1733
////                                                              ////
1734
////  Description                                                 ////
1735
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1736
////  counter                                                     ////
1737
////                                                              ////
1738
////  To Do:                                                      ////
1739
////   - add LFSR with more taps                                  ////
1740
////                                                              ////
1741
////  Author(s):                                                  ////
1742
////      - Michael Unneback, unneback@opencores.org              ////
1743
////        ORSoC AB                                              ////
1744
////                                                              ////
1745
//////////////////////////////////////////////////////////////////////
1746
////                                                              ////
1747
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1748
////                                                              ////
1749
//// This source file may be used and distributed without         ////
1750
//// restriction provided that this copyright statement is not    ////
1751
//// removed from the file and that any derivative work contains  ////
1752
//// the original copyright notice and the associated disclaimer. ////
1753
////                                                              ////
1754
//// This source file is free software; you can redistribute it   ////
1755
//// and/or modify it under the terms of the GNU Lesser General   ////
1756
//// Public License as published by the Free Software Foundation; ////
1757
//// either version 2.1 of the License, or (at your option) any   ////
1758
//// later version.                                               ////
1759
////                                                              ////
1760
//// This source is distributed in the hope that it will be       ////
1761
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1762
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1763
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1764
//// details.                                                     ////
1765
////                                                              ////
1766
//// You should have received a copy of the GNU Lesser General    ////
1767
//// Public License along with this source; if not, download it   ////
1768
//// from http://www.opencores.org/lgpl.shtml                     ////
1769
////                                                              ////
1770
//////////////////////////////////////////////////////////////////////
1771 139 unneback
// LFSR counter
1772
module vl_cnt_lfsr_ce_zq (
1773
 cke, zq, rst, clk);
1774
   parameter length = 4;
1775
   input cke;
1776
   output reg zq;
1777
   input rst;
1778
   input clk;
1779
   parameter clear_value = 0;
1780
   parameter set_value = 1;
1781
   parameter wrap_value = 8;
1782
   parameter level1_value = 15;
1783
   reg  [length:1] qi;
1784
   reg lfsr_fb;
1785
   wire [length:1] q_next;
1786
   reg [32:1] polynom;
1787
   integer i;
1788
   always @ (qi)
1789
   begin
1790
        case (length)
1791
         2: polynom = 32'b11;                               // 0x3
1792
         3: polynom = 32'b110;                              // 0x6
1793
         4: polynom = 32'b1100;                             // 0xC
1794
         5: polynom = 32'b10100;                            // 0x14
1795
         6: polynom = 32'b110000;                           // 0x30
1796
         7: polynom = 32'b1100000;                          // 0x60
1797
         8: polynom = 32'b10111000;                         // 0xb8
1798
         9: polynom = 32'b100010000;                        // 0x110
1799
        10: polynom = 32'b1001000000;                       // 0x240
1800
        11: polynom = 32'b10100000000;                      // 0x500
1801
        12: polynom = 32'b100000101001;                     // 0x829
1802
        13: polynom = 32'b1000000001100;                    // 0x100C
1803
        14: polynom = 32'b10000000010101;                   // 0x2015
1804
        15: polynom = 32'b110000000000000;                  // 0x6000
1805
        16: polynom = 32'b1101000000001000;                 // 0xD008
1806
        17: polynom = 32'b10010000000000000;                // 0x12000
1807
        18: polynom = 32'b100000010000000000;               // 0x20400
1808
        19: polynom = 32'b1000000000000100011;              // 0x40023
1809
        20: polynom = 32'b10010000000000000000;             // 0x90000
1810
        21: polynom = 32'b101000000000000000000;            // 0x140000
1811
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1812
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1813
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1814
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1815
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1816
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1817
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1818
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1819
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1820
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1821
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1822
        default: polynom = 32'b0;
1823
        endcase
1824
        lfsr_fb = qi[length];
1825
        for (i=length-1; i>=1; i=i-1) begin
1826
            if (polynom[i])
1827
                lfsr_fb = lfsr_fb  ~^ qi[i];
1828
        end
1829
    end
1830
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1831
   always @ (posedge clk or posedge rst)
1832
     if (rst)
1833
       qi <= {length{1'b0}};
1834
     else
1835
     if (cke)
1836
       qi <= q_next;
1837
   always @ (posedge clk or posedge rst)
1838
     if (rst)
1839
       zq <= 1'b1;
1840
     else
1841
     if (cke)
1842
       zq <= q_next == {length{1'b0}};
1843
endmodule
1844
//////////////////////////////////////////////////////////////////////
1845
////                                                              ////
1846
////  Versatile counter                                           ////
1847
////                                                              ////
1848
////  Description                                                 ////
1849
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1850
////  counter                                                     ////
1851
////                                                              ////
1852
////  To Do:                                                      ////
1853
////   - add LFSR with more taps                                  ////
1854
////                                                              ////
1855
////  Author(s):                                                  ////
1856
////      - Michael Unneback, unneback@opencores.org              ////
1857
////        ORSoC AB                                              ////
1858
////                                                              ////
1859
//////////////////////////////////////////////////////////////////////
1860
////                                                              ////
1861
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1862
////                                                              ////
1863
//// This source file may be used and distributed without         ////
1864
//// restriction provided that this copyright statement is not    ////
1865
//// removed from the file and that any derivative work contains  ////
1866
//// the original copyright notice and the associated disclaimer. ////
1867
////                                                              ////
1868
//// This source file is free software; you can redistribute it   ////
1869
//// and/or modify it under the terms of the GNU Lesser General   ////
1870
//// Public License as published by the Free Software Foundation; ////
1871
//// either version 2.1 of the License, or (at your option) any   ////
1872
//// later version.                                               ////
1873
////                                                              ////
1874
//// This source is distributed in the hope that it will be       ////
1875
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1876
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1877
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1878
//// details.                                                     ////
1879
////                                                              ////
1880
//// You should have received a copy of the GNU Lesser General    ////
1881
//// Public License along with this source; if not, download it   ////
1882
//// from http://www.opencores.org/lgpl.shtml                     ////
1883
////                                                              ////
1884
//////////////////////////////////////////////////////////////////////
1885
// LFSR counter
1886
module vl_cnt_lfsr_ce_q (
1887
 cke, q, rst, clk);
1888
   parameter length = 4;
1889
   input cke;
1890
   output [length:1] q;
1891
   input rst;
1892
   input clk;
1893
   parameter clear_value = 0;
1894
   parameter set_value = 1;
1895
   parameter wrap_value = 8;
1896
   parameter level1_value = 15;
1897
   reg  [length:1] qi;
1898
   reg lfsr_fb;
1899
   wire [length:1] q_next;
1900
   reg [32:1] polynom;
1901
   integer i;
1902
   always @ (qi)
1903
   begin
1904
        case (length)
1905
         2: polynom = 32'b11;                               // 0x3
1906
         3: polynom = 32'b110;                              // 0x6
1907
         4: polynom = 32'b1100;                             // 0xC
1908
         5: polynom = 32'b10100;                            // 0x14
1909
         6: polynom = 32'b110000;                           // 0x30
1910
         7: polynom = 32'b1100000;                          // 0x60
1911
         8: polynom = 32'b10111000;                         // 0xb8
1912
         9: polynom = 32'b100010000;                        // 0x110
1913
        10: polynom = 32'b1001000000;                       // 0x240
1914
        11: polynom = 32'b10100000000;                      // 0x500
1915
        12: polynom = 32'b100000101001;                     // 0x829
1916
        13: polynom = 32'b1000000001100;                    // 0x100C
1917
        14: polynom = 32'b10000000010101;                   // 0x2015
1918
        15: polynom = 32'b110000000000000;                  // 0x6000
1919
        16: polynom = 32'b1101000000001000;                 // 0xD008
1920
        17: polynom = 32'b10010000000000000;                // 0x12000
1921
        18: polynom = 32'b100000010000000000;               // 0x20400
1922
        19: polynom = 32'b1000000000000100011;              // 0x40023
1923
        20: polynom = 32'b10010000000000000000;             // 0x90000
1924
        21: polynom = 32'b101000000000000000000;            // 0x140000
1925
        22: polynom = 32'b1100000000000000000000;           // 0x300000
1926
        23: polynom = 32'b10000100000000000000000;          // 0x420000
1927
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
1928
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
1929
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
1930
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
1931
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
1932
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
1933
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
1934
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
1935
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
1936
        default: polynom = 32'b0;
1937
        endcase
1938
        lfsr_fb = qi[length];
1939
        for (i=length-1; i>=1; i=i-1) begin
1940
            if (polynom[i])
1941
                lfsr_fb = lfsr_fb  ~^ qi[i];
1942
        end
1943
    end
1944
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
1945
   always @ (posedge clk or posedge rst)
1946
     if (rst)
1947
       qi <= {length{1'b0}};
1948
     else
1949
     if (cke)
1950
       qi <= q_next;
1951
   assign q = qi;
1952
endmodule
1953
//////////////////////////////////////////////////////////////////////
1954
////                                                              ////
1955
////  Versatile counter                                           ////
1956
////                                                              ////
1957
////  Description                                                 ////
1958
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
1959
////  counter                                                     ////
1960
////                                                              ////
1961
////  To Do:                                                      ////
1962
////   - add LFSR with more taps                                  ////
1963
////                                                              ////
1964
////  Author(s):                                                  ////
1965
////      - Michael Unneback, unneback@opencores.org              ////
1966
////        ORSoC AB                                              ////
1967
////                                                              ////
1968
//////////////////////////////////////////////////////////////////////
1969
////                                                              ////
1970
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
1971
////                                                              ////
1972
//// This source file may be used and distributed without         ////
1973
//// restriction provided that this copyright statement is not    ////
1974
//// removed from the file and that any derivative work contains  ////
1975
//// the original copyright notice and the associated disclaimer. ////
1976
////                                                              ////
1977
//// This source file is free software; you can redistribute it   ////
1978
//// and/or modify it under the terms of the GNU Lesser General   ////
1979
//// Public License as published by the Free Software Foundation; ////
1980
//// either version 2.1 of the License, or (at your option) any   ////
1981
//// later version.                                               ////
1982
////                                                              ////
1983
//// This source is distributed in the hope that it will be       ////
1984
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
1985
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
1986
//// PURPOSE.  See the GNU Lesser General Public License for more ////
1987
//// details.                                                     ////
1988
////                                                              ////
1989
//// You should have received a copy of the GNU Lesser General    ////
1990
//// Public License along with this source; if not, download it   ////
1991
//// from http://www.opencores.org/lgpl.shtml                     ////
1992
////                                                              ////
1993
//////////////////////////////////////////////////////////////////////
1994
// LFSR counter
1995
module vl_cnt_lfsr_ce_clear_q (
1996
 clear, cke, q, rst, clk);
1997
   parameter length = 4;
1998
   input clear;
1999
   input cke;
2000
   output [length:1] q;
2001
   input rst;
2002
   input clk;
2003
   parameter clear_value = 0;
2004
   parameter set_value = 1;
2005
   parameter wrap_value = 8;
2006
   parameter level1_value = 15;
2007
   reg  [length:1] qi;
2008
   reg lfsr_fb;
2009
   wire [length:1] q_next;
2010
   reg [32:1] polynom;
2011
   integer i;
2012
   always @ (qi)
2013
   begin
2014
        case (length)
2015
         2: polynom = 32'b11;                               // 0x3
2016
         3: polynom = 32'b110;                              // 0x6
2017
         4: polynom = 32'b1100;                             // 0xC
2018
         5: polynom = 32'b10100;                            // 0x14
2019
         6: polynom = 32'b110000;                           // 0x30
2020
         7: polynom = 32'b1100000;                          // 0x60
2021
         8: polynom = 32'b10111000;                         // 0xb8
2022
         9: polynom = 32'b100010000;                        // 0x110
2023
        10: polynom = 32'b1001000000;                       // 0x240
2024
        11: polynom = 32'b10100000000;                      // 0x500
2025
        12: polynom = 32'b100000101001;                     // 0x829
2026
        13: polynom = 32'b1000000001100;                    // 0x100C
2027
        14: polynom = 32'b10000000010101;                   // 0x2015
2028
        15: polynom = 32'b110000000000000;                  // 0x6000
2029
        16: polynom = 32'b1101000000001000;                 // 0xD008
2030
        17: polynom = 32'b10010000000000000;                // 0x12000
2031
        18: polynom = 32'b100000010000000000;               // 0x20400
2032
        19: polynom = 32'b1000000000000100011;              // 0x40023
2033
        20: polynom = 32'b10010000000000000000;             // 0x90000
2034
        21: polynom = 32'b101000000000000000000;            // 0x140000
2035
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2036
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2037
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2038
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2039
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2040
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2041
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2042
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2043
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2044
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2045
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2046
        default: polynom = 32'b0;
2047
        endcase
2048
        lfsr_fb = qi[length];
2049
        for (i=length-1; i>=1; i=i-1) begin
2050
            if (polynom[i])
2051
                lfsr_fb = lfsr_fb  ~^ qi[i];
2052
        end
2053
    end
2054
   assign q_next =  clear ? {length{1'b0}} :(qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2055
   always @ (posedge clk or posedge rst)
2056
     if (rst)
2057
       qi <= {length{1'b0}};
2058
     else
2059
     if (cke)
2060
       qi <= q_next;
2061
   assign q = qi;
2062
endmodule
2063
//////////////////////////////////////////////////////////////////////
2064
////                                                              ////
2065
////  Versatile counter                                           ////
2066
////                                                              ////
2067
////  Description                                                 ////
2068
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2069
////  counter                                                     ////
2070
////                                                              ////
2071
////  To Do:                                                      ////
2072
////   - add LFSR with more taps                                  ////
2073
////                                                              ////
2074
////  Author(s):                                                  ////
2075
////      - Michael Unneback, unneback@opencores.org              ////
2076
////        ORSoC AB                                              ////
2077
////                                                              ////
2078
//////////////////////////////////////////////////////////////////////
2079
////                                                              ////
2080
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2081
////                                                              ////
2082
//// This source file may be used and distributed without         ////
2083
//// restriction provided that this copyright statement is not    ////
2084
//// removed from the file and that any derivative work contains  ////
2085
//// the original copyright notice and the associated disclaimer. ////
2086
////                                                              ////
2087
//// This source file is free software; you can redistribute it   ////
2088
//// and/or modify it under the terms of the GNU Lesser General   ////
2089
//// Public License as published by the Free Software Foundation; ////
2090
//// either version 2.1 of the License, or (at your option) any   ////
2091
//// later version.                                               ////
2092
////                                                              ////
2093
//// This source is distributed in the hope that it will be       ////
2094
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2095
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2096
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2097
//// details.                                                     ////
2098
////                                                              ////
2099
//// You should have received a copy of the GNU Lesser General    ////
2100
//// Public License along with this source; if not, download it   ////
2101
//// from http://www.opencores.org/lgpl.shtml                     ////
2102
////                                                              ////
2103
//////////////////////////////////////////////////////////////////////
2104
// LFSR counter
2105
module vl_cnt_lfsr_ce_q_zq (
2106
 cke, q, zq, rst, clk);
2107
   parameter length = 4;
2108
   input cke;
2109
   output [length:1] q;
2110
   output reg zq;
2111
   input rst;
2112
   input clk;
2113
   parameter clear_value = 0;
2114
   parameter set_value = 1;
2115
   parameter wrap_value = 8;
2116
   parameter level1_value = 15;
2117
   reg  [length:1] qi;
2118
   reg lfsr_fb;
2119
   wire [length:1] q_next;
2120
   reg [32:1] polynom;
2121
   integer i;
2122
   always @ (qi)
2123
   begin
2124
        case (length)
2125
         2: polynom = 32'b11;                               // 0x3
2126
         3: polynom = 32'b110;                              // 0x6
2127
         4: polynom = 32'b1100;                             // 0xC
2128
         5: polynom = 32'b10100;                            // 0x14
2129
         6: polynom = 32'b110000;                           // 0x30
2130
         7: polynom = 32'b1100000;                          // 0x60
2131
         8: polynom = 32'b10111000;                         // 0xb8
2132
         9: polynom = 32'b100010000;                        // 0x110
2133
        10: polynom = 32'b1001000000;                       // 0x240
2134
        11: polynom = 32'b10100000000;                      // 0x500
2135
        12: polynom = 32'b100000101001;                     // 0x829
2136
        13: polynom = 32'b1000000001100;                    // 0x100C
2137
        14: polynom = 32'b10000000010101;                   // 0x2015
2138
        15: polynom = 32'b110000000000000;                  // 0x6000
2139
        16: polynom = 32'b1101000000001000;                 // 0xD008
2140
        17: polynom = 32'b10010000000000000;                // 0x12000
2141
        18: polynom = 32'b100000010000000000;               // 0x20400
2142
        19: polynom = 32'b1000000000000100011;              // 0x40023
2143
        20: polynom = 32'b10010000000000000000;             // 0x90000
2144
        21: polynom = 32'b101000000000000000000;            // 0x140000
2145
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2146
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2147
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2148
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2149
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2150
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2151
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2152
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2153
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2154
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2155
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2156
        default: polynom = 32'b0;
2157
        endcase
2158
        lfsr_fb = qi[length];
2159
        for (i=length-1; i>=1; i=i-1) begin
2160
            if (polynom[i])
2161
                lfsr_fb = lfsr_fb  ~^ qi[i];
2162
        end
2163
    end
2164
   assign q_next = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2165
   always @ (posedge clk or posedge rst)
2166
     if (rst)
2167
       qi <= {length{1'b0}};
2168
     else
2169
     if (cke)
2170
       qi <= q_next;
2171
   assign q = qi;
2172
   always @ (posedge clk or posedge rst)
2173
     if (rst)
2174
       zq <= 1'b1;
2175
     else
2176
     if (cke)
2177
       zq <= q_next == {length{1'b0}};
2178
endmodule
2179
//////////////////////////////////////////////////////////////////////
2180
////                                                              ////
2181
////  Versatile counter                                           ////
2182
////                                                              ////
2183
////  Description                                                 ////
2184
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2185
////  counter                                                     ////
2186
////                                                              ////
2187
////  To Do:                                                      ////
2188
////   - add LFSR with more taps                                  ////
2189
////                                                              ////
2190
////  Author(s):                                                  ////
2191
////      - Michael Unneback, unneback@opencores.org              ////
2192
////        ORSoC AB                                              ////
2193
////                                                              ////
2194
//////////////////////////////////////////////////////////////////////
2195
////                                                              ////
2196
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2197
////                                                              ////
2198
//// This source file may be used and distributed without         ////
2199
//// restriction provided that this copyright statement is not    ////
2200
//// removed from the file and that any derivative work contains  ////
2201
//// the original copyright notice and the associated disclaimer. ////
2202
////                                                              ////
2203
//// This source file is free software; you can redistribute it   ////
2204
//// and/or modify it under the terms of the GNU Lesser General   ////
2205
//// Public License as published by the Free Software Foundation; ////
2206
//// either version 2.1 of the License, or (at your option) any   ////
2207
//// later version.                                               ////
2208
////                                                              ////
2209
//// This source is distributed in the hope that it will be       ////
2210
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2211
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2212
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2213
//// details.                                                     ////
2214
////                                                              ////
2215
//// You should have received a copy of the GNU Lesser General    ////
2216
//// Public License along with this source; if not, download it   ////
2217
//// from http://www.opencores.org/lgpl.shtml                     ////
2218
////                                                              ////
2219
//////////////////////////////////////////////////////////////////////
2220
// LFSR counter
2221
module vl_cnt_lfsr_ce_rew_l1 (
2222
 cke, rew, level1, rst, clk);
2223
   parameter length = 4;
2224
   input cke;
2225
   input rew;
2226
   output reg level1;
2227
   input rst;
2228
   input clk;
2229
   parameter clear_value = 0;
2230
   parameter set_value = 1;
2231
   parameter wrap_value = 8;
2232
   parameter level1_value = 15;
2233
   wire clear;
2234
   assign clear = 1'b0;
2235
   reg  [length:1] qi;
2236
   reg lfsr_fb, lfsr_fb_rew;
2237
   wire  [length:1] q_next, q_next_fw, q_next_rew;
2238
   reg [32:1] polynom_rew;
2239
   integer j;
2240
   reg [32:1] polynom;
2241
   integer i;
2242
   always @ (qi)
2243
   begin
2244
        case (length)
2245
         2: polynom = 32'b11;                               // 0x3
2246
         3: polynom = 32'b110;                              // 0x6
2247
         4: polynom = 32'b1100;                             // 0xC
2248
         5: polynom = 32'b10100;                            // 0x14
2249
         6: polynom = 32'b110000;                           // 0x30
2250
         7: polynom = 32'b1100000;                          // 0x60
2251
         8: polynom = 32'b10111000;                         // 0xb8
2252
         9: polynom = 32'b100010000;                        // 0x110
2253
        10: polynom = 32'b1001000000;                       // 0x240
2254
        11: polynom = 32'b10100000000;                      // 0x500
2255
        12: polynom = 32'b100000101001;                     // 0x829
2256
        13: polynom = 32'b1000000001100;                    // 0x100C
2257
        14: polynom = 32'b10000000010101;                   // 0x2015
2258
        15: polynom = 32'b110000000000000;                  // 0x6000
2259
        16: polynom = 32'b1101000000001000;                 // 0xD008
2260
        17: polynom = 32'b10010000000000000;                // 0x12000
2261
        18: polynom = 32'b100000010000000000;               // 0x20400
2262
        19: polynom = 32'b1000000000000100011;              // 0x40023
2263
        20: polynom = 32'b10010000000000000000;             // 0x90000
2264
        21: polynom = 32'b101000000000000000000;            // 0x140000
2265
        22: polynom = 32'b1100000000000000000000;           // 0x300000
2266
        23: polynom = 32'b10000100000000000000000;          // 0x420000
2267
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
2268
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
2269
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
2270
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
2271
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
2272
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
2273
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
2274
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
2275
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
2276
        default: polynom = 32'b0;
2277
        endcase
2278
        lfsr_fb = qi[length];
2279
        for (i=length-1; i>=1; i=i-1) begin
2280
            if (polynom[i])
2281
                lfsr_fb = lfsr_fb  ~^ qi[i];
2282
        end
2283
    end
2284
   assign q_next_fw  = (qi == wrap_value) ? {length{1'b0}} :{qi[length-1:1],lfsr_fb};
2285
   always @ (qi)
2286
   begin
2287
        case (length)
2288
         2: polynom_rew = 32'b11;
2289
         3: polynom_rew = 32'b110;
2290
         4: polynom_rew = 32'b1100;
2291
         5: polynom_rew = 32'b10100;
2292
         6: polynom_rew = 32'b110000;
2293
         7: polynom_rew = 32'b1100000;
2294
         8: polynom_rew = 32'b10111000;
2295
         9: polynom_rew = 32'b100010000;
2296
        10: polynom_rew = 32'b1001000000;
2297
        11: polynom_rew = 32'b10100000000;
2298
        12: polynom_rew = 32'b100000101001;
2299
        13: polynom_rew = 32'b1000000001100;
2300
        14: polynom_rew = 32'b10000000010101;
2301
        15: polynom_rew = 32'b110000000000000;
2302
        16: polynom_rew = 32'b1101000000001000;
2303
        17: polynom_rew = 32'b10010000000000000;
2304
        18: polynom_rew = 32'b100000010000000000;
2305
        19: polynom_rew = 32'b1000000000000100011;
2306
        20: polynom_rew = 32'b10000010000000000000;
2307
        21: polynom_rew = 32'b101000000000000000000;
2308
        22: polynom_rew = 32'b1100000000000000000000;
2309
        23: polynom_rew = 32'b10000100000000000000000;
2310
        24: polynom_rew = 32'b111000010000000000000000;
2311
        25: polynom_rew = 32'b1001000000000000000000000;
2312
        26: polynom_rew = 32'b10000000000000000000100011;
2313
        27: polynom_rew = 32'b100000000000000000000010011;
2314
        28: polynom_rew = 32'b1100100000000000000000000000;
2315
        29: polynom_rew = 32'b10100000000000000000000000000;
2316
        30: polynom_rew = 32'b100000000000000000000000101001;
2317
        31: polynom_rew = 32'b1001000000000000000000000000000;
2318
        32: polynom_rew = 32'b10000000001000000000000000000011;
2319
        default: polynom_rew = 32'b0;
2320
        endcase
2321
        // rotate left
2322
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
2323
        lfsr_fb_rew = qi[length];
2324
        for (i=length-1; i>=1; i=i-1) begin
2325
            if (polynom_rew[i])
2326
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
2327
        end
2328
    end
2329
   assign q_next_rew = (qi == wrap_value) ? {length{1'b0}} :{lfsr_fb_rew,qi[length:2]};
2330
   assign q_next = rew ? q_next_rew : q_next_fw;
2331
   always @ (posedge clk or posedge rst)
2332
     if (rst)
2333
       qi <= {length{1'b0}};
2334
     else
2335
     if (cke)
2336
       qi <= q_next;
2337
    always @ (posedge clk or posedge rst)
2338
    if (rst)
2339
        level1 <= 1'b0;
2340
    else
2341
    if (cke)
2342
    if (clear)
2343
        level1 <= 1'b0;
2344
    else if (q_next == level1_value)
2345
        level1 <= 1'b1;
2346
    else if (qi == level1_value & rew)
2347
        level1 <= 1'b0;
2348
endmodule
2349
//////////////////////////////////////////////////////////////////////
2350
////                                                              ////
2351
////  Versatile counter                                           ////
2352
////                                                              ////
2353
////  Description                                                 ////
2354
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2355
////  counter                                                     ////
2356
////                                                              ////
2357
////  To Do:                                                      ////
2358
////   - add LFSR with more taps                                  ////
2359
////                                                              ////
2360
////  Author(s):                                                  ////
2361
////      - Michael Unneback, unneback@opencores.org              ////
2362
////        ORSoC AB                                              ////
2363
////                                                              ////
2364
//////////////////////////////////////////////////////////////////////
2365
////                                                              ////
2366
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2367
////                                                              ////
2368
//// This source file may be used and distributed without         ////
2369
//// restriction provided that this copyright statement is not    ////
2370
//// removed from the file and that any derivative work contains  ////
2371
//// the original copyright notice and the associated disclaimer. ////
2372
////                                                              ////
2373
//// This source file is free software; you can redistribute it   ////
2374
//// and/or modify it under the terms of the GNU Lesser General   ////
2375
//// Public License as published by the Free Software Foundation; ////
2376
//// either version 2.1 of the License, or (at your option) any   ////
2377
//// later version.                                               ////
2378
////                                                              ////
2379
//// This source is distributed in the hope that it will be       ////
2380
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2381
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2382
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2383
//// details.                                                     ////
2384
////                                                              ////
2385
//// You should have received a copy of the GNU Lesser General    ////
2386
//// Public License along with this source; if not, download it   ////
2387
//// from http://www.opencores.org/lgpl.shtml                     ////
2388
////                                                              ////
2389
//////////////////////////////////////////////////////////////////////
2390 6 unneback
// GRAY counter
2391 139 unneback
module vl_cnt_gray (
2392
 q, rst, clk);
2393
   parameter length = 4;
2394
   output reg [length:1] q;
2395
   input rst;
2396
   input clk;
2397
   parameter clear_value = 0;
2398
   parameter set_value = 1;
2399
   parameter wrap_value = 8;
2400
   parameter level1_value = 15;
2401
   reg  [length:1] qi;
2402
   wire [length:1] q_next;
2403
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2404
   always @ (posedge clk or posedge rst)
2405
     if (rst)
2406
       qi <= {length{1'b0}};
2407
     else
2408
       qi <= q_next;
2409
   always @ (posedge clk or posedge rst)
2410
     if (rst)
2411
       q <= {length{1'b0}};
2412
     else
2413
         q <= (q_next>>1) ^ q_next;
2414
endmodule
2415
//////////////////////////////////////////////////////////////////////
2416
////                                                              ////
2417
////  Versatile counter                                           ////
2418
////                                                              ////
2419
////  Description                                                 ////
2420
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2421
////  counter                                                     ////
2422
////                                                              ////
2423
////  To Do:                                                      ////
2424
////   - add LFSR with more taps                                  ////
2425
////                                                              ////
2426
////  Author(s):                                                  ////
2427
////      - Michael Unneback, unneback@opencores.org              ////
2428
////        ORSoC AB                                              ////
2429
////                                                              ////
2430
//////////////////////////////////////////////////////////////////////
2431
////                                                              ////
2432
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2433
////                                                              ////
2434
//// This source file may be used and distributed without         ////
2435
//// restriction provided that this copyright statement is not    ////
2436
//// removed from the file and that any derivative work contains  ////
2437
//// the original copyright notice and the associated disclaimer. ////
2438
////                                                              ////
2439
//// This source file is free software; you can redistribute it   ////
2440
//// and/or modify it under the terms of the GNU Lesser General   ////
2441
//// Public License as published by the Free Software Foundation; ////
2442
//// either version 2.1 of the License, or (at your option) any   ////
2443
//// later version.                                               ////
2444
////                                                              ////
2445
//// This source is distributed in the hope that it will be       ////
2446
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2447
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2448
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2449
//// details.                                                     ////
2450
////                                                              ////
2451
//// You should have received a copy of the GNU Lesser General    ////
2452
//// Public License along with this source; if not, download it   ////
2453
//// from http://www.opencores.org/lgpl.shtml                     ////
2454
////                                                              ////
2455
//////////////////////////////////////////////////////////////////////
2456
// GRAY counter
2457
module vl_cnt_gray_ce (
2458
 cke, q, rst, clk);
2459
   parameter length = 4;
2460
   input cke;
2461
   output reg [length:1] q;
2462
   input rst;
2463
   input clk;
2464
   parameter clear_value = 0;
2465
   parameter set_value = 1;
2466
   parameter wrap_value = 8;
2467
   parameter level1_value = 15;
2468
   reg  [length:1] qi;
2469
   wire [length:1] q_next;
2470
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2471
   always @ (posedge clk or posedge rst)
2472
     if (rst)
2473
       qi <= {length{1'b0}};
2474
     else
2475
     if (cke)
2476
       qi <= q_next;
2477
   always @ (posedge clk or posedge rst)
2478
     if (rst)
2479
       q <= {length{1'b0}};
2480
     else
2481
       if (cke)
2482
         q <= (q_next>>1) ^ q_next;
2483
endmodule
2484
//////////////////////////////////////////////////////////////////////
2485
////                                                              ////
2486
////  Versatile counter                                           ////
2487
////                                                              ////
2488
////  Description                                                 ////
2489
////  Versatile counter, a reconfigurable binary, gray or LFSR    ////
2490
////  counter                                                     ////
2491
////                                                              ////
2492
////  To Do:                                                      ////
2493
////   - add LFSR with more taps                                  ////
2494
////                                                              ////
2495
////  Author(s):                                                  ////
2496
////      - Michael Unneback, unneback@opencores.org              ////
2497
////        ORSoC AB                                              ////
2498
////                                                              ////
2499
//////////////////////////////////////////////////////////////////////
2500
////                                                              ////
2501
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
2502
////                                                              ////
2503
//// This source file may be used and distributed without         ////
2504
//// restriction provided that this copyright statement is not    ////
2505
//// removed from the file and that any derivative work contains  ////
2506
//// the original copyright notice and the associated disclaimer. ////
2507
////                                                              ////
2508
//// This source file is free software; you can redistribute it   ////
2509
//// and/or modify it under the terms of the GNU Lesser General   ////
2510
//// Public License as published by the Free Software Foundation; ////
2511
//// either version 2.1 of the License, or (at your option) any   ////
2512
//// later version.                                               ////
2513
////                                                              ////
2514
//// This source is distributed in the hope that it will be       ////
2515
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2516
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2517
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2518
//// details.                                                     ////
2519
////                                                              ////
2520
//// You should have received a copy of the GNU Lesser General    ////
2521
//// Public License along with this source; if not, download it   ////
2522
//// from http://www.opencores.org/lgpl.shtml                     ////
2523
////                                                              ////
2524
//////////////////////////////////////////////////////////////////////
2525
// GRAY counter
2526 40 unneback
module vl_cnt_gray_ce_bin (
2527
 cke, q, q_bin, rst, clk);
2528 6 unneback
   parameter length = 4;
2529
   input cke;
2530
   output reg [length:1] q;
2531
   output [length:1] q_bin;
2532
   input rst;
2533
   input clk;
2534
   parameter clear_value = 0;
2535
   parameter set_value = 1;
2536
   parameter wrap_value = 8;
2537
   parameter level1_value = 15;
2538
   reg  [length:1] qi;
2539
   wire [length:1] q_next;
2540
   assign q_next = qi + {{length-1{1'b0}},1'b1};
2541
   always @ (posedge clk or posedge rst)
2542
     if (rst)
2543
       qi <= {length{1'b0}};
2544
     else
2545
     if (cke)
2546
       qi <= q_next;
2547
   always @ (posedge clk or posedge rst)
2548
     if (rst)
2549
       q <= {length{1'b0}};
2550
     else
2551
       if (cke)
2552
         q <= (q_next>>1) ^ q_next;
2553
   assign q_bin = qi;
2554
endmodule
2555
//////////////////////////////////////////////////////////////////////
2556
////                                                              ////
2557
////  Versatile library, counters                                 ////
2558
////                                                              ////
2559
////  Description                                                 ////
2560
////  counters                                                    ////
2561
////                                                              ////
2562
////                                                              ////
2563
////  To Do:                                                      ////
2564
////   - add more counters                                        ////
2565
////                                                              ////
2566
////  Author(s):                                                  ////
2567
////      - Michael Unneback, unneback@opencores.org              ////
2568
////        ORSoC AB                                              ////
2569
////                                                              ////
2570
//////////////////////////////////////////////////////////////////////
2571
////                                                              ////
2572
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2573
////                                                              ////
2574
//// This source file may be used and distributed without         ////
2575
//// restriction provided that this copyright statement is not    ////
2576
//// removed from the file and that any derivative work contains  ////
2577
//// the original copyright notice and the associated disclaimer. ////
2578
////                                                              ////
2579
//// This source file is free software; you can redistribute it   ////
2580
//// and/or modify it under the terms of the GNU Lesser General   ////
2581
//// Public License as published by the Free Software Foundation; ////
2582
//// either version 2.1 of the License, or (at your option) any   ////
2583
//// later version.                                               ////
2584
////                                                              ////
2585
//// This source is distributed in the hope that it will be       ////
2586
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2587
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2588
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2589
//// details.                                                     ////
2590
////                                                              ////
2591
//// You should have received a copy of the GNU Lesser General    ////
2592
//// Public License along with this source; if not, download it   ////
2593
//// from http://www.opencores.org/lgpl.shtml                     ////
2594
////                                                              ////
2595
//////////////////////////////////////////////////////////////////////
2596 18 unneback
module vl_cnt_shreg_wrap ( q, rst, clk);
2597 6 unneback
   parameter length = 4;
2598
   output reg [0:length-1] q;
2599
   input rst;
2600
   input clk;
2601
    always @ (posedge clk or posedge rst)
2602
    if (rst)
2603
        q <= {1'b1,{length-1{1'b0}}};
2604
    else
2605
        q <= {q[length-1],q[0:length-2]};
2606
endmodule
2607 18 unneback
module vl_cnt_shreg_ce_wrap ( cke, q, rst, clk);
2608 6 unneback
   parameter length = 4;
2609
   input cke;
2610
   output reg [0:length-1] q;
2611
   input rst;
2612
   input clk;
2613
    always @ (posedge clk or posedge rst)
2614
    if (rst)
2615
        q <= {1'b1,{length-1{1'b0}}};
2616
    else
2617
        if (cke)
2618
            q <= {q[length-1],q[0:length-2]};
2619
endmodule
2620 105 unneback
module vl_cnt_shreg_clear ( clear, q, rst, clk);
2621
   parameter length = 4;
2622
   input clear;
2623
   output reg [0:length-1] q;
2624
   input rst;
2625
   input clk;
2626
    always @ (posedge clk or posedge rst)
2627
    if (rst)
2628
        q <= {1'b1,{length-1{1'b0}}};
2629
    else
2630
        if (clear)
2631
            q <= {1'b1,{length-1{1'b0}}};
2632
        else
2633
            q <= q >> 1;
2634
endmodule
2635 18 unneback
module vl_cnt_shreg_ce_clear ( cke, clear, q, rst, clk);
2636 6 unneback
   parameter length = 4;
2637
   input cke, clear;
2638
   output reg [0:length-1] q;
2639
   input rst;
2640
   input clk;
2641
    always @ (posedge clk or posedge rst)
2642
    if (rst)
2643
        q <= {1'b1,{length-1{1'b0}}};
2644
    else
2645
        if (cke)
2646
            if (clear)
2647
                q <= {1'b1,{length-1{1'b0}}};
2648
            else
2649
                q <= q >> 1;
2650
endmodule
2651 18 unneback
module vl_cnt_shreg_ce_clear_wrap ( cke, clear, q, rst, clk);
2652 6 unneback
   parameter length = 4;
2653
   input cke, clear;
2654
   output reg [0:length-1] q;
2655
   input rst;
2656
   input clk;
2657
    always @ (posedge clk or posedge rst)
2658
    if (rst)
2659
        q <= {1'b1,{length-1{1'b0}}};
2660
    else
2661
        if (cke)
2662
            if (clear)
2663
                q <= {1'b1,{length-1{1'b0}}};
2664
            else
2665
            q <= {q[length-1],q[0:length-2]};
2666
endmodule
2667
//////////////////////////////////////////////////////////////////////
2668
////                                                              ////
2669
////  Versatile library, memories                                 ////
2670
////                                                              ////
2671
////  Description                                                 ////
2672
////  memories                                                    ////
2673
////                                                              ////
2674
////                                                              ////
2675
////  To Do:                                                      ////
2676
////   - add more memory types                                    ////
2677
////                                                              ////
2678
////  Author(s):                                                  ////
2679
////      - Michael Unneback, unneback@opencores.org              ////
2680
////        ORSoC AB                                              ////
2681
////                                                              ////
2682
//////////////////////////////////////////////////////////////////////
2683
////                                                              ////
2684
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
2685
////                                                              ////
2686
//// This source file may be used and distributed without         ////
2687
//// restriction provided that this copyright statement is not    ////
2688
//// removed from the file and that any derivative work contains  ////
2689
//// the original copyright notice and the associated disclaimer. ////
2690
////                                                              ////
2691
//// This source file is free software; you can redistribute it   ////
2692
//// and/or modify it under the terms of the GNU Lesser General   ////
2693
//// Public License as published by the Free Software Foundation; ////
2694
//// either version 2.1 of the License, or (at your option) any   ////
2695
//// later version.                                               ////
2696
////                                                              ////
2697
//// This source is distributed in the hope that it will be       ////
2698
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
2699
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
2700
//// PURPOSE.  See the GNU Lesser General Public License for more ////
2701
//// details.                                                     ////
2702
////                                                              ////
2703
//// You should have received a copy of the GNU Lesser General    ////
2704
//// Public License along with this source; if not, download it   ////
2705
//// from http://www.opencores.org/lgpl.shtml                     ////
2706
////                                                              ////
2707
//////////////////////////////////////////////////////////////////////
2708
/// ROM
2709 7 unneback
module vl_rom_init ( adr, q, clk);
2710
   parameter data_width = 32;
2711
   parameter addr_width = 8;
2712 75 unneback
   parameter mem_size = 1<<addr_width;
2713 7 unneback
   input [(addr_width-1):0]       adr;
2714
   output reg [(data_width-1):0] q;
2715
   input                         clk;
2716 75 unneback
   reg [data_width-1:0] rom [mem_size-1:0];
2717 7 unneback
   parameter memory_file = "vl_rom.vmem";
2718
   initial
2719
     begin
2720
        $readmemh(memory_file, rom);
2721
     end
2722
   always @ (posedge clk)
2723
     q <= rom[adr];
2724
endmodule
2725 6 unneback
// Single port RAM
2726
module vl_ram ( d, adr, we, q, clk);
2727
   parameter data_width = 32;
2728
   parameter addr_width = 8;
2729 75 unneback
   parameter mem_size = 1<<addr_width;
2730 100 unneback
   parameter debug = 0;
2731 6 unneback
   input [(data_width-1):0]      d;
2732
   input [(addr_width-1):0]       adr;
2733
   input                         we;
2734 7 unneback
   output reg [(data_width-1):0] q;
2735 6 unneback
   input                         clk;
2736 98 unneback
   reg [data_width-1:0] ram [mem_size-1:0];
2737 100 unneback
    parameter memory_init = 0;
2738
    parameter memory_file = "vl_ram.vmem";
2739
    generate
2740
    if (memory_init == 1) begin : init_mem
2741
        initial
2742
            $readmemh(memory_file, ram);
2743
   end else if (memory_init == 2) begin : init_zero
2744
        integer k;
2745
        initial
2746
            for (k = 0; k < mem_size; k = k + 1)
2747
                ram[k] = 0;
2748 7 unneback
   end
2749
   endgenerate
2750 100 unneback
    generate
2751
    if (debug==1) begin : debug_we
2752
        always @ (posedge clk)
2753
        if (we)
2754
            $display ("Value %h written at address %h : time %t", d, adr, $time);
2755
    end
2756
    endgenerate
2757 6 unneback
   always @ (posedge clk)
2758
   begin
2759
   if (we)
2760
     ram[adr] <= d;
2761
   q <= ram[adr];
2762
   end
2763
endmodule
2764 91 unneback
module vl_ram_be ( d, adr, be, we, q, clk);
2765 7 unneback
   parameter data_width = 32;
2766 72 unneback
   parameter addr_width = 6;
2767 75 unneback
   parameter mem_size = 1<<addr_width;
2768 7 unneback
   input [(data_width-1):0]      d;
2769
   input [(addr_width-1):0]       adr;
2770 73 unneback
   input [(data_width/8)-1:0]    be;
2771 7 unneback
   input                         we;
2772
   output reg [(data_width-1):0] q;
2773
   input                         clk;
2774 65 unneback
`ifdef SYSTEMVERILOG
2775 95 unneback
    // use a multi-dimensional packed array
2776
    //t o model individual bytes within the word
2777
    logic [data_width/8-1:0][7:0] ram [0:mem_size-1];// # words = 1 << address width
2778 65 unneback
`else
2779 85 unneback
    reg [data_width-1:0] ram [mem_size-1:0];
2780
    wire [data_width/8-1:0] cke;
2781 65 unneback
`endif
2782 100 unneback
    parameter memory_init = 0;
2783
    parameter memory_file = "vl_ram.vmem";
2784
    generate
2785
    if (memory_init == 1) begin : init_mem
2786
        initial
2787
            $readmemh(memory_file, ram);
2788
    end else if (memory_init == 2) begin : init_zero
2789
        integer k;
2790
        initial
2791
            for (k = 0; k < mem_size; k = k + 1)
2792
                ram[k] = 0;
2793
    end
2794 7 unneback
   endgenerate
2795 60 unneback
`ifdef SYSTEMVERILOG
2796
always_ff@(posedge clk)
2797
begin
2798 95 unneback
    if(we) begin
2799 86 unneback
        if(be[3]) ram[adr][3] <= d[31:24];
2800
        if(be[2]) ram[adr][2] <= d[23:16];
2801
        if(be[1]) ram[adr][1] <= d[15:8];
2802
        if(be[0]) ram[adr][0] <= d[7:0];
2803 60 unneback
    end
2804 90 unneback
        q <= ram[adr];
2805 60 unneback
end
2806
`else
2807 85 unneback
assign cke = {data_width/8{we}} & be;
2808 7 unneback
   genvar i;
2809 85 unneback
   generate for (i=0;i<data_width/8;i=i+1) begin : be_ram
2810 7 unneback
      always @ (posedge clk)
2811 85 unneback
      if (cke[i])
2812 7 unneback
        ram[adr][(i+1)*8-1:i*8] <= d[(i+1)*8-1:i*8];
2813
   end
2814
   endgenerate
2815
   always @ (posedge clk)
2816
      q <= ram[adr];
2817 60 unneback
`endif
2818 93 unneback
`ifdef verilator
2819 85 unneback
   // Function to access RAM (for use by Verilator).
2820
   function [31:0] get_mem;
2821
      // verilator public
2822 90 unneback
      input [addr_width-1:0]             addr;
2823 85 unneback
      get_mem = ram[addr];
2824
   endfunction // get_mem
2825
   // Function to write RAM (for use by Verilator).
2826
   function set_mem;
2827
      // verilator public
2828 90 unneback
      input [addr_width-1:0]             addr;
2829
      input [data_width-1:0]             data;
2830 85 unneback
      ram[addr] = data;
2831
   endfunction // set_mem
2832 93 unneback
`endif
2833 7 unneback
endmodule
2834
module vl_dpram_1r1w ( d_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
2835 6 unneback
   parameter data_width = 32;
2836
   parameter addr_width = 8;
2837 75 unneback
   parameter mem_size = 1<<addr_width;
2838 6 unneback
   input [(data_width-1):0]      d_a;
2839
   input [(addr_width-1):0]       adr_a;
2840
   input [(addr_width-1):0]       adr_b;
2841
   input                         we_a;
2842 118 unneback
   output reg [(data_width-1):0]          q_b;
2843 6 unneback
   input                         clk_a, clk_b;
2844 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] ;
2845 100 unneback
    parameter memory_init = 0;
2846
    parameter memory_file = "vl_ram.vmem";
2847
    parameter debug = 0;
2848
    generate
2849
    if (memory_init == 1) begin : init_mem
2850
        initial
2851
            $readmemh(memory_file, ram);
2852
    end else if (memory_init == 2) begin : init_zero
2853
        integer k;
2854
        initial
2855
            for (k = 0; k < mem_size; k = k + 1)
2856
                ram[k] = 0;
2857
    end
2858 7 unneback
   endgenerate
2859 100 unneback
    generate
2860
    if (debug==1) begin : debug_we
2861
        always @ (posedge clk_a)
2862
        if (we_a)
2863
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
2864
    end
2865
    endgenerate
2866 6 unneback
   always @ (posedge clk_a)
2867
   if (we_a)
2868
     ram[adr_a] <= d_a;
2869
   always @ (posedge clk_b)
2870 118 unneback
      q_b = ram[adr_b];
2871 6 unneback
endmodule
2872 7 unneback
module vl_dpram_2r1w ( d_a, q_a, adr_a, we_a, clk_a, q_b, adr_b, clk_b );
2873 6 unneback
   parameter data_width = 32;
2874
   parameter addr_width = 8;
2875 75 unneback
   parameter mem_size = 1<<addr_width;
2876 6 unneback
   input [(data_width-1):0]      d_a;
2877
   input [(addr_width-1):0]       adr_a;
2878
   input [(addr_width-1):0]       adr_b;
2879
   input                         we_a;
2880
   output [(data_width-1):0]      q_b;
2881
   output reg [(data_width-1):0] q_a;
2882
   input                         clk_a, clk_b;
2883
   reg [(data_width-1):0]         q_b;
2884 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] ;
2885 100 unneback
    parameter memory_init = 0;
2886
    parameter memory_file = "vl_ram.vmem";
2887
    parameter debug = 0;
2888
    generate
2889
    if (memory_init == 1) begin : init_mem
2890
        initial
2891
            $readmemh(memory_file, ram);
2892
    end else if (memory_init == 2) begin : init_zero
2893
        integer k;
2894
        initial
2895
            for (k = 0; k < mem_size; k = k + 1)
2896
                ram[k] = 0;
2897
    end
2898 7 unneback
   endgenerate
2899 100 unneback
    generate
2900
    if (debug==1) begin : debug_we
2901
        always @ (posedge clk_a)
2902
        if (we_a)
2903
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
2904
    end
2905
    endgenerate
2906 6 unneback
   always @ (posedge clk_a)
2907
     begin
2908
        q_a <= ram[adr_a];
2909
        if (we_a)
2910
             ram[adr_a] <= d_a;
2911
     end
2912
   always @ (posedge clk_b)
2913
          q_b <= ram[adr_b];
2914
endmodule
2915 100 unneback
module vl_dpram_1r2w ( d_a, q_a, adr_a, we_a, clk_a, d_b, adr_b, we_b, clk_b );
2916
   parameter data_width = 32;
2917
   parameter addr_width = 8;
2918
   parameter mem_size = 1<<addr_width;
2919
   input [(data_width-1):0]      d_a;
2920
   input [(addr_width-1):0]       adr_a;
2921
   input [(addr_width-1):0]       adr_b;
2922
   input                         we_a;
2923
   input [(data_width-1):0]       d_b;
2924
   output reg [(data_width-1):0] q_a;
2925
   input                         we_b;
2926
   input                         clk_a, clk_b;
2927
   reg [(data_width-1):0]         q_b;
2928 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] ;
2929 100 unneback
    parameter memory_init = 0;
2930
    parameter memory_file = "vl_ram.vmem";
2931
    parameter debug = 0;
2932
    generate
2933
    if (memory_init == 1) begin : init_mem
2934
        initial
2935
            $readmemh(memory_file, ram);
2936
    end else if (memory_init == 2) begin : init_zero
2937
        integer k;
2938
        initial
2939
            for (k = 0; k < mem_size; k = k + 1)
2940
                ram[k] = 0;
2941
    end
2942
   endgenerate
2943
    generate
2944
    if (debug==1) begin : debug_we
2945
        always @ (posedge clk_a)
2946
        if (we_a)
2947
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
2948
        always @ (posedge clk_b)
2949
        if (we_b)
2950
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
2951
    end
2952
    endgenerate
2953
   always @ (posedge clk_a)
2954
     begin
2955
        q_a <= ram[adr_a];
2956
        if (we_a)
2957
             ram[adr_a] <= d_a;
2958
     end
2959
   always @ (posedge clk_b)
2960
     begin
2961
        if (we_b)
2962
          ram[adr_b] <= d_b;
2963
     end
2964
endmodule
2965 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 );
2966 6 unneback
   parameter data_width = 32;
2967
   parameter addr_width = 8;
2968 75 unneback
   parameter mem_size = 1<<addr_width;
2969 6 unneback
   input [(data_width-1):0]      d_a;
2970
   input [(addr_width-1):0]       adr_a;
2971
   input [(addr_width-1):0]       adr_b;
2972
   input                         we_a;
2973
   output [(data_width-1):0]      q_b;
2974
   input [(data_width-1):0]       d_b;
2975
   output reg [(data_width-1):0] q_a;
2976
   input                         we_b;
2977
   input                         clk_a, clk_b;
2978
   reg [(data_width-1):0]         q_b;
2979 119 unneback
   reg [data_width-1:0] ram [0:mem_size-1] ;
2980 100 unneback
    parameter memory_init = 0;
2981
    parameter memory_file = "vl_ram.vmem";
2982
    parameter debug = 0;
2983
    generate
2984
    if (memory_init) begin : init_mem
2985
        initial
2986
            $readmemh(memory_file, ram);
2987
    end else if (memory_init == 2) begin : init_zero
2988
        integer k;
2989
        initial
2990
            for (k = 0; k < mem_size; k = k + 1)
2991
                ram[k] = 0;
2992
    end
2993 7 unneback
   endgenerate
2994 100 unneback
    generate
2995
    if (debug==1) begin : debug_we
2996
        always @ (posedge clk_a)
2997
        if (we_a)
2998
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
2999
        always @ (posedge clk_b)
3000
        if (we_b)
3001
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
3002
    end
3003
    endgenerate
3004 6 unneback
   always @ (posedge clk_a)
3005
     begin
3006
        q_a <= ram[adr_a];
3007
        if (we_a)
3008
             ram[adr_a] <= d_a;
3009
     end
3010
   always @ (posedge clk_b)
3011
     begin
3012
        q_b <= ram[adr_b];
3013
        if (we_b)
3014
          ram[adr_b] <= d_b;
3015
     end
3016
endmodule
3017 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 );
3018 75 unneback
   parameter a_data_width = 32;
3019
   parameter a_addr_width = 8;
3020 95 unneback
   parameter b_data_width = 64; //a_data_width;
3021 124 unneback
   //localparam b_addr_width = a_data_width * a_addr_width / b_data_width;
3022
   localparam b_addr_width =
3023 125 unneback
        (a_data_width==b_data_width) ? a_addr_width :
3024
        (a_data_width==b_data_width*2) ? a_addr_width+1 :
3025
        (a_data_width==b_data_width*4) ? a_addr_width+2 :
3026
        (a_data_width==b_data_width*8) ? a_addr_width+3 :
3027
        (a_data_width==b_data_width*16) ? a_addr_width+4 :
3028
        (a_data_width==b_data_width*32) ? a_addr_width+5 :
3029
        (a_data_width==b_data_width/2) ? a_addr_width-1 :
3030
        (a_data_width==b_data_width/4) ? a_addr_width-2 :
3031
        (a_data_width==b_data_width/8) ? a_addr_width-3 :
3032
        (a_data_width==b_data_width/16) ? a_addr_width-4 :
3033
        (a_data_width==b_data_width/32) ? a_addr_width-5 : 0;
3034 95 unneback
   localparam ratio = (a_addr_width>b_addr_width) ? (a_addr_width/b_addr_width) : (b_addr_width/a_addr_width);
3035
   parameter mem_size = (a_addr_width>b_addr_width) ? (1<<b_addr_width) : (1<<a_addr_width);
3036 100 unneback
   parameter memory_init = 0;
3037 95 unneback
   parameter memory_file = "vl_ram.vmem";
3038 100 unneback
   parameter debug = 0;
3039 75 unneback
   input [(a_data_width-1):0]      d_a;
3040 91 unneback
   input [(a_addr_width-1):0]       adr_a;
3041
   input [(a_data_width/8-1):0]    be_a;
3042
   input                           we_a;
3043 75 unneback
   output reg [(a_data_width-1):0] q_a;
3044 91 unneback
   input [(b_data_width-1):0]       d_b;
3045
   input [(b_addr_width-1):0]       adr_b;
3046 92 unneback
   input [(b_data_width/8-1):0]    be_b;
3047
   input                           we_b;
3048
   output reg [(b_data_width-1):0]          q_b;
3049 91 unneback
   input                           clk_a, clk_b;
3050 100 unneback
    generate
3051
    if (debug==1) begin : debug_we
3052
        always @ (posedge clk_a)
3053
        if (we_a)
3054
            $display ("Debug: Value %h written at address %h : time %t", d_a, adr_a, $time);
3055
        always @ (posedge clk_b)
3056
        if (we_b)
3057
            $display ("Debug: Value %h written at address %h : time %t", d_b, adr_b, $time);
3058
    end
3059
    endgenerate
3060 91 unneback
`ifdef SYSTEMVERILOG
3061
// use a multi-dimensional packed array
3062
//to model individual bytes within the word
3063 75 unneback
generate
3064 91 unneback
if (a_data_width==32 & b_data_width==32) begin : dpram_3232
3065 98 unneback
    logic [0:3][7:0] ram [0:mem_size-1] ;
3066 95 unneback
    initial
3067 100 unneback
        if (memory_init==1)
3068 95 unneback
            $readmemh(memory_file, ram);
3069 100 unneback
    integer k;
3070
    initial
3071
        if (memory_init==2)
3072
            for (k = 0; k < mem_size; k = k + 1)
3073
                ram[k] = 0;
3074 91 unneback
    always_ff@(posedge clk_a)
3075
    begin
3076
        if(we_a) begin
3077 100 unneback
            if(be_a[3]) ram[adr_a][0] <= d_a[31:24];
3078
            if(be_a[2]) ram[adr_a][1] <= d_a[23:16];
3079
            if(be_a[1]) ram[adr_a][2] <= d_a[15:8];
3080
            if(be_a[0]) ram[adr_a][3] <= d_a[7:0];
3081 91 unneback
        end
3082
    end
3083 92 unneback
    always@(posedge clk_a)
3084
        q_a = ram[adr_a];
3085 91 unneback
    always_ff@(posedge clk_b)
3086 92 unneback
    begin
3087
        if(we_b) begin
3088 100 unneback
            if(be_b[3]) ram[adr_b][0] <= d_b[31:24];
3089
            if(be_b[2]) ram[adr_b][1] <= d_b[23:16];
3090
            if(be_b[1]) ram[adr_b][2] <= d_b[15:8];
3091
            if(be_b[0]) ram[adr_b][3] <= d_b[7:0];
3092 92 unneback
        end
3093
    end
3094
    always@(posedge clk_b)
3095
        q_b = ram[adr_b];
3096 75 unneback
end
3097
endgenerate
3098 95 unneback
generate
3099
if (a_data_width==64 & b_data_width==64) begin : dpram_6464
3100 98 unneback
    logic [0:7][7:0] ram [0:mem_size-1] ;
3101 95 unneback
    initial
3102 100 unneback
        if (memory_init==1)
3103 95 unneback
            $readmemh(memory_file, ram);
3104 100 unneback
    integer k;
3105
    initial
3106
        if (memory_init==2)
3107
            for (k = 0; k < mem_size; k = k + 1)
3108
                ram[k] = 0;
3109 95 unneback
    always_ff@(posedge clk_a)
3110
    begin
3111
        if(we_a) begin
3112
            if(be_a[7]) ram[adr_a][7] <= d_a[63:56];
3113
            if(be_a[6]) ram[adr_a][6] <= d_a[55:48];
3114
            if(be_a[5]) ram[adr_a][5] <= d_a[47:40];
3115
            if(be_a[4]) ram[adr_a][4] <= d_a[39:32];
3116
            if(be_a[3]) ram[adr_a][3] <= d_a[31:24];
3117
            if(be_a[2]) ram[adr_a][2] <= d_a[23:16];
3118
            if(be_a[1]) ram[adr_a][1] <= d_a[15:8];
3119
            if(be_a[0]) ram[adr_a][0] <= d_a[7:0];
3120
        end
3121
    end
3122
    always@(posedge clk_a)
3123
        q_a = ram[adr_a];
3124
    always_ff@(posedge clk_b)
3125
    begin
3126
        if(we_b) begin
3127
            if(be_b[7]) ram[adr_b][7] <= d_b[63:56];
3128
            if(be_b[6]) ram[adr_b][6] <= d_b[55:48];
3129
            if(be_b[5]) ram[adr_b][5] <= d_b[47:40];
3130
            if(be_b[4]) ram[adr_b][4] <= d_b[39:32];
3131
            if(be_b[3]) ram[adr_b][3] <= d_b[31:24];
3132
            if(be_b[2]) ram[adr_b][2] <= d_b[23:16];
3133
            if(be_b[1]) ram[adr_b][1] <= d_b[15:8];
3134
            if(be_b[0]) ram[adr_b][0] <= d_b[7:0];
3135
        end
3136
    end
3137
    always@(posedge clk_b)
3138
        q_b = ram[adr_b];
3139
end
3140
endgenerate
3141
generate
3142
if (a_data_width==32 & b_data_width==16) begin : dpram_3216
3143
logic [31:0] temp;
3144 128 unneback
vl_dpram_be_2r2w # (.a_data_width(32), .b_data_width(32), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
3145
dpram3232 (
3146 95 unneback
    .d_a(d_a),
3147
    .q_a(q_a),
3148
    .adr_a(adr_a),
3149
    .be_a(be_a),
3150
    .we_a(we_a),
3151
    .clk_a(clk_a),
3152
    .d_b({d_b,d_b}),
3153
    .q_b(temp),
3154 128 unneback
    .adr_b(adr_b[b_addr_width-1:1]),
3155 137 unneback
    .be_b({be_b,be_b} & {{2{!adr_b[0]}},{2{adr_b[0]}}}),
3156 95 unneback
    .we_b(we_b),
3157
    .clk_b(clk_b)
3158
);
3159 100 unneback
always @ (adr_b[0] or temp)
3160 95 unneback
    if (adr_b[0])
3161
        q_b = temp[31:16];
3162
    else
3163
        q_b = temp[15:0];
3164
end
3165
endgenerate
3166
generate
3167
if (a_data_width==32 & b_data_width==64) begin : dpram_3264
3168
logic [63:0] temp;
3169 128 unneback
vl_dpram_be_2r2w # (.a_data_width(32), .b_data_width(64), .a_addr_width(a_addr_width), .mem_size(mem_size), .memory_init(memory_init), .memory_file(memory_file))
3170 95 unneback
dpram6464 (
3171
    .d_a({d_a,d_a}),
3172
    .q_a(temp),
3173
    .adr_a(adr_a[a_addr_width-1:1]),
3174
    .be_a({be_a,be_a} & {{4{adr_a[0]}},{4{!adr_a[0]}}}),
3175
    .we_a(we_a),
3176
    .clk_a(clk_a),
3177
    .d_b(d_b),
3178
    .q_b(q_b),
3179
    .adr_b(adr_b),
3180
    .be_b(be_b),
3181
    .we_b(we_b),
3182
    .clk_b(clk_b)
3183
);
3184 100 unneback
always @ (adr_a[0] or temp)
3185 95 unneback
    if (adr_a[0])
3186
        q_a = temp[63:32];
3187
    else
3188
        q_a = temp[31:0];
3189
end
3190
endgenerate
3191 91 unneback
`else
3192 92 unneback
    // This modules requires SystemVerilog
3193 98 unneback
    // at this point anyway
3194 91 unneback
`endif
3195 75 unneback
endmodule
3196 6 unneback
// FIFO
3197 25 unneback
module vl_fifo_1r1w_fill_level_sync (
3198
    d, wr, fifo_full,
3199
    q, rd, fifo_empty,
3200
    fill_level,
3201
    clk, rst
3202
    );
3203
parameter data_width = 18;
3204
parameter addr_width = 4;
3205
// write side
3206
input  [data_width-1:0] d;
3207
input                   wr;
3208
output                  fifo_full;
3209
// read side
3210
output [data_width-1:0] q;
3211
input                   rd;
3212
output                  fifo_empty;
3213
// common
3214
output [addr_width:0]   fill_level;
3215
input rst, clk;
3216
wire [addr_width:1] wadr, radr;
3217
vl_cnt_bin_ce
3218
    # ( .length(addr_width))
3219
    fifo_wr_adr( .cke(wr), .q(wadr), .rst(rst), .clk(clk));
3220
vl_cnt_bin_ce
3221
    # (.length(addr_width))
3222
    fifo_rd_adr( .cke(rd), .q(radr), .rst(rst), .clk(clk));
3223
vl_dpram_1r1w
3224
    # (.data_width(data_width), .addr_width(addr_width))
3225
    dpram ( .d_a(d), .adr_a(wadr), .we_a(wr), .clk_a(clk), .q_b(q), .adr_b(radr), .clk_b(clk));
3226 31 unneback
vl_cnt_bin_ce_rew_q_zq_l1
3227 27 unneback
    # (.length(addr_width+1), .level1_value(1<<addr_width))
3228 25 unneback
    fill_level_cnt( .cke(rd ^ wr), .rew(rd), .q(fill_level), .zq(fifo_empty), .level1(fifo_full), .rst(rst), .clk(clk));
3229
endmodule
3230 27 unneback
// Intended use is two small FIFOs (RX and TX typically) in one FPGA RAM resource
3231
// RAM is supposed to be larger than the two FIFOs
3232
// LFSR counters used adr pointers
3233
module vl_fifo_2r2w_sync_simplex (
3234
    // a side
3235
    a_d, a_wr, a_fifo_full,
3236
    a_q, a_rd, a_fifo_empty,
3237
    a_fill_level,
3238
    // b side
3239
    b_d, b_wr, b_fifo_full,
3240
    b_q, b_rd, b_fifo_empty,
3241
    b_fill_level,
3242
    // common
3243
    clk, rst
3244
    );
3245
parameter data_width = 8;
3246
parameter addr_width = 5;
3247
parameter fifo_full_level = (1<<addr_width)-1;
3248
// a side
3249
input  [data_width-1:0] a_d;
3250
input                   a_wr;
3251
output                  a_fifo_full;
3252
output [data_width-1:0] a_q;
3253
input                   a_rd;
3254
output                  a_fifo_empty;
3255
output [addr_width-1:0] a_fill_level;
3256
// b side
3257
input  [data_width-1:0] b_d;
3258
input                   b_wr;
3259
output                  b_fifo_full;
3260
output [data_width-1:0] b_q;
3261
input                   b_rd;
3262
output                  b_fifo_empty;
3263
output [addr_width-1:0] b_fill_level;
3264
input                   clk;
3265
input                   rst;
3266
// adr_gen
3267
wire [addr_width:1] a_wadr, a_radr;
3268
wire [addr_width:1] b_wadr, b_radr;
3269
// dpram
3270
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
3271
vl_cnt_lfsr_ce
3272
    # ( .length(addr_width))
3273
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .rst(rst), .clk(clk));
3274
vl_cnt_lfsr_ce
3275
    # (.length(addr_width))
3276
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .rst(rst), .clk(clk));
3277
vl_cnt_lfsr_ce
3278
    # ( .length(addr_width))
3279
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .rst(rst), .clk(clk));
3280
vl_cnt_lfsr_ce
3281
    # (.length(addr_width))
3282
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .rst(rst), .clk(clk));
3283
// mux read or write adr to DPRAM
3284
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr} : {1'b1,a_radr};
3285
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr} : {1'b0,b_radr};
3286
vl_dpram_2r2w
3287
    # (.data_width(data_width), .addr_width(addr_width+1))
3288
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
3289
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
3290
vl_cnt_bin_ce_rew_zq_l1
3291 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
3292 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));
3293
vl_cnt_bin_ce_rew_zq_l1
3294 28 unneback
    # (.length(addr_width), .level1_value(fifo_full_level))
3295 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));
3296
endmodule
3297 6 unneback
module vl_fifo_cmp_async ( wptr, rptr, fifo_empty, fifo_full, wclk, rclk, rst );
3298 11 unneback
   parameter addr_width = 4;
3299
   parameter N = addr_width-1;
3300 6 unneback
   parameter Q1 = 2'b00;
3301
   parameter Q2 = 2'b01;
3302
   parameter Q3 = 2'b11;
3303
   parameter Q4 = 2'b10;
3304
   parameter going_empty = 1'b0;
3305
   parameter going_full  = 1'b1;
3306
   input [N:0]  wptr, rptr;
3307 14 unneback
   output       fifo_empty;
3308 6 unneback
   output       fifo_full;
3309
   input        wclk, rclk, rst;
3310
   wire direction;
3311
   reg  direction_set, direction_clr;
3312
   wire async_empty, async_full;
3313
   wire fifo_full2;
3314 14 unneback
   wire fifo_empty2;
3315 6 unneback
   // direction_set
3316
   always @ (wptr[N:N-1] or rptr[N:N-1])
3317
     case ({wptr[N:N-1],rptr[N:N-1]})
3318
       {Q1,Q2} : direction_set <= 1'b1;
3319
       {Q2,Q3} : direction_set <= 1'b1;
3320
       {Q3,Q4} : direction_set <= 1'b1;
3321
       {Q4,Q1} : direction_set <= 1'b1;
3322
       default : direction_set <= 1'b0;
3323
     endcase
3324
   // direction_clear
3325
   always @ (wptr[N:N-1] or rptr[N:N-1] or rst)
3326
     if (rst)
3327
       direction_clr <= 1'b1;
3328
     else
3329
       case ({wptr[N:N-1],rptr[N:N-1]})
3330
         {Q2,Q1} : direction_clr <= 1'b1;
3331
         {Q3,Q2} : direction_clr <= 1'b1;
3332
         {Q4,Q3} : direction_clr <= 1'b1;
3333
         {Q1,Q4} : direction_clr <= 1'b1;
3334
         default : direction_clr <= 1'b0;
3335
       endcase
3336 18 unneback
    vl_dff_sr dff_sr_dir( .aclr(direction_clr), .aset(direction_set), .clock(1'b1), .data(1'b1), .q(direction));
3337 6 unneback
   assign async_empty = (wptr == rptr) && (direction==going_empty);
3338
   assign async_full  = (wptr == rptr) && (direction==going_full);
3339 18 unneback
    vl_dff_sr dff_sr_empty0( .aclr(rst), .aset(async_full), .clock(wclk), .data(async_full), .q(fifo_full2));
3340
    vl_dff_sr dff_sr_empty1( .aclr(rst), .aset(async_full), .clock(wclk), .data(fifo_full2), .q(fifo_full));
3341 6 unneback
/*
3342
   always @ (posedge wclk or posedge rst or posedge async_full)
3343
     if (rst)
3344
       {fifo_full, fifo_full2} <= 2'b00;
3345
     else if (async_full)
3346
       {fifo_full, fifo_full2} <= 2'b11;
3347
     else
3348
       {fifo_full, fifo_full2} <= {fifo_full2, async_full};
3349
*/
3350 14 unneback
/*   always @ (posedge rclk or posedge async_empty)
3351 6 unneback
     if (async_empty)
3352
       {fifo_empty, fifo_empty2} <= 2'b11;
3353
     else
3354 14 unneback
       {fifo_empty,fifo_empty2} <= {fifo_empty2,async_empty}; */
3355 18 unneback
    vl_dff # ( .reset_value(1'b1)) dff0 ( .d(async_empty), .q(fifo_empty2), .clk(rclk), .rst(async_empty));
3356
    vl_dff # ( .reset_value(1'b1)) dff1 ( .d(fifo_empty2), .q(fifo_empty),  .clk(rclk), .rst(async_empty));
3357 27 unneback
endmodule // async_compb
3358 6 unneback
module vl_fifo_1r1w_async (
3359
    d, wr, fifo_full, wr_clk, wr_rst,
3360
    q, rd, fifo_empty, rd_clk, rd_rst
3361
    );
3362
parameter data_width = 18;
3363
parameter addr_width = 4;
3364
// write side
3365
input  [data_width-1:0] d;
3366
input                   wr;
3367
output                  fifo_full;
3368
input                   wr_clk;
3369
input                   wr_rst;
3370
// read side
3371
output [data_width-1:0] q;
3372
input                   rd;
3373
output                  fifo_empty;
3374
input                   rd_clk;
3375
input                   rd_rst;
3376
wire [addr_width:1] wadr, wadr_bin, radr, radr_bin;
3377 18 unneback
vl_cnt_gray_ce_bin
3378 6 unneback
    # ( .length(addr_width))
3379
    fifo_wr_adr( .cke(wr), .q(wadr), .q_bin(wadr_bin), .rst(wr_rst), .clk(wr_clk));
3380 18 unneback
vl_cnt_gray_ce_bin
3381 6 unneback
    # (.length(addr_width))
3382 23 unneback
    fifo_rd_adr( .cke(rd), .q(radr), .q_bin(radr_bin), .rst(rd_rst), .clk(rd_clk));
3383 7 unneback
vl_dpram_1r1w
3384 6 unneback
    # (.data_width(data_width), .addr_width(addr_width))
3385
    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));
3386
vl_fifo_cmp_async
3387
    # (.addr_width(addr_width))
3388
    cmp ( .wptr(wadr), .rptr(radr), .fifo_empty(fifo_empty), .fifo_full(fifo_full), .wclk(wr_clk), .rclk(rd_clk), .rst(wr_rst) );
3389
endmodule
3390 8 unneback
module vl_fifo_2r2w_async (
3391 6 unneback
    // a side
3392
    a_d, a_wr, a_fifo_full,
3393
    a_q, a_rd, a_fifo_empty,
3394
    a_clk, a_rst,
3395
    // b side
3396
    b_d, b_wr, b_fifo_full,
3397
    b_q, b_rd, b_fifo_empty,
3398
    b_clk, b_rst
3399
    );
3400
parameter data_width = 18;
3401
parameter addr_width = 4;
3402
// a side
3403
input  [data_width-1:0] a_d;
3404
input                   a_wr;
3405
output                  a_fifo_full;
3406
output [data_width-1:0] a_q;
3407
input                   a_rd;
3408
output                  a_fifo_empty;
3409
input                   a_clk;
3410
input                   a_rst;
3411
// b side
3412
input  [data_width-1:0] b_d;
3413
input                   b_wr;
3414
output                  b_fifo_full;
3415
output [data_width-1:0] b_q;
3416
input                   b_rd;
3417
output                  b_fifo_empty;
3418
input                   b_clk;
3419
input                   b_rst;
3420
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
3421
vl_fifo_1r1w_async_a (
3422
    .d(a_d), .wr(a_wr), .fifo_full(a_fifo_full), .wr_clk(a_clk), .wr_rst(a_rst),
3423
    .q(b_q), .rd(b_rd), .fifo_empty(b_fifo_empty), .rd_clk(b_clk), .rd_rst(b_rst)
3424
    );
3425
vl_fifo_1r1w_async # (.data_width(data_width), .addr_width(addr_width))
3426
vl_fifo_1r1w_async_b (
3427
    .d(b_d), .wr(b_wr), .fifo_full(b_fifo_full), .wr_clk(b_clk), .wr_rst(b_rst),
3428
    .q(a_q), .rd(a_rd), .fifo_empty(a_fifo_empty), .rd_clk(a_clk), .rd_rst(a_rst)
3429
    );
3430
endmodule
3431 8 unneback
module vl_fifo_2r2w_async_simplex (
3432 6 unneback
    // a side
3433
    a_d, a_wr, a_fifo_full,
3434
    a_q, a_rd, a_fifo_empty,
3435
    a_clk, a_rst,
3436
    // b side
3437
    b_d, b_wr, b_fifo_full,
3438
    b_q, b_rd, b_fifo_empty,
3439
    b_clk, b_rst
3440
    );
3441
parameter data_width = 18;
3442
parameter addr_width = 4;
3443
// a side
3444
input  [data_width-1:0] a_d;
3445
input                   a_wr;
3446
output                  a_fifo_full;
3447
output [data_width-1:0] a_q;
3448
input                   a_rd;
3449
output                  a_fifo_empty;
3450
input                   a_clk;
3451
input                   a_rst;
3452
// b side
3453
input  [data_width-1:0] b_d;
3454
input                   b_wr;
3455
output                  b_fifo_full;
3456
output [data_width-1:0] b_q;
3457
input                   b_rd;
3458
output                  b_fifo_empty;
3459
input                   b_clk;
3460
input                   b_rst;
3461
// adr_gen
3462
wire [addr_width:1] a_wadr, a_wadr_bin, a_radr, a_radr_bin;
3463
wire [addr_width:1] b_wadr, b_wadr_bin, b_radr, b_radr_bin;
3464
// dpram
3465
wire [addr_width:0] a_dpram_adr, b_dpram_adr;
3466 18 unneback
vl_cnt_gray_ce_bin
3467 6 unneback
    # ( .length(addr_width))
3468
    fifo_a_wr_adr( .cke(a_wr), .q(a_wadr), .q_bin(a_wadr_bin), .rst(a_rst), .clk(a_clk));
3469 18 unneback
vl_cnt_gray_ce_bin
3470 6 unneback
    # (.length(addr_width))
3471
    fifo_a_rd_adr( .cke(a_rd), .q(a_radr), .q_bin(a_radr_bin), .rst(a_rst), .clk(a_clk));
3472 18 unneback
vl_cnt_gray_ce_bin
3473 6 unneback
    # ( .length(addr_width))
3474
    fifo_b_wr_adr( .cke(b_wr), .q(b_wadr), .q_bin(b_wadr_bin), .rst(b_rst), .clk(b_clk));
3475 18 unneback
vl_cnt_gray_ce_bin
3476 6 unneback
    # (.length(addr_width))
3477
    fifo_b_rd_adr( .cke(b_rd), .q(b_radr), .q_bin(b_radr_bin), .rst(b_rst), .clk(b_clk));
3478
// mux read or write adr to DPRAM
3479
assign a_dpram_adr = (a_wr) ? {1'b0,a_wadr_bin} : {1'b1,a_radr_bin};
3480
assign b_dpram_adr = (b_wr) ? {1'b1,b_wadr_bin} : {1'b0,b_radr_bin};
3481 11 unneback
vl_dpram_2r2w
3482 6 unneback
    # (.data_width(data_width), .addr_width(addr_width+1))
3483
    dpram ( .d_a(a_d), .q_a(a_q), .adr_a(a_dpram_adr), .we_a(a_wr), .clk_a(a_clk),
3484
            .d_b(b_d), .q_b(b_q), .adr_b(b_dpram_adr), .we_b(b_wr), .clk_b(b_clk));
3485 11 unneback
vl_fifo_cmp_async
3486 6 unneback
    # (.addr_width(addr_width))
3487
    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) );
3488 11 unneback
vl_fifo_cmp_async
3489 6 unneback
    # (.addr_width(addr_width))
3490
    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) );
3491
endmodule
3492 48 unneback
module vl_reg_file (
3493
    a1, a2, a3, wd3, we3, rd1, rd2, clk
3494
);
3495
parameter data_width = 32;
3496
parameter addr_width = 5;
3497
input [addr_width-1:0] a1, a2, a3;
3498
input [data_width-1:0] wd3;
3499
input we3;
3500
output [data_width-1:0] rd1, rd2;
3501
input clk;
3502
vl_dpram_1r1w
3503
    # ( .data_width(data_width), .addr_width(addr_width))
3504
    ram1 (
3505
        .d_a(wd3),
3506
        .adr_a(a3),
3507
        .we_a(we3),
3508
        .clk_a(clk),
3509
        .q_b(rd1),
3510
        .adr_b(a1),
3511
        .clk_b(clk) );
3512
vl_dpram_1r1w
3513
    # ( .data_width(data_width), .addr_width(addr_width))
3514
    ram2 (
3515
        .d_a(wd3),
3516
        .adr_a(a3),
3517
        .we_a(we3),
3518
        .clk_a(clk),
3519
        .q_b(rd2),
3520
        .adr_b(a2),
3521
        .clk_b(clk) );
3522
endmodule
3523 12 unneback
//////////////////////////////////////////////////////////////////////
3524
////                                                              ////
3525
////  Versatile library, wishbone stuff                           ////
3526
////                                                              ////
3527
////  Description                                                 ////
3528
////  Wishbone compliant modules                                  ////
3529
////                                                              ////
3530
////                                                              ////
3531
////  To Do:                                                      ////
3532
////   -                                                          ////
3533
////                                                              ////
3534
////  Author(s):                                                  ////
3535
////      - Michael Unneback, unneback@opencores.org              ////
3536
////        ORSoC AB                                              ////
3537
////                                                              ////
3538
//////////////////////////////////////////////////////////////////////
3539
////                                                              ////
3540
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
3541
////                                                              ////
3542
//// This source file may be used and distributed without         ////
3543
//// restriction provided that this copyright statement is not    ////
3544
//// removed from the file and that any derivative work contains  ////
3545
//// the original copyright notice and the associated disclaimer. ////
3546
////                                                              ////
3547
//// This source file is free software; you can redistribute it   ////
3548
//// and/or modify it under the terms of the GNU Lesser General   ////
3549
//// Public License as published by the Free Software Foundation; ////
3550
//// either version 2.1 of the License, or (at your option) any   ////
3551
//// later version.                                               ////
3552
////                                                              ////
3553
//// This source is distributed in the hope that it will be       ////
3554
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
3555
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
3556
//// PURPOSE.  See the GNU Lesser General Public License for more ////
3557
//// details.                                                     ////
3558
////                                                              ////
3559
//// You should have received a copy of the GNU Lesser General    ////
3560
//// Public License along with this source; if not, download it   ////
3561
//// from http://www.opencores.org/lgpl.shtml                     ////
3562
////                                                              ////
3563
//////////////////////////////////////////////////////////////////////
3564
`timescale 1ns/1ns
3565 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);
3566 83 unneback
parameter adr_width = 10;
3567
parameter max_burst_width = 4;
3568 85 unneback
input cyc_i, stb_i, we_i;
3569 83 unneback
input [2:0] cti_i;
3570
input [1:0] bte_i;
3571
input [adr_width-1:0] adr_i;
3572
output [adr_width-1:0] adr_o;
3573
output ack_o;
3574
input clk, rst;
3575
reg [adr_width-1:0] adr;
3576 90 unneback
wire [max_burst_width-1:0] to_adr;
3577 91 unneback
reg [max_burst_width-1:0] last_adr;
3578 92 unneback
reg last_cycle;
3579
localparam idle_or_eoc = 1'b0;
3580
localparam cyc_or_ws   = 1'b1;
3581 91 unneback
always @ (posedge clk or posedge rst)
3582
if (rst)
3583
    last_adr <= {max_burst_width{1'b0}};
3584
else
3585
    if (stb_i)
3586 92 unneback
        last_adr <=adr_o[max_burst_width-1:0];
3587 83 unneback
generate
3588
if (max_burst_width==0) begin : inst_0
3589 97 unneback
        reg ack_o;
3590
        assign adr_o = adr_i;
3591
        always @ (posedge clk or posedge rst)
3592
        if (rst)
3593
            ack_o <= 1'b0;
3594
        else
3595
            ack_o <= cyc_i & stb_i & !ack_o;
3596 83 unneback
end else begin
3597
    always @ (posedge clk or posedge rst)
3598
    if (rst)
3599 92 unneback
        last_cycle <= idle_or_eoc;
3600 83 unneback
    else
3601 92 unneback
        last_cycle <= (!cyc_i) ? idle_or_eoc : //idle
3602
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? idle_or_eoc : // eoc
3603
                      (cyc_i & !stb_i) ? cyc_or_ws : //ws
3604
                      cyc_or_ws; // cyc
3605
    assign to_adr = (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
3606 85 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
3607 91 unneback
                                        (!stb_i) ? last_adr :
3608 92 unneback
                                        (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] :
3609 85 unneback
                                        adr[max_burst_width-1:0];
3610 92 unneback
    assign ack_o = (last_cycle==cyc_or_ws) & stb_i;
3611 83 unneback
end
3612
endgenerate
3613
generate
3614
if (max_burst_width==2) begin : inst_2
3615
    always @ (posedge clk or posedge rst)
3616
    if (rst)
3617
        adr <= 2'h0;
3618
    else
3619
        if (cyc_i & stb_i)
3620
            adr[1:0] <= to_adr[1:0] + 2'd1;
3621
        else
3622
            adr <= to_adr[1:0];
3623
end
3624
endgenerate
3625
generate
3626
if (max_burst_width==3) begin : inst_3
3627
    always @ (posedge clk or posedge rst)
3628
    if (rst)
3629
        adr <= 3'h0;
3630
    else
3631
        if (cyc_i & stb_i)
3632
            case (bte_i)
3633
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
3634
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
3635
            endcase
3636
        else
3637
            adr <= to_adr[2:0];
3638
end
3639
endgenerate
3640
generate
3641
if (max_burst_width==4) begin : inst_4
3642
    always @ (posedge clk or posedge rst)
3643
    if (rst)
3644
        adr <= 4'h0;
3645
    else
3646 91 unneback
        if (stb_i) // | (!stb_i & last_cycle!=ws)) // for !stb_i restart with adr_i +1, only inc once
3647 83 unneback
            case (bte_i)
3648
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
3649
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
3650
            default: adr[3:0] <= to_adr + 4'd1;
3651
            endcase
3652
        else
3653
            adr <= to_adr[3:0];
3654
end
3655
endgenerate
3656
generate
3657
if (adr_width > max_burst_width) begin : pass_through
3658
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
3659
end
3660
endgenerate
3661
endmodule
3662
// async wb3 - wb3 bridge
3663
`timescale 1ns/1ns
3664 18 unneback
module vl_wb3wb3_bridge (
3665 12 unneback
        // wishbone slave side
3666
        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,
3667
        // wishbone master side
3668
        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);
3669 95 unneback
parameter style = "FIFO"; // valid: simple, FIFO
3670
parameter addr_width = 4;
3671 12 unneback
input [31:0] wbs_dat_i;
3672
input [31:2] wbs_adr_i;
3673
input [3:0]  wbs_sel_i;
3674
input [1:0]  wbs_bte_i;
3675
input [2:0]  wbs_cti_i;
3676
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
3677
output [31:0] wbs_dat_o;
3678 14 unneback
output wbs_ack_o;
3679 12 unneback
input wbs_clk, wbs_rst;
3680
output [31:0] wbm_dat_o;
3681
output reg [31:2] wbm_adr_o;
3682
output [3:0]  wbm_sel_o;
3683
output reg [1:0]  wbm_bte_o;
3684
output reg [2:0]  wbm_cti_o;
3685 14 unneback
output reg wbm_we_o;
3686
output wbm_cyc_o;
3687 12 unneback
output wbm_stb_o;
3688
input [31:0]  wbm_dat_i;
3689
input wbm_ack_i;
3690
input wbm_clk, wbm_rst;
3691
// bte
3692
parameter linear       = 2'b00;
3693
parameter wrap4        = 2'b01;
3694
parameter wrap8        = 2'b10;
3695
parameter wrap16       = 2'b11;
3696
// cti
3697
parameter classic      = 3'b000;
3698
parameter incburst     = 3'b010;
3699
parameter endofburst   = 3'b111;
3700 95 unneback
localparam wbs_adr  = 1'b0;
3701
localparam wbs_data = 1'b1;
3702
localparam wbm_adr0      = 2'b00;
3703
localparam wbm_adr1      = 2'b01;
3704
localparam wbm_data      = 2'b10;
3705
localparam wbm_data_wait = 2'b11;
3706 12 unneback
reg [1:0] wbs_bte_reg;
3707
reg wbs;
3708
wire wbs_eoc_alert, wbm_eoc_alert;
3709
reg wbs_eoc, wbm_eoc;
3710
reg [1:0] wbm;
3711 14 unneback
wire [1:16] wbs_count, wbm_count;
3712 12 unneback
wire [35:0] a_d, a_q, b_d, b_q;
3713
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
3714
reg a_rd_reg;
3715
wire b_rd_adr, b_rd_data;
3716 14 unneback
wire b_rd_data_reg;
3717
wire [35:0] temp;
3718 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]);
3719
always @ (posedge wbs_clk or posedge wbs_rst)
3720
if (wbs_rst)
3721
        wbs_eoc <= 1'b0;
3722
else
3723
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
3724 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
3725 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
3726
                wbs_eoc <= 1'b1;
3727 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
3728 12 unneback
    cnt0 (
3729
        .cke(wbs_ack_o),
3730
        .clear(wbs_eoc),
3731
        .q(wbs_count),
3732
        .rst(wbs_rst),
3733
        .clk(wbs_clk));
3734
always @ (posedge wbs_clk or posedge wbs_rst)
3735
if (wbs_rst)
3736
        wbs <= wbs_adr;
3737
else
3738 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
3739 12 unneback
                wbs <= wbs_data;
3740
        else if (wbs_eoc & wbs_ack_o)
3741
                wbs <= wbs_adr;
3742
// wbs FIFO
3743 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};
3744
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
3745 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
3746
              1'b0;
3747
assign a_rd = !a_fifo_empty;
3748
always @ (posedge wbs_clk or posedge wbs_rst)
3749
if (wbs_rst)
3750
        a_rd_reg <= 1'b0;
3751
else
3752
        a_rd_reg <= a_rd;
3753
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
3754
assign wbs_dat_o = a_q[35:4];
3755
always @ (posedge wbs_clk or posedge wbs_rst)
3756
if (wbs_rst)
3757 13 unneback
        wbs_bte_reg <= 2'b00;
3758 12 unneback
else
3759 13 unneback
        wbs_bte_reg <= wbs_bte_i;
3760 12 unneback
// wbm FIFO
3761
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]);
3762
always @ (posedge wbm_clk or posedge wbm_rst)
3763
if (wbm_rst)
3764
        wbm_eoc <= 1'b0;
3765
else
3766
        if (wbm==wbm_adr0 & !b_fifo_empty)
3767
                wbm_eoc <= b_q[4:3] == linear;
3768
        else if (wbm_eoc_alert & wbm_ack_i)
3769
                wbm_eoc <= 1'b1;
3770
always @ (posedge wbm_clk or posedge wbm_rst)
3771
if (wbm_rst)
3772
        wbm <= wbm_adr0;
3773
else
3774 33 unneback
/*
3775 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
3776
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
3777
        (wbm==wbm_adr1 & !wbm_we_o) |
3778
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
3779
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
3780 33 unneback
*/
3781
    case (wbm)
3782
    wbm_adr0:
3783
        if (!b_fifo_empty)
3784
            wbm <= wbm_adr1;
3785
    wbm_adr1:
3786
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
3787
            wbm <= wbm_data;
3788
    wbm_data:
3789
        if (wbm_ack_i & wbm_eoc)
3790
            wbm <= wbm_adr0;
3791
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
3792
            wbm <= wbm_data_wait;
3793
    wbm_data_wait:
3794
        if (!b_fifo_empty)
3795
            wbm <= wbm_data;
3796
    endcase
3797 12 unneback
assign b_d = {wbm_dat_i,4'b1111};
3798
assign b_wr = !wbm_we_o & wbm_ack_i;
3799
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
3800
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
3801
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
3802 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
3803 12 unneback
                   1'b0;
3804
assign b_rd = b_rd_adr | b_rd_data;
3805 18 unneback
vl_dff dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
3806
vl_dff_ce # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
3807 12 unneback
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
3808 18 unneback
vl_cnt_shreg_ce_clear # ( .length(16))
3809 12 unneback
    cnt1 (
3810
        .cke(wbm_ack_i),
3811
        .clear(wbm_eoc),
3812
        .q(wbm_count),
3813
        .rst(wbm_rst),
3814
        .clk(wbm_clk));
3815 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
3816
assign wbm_stb_o = (wbm==wbm_data);
3817 12 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
3818
if (wbm_rst)
3819
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
3820
else begin
3821
        if (wbm==wbm_adr0 & !b_fifo_empty)
3822
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
3823
        else if (wbm_eoc_alert & wbm_ack_i)
3824
                wbm_cti_o <= endofburst;
3825
end
3826
//async_fifo_dw_simplex_top
3827
vl_fifo_2r2w_async_simplex
3828
# ( .data_width(36), .addr_width(addr_width))
3829
fifo (
3830
    // a side
3831
    .a_d(a_d),
3832
    .a_wr(a_wr),
3833
    .a_fifo_full(a_fifo_full),
3834
    .a_q(a_q),
3835
    .a_rd(a_rd),
3836
    .a_fifo_empty(a_fifo_empty),
3837
    .a_clk(wbs_clk),
3838
    .a_rst(wbs_rst),
3839
    // b side
3840
    .b_d(b_d),
3841
    .b_wr(b_wr),
3842
    .b_fifo_full(b_fifo_full),
3843
    .b_q(b_q),
3844
    .b_rd(b_rd),
3845
    .b_fifo_empty(b_fifo_empty),
3846
    .b_clk(wbm_clk),
3847
    .b_rst(wbm_rst)
3848
    );
3849
endmodule
3850 75 unneback
module vl_wb3avalon_bridge (
3851
        // wishbone slave side
3852
        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,
3853 77 unneback
        // avalon master side
3854 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
3855 85 unneback
parameter linewrapburst = 1'b0;
3856 75 unneback
input [31:0] wbs_dat_i;
3857
input [31:2] wbs_adr_i;
3858
input [3:0]  wbs_sel_i;
3859
input [1:0]  wbs_bte_i;
3860
input [2:0]  wbs_cti_i;
3861 83 unneback
input wbs_we_i;
3862
input wbs_cyc_i;
3863
input wbs_stb_i;
3864 75 unneback
output [31:0] wbs_dat_o;
3865
output wbs_ack_o;
3866
input wbs_clk, wbs_rst;
3867
input [31:0] readdata;
3868
output [31:0] writedata;
3869
output [31:2] address;
3870
output [3:0]  be;
3871
output write;
3872 81 unneback
output read;
3873 75 unneback
output beginbursttransfer;
3874
output [3:0] burstcount;
3875
input readdatavalid;
3876
input waitrequest;
3877
input clk;
3878
input rst;
3879
wire [1:0] wbm_bte_o;
3880
wire [2:0] wbm_cti_o;
3881
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
3882
reg last_cyc;
3883 79 unneback
reg [3:0] counter;
3884 82 unneback
reg read_busy;
3885 75 unneback
always @ (posedge clk or posedge rst)
3886
if (rst)
3887
    last_cyc <= 1'b0;
3888
else
3889
    last_cyc <= wbm_cyc_o;
3890 79 unneback
always @ (posedge clk or posedge rst)
3891
if (rst)
3892 82 unneback
    read_busy <= 1'b0;
3893 79 unneback
else
3894 82 unneback
    if (read & !waitrequest)
3895
        read_busy <= 1'b1;
3896
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
3897
        read_busy <= 1'b0;
3898
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
3899 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
3900
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
3901
                    (wbm_bte_o==2'b10) ? 4'd8 :
3902 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
3903
                    4'd1;
3904 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
3905 79 unneback
always @ (posedge clk or posedge rst)
3906
if (rst) begin
3907
    counter <= 4'd0;
3908
end else
3909 80 unneback
    if (wbm_we_o) begin
3910
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
3911 85 unneback
            counter <= burstcount -4'd1;
3912 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
3913
            counter <= burstcount;
3914
        end else if (!waitrequest & wbm_stb_o) begin
3915
            counter <= counter - 4'd1;
3916
        end
3917 82 unneback
    end
3918 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
3919 77 unneback
vl_wb3wb3_bridge wbwb3inst (
3920 75 unneback
    // wishbone slave side
3921
    .wbs_dat_i(wbs_dat_i),
3922
    .wbs_adr_i(wbs_adr_i),
3923
    .wbs_sel_i(wbs_sel_i),
3924
    .wbs_bte_i(wbs_bte_i),
3925
    .wbs_cti_i(wbs_cti_i),
3926
    .wbs_we_i(wbs_we_i),
3927
    .wbs_cyc_i(wbs_cyc_i),
3928
    .wbs_stb_i(wbs_stb_i),
3929
    .wbs_dat_o(wbs_dat_o),
3930
    .wbs_ack_o(wbs_ack_o),
3931
    .wbs_clk(wbs_clk),
3932
    .wbs_rst(wbs_rst),
3933
    // wishbone master side
3934
    .wbm_dat_o(writedata),
3935 78 unneback
    .wbm_adr_o(address),
3936 75 unneback
    .wbm_sel_o(be),
3937
    .wbm_bte_o(wbm_bte_o),
3938
    .wbm_cti_o(wbm_cti_o),
3939
    .wbm_we_o(wbm_we_o),
3940
    .wbm_cyc_o(wbm_cyc_o),
3941
    .wbm_stb_o(wbm_stb_o),
3942
    .wbm_dat_i(readdata),
3943
    .wbm_ack_i(wbm_ack_i),
3944
    .wbm_clk(clk),
3945
    .wbm_rst(rst));
3946
endmodule
3947 49 unneback
// WB RAM with byte enable
3948 101 unneback
module vl_wb_ram (
3949 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,
3950 101 unneback
    wbs_dat_o, wbs_ack_o, wbs_stall_o, wb_clk, wb_rst);
3951
parameter adr_width = 16;
3952
parameter mem_size = 1<<adr_width;
3953
parameter dat_width = 32;
3954
parameter max_burst_width = 4; // only used for B3
3955
parameter mode = "B3"; // valid options: B3, B4
3956 60 unneback
parameter memory_init = 1;
3957
parameter memory_file = "vl_ram.vmem";
3958 101 unneback
input [dat_width-1:0] wbs_dat_i;
3959
input [adr_width-1:0] wbs_adr_i;
3960
input [2:0] wbs_cti_i;
3961
input [1:0] wbs_bte_i;
3962
input [dat_width/8-1:0] wbs_sel_i;
3963 70 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
3964 101 unneback
output [dat_width-1:0] wbs_dat_o;
3965 70 unneback
output wbs_ack_o;
3966 101 unneback
output wbs_stall_o;
3967 71 unneback
input wb_clk, wb_rst;
3968 101 unneback
wire [adr_width-1:0] adr;
3969
wire we;
3970
generate
3971
if (mode=="B3") begin : B3_inst
3972
vl_wb_adr_inc # ( .adr_width(adr_width), .max_burst_width(max_burst_width)) adr_inc0 (
3973 83 unneback
    .cyc_i(wbs_cyc_i),
3974
    .stb_i(wbs_stb_i),
3975
    .cti_i(wbs_cti_i),
3976
    .bte_i(wbs_bte_i),
3977
    .adr_i(wbs_adr_i),
3978 85 unneback
    .we_i(wbs_we_i),
3979 83 unneback
    .ack_o(wbs_ack_o),
3980
    .adr_o(adr),
3981
    .clk(wb_clk),
3982
    .rst(wb_rst));
3983 101 unneback
assign we = wbs_we_i & wbs_ack_o;
3984
end else if (mode=="B4") begin : B4_inst
3985
reg wbs_ack_o_reg;
3986
always @ (posedge wb_clk or posedge wb_rst)
3987
    if (wb_rst)
3988
        wbs_ack_o_reg <= 1'b0;
3989
    else
3990
        wbs_ack_o_reg <= wbs_stb_i & wbs_cyc_i;
3991
assign wbs_ack_o = wbs_ack_o_reg;
3992
assign wbs_stall_o = 1'b0;
3993
assign adr = wbs_adr_i;
3994
assign we = wbs_we_i & wbs_cyc_i & wbs_stb_i;
3995
end
3996
endgenerate
3997 100 unneback
vl_ram_be # (
3998
    .data_width(dat_width),
3999
    .addr_width(adr_width),
4000
    .mem_size(mem_size),
4001
    .memory_init(memory_init),
4002
    .memory_file(memory_file))
4003
ram0(
4004 101 unneback
    .d(wbs_dat_i),
4005
    .adr(adr),
4006
    .be(wbs_sel_i),
4007
    .we(we),
4008
    .q(wbs_dat_o),
4009 100 unneback
    .clk(wb_clk)
4010
);
4011 49 unneback
endmodule
4012 103 unneback
// A wishbone compliant RAM module that can be placed in front of other memory controllers
4013
module vl_wb_shadow_ram (
4014
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
4015
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
4016
    wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
4017
    wbm_dat_i, wbm_ack_i, wbm_stall_i,
4018
    wb_clk, wb_rst);
4019
parameter dat_width = 32;
4020
parameter mode = "B4";
4021
parameter max_burst_width = 4; // only used for B3
4022
parameter shadow_mem_adr_width = 10;
4023
parameter shadow_mem_size = 1024;
4024
parameter shadow_mem_init = 2;
4025
parameter shadow_mem_file = "vl_ram.v";
4026
parameter main_mem_adr_width = 24;
4027
input [dat_width-1:0] wbs_dat_i;
4028
input [main_mem_adr_width-1:0] wbs_adr_i;
4029
input [2:0] wbs_cti_i;
4030
input [1:0] wbs_bte_i;
4031
input [dat_width/8-1:0] wbs_sel_i;
4032
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
4033
output [dat_width-1:0] wbs_dat_o;
4034
output wbs_ack_o;
4035
output wbs_stall_o;
4036
output [dat_width-1:0] wbm_dat_o;
4037
output [main_mem_adr_width-1:0] wbm_adr_o;
4038
output [2:0] wbm_cti_o;
4039
output [1:0] wbm_bte_o;
4040
output [dat_width/8-1:0] wbm_sel_o;
4041
output wbm_we_o, wbm_stb_o, wbm_cyc_o;
4042
input [dat_width-1:0] wbm_dat_i;
4043
input wbm_ack_i, wbm_stall_i;
4044
input wb_clk, wb_rst;
4045
generate
4046
if (shadow_mem_size>0) begin : shadow_ram_inst
4047
wire cyc;
4048
wire [dat_width-1:0] dat;
4049
wire stall, ack;
4050
assign cyc = wbs_cyc_i & (wbs_adr_i<=shadow_mem_size);
4051
vl_wb_ram # (
4052
    .dat_width(dat_width),
4053
    .adr_width(shadow_mem_adr_width),
4054
    .mem_size(shadow_mem_size),
4055
    .memory_init(shadow_mem_init),
4056 117 unneback
    .memory_file(shadow_mem_file),
4057 103 unneback
    .mode(mode))
4058
shadow_mem0 (
4059
    .wbs_dat_i(wbs_dat_i),
4060
    .wbs_adr_i(wbs_adr_i[shadow_mem_adr_width-1:0]),
4061
    .wbs_sel_i(wbs_sel_i),
4062
    .wbs_we_i (wbs_we_i),
4063
    .wbs_bte_i(wbs_bte_i),
4064
    .wbs_cti_i(wbs_cti_i),
4065
    .wbs_stb_i(wbs_stb_i),
4066
    .wbs_cyc_i(cyc),
4067
    .wbs_dat_o(dat),
4068
    .wbs_stall_o(stall),
4069
    .wbs_ack_o(ack),
4070
    .wb_clk(wb_clk),
4071
    .wb_rst(wb_rst));
4072
assign {wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o} =
4073
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i};
4074
assign wbm_cyc_o = wbs_cyc_i & (wbs_adr_i>shadow_mem_size);
4075
assign wbs_dat_o = (dat & {dat_width{cyc}}) | (wbm_dat_i & {dat_width{wbm_cyc_o}});
4076
assign wbs_ack_o = (ack & cyc) | (wbm_ack_i & wbm_cyc_o);
4077
assign wbs_stall_o = (stall & cyc) | (wbm_stall_i & wbm_cyc_o);
4078
end else begin : no_shadow_ram_inst
4079
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} =
4080
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i};
4081
assign {wbs_dat_o, wbs_ack_o, wbs_stall_o} = {wbm_dat_i, wbm_ack_i, wbm_stall_i};
4082
end
4083
endgenerate
4084
endmodule
4085 17 unneback
// WB ROM
4086 48 unneback
module vl_wb_b4_rom (
4087
    wb_adr_i, wb_stb_i, wb_cyc_i,
4088
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
4089
    parameter dat_width = 32;
4090
    parameter dat_default = 32'h15000000;
4091
    parameter adr_width = 32;
4092
/*
4093
`ifndef ROM
4094
`define ROM "rom.v"
4095
`endif
4096
*/
4097
    input [adr_width-1:2]   wb_adr_i;
4098
    input                   wb_stb_i;
4099
    input                   wb_cyc_i;
4100
    output [dat_width-1:0]  wb_dat_o;
4101
    reg [dat_width-1:0]     wb_dat_o;
4102
    output                  wb_ack_o;
4103
    reg                     wb_ack_o;
4104
    output                  stall_o;
4105
    input                   wb_clk;
4106
    input                   wb_rst;
4107
always @ (posedge wb_clk or posedge wb_rst)
4108
    if (wb_rst)
4109
        wb_dat_o <= {dat_width{1'b0}};
4110
    else
4111
         case (wb_adr_i[adr_width-1:2])
4112
`ifdef ROM
4113
`include `ROM
4114
`endif
4115
           default:
4116
             wb_dat_o <= dat_default;
4117
         endcase // case (wb_adr_i)
4118
always @ (posedge wb_clk or posedge wb_rst)
4119
    if (wb_rst)
4120
        wb_ack_o <= 1'b0;
4121
    else
4122
        wb_ack_o <= wb_stb_i & wb_cyc_i;
4123
assign stall_o = 1'b0;
4124
endmodule
4125
// WB ROM
4126 18 unneback
module vl_wb_boot_rom (
4127 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
4128 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
4129
    parameter adr_hi = 31;
4130
    parameter adr_lo = 28;
4131
    parameter adr_sel = 4'hf;
4132
    parameter addr_width = 5;
4133 33 unneback
/*
4134
`ifndef BOOT_ROM
4135
`define BOOT_ROM "boot_rom.v"
4136
`endif
4137
*/
4138 18 unneback
    input [adr_hi:2]    wb_adr_i;
4139
    input               wb_stb_i;
4140
    input               wb_cyc_i;
4141
    output [31:0]        wb_dat_o;
4142
    output              wb_ack_o;
4143
    output              hit_o;
4144
    input               wb_clk;
4145
    input               wb_rst;
4146
    wire hit;
4147
    reg [31:0] wb_dat;
4148
    reg wb_ack;
4149
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
4150 17 unneback
always @ (posedge wb_clk or posedge wb_rst)
4151
    if (wb_rst)
4152 18 unneback
        wb_dat <= 32'h15000000;
4153 17 unneback
    else
4154 18 unneback
         case (wb_adr_i[addr_width-1:2])
4155 33 unneback
`ifdef BOOT_ROM
4156
`include `BOOT_ROM
4157
`endif
4158 17 unneback
           /*
4159
            // Zero r0 and jump to 0x00000100
4160 18 unneback
 
4161
            1 : wb_dat <= 32'hA8200000;
4162
            2 : wb_dat <= 32'hA8C00100;
4163
            3 : wb_dat <= 32'h44003000;
4164
            4 : wb_dat <= 32'h15000000;
4165 17 unneback
            */
4166
           default:
4167 18 unneback
             wb_dat <= 32'h00000000;
4168 17 unneback
         endcase // case (wb_adr_i)
4169
always @ (posedge wb_clk or posedge wb_rst)
4170
    if (wb_rst)
4171 18 unneback
        wb_ack <= 1'b0;
4172 17 unneback
    else
4173 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
4174
assign hit_o = hit;
4175
assign wb_dat_o = wb_dat & {32{wb_ack}};
4176
assign wb_ack_o = wb_ack;
4177 17 unneback
endmodule
4178 106 unneback
module vl_wb_dpram (
4179
        // wishbone slave side a
4180
        wbsa_dat_i, wbsa_adr_i, wbsa_sel_i, wbsa_cti_i, wbsa_bte_i, wbsa_we_i, wbsa_cyc_i, wbsa_stb_i, wbsa_dat_o, wbsa_ack_o, wbsa_stall_o,
4181
        wbsa_clk, wbsa_rst,
4182
        // wishbone slave side b
4183
        wbsb_dat_i, wbsb_adr_i, wbsb_sel_i, wbsb_cti_i, wbsb_bte_i, wbsb_we_i, wbsb_cyc_i, wbsb_stb_i, wbsb_dat_o, wbsb_ack_o, wbsb_stall_o,
4184
        wbsb_clk, wbsb_rst);
4185
parameter data_width_a = 32;
4186
parameter data_width_b = data_width_a;
4187
parameter addr_width_a = 8;
4188
localparam addr_width_b = data_width_a * addr_width_a / data_width_b;
4189
parameter mem_size = (addr_width_a>addr_width_b) ? (1<<addr_width_a) : (1<<addr_width_b);
4190
parameter max_burst_width_a = 4;
4191
parameter max_burst_width_b = max_burst_width_a;
4192
parameter mode = "B3";
4193 109 unneback
parameter memory_init = 0;
4194
parameter memory_file = "vl_ram.v";
4195 106 unneback
input [data_width_a-1:0] wbsa_dat_i;
4196
input [addr_width_a-1:0] wbsa_adr_i;
4197
input [data_width_a/8-1:0] wbsa_sel_i;
4198
input [2:0] wbsa_cti_i;
4199
input [1:0] wbsa_bte_i;
4200
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
4201
output [data_width_a-1:0] wbsa_dat_o;
4202 109 unneback
output wbsa_ack_o;
4203 106 unneback
output wbsa_stall_o;
4204
input wbsa_clk, wbsa_rst;
4205
input [data_width_b-1:0] wbsb_dat_i;
4206
input [addr_width_b-1:0] wbsb_adr_i;
4207
input [data_width_b/8-1:0] wbsb_sel_i;
4208
input [2:0] wbsb_cti_i;
4209
input [1:0] wbsb_bte_i;
4210
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
4211
output [data_width_b-1:0] wbsb_dat_o;
4212 109 unneback
output wbsb_ack_o;
4213 106 unneback
output wbsb_stall_o;
4214
input wbsb_clk, wbsb_rst;
4215
wire [addr_width_a-1:0] adr_a;
4216
wire [addr_width_b-1:0] adr_b;
4217
wire we_a, we_b;
4218
generate
4219
if (mode=="B3") begin : b3_inst
4220
vl_wb_adr_inc # ( .adr_width(addr_width_a), .max_burst_width(max_burst_width_a)) adr_inc0 (
4221
    .cyc_i(wbsa_cyc_i),
4222
    .stb_i(wbsa_stb_i),
4223
    .cti_i(wbsa_cti_i),
4224
    .bte_i(wbsa_bte_i),
4225
    .adr_i(wbsa_adr_i),
4226
    .we_i(wbsa_we_i),
4227
    .ack_o(wbsa_ack_o),
4228
    .adr_o(adr_a),
4229
    .clk(wbsa_clk),
4230
    .rst(wbsa_rst));
4231
assign we_a = wbsa_we_i & wbsa_ack_o;
4232
vl_wb_adr_inc # ( .adr_width(addr_width_b), .max_burst_width(max_burst_width_b)) adr_inc1 (
4233
    .cyc_i(wbsb_cyc_i),
4234
    .stb_i(wbsb_stb_i),
4235
    .cti_i(wbsb_cti_i),
4236
    .bte_i(wbsb_bte_i),
4237
    .adr_i(wbsb_adr_i),
4238
    .we_i(wbsb_we_i),
4239
    .ack_o(wbsb_ack_o),
4240
    .adr_o(adr_b),
4241
    .clk(wbsb_clk),
4242
    .rst(wbsb_rst));
4243
assign we_b = wbsb_we_i & wbsb_ack_o;
4244
end else if (mode=="B4") begin : b4_inst
4245 109 unneback
vl_dff dffacka ( .d(wbsa_stb_i & wbsa_cyc_i), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
4246 106 unneback
assign wbsa_stall_o = 1'b0;
4247
assign we_a = wbsa_we_i & wbsa_cyc_i & wbsa_stb_i;
4248 109 unneback
vl_dff dffackb ( .d(wbsb_stb_i & wbsb_cyc_i), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
4249 106 unneback
assign wbsb_stall_o = 1'b0;
4250
assign we_b = wbsb_we_i & wbsb_cyc_i & wbsb_stb_i;
4251
end
4252
endgenerate
4253 109 unneback
vl_dpram_be_2r2w # ( .a_data_width(data_width_a), .a_addr_width(addr_width_a), .mem_size(mem_size),
4254 110 unneback
                 .b_data_width(data_width_b),
4255 109 unneback
                 .memory_init(memory_init), .memory_file(memory_file))
4256 106 unneback
ram_i (
4257
    .d_a(wbsa_dat_i),
4258
    .q_a(wbsa_dat_o),
4259
    .adr_a(adr_a),
4260
    .be_a(wbsa_sel_i),
4261
    .we_a(we_a),
4262
    .clk_a(wbsa_clk),
4263
    .d_b(wbsb_dat_i),
4264
    .q_b(wbsb_dat_o),
4265
    .adr_b(adr_b),
4266
    .be_b(wbsb_sel_i),
4267
    .we_b(we_b),
4268
    .clk_b(wbsb_clk) );
4269
endmodule
4270 101 unneback
module vl_wb_cache (
4271 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,
4272 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
4273 97 unneback
);
4274
parameter dw_s = 32;
4275
parameter aw_s = 24;
4276
parameter dw_m = dw_s;
4277 124 unneback
//localparam aw_m = dw_s * aw_s / dw_m;
4278
localparam aw_m =
4279 126 unneback
        (dw_s==dw_m) ? aw_s :
4280
        (dw_s==dw_m*2) ? aw_s+1 :
4281
        (dw_s==dw_m*4) ? aw_s+2 :
4282
        (dw_s==dw_m*8) ? aw_s+3 :
4283
        (dw_s==dw_m*16) ? aw_s+4 :
4284
        (dw_s==dw_m*32) ? aw_s+5 :
4285
        (dw_s==dw_m/2) ? aw_s-1 :
4286 127 unneback
        (dw_s==dw_m/4) ? aw_s-2 :
4287 126 unneback
        (dw_s==dw_m/8) ? aw_s-3 :
4288
        (dw_s==dw_m/16) ? aw_s-4 :
4289
        (dw_s==dw_m/32) ? aw_s-5 : 0;
4290 100 unneback
parameter wbs_max_burst_width = 4;
4291 103 unneback
parameter wbs_mode = "B3";
4292 97 unneback
parameter async = 1; // wbs_clk != wbm_clk
4293
parameter nr_of_ways = 1;
4294
parameter aw_offset = 4; // 4 => 16 words per cache line
4295
parameter aw_slot = 10;
4296 100 unneback
parameter valid_mem = 0;
4297
parameter debug = 0;
4298
localparam aw_b_offset = aw_offset * dw_s / dw_m;
4299 98 unneback
localparam aw_tag = aw_s - aw_slot - aw_offset;
4300 97 unneback
parameter wbm_burst_size = 4; // valid options 4,8,16
4301 98 unneback
localparam bte = (wbm_burst_size==4) ? 2'b01 : (wbm_burst_size==8) ? 2'b10 : 2'b11;
4302 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;
4303 97 unneback
localparam nr_of_wbm_burst = ((1<<aw_offset)/wbm_burst_size) * dw_s / dw_m;
4304 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;
4305 97 unneback
input [dw_s-1:0] wbs_dat_i;
4306
input [aw_s-1:0] wbs_adr_i; // dont include a1,a0
4307 98 unneback
input [dw_s/8-1:0] wbs_sel_i;
4308 97 unneback
input [2:0] wbs_cti_i;
4309
input [1:0] wbs_bte_i;
4310 98 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
4311 97 unneback
output [dw_s-1:0] wbs_dat_o;
4312
output wbs_ack_o;
4313 103 unneback
output wbs_stall_o;
4314 97 unneback
input wbs_clk, wbs_rst;
4315
output [dw_m-1:0] wbm_dat_o;
4316
output [aw_m-1:0] wbm_adr_o;
4317
output [dw_m/8-1:0] wbm_sel_o;
4318
output [2:0] wbm_cti_o;
4319
output [1:0] wbm_bte_o;
4320 98 unneback
output wbm_stb_o, wbm_cyc_o, wbm_we_o;
4321 97 unneback
input [dw_m-1:0] wbm_dat_i;
4322
input wbm_ack_i;
4323
input wbm_stall_i;
4324
input wbm_clk, wbm_rst;
4325 100 unneback
wire valid, dirty, hit;
4326 97 unneback
wire [aw_tag-1:0] tag;
4327
wire tag_mem_we;
4328
wire [aw_tag-1:0] wbs_adr_tag;
4329
wire [aw_slot-1:0] wbs_adr_slot;
4330 98 unneback
wire [aw_offset-1:0] wbs_adr_word;
4331
wire [aw_s-1:0] wbs_adr;
4332 97 unneback
reg [1:0] state;
4333
localparam idle = 2'h0;
4334
localparam rdwr = 2'h1;
4335
localparam push = 2'h2;
4336
localparam pull = 2'h3;
4337
wire eoc;
4338 103 unneback
wire we;
4339 97 unneback
// cdc
4340
wire done, mem_alert, mem_done;
4341 98 unneback
// wbm side
4342
reg [aw_m-1:0] wbm_radr;
4343
reg [aw_m-1:0] wbm_wadr;
4344 137 unneback
//wire [aw_slot-1:0] wbm_adr;
4345
wire [aw_m-1:0] wbm_adr;
4346 98 unneback
wire wbm_radr_cke, wbm_wadr_cke;
4347 100 unneback
reg [2:0] phase;
4348
// phase = {we,stb,cyc}
4349
localparam wbm_wait     = 3'b000;
4350
localparam wbm_wr       = 3'b111;
4351
localparam wbm_wr_drain = 3'b101;
4352
localparam wbm_rd       = 3'b011;
4353
localparam wbm_rd_drain = 3'b001;
4354 97 unneback
assign {wbs_adr_tag, wbs_adr_slot, wbs_adr_word} = wbs_adr_i;
4355 100 unneback
generate
4356
if (valid_mem==0) begin : no_valid_mem
4357
assign valid = 1'b1;
4358
end else begin : valid_mem_inst
4359
vl_dpram_1r1w
4360
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
4361
    valid_mem ( .d_a(1'b1), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
4362
                .q_b(valid), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
4363
end
4364
endgenerate
4365
vl_dpram_1r1w
4366
    # ( .data_width(aw_tag), .addr_width(aw_slot), .memory_init(2), .debug(debug))
4367
    tag_mem ( .d_a(wbs_adr_tag), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
4368
              .q_b(tag), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
4369
assign hit = wbs_adr_tag == tag;
4370
vl_dpram_1r2w
4371
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
4372
    dirty_mem (
4373
        .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),
4374
        .d_b(1'b0), .adr_b(wbs_adr_slot), .we_b(mem_done), .clk_b(wbm_clk));
4375 103 unneback
generate
4376
if (wbs_mode=="B3") begin : inst_b3
4377 100 unneback
vl_wb_adr_inc # ( .adr_width(aw_s), .max_burst_width(wbs_max_burst_width)) adr_inc0 (
4378
    .cyc_i(wbs_cyc_i & (state==rdwr) & hit & valid),
4379
    .stb_i(wbs_stb_i & (state==rdwr) & hit & valid), // throttle depending on valid
4380 97 unneback
    .cti_i(wbs_cti_i),
4381
    .bte_i(wbs_bte_i),
4382
    .adr_i(wbs_adr_i),
4383
    .we_i (wbs_we_i),
4384
    .ack_o(wbs_ack_o),
4385
    .adr_o(wbs_adr),
4386 100 unneback
    .clk(wbs_clk),
4387
    .rst(wbs_rst));
4388 103 unneback
assign eoc = (wbs_cti_i==3'b000 | wbs_cti_i==3'b111) & wbs_ack_o;
4389
assign we = wbs_cyc_i &  wbs_we_i & wbs_ack_o;
4390
end else if (wbs_mode=="B4") begin : inst_b4
4391
end
4392
endgenerate
4393 131 unneback
localparam cache_mem_b_aw =
4394
    (dw_s==dw_m) ? aw_slot+aw_offset :
4395 133 unneback
    (dw_s==dw_m/2) ? aw_slot+aw_offset-1 :
4396
    (dw_s==dw_m/4) ? aw_slot+aw_offset-2 :
4397
    (dw_s==dw_m/8) ? aw_slot+aw_offset-3 :
4398
    (dw_s==dw_m/16) ? aw_slot+aw_offset-4 :
4399
    (dw_s==dw_m*2) ? aw_slot+aw_offset+1 :
4400
    (dw_s==dw_m*4) ? aw_slot+aw_offset+2 :
4401
    (dw_s==dw_m*8) ? aw_slot+aw_offset+3 :
4402
    (dw_s==dw_m*16) ? aw_slot+aw_offset+4 : 0;
4403 97 unneback
vl_dpram_be_2r2w
4404 100 unneback
    # ( .a_data_width(dw_s), .a_addr_width(aw_slot+aw_offset), .b_data_width(dw_m), .debug(debug))
4405 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),
4406 136 unneback
                .d_b(wbm_dat_i), .adr_b(wbm_adr[cache_mem_b_aw-1:0]), .be_b(wbm_sel_o), .we_b(wbm_cyc_o & !wbm_we_o & wbm_ack_i), .q_b(wbm_dat_o), .clk_b(wbm_clk));
4407 97 unneback
always @ (posedge wbs_clk or posedge wbs_rst)
4408
if (wbs_rst)
4409 98 unneback
    state <= idle;
4410 97 unneback
else
4411
    case (state)
4412
    idle:
4413
        if (wbs_cyc_i)
4414
            state <= rdwr;
4415
    rdwr:
4416 100 unneback
        casex ({valid, hit, dirty, eoc})
4417
        4'b0xxx: state <= pull;
4418
        4'b11x1: state <= idle;
4419
        4'b101x: state <= push;
4420
        4'b100x: state <= pull;
4421
        endcase
4422 97 unneback
    push:
4423
        if (done)
4424
            state <= rdwr;
4425
    pull:
4426
        if (done)
4427
            state <= rdwr;
4428
    default: state <= idle;
4429
    endcase
4430
// cdc
4431
generate
4432
if (async==1) begin : cdc0
4433 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));
4434 97 unneback
end
4435
else begin : nocdc
4436 100 unneback
    assign mem_alert = state==rdwr & (!valid | !hit);
4437 97 unneback
    assign done = mem_done;
4438
end
4439
endgenerate
4440 136 unneback
// FSM generating a number of bursts 4 cycles
4441 97 unneback
// actual number depends on data width ratio
4442
// nr_of_wbm_burst
4443 101 unneback
reg [nr_of_wbm_burst_width+wbm_burst_width-1:0]       cnt_rw, cnt_ack;
4444 97 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
4445
if (wbm_rst)
4446 100 unneback
    cnt_rw <= {wbm_burst_width{1'b0}};
4447 97 unneback
else
4448 100 unneback
    if (wbm_cyc_o & wbm_stb_o & !wbm_stall_i)
4449
        cnt_rw <= cnt_rw + 1;
4450 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
4451
if (wbm_rst)
4452 100 unneback
    cnt_ack <= {wbm_burst_width{1'b0}};
4453 98 unneback
else
4454 100 unneback
    if (wbm_ack_i)
4455
        cnt_ack <= cnt_ack + 1;
4456
generate
4457 101 unneback
if (nr_of_wbm_burst==1) begin : one_burst
4458 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
4459
if (wbm_rst)
4460
    phase <= wbm_wait;
4461
else
4462
    case (phase)
4463
    wbm_wait:
4464
        if (mem_alert)
4465 100 unneback
            if (state==push)
4466
                phase <= wbm_wr;
4467
            else
4468
                phase <= wbm_rd;
4469 98 unneback
    wbm_wr:
4470 100 unneback
        if (&cnt_rw)
4471
            phase <= wbm_wr_drain;
4472
    wbm_wr_drain:
4473
        if (&cnt_ack)
4474 98 unneback
            phase <= wbm_rd;
4475
    wbm_rd:
4476 100 unneback
        if (&cnt_rw)
4477
            phase <= wbm_rd_drain;
4478
    wbm_rd_drain:
4479
        if (&cnt_ack)
4480
            phase <= wbm_wait;
4481 98 unneback
    default: phase <= wbm_wait;
4482
    endcase
4483 100 unneback
end else begin : multiple_burst
4484 101 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
4485
if (wbm_rst)
4486
    phase <= wbm_wait;
4487
else
4488
    case (phase)
4489
    wbm_wait:
4490
        if (mem_alert)
4491
            if (state==push)
4492
                phase <= wbm_wr;
4493
            else
4494
                phase <= wbm_rd;
4495
    wbm_wr:
4496
        if (&cnt_rw[wbm_burst_width-1:0])
4497
            phase <= wbm_wr_drain;
4498
    wbm_wr_drain:
4499
        if (&cnt_ack)
4500
            phase <= wbm_rd;
4501
        else if (&cnt_ack[wbm_burst_width-1:0])
4502
            phase <= wbm_wr;
4503
    wbm_rd:
4504
        if (&cnt_rw[wbm_burst_width-1:0])
4505
            phase <= wbm_rd_drain;
4506
    wbm_rd_drain:
4507
        if (&cnt_ack)
4508
            phase <= wbm_wait;
4509
        else if (&cnt_ack[wbm_burst_width-1:0])
4510
            phase <= wbm_rd;
4511
    default: phase <= wbm_wait;
4512
    endcase
4513 100 unneback
end
4514
endgenerate
4515 101 unneback
assign mem_done = phase==wbm_rd_drain & (&cnt_ack) & wbm_ack_i;
4516 100 unneback
assign wbm_adr_o = (phase[2]) ? {tag, wbs_adr_slot, cnt_rw} : {wbs_adr_tag, wbs_adr_slot, cnt_rw};
4517 137 unneback
assign wbm_adr   = (phase[2]) ? {wbs_adr_slot, cnt_rw} : {wbs_adr_slot, cnt_ack};
4518 100 unneback
assign wbm_sel_o = {dw_m/8{1'b1}};
4519
assign wbm_cti_o = (&cnt_rw | !wbm_stb_o) ? 3'b111 : 3'b010;
4520 98 unneback
assign wbm_bte_o = bte;
4521 100 unneback
assign {wbm_we_o, wbm_stb_o, wbm_cyc_o}  = phase;
4522 97 unneback
endmodule
4523 103 unneback
// Wishbone to avalon bridge supporting one type of burst transfer only
4524
// intended use is together with cache above
4525
// WB B4 -> pipelined avalon
4526
module vl_wb_avalon_bridge (
4527
        // wishbone slave side
4528
        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,
4529
        // avalon master side
4530
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer,
4531 136 unneback
        init_done,
4532 103 unneback
        // common
4533
        clk, rst);
4534
parameter adr_width = 30;
4535
parameter dat_width = 32;
4536
parameter burst_size = 4;
4537
input [dat_width-1:0] wbs_dat_i;
4538
input [adr_width-1:0] wbs_adr_i;
4539
input [dat_width/8-1:0]  wbs_sel_i;
4540
input [1:0]  wbs_bte_i;
4541
input [2:0]  wbs_cti_i;
4542
input wbs_we_i;
4543
input wbs_cyc_i;
4544
input wbs_stb_i;
4545 130 unneback
output [dat_width-1:0] wbs_dat_o;
4546 103 unneback
output wbs_ack_o;
4547
output wbs_stall_o;
4548
input [dat_width-1:0] readdata;
4549
input readdatavalid;
4550
output [dat_width-1:0] writedata;
4551
output [adr_width-1:0] address;
4552
output [dat_width/8-1:0]  be;
4553
output write;
4554
output read;
4555
output beginbursttransfer;
4556
output [3:0] burstcount;
4557
input waitrequest;
4558 136 unneback
input init_done;
4559 103 unneback
input clk, rst;
4560 136 unneback
// cnt1 - initiated read or writes
4561
// cnt2 - # of read or writes in pipeline
4562
reg [3:0] cnt1;
4563
reg [3:0] cnt2;
4564
reg next_state, state;
4565
localparam s0 = 1'b0;
4566
localparam s1 = 1'b1;
4567
wire eoc;
4568
always @ *
4569
begin
4570
    case (state)
4571
    s0: if (init_done & wbs_cyc_i) next_state <= s1;
4572
    s1:
4573
    default: next_state <= state;
4574
    end
4575
end
4576 103 unneback
always @ (posedge clk or posedge rst)
4577
if (rst)
4578 136 unneback
    state <= s0;
4579 103 unneback
else
4580 136 unneback
    state <= next_state;
4581
assign eoc = state==s1 & !(read | write) & (& !waitrequest & cnt2=;
4582
always @ (posedge clk or posedge rst)
4583
if (rst)
4584
    cnt1 <= 4'h0;
4585
else
4586
    if (read & !waitrequest & init_done)
4587
        cnt1 <= burst_size - 1;
4588
    else if (write & !waitrequest & init_done)
4589
        cnt1 <= cnt1 + 4'h1;
4590
    else if (next_state==idle)
4591
        cnt1 <= 4'h0;
4592
always @ (posedge clk or posedge rst)
4593
if (rst)
4594
    cnt2 <= 4'h0;
4595
else
4596
    if (read & !waitrequest & init_done)
4597
        cnt2 <= burst_size - 1;
4598
    else if (write & !waitrequest & init_done & )
4599
        cnt2 <= cnt1 + 4'h1;
4600
    else if (next_state==idle)
4601
        cnt2 <= 4'h0;
4602 103 unneback
reg wr_ack;
4603
always @ (posedge clk or posedge rst)
4604
if (rst)
4605
    wr_ack <= 1'b0;
4606
else
4607
    wr_ack <=  (wbs_we_i & wbs_cyc_i & wbs_stb_i & !wbs_stall_o);
4608
// to avalon
4609
assign writedata = wbs_dat_i;
4610
assign address = wbs_adr_i;
4611
assign be = wbs_sel_i;
4612 136 unneback
assign write = cnt!=4'h0 & wbs_cyc_i &  wbs_we_i;
4613
assign read  = cnt!=4'h0 & wbs_cyc_i & !wbs_we_i;
4614
assign beginbursttransfer = state==s0 & next_state==s1;
4615 103 unneback
assign burstcount = burst_size;
4616
// to wishbone
4617
assign wbs_dat_o = readdata;
4618
assign wbs_ack_o = wr_ack | readdatavalid;
4619
assign wbs_stall_o = waitrequest;
4620
endmodule
4621
module vl_wb_avalon_mem_cache (
4622
    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,
4623
    readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst
4624
);
4625
// wishbone
4626
parameter wb_dat_width = 32;
4627
parameter wb_adr_width = 22;
4628
parameter wb_max_burst_width = 4;
4629
parameter wb_mode = "B4";
4630
// avalon
4631
parameter avalon_dat_width = 32;
4632 121 unneback
//localparam avalon_adr_width = wb_dat_width * wb_adr_width / avalon_dat_width;
4633 122 unneback
localparam avalon_adr_width =
4634
        (wb_dat_width==avalon_dat_width) ? wb_adr_width :
4635
        (wb_dat_width==avalon_dat_width*2) ? wb_adr_width+1 :
4636
        (wb_dat_width==avalon_dat_width*4) ? wb_adr_width+2 :
4637
        (wb_dat_width==avalon_dat_width*8) ? wb_adr_width+3 :
4638
        (wb_dat_width==avalon_dat_width*16) ? wb_adr_width+4 :
4639
        (wb_dat_width==avalon_dat_width*32) ? wb_adr_width+5 :
4640
        (wb_dat_width==avalon_dat_width/2) ? wb_adr_width-1 :
4641
        (wb_dat_width==avalon_dat_width/4) ? wb_adr_width-2 :
4642
        (wb_dat_width==avalon_dat_width/8) ? wb_adr_width-3 :
4643
        (wb_dat_width==avalon_dat_width/16) ? wb_adr_width-4 :
4644 123 unneback
        (wb_dat_width==avalon_dat_width/32) ? wb_adr_width-5 : 0;
4645 103 unneback
parameter avalon_burst_size = 4;
4646
// cache
4647
parameter async = 1;
4648
parameter nr_of_ways = 1;
4649
parameter aw_offset = 4;
4650
parameter aw_slot = 10;
4651
parameter valid_mem = 1;
4652
// shadow RAM
4653
parameter shadow_ram = 0;
4654
parameter shadow_ram_adr_width = 10;
4655
parameter shadow_ram_size = 1024;
4656
parameter shadow_ram_init = 2; // 0: no init, 1: from file, 2: with zero
4657
parameter shadow_ram_file = "vl_ram.v";
4658
input [wb_dat_width-1:0] wbs_dat_i;
4659
input [wb_adr_width-1:0] wbs_adr_i; // dont include a1,a0
4660
input [wb_dat_width/8-1:0] wbs_sel_i;
4661
input [2:0] wbs_cti_i;
4662
input [1:0] wbs_bte_i;
4663
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
4664
output [wb_dat_width-1:0] wbs_dat_o;
4665
output wbs_ack_o;
4666
output wbs_stall_o;
4667
input wbs_clk, wbs_rst;
4668
input [avalon_dat_width-1:0] readdata;
4669
input readdatavalid;
4670
output [avalon_dat_width-1:0] writedata;
4671
output [avalon_adr_width-1:0] address;
4672
output [avalon_dat_width/8-1:0]  be;
4673
output write;
4674
output read;
4675
output beginbursttransfer;
4676
output [3:0] burstcount;
4677
input waitrequest;
4678
input clk, rst;
4679
wire [wb_dat_width-1:0] wb1_dat_o;
4680
wire [wb_adr_width-1:0] wb1_adr_o;
4681
wire [wb_dat_width/8-1:0] wb1_sel_o;
4682
wire [2:0] wb1_cti_o;
4683
wire [1:0] wb1_bte_o;
4684
wire wb1_we_o;
4685
wire wb1_stb_o;
4686
wire wb1_cyc_o;
4687
wire wb1_stall_i;
4688
wire [wb_dat_width-1:0] wb1_dat_i;
4689
wire wb1_ack_i;
4690 129 unneback
wire [avalon_dat_width-1:0] wb2_dat_o;
4691
wire [avalon_adr_width-1:0] wb2_adr_o;
4692
wire [avalon_dat_width/8-1:0] wb2_sel_o;
4693 103 unneback
wire [2:0] wb2_cti_o;
4694
wire [1:0] wb2_bte_o;
4695
wire wb2_we_o;
4696
wire wb2_stb_o;
4697
wire wb2_cyc_o;
4698
wire wb2_stall_i;
4699 129 unneback
wire [avalon_dat_width-1:0] wb2_dat_i;
4700 103 unneback
wire wb2_ack_i;
4701
vl_wb_shadow_ram # ( .dat_width(wb_dat_width), .mode(wb_mode), .max_burst_width(wb_max_burst_width),
4702 120 unneback
                 .shadow_mem_adr_width(shadow_ram_adr_width), .shadow_mem_size(shadow_ram_size), .shadow_mem_init(shadow_ram_init), .shadow_mem_file(shadow_ram_file),
4703 103 unneback
                 .main_mem_adr_width(wb_adr_width))
4704
shadow_ram0 (
4705
    .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),
4706
    .wbs_dat_o(wbs_dat_o), .wbs_ack_o(wbs_ack_o), .wbs_stall_o(wbs_stall_o),
4707
    .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),
4708
    .wbm_dat_i(wb1_dat_i), .wbm_ack_i(wb1_ack_i), .wbm_stall_i(wb1_stall_i),
4709
    .wb_clk(wbs_clk), .wb_rst(wbs_rst));
4710
vl_wb_cache
4711
# ( .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))
4712
cache0 (
4713
    .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),
4714
    .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),
4715
    .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),
4716
    .wbm_dat_i(wb2_dat_i), .wbm_ack_i(wb2_ack_i), .wbm_stall_i(wb2_stall_i), .wbm_clk(clk), .wbm_rst(rst));
4717
vl_wb_avalon_bridge # ( .adr_width(avalon_adr_width), .dat_width(avalon_dat_width), .burst_size(avalon_burst_size))
4718
bridge0 (
4719
        // wishbone slave side
4720
        .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),
4721
        .wbs_dat_o(wb2_dat_i), .wbs_ack_o(wb2_ack_i), .wbs_stall_o(wb2_stall_i),
4722
        // avalon master side
4723
        .readdata(readdata), .readdatavalid(readdatavalid), .address(address), .read(read), .be(be), .write(write), .burstcount(burstcount), .writedata(writedata), .waitrequest(waitrequest), .beginbursttransfer(beginbursttransfer),
4724
        // common
4725
        .clk(clk), .rst(rst));
4726
endmodule
4727 136 unneback
module vl_wb_sdr_sdram (
4728
    // wisbone i/f
4729
    dat_i, adr_i, sel_i, we_i, cyc_i, stb_i, dat_o, ack_o, stall_o,
4730
    // SDR SDRAM
4731
    ba, a, cmd, cke, cs_n, dqm, dq_i, dq_o, dq_oe,
4732
    // system
4733
    clk, rst);
4734
    // external data bus size
4735
    parameter dat_size = 16;
4736
    // memory geometry parameters
4737
    parameter ba_size  = 2;
4738
    parameter row_size = 13;
4739
    parameter col_size = 9;
4740
    parameter cl = 2;
4741
    // memory timing parameters
4742
    parameter tRFC = 9;
4743
    parameter tRP  = 2;
4744
    parameter tRCD = 2;
4745
    parameter tMRD = 2;
4746
    // LMR
4747
    // [12:10] reserved
4748
    // [9]     WB, write burst; 0 - programmed burst length, 1 - single location
4749
    // [8:7]   OP Mode, 2'b00
4750
    // [6:4]   CAS Latency; 3'b010 - 2, 3'b011 - 3
4751
    // [3]     BT, Burst Type; 1'b0 - sequential, 1'b1 - interleaved
4752
    // [2:0]   Burst length; 3'b000 - 1, 3'b001 - 2, 3'b010 - 4, 3'b011 - 8, 3'b111 - full page
4753
    localparam init_wb = 1'b1;
4754
    localparam init_cl = (cl==2) ? 3'b010 : 3'b011;
4755
    localparam init_bt = 1'b0;
4756
    localparam init_bl = 3'b000;
4757
    input [dat_size-1:0] dat_i;
4758
    input [ba_size+col_size+row_size-1:0] adr_i;
4759
    input [dat_size/8-1:0] sel_i;
4760
    input we_i, cyc_i, stb_i;
4761
    output [dat_size-1:0] dat_o;
4762
    output ack_o;
4763
    output reg stall_o;
4764
    output [ba_size-1:0]    ba;
4765
    output reg [12:0]   a;
4766
    output reg [2:0]    cmd; // {ras,cas,we}
4767
    output cke, cs_n;
4768
    output reg [dat_size/8-1:0]    dqm;
4769
    output [dat_size-1:0]       dq_o;
4770
    output reg          dq_oe;
4771
    input  [dat_size-1:0]       dq_i;
4772
    input clk, rst;
4773
    wire [ba_size-1:0]   bank;
4774
    wire [row_size-1:0] row;
4775
    wire [col_size-1:0] col;
4776
    wire [0:31]  shreg;
4777
    wire                ref_cnt_zero;
4778
    reg                 refresh_req;
4779
    wire ack_rd, rd_ack_emptyflag;
4780
    wire ack_wr;
4781
    // to keep track of open rows per bank
4782
    reg [row_size-1:0]   open_row[0:3];
4783
    reg [0:3]            open_ba;
4784
    reg                 current_bank_closed, current_row_open;
4785
    parameter rfr_length = 10;
4786
    parameter rfr_wrap_value = 1010;
4787
    parameter [2:0] cmd_nop = 3'b111,
4788
                    cmd_act = 3'b011,
4789
                    cmd_rd  = 3'b101,
4790
                    cmd_wr  = 3'b100,
4791
                    cmd_pch = 3'b010,
4792
                    cmd_rfr = 3'b001,
4793
                    cmd_lmr = 3'b000;
4794
// ctrl FSM
4795
    assign cke = 1'b1;
4796
    assign cs_n = 1'b0;
4797
    reg [2:0] state, next;
4798
    function [12:0] a10_fix;
4799
        input [col_size-1:0] a;
4800
        integer i;
4801
    begin
4802
        for (i=0;i<13;i=i+1) begin
4803
            if (i<10)
4804
              if (i<col_size)
4805
                a10_fix[i] = a[i];
4806
              else
4807
                a10_fix[i] = 1'b0;
4808
            else if (i==10)
4809
              a10_fix[i] = 1'b0;
4810
            else
4811
              if (i<col_size)
4812
                a10_fix[i] = a[i-1];
4813
              else
4814
                a10_fix[i] = 1'b0;
4815
        end
4816
    end
4817
    endfunction
4818
    assign {bank,row,col} = adr_i;
4819
    always @ (posedge clk or posedge rst)
4820
    if (rst)
4821
       state <= 3'b000;
4822
    else
4823
       state <= next;
4824
    always @*
4825
    begin
4826
        next = state;
4827
        case (state)
4828
        3'b000:
4829
            if (shreg[3+tRP+tRFC+tRFC+tMRD]) next = 3'b001;
4830
        3'b001:
4831
            if (refresh_req) next = 3'b010;
4832
            else if (cyc_i & stb_i & rd_ack_emptyflag) next = 3'b011;
4833
        3'b010:
4834
            if (shreg[tRP+tRFC-2]) next = 3'b001; // take away two cycles because no cmd will be issued in idle and adr
4835
        3'b011:
4836
            if (current_bank_closed) next = 3'b101;
4837
            else if (current_row_open) next = 3'b111;
4838
            else next = 3'b100;
4839
        3'b100:
4840
            if (shreg[tRP]) next = 3'b101;
4841
        3'b101:
4842
            if (shreg[tRCD]) next = 3'b111;
4843
        3'b111:
4844
            if (!stb_i) next = 3'b001;
4845
        endcase
4846
    end
4847
    // counter
4848
    vl_cnt_shreg_clear # ( .length(32))
4849
        cnt0 (
4850
            .clear(state!=next),
4851
            .q(shreg),
4852
            .rst(rst),
4853
            .clk(clk));
4854
    // ba, a, cmd
4855
    // outputs dependent on state vector
4856
    always @ (*)
4857
        begin
4858
            {a,cmd} = {13'd0,cmd_nop};
4859
            dqm = 2'b11;
4860
            dq_oe = 1'b0;
4861
            stall_o = 1'b1;
4862
            case (state)
4863
            3'b000:
4864
                if (shreg[3]) begin
4865
                    {a,cmd} = {13'b0010000000000, cmd_pch};
4866
                end else if (shreg[3+tRP] | shreg[3+tRP+tRFC])
4867
                    {a,cmd} = {13'd0, cmd_rfr};
4868
                else if (shreg[3+tRP+tRFC+tRFC])
4869
                    {a,cmd} = {3'b000,init_wb,2'b00,init_cl,init_bt,init_bl,cmd_lmr};
4870
            3'b010:
4871
                if (shreg[0])
4872
                    {a,cmd} = {13'b0010000000000, cmd_pch};
4873
                else if (shreg[tRP])
4874
                    {a,cmd} = {13'd0, cmd_rfr};
4875
            3'b100:
4876
                if (shreg[0])
4877
                    {a,cmd} = {13'd0,cmd_pch};
4878
            3'b101:
4879
                if (shreg[0])
4880
                    {a[row_size-1:0],cmd} = {row,cmd_act};
4881
            3'b111:
4882
                begin
4883
                    if (we_i)
4884
                        cmd = cmd_wr;
4885
                    else
4886
                        cmd = cmd_rd;
4887
                    if (we_i)
4888
                        dqm = ~sel_i;
4889
                    else
4890
                        dqm = 2'b00;
4891
                    if (we_i)
4892
                        dq_oe = 1'b1;
4893
                    a = a10_fix(col);
4894
                    stall_o = 1'b0;
4895
                end
4896
            endcase
4897
        end
4898
    assign ba = bank;
4899
    // precharge individual bank A10=0
4900
    // precharge all bank A10=1
4901
    genvar i;
4902
    generate
4903
    for (i=0;i<2<<ba_size-1;i=i+1) begin : open_ba_logic
4904
        always @ (posedge clk or posedge rst)
4905
        if (rst)
4906
            {open_ba[i],open_row[i]} <= {1'b0,{row_size{1'b0}}};
4907
        else
4908
            if (cmd==cmd_pch & (a[10] | bank==i))
4909
                open_ba[i] <= 1'b0;
4910
            else if (cmd==cmd_act & bank==i)
4911
                {open_ba[i],open_row[i]} <= {1'b1,row};
4912
    end
4913
    endgenerate
4914
    // bank and row open ?
4915
    always @ (posedge clk or posedge rst)
4916
    if (rst)
4917
       {current_bank_closed, current_row_open} <= {1'b1, 1'b0};
4918
    else
4919
       {current_bank_closed, current_row_open} <= {!(open_ba[bank]), open_row[bank]==row};
4920
    // refresh counter
4921
    vl_cnt_lfsr_zq # ( .length(rfr_length), .wrap_value (rfr_wrap_value)) ref_counter0( .zq(ref_cnt_zero), .rst(rst), .clk(clk));
4922
    always @ (posedge clk or posedge rst)
4923
    if (rst)
4924
        refresh_req <= 1'b0;
4925
    else
4926
        if (ref_cnt_zero)
4927
            refresh_req <= 1'b1;
4928
        else if (state==3'b010)
4929
            refresh_req <= 1'b0;
4930
    assign dat_o = dq_i;
4931
    assign ack_wr = (state==3'b111 & we_i);
4932
    vl_delay_emptyflag # ( .depth(cl+2)) delay0 ( .d(state==3'b111 & stb_i & !we_i), .q(ack_rd), .emptyflag(rd_ack_emptyflag), .clk(clk), .rst(rst));
4933
    assign ack_o = ack_rd | ack_wr;
4934
    assign dq_o = dat_i;
4935
endmodule
4936
module vl_wb_sdr_sdram_ctrl (
4937
    // WB i/f
4938
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
4939
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
4940
    // SDR SDRAM
4941
    mem_ba, mem_a, mem_cmd, mem_cke, mem_cs_n, mem_dqm, mem_dq_i, mem_dq_o, mem_dq_oe,
4942
    // system
4943
    wb_clk, wb_rst, mem_clk, mem_rst);
4944
    // WB slave
4945
    parameter wbs_dat_width = 32;
4946
    parameter wbs_adr_width = 24;
4947
    parameter wbs_mode = "B3";
4948
    parameter wbs_max_burst_width = 4;
4949
    // Shadow RAM
4950
    parameter shadow_mem_adr_width = 10;
4951
    parameter shadow_mem_size = 1024;
4952
    parameter shadow_mem_init = 2;
4953
    parameter shadow_mem_file = "vl_ram.v";
4954
    // Cache
4955
    parameter cache_async = 1; // wbs_clk != wbm_clk
4956
    parameter cache_nr_of_ways = 1;
4957
    parameter cache_aw_offset = 4; // 4 => 16 words per cache line
4958
    parameter cache_aw_slot = 10;
4959
    parameter cache_valid_mem = 0;
4960
    parameter cache_debug = 0;
4961
    // SDRAM parameters
4962
    parameter mem_dat_size = 16;
4963
    parameter mem_ba_size  = 2;
4964
    parameter mem_row_size = 13;
4965
    parameter mem_col_size = 9;
4966
    parameter mem_cl = 2;
4967
    parameter mem_tRFC = 9;
4968
    parameter mem_tRP  = 2;
4969
    parameter mem_tRCD = 2;
4970
    parameter mem_tMRD = 2;
4971
    parameter mem_rfr_length = 10;
4972
    parameter mem_rfr_wrap_value = 1010;
4973
    input [wbs_dat_width-1:0] wbs_dat_i;
4974
    input [wbs_adr_width-1:0] wbs_adr_i;
4975
    input [2:0] wbs_cti_i;
4976
    input [1:0] wbs_bte_i;
4977
    input [wbs_dat_width/8-1:0] wbs_sel_i;
4978
    input wbs_we_i, wbs_stb_i, wbs_cyc_i;
4979
    output [wbs_dat_width-1:0] wbs_dat_o;
4980
    output wbs_ack_o;
4981
    output wbs_stall_o;
4982
    output [mem_ba_size-1:0]    mem_ba;
4983
    output reg [12:0]           mem_a;
4984
    output reg [2:0]            mem_cmd; // {ras,cas,we}
4985
    output                      mem_cke, mem_cs_n;
4986
    output reg [mem_dat_size/8-1:0] mem_dqm;
4987
    output [mem_dat_size-1:0]       mem_dq_o;
4988
    output reg                  mem_dq_oe;
4989
    input  [mem_dat_size-1:0]       mem_dq_i;
4990
    input wb_clk, wb_rst, mem_clk, mem_rst;
4991
    // wbm1
4992
    wire [wbs_dat_width-1:0] wbm1_dat_o;
4993
    wire [wbs_adr_width-1:0] wbm1_adr_o;
4994
    wire [2:0] wbm1_cti_o;
4995
    wire [1:0] wbm1_bte_o;
4996
    wire [wbs_dat_width/8-1:0] wbm1_sel_o;
4997
    wire wbm1_we_o, wbm1_stb_o, wbm1_cyc_o;
4998
    wire [wbs_dat_width-1:0] wbm1_dat_i;
4999
    wire wbm1_ack_i, wbm1_stall_i;
5000
    // wbm2
5001
    wire [mem_dat_size-1:0] wbm2_dat_o;
5002
    wire [mem_ba_size+mem_row_size+mem_col_size-1:0] wbm2_adr_o;
5003
    wire [2:0] wbm2_cti_o;
5004
    wire [1:0] wbm2_bte_o;
5005
    wire [mem_dat_size/8-1:0] wbm2_sel_o;
5006
    wire wbm2_we_o, wbm2_stb_o, wbm2_cyc_o;
5007
    wire [mem_dat_size-1:0] wbm2_dat_i;
5008
    wire wbm2_ack_i, wbm2_stall_i;
5009
vl_wb_shadow_ram # (
5010
    .shadow_mem_adr_width(shadow_mem_adr_width), .shadow_mem_size(shadow_mem_size), .shadow_mem_init(shadow_mem_init), .shadow_mem_file(shadow_mem_file), .main_mem_adr_width(wbs_adr_width), .dat_width(wbs_dat_width), .mode(wbs_mode), .max_burst_width(wbs_max_burst_width) )
5011
shadow_ram0 (
5012
    .wbs_dat_i(wbs_dat_i),
5013
    .wbs_adr_i(wbs_adr_i),
5014
    .wbs_cti_i(wbs_cti_i),
5015
    .wbs_bte_i(wbs_bte_i),
5016
    .wbs_sel_i(wbs_sel_i),
5017
    .wbs_we_i (wbs_we_i),
5018
    .wbs_stb_i(wbs_stb_i),
5019
    .wbs_cyc_i(wbs_cyc_i),
5020
    .wbs_dat_o(wbs_dat_o),
5021
    .wbs_ack_o(wbs_ack_o),
5022
    .wbs_stall_o(wbs_stall_o),
5023
    .wbm_dat_o(wbm1_dat_o),
5024
    .wbm_adr_o(wbm1_adr_o),
5025
    .wbm_cti_o(wbm1_cti_o),
5026
    .wbm_bte_o(wbm1_bte_o),
5027
    .wbm_sel_o(wbm1_sel_o),
5028
    .wbm_we_o(wbm1_we_o),
5029
    .wbm_stb_o(wbm1_stb_o),
5030
    .wbm_cyc_o(wbm1_cyc_o),
5031
    .wbm_dat_i(wbm1_dat_i),
5032
    .wbm_ack_i(wbm1_ack_i),
5033
    .wbm_stall_i(wbm1_stall_i),
5034
    .wb_clk(wb_clk),
5035
    .wb_rst(wb_rst) );
5036
vl_wb_cache # (
5037
    .dw_s(wbs_dat_width), .aw_s(wbs_adr_width), .dw_m(mem_dat_size), .wbs_max_burst_width(cache_aw_offset), .wbs_mode(wbs_mode), .async(cache_async), .nr_of_ways(cache_nr_of_ways), .aw_offset(cache_aw_offset), .aw_slot(cache_aw_slot), .valid_mem(cache_valid_mem) )
5038
cache0 (
5039
    .wbs_dat_i(wbm1_dat_o),
5040
    .wbs_adr_i(wbm1_adr_o),
5041
    .wbs_sel_i(wbm1_sel_o),
5042
    .wbs_cti_i(wbm1_cti_o),
5043
    .wbs_bte_i(wbm1_bte_o),
5044
    .wbs_we_i (wbm1_we_o),
5045
    .wbs_stb_i(wbm1_stb_o),
5046
    .wbs_cyc_i(wbm1_cyc_o),
5047
    .wbs_dat_o(wbm1_dat_i),
5048
    .wbs_ack_o(wbm1_ack_i),
5049
    .wbs_stall_o(wbm1_stall_i),
5050
    .wbs_clk(wb_clk),
5051
    .wbs_rst(wb_rst),
5052
    .wbm_dat_o(wbm2_dat_o),
5053
    .wbm_adr_o(wbm2_adr_o),
5054
    .wbm_sel_o(wbm2_sel_o),
5055
    .wbm_cti_o(wbm2_cti_o),
5056
    .wbm_bte_o(wbm2_bte_o),
5057
    .wbm_we_o (wbm2_we_o),
5058
    .wbm_stb_o(wbm2_stb_o),
5059
    .wbm_cyc_o(wbm2_cyc_o),
5060
    .wbm_dat_i(wbm2_dat_i),
5061
    .wbm_ack_i(wbm2_ack_i),
5062
    .wbm_stall_i(wbm2_stall_i),
5063
    .wbm_clk(mem_clk),
5064
    .wbm_rst(mem_rst) );
5065
vl_wb_sdr_sdram # (
5066
    .dat_size(mem_dat_size), .ba_size(mem_ba_size), .row_size(mem_row_size), .col_size(mem_col_size), .cl(mem_cl), .tRFC(mem_tRFC), .tRP(mem_tRP), .tRCD(mem_tRCD), .tMRD(mem_tMRD), .rfr_length(mem_rfr_length), .rfr_wrap_value(mem_rfr_wrap_value) )
5067
ctrl0(
5068
    // wisbone i/f
5069
    .dat_i(wbm2_dat_o),
5070
    .adr_i(wbm2_adr_o),
5071
    .sel_i(wbm2_sel_o),
5072
    .we_i (wbm2_we_o),
5073
    .cyc_i(wbm2_cyc_o),
5074
    .stb_i(wbm2_stb_o),
5075
    .dat_o(wbm2_dat_i),
5076
    .ack_o(wbm2_ack_i),
5077
    .stall_o(wbm2_stall_i),
5078
    // SDR SDRAM
5079
    .ba(mem_ba),
5080
    .a(mem_a),
5081
    .cmd(mem_cmd),
5082
    .cke(mem_cke),
5083
    .cs_n(mem_cs_n),
5084
    .dqm(mem_dqm),
5085
    .dq_i(mem_dq_i),
5086
    .dq_o(mem_dq_o),
5087
    .dq_oe(mem_dq_oe),
5088
    // system
5089
    .clk(mem_clk),
5090
    .rst(mem_rst) );
5091
endmodule
5092 18 unneback
//////////////////////////////////////////////////////////////////////
5093
////                                                              ////
5094
////  Arithmetic functions                                        ////
5095
////                                                              ////
5096
////  Description                                                 ////
5097
////  Arithmetic functions for ALU and DSP                        ////
5098
////                                                              ////
5099
////                                                              ////
5100
////  To Do:                                                      ////
5101
////   -                                                          ////
5102
////                                                              ////
5103
////  Author(s):                                                  ////
5104
////      - Michael Unneback, unneback@opencores.org              ////
5105
////        ORSoC AB                                              ////
5106
////                                                              ////
5107
//////////////////////////////////////////////////////////////////////
5108
////                                                              ////
5109
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
5110
////                                                              ////
5111
//// This source file may be used and distributed without         ////
5112
//// restriction provided that this copyright statement is not    ////
5113
//// removed from the file and that any derivative work contains  ////
5114
//// the original copyright notice and the associated disclaimer. ////
5115
////                                                              ////
5116
//// This source file is free software; you can redistribute it   ////
5117
//// and/or modify it under the terms of the GNU Lesser General   ////
5118
//// Public License as published by the Free Software Foundation; ////
5119
//// either version 2.1 of the License, or (at your option) any   ////
5120
//// later version.                                               ////
5121
////                                                              ////
5122
//// This source is distributed in the hope that it will be       ////
5123
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
5124
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
5125
//// PURPOSE.  See the GNU Lesser General Public License for more ////
5126
//// details.                                                     ////
5127
////                                                              ////
5128
//// You should have received a copy of the GNU Lesser General    ////
5129
//// Public License along with this source; if not, download it   ////
5130
//// from http://www.opencores.org/lgpl.shtml                     ////
5131
////                                                              ////
5132
//////////////////////////////////////////////////////////////////////
5133
// signed multiplication
5134
module vl_mults (a,b,p);
5135
parameter operand_a_width = 18;
5136
parameter operand_b_width = 18;
5137
parameter result_hi = 35;
5138
parameter result_lo = 0;
5139
input [operand_a_width-1:0] a;
5140
input [operand_b_width-1:0] b;
5141
output [result_hi:result_lo] p;
5142
wire signed [operand_a_width-1:0] ai;
5143
wire signed [operand_b_width-1:0] bi;
5144
wire signed [operand_a_width+operand_b_width-1:0] result;
5145
    assign ai = a;
5146
    assign bi = b;
5147
    assign result = ai * bi;
5148
    assign p = result[result_hi:result_lo];
5149
endmodule
5150
module vl_mults18x18 (a,b,p);
5151
input [17:0] a,b;
5152
output [35:0] p;
5153
vl_mult
5154
    # (.operand_a_width(18), .operand_b_width(18))
5155
    mult0 (.a(a), .b(b), .p(p));
5156
endmodule
5157
// unsigned multiplication
5158
module vl_mult (a,b,p);
5159
parameter operand_a_width = 18;
5160
parameter operand_b_width = 18;
5161
parameter result_hi = 35;
5162
parameter result_lo = 0;
5163
input [operand_a_width-1:0] a;
5164
input [operand_b_width-1:0] b;
5165
output [result_hi:result_hi] p;
5166
wire [operand_a_width+operand_b_width-1:0] result;
5167
    assign result = a * b;
5168
    assign p = result[result_hi:result_lo];
5169
endmodule
5170
// shift unit
5171
// supporting the following shift functions
5172
//   SLL
5173
//   SRL
5174
//   SRA
5175
module vl_shift_unit_32( din, s, dout, opcode);
5176
input [31:0] din; // data in operand
5177
input [4:0] s; // shift operand
5178
input [1:0] opcode;
5179
output [31:0] dout;
5180
parameter opcode_sll = 2'b00;
5181
//parameter opcode_srl = 2'b01;
5182
parameter opcode_sra = 2'b10;
5183
//parameter opcode_ror = 2'b11;
5184
wire sll, sra;
5185
assign sll = opcode == opcode_sll;
5186
assign sra = opcode == opcode_sra;
5187
wire [15:1] s1;
5188
wire [3:0] sign;
5189
wire [7:0] tmp [0:3];
5190
// first stage is multiplier based
5191
// shift operand as fractional 8.7
5192
assign s1[15] = sll & s[2:0]==3'd7;
5193
assign s1[14] = sll & s[2:0]==3'd6;
5194
assign s1[13] = sll & s[2:0]==3'd5;
5195
assign s1[12] = sll & s[2:0]==3'd4;
5196
assign s1[11] = sll & s[2:0]==3'd3;
5197
assign s1[10] = sll & s[2:0]==3'd2;
5198
assign s1[ 9] = sll & s[2:0]==3'd1;
5199
assign s1[ 8] = s[2:0]==3'd0;
5200
assign s1[ 7] = !sll & s[2:0]==3'd1;
5201
assign s1[ 6] = !sll & s[2:0]==3'd2;
5202
assign s1[ 5] = !sll & s[2:0]==3'd3;
5203
assign s1[ 4] = !sll & s[2:0]==3'd4;
5204
assign s1[ 3] = !sll & s[2:0]==3'd5;
5205
assign s1[ 2] = !sll & s[2:0]==3'd6;
5206
assign s1[ 1] = !sll & s[2:0]==3'd7;
5207
assign sign[3] = din[31] & sra;
5208
assign sign[2] = sign[3] & (&din[31:24]);
5209
assign sign[1] = sign[2] & (&din[23:16]);
5210
assign sign[0] = sign[1] & (&din[15:8]);
5211
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]));
5212
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]));
5213
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]));
5214
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]));
5215
// second stage is multiplexer based
5216
// shift on byte level
5217
// mux byte 3
5218
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
5219
                     (sll & s[4:3]==2'b01) ? tmp[2] :
5220
                     (sll & s[4:3]==2'b10) ? tmp[1] :
5221
                     (sll & s[4:3]==2'b11) ? tmp[0] :
5222
                     {8{sign[3]}};
5223
// mux byte 2
5224
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
5225
                     (sll & s[4:3]==2'b01) ? tmp[1] :
5226
                     (sll & s[4:3]==2'b10) ? tmp[0] :
5227
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
5228
                     (s[4:3]==2'b01) ? tmp[3] :
5229
                     {8{sign[3]}};
5230
// mux byte 1
5231
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
5232
                     (sll & s[4:3]==2'b01) ? tmp[0] :
5233
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
5234
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
5235
                     (s[4:3]==2'b01) ? tmp[2] :
5236
                     (s[4:3]==2'b10) ? tmp[3] :
5237
                     {8{sign[3]}};
5238
// mux byte 0
5239
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
5240
                     (sll) ?  {8{1'b0}}:
5241
                     (s[4:3]==2'b01) ? tmp[1] :
5242
                     (s[4:3]==2'b10) ? tmp[2] :
5243
                     tmp[3];
5244
endmodule
5245
// logic unit
5246
// supporting the following logic functions
5247
//    a and b
5248
//    a or  b
5249
//    a xor b
5250
//    not b
5251
module vl_logic_unit( a, b, result, opcode);
5252
parameter width = 32;
5253
parameter opcode_and = 2'b00;
5254
parameter opcode_or  = 2'b01;
5255
parameter opcode_xor = 2'b10;
5256
input [width-1:0] a,b;
5257
output [width-1:0] result;
5258
input [1:0] opcode;
5259
assign result = (opcode==opcode_and) ? a & b :
5260
                (opcode==opcode_or)  ? a | b :
5261
                (opcode==opcode_xor) ? a ^ b :
5262
                b;
5263
endmodule

powered by: WebSVN 2.1.0

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