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 33

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

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

powered by: WebSVN 2.1.0

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