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

Subversion Repositories versatile_library

[/] [versatile_library/] [trunk/] [rtl/] [verilog/] [wb.v] - Blame information for rev 153

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

Line No. Rev Author Line
1 12 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Versatile library, wishbone stuff                           ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  Wishbone compliant modules                                  ////
7
////                                                              ////
8
////                                                              ////
9
////  To Do:                                                      ////
10
////   -                                                          ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - Michael Unneback, unneback@opencores.org              ////
14
////        ORSoC AB                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
 
43 75 unneback
`ifdef WB_ADR_INC
44
`timescale 1ns/1ns
45
`define MODULE wb_adr_inc
46 84 unneback
module `BASE`MODULE ( cyc_i, stb_i, cti_i, bte_i, adr_i, we_i, ack_o, adr_o, clk, rst);
47 75 unneback
`undef MODULE
48 83 unneback
parameter adr_width = 10;
49
parameter max_burst_width = 4;
50 84 unneback
input cyc_i, stb_i, we_i;
51 83 unneback
input [2:0] cti_i;
52
input [1:0] bte_i;
53
input [adr_width-1:0] adr_i;
54
output [adr_width-1:0] adr_o;
55
output ack_o;
56
input clk, rst;
57 75 unneback
 
58 83 unneback
reg [adr_width-1:0] adr;
59 90 unneback
wire [max_burst_width-1:0] to_adr;
60 91 unneback
reg [max_burst_width-1:0] last_adr;
61 92 unneback
reg last_cycle;
62
localparam idle_or_eoc = 1'b0;
63
localparam cyc_or_ws   = 1'b1;
64 90 unneback
 
65 91 unneback
always @ (posedge clk or posedge rst)
66
if (rst)
67
    last_adr <= {max_burst_width{1'b0}};
68
else
69
    if (stb_i)
70 92 unneback
        last_adr <=adr_o[max_burst_width-1:0];
71 91 unneback
 
72 83 unneback
generate
73
if (max_burst_width==0) begin : inst_0
74 96 unneback
 
75
        reg ack_o;
76
        assign adr_o = adr_i;
77
        always @ (posedge clk or posedge rst)
78
        if (rst)
79
            ack_o <= 1'b0;
80
        else
81
            ack_o <= cyc_i & stb_i & !ack_o;
82
 
83 83 unneback
end else begin
84
 
85
    always @ (posedge clk or posedge rst)
86
    if (rst)
87 92 unneback
        last_cycle <= idle_or_eoc;
88 83 unneback
    else
89 92 unneback
        last_cycle <= (!cyc_i) ? idle_or_eoc : //idle
90
                      (cyc_i & ack_o & (cti_i==3'b000 | cti_i==3'b111)) ? idle_or_eoc : // eoc
91
                      (cyc_i & !stb_i) ? cyc_or_ws : //ws
92
                      cyc_or_ws; // cyc
93
    assign to_adr = (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] : adr[max_burst_width-1:0];
94 84 unneback
    assign adr_o[max_burst_width-1:0] = (we_i) ? adr_i[max_burst_width-1:0] :
95 91 unneback
                                        (!stb_i) ? last_adr :
96 92 unneback
                                        (last_cycle==idle_or_eoc) ? adr_i[max_burst_width-1:0] :
97 84 unneback
                                        adr[max_burst_width-1:0];
98 92 unneback
    assign ack_o = (last_cycle==cyc_or_ws) & stb_i;
99 96 unneback
 
100 83 unneback
end
101
endgenerate
102
 
103
generate
104
if (max_burst_width==2) begin : inst_2
105
    always @ (posedge clk or posedge rst)
106
    if (rst)
107
        adr <= 2'h0;
108
    else
109
        if (cyc_i & stb_i)
110
            adr[1:0] <= to_adr[1:0] + 2'd1;
111 75 unneback
        else
112 83 unneback
            adr <= to_adr[1:0];
113
end
114
endgenerate
115
 
116
generate
117
if (max_burst_width==3) begin : inst_3
118
    always @ (posedge clk or posedge rst)
119
    if (rst)
120
        adr <= 3'h0;
121
    else
122
        if (cyc_i & stb_i)
123
            case (bte_i)
124
            2'b01: adr[2:0] <= {to_adr[2],to_adr[1:0] + 2'd1};
125
            default: adr[3:0] <= to_adr[2:0] + 3'd1;
126 75 unneback
            endcase
127 83 unneback
        else
128
            adr <= to_adr[2:0];
129
end
130
endgenerate
131
 
132
generate
133
if (max_burst_width==4) begin : inst_4
134
    always @ (posedge clk or posedge rst)
135
    if (rst)
136
        adr <= 4'h0;
137
    else
138 91 unneback
        if (stb_i) // | (!stb_i & last_cycle!=ws)) // for !stb_i restart with adr_i +1, only inc once
139 83 unneback
            case (bte_i)
140
            2'b01: adr[3:0] <= {to_adr[3:2],to_adr[1:0] + 2'd1};
141
            2'b10: adr[3:0] <= {to_adr[3],to_adr[2:0] + 3'd1};
142
            default: adr[3:0] <= to_adr + 4'd1;
143
            endcase
144
        else
145
            adr <= to_adr[3:0];
146
end
147
endgenerate
148
 
149
generate
150
if (adr_width > max_burst_width) begin : pass_through
151
    assign adr_o[adr_width-1:max_burst_width] = adr_i[adr_width-1:max_burst_width];
152
end
153
endgenerate
154
 
155
endmodule
156 75 unneback
`endif
157
 
158 104 unneback
`ifdef WB_B4_EOC
159
`define MODULE wb_b4_eoc
160
module `BASE`MODULE ( cyc_i, stb_i, stall_o, ack_o, busy, eoc, clk, rst);
161
`undef MODULE
162
input cyc_i, stb_i, ack_o;
163
output busy, eoc;
164
input clk, rst;
165
 
166
`define MODULE cnt_bin_ce_rew_zq_l1
167
`BASE`MODULE # ( .length(4), level1_value(1))
168
cnt0 (
169
    .cke(), .rew(), .zq(), .level1(), .rst(), clk);
170
`undef MODULE
171
 
172
endmodule
173
`endif
174
 
175 40 unneback
`ifdef WB3WB3_BRIDGE
176 12 unneback
// async wb3 - wb3 bridge
177
`timescale 1ns/1ns
178 40 unneback
`define MODULE wb3wb3_bridge
179
module `BASE`MODULE (
180
`undef MODULE
181 12 unneback
        // wishbone slave side
182
        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,
183
        // wishbone master side
184
        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);
185
 
186 94 unneback
parameter style = "FIFO"; // valid: simple, FIFO
187
parameter addr_width = 4;
188
 
189 12 unneback
input [31:0] wbs_dat_i;
190
input [31:2] wbs_adr_i;
191
input [3:0]  wbs_sel_i;
192
input [1:0]  wbs_bte_i;
193
input [2:0]  wbs_cti_i;
194
input wbs_we_i, wbs_cyc_i, wbs_stb_i;
195
output [31:0] wbs_dat_o;
196 14 unneback
output wbs_ack_o;
197 12 unneback
input wbs_clk, wbs_rst;
198
 
199
output [31:0] wbm_dat_o;
200
output reg [31:2] wbm_adr_o;
201
output [3:0]  wbm_sel_o;
202
output reg [1:0]  wbm_bte_o;
203
output reg [2:0]  wbm_cti_o;
204 14 unneback
output reg wbm_we_o;
205
output wbm_cyc_o;
206 12 unneback
output wbm_stb_o;
207
input [31:0]  wbm_dat_i;
208
input wbm_ack_i;
209
input wbm_clk, wbm_rst;
210
 
211
// bte
212
parameter linear       = 2'b00;
213
parameter wrap4        = 2'b01;
214
parameter wrap8        = 2'b10;
215
parameter wrap16       = 2'b11;
216
// cti
217
parameter classic      = 3'b000;
218
parameter incburst     = 3'b010;
219
parameter endofburst   = 3'b111;
220
 
221 94 unneback
localparam wbs_adr  = 1'b0;
222
localparam wbs_data = 1'b1;
223 12 unneback
 
224 94 unneback
localparam wbm_adr0      = 2'b00;
225
localparam wbm_adr1      = 2'b01;
226
localparam wbm_data      = 2'b10;
227
localparam wbm_data_wait = 2'b11;
228 12 unneback
 
229
reg [1:0] wbs_bte_reg;
230
reg wbs;
231
wire wbs_eoc_alert, wbm_eoc_alert;
232
reg wbs_eoc, wbm_eoc;
233
reg [1:0] wbm;
234
 
235 14 unneback
wire [1:16] wbs_count, wbm_count;
236 12 unneback
 
237
wire [35:0] a_d, a_q, b_d, b_q;
238
wire a_wr, a_rd, a_fifo_full, a_fifo_empty, b_wr, b_rd, b_fifo_full, b_fifo_empty;
239
reg a_rd_reg;
240
wire b_rd_adr, b_rd_data;
241 14 unneback
wire b_rd_data_reg;
242
wire [35:0] temp;
243 12 unneback
 
244
`define WE 5
245
`define BTE 4:3
246
`define CTI 2:0
247
 
248
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]);
249
always @ (posedge wbs_clk or posedge wbs_rst)
250
if (wbs_rst)
251
        wbs_eoc <= 1'b0;
252
else
253
        if (wbs==wbs_adr & wbs_stb_i & !a_fifo_full)
