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

Subversion Repositories versatile_library

[/] [versatile_library/] [trunk/] [rtl/] [verilog/] [versatile_library_actel.v] - Blame information for rev 72

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

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

powered by: WebSVN 2.1.0

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