254 78 unneback
                wbs_eoc <= (wbs_bte_i==linear) | (wbs_cti_i==3'b111);
255 12 unneback
        else if (wbs_eoc_alert & (a_rd | a_wr))
256
                wbs_eoc <= 1'b1;
257
 
258 40 unneback
`define MODULE cnt_shreg_ce_clear
259
`BASE`MODULE # ( .length(16))
260
`undef MODULE
261 12 unneback
    cnt0 (
262
        .cke(wbs_ack_o),
263
        .clear(wbs_eoc),
264
        .q(wbs_count),
265
        .rst(wbs_rst),
266
        .clk(wbs_clk));
267
 
268
always @ (posedge wbs_clk or posedge wbs_rst)
269
if (wbs_rst)
270
        wbs <= wbs_adr;
271
else
272 75 unneback
        if ((wbs==wbs_adr) & wbs_cyc_i & wbs_stb_i & a_fifo_empty)
273 12 unneback
                wbs <= wbs_data;
274
        else if (wbs_eoc & wbs_ack_o)
275
                wbs <= wbs_adr;
276
 
277
// wbs FIFO
278 75 unneback
assign a_d = (wbs==wbs_adr) ? {wbs_adr_i[31:2],wbs_we_i,((wbs_cti_i==3'b111) ? {2'b00,3'b000} : {wbs_bte_i,wbs_cti_i})} : {wbs_dat_i,wbs_sel_i};
279
assign a_wr = (wbs==wbs_adr)  ? wbs_cyc_i & wbs_stb_i & a_fifo_empty :
280 12 unneback
              (wbs==wbs_data) ? wbs_we_i  & wbs_stb_i & !a_fifo_full :
281
              1'b0;
282
assign a_rd = !a_fifo_empty;
283
always @ (posedge wbs_clk or posedge wbs_rst)
284
if (wbs_rst)
285
        a_rd_reg <= 1'b0;
286
else
287
        a_rd_reg <= a_rd;
288
assign wbs_ack_o = a_rd_reg | (a_wr & wbs==wbs_data);
289
 
290
assign wbs_dat_o = a_q[35:4];
291
 
292
always @ (posedge wbs_clk or posedge wbs_rst)
293
if (wbs_rst)
294 13 unneback
        wbs_bte_reg <= 2'b00;
295 12 unneback
else
296 13 unneback
        wbs_bte_reg <= wbs_bte_i;
297 12 unneback
 
298
// wbm FIFO
299
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]);
300
always @ (posedge wbm_clk or posedge wbm_rst)
301
if (wbm_rst)
302
        wbm_eoc <= 1'b0;
303
else
304
        if (wbm==wbm_adr0 & !b_fifo_empty)
305
                wbm_eoc <= b_q[`BTE] == linear;
306
        else if (wbm_eoc_alert & wbm_ack_i)
307
                wbm_eoc <= 1'b1;
308
 
309
always @ (posedge wbm_clk or posedge wbm_rst)
310
if (wbm_rst)
311
        wbm <= wbm_adr0;
312
else
313 33 unneback
/*
314 12 unneback
    if ((wbm==wbm_adr0 & !b_fifo_empty) |
315
        (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) |
316
        (wbm==wbm_adr1 & !wbm_we_o) |
317
        (wbm==wbm_data & wbm_ack_i & wbm_eoc))
318
        wbm <= {wbm[0],!(wbm[1] ^ wbm[0])};  // count sequence 00,01,10
319 33 unneback
*/
320
    case (wbm)
321
    wbm_adr0:
322
        if (!b_fifo_empty)
323
            wbm <= wbm_adr1;
324
    wbm_adr1:
325
        if (!wbm_we_o | (!b_fifo_empty & wbm_we_o))
326
            wbm <= wbm_data;
327
    wbm_data:
328
        if (wbm_ack_i & wbm_eoc)
329
            wbm <= wbm_adr0;
330
        else if (b_fifo_empty & wbm_we_o & wbm_ack_i)
331
            wbm <= wbm_data_wait;
332
    wbm_data_wait:
333
        if (!b_fifo_empty)
334
            wbm <= wbm_data;
335
    endcase
336 12 unneback
 
337
assign b_d = {wbm_dat_i,4'b1111};
338
assign b_wr = !wbm_we_o & wbm_ack_i;
339
assign b_rd_adr  = (wbm==wbm_adr0 & !b_fifo_empty);
340
assign b_rd_data = (wbm==wbm_adr1 & !b_fifo_empty & wbm_we_o) ? 1'b1 : // b_q[`WE]
341
                   (wbm==wbm_data & !b_fifo_empty & wbm_we_o & wbm_ack_i & !wbm_eoc) ? 1'b1 :
342 33 unneback
                   (wbm==wbm_data_wait & !b_fifo_empty) ? 1'b1 :
343 12 unneback
                   1'b0;
344
assign b_rd = b_rd_adr | b_rd_data;
345
 
346 40 unneback
`define MODULE dff
347
`BASE`MODULE dff1 ( .d(b_rd_data), .q(b_rd_data_reg), .clk(wbm_clk), .rst(wbm_rst));
348
`undef MODULE
349
`define MODULE dff_ce
350
`BASE`MODULE # ( .width(36)) dff2 ( .d(b_q), .ce(b_rd_data_reg), .q(temp), .clk(wbm_clk), .rst(wbm_rst));
351
`undef MODULE
352 12 unneback
 
353
assign {wbm_dat_o,wbm_sel_o} = (b_rd_data_reg) ? b_q : temp;
354
 
355 40 unneback
`define MODULE cnt_shreg_ce_clear
356 42 unneback
`BASE`MODULE # ( .length(16))
357 40 unneback
`undef MODULE
358 12 unneback
    cnt1 (
359
        .cke(wbm_ack_i),
360
        .clear(wbm_eoc),
361
        .q(wbm_count),
362
        .rst(wbm_rst),
363
        .clk(wbm_clk));
364
 
365 33 unneback
assign wbm_cyc_o = (wbm==wbm_data | wbm==wbm_data_wait);
366
assign wbm_stb_o = (wbm==wbm_data);
367 12 unneback
 
368
always @ (posedge wbm_clk or posedge wbm_rst)
369
if (wbm_rst)
370
        {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= {30'h0,1'b0,linear,classic};
371
else begin
372
        if (wbm==wbm_adr0 & !b_fifo_empty)
373
                {wbm_adr_o,wbm_we_o,wbm_bte_o,wbm_cti_o} <= b_q;
374
        else if (wbm_eoc_alert & wbm_ack_i)
375
                wbm_cti_o <= endofburst;
376
end
377
 
378
//async_fifo_dw_simplex_top
379 40 unneback
`define MODULE fifo_2r2w_async_simplex
380
`BASE`MODULE
381
`undef MODULE
382 12 unneback
# ( .data_width(36), .addr_width(addr_width))
383
fifo (
384
    // a side
385
    .a_d(a_d),
386
    .a_wr(a_wr),
387
    .a_fifo_full(a_fifo_full),
388
    .a_q(a_q),
389
    .a_rd(a_rd),
390
    .a_fifo_empty(a_fifo_empty),
391
    .a_clk(wbs_clk),
392
    .a_rst(wbs_rst),
393
    // b side
394
    .b_d(b_d),
395
    .b_wr(b_wr),
396
    .b_fifo_full(b_fifo_full),
397
    .b_q(b_q),
398
    .b_rd(b_rd),
399
    .b_fifo_empty(b_fifo_empty),
400
    .b_clk(wbm_clk),
401
    .b_rst(wbm_rst)
402
    );
403
 
404
endmodule
405 40 unneback
`undef WE
406
`undef BTE
407
`undef CTI
408
`endif
409 17 unneback
 
410 75 unneback
`ifdef WB3AVALON_BRIDGE
411
`define MODULE wb3avalon_bridge
412
module `BASE`MODULE (
413
`undef MODULE
414
        // wishbone slave side
415
        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,
416 77 unneback
        // avalon master side
417 75 unneback
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst);
418
 
419 84 unneback
parameter linewrapburst = 1'b0;
420
 
421 75 unneback
input [31:0] wbs_dat_i;
422
input [31:2] wbs_adr_i;
423
input [3:0]  wbs_sel_i;
424
input [1:0]  wbs_bte_i;
425
input [2:0]  wbs_cti_i;
426 83 unneback
input wbs_we_i;
427
input wbs_cyc_i;
428
input wbs_stb_i;
429 75 unneback
output [31:0] wbs_dat_o;
430
output wbs_ack_o;
431
input wbs_clk, wbs_rst;
432
 
433
input [31:0] readdata;
434
output [31:0] writedata;
435
output [31:2] address;
436
output [3:0]  be;
437
output write;
438 81 unneback
output read;
439 75 unneback
output beginbursttransfer;
440
output [3:0] burstcount;
441
input readdatavalid;
442
input waitrequest;
443
input clk;
444
input rst;
445
 
446
wire [1:0] wbm_bte_o;
447
wire [2:0] wbm_cti_o;
448
wire wbm_we_o, wbm_cyc_o, wbm_stb_o, wbm_ack_i;
449
reg last_cyc;
450 79 unneback
reg [3:0] counter;
451 82 unneback
reg read_busy;
452 75 unneback
 
453
always @ (posedge clk or posedge rst)
454
if (rst)
455
    last_cyc <= 1'b0;
456
else
457
    last_cyc <= wbm_cyc_o;
458
 
459 79 unneback
always @ (posedge clk or posedge rst)
460
if (rst)
461 82 unneback
    read_busy <= 1'b0;
462 79 unneback
else
463 82 unneback
    if (read & !waitrequest)
464
        read_busy <= 1'b1;
465
    else if (wbm_ack_i & wbm_cti_o!=3'b010)
466
        read_busy <= 1'b0;
467
assign read = wbm_cyc_o & wbm_stb_o & !wbm_we_o & !read_busy;
468 81 unneback
 
469 75 unneback
assign beginbursttransfer = (!last_cyc & wbm_cyc_o) & wbm_cti_o==3'b010;
470
assign burstcount = (wbm_bte_o==2'b01) ? 4'd4 :
471
                    (wbm_bte_o==2'b10) ? 4'd8 :
472 78 unneback
                    (wbm_bte_o==2'b11) ? 4'd16:
473
                    4'd1;
474 82 unneback
assign wbm_ack_i = (readdatavalid) | (write & !waitrequest);
475 75 unneback
 
476 79 unneback
always @ (posedge clk or posedge rst)
477
if (rst) begin
478
    counter <= 4'd0;
479
end else
480 80 unneback
    if (wbm_we_o) begin
481
        if (!waitrequest & !last_cyc & wbm_cyc_o) begin
482 84 unneback
            counter <= burstcount -4'd1;
483 80 unneback
        end else if (waitrequest & !last_cyc & wbm_cyc_o) begin
484
            counter <= burstcount;
485
        end else if (!waitrequest & wbm_stb_o) begin
486
            counter <= counter - 4'd1;
487
        end
488 82 unneback
    end
489 81 unneback
assign write = wbm_cyc_o & wbm_stb_o & wbm_we_o & counter!=4'd0;
490 79 unneback
 
491 75 unneback
`define MODULE wb3wb3_bridge
492 77 unneback
`BASE`MODULE wbwb3inst (
493 75 unneback
`undef MODULE
494
    // wishbone slave side
495
    .wbs_dat_i(wbs_dat_i),
496
    .wbs_adr_i(wbs_adr_i),
497
    .wbs_sel_i(wbs_sel_i),
498
    .wbs_bte_i(wbs_bte_i),
499
    .wbs_cti_i(wbs_cti_i),
500
    .wbs_we_i(wbs_we_i),
501
    .wbs_cyc_i(wbs_cyc_i),
502
    .wbs_stb_i(wbs_stb_i),
503
    .wbs_dat_o(wbs_dat_o),
504
    .wbs_ack_o(wbs_ack_o),
505
    .wbs_clk(wbs_clk),
506
    .wbs_rst(wbs_rst),
507
    // wishbone master side
508
    .wbm_dat_o(writedata),
509 78 unneback
    .wbm_adr_o(address),
510 75 unneback
    .wbm_sel_o(be),
511
    .wbm_bte_o(wbm_bte_o),
512
    .wbm_cti_o(wbm_cti_o),
513
    .wbm_we_o(wbm_we_o),
514
    .wbm_cyc_o(wbm_cyc_o),
515
    .wbm_stb_o(wbm_stb_o),
516
    .wbm_dat_i(readdata),
517
    .wbm_ack_i(wbm_ack_i),
518
    .wbm_clk(clk),
519
    .wbm_rst(rst));
520
 
521
 
522
endmodule
523
`endif
524
 
525 105 unneback
`ifdef WB_ARBITER
526
`define MODULE wb_arbiter
527 42 unneback
module `BASE`MODULE (
528 40 unneback
`undef MODULE
529 39 unneback
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
530 105 unneback
    wbm_dat_i, wbm_stall_i, wbm_ack_i, wbm_err_i, wbm_rty_i,
531 39 unneback
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
532 105 unneback
    wbs_dat_o, wbs_stall_o, wbs_ack_o, wbs_err_o, wbs_rty_o,
533 39 unneback
    wb_clk, wb_rst
534
);
535
 
536
parameter nr_of_ports = 3;
537
parameter adr_size = 26;
538
parameter adr_lo   = 2;
539
parameter dat_size = 32;
540
parameter sel_size = dat_size/8;
541
 
542
localparam aw = (adr_size - adr_lo) * nr_of_ports;
543
localparam dw = dat_size * nr_of_ports;
544
localparam sw = sel_size * nr_of_ports;
545
localparam cw = 3 * nr_of_ports;
546
localparam bw = 2 * nr_of_ports;
547
 
548
input  [dw-1:0] wbm_dat_o;
549
input  [aw-1:0] wbm_adr_o;
550
input  [sw-1:0] wbm_sel_o;
551
input  [cw-1:0] wbm_cti_o;
552
input  [bw-1:0] wbm_bte_o;
553
input  [nr_of_ports-1:0] wbm_we_o, wbm_stb_o, wbm_cyc_o;
554
output [dw-1:0] wbm_dat_i;
555 105 unneback
output [nr_of_ports-1:0] wbm_stall_o, wbm_ack_i, wbm_err_i, wbm_rty_i;
556 39 unneback
 
557
output [dat_size-1:0] wbs_dat_i;
558
output [adr_size-1:adr_lo] wbs_adr_i;
559
output [sel_size-1:0] wbs_sel_i;
560
output [2:0] wbs_cti_i;
561
output [1:0] wbs_bte_i;
562
output wbs_we_i, wbs_stb_i, wbs_cyc_i;
563
input  [dat_size-1:0] wbs_dat_o;
564 105 unneback
input  wbs_stall_o, wbs_ack_o, wbs_err_o, wbs_rty_o;
565 39 unneback
 
566
input wb_clk, wb_rst;
567
 
568 44 unneback
reg  [nr_of_ports-1:0] select;
569 39 unneback
wire [nr_of_ports-1:0] state;
570
wire [nr_of_ports-1:0] eoc; // end-of-cycle
571
wire [nr_of_ports-1:0] sel;
572
wire idle;
573
 
574
genvar i;
575
 
576
assign idle = !(|state);
577
 
578
generate
579
if (nr_of_ports == 2) begin
580
 
581
    wire [2:0] wbm1_cti_o, wbm0_cti_o;
582
 
583
    assign {wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
584
 
585 44 unneback
    //assign select = (idle) ? {wbm_cyc_o[1],!wbm_cyc_o[1] & wbm_cyc_o[0]} : {nr_of_ports{1'b0}};
586
 
587
    always @ (idle or wbm_cyc_o)
588
    if (idle)
589
        casex (wbm_cyc_o)
590
        2'b1x : select = 2'b10;
591
        2'b01 : select = 2'b01;
592
        default : select = {nr_of_ports{1'b0}};
593
        endcase
594
    else
595
        select = {nr_of_ports{1'b0}};
596
 
597 39 unneback
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
598
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
599
 
600
end
601
endgenerate
602
 
603
generate
604
if (nr_of_ports == 3) begin
605
 
606
    wire [2:0] wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
607
 
608
    assign {wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
609
 
610 44 unneback
    always @ (idle or wbm_cyc_o)
611
    if (idle)
612
        casex (wbm_cyc_o)
613
        3'b1xx : select = 3'b100;
614
        3'b01x : select = 3'b010;
615
        3'b001 : select = 3'b001;
616
        default : select = {nr_of_ports{1'b0}};
617
        endcase
618
    else
619
        select = {nr_of_ports{1'b0}};
620
 
621
//    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}};
622 39 unneback
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
623
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
624
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
625
 
626
end
627
endgenerate
628
 
629
generate
630 44 unneback
if (nr_of_ports == 4) begin
631
 
632
    wire [2:0] wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
633
 
634
    assign {wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
635
 
636
    //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}};
637
 
638
    always @ (idle or wbm_cyc_o)
639
    if (idle)
640
        casex (wbm_cyc_o)
641
        4'b1xxx : select = 4'b1000;
642
        4'b01xx : select = 4'b0100;
643
        4'b001x : select = 4'b0010;
644
        4'b0001 : select = 4'b0001;
645
        default : select = {nr_of_ports{1'b0}};
646
        endcase
647
    else
648
        select = {nr_of_ports{1'b0}};
649
 
650
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
651
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
652
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
653
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
654
 
655
end
656
endgenerate
657
 
658
generate
659
if (nr_of_ports == 5) begin
660
 
661
    wire [2:0] wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
662
 
663
    assign {wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
664
 
665
    //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}};
666
 
667
    always @ (idle or wbm_cyc_o)
668
    if (idle)
669
        casex (wbm_cyc_o)
670
        5'b1xxxx : select = 5'b10000;
671
        5'b01xxx : select = 5'b01000;
672
        5'b001xx : select = 5'b00100;
673
        5'b0001x : select = 5'b00010;
674
        5'b00001 : select = 5'b00001;
675
        default : select = {nr_of_ports{1'b0}};
676
        endcase
677
    else
678
        select = {nr_of_ports{1'b0}};
679
 
680
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
681
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
682
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
683
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
684
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
685
 
686
end
687
endgenerate
688
 
689
generate
690 67 unneback
if (nr_of_ports == 6) begin
691
 
692
    wire [2:0] wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
693
 
694
    assign {wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o,wbm1_cti_o,wbm0_cti_o} = wbm_cti_o;
695
 
696
    //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}};
697
 
698
    always @ (idle or wbm_cyc_o)
699
    if (idle)
700
        casex (wbm_cyc_o)
701
        6'b1xxxxx : select = 6'b100000;
702
        6'b01xxxx : select = 6'b010000;
703
        6'b001xxx : select = 6'b001000;
704
        6'b0001xx : select = 6'b000100;
705
        6'b00001x : select = 6'b000010;
706
        6'b000001 : select = 6'b000001;
707
        default : select = {nr_of_ports{1'b0}};
708
        endcase
709
    else
710
        select = {nr_of_ports{1'b0}};
711
 
712
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
713
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
714
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
715
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
716
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
717
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
718
 
719
end
720
endgenerate
721
 
722
generate
723
if (nr_of_ports == 7) begin
724
 
725
    wire [2:0] wbm6_cti_o, wbm5_cti_o, wbm4_cti_o, wbm3_cti_o, wbm2_cti_o, wbm1_cti_o, wbm0_cti_o;
726
 
727
    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;
728
 
729
    //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}};
730
 
731
    always @ (idle or wbm_cyc_o)
732
    if (idle)
733
        casex (wbm_cyc_o)
734
        7'b1xxxxxx : select = 7'b1000000;
735
        7'b01xxxxx : select = 7'b0100000;
736
        7'b001xxxx : select = 7'b0010000;
737
        7'b0001xxx : select = 7'b0001000;
738
        7'b00001xx : select = 7'b0000100;
739
        7'b000001x : select = 7'b0000010;
740
        7'b0000001 : select = 7'b0000001;
741
        default : select = {nr_of_ports{1'b0}};
742
        endcase
743
    else
744
        select = {nr_of_ports{1'b0}};
745
 
746
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
747
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
748
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
749
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
750
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
751
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
752
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
753
 
754
end
755
endgenerate
756
 
757
generate
758
if (nr_of_ports == 8) begin
759
 
760
    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;
761
 
762
    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;
763
 
764
    //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}};
765
 
766
    always @ (idle or wbm_cyc_o)
767
    if (idle)
768
        casex (wbm_cyc_o)
769
        8'b1xxxxxxx : select = 8'b10000000;
770
        8'b01xxxxxx : select = 8'b01000000;
771
        8'b001xxxxx : select = 8'b00100000;
772
        8'b0001xxxx : select = 8'b00010000;
773
        8'b00001xxx : select = 8'b00001000;
774
        8'b000001xx : select = 8'b00000100;
775
        8'b0000001x : select = 8'b00000010;
776
        8'b00000001 : select = 8'b00000001;
777
        default : select = {nr_of_ports{1'b0}};
778
        endcase
779
    else
780
        select = {nr_of_ports{1'b0}};
781
 
782
    assign eoc[7] = (wbm_ack_i[7] & (wbm7_cti_o == 3'b000 | wbm7_cti_o == 3'b111)) | !wbm_cyc_o[7];
783
    assign eoc[6] = (wbm_ack_i[6] & (wbm6_cti_o == 3'b000 | wbm6_cti_o == 3'b111)) | !wbm_cyc_o[6];
784
    assign eoc[5] = (wbm_ack_i[5] & (wbm5_cti_o == 3'b000 | wbm5_cti_o == 3'b111)) | !wbm_cyc_o[5];
785
    assign eoc[4] = (wbm_ack_i[4] & (wbm4_cti_o == 3'b000 | wbm4_cti_o == 3'b111)) | !wbm_cyc_o[4];
786
    assign eoc[3] = (wbm_ack_i[3] & (wbm3_cti_o == 3'b000 | wbm3_cti_o == 3'b111)) | !wbm_cyc_o[3];
787
    assign eoc[2] = (wbm_ack_i[2] & (wbm2_cti_o == 3'b000 | wbm2_cti_o == 3'b111)) | !wbm_cyc_o[2];
788
    assign eoc[1] = (wbm_ack_i[1] & (wbm1_cti_o == 3'b000 | wbm1_cti_o == 3'b111)) | !wbm_cyc_o[1];
789
    assign eoc[0] = (wbm_ack_i[0] & (wbm0_cti_o == 3'b000 | wbm0_cti_o == 3'b111)) | !wbm_cyc_o[0];
790
 
791
end
792
endgenerate
793
 
794
generate
795 63 unneback
for (i=0;i<nr_of_ports;i=i+1) begin : spr0
796 42 unneback
`define MODULE spr
797
    `BASE`MODULE sr0( .sp(select[i]), .r(eoc[i]), .q(state[i]), .clk(wb_clk), .rst(wb_rst));
798
`undef MODULE
799 39 unneback
end
800
endgenerate
801
 
802
    assign sel = select | state;
803
 
804 40 unneback
`define MODULE mux_andor
805
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(32)) mux0 ( .a(wbm_dat_o), .sel(sel), .dout(wbs_dat_i));
806
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(adr_size-adr_lo)) mux1 ( .a(wbm_adr_o), .sel(sel), .dout(wbs_adr_i));
807
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(sel_size)) mux2 ( .a(wbm_sel_o), .sel(sel), .dout(wbs_sel_i));
808
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(3)) mux3 ( .a(wbm_cti_o), .sel(sel), .dout(wbs_cti_i));
809
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(2)) mux4 ( .a(wbm_bte_o), .sel(sel), .dout(wbs_bte_i));
810
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux5 ( .a(wbm_we_o), .sel(sel), .dout(wbs_we_i));
811
    `BASE`MODULE # ( .nr_of_ports(nr_of_ports), .width(1)) mux6 ( .a(wbm_stb_o), .sel(sel), .dout(wbs_stb_i));
812
`undef MODULE
813 39 unneback
    assign wbs_cyc_i = |sel;
814
 
815
    assign wbm_dat_i = {nr_of_ports{wbs_dat_o}};
816
    assign wbm_ack_i = {nr_of_ports{wbs_ack_o}} & sel;
817
    assign wbm_err_i = {nr_of_ports{wbs_err_o}} & sel;
818
    assign wbm_rty_i = {nr_of_ports{wbs_rty_o}} & sel;
819
 
820
endmodule
821 40 unneback
`endif
822 39 unneback
 
823 101 unneback
`ifdef WB_RAM
824 49 unneback
// WB RAM with byte enable
825 101 unneback
`define MODULE wb_ram
826 59 unneback
module `BASE`MODULE (
827
`undef MODULE
828 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,
829 101 unneback
    wbs_dat_o, wbs_ack_o, wbs_stall_o, wb_clk, wb_rst);
830 59 unneback
 
831 101 unneback
parameter adr_width = 16;
832
parameter mem_size = 1<<adr_width;
833
parameter dat_width = 32;
834
parameter max_burst_width = 4; // only used for B3
835
parameter mode = "B3"; // valid options: B3, B4
836 60 unneback
parameter memory_init = 1;
837
parameter memory_file = "vl_ram.vmem";
838 59 unneback
 
839 101 unneback
input [dat_width-1:0] wbs_dat_i;
840
input [adr_width-1:0] wbs_adr_i;
841
input [2:0] wbs_cti_i;
842
input [1:0] wbs_bte_i;
843
input [dat_width/8-1:0] wbs_sel_i;
844 70 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
845 101 unneback
output [dat_width-1:0] wbs_dat_o;
846 70 unneback
output wbs_ack_o;
847 101 unneback
output wbs_stall_o;
848 71 unneback
input wb_clk, wb_rst;
849 59 unneback
 
850 101 unneback
wire [adr_width-1:0] adr;
851
wire we;
852 59 unneback
 
853 101 unneback
generate
854
if (mode=="B3") begin : B3_inst
855 83 unneback
`define MODULE wb_adr_inc
856 101 unneback
`BASE`MODULE # ( .adr_width(adr_width), .max_burst_width(max_burst_width)) adr_inc0 (
857 83 unneback
    .cyc_i(wbs_cyc_i),
858
    .stb_i(wbs_stb_i),
859
    .cti_i(wbs_cti_i),
860
    .bte_i(wbs_bte_i),
861
    .adr_i(wbs_adr_i),
862 84 unneback
    .we_i(wbs_we_i),
863 83 unneback
    .ack_o(wbs_ack_o),
864
    .adr_o(adr),
865
    .clk(wb_clk),
866
    .rst(wb_rst));
867
`undef MODULE
868 101 unneback
assign we = wbs_we_i & wbs_ack_o;
869
end else if (mode=="B4") begin : B4_inst
870
reg wbs_ack_o_reg;
871
always @ (posedge wb_clk or posedge wb_rst)
872
    if (wb_rst)
873
        wbs_ack_o_reg <= 1'b0;
874
    else
875
        wbs_ack_o_reg <= wbs_stb_i & wbs_cyc_i;
876
assign wbs_ack_o = wbs_ack_o_reg;
877
assign wbs_stall_o = 1'b0;
878
assign adr = wbs_adr_i;
879
assign we = wbs_we_i & wbs_cyc_i & wbs_stb_i;
880
end
881
endgenerate
882 60 unneback
 
883 100 unneback
`define MODULE ram_be
884
`BASE`MODULE # (
885
    .data_width(dat_width),
886
    .addr_width(adr_width),
887
    .mem_size(mem_size),
888
    .memory_init(memory_init),
889
    .memory_file(memory_file))
890
ram0(
891
`undef MODULE
892 101 unneback
    .d(wbs_dat_i),
893
    .adr(adr),
894
    .be(wbs_sel_i),
895
    .we(we),
896
    .q(wbs_dat_o),
897 100 unneback
    .clk(wb_clk)
898
);
899 49 unneback
 
900
endmodule
901
`endif
902
 
903 103 unneback
`ifdef WB_SHADOW_RAM
904
// A wishbone compliant RAM module that can be placed in front of other memory controllers
905
`define MODULE wb_shadow_ram
906
module `BASE`MODULE (
907
`undef MODULE
908
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
909
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
910
    wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o, wbm_cyc_o,
911
    wbm_dat_i, wbm_ack_i, wbm_stall_i,
912
    wb_clk, wb_rst);
913
 
914
parameter dat_width = 32;
915
parameter mode = "B4";
916
parameter max_burst_width = 4; // only used for B3
917
 
918
parameter shadow_mem_adr_width = 10;
919
parameter shadow_mem_size = 1024;
920
parameter shadow_mem_init = 2;
921
parameter shadow_mem_file = "vl_ram.v";
922
 
923
parameter main_mem_adr_width = 24;
924
 
925
input [dat_width-1:0] wbs_dat_i;
926
input [main_mem_adr_width-1:0] wbs_adr_i;
927
input [2:0] wbs_cti_i;
928
input [1:0] wbs_bte_i;
929
input [dat_width/8-1:0] wbs_sel_i;
930
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
931
output [dat_width-1:0] wbs_dat_o;
932
output wbs_ack_o;
933
output wbs_stall_o;
934
 
935
output [dat_width-1:0] wbm_dat_o;
936
output [main_mem_adr_width-1:0] wbm_adr_o;
937
output [2:0] wbm_cti_o;
938
output [1:0] wbm_bte_o;
939
output [dat_width/8-1:0] wbm_sel_o;
940
output wbm_we_o, wbm_stb_o, wbm_cyc_o;
941
input [dat_width-1:0] wbm_dat_i;
942
input wbm_ack_i, wbm_stall_i;
943
 
944
input wb_clk, wb_rst;
945
 
946
generate
947
if (shadow_mem_size>0) begin : shadow_ram_inst
948
 
949
wire cyc;
950
wire [dat_width-1:0] dat;
951
wire stall, ack;
952
 
953
assign cyc = wbs_cyc_i & (wbs_adr_i<=shadow_mem_size);
954
`define MODULE wb_ram
955
`BASE`MODULE # (
956
    .dat_width(dat_width),
957
    .adr_width(shadow_mem_adr_width),
958
    .mem_size(shadow_mem_size),
959
    .memory_init(shadow_mem_init),
960 117 unneback
    .memory_file(shadow_mem_file),
961 103 unneback
    .mode(mode))
962
shadow_mem0 (
963
    .wbs_dat_i(wbs_dat_i),
964
    .wbs_adr_i(wbs_adr_i[shadow_mem_adr_width-1:0]),
965
    .wbs_sel_i(wbs_sel_i),
966
    .wbs_we_i (wbs_we_i),
967
    .wbs_bte_i(wbs_bte_i),
968
    .wbs_cti_i(wbs_cti_i),
969
    .wbs_stb_i(wbs_stb_i),
970
    .wbs_cyc_i(cyc),
971
    .wbs_dat_o(dat),
972
    .wbs_stall_o(stall),
973
    .wbs_ack_o(ack),
974
    .wb_clk(wb_clk),
975
    .wb_rst(wb_rst));
976
`undef MODULE
977
 
978
assign {wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o} =
979
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i};
980
assign wbm_cyc_o = wbs_cyc_i & (wbs_adr_i>shadow_mem_size);
981
 
982
assign wbs_dat_o = (dat & {dat_width{cyc}}) | (wbm_dat_i & {dat_width{wbm_cyc_o}});
983
assign wbs_ack_o = (ack & cyc) | (wbm_ack_i & wbm_cyc_o);
984
assign wbs_stall_o = (stall & cyc) | (wbm_stall_i & wbm_cyc_o);
985
 
986
end else begin : no_shadow_ram_inst
987
 
988
assign {wbm_dat_o, wbm_adr_o, wbm_cti_o, wbm_bte_o, wbm_sel_o, wbm_we_o, wbm_stb_o, wbm_cyc_o} =
989
       {wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i};
990
assign {wbs_dat_o, wbs_ack_o, wbs_stall_o} = {wbm_dat_i, wbm_ack_i, wbm_stall_i};
991
 
992
end
993
endgenerate
994
 
995
endmodule
996
`endif
997
 
998 48 unneback
`ifdef WB_B4_ROM
999
// WB ROM
1000
`define MODULE wb_b4_rom
1001
module `BASE`MODULE (
1002
`undef MODULE
1003
    wb_adr_i, wb_stb_i, wb_cyc_i,
1004
    wb_dat_o, stall_o, wb_ack_o, wb_clk, wb_rst);
1005
 
1006
    parameter dat_width = 32;
1007
    parameter dat_default = 32'h15000000;
1008
    parameter adr_width = 32;
1009
 
1010
/*
1011
//E2_ifndef ROM
1012
//E2_define ROM "rom.v"
1013
//E2_endif
1014
*/
1015
    input [adr_width-1:2]   wb_adr_i;
1016
    input                   wb_stb_i;
1017
    input                   wb_cyc_i;
1018
    output [dat_width-1:0]  wb_dat_o;
1019
    reg [dat_width-1:0]     wb_dat_o;
1020
    output                  wb_ack_o;
1021
    reg                     wb_ack_o;
1022
    output                  stall_o;
1023
    input                   wb_clk;
1024
    input                   wb_rst;
1025
 
1026
always @ (posedge wb_clk or posedge wb_rst)
1027
    if (wb_rst)
1028
        wb_dat_o <= {dat_width{1'b0}};
1029
    else
1030
         case (wb_adr_i[adr_width-1:2])
1031
//E2_ifdef ROM
1032
//E2_include `ROM
1033
//E2_endif
1034
           default:
1035
             wb_dat_o <= dat_default;
1036
 
1037
         endcase // case (wb_adr_i)
1038
 
1039
 
1040
always @ (posedge wb_clk or posedge wb_rst)
1041
    if (wb_rst)
1042
        wb_ack_o <= 1'b0;
1043
    else
1044
        wb_ack_o <= wb_stb_i & wb_cyc_i;
1045
 
1046
assign stall_o = 1'b0;
1047
 
1048
endmodule
1049
`endif
1050
 
1051
 
1052 40 unneback
`ifdef WB_BOOT_ROM
1053 17 unneback
// WB ROM
1054 40 unneback
`define MODULE wb_boot_rom
1055
module `BASE`MODULE (
1056
`undef MODULE
1057 17 unneback
    wb_adr_i, wb_stb_i, wb_cyc_i,
1058 18 unneback
    wb_dat_o, wb_ack_o, hit_o, wb_clk, wb_rst);
1059 17 unneback
 
1060 18 unneback
    parameter adr_hi = 31;
1061
    parameter adr_lo = 28;
1062
    parameter adr_sel = 4'hf;
1063
    parameter addr_width = 5;
1064 33 unneback
/*
1065 17 unneback
//E2_ifndef BOOT_ROM
1066
//E2_define BOOT_ROM "boot_rom.v"
1067
//E2_endif
1068 33 unneback
*/
1069 18 unneback
    input [adr_hi:2]    wb_adr_i;
1070
    input               wb_stb_i;
1071
    input               wb_cyc_i;
1072
    output [31:0]        wb_dat_o;
1073
    output              wb_ack_o;
1074
    output              hit_o;
1075
    input               wb_clk;
1076
    input               wb_rst;
1077
 
1078
    wire hit;
1079
    reg [31:0] wb_dat;
1080
    reg wb_ack;
1081
 
1082
assign hit = wb_adr_i[adr_hi:adr_lo] == adr_sel;
1083 17 unneback
 
1084
always @ (posedge wb_clk or posedge wb_rst)
1085
    if (wb_rst)
1086 18 unneback
        wb_dat <= 32'h15000000;
1087 17 unneback
    else
1088 18 unneback
         case (wb_adr_i[addr_width-1:2])
1089 33 unneback
//E2_ifdef BOOT_ROM
1090 17 unneback
//E2_include `BOOT_ROM
1091 33 unneback
//E2_endif
1092 17 unneback
           /*
1093
            // Zero r0 and jump to 0x00000100
1094 18 unneback
 
1095
            1 : wb_dat <= 32'hA8200000;
1096
            2 : wb_dat <= 32'hA8C00100;
1097
            3 : wb_dat <= 32'h44003000;
1098
            4 : wb_dat <= 32'h15000000;
1099 17 unneback
            */
1100
           default:
1101 18 unneback
             wb_dat <= 32'h00000000;
1102 17 unneback
 
1103
         endcase // case (wb_adr_i)
1104
 
1105
 
1106
always @ (posedge wb_clk or posedge wb_rst)
1107
    if (wb_rst)
1108 18 unneback
        wb_ack <= 1'b0;
1109 17 unneback
    else
1110 18 unneback
        wb_ack <= wb_stb_i & wb_cyc_i & hit & !wb_ack;
1111 17 unneback
 
1112 18 unneback
assign hit_o = hit;
1113
assign wb_dat_o = wb_dat & {32{wb_ack}};
1114
assign wb_ack_o = wb_ack;
1115
 
1116 17 unneback
endmodule
1117 40 unneback
`endif
1118 32 unneback
 
1119 106 unneback
`ifdef WB_DPRAM
1120
`define MODULE wb_dpram
1121 40 unneback
module `BASE`MODULE (
1122
`undef MODULE
1123 32 unneback
        // wishbone slave side a
1124 106 unneback
        wbsa_dat_i, wbsa_adr_i, wbsa_sel_i, wbsa_cti_i, wbsa_bte_i, wbsa_we_i, wbsa_cyc_i, wbsa_stb_i, wbsa_dat_o, wbsa_ack_o, wbsa_stall_o,
1125 32 unneback
        wbsa_clk, wbsa_rst,
1126 92 unneback
        // wishbone slave side b
1127 106 unneback
        wbsb_dat_i, wbsb_adr_i, wbsb_sel_i, wbsb_cti_i, wbsb_bte_i, wbsb_we_i, wbsb_cyc_i, wbsb_stb_i, wbsb_dat_o, wbsb_ack_o, wbsb_stall_o,
1128 32 unneback
        wbsb_clk, wbsb_rst);
1129
 
1130 92 unneback
parameter data_width_a = 32;
1131
parameter data_width_b = data_width_a;
1132
parameter addr_width_a = 8;
1133
localparam addr_width_b = data_width_a * addr_width_a / data_width_b;
1134 101 unneback
parameter mem_size = (addr_width_a>addr_width_b) ? (1<<addr_width_a) : (1<<addr_width_b);
1135 92 unneback
parameter max_burst_width_a = 4;
1136
parameter max_burst_width_b = max_burst_width_a;
1137 101 unneback
parameter mode = "B3";
1138 109 unneback
parameter memory_init = 0;
1139
parameter memory_file = "vl_ram.v";
1140 142 unneback
parameter debug = 0;
1141 92 unneback
input [data_width_a-1:0] wbsa_dat_i;
1142
input [addr_width_a-1:0] wbsa_adr_i;
1143
input [data_width_a/8-1:0] wbsa_sel_i;
1144
input [2:0] wbsa_cti_i;
1145
input [1:0] wbsa_bte_i;
1146 32 unneback
input wbsa_we_i, wbsa_cyc_i, wbsa_stb_i;
1147 92 unneback
output [data_width_a-1:0] wbsa_dat_o;
1148 109 unneback
output wbsa_ack_o;
1149 106 unneback
output wbsa_stall_o;
1150 32 unneback
input wbsa_clk, wbsa_rst;
1151
 
1152 92 unneback
input [data_width_b-1:0] wbsb_dat_i;
1153
input [addr_width_b-1:0] wbsb_adr_i;
1154
input [data_width_b/8-1:0] wbsb_sel_i;
1155
input [2:0] wbsb_cti_i;
1156
input [1:0] wbsb_bte_i;
1157 32 unneback
input wbsb_we_i, wbsb_cyc_i, wbsb_stb_i;
1158 92 unneback
output [data_width_b-1:0] wbsb_dat_o;
1159 109 unneback
output wbsb_ack_o;
1160 106 unneback
output wbsb_stall_o;
1161 32 unneback
input wbsb_clk, wbsb_rst;
1162
 
1163 92 unneback
wire [addr_width_a-1:0] adr_a;
1164
wire [addr_width_b-1:0] adr_b;
1165 101 unneback
wire we_a, we_b;
1166
generate
1167
if (mode=="B3") begin : b3_inst
1168 92 unneback
`define MODULE wb_adr_inc
1169
`BASE`MODULE # ( .adr_width(addr_width_a), .max_burst_width(max_burst_width_a)) adr_inc0 (
1170
    .cyc_i(wbsa_cyc_i),
1171
    .stb_i(wbsa_stb_i),
1172
    .cti_i(wbsa_cti_i),
1173
    .bte_i(wbsa_bte_i),
1174
    .adr_i(wbsa_adr_i),
1175
    .we_i(wbsa_we_i),
1176
    .ack_o(wbsa_ack_o),
1177
    .adr_o(adr_a),
1178
    .clk(wbsa_clk),
1179
    .rst(wbsa_rst));
1180 101 unneback
assign we_a = wbsa_we_i & wbsa_ack_o;
1181 92 unneback
`BASE`MODULE # ( .adr_width(addr_width_b), .max_burst_width(max_burst_width_b)) adr_inc1 (
1182
    .cyc_i(wbsb_cyc_i),
1183
    .stb_i(wbsb_stb_i),
1184
    .cti_i(wbsb_cti_i),
1185
    .bte_i(wbsb_bte_i),
1186
    .adr_i(wbsb_adr_i),
1187
    .we_i(wbsb_we_i),
1188
    .ack_o(wbsb_ack_o),
1189
    .adr_o(adr_b),
1190
    .clk(wbsb_clk),
1191
    .rst(wbsb_rst));
1192 40 unneback
`undef MODULE
1193 101 unneback
assign we_b = wbsb_we_i & wbsb_ack_o;
1194
end else if (mode=="B4") begin : b4_inst
1195 142 unneback
assign adr_a = wbsa_adr_i;
1196 109 unneback
`define MODULE dff
1197
`BASE`MODULE dffacka ( .d(wbsa_stb_i & wbsa_cyc_i), .q(wbsa_ack_o), .clk(wbsa_clk), .rst(wbsa_rst));
1198 101 unneback
assign wbsa_stall_o = 1'b0;
1199
assign we_a = wbsa_we_i & wbsa_cyc_i & wbsa_stb_i;
1200 142 unneback
assign adr_b = wbsb_adr_i;
1201 109 unneback
`BASE`MODULE dffackb ( .d(wbsb_stb_i & wbsb_cyc_i), .q(wbsb_ack_o), .clk(wbsb_clk), .rst(wbsb_rst));
1202
`undef MODULE
1203 101 unneback
assign wbsb_stall_o = 1'b0;
1204
assign we_b = wbsb_we_i & wbsb_cyc_i & wbsb_stb_i;
1205
end
1206
endgenerate
1207 92 unneback
 
1208
`define MODULE dpram_be_2r2w
1209 109 unneback
`BASE`MODULE # ( .a_data_width(data_width_a), .a_addr_width(addr_width_a), .mem_size(mem_size),
1210 110 unneback
                 .b_data_width(data_width_b),
1211 142 unneback
                 .memory_init(memory_init), .memory_file(memory_file),
1212
                 .debug(debug))
1213 92 unneback
`undef MODULE
1214
ram_i (
1215 32 unneback
    .d_a(wbsa_dat_i),
1216 92 unneback
    .q_a(wbsa_dat_o),
1217
    .adr_a(adr_a),
1218
    .be_a(wbsa_sel_i),
1219 101 unneback
    .we_a(we_a),
1220 32 unneback
    .clk_a(wbsa_clk),
1221
    .d_b(wbsb_dat_i),
1222 92 unneback
    .q_b(wbsb_dat_o),
1223
    .adr_b(adr_b),
1224
    .be_b(wbsb_sel_i),
1225 101 unneback
    .we_b(we_b),
1226 32 unneback
    .clk_b(wbsb_clk) );
1227
 
1228
endmodule
1229 40 unneback
`endif
1230 94 unneback
 
1231 101 unneback
`ifdef WB_CACHE
1232
`define MODULE wb_cache
1233 96 unneback
module `BASE`MODULE (
1234 103 unneback
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i, wbs_dat_o, wbs_ack_o, wbs_stall_o, wbs_clk, wbs_rst,
1235 98 unneback
    wbm_dat_o, wbm_adr_o, wbm_sel_o, wbm_cti_o, wbm_bte_o, wbm_we_o, wbm_stb_o, wbm_cyc_o, wbm_dat_i, wbm_ack_i, wbm_stall_i, wbm_clk, wbm_rst
1236 96 unneback
);
1237
`undef MODULE
1238
 
1239
parameter dw_s = 32;
1240
parameter aw_s = 24;
1241
parameter dw_m = dw_s;
1242 124 unneback
//localparam aw_m = dw_s * aw_s / dw_m;
1243
localparam aw_m =
1244 126 unneback
        (dw_s==dw_m) ? aw_s :
1245
        (dw_s==dw_m*2) ? aw_s+1 :
1246
        (dw_s==dw_m*4) ? aw_s+2 :
1247
        (dw_s==dw_m*8) ? aw_s+3 :
1248
        (dw_s==dw_m*16) ? aw_s+4 :
1249
        (dw_s==dw_m*32) ? aw_s+5 :
1250
        (dw_s==dw_m/2) ? aw_s-1 :
1251 127 unneback
        (dw_s==dw_m/4) ? aw_s-2 :
1252 126 unneback
        (dw_s==dw_m/8) ? aw_s-3 :
1253
        (dw_s==dw_m/16) ? aw_s-4 :
1254
        (dw_s==dw_m/32) ? aw_s-5 : 0;
1255 124 unneback
 
1256 100 unneback
parameter wbs_max_burst_width = 4;
1257 103 unneback
parameter wbs_mode = "B3";
1258 96 unneback
 
1259 97 unneback
parameter async = 1; // wbs_clk != wbm_clk
1260
 
1261 96 unneback
parameter nr_of_ways = 1;
1262 97 unneback
parameter aw_offset = 4; // 4 => 16 words per cache line
1263
parameter aw_slot = 10;
1264 100 unneback
 
1265
parameter valid_mem = 0;
1266
parameter debug = 0;
1267
 
1268
localparam aw_b_offset = aw_offset * dw_s / dw_m;
1269 98 unneback
localparam aw_tag = aw_s - aw_slot - aw_offset;
1270 97 unneback
parameter wbm_burst_size = 4; // valid options 4,8,16
1271 98 unneback
localparam bte = (wbm_burst_size==4) ? 2'b01 : (wbm_burst_size==8) ? 2'b10 : 2'b11;
1272 97 unneback
`define SIZE2WIDTH wbm_burst_size
1273
localparam wbm_burst_width `SIZE2WIDTH_EXPR
1274
`undef SIZE2WIDTH
1275
localparam nr_of_wbm_burst = ((1<<aw_offset)/wbm_burst_size) * dw_s / dw_m;
1276
`define SIZE2WIDTH nr_of_wbm_burst
1277
localparam nr_of_wbm_burst_width `SIZE2WIDTH_EXPR
1278
`undef SIZE2WIDTH
1279 100 unneback
 
1280 96 unneback
input [dw_s-1:0] wbs_dat_i;
1281
input [aw_s-1:0] wbs_adr_i; // dont include a1,a0
1282 98 unneback
input [dw_s/8-1:0] wbs_sel_i;
1283 96 unneback
input [2:0] wbs_cti_i;
1284
input [1:0] wbs_bte_i;
1285 98 unneback
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
1286 96 unneback
output [dw_s-1:0] wbs_dat_o;
1287
output wbs_ack_o;
1288 103 unneback
output wbs_stall_o;
1289 96 unneback
input wbs_clk, wbs_rst;
1290
 
1291
output [dw_m-1:0] wbm_dat_o;
1292
output [aw_m-1:0] wbm_adr_o;
1293
output [dw_m/8-1:0] wbm_sel_o;
1294
output [2:0] wbm_cti_o;
1295
output [1:0] wbm_bte_o;
1296 98 unneback
output wbm_stb_o, wbm_cyc_o, wbm_we_o;
1297 96 unneback
input [dw_m-1:0] wbm_dat_i;
1298
input wbm_ack_i;
1299
input wbm_stall_i;
1300
input wbm_clk, wbm_rst;
1301
 
1302 100 unneback
wire valid, dirty, hit;
1303 97 unneback
wire [aw_tag-1:0] tag;
1304
wire tag_mem_we;
1305
wire [aw_tag-1:0] wbs_adr_tag;
1306
wire [aw_slot-1:0] wbs_adr_slot;
1307 98 unneback
wire [aw_offset-1:0] wbs_adr_word;
1308
wire [aw_s-1:0] wbs_adr;
1309 96 unneback
 
1310 97 unneback
reg [1:0] state;
1311
localparam idle = 2'h0;
1312
localparam rdwr = 2'h1;
1313
localparam push = 2'h2;
1314
localparam pull = 2'h3;
1315
wire eoc;
1316 103 unneback
wire we;
1317 97 unneback
 
1318
// cdc
1319
wire done, mem_alert, mem_done;
1320
 
1321 98 unneback
// wbm side
1322
reg [aw_m-1:0] wbm_radr;
1323
reg [aw_m-1:0] wbm_wadr;
1324 137 unneback
//wire [aw_slot-1:0] wbm_adr;
1325
wire [aw_m-1:0] wbm_adr;
1326 98 unneback
wire wbm_radr_cke, wbm_wadr_cke;
1327
 
1328 100 unneback
reg [2:0] phase;
1329
// phase = {we,stb,cyc}
1330
localparam wbm_wait     = 3'b000;
1331
localparam wbm_wr       = 3'b111;
1332
localparam wbm_wr_drain = 3'b101;
1333
localparam wbm_rd       = 3'b011;
1334
localparam wbm_rd_drain = 3'b001;
1335 98 unneback
 
1336 97 unneback
assign {wbs_adr_tag, wbs_adr_slot, wbs_adr_word} = wbs_adr_i;
1337
 
1338 100 unneback
generate
1339
if (valid_mem==0) begin : no_valid_mem
1340
assign valid = 1'b1;
1341
end else begin : valid_mem_inst
1342
`define MODULE dpram_1r1w
1343 97 unneback
`BASE`MODULE
1344 100 unneback
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
1345
    valid_mem ( .d_a(1'b1), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
1346
                .q_b(valid), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
1347 97 unneback
`undef MODULE
1348 100 unneback
end
1349
endgenerate
1350 97 unneback
 
1351 100 unneback
`define MODULE dpram_1r1w
1352
`BASE`MODULE
1353
    # ( .data_width(aw_tag), .addr_width(aw_slot), .memory_init(2), .debug(debug))
1354
    tag_mem ( .d_a(wbs_adr_tag), .adr_a(wbs_adr_slot), .we_a(mem_done), .clk_a(wbm_clk),
1355
              .q_b(tag), .adr_b(wbs_adr_slot), .clk_b(wbs_clk));
1356
assign hit = wbs_adr_tag == tag;
1357
`undef MODULE
1358
 
1359
`define MODULE dpram_1r2w
1360
`BASE`MODULE
1361
    # ( .data_width(1), .addr_width(aw_slot), .memory_init(2), .debug(debug))
1362
    dirty_mem (
1363
        .d_a(1'b1), .q_a(dirty), .adr_a(wbs_adr_slot), .we_a(wbs_cyc_i & wbs_we_i & wbs_ack_o), .clk_a(wbs_clk),
1364
        .d_b(1'b0), .adr_b(wbs_adr_slot), .we_b(mem_done), .clk_b(wbm_clk));
1365
`undef MODULE
1366
 
1367 103 unneback
generate
1368
if (wbs_mode=="B3") begin : inst_b3
1369 96 unneback
`define MODULE wb_adr_inc
1370 100 unneback
`BASE`MODULE # ( .adr_width(aw_s), .max_burst_width(wbs_max_burst_width)) adr_inc0 (
1371
    .cyc_i(wbs_cyc_i & (state==rdwr) & hit & valid),
1372
    .stb_i(wbs_stb_i & (state==rdwr) & hit & valid), // throttle depending on valid
1373 96 unneback
    .cti_i(wbs_cti_i),
1374
    .bte_i(wbs_bte_i),
1375
    .adr_i(wbs_adr_i),
1376 97 unneback
    .we_i (wbs_we_i),
1377 96 unneback
    .ack_o(wbs_ack_o),
1378 97 unneback
    .adr_o(wbs_adr),
1379 100 unneback
    .clk(wbs_clk),
1380
    .rst(wbs_rst));
1381 96 unneback
`undef MODULE
1382 103 unneback
assign eoc = (wbs_cti_i==3'b000 | wbs_cti_i==3'b111) & wbs_ack_o;
1383
assign we = wbs_cyc_i &  wbs_we_i & wbs_ack_o;
1384
end else if (wbs_mode=="B4") begin : inst_b4
1385
end
1386 96 unneback
 
1387 103 unneback
endgenerate
1388 131 unneback
localparam cache_mem_b_aw =
1389
    (dw_s==dw_m) ? aw_slot+aw_offset :
1390 133 unneback
    (dw_s==dw_m/2) ? aw_slot+aw_offset-1 :
1391
    (dw_s==dw_m/4) ? aw_slot+aw_offset-2 :
1392
    (dw_s==dw_m/8) ? aw_slot+aw_offset-3 :
1393
    (dw_s==dw_m/16) ? aw_slot+aw_offset-4 :
1394
    (dw_s==dw_m*2) ? aw_slot+aw_offset+1 :
1395
    (dw_s==dw_m*4) ? aw_slot+aw_offset+2 :
1396
    (dw_s==dw_m*8) ? aw_slot+aw_offset+3 :
1397
    (dw_s==dw_m*16) ? aw_slot+aw_offset+4 : 0;
1398 103 unneback
 
1399 97 unneback
`define MODULE dpram_be_2r2w
1400
`BASE`MODULE
1401 100 unneback
    # ( .a_data_width(dw_s), .a_addr_width(aw_slot+aw_offset), .b_data_width(dw_m), .debug(debug))
1402 103 unneback
    cache_mem ( .d_a(wbs_dat_i), .adr_a(wbs_adr[aw_slot+aw_offset-1:0]),   .be_a(wbs_sel_i), .we_a(we), .q_a(wbs_dat_o), .clk_a(wbs_clk),
1403 136 unneback
                .d_b(wbm_dat_i), .adr_b(wbm_adr[cache_mem_b_aw-1:0]), .be_b(wbm_sel_o), .we_b(wbm_cyc_o & !wbm_we_o & wbm_ack_i), .q_b(wbm_dat_o), .clk_b(wbm_clk));
1404 97 unneback
`undef MODULE
1405
 
1406
always @ (posedge wbs_clk or posedge wbs_rst)
1407
if (wbs_rst)
1408 98 unneback
    state <= idle;
1409 97 unneback
else
1410
    case (state)
1411
    idle:
1412
        if (wbs_cyc_i)
1413
            state <= rdwr;
1414
    rdwr:
1415 100 unneback
        casex ({valid, hit, dirty, eoc})
1416
        4'b0xxx: state <= pull;
1417
        4'b11x1: state <= idle;
1418
        4'b101x: state <= push;
1419
        4'b100x: state <= pull;
1420
        endcase
1421 97 unneback
    push:
1422
        if (done)
1423
            state <= rdwr;
1424
    pull:
1425
        if (done)
1426
            state <= rdwr;
1427
    default: state <= idle;
1428
    endcase
1429
 
1430
// cdc
1431
generate
1432
if (async==1) begin : cdc0
1433
`define MODULE cdc
1434 100 unneback
`BASE`MODULE cdc0 ( .start_pl(state==rdwr & (!valid | !hit)), .take_it_pl(mem_alert), .take_it_grant_pl(mem_done), .got_it_pl(done), .clk_src(wbs_clk), .rst_src(wbs_rst), .clk_dst(wbm_clk), .rst_dst(wbm_rst));
1435 97 unneback
`undef MODULE
1436
end
1437
else begin : nocdc
1438 100 unneback
    assign mem_alert = state==rdwr & (!valid | !hit);
1439 97 unneback
    assign done = mem_done;
1440
end
1441
endgenerate
1442
 
1443 136 unneback
// FSM generating a number of bursts 4 cycles
1444 97 unneback
// actual number depends on data width ratio
1445
// nr_of_wbm_burst
1446 101 unneback
reg [nr_of_wbm_burst_width+wbm_burst_width-1:0]       cnt_rw, cnt_ack;
1447 97 unneback
 
1448
always @ (posedge wbm_clk or posedge wbm_rst)
1449
if (wbm_rst)
1450 100 unneback
    cnt_rw <= {wbm_burst_width{1'b0}};
1451 97 unneback
else
1452 100 unneback
    if (wbm_cyc_o & wbm_stb_o & !wbm_stall_i)
1453
        cnt_rw <= cnt_rw + 1;
1454 97 unneback
 
1455 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
1456
if (wbm_rst)
1457 100 unneback
    cnt_ack <= {wbm_burst_width{1'b0}};
1458 98 unneback
else
1459 100 unneback
    if (wbm_ack_i)
1460
        cnt_ack <= cnt_ack + 1;
1461 97 unneback
 
1462 100 unneback
generate
1463 101 unneback
if (nr_of_wbm_burst==1) begin : one_burst
1464 100 unneback
 
1465 98 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
1466
if (wbm_rst)
1467
    phase <= wbm_wait;
1468
else
1469
    case (phase)
1470
    wbm_wait:
1471
        if (mem_alert)
1472 100 unneback
            if (state==push)
1473
                phase <= wbm_wr;
1474
            else
1475
                phase <= wbm_rd;
1476 98 unneback
    wbm_wr:
1477 100 unneback
        if (&cnt_rw)
1478
            phase <= wbm_wr_drain;
1479
    wbm_wr_drain:
1480
        if (&cnt_ack)
1481 98 unneback
            phase <= wbm_rd;
1482
    wbm_rd:
1483 100 unneback
        if (&cnt_rw)
1484
            phase <= wbm_rd_drain;
1485
    wbm_rd_drain:
1486
        if (&cnt_ack)
1487
            phase <= wbm_wait;
1488 98 unneback
    default: phase <= wbm_wait;
1489
    endcase
1490
 
1491 100 unneback
end else begin : multiple_burst
1492
 
1493 101 unneback
always @ (posedge wbm_clk or posedge wbm_rst)
1494
if (wbm_rst)
1495
    phase <= wbm_wait;
1496
else
1497
    case (phase)
1498
    wbm_wait:
1499
        if (mem_alert)
1500
            if (state==push)
1501
                phase <= wbm_wr;
1502
            else
1503
                phase <= wbm_rd;
1504
    wbm_wr:
1505
        if (&cnt_rw[wbm_burst_width-1:0])
1506
            phase <= wbm_wr_drain;
1507
    wbm_wr_drain:
1508
        if (&cnt_ack)
1509
            phase <= wbm_rd;
1510
        else if (&cnt_ack[wbm_burst_width-1:0])
1511
            phase <= wbm_wr;
1512
    wbm_rd:
1513
        if (&cnt_rw[wbm_burst_width-1:0])
1514
            phase <= wbm_rd_drain;
1515
    wbm_rd_drain:
1516
        if (&cnt_ack)
1517
            phase <= wbm_wait;
1518
        else if (&cnt_ack[wbm_burst_width-1:0])
1519
            phase <= wbm_rd;
1520
    default: phase <= wbm_wait;
1521
    endcase
1522 100 unneback
 
1523 101 unneback
 
1524 100 unneback
end
1525
endgenerate
1526
 
1527 101 unneback
assign mem_done = phase==wbm_rd_drain & (&cnt_ack) & wbm_ack_i;
1528 100 unneback
 
1529
assign wbm_adr_o = (phase[2]) ? {tag, wbs_adr_slot, cnt_rw} : {wbs_adr_tag, wbs_adr_slot, cnt_rw};
1530 137 unneback
assign wbm_adr   = (phase[2]) ? {wbs_adr_slot, cnt_rw} : {wbs_adr_slot, cnt_ack};
1531 100 unneback
assign wbm_sel_o = {dw_m/8{1'b1}};
1532
assign wbm_cti_o = (&cnt_rw | !wbm_stb_o) ? 3'b111 : 3'b010;
1533 98 unneback
assign wbm_bte_o = bte;
1534 100 unneback
assign {wbm_we_o, wbm_stb_o, wbm_cyc_o}  = phase;
1535 98 unneback
 
1536 96 unneback
endmodule
1537
`endif
1538 103 unneback
 
1539
`ifdef WB_AVALON_BRIDGE
1540
// Wishbone to avalon bridge supporting one type of burst transfer only
1541
// intended use is together with cache above
1542
// WB B4 -> pipelined avalon
1543
`define MODULE wb_avalon_bridge
1544
module `BASE`MODULE (
1545
`undef MODULE
1546
        // wishbone slave side
1547
        wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_bte_i, wbs_cti_i, wbs_we_i, wbs_cyc_i, wbs_stb_i, wbs_dat_o, wbs_ack_o, wbs_stall_o,
1548
        // avalon master side
1549
        readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer,
1550 135 unneback
        init_done,
1551 103 unneback
        // common
1552
        clk, rst);
1553
 
1554
parameter adr_width = 30;
1555
parameter dat_width = 32;
1556
parameter burst_size = 4;
1557
 
1558
input [dat_width-1:0] wbs_dat_i;
1559
input [adr_width-1:0] wbs_adr_i;
1560
input [dat_width/8-1:0]  wbs_sel_i;
1561
input [1:0]  wbs_bte_i;
1562
input [2:0]  wbs_cti_i;
1563
input wbs_we_i;
1564
input wbs_cyc_i;
1565
input wbs_stb_i;
1566 130 unneback
output [dat_width-1:0] wbs_dat_o;
1567 103 unneback
output wbs_ack_o;
1568
output wbs_stall_o;
1569
 
1570
input [dat_width-1:0] readdata;
1571
input readdatavalid;
1572
output [dat_width-1:0] writedata;
1573
output [adr_width-1:0] address;
1574
output [dat_width/8-1:0]  be;
1575
output write;
1576
output read;
1577
output beginbursttransfer;
1578
output [3:0] burstcount;
1579
input waitrequest;
1580 135 unneback
input init_done;
1581 103 unneback
input clk, rst;
1582
 
1583 135 unneback
// cnt1 - initiated read or writes
1584
// cnt2 - # of read or writes in pipeline
1585
reg [3:0] cnt1;
1586 136 unneback
reg [3:0] cnt2;
1587 103 unneback
 
1588 135 unneback
reg next_state, state;
1589
localparam s0 = 1'b0;
1590
localparam s1 = 1'b1;
1591
 
1592
wire eoc;
1593
 
1594
always @ *
1595
begin
1596
    case (state)
1597
    s0: if (init_done & wbs_cyc_i) next_state <= s1;
1598 136 unneback
    s1:
1599 135 unneback
    default: next_state <= state;
1600
    end
1601
end
1602
 
1603 103 unneback
always @ (posedge clk or posedge rst)
1604
if (rst)
1605 135 unneback
    state <= s0;
1606 103 unneback
else
1607 135 unneback
    state <= next_state;
1608 103 unneback
 
1609 135 unneback
assign eoc = state==s1 & !(read | write) & (& !waitrequest & cnt2=;
1610
always @ (posedge clk or posedge rst)
1611
if (rst)
1612
    cnt1 <= 4'h0;
1613
else
1614
    if (read & !waitrequest & init_done)
1615
        cnt1 <= burst_size - 1;
1616
    else if (write & !waitrequest & init_done)
1617
        cnt1 <= cnt1 + 4'h1;
1618
    else if (next_state==idle)
1619
        cnt1 <= 4'h0;
1620
 
1621
always @ (posedge clk or posedge rst)
1622
if (rst)
1623
    cnt2 <= 4'h0;
1624
else
1625
    if (read & !waitrequest & init_done)
1626
        cnt2 <= burst_size - 1;
1627
    else if (write & !waitrequest & init_done & )
1628
        cnt2 <= cnt1 + 4'h1;
1629
    else if (next_state==idle)
1630
        cnt2 <= 4'h0;
1631
 
1632 103 unneback
reg wr_ack;
1633
always @ (posedge clk or posedge rst)
1634
if (rst)
1635
    wr_ack <= 1'b0;
1636
else
1637
    wr_ack <=  (wbs_we_i & wbs_cyc_i & wbs_stb_i & !wbs_stall_o);
1638
 
1639
// to avalon
1640
assign writedata = wbs_dat_i;
1641
assign address = wbs_adr_i;
1642
assign be = wbs_sel_i;
1643 135 unneback
assign write = cnt!=4'h0 & wbs_cyc_i &  wbs_we_i;
1644
assign read  = cnt!=4'h0 & wbs_cyc_i & !wbs_we_i;
1645
assign beginbursttransfer = state==s0 & next_state==s1;
1646 103 unneback
assign burstcount = burst_size;
1647
 
1648
// to wishbone
1649
assign wbs_dat_o = readdata;
1650
assign wbs_ack_o = wr_ack | readdatavalid;
1651
assign wbs_stall_o = waitrequest;
1652
 
1653
endmodule
1654
`endif
1655
 
1656
`ifdef WB_AVALON_MEM_CACHE
1657
`define MODULE wb_avalon_mem_cache
1658
module `BASE`MODULE (
1659
    wbs_dat_i, wbs_adr_i, wbs_sel_i, wbs_cti_i, wbs_bte_i, wbs_we_i, wbs_stb_i, wbs_cyc_i, wbs_dat_o, wbs_ack_o, wbs_stall_o, wbs_clk, wbs_rst,
1660
    readdata, readdatavalid, address, read, be, write, burstcount, writedata, waitrequest, beginbursttransfer, clk, rst
1661
);
1662
`undef MODULE
1663
 
1664
// wishbone
1665
parameter wb_dat_width = 32;
1666
parameter wb_adr_width = 22;
1667
parameter wb_max_burst_width = 4;
1668
parameter wb_mode = "B4";
1669
// avalon
1670
parameter avalon_dat_width = 32;
1671 121 unneback
//localparam avalon_adr_width = wb_dat_width * wb_adr_width / avalon_dat_width;
1672 122 unneback
localparam avalon_adr_width =
1673
        (wb_dat_width==avalon_dat_width) ? wb_adr_width :
1674
        (wb_dat_width==avalon_dat_width*2) ? wb_adr_width+1 :
1675
        (wb_dat_width==avalon_dat_width*4) ? wb_adr_width+2 :
1676
        (wb_dat_width==avalon_dat_width*8) ? wb_adr_width+3 :
1677
        (wb_dat_width==avalon_dat_width*16) ? wb_adr_width+4 :
1678
        (wb_dat_width==avalon_dat_width*32) ? wb_adr_width+5 :
1679
        (wb_dat_width==avalon_dat_width/2) ? wb_adr_width-1 :
1680
        (wb_dat_width==avalon_dat_width/4) ? wb_adr_width-2 :
1681
        (wb_dat_width==avalon_dat_width/8) ? wb_adr_width-3 :
1682
        (wb_dat_width==avalon_dat_width/16) ? wb_adr_width-4 :
1683 123 unneback
        (wb_dat_width==avalon_dat_width/32) ? wb_adr_width-5 : 0;
1684 103 unneback
parameter avalon_burst_size = 4;
1685
// cache
1686
parameter async = 1;
1687
parameter nr_of_ways = 1;
1688
parameter aw_offset = 4;
1689
parameter aw_slot = 10;
1690
parameter valid_mem = 1;
1691
// shadow RAM
1692
parameter shadow_ram = 0;
1693
parameter shadow_ram_adr_width = 10;
1694
parameter shadow_ram_size = 1024;
1695
parameter shadow_ram_init = 2; // 0: no init, 1: from file, 2: with zero
1696
parameter shadow_ram_file = "vl_ram.v";
1697
 
1698
input [wb_dat_width-1:0] wbs_dat_i;
1699
input [wb_adr_width-1:0] wbs_adr_i; // dont include a1,a0
1700
input [wb_dat_width/8-1:0] wbs_sel_i;
1701
input [2:0] wbs_cti_i;
1702
input [1:0] wbs_bte_i;
1703
input wbs_we_i, wbs_stb_i, wbs_cyc_i;
1704
output [wb_dat_width-1:0] wbs_dat_o;
1705
output wbs_ack_o;
1706
output wbs_stall_o;
1707
input wbs_clk, wbs_rst;
1708
 
1709
input [avalon_dat_width-1:0] readdata;
1710
input readdatavalid;
1711
output [avalon_dat_width-1:0] writedata;
1712
output [avalon_adr_width-1:0] address;
1713
output [avalon_dat_width/8-1:0]  be;
1714
output write;
1715
output read;
1716
output beginbursttransfer;
1717
output [3:0] burstcount;
1718
input waitrequest;
1719
input clk, rst;
1720
 
1721
`define DAT_WIDTH wb_dat_width
1722
`define ADR_WIDTH wb_adr_width
1723
`define WB wb1
1724
`include "wb_wires.v"
1725 129 unneback
`undef DAT_WIDTH
1726
`undef ADR_WIDTH
1727
`define DAT_WIDTH avalon_dat_width
1728
`define ADR_WIDTH avalon_adr_width
1729 103 unneback
`define WB wb2
1730
`include "wb_wires.v"
1731
`undef DAT_WIDTH
1732
`undef ADR_WIDTH
1733
 
1734
`define MODULE wb_shadow_ram
1735
`BASE`MODULE # ( .dat_width(wb_dat_width), .mode(wb_mode), .max_burst_width(wb_max_burst_width),
1736 120 unneback
                 .shadow_mem_adr_width(shadow_ram_adr_width), .shadow_mem_size(shadow_ram_size), .shadow_mem_init(shadow_ram_init), .shadow_mem_file(shadow_ram_file),
1737 103 unneback
                 .main_mem_adr_width(wb_adr_width))
1738
shadow_ram0 (
1739
    .wbs_dat_i(wbs_dat_i), .wbs_adr_i(wbs_adr_i), .wbs_cti_i(wbs_cti_i), .wbs_bte_i(wbs_bte_i), .wbs_sel_i(wbs_sel_i), .wbs_we_i(wbs_we_i), .wbs_stb_i(wbs_stb_i), .wbs_cyc_i(wbs_cyc_i),
1740
    .wbs_dat_o(wbs_dat_o), .wbs_ack_o(wbs_ack_o), .wbs_stall_o(wbs_stall_o),
1741
    .wbm_dat_o(wb1_dat_o), .wbm_adr_o(wb1_adr_o), .wbm_cti_o(wb1_cti_o), .wbm_bte_o(wb1_bte_o), .wbm_sel_o(wb1_sel_o), .wbm_we_o(wb1_we_o), .wbm_stb_o(wb1_stb_o), .wbm_cyc_o(wb1_cyc_o),
1742
    .wbm_dat_i(wb1_dat_i), .wbm_ack_i(wb1_ack_i), .wbm_stall_i(wb1_stall_i),
1743
    .wb_clk(wbs_clk), .wb_rst(wbs_rst));
1744
`undef MODULE
1745
 
1746
`define MODULE wb_cache
1747
`BASE`MODULE
1748
# ( .dw_s(wb_dat_width), .aw_s(wb_adr_width), .dw_m(avalon_dat_width), .wbs_mode(wb_mode), .wbs_max_burst_width(wb_max_burst_width), .async(async), .nr_of_ways(nr_of_ways), .aw_offset(aw_offset), .aw_slot(aw_slot), .valid_mem(valid_mem))
1749
cache0 (
1750
    .wbs_dat_i(wb1_dat_o), .wbs_adr_i(wb1_adr_o), .wbs_sel_i(wb1_sel_o), .wbs_cti_i(wb1_cti_o), .wbs_bte_i(wb1_bte_o), .wbs_we_i(wb1_we_o), .wbs_stb_i(wb1_stb_o), .wbs_cyc_i(wb1_cyc_o),
1751
    .wbs_dat_o(wb1_dat_i), .wbs_ack_o(wb1_ack_i), .wbs_stall_o(wb1_stall_i), .wbs_clk(wbs_clk), .wbs_rst(wbs_rst),
1752
    .wbm_dat_o(wb2_dat_o), .wbm_adr_o(wb2_adr_o), .wbm_sel_o(wb2_sel_o), .wbm_cti_o(wb2_cti_o), .wbm_bte_o(wb2_bte_o), .wbm_we_o(wb2_we_o), .wbm_stb_o(wb2_stb_o), .wbm_cyc_o(wb2_cyc_o),
1753
    .wbm_dat_i(wb2_dat_i), .wbm_ack_i(wb2_ack_i), .wbm_stall_i(wb2_stall_i), .wbm_clk(clk), .wbm_rst(rst));
1754
`undef MODULE
1755
 
1756
`define MODULE wb_avalon_bridge
1757
`BASE`MODULE # ( .adr_width(avalon_adr_width), .dat_width(avalon_dat_width), .burst_size(avalon_burst_size))
1758
bridge0 (
1759
        // wishbone slave side
1760
        .wbs_dat_i(wb2_dat_o), .wbs_adr_i(wb2_adr_o), .wbs_sel_i(wb2_sel_o), .wbs_bte_i(wb2_bte_o), .wbs_cti_i(wb2_cti_o), .wbs_we_i(wb2_we_o), .wbs_cyc_i(wb2_cyc_o), .wbs_stb_i(wb2_stb_o),
1761
        .wbs_dat_o(wb2_dat_i), .wbs_ack_o(wb2_ack_i), .wbs_stall_o(wb2_stall_i),
1762
        // avalon master side
1763
        .readdata(readdata), .readdatavalid(readdatavalid), .address(address), .read(read), .be(be), .write(write), .burstcount(burstcount), .writedata(writedata), .waitrequest(waitrequest), .beginbursttransfer(beginbursttransfer),
1764
        // common
1765
        .clk(clk), .rst(rst));
1766
`undef MODULE
1767
 
1768
endmodule
1769
`endif
1770 104 unneback
 
1771
`ifdef WB_SDR_SDRAM
1772
`define MODULE wb_sdr_sdram
1773
module `BASE`MODULE (
1774
`undef MODULE
1775
    // wisbone i/f
1776 136 unneback
    dat_i, adr_i, sel_i, we_i, cyc_i, stb_i, dat_o, ack_o, stall_o,
1777 104 unneback
    // SDR SDRAM
1778
    ba, a, cmd, cke, cs_n, dqm, dq_i, dq_o, dq_oe,
1779
    // system
1780
    clk, rst);
1781
 
1782
    // external data bus size
1783
    parameter dat_size = 16;
1784
    // memory geometry parameters
1785 136 unneback
    parameter ba_size  = 2;
1786
    parameter row_size = 13;
1787
    parameter col_size = 9;
1788 104 unneback
    parameter cl = 2;
1789
    // memory timing parameters
1790
    parameter tRFC = 9;
1791
    parameter tRP  = 2;
1792
    parameter tRCD = 2;
1793
    parameter tMRD = 2;
1794
 
1795
    // LMR
1796
    // [12:10] reserved
1797
    // [9]     WB, write burst; 0 - programmed burst length, 1 - single location
1798
    // [8:7]   OP Mode, 2'b00
1799
    // [6:4]   CAS Latency; 3'b010 - 2, 3'b011 - 3
1800
    // [3]     BT, Burst Type; 1'b0 - sequential, 1'b1 - interleaved
1801
    // [2:0]   Burst length; 3'b000 - 1, 3'b001 - 2, 3'b010 - 4, 3'b011 - 8, 3'b111 - full page
1802
    localparam init_wb = 1'b1;
1803
    localparam init_cl = (cl==2) ? 3'b010 : 3'b011;
1804
    localparam init_bt = 1'b0;
1805
    localparam init_bl = 3'b000;
1806
 
1807 136 unneback
    input [dat_size-1:0] dat_i;
1808 104 unneback
    input [ba_size+col_size+row_size-1:0] adr_i;
1809
    input [dat_size/8-1:0] sel_i;
1810
    input we_i, cyc_i, stb_i;
1811
    output [dat_size-1:0] dat_o;
1812
    output ack_o;
1813
    output reg stall_o;
1814
 
1815
    output [ba_size-1:0]    ba;
1816
    output reg [12:0]   a;
1817
    output reg [2:0]    cmd; // {ras,cas,we}
1818
    output cke, cs_n;
1819
    output reg [dat_size/8-1:0]    dqm;
1820
    output [dat_size-1:0]       dq_o;
1821
    output reg          dq_oe;
1822
    input  [dat_size-1:0]       dq_i;
1823
 
1824
    input clk, rst;
1825
 
1826
    wire [ba_size-1:0]   bank;
1827
    wire [row_size-1:0] row;
1828
    wire [col_size-1:0] col;
1829
    wire [0:31]  shreg;
1830
    wire                ref_cnt_zero;
1831
    reg                 refresh_req;
1832
 
1833
    wire ack_rd, rd_ack_emptyflag;
1834
    wire ack_wr;
1835
 
1836
    // to keep track of open rows per bank
1837
    reg [row_size-1:0]   open_row[0:3];
1838
    reg [0:3]            open_ba;
1839
    reg                 current_bank_closed, current_row_open;
1840
 
1841
    parameter rfr_length = 10;
1842
    parameter rfr_wrap_value = 1010;
1843
 
1844
    parameter [2:0] cmd_nop = 3'b111,
1845
                    cmd_act = 3'b011,
1846
                    cmd_rd  = 3'b101,
1847
                    cmd_wr  = 3'b100,
1848
                    cmd_pch = 3'b010,
1849
                    cmd_rfr = 3'b001,
1850
                    cmd_lmr = 3'b000;
1851
 
1852
// ctrl FSM
1853
`define FSM_INIT 3'b000
1854
`define FSM_IDLE 3'b001
1855
`define FSM_RFR  3'b010
1856
`define FSM_ADR  3'b011
1857
`define FSM_PCH  3'b100
1858
`define FSM_ACT  3'b101
1859
`define FSM_RW   3'b111
1860
 
1861
    assign cke = 1'b1;
1862
    assign cs_n = 1'b0;
1863
 
1864
    reg [2:0] state, next;
1865
 
1866
    function [12:0] a10_fix;
1867
        input [col_size-1:0] a;
1868
        integer i;
1869
    begin
1870
        for (i=0;i<13;i=i+1) begin
1871
            if (i<10)
1872
              if (i<col_size)
1873
                a10_fix[i] = a[i];
1874
              else
1875
                a10_fix[i] = 1'b0;
1876
            else if (i==10)
1877
              a10_fix[i] = 1'b0;
1878
            else
1879
              if (i<col_size)
1880
                a10_fix[i] = a[i-1];
1881
              else
1882
                a10_fix[i] = 1'b0;
1883
        end
1884
    end
1885
    endfunction
1886
 
1887
    assign {bank,row,col} = adr_i;
1888
 
1889
    always @ (posedge clk or posedge rst)
1890
    if (rst)
1891
       state <= `FSM_INIT;
1892
    else
1893
       state <= next;
1894
 
1895
    always @*
1896
    begin
1897
        next = state;
1898
        case (state)
1899
        `FSM_INIT:
1900
            if (shreg[3+tRP+tRFC+tRFC+tMRD]) next = `FSM_IDLE;
1901
        `FSM_IDLE:
1902
            if (refresh_req) next = `FSM_RFR;
1903
            else if (cyc_i & stb_i & rd_ack_emptyflag) next = `FSM_ADR;
1904
        `FSM_RFR:
1905
            if (shreg[tRP+tRFC-2]) next = `FSM_IDLE; // take away two cycles because no cmd will be issued in idle and adr
1906
        `FSM_ADR:
1907
            if (current_bank_closed) next = `FSM_ACT;
1908
            else if (current_row_open) next = `FSM_RW;
1909
            else next = `FSM_PCH;
1910
        `FSM_PCH:
1911
            if (shreg[tRP]) next = `FSM_ACT;
1912
        `FSM_ACT:
1913
            if (shreg[tRCD]) next = `FSM_RW;
1914
        `FSM_RW:
1915
            if (!stb_i) next = `FSM_IDLE;
1916
        endcase
1917
    end
1918
 
1919
    // counter
1920 136 unneback
`define MODULE cnt_shreg_clear
1921
    `BASE`MODULE # ( .length(32))
1922 104 unneback
`undef MODULE
1923
        cnt0 (
1924
            .clear(state!=next),
1925
            .q(shreg),
1926
            .rst(rst),
1927
            .clk(clk));
1928
 
1929
    // ba, a, cmd
1930
    // outputs dependent on state vector
1931
    always @ (*)
1932
        begin
1933
            {a,cmd} = {13'd0,cmd_nop};
1934
            dqm = 2'b11;
1935
            dq_oe = 1'b0;
1936
            stall_o = 1'b1;
1937
            case (state)
1938
            `FSM_INIT:
1939
                if (shreg[3]) begin
1940
                    {a,cmd} = {13'b0010000000000, cmd_pch};
1941
                end else if (shreg[3+tRP] | shreg[3+tRP+tRFC])
1942
                    {a,cmd} = {13'd0, cmd_rfr};
1943
                else if (shreg[3+tRP+tRFC+tRFC])
1944
                    {a,cmd} = {3'b000,init_wb,2'b00,init_cl,init_bt,init_bl,cmd_lmr};
1945
            `FSM_RFR:
1946
                if (shreg[0])
1947
                    {a,cmd} = {13'b0010000000000, cmd_pch};
1948
                else if (shreg[tRP])
1949
                    {a,cmd} = {13'd0, cmd_rfr};
1950
            `FSM_PCH:
1951
                if (shreg[0])
1952
                    {a,cmd} = {13'd0,cmd_pch};
1953
            `FSM_ACT:
1954
                if (shreg[0])
1955
                    {a[row_size-1:0],cmd} = {row,cmd_act};
1956
            `FSM_RW:
1957
                begin
1958
                    if (we_i)
1959
                        cmd = cmd_wr;
1960
                    else
1961
                        cmd = cmd_rd;
1962
                    if (we_i)
1963
                        dqm = ~sel_i;
1964
                    else
1965
                        dqm = 2'b00;
1966
                    if (we_i)
1967
                        dq_oe = 1'b1;
1968
                    a = a10_fix(col);
1969 136 unneback
                    stall_o = 1'b0;
1970 104 unneback
                end
1971
            endcase
1972
        end
1973
 
1974
    assign ba = bank;
1975
 
1976
    // precharge individual bank A10=0
1977
    // precharge all bank A10=1
1978
    genvar i;
1979
    generate
1980 136 unneback
    for (i=0;i<2<<ba_size-1;i=i+1) begin : open_ba_logic
1981 104 unneback
 
1982
        always @ (posedge clk or posedge rst)
1983
        if (rst)
1984
            {open_ba[i],open_row[i]} <= {1'b0,{row_size{1'b0}}};
1985
        else
1986
            if (cmd==cmd_pch & (a[10] | bank==i))
1987
                open_ba[i] <= 1'b0;
1988
            else if (cmd==cmd_act & bank==i)
1989
                {open_ba[i],open_row[i]} <= {1'b1,row};
1990
 
1991
    end
1992
    endgenerate
1993
 
1994
    // bank and row open ?
1995
    always @ (posedge clk or posedge rst)
1996
    if (rst)
1997
       {current_bank_closed, current_row_open} <= {1'b1, 1'b0};
1998
    else
1999
       {current_bank_closed, current_row_open} <= {!(open_ba[bank]), open_row[bank]==row};
2000
 
2001
    // refresh counter
2002
`define MODULE cnt_lfsr_zq
2003 136 unneback
    `BASE`MODULE # ( .length(rfr_length), .wrap_value (rfr_wrap_value)) ref_counter0( .zq(ref_cnt_zero), .rst(rst), .clk(clk));
2004 104 unneback
`undef MODULE
2005
 
2006
    always @ (posedge clk or posedge rst)
2007
    if (rst)
2008
        refresh_req <= 1'b0;
2009
    else
2010
        if (ref_cnt_zero)
2011
            refresh_req <= 1'b1;
2012
        else if (state==`FSM_RFR)
2013
            refresh_req <= 1'b0;
2014
 
2015
    assign dat_o = dq_i;
2016
 
2017 136 unneback
    assign ack_wr = (state==`FSM_RW & we_i);
2018 104 unneback
`define MODULE delay_emptyflag
2019 136 unneback
    `BASE`MODULE # ( .depth(cl+2)) delay0 ( .d(state==`FSM_RW & stb_i & !we_i), .q(ack_rd), .emptyflag(rd_ack_emptyflag), .clk(clk), .rst(rst));
2020 104 unneback
`undef MODULE
2021
    assign ack_o = ack_rd | ack_wr;
2022
 
2023
    assign dq_o = dat_i;
2024
 
2025
endmodule
2026
`endif
2027 136 unneback
 
2028
`ifdef WB_SDR_SDRAM_CTRL
2029
`define MODULE wb_sdr_sdram_ctrl
2030
module `BASE`MODULE (
2031
    // WB i/f
2032
    wbs_dat_i, wbs_adr_i, wbs_cti_i, wbs_bte_i, wbs_sel_i, wbs_we_i, wbs_stb_i, wbs_cyc_i,
2033
    wbs_dat_o, wbs_ack_o, wbs_stall_o,
2034
    // SDR SDRAM
2035
    mem_ba, mem_a, mem_cmd, mem_cke, mem_cs_n, mem_dqm, mem_dq_i, mem_dq_o, mem_dq_oe,
2036
    // system
2037
    wb_clk, wb_rst, mem_clk, mem_rst);
2038
`undef MODULE
2039
 
2040
    // WB slave
2041
    parameter wbs_dat_width = 32;
2042
    parameter wbs_adr_width = 24;
2043
    parameter wbs_mode = "B3";
2044
    parameter wbs_max_burst_width = 4;
2045
 
2046
    // Shadow RAM
2047
    parameter shadow_mem_adr_width = 10;
2048
    parameter shadow_mem_size = 1024;
2049
    parameter shadow_mem_init = 2;
2050
    parameter shadow_mem_file = "vl_ram.v";
2051
 
2052
    // Cache
2053
    parameter cache_async = 1; // wbs_clk != wbm_clk
2054
    parameter cache_nr_of_ways = 1;
2055
    parameter cache_aw_offset = 4; // 4 => 16 words per cache line
2056
    parameter cache_aw_slot = 10;
2057
    parameter cache_valid_mem = 0;
2058
    parameter cache_debug = 0;
2059
 
2060
    // SDRAM parameters
2061
    parameter mem_dat_size = 16;
2062
    parameter mem_ba_size  = 2;
2063
    parameter mem_row_size = 13;
2064
    parameter mem_col_size = 9;
2065
    parameter mem_cl = 2;
2066
    parameter mem_tRFC = 9;
2067
    parameter mem_tRP  = 2;
2068
    parameter mem_tRCD = 2;
2069
    parameter mem_tMRD = 2;
2070
    parameter mem_rfr_length = 10;
2071
    parameter mem_rfr_wrap_value = 1010;
2072
 
2073
    input [wbs_dat_width-1:0] wbs_dat_i;
2074
    input [wbs_adr_width-1:0] wbs_adr_i;
2075
    input [2:0] wbs_cti_i;
2076
    input [1:0] wbs_bte_i;
2077
    input [wbs_dat_width/8-1:0] wbs_sel_i;
2078
    input wbs_we_i, wbs_stb_i, wbs_cyc_i;
2079
    output [wbs_dat_width-1:0] wbs_dat_o;
2080
    output wbs_ack_o;
2081
    output wbs_stall_o;
2082
 
2083
    output [mem_ba_size-1:0]    mem_ba;
2084
    output reg [12:0]           mem_a;
2085
    output reg [2:0]            mem_cmd; // {ras,cas,we}
2086
    output                      mem_cke, mem_cs_n;
2087
    output reg [mem_dat_size/8-1:0] mem_dqm;
2088
    output [mem_dat_size-1:0]       mem_dq_o;
2089
    output reg                  mem_dq_oe;
2090
    input  [mem_dat_size-1:0]       mem_dq_i;
2091
 
2092
    input wb_clk, wb_rst, mem_clk, mem_rst;
2093
 
2094
    // wbm1
2095
    wire [wbs_dat_width-1:0] wbm1_dat_o;
2096
    wire [wbs_adr_width-1:0] wbm1_adr_o;
2097
    wire [2:0] wbm1_cti_o;
2098
    wire [1:0] wbm1_bte_o;
2099
    wire [wbs_dat_width/8-1:0] wbm1_sel_o;
2100
    wire wbm1_we_o, wbm1_stb_o, wbm1_cyc_o;
2101
    wire [wbs_dat_width-1:0] wbm1_dat_i;
2102
    wire wbm1_ack_i, wbm1_stall_i;
2103
    // wbm2
2104
    wire [mem_dat_size-1:0] wbm2_dat_o;
2105
    wire [mem_ba_size+mem_row_size+mem_col_size-1:0] wbm2_adr_o;
2106
    wire [2:0] wbm2_cti_o;
2107
    wire [1:0] wbm2_bte_o;
2108
    wire [mem_dat_size/8-1:0] wbm2_sel_o;
2109
    wire wbm2_we_o, wbm2_stb_o, wbm2_cyc_o;
2110
    wire [mem_dat_size-1:0] wbm2_dat_i;
2111
    wire wbm2_ack_i, wbm2_stall_i;
2112
 
2113
`define MODULE wb_shadow_ram
2114
`BASE`MODULE # (
2115
    .shadow_mem_adr_width(shadow_mem_adr_width), .shadow_mem_size(shadow_mem_size), .shadow_mem_init(shadow_mem_init), .shadow_mem_file(shadow_mem_file), .main_mem_adr_width(wbs_adr_width), .dat_width(wbs_dat_width), .mode(wbs_mode), .max_burst_width(wbs_max_burst_width) )
2116
shadow_ram0 (
2117
    .wbs_dat_i(wbs_dat_i),
2118
    .wbs_adr_i(wbs_adr_i),
2119
    .wbs_cti_i(wbs_cti_i),
2120
    .wbs_bte_i(wbs_bte_i),
2121
    .wbs_sel_i(wbs_sel_i),
2122
    .wbs_we_i (wbs_we_i),
2123
    .wbs_stb_i(wbs_stb_i),
2124
    .wbs_cyc_i(wbs_cyc_i),
2125
    .wbs_dat_o(wbs_dat_o),
2126
    .wbs_ack_o(wbs_ack_o),
2127
    .wbs_stall_o(wbs_stall_o),
2128
    .wbm_dat_o(wbm1_dat_o),
2129
    .wbm_adr_o(wbm1_adr_o),
2130
    .wbm_cti_o(wbm1_cti_o),
2131
    .wbm_bte_o(wbm1_bte_o),
2132
    .wbm_sel_o(wbm1_sel_o),
2133
    .wbm_we_o(wbm1_we_o),
2134
    .wbm_stb_o(wbm1_stb_o),
2135
    .wbm_cyc_o(wbm1_cyc_o),
2136
    .wbm_dat_i(wbm1_dat_i),
2137
    .wbm_ack_i(wbm1_ack_i),
2138
    .wbm_stall_i(wbm1_stall_i),
2139
    .wb_clk(wb_clk),
2140
    .wb_rst(wb_rst) );
2141
`undef MODULE
2142
 
2143
`define MODULE wb_cache
2144
`BASE`MODULE # (
2145
    .dw_s(wbs_dat_width), .aw_s(wbs_adr_width), .dw_m(mem_dat_size), .wbs_max_burst_width(cache_aw_offset), .wbs_mode(wbs_mode), .async(cache_async), .nr_of_ways(cache_nr_of_ways), .aw_offset(cache_aw_offset), .aw_slot(cache_aw_slot), .valid_mem(cache_valid_mem) )
2146
cache0 (
2147
    .wbs_dat_i(wbm1_dat_o),
2148
    .wbs_adr_i(wbm1_adr_o),
2149
    .wbs_sel_i(wbm1_sel_o),
2150
    .wbs_cti_i(wbm1_cti_o),
2151
    .wbs_bte_i(wbm1_bte_o),
2152
    .wbs_we_i (wbm1_we_o),
2153
    .wbs_stb_i(wbm1_stb_o),
2154
    .wbs_cyc_i(wbm1_cyc_o),
2155
    .wbs_dat_o(wbm1_dat_i),
2156
    .wbs_ack_o(wbm1_ack_i),
2157
    .wbs_stall_o(wbm1_stall_i),
2158
    .wbs_clk(wb_clk),
2159
    .wbs_rst(wb_rst),
2160
    .wbm_dat_o(wbm2_dat_o),
2161
    .wbm_adr_o(wbm2_adr_o),
2162
    .wbm_sel_o(wbm2_sel_o),
2163
    .wbm_cti_o(wbm2_cti_o),
2164
    .wbm_bte_o(wbm2_bte_o),
2165
    .wbm_we_o (wbm2_we_o),
2166
    .wbm_stb_o(wbm2_stb_o),
2167
    .wbm_cyc_o(wbm2_cyc_o),
2168
    .wbm_dat_i(wbm2_dat_i),
2169
    .wbm_ack_i(wbm2_ack_i),
2170
    .wbm_stall_i(wbm2_stall_i),
2171
    .wbm_clk(mem_clk),
2172
    .wbm_rst(mem_rst) );
2173
`undef MODULE
2174
 
2175
`define MODULE wb_sdr_sdram
2176
`BASE`MODULE # (
2177
    .dat_size(mem_dat_size), .ba_size(mem_ba_size), .row_size(mem_row_size), .col_size(mem_col_size), .cl(mem_cl), .tRFC(mem_tRFC), .tRP(mem_tRP), .tRCD(mem_tRCD), .tMRD(mem_tMRD), .rfr_length(mem_rfr_length), .rfr_wrap_value(mem_rfr_wrap_value) )
2178
ctrl0(
2179
    // wisbone i/f
2180
    .dat_i(wbm2_dat_o),
2181
    .adr_i(wbm2_adr_o),
2182
    .sel_i(wbm2_sel_o),
2183
    .we_i (wbm2_we_o),
2184
    .cyc_i(wbm2_cyc_o),
2185
    .stb_i(wbm2_stb_o),
2186
    .dat_o(wbm2_dat_i),
2187
    .ack_o(wbm2_ack_i),
2188
    .stall_o(wbm2_stall_i),
2189
    // SDR SDRAM
2190
    .ba(mem_ba),
2191
    .a(mem_a),
2192
    .cmd(mem_cmd),
2193
    .cke(mem_cke),
2194
    .cs_n(mem_cs_n),
2195
    .dqm(mem_dqm),
2196
    .dq_i(mem_dq_i),
2197
    .dq_o(mem_dq_o),
2198
    .dq_oe(mem_dq_oe),
2199
    // system
2200
    .clk(mem_clk),
2201
    .rst(mem_rst) );
2202
`undef MODULE
2203
 
2204
endmodule
2205
`endif

powered by: WebSVN 2.1.0

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