1 |
627 |
stekern |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// Xilinx Spartan-6 DDR2 controller Wishbone Interface ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// Description ////
|
6 |
|
|
//// Simple interface to the Xilinx MIG generated DDR2 controller////
|
7 |
|
|
//// ////
|
8 |
|
|
//// To Do: ////
|
9 |
|
|
//// Use full capacity of BRAM ////
|
10 |
|
|
//// Employ LRU replacement scheme ////
|
11 |
|
|
//// Remove hard-coding of things relating to number of lines ////
|
12 |
|
|
//// ////
|
13 |
|
|
//// Author(s): ////
|
14 |
|
|
//// - Julius Baxter, julius.baxter@orsoc.se ////
|
15 |
|
|
//// - Stefan Kristiansson, stefan.kristiansson@saunalahti.fi////
|
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 |
|
|
/*
|
44 |
|
|
* This is an interface to the Xilinx MIG-sourced DDR2 controller.
|
45 |
|
|
*
|
46 |
|
|
* The interface is based on the ML501 Virtex-5 board implementation
|
47 |
|
|
* with the following adaptions to suite the Atlys Spartan-6 MIG and
|
48 |
|
|
* DDR2 memory chip:
|
49 |
|
|
* - Control and data FIFOs are clocked with the Wishbone bus clock
|
50 |
|
|
* and not the DDR2 clock.
|
51 |
|
|
* This way alot of clock domain crossing headaches can be avoided
|
52 |
|
|
* (Virtex-5 MIG demands control and data FIFOs clocks to be
|
53 |
|
|
* synchronous with the DDR2 clock, Spartan-6 have FIFOs that
|
54 |
|
|
* are asynchronous to the DDR2 clock)
|
55 |
|
|
* - The Atlys board have a DDR2 memory with a 16-bit data bus
|
56 |
|
|
* apposed to the ML501 64-bit databus. This in combination
|
57 |
|
|
* with changes in the MIG causes the addressing scheme to be a
|
58 |
|
|
* bit different. A user port of 128-bit is being used, so
|
59 |
|
|
* we are doing memory accesses on 128-bit boundaries
|
60 |
|
|
* (4 address bits).
|
61 |
|
|
*
|
62 |
|
|
* See the Xilinx user guide UG388.pdf for more information on the
|
63 |
|
|
* Spartan-6 FPGA memory controller
|
64 |
|
|
*
|
65 |
|
|
*
|
66 |
|
|
* The controller's interface is via FIFO buffers - one for address and control
|
67 |
|
|
* the other is for data. The data FIFO interface is 128-bits wide.
|
68 |
|
|
*
|
69 |
|
|
* This module has a cache with different aspects on each port. As we're to
|
70 |
|
|
* ultimately interface to a 32-bit wide Wishbone bus, one side is 32-bits
|
71 |
|
|
* and the other is 128-bits wide to accommodate the DDR2 controller's data
|
72 |
|
|
* path.
|
73 |
|
|
*
|
74 |
|
|
* At present, the cache controller doesn't employ associativity, so any
|
75 |
|
|
* line can be used for any location. A round-robin approach to line
|
76 |
|
|
* use is employed. TODO is LRU scheme instead of round robin.
|
77 |
|
|
*
|
78 |
|
|
* The cache is a macro generated by Xilinx's IP generation tool. This is
|
79 |
|
|
* because memories with dual-aspect ratios cannot be inferred via HDL.
|
80 |
|
|
*
|
81 |
|
|
* The size of lines, as set by the defines, controls how long each read
|
82 |
|
|
* and write burst to/from the SDRAM is.
|
83 |
|
|
*
|
84 |
|
|
* The control and data FIFOS of the DDR2 interface are asynchronous to the DDR2 bus,
|
85 |
|
|
* so they are clocked with the same clock as the Wisbone interface (i.e. wb_clk)
|
86 |
|
|
*/
|
87 |
|
|
module xilinx_ddr2_if (
|
88 |
|
|
input [31:0] wb_adr_i,
|
89 |
|
|
input wb_stb_i,
|
90 |
|
|
input wb_cyc_i,
|
91 |
|
|
input [2:0] wb_cti_i,
|
92 |
|
|
input [1:0] wb_bte_i,
|
93 |
|
|
input wb_we_i,
|
94 |
|
|
input [3:0] wb_sel_i,
|
95 |
|
|
input [31:0] wb_dat_i,
|
96 |
|
|
output [31:0] wb_dat_o,
|
97 |
|
|
output reg wb_ack_o,
|
98 |
|
|
|
99 |
|
|
output [12:0] ddr2_a,
|
100 |
|
|
output [2:0] ddr2_ba,
|
101 |
|
|
output ddr2_ras_n,
|
102 |
|
|
output ddr2_cas_n,
|
103 |
|
|
output ddr2_we_n,
|
104 |
|
|
output ddr2_rzq,
|
105 |
|
|
output ddr2_zio,
|
106 |
|
|
output ddr2_odt,
|
107 |
|
|
output ddr2_cke,
|
108 |
|
|
output ddr2_dm,
|
109 |
|
|
output ddr2_udm,
|
110 |
|
|
|
111 |
|
|
inout [15:0] ddr2_dq,
|
112 |
|
|
inout ddr2_dqs,
|
113 |
|
|
inout ddr2_dqs_n,
|
114 |
|
|
inout ddr2_udqs,
|
115 |
|
|
inout ddr2_udqs_n,
|
116 |
|
|
output ddr2_ck,
|
117 |
|
|
output ddr2_ck_n,
|
118 |
|
|
|
119 |
|
|
input ddr2_if_clk,
|
120 |
|
|
input ddr2_if_rst,
|
121 |
|
|
input idly_clk_100,
|
122 |
|
|
input wb_clk,
|
123 |
|
|
input wb_rst);
|
124 |
|
|
|
125 |
|
|
`include "xilinx_ddr2_params.v"
|
126 |
|
|
|
127 |
|
|
// Define to add a counter, signaling error if the controller locks up
|
128 |
|
|
// (no ack after a certain period of time)
|
129 |
|
|
//`define ERR_COUNTER
|
130 |
|
|
|
131 |
|
|
/*
|
132 |
|
|
`define DDR2_CACHE_NUM_LINES 16
|
133 |
|
|
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 4 // log2(`DDR2_CACHE_NUM_LINES)
|
134 |
|
|
*/
|
135 |
|
|
`define DDR2_CACHE_NUM_LINES 4
|
136 |
|
|
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 2 // log2(`DDR2_CACHE_NUM_LINES)
|
137 |
|
|
|
138 |
|
|
`define DDR2_CACHE_NUM_WORDS_PER_LINE 256
|
139 |
|
|
`define DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE 8
|
140 |
|
|
`define DDR2_CACHE_TAG_ADDR_WIDTH (32-`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-2)
|
141 |
|
|
|
142 |
|
|
`define DDR2_CACHE_DDR2_SIDE_NUM_WORDS_PER_LINE (`DDR2_CACHE_NUM_WORDS_PER_LINE/4)
|
143 |
|
|
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE (`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE - 2)
|
144 |
|
|
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH (`DDR2_CACHE_NUM_LINES_ENC_WIDTH + `DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE)
|
145 |
|
|
|
146 |
|
|
`define DDR2_CACHE_TAG_BITS 31:(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)
|
147 |
|
|
|
148 |
|
|
wire ddr2_clk; // DDR2 iface domain clock.
|
149 |
|
|
wire ddr2_rst; // reset from the ddr2 module
|
150 |
|
|
|
151 |
|
|
wire wb_req;
|
152 |
|
|
reg wb_req_r;
|
153 |
|
|
reg wb_ack_o_r;
|
154 |
|
|
|
155 |
|
|
wire wb_req_new;
|
156 |
|
|
reg wb_req_new_r;
|
157 |
|
|
|
158 |
|
|
wire wb_req_addr_hit;
|
159 |
|
|
|
160 |
|
|
wire cached_addr_valid;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
wire [31:(32 -`DDR2_CACHE_TAG_ADDR_WIDTH)] cached_addr;
|
164 |
|
|
|
165 |
|
|
// Spartan-6 MIG doesn't have any defines for the DDR2 burst length,
|
166 |
|
|
// only burst length for user data.
|
167 |
|
|
// Our user port is 128-bit
|
168 |
|
|
//`define DDR2_BURSTLENGTH_1
|
169 |
|
|
//`define DDR2_BURSTLENGTH_2
|
170 |
|
|
//`define DDR2_BURSTLENGTH_4
|
171 |
|
|
//`define DDR2_BURSTLENGTH_8
|
172 |
|
|
`define DDR2_BURSTLENGTH_16
|
173 |
|
|
|
174 |
|
|
`ifdef DDR2_BURSTLENGTH_1
|
175 |
|
|
`define DDR2_BURST_DW128_ADDR_WIDTH 2 // = log2(burst of 1 128-bits = 4 words)
|
176 |
|
|
`define DDR2_ADDR_ALIGN 4
|
177 |
|
|
`elsif DDR2_BURSTLENGTH_2
|
178 |
|
|
`define DDR2_BURST_DW128_ADDR_WIDTH 3 // = log2(burst of 2 128-bits = 8 words)
|
179 |
|
|
`define DDR2_ADDR_ALIGN 5
|
180 |
|
|
`elsif DDR2_BURSTLENGTH_4
|
181 |
|
|
`define DDR2_BURST_DW128_ADDR_WIDTH 4 // = log2(burst of 4 128-bits = 16 words)
|
182 |
|
|
`define DDR2_ADDR_ALIGN 6
|
183 |
|
|
`elsif DDR2_BURSTLENGTH_8
|
184 |
|
|
`define DDR2_BURST_DW128_ADDR_WIDTH 5 // = log2(burst of 8 128-bits = 32 words)
|
185 |
|
|
`define DDR2_ADDR_ALIGN 7
|
186 |
|
|
`elsif DDR2_BURSTLENGTH_16
|
187 |
|
|
`define DDR2_BURST_DW128_ADDR_WIDTH 6 // = log2(burst of 16 128-bits = 64 words)
|
188 |
|
|
`define DDR2_ADDR_ALIGN 8
|
189 |
|
|
`endif
|
190 |
|
|
|
191 |
|
|
// This counts how many addresses we should write to the fifo - the number
|
192 |
|
|
// of discrete FIFO transactions.
|
193 |
|
|
reg [`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-`DDR2_BURST_DW128_ADDR_WIDTH - 1:0] addr_counter;
|
194 |
|
|
|
195 |
|
|
wire cache_write;
|
196 |
|
|
|
197 |
|
|
wire cache_hit;
|
198 |
|
|
|
199 |
|
|
wire wb_cache_en;
|
200 |
|
|
|
201 |
|
|
reg do_writeback, do_writeback_r;
|
202 |
|
|
wire do_writeback_start, do_writeback_finished;
|
203 |
|
|
// Wire to indicate writing to data FIFO of MIG has completed
|
204 |
|
|
reg do_writeback_data_finished;
|
205 |
|
|
|
206 |
|
|
// Wire to indicate that address FIFO of MIG should be written to to
|
207 |
|
|
// initiate memory accesses.
|
208 |
|
|
reg do_writeback_addresses, do_writeback_addresses_r;
|
209 |
|
|
|
210 |
|
|
reg do_readfrom, do_readfrom_r;
|
211 |
|
|
wire do_readfrom_start, do_readfrom_finished;
|
212 |
|
|
wire doing_readfrom;
|
213 |
|
|
|
214 |
|
|
reg do_af_write;
|
215 |
|
|
|
216 |
|
|
reg do_writeback_ddr2_fifo_we;
|
217 |
|
|
reg ddr2_write_done;
|
218 |
|
|
reg [`DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE - 1:0] ddr2_cache_line_word_addr;
|
219 |
|
|
wire [127:0] ddr2_cache_data_o;
|
220 |
|
|
reg rd_data_valid_r;
|
221 |
|
|
reg ddr2_read_done;
|
222 |
|
|
|
223 |
|
|
// DDR2 MIG interface wires
|
224 |
|
|
wire ddr2_p0_cmd_en;
|
225 |
|
|
wire [30:0] ddr2_p0_cmd_byte_addr;
|
226 |
|
|
wire [2:0] ddr2_p0_cmd_instr;
|
227 |
|
|
wire ddr2_p0_cmd_full;
|
228 |
|
|
wire ddr2_p0_cmd_empty;
|
229 |
|
|
wire [5:0] ddr2_p0_cmd_bl;
|
230 |
|
|
wire ddr2_p0_wr_en;
|
231 |
|
|
wire [(C3_P0_DATA_PORT_SIZE)-1:0] ddr2_p0_wr_data;
|
232 |
|
|
wire [(C3_P0_MASK_SIZE)-1:0] ddr2_p0_wr_mask;
|
233 |
|
|
wire ddr2_p0_wr_full;
|
234 |
|
|
wire ddr2_p0_wr_empty;
|
235 |
|
|
wire [6:0] ddr2_p0_wr_count;
|
236 |
|
|
wire ddr2_p0_wr_underrun;
|
237 |
|
|
wire ddr2_p0_wr_error;
|
238 |
|
|
wire ddr2_p0_rd_en;
|
239 |
|
|
wire [(C3_P0_DATA_PORT_SIZE)-1:0] ddr2_p0_rd_data;
|
240 |
|
|
wire ddr2_p0_rd_full;
|
241 |
|
|
wire ddr2_p0_rd_empty;
|
242 |
|
|
wire [6:0] ddr2_p0_rd_count;
|
243 |
|
|
wire ddr2_p0_rd_overflow;
|
244 |
|
|
wire ddr2_p0_rd_error;
|
245 |
|
|
wire ddr2_calib_done;
|
246 |
|
|
|
247 |
|
|
wire [30:0] readfrom_af_addr;
|
248 |
|
|
wire [30:0] writeback_af_addr;
|
249 |
|
|
|
250 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_validate;
|
251 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_invalidate;
|
252 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_valid;
|
253 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_hit;
|
254 |
|
|
wire [`DDR2_CACHE_TAG_BITS] cache_line_addr [0:`DDR2_CACHE_NUM_LINES-1] ;
|
255 |
|
|
|
256 |
|
|
// Cache control signals
|
257 |
|
|
// Wishbone side
|
258 |
|
|
wire [`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-1:0] wb_cache_adr;
|
259 |
|
|
wire [3:0] wb_cache_sel_we;
|
260 |
|
|
// DDR side
|
261 |
|
|
wire ddr2_cache_en;
|
262 |
|
|
wire [15:0] ddr2_cache_we;
|
263 |
|
|
|
264 |
|
|
reg wb_bursting; // Indicate if burst is enabled
|
265 |
|
|
reg [3:0] wb_burst_addr; // Burst counter, up to 16
|
266 |
|
|
wire [1:0] wb_burst_addr_4beat;
|
267 |
|
|
wire [2:0] wb_burst_addr_8beat;
|
268 |
|
|
wire wb_burst_addr_incr;
|
269 |
|
|
wire ack_err;
|
270 |
|
|
reg ack_err_r;
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
// Synchronisation signals
|
274 |
|
|
reg sync, sync_r;
|
275 |
|
|
wire sync_start;
|
276 |
|
|
wire sync_done;
|
277 |
|
|
|
278 |
|
|
// Decoded select line
|
279 |
|
|
wire [`DDR2_CACHE_NUM_LINES-1:0] selected_cache_line;
|
280 |
|
|
wire [`DDR2_CACHE_NUM_LINES_ENC_WIDTH-1:0] selected_cache_line_enc;
|
281 |
|
|
|
282 |
|
|
genvar i;
|
283 |
|
|
generate
|
284 |
|
|
for (i=0;i<`DDR2_CACHE_NUM_LINES;i=i+1) begin : cache_addr
|
285 |
|
|
xilinx_ddr2_wb_if_cache_adr_reg cache_addr_reg_inst
|
286 |
|
|
( .adr_i(wb_adr_i[`DDR2_CACHE_TAG_BITS]),
|
287 |
|
|
.validate(cache_line_addr_validate[i]),
|
288 |
|
|
.invalidate(cache_line_addr_invalidate[i]),
|
289 |
|
|
.cache_hit(cache_line_hit[i]),
|
290 |
|
|
.adr_valid(cache_line_addr_valid[i]),
|
291 |
|
|
.cached_adr_o(cache_line_addr[i]),
|
292 |
|
|
.clk(wb_clk),
|
293 |
|
|
.rst(wb_rst));
|
294 |
|
|
end
|
295 |
|
|
endgenerate
|
296 |
|
|
|
297 |
|
|
wire start_writeback, start_fill;
|
298 |
|
|
|
299 |
|
|
xilinx_ddr2_wb_if_cache_control xilinx_ddr2_wb_if_cache_control0
|
300 |
|
|
(
|
301 |
|
|
// Outputs
|
302 |
|
|
.start_writeback (start_writeback),
|
303 |
|
|
.start_fill (start_fill),
|
304 |
|
|
.cache_line_validate (cache_line_addr_validate),
|
305 |
|
|
.cache_line_invalidate (cache_line_addr_invalidate),
|
306 |
|
|
.selected_cache_line (selected_cache_line),
|
307 |
|
|
.selected_cache_line_enc (selected_cache_line_enc),
|
308 |
|
|
.sync_done (sync_done),
|
309 |
|
|
// Inputs
|
310 |
|
|
.cache_line_addr_valid (cache_line_addr_valid),
|
311 |
|
|
.cache_line_addr_hit (cache_line_hit),
|
312 |
|
|
.wb_req (wb_req),
|
313 |
|
|
.cache_write (cache_write),
|
314 |
|
|
.writeback_done (do_writeback_finished),
|
315 |
|
|
.fill_done (do_readfrom_finished),
|
316 |
|
|
.sync_start (sync_start),
|
317 |
|
|
.wb_clk (wb_clk),
|
318 |
|
|
.wb_rst (wb_rst));
|
319 |
|
|
|
320 |
|
|
defparam xilinx_ddr2_wb_if_cache_control0.num_lines = `DDR2_CACHE_NUM_LINES;
|
321 |
|
|
defparam xilinx_ddr2_wb_if_cache_control0.num_lines_log2 = `DDR2_CACHE_NUM_LINES_ENC_WIDTH;
|
322 |
|
|
|
323 |
|
|
assign cached_addr = selected_cache_line[0] ? cache_line_addr[0] :
|
324 |
|
|
selected_cache_line[1] ? cache_line_addr[1] :
|
325 |
|
|
selected_cache_line[2] ? cache_line_addr[2] :
|
326 |
|
|
selected_cache_line[3] ? cache_line_addr[3] : 0;
|
327 |
|
|
|
328 |
|
|
assign cache_write = wb_req & wb_we_i & wb_ack_o;
|
329 |
|
|
|
330 |
|
|
assign cache_hit = |(selected_cache_line & cache_line_hit);
|
331 |
|
|
|
332 |
|
|
assign cached_addr_valid = |(selected_cache_line & cache_line_addr_valid);
|
333 |
|
|
|
334 |
|
|
assign wb_req_addr_hit = (wb_req & cache_hit & cached_addr_valid);
|
335 |
|
|
|
336 |
|
|
// Wishbone request detection
|
337 |
|
|
assign wb_req = wb_stb_i & wb_cyc_i & ddr2_calib_done & !sync;
|
338 |
|
|
|
339 |
|
|
always @(posedge wb_clk)
|
340 |
|
|
wb_req_r <= wb_req;
|
341 |
|
|
|
342 |
|
|
assign wb_req_new = wb_req & !wb_req_r;
|
343 |
|
|
|
344 |
|
|
always @(posedge wb_clk)
|
345 |
|
|
wb_req_new_r <= wb_req_new;
|
346 |
|
|
|
347 |
|
|
always @(posedge wb_clk)
|
348 |
|
|
if (wb_rst)
|
349 |
|
|
wb_bursting <= 0;
|
350 |
|
|
// Reset if acking end of transfer
|
351 |
|
|
else if (wb_ack_o && wb_cti_i == 3'b111)
|
352 |
|
|
wb_bursting <= 0;
|
353 |
|
|
// Set if beginning new transaction and incrementing burst indicated
|
354 |
|
|
// TODO - double check if this burst is going to go over a cache line
|
355 |
|
|
// boundary - if so don't allow burst, fall back to classic cycles.
|
356 |
|
|
else if (wb_req_new)
|
357 |
|
|
wb_bursting <= (wb_cti_i == 3'b010);
|
358 |
|
|
|
359 |
|
|
// Help constrain additions to appropriate bit-width for wrapping
|
360 |
|
|
assign wb_burst_addr_4beat = wb_adr_i[3:2] + 1;
|
361 |
|
|
assign wb_burst_addr_8beat = wb_adr_i[4:2] + 1;
|
362 |
|
|
|
363 |
|
|
// Increment burst address whenever we get a hit when reading, or
|
364 |
|
|
// when acking and writing.
|
365 |
|
|
assign wb_burst_addr_incr = (wb_req_addr_hit & (!wb_we_i |
|
366 |
|
|
(wb_we_i & wb_ack_o)));
|
367 |
|
|
|
368 |
|
|
// Calculate burst address depending on burst type indicator
|
369 |
|
|
always @(posedge wb_clk)
|
370 |
|
|
if (wb_rst)
|
371 |
|
|
wb_burst_addr <= 0;
|
372 |
|
|
else if (wb_req_new)
|
373 |
|
|
// When we have a bursting read to an address which is in cache then
|
374 |
|
|
// initialise the address to the next word in the burst sequence.
|
375 |
|
|
// If it's a miss, or it's a write, then we just take what's on the
|
376 |
|
|
// bus.
|
377 |
|
|
wb_burst_addr <= !(wb_req_addr_hit & !wb_we_i) ? wb_adr_i[5:2] :
|
378 |
|
|
wb_bte_i==2'b01 ? {wb_adr_i[5:4], wb_burst_addr_4beat }:
|
379 |
|
|
wb_bte_i==2'b10 ? {wb_adr_i[5], wb_burst_addr_8beat }:
|
380 |
|
|
wb_bte_i==2'b11 ? wb_adr_i[5:2] + 1 :
|
381 |
|
|
wb_adr_i[5:2];
|
382 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b01)
|
383 |
|
|
wb_burst_addr[1:0] <= wb_burst_addr[1:0] + 1;
|
384 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b10)
|
385 |
|
|
wb_burst_addr[2:0] <= wb_burst_addr[2:0] + 1;
|
386 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b11)
|
387 |
|
|
wb_burst_addr[3:0] <= wb_burst_addr[3:0] + 1;
|
388 |
|
|
|
389 |
|
|
`ifdef ERR_COUNTER
|
390 |
|
|
reg [26:0] ack_err_cntr;
|
391 |
|
|
|
392 |
|
|
always @(posedge wb_clk)
|
393 |
|
|
if (wb_rst)
|
394 |
|
|
ack_err_cntr <= 0;
|
395 |
|
|
else if (!wb_req)
|
396 |
|
|
ack_err_cntr <= 0;
|
397 |
|
|
else if (|ack_err_cntr)
|
398 |
|
|
ack_err_cntr <= ack_err_cntr + 1;
|
399 |
|
|
else if (wb_req_new & !(|ack_err_cntr))
|
400 |
|
|
ack_err_cntr <= 1;
|
401 |
|
|
|
402 |
|
|
assign ack_err = (&ack_err_cntr);
|
403 |
|
|
|
404 |
|
|
always @(posedge wb_clk)
|
405 |
|
|
ack_err_r <= ack_err;
|
406 |
|
|
|
407 |
|
|
assign wb_err_o = ack_err_r;
|
408 |
|
|
|
409 |
|
|
`else // !`ifdef ERR_COUNTER
|
410 |
|
|
|
411 |
|
|
assign ack_err = 0;
|
412 |
|
|
always @(posedge wb_clk)
|
413 |
|
|
ack_err_r <= 0;
|
414 |
|
|
|
415 |
|
|
assign wb_err_o = 0;
|
416 |
|
|
|
417 |
|
|
`endif
|
418 |
|
|
|
419 |
|
|
always @(posedge wb_clk)
|
420 |
|
|
if (wb_rst)
|
421 |
|
|
wb_ack_o <= 0;
|
422 |
|
|
else
|
423 |
|
|
wb_ack_o <= wb_req_addr_hit &
|
424 |
|
|
(
|
425 |
|
|
// Simple acks on classic cycles
|
426 |
|
|
(!wb_bursting && !wb_ack_o && !wb_ack_o_r)
|
427 |
|
|
// De-assert ack when we see the final transaction
|
428 |
|
|
|| (wb_bursting && !(wb_cti_i==3'b111))
|
429 |
|
|
);
|
430 |
|
|
|
431 |
|
|
always @(posedge wb_clk)
|
432 |
|
|
wb_ack_o_r <= wb_ack_o;
|
433 |
|
|
|
434 |
|
|
// Logic controling synchronisation
|
435 |
|
|
always @(posedge wb_clk)
|
436 |
|
|
if (wb_rst)
|
437 |
|
|
sync <= 0;
|
438 |
|
|
else if (sync_done) // Sync. done indicator from cache controller
|
439 |
|
|
sync <= 0;
|
440 |
|
|
|
441 |
|
|
always @(posedge wb_clk)
|
442 |
|
|
sync_r <= sync;
|
443 |
|
|
|
444 |
|
|
assign sync_start = sync & !sync_r;
|
445 |
|
|
|
446 |
|
|
task do_sync;
|
447 |
|
|
begin
|
448 |
|
|
// Wait for us to not be doing a transaction.
|
449 |
|
|
while(wb_req)
|
450 |
|
|
@wb_clk;
|
451 |
|
|
// Cache not busy, initiate sync.
|
452 |
|
|
sync = 1;
|
453 |
|
|
end
|
454 |
|
|
endtask // do_sync
|
455 |
|
|
|
456 |
|
|
// Writeback/readfrom lower address generation
|
457 |
|
|
always @(posedge wb_clk)
|
458 |
|
|
if (wb_rst)
|
459 |
|
|
addr_counter <= 0;
|
460 |
|
|
else if (ddr2_p0_cmd_en)
|
461 |
|
|
addr_counter <= addr_counter+1;
|
462 |
|
|
|
463 |
|
|
// Determine if we're writing access requests into DDR2 interface AF
|
464 |
|
|
always @(posedge wb_clk)
|
465 |
|
|
if (wb_rst)
|
466 |
|
|
do_af_write <= 0;
|
467 |
|
|
else if (do_readfrom_start | do_writeback_data_finished)
|
468 |
|
|
do_af_write <= 1;
|
469 |
|
|
else if ((&addr_counter) & !ddr2_p0_cmd_full) // Stop when counter rolls over
|
470 |
|
|
do_af_write <= 0;
|
471 |
|
|
|
472 |
|
|
// Wishbone side of cache enable. Always enabled unless doing DDR2-side
|
473 |
|
|
// things (fill or writeback).
|
474 |
|
|
assign wb_cache_en = !(do_readfrom | do_writeback);
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
// Writeback detect logic
|
478 |
|
|
always @(posedge wb_clk)
|
479 |
|
|
if (wb_rst)
|
480 |
|
|
do_writeback <= 0;
|
481 |
|
|
else if (start_writeback)
|
482 |
|
|
do_writeback <= 1;
|
483 |
|
|
else if (&ddr2_cache_line_word_addr)
|
484 |
|
|
do_writeback <= 0;
|
485 |
|
|
|
486 |
|
|
always @(posedge wb_clk)
|
487 |
|
|
do_writeback_r <= do_writeback;
|
488 |
|
|
|
489 |
|
|
// Detect falling edge of do_writeback
|
490 |
|
|
always @(posedge wb_clk)
|
491 |
|
|
do_writeback_data_finished <= !do_writeback & do_writeback_r;
|
492 |
|
|
|
493 |
|
|
always @(posedge wb_clk)
|
494 |
|
|
if (wb_rst)
|
495 |
|
|
do_writeback_addresses <= 0;
|
496 |
|
|
else if (do_writeback_data_finished)
|
497 |
|
|
do_writeback_addresses <= 1;
|
498 |
|
|
else if ((&addr_counter) & !ddr2_p0_cmd_full)
|
499 |
|
|
do_writeback_addresses <= 0;
|
500 |
|
|
|
501 |
|
|
always @(posedge wb_clk)
|
502 |
|
|
do_writeback_addresses_r <= do_writeback_addresses;
|
503 |
|
|
|
504 |
|
|
// Detect rising edge of do_writeback
|
505 |
|
|
assign do_writeback_start = do_writeback & !do_writeback_r;
|
506 |
|
|
// Detect falling edge of address writing control signal
|
507 |
|
|
assign do_writeback_finished = !do_writeback_addresses &
|
508 |
|
|
do_writeback_addresses_r;
|
509 |
|
|
|
510 |
|
|
// DDR2 Read detect logic
|
511 |
|
|
always @(posedge wb_clk)
|
512 |
|
|
if (wb_rst)
|
513 |
|
|
do_readfrom <= 0;
|
514 |
|
|
else if (start_fill)
|
515 |
|
|
do_readfrom <= 1;
|
516 |
|
|
else if ((&ddr2_cache_line_word_addr))
|
517 |
|
|
do_readfrom <= 0;
|
518 |
|
|
|
519 |
|
|
always @(posedge wb_clk)
|
520 |
|
|
do_readfrom_r <= do_readfrom;
|
521 |
|
|
|
522 |
|
|
// Detect line fill request rising edge
|
523 |
|
|
assign do_readfrom_start = do_readfrom & !do_readfrom_r;
|
524 |
|
|
// Detect line fill request falling edge
|
525 |
|
|
assign do_readfrom_finished = !do_readfrom & do_readfrom_r;
|
526 |
|
|
assign doing_readfrom = do_readfrom | do_readfrom_r;
|
527 |
|
|
|
528 |
|
|
// Address fifo signals
|
529 |
|
|
assign ddr2_p0_cmd_en = ((do_readfrom_r & ddr2_p0_wr_empty) | (do_writeback_addresses_r & !ddr2_p0_wr_empty)) &
|
530 |
|
|
!ddr2_p0_cmd_full & do_af_write;
|
531 |
|
|
assign ddr2_p0_cmd_instr[0] = doing_readfrom; // 1 - read, 0 - write
|
532 |
|
|
assign ddr2_p0_cmd_instr[2:1] = 0;
|
533 |
|
|
|
534 |
|
|
assign writeback_af_addr = {cached_addr, addr_counter, `DDR2_ADDR_ALIGN'd0};
|
535 |
|
|
|
536 |
|
|
assign readfrom_af_addr = {wb_adr_i[`DDR2_CACHE_TAG_BITS], addr_counter, `DDR2_ADDR_ALIGN'd0};
|
537 |
|
|
|
538 |
|
|
assign ddr2_p0_cmd_byte_addr = doing_readfrom ? readfrom_af_addr : writeback_af_addr;
|
539 |
|
|
assign ddr2_p0_wr_en = do_writeback_ddr2_fifo_we;
|
540 |
|
|
assign ddr2_p0_wr_data = ddr2_cache_data_o;
|
541 |
|
|
assign ddr2_p0_wr_mask = 0;
|
542 |
|
|
|
543 |
|
|
always @(posedge wb_clk)
|
544 |
|
|
if (wb_rst)
|
545 |
|
|
ddr2_cache_line_word_addr <= 0;
|
546 |
|
|
else if (!ddr2_p0_rd_empty | (do_writeback & !ddr2_p0_wr_full))
|
547 |
|
|
ddr2_cache_line_word_addr <= ddr2_cache_line_word_addr + 1;
|
548 |
|
|
else if (ddr2_write_done | ddr2_read_done)
|
549 |
|
|
ddr2_cache_line_word_addr <= 0;
|
550 |
|
|
|
551 |
|
|
always @(posedge wb_clk)
|
552 |
|
|
do_writeback_ddr2_fifo_we <= (do_writeback & !ddr2_p0_wr_full);
|
553 |
|
|
|
554 |
|
|
always @(posedge wb_clk)
|
555 |
|
|
if (wb_rst)
|
556 |
|
|
ddr2_write_done <= 0;
|
557 |
|
|
else if ((&ddr2_cache_line_word_addr) & do_writeback)
|
558 |
|
|
ddr2_write_done <= 1;
|
559 |
|
|
else if (!do_writeback) // sample WB domain
|
560 |
|
|
ddr2_write_done <= 0;
|
561 |
|
|
|
562 |
|
|
always @(posedge wb_clk)
|
563 |
|
|
rd_data_valid_r <= !ddr2_p0_rd_empty;
|
564 |
|
|
|
565 |
|
|
// Read done signaling to WB domain
|
566 |
|
|
always @(posedge wb_clk)
|
567 |
|
|
if (wb_rst)
|
568 |
|
|
ddr2_read_done <= 0;
|
569 |
|
|
else if (rd_data_valid_r & (&ddr2_cache_line_word_addr))
|
570 |
|
|
ddr2_read_done <= 1;
|
571 |
|
|
else if (!do_readfrom) // Read WB domain
|
572 |
|
|
ddr2_read_done <= 0;
|
573 |
|
|
|
574 |
|
|
// Lower word address uses potentially bursting address counter
|
575 |
|
|
assign wb_cache_adr = wb_bursting ?
|
576 |
|
|
{wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:6],wb_burst_addr}:
|
577 |
|
|
wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:2];
|
578 |
|
|
|
579 |
|
|
assign wb_cache_sel_we = {4{wb_we_i & wb_ack_o}} & wb_sel_i;
|
580 |
|
|
assign ddr2_cache_en = (!ddr2_p0_rd_empty |do_writeback);
|
581 |
|
|
assign ddr2_cache_we = {16{!ddr2_p0_rd_empty}};
|
582 |
|
|
|
583 |
|
|
// Read enable always on
|
584 |
|
|
assign ddr2_p0_rd_en = 1'b1;
|
585 |
|
|
|
586 |
|
|
`ifdef DDR2_BURSTLENGTH_1
|
587 |
|
|
assign ddr2_p0_cmd_bl = 0; // burst of 1 * 128-bit
|
588 |
|
|
`elsif DDR2_BURSTLENGTH_2
|
589 |
|
|
assign ddr2_p0_cmd_bl = 1; // burst of 2 * 128-bit
|
590 |
|
|
`elsif DDR2_BURSTLENGTH_4
|
591 |
|
|
assign ddr2_p0_cmd_bl = 3; // burst of 4 * 128-bit
|
592 |
|
|
`elsif DDR2_BURSTLENGTH_8
|
593 |
|
|
assign ddr2_p0_cmd_bl = 7; // burst of 8 * 128-bit
|
594 |
|
|
`elsif DDR2_BURSTLENGTH_16
|
595 |
|
|
assign ddr2_p0_cmd_bl = 15; // burst of 16 * 128-bit
|
596 |
|
|
`endif
|
597 |
|
|
|
598 |
|
|
// Xilinx Coregen true dual-port RAMB
|
599 |
|
|
// Wishbone side : 32-bit
|
600 |
|
|
// DDR2 side : 128-bit
|
601 |
|
|
xilinx_ddr2_if_cache cache_mem0
|
602 |
|
|
(
|
603 |
|
|
// Wishbone side
|
604 |
|
|
.clka(wb_clk),
|
605 |
|
|
.ena(wb_cache_en),
|
606 |
|
|
.wea(wb_cache_sel_we),
|
607 |
|
|
.addra({2'd0, selected_cache_line_enc,wb_cache_adr}),
|
608 |
|
|
.dina(wb_dat_i),
|
609 |
|
|
.douta(wb_dat_o),
|
610 |
|
|
|
611 |
|
|
// DDR2 controller side
|
612 |
|
|
.clkb(wb_clk),
|
613 |
|
|
.enb(ddr2_cache_en),
|
614 |
|
|
.web(ddr2_cache_we),
|
615 |
|
|
.addrb({2'd0, selected_cache_line_enc,
|
616 |
|
|
ddr2_cache_line_word_addr}),
|
617 |
|
|
.dinb(ddr2_p0_rd_data),
|
618 |
|
|
.doutb(ddr2_cache_data_o));
|
619 |
|
|
|
620 |
|
|
ddr2_mig #
|
621 |
|
|
(
|
622 |
|
|
.C3_P0_MASK_SIZE (C3_P0_MASK_SIZE),
|
623 |
|
|
.C3_P0_DATA_PORT_SIZE (C3_P0_DATA_PORT_SIZE),
|
624 |
|
|
.DEBUG_EN (DEBUG_EN),
|
625 |
|
|
.C3_MEMCLK_PERIOD (C3_MEMCLK_PERIOD),
|
626 |
|
|
.C3_CALIB_SOFT_IP (C3_CALIB_SOFT_IP),
|
627 |
|
|
.C3_SIMULATION (C3_SIMULATION),
|
628 |
|
|
.C3_RST_ACT_LOW (C3_RST_ACT_LOW),
|
629 |
|
|
.C3_INPUT_CLK_TYPE (C3_INPUT_CLK_TYPE),
|
630 |
|
|
.C3_MEM_ADDR_ORDER (C3_MEM_ADDR_ORDER),
|
631 |
|
|
.C3_NUM_DQ_PINS (C3_NUM_DQ_PINS),
|
632 |
|
|
.C3_MEM_ADDR_WIDTH (C3_MEM_ADDR_WIDTH),
|
633 |
|
|
.C3_MEM_BANKADDR_WIDTH (C3_MEM_BANKADDR_WIDTH)
|
634 |
|
|
)
|
635 |
|
|
ddr2_mig
|
636 |
|
|
(
|
637 |
|
|
|
638 |
|
|
.mcb3_dram_dq (ddr2_dq),
|
639 |
|
|
.mcb3_dram_a (ddr2_a),
|
640 |
|
|
.mcb3_dram_ba (ddr2_ba),
|
641 |
|
|
.mcb3_dram_ras_n (ddr2_ras_n),
|
642 |
|
|
.mcb3_dram_cas_n (ddr2_cas_n),
|
643 |
|
|
.mcb3_dram_we_n (ddr2_we_n),
|
644 |
|
|
.mcb3_dram_odt (ddr2_odt),
|
645 |
|
|
.mcb3_dram_cke (ddr2_cke),
|
646 |
|
|
.mcb3_dram_dm (ddr2_dm),
|
647 |
|
|
.mcb3_dram_udqs (ddr2_udqs),
|
648 |
|
|
.mcb3_dram_udqs_n (ddr2_udqs_n),
|
649 |
|
|
.mcb3_rzq (ddr2_rzq),
|
650 |
|
|
.mcb3_zio (ddr2_zio),
|
651 |
|
|
.mcb3_dram_udm (ddr2_udm),
|
652 |
|
|
.c3_sys_clk (ddr2_if_clk),
|
653 |
|
|
.c3_sys_rst_n (ddr2_if_rst),
|
654 |
|
|
.c3_calib_done (ddr2_calib_done),
|
655 |
|
|
.c3_clk0 (ddr2_clk),
|
656 |
|
|
.c3_rst0 (ddr2_rst),
|
657 |
|
|
.mcb3_dram_dqs (ddr2_dqs),
|
658 |
|
|
.mcb3_dram_dqs_n (ddr2_dqs_n),
|
659 |
|
|
.mcb3_dram_ck (ddr2_ck),
|
660 |
|
|
.mcb3_dram_ck_n (ddr2_ck_n),
|
661 |
|
|
.c3_p0_cmd_clk (wb_clk),
|
662 |
|
|
.c3_p0_cmd_en (ddr2_p0_cmd_en),
|
663 |
|
|
.c3_p0_cmd_instr (ddr2_p0_cmd_instr),
|
664 |
|
|
.c3_p0_cmd_bl (ddr2_p0_cmd_bl),
|
665 |
|
|
.c3_p0_cmd_byte_addr (ddr2_p0_cmd_byte_addr[29:0]),
|
666 |
|
|
.c3_p0_cmd_empty (ddr2_p0_cmd_empty),
|
667 |
|
|
.c3_p0_cmd_full (ddr2_p0_cmd_full),
|
668 |
|
|
.c3_p0_wr_clk (wb_clk),
|
669 |
|
|
.c3_p0_wr_en (ddr2_p0_wr_en),
|
670 |
|
|
.c3_p0_wr_mask (ddr2_p0_wr_mask),
|
671 |
|
|
.c3_p0_wr_data (ddr2_p0_wr_data),
|
672 |
|
|
.c3_p0_wr_full (ddr2_p0_wr_full),
|
673 |
|
|
.c3_p0_wr_empty (ddr2_p0_wr_empty),
|
674 |
|
|
.c3_p0_wr_count (ddr2_p0_wr_count),
|
675 |
|
|
.c3_p0_wr_underrun (ddr2_p0_wr_underrun),
|
676 |
|
|
.c3_p0_wr_error (ddr2_p0_wr_error),
|
677 |
|
|
.c3_p0_rd_clk (wb_clk),
|
678 |
|
|
.c3_p0_rd_en (ddr2_p0_rd_en),
|
679 |
|
|
.c3_p0_rd_data (ddr2_p0_rd_data),
|
680 |
|
|
.c3_p0_rd_full (ddr2_p0_rd_full),
|
681 |
|
|
.c3_p0_rd_empty (ddr2_p0_rd_empty),
|
682 |
|
|
.c3_p0_rd_count (ddr2_p0_rd_count),
|
683 |
|
|
.c3_p0_rd_overflow (ddr2_p0_rd_overflow),
|
684 |
|
|
.c3_p0_rd_error (ddr2_p0_rd_error)
|
685 |
|
|
);
|
686 |
|
|
|
687 |
|
|
endmodule // xilinx_ddr2_if2
|
688 |
|
|
|
689 |
|
|
// Local Variables:
|
690 |
|
|
// verilog-library-directories:("." "ddr2_mig")
|
691 |
|
|
// verilog-library-extensions:(".v" ".h")
|
692 |
|
|
// End:
|
693 |
|
|
|
694 |
|
|
|
695 |
|
|
module xilinx_ddr2_wb_if_cache_adr_reg
|
696 |
|
|
(adr_i, validate, invalidate,
|
697 |
|
|
cached_adr_o, cache_hit, adr_valid,
|
698 |
|
|
clk, rst);
|
699 |
|
|
|
700 |
|
|
parameter full_adr_width = 32;
|
701 |
|
|
parameter word_adr_width = 2; // 4 bytes per word
|
702 |
|
|
parameter line_adr_width = 8; // 256 words per "line"
|
703 |
|
|
|
704 |
|
|
parameter tag_width = full_adr_width - line_adr_width - word_adr_width;
|
705 |
|
|
|
706 |
|
|
|
707 |
|
|
input [full_adr_width-1: word_adr_width + line_adr_width] adr_i;
|
708 |
|
|
input validate;
|
709 |
|
|
input invalidate;
|
710 |
|
|
output [full_adr_width-1: word_adr_width + line_adr_width] cached_adr_o;
|
711 |
|
|
output cache_hit;
|
712 |
|
|
output reg adr_valid;
|
713 |
|
|
|
714 |
|
|
input clk, rst;
|
715 |
|
|
|
716 |
|
|
reg [tag_width-1:0] cached_adr;
|
717 |
|
|
|
718 |
|
|
assign cached_adr_o = cached_adr;
|
719 |
|
|
|
720 |
|
|
always @(posedge clk)
|
721 |
|
|
if (rst)
|
722 |
|
|
cached_adr <= 0;
|
723 |
|
|
else if (validate)
|
724 |
|
|
cached_adr <= adr_i;
|
725 |
|
|
|
726 |
|
|
always @(posedge clk)
|
727 |
|
|
if (rst)
|
728 |
|
|
adr_valid <= 0;
|
729 |
|
|
else if (validate)
|
730 |
|
|
adr_valid <= 1;
|
731 |
|
|
else if (invalidate)
|
732 |
|
|
adr_valid <= 0;
|
733 |
|
|
|
734 |
|
|
assign cache_hit = (adr_i == cached_adr);
|
735 |
|
|
|
736 |
|
|
endmodule // xilinx_ddr2_wb_if_cache_adr_reg
|
737 |
|
|
|
738 |
|
|
module xilinx_ddr2_wb_if_cache_control
|
739 |
|
|
( cache_line_addr_valid, cache_line_addr_hit,
|
740 |
|
|
wb_req,
|
741 |
|
|
cache_write,
|
742 |
|
|
writeback_done, fill_done,
|
743 |
|
|
sync_start, sync_done,
|
744 |
|
|
start_writeback, start_fill,
|
745 |
|
|
cache_line_validate, cache_line_invalidate,
|
746 |
|
|
selected_cache_line, selected_cache_line_enc,
|
747 |
|
|
wb_clk, wb_rst);
|
748 |
|
|
|
749 |
|
|
parameter num_lines = 16;
|
750 |
|
|
parameter num_lines_log2 = 4;
|
751 |
|
|
|
752 |
|
|
input [num_lines-1:0] cache_line_addr_valid;
|
753 |
|
|
input [num_lines-1:0] cache_line_addr_hit;
|
754 |
|
|
|
755 |
|
|
input wb_req;
|
756 |
|
|
input cache_write;
|
757 |
|
|
input writeback_done, fill_done;
|
758 |
|
|
|
759 |
|
|
input sync_start;
|
760 |
|
|
output sync_done;
|
761 |
|
|
|
762 |
|
|
output reg start_writeback;
|
763 |
|
|
output reg start_fill;
|
764 |
|
|
output reg [num_lines-1:0] cache_line_validate;
|
765 |
|
|
output reg [num_lines-1:0] cache_line_invalidate;
|
766 |
|
|
|
767 |
|
|
output [num_lines-1:0] selected_cache_line;
|
768 |
|
|
output reg [num_lines_log2-1:0] selected_cache_line_enc;
|
769 |
|
|
|
770 |
|
|
input wb_clk, wb_rst;
|
771 |
|
|
|
772 |
|
|
reg [num_lines-1:0] lines_dirty;
|
773 |
|
|
|
774 |
|
|
reg [num_lines-1:0] selected_cache_line_from_miss;
|
775 |
|
|
|
776 |
|
|
reg selected_cache_line_new;
|
777 |
|
|
|
778 |
|
|
reg invalidate_clean_line;
|
779 |
|
|
|
780 |
|
|
reg [num_lines-1:0] selected_cache_line_r;
|
781 |
|
|
reg [num_lines-1:0] selected_cache_line_r2;
|
782 |
|
|
|
783 |
|
|
reg wb_req_r;
|
784 |
|
|
|
785 |
|
|
wire wb_req_new;
|
786 |
|
|
reg wb_req_new_r;
|
787 |
|
|
|
788 |
|
|
parameter sync_line_check_wait = 4;
|
789 |
|
|
reg [num_lines-1:0] sync_line_counter;
|
790 |
|
|
reg sync_doing;
|
791 |
|
|
reg [sync_line_check_wait-1:0] sync_line_select_wait_counter_shr;
|
792 |
|
|
reg sync_line_done;
|
793 |
|
|
wire sync_writeback_line;
|
794 |
|
|
|
795 |
|
|
|
796 |
|
|
always @(posedge wb_clk)
|
797 |
|
|
wb_req_r <= wb_req;
|
798 |
|
|
|
799 |
|
|
assign wb_req_new = wb_req & !wb_req_r;
|
800 |
|
|
|
801 |
|
|
always @(posedge wb_clk)
|
802 |
|
|
wb_req_new_r <= wb_req_new;
|
803 |
|
|
|
804 |
|
|
// Select a cache line when we miss. Currently very simply is round robin
|
805 |
|
|
always @(posedge wb_clk)
|
806 |
|
|
if (wb_rst)
|
807 |
|
|
selected_cache_line_from_miss <= 1;
|
808 |
|
|
else if (wb_req_new_r & !(|selected_cache_line_r)) // miss,no line selected
|
809 |
|
|
// Shift select bit one
|
810 |
|
|
selected_cache_line_from_miss
|
811 |
|
|
<= {selected_cache_line_from_miss[num_lines-2:0],
|
812 |
|
|
selected_cache_line_from_miss[num_lines-1]};
|
813 |
|
|
|
814 |
|
|
|
815 |
|
|
// Line selection logic, when line address is valid and hit, we select
|
816 |
|
|
always @(posedge wb_clk)
|
817 |
|
|
if (wb_rst)
|
818 |
|
|
selected_cache_line_r <= 0;
|
819 |
|
|
else if (wb_req_new)
|
820 |
|
|
selected_cache_line_r <= cache_line_addr_valid & cache_line_addr_hit;
|
821 |
|
|
else if (wb_req_new_r & !(|selected_cache_line_r))
|
822 |
|
|
selected_cache_line_r <= selected_cache_line_from_miss;
|
823 |
|
|
else if (sync_doing)
|
824 |
|
|
selected_cache_line_r <= sync_line_counter;
|
825 |
|
|
|
826 |
|
|
always @(posedge wb_clk)
|
827 |
|
|
selected_cache_line_r2 <= selected_cache_line_r;
|
828 |
|
|
|
829 |
|
|
assign selected_cache_line = selected_cache_line_r2;
|
830 |
|
|
|
831 |
|
|
// A new line of cache has been selected
|
832 |
|
|
always @(posedge wb_clk)
|
833 |
|
|
if (wb_rst)
|
834 |
|
|
selected_cache_line_new <= 0;
|
835 |
|
|
else if (wb_req_new & (&(cache_line_addr_valid & cache_line_addr_hit)))
|
836 |
|
|
// New line address selected
|
837 |
|
|
selected_cache_line_new <= 1;
|
838 |
|
|
else if ((!selected_cache_line_new) & wb_req_new_r)
|
839 |
|
|
// Didn't select one last time, so we must have forced ourselves to
|
840 |
|
|
// select a new one
|
841 |
|
|
selected_cache_line_new <= 1;
|
842 |
|
|
else if (selected_cache_line_new)
|
843 |
|
|
selected_cache_line_new <= 0;
|
844 |
|
|
|
845 |
|
|
always @(posedge wb_clk)
|
846 |
|
|
if (wb_rst)
|
847 |
|
|
lines_dirty <= 0;
|
848 |
|
|
else if (cache_write)
|
849 |
|
|
lines_dirty <= lines_dirty | selected_cache_line_r;
|
850 |
|
|
else if (writeback_done)
|
851 |
|
|
lines_dirty <= lines_dirty & ~(selected_cache_line_r);
|
852 |
|
|
|
853 |
|
|
// Validate the cache line address in the register when line filled
|
854 |
|
|
always @(posedge wb_clk)
|
855 |
|
|
if (wb_rst)
|
856 |
|
|
cache_line_validate <= 0;
|
857 |
|
|
else if (fill_done)
|
858 |
|
|
cache_line_validate <= selected_cache_line_r;
|
859 |
|
|
else if (|cache_line_validate)
|
860 |
|
|
cache_line_validate <= 0;
|
861 |
|
|
|
862 |
|
|
// Invalidate the cache line address in the register when line written back
|
863 |
|
|
always @(posedge wb_clk)
|
864 |
|
|
if (wb_rst)
|
865 |
|
|
cache_line_invalidate <= 0;
|
866 |
|
|
else if ((writeback_done & !sync_doing) | invalidate_clean_line)
|
867 |
|
|
cache_line_invalidate <= selected_cache_line_r;
|
868 |
|
|
else if (|cache_line_invalidate)
|
869 |
|
|
cache_line_invalidate <= 0;
|
870 |
|
|
|
871 |
|
|
// Initiate-writeback logic
|
872 |
|
|
always @(posedge wb_clk)
|
873 |
|
|
if (wb_rst)
|
874 |
|
|
start_writeback <= 0;
|
875 |
|
|
else if (start_writeback)
|
876 |
|
|
start_writeback <= 0;
|
877 |
|
|
else if (selected_cache_line_new &
|
878 |
|
|
(|(lines_dirty & selected_cache_line_r)) &
|
879 |
|
|
(|(selected_cache_line_r & cache_line_addr_valid)) &
|
880 |
|
|
!(|(cache_line_addr_hit & selected_cache_line_r)))
|
881 |
|
|
start_writeback <= 1;
|
882 |
|
|
else if (sync_writeback_line)
|
883 |
|
|
start_writeback <= 1;
|
884 |
|
|
|
885 |
|
|
// Invalidate lines which we haven't written to so we can fill them
|
886 |
|
|
always @(posedge wb_clk)
|
887 |
|
|
if (wb_rst)
|
888 |
|
|
invalidate_clean_line <= 0;
|
889 |
|
|
else if (invalidate_clean_line)
|
890 |
|
|
invalidate_clean_line <= 0;
|
891 |
|
|
else if ((selected_cache_line_new) & // New line selected
|
892 |
|
|
!(|(lines_dirty & selected_cache_line_r)) & // It's not dirty
|
893 |
|
|
// It's valid, but we've selected it so we're trashing it
|
894 |
|
|
(|(selected_cache_line_r & cache_line_addr_valid)) &
|
895 |
|
|
!(|(cache_line_addr_hit & selected_cache_line_r))) // Not a hit
|
896 |
|
|
invalidate_clean_line <= 1;
|
897 |
|
|
|
898 |
|
|
reg invalidate_clean_line_r;
|
899 |
|
|
always @(posedge wb_clk)
|
900 |
|
|
invalidate_clean_line_r <= invalidate_clean_line;
|
901 |
|
|
|
902 |
|
|
|
903 |
|
|
// Initiate-fill logic
|
904 |
|
|
always @(posedge wb_clk)
|
905 |
|
|
if (wb_rst)
|
906 |
|
|
start_fill <= 0;
|
907 |
|
|
else if (((selected_cache_line_new) & // New line selected
|
908 |
|
|
// not valid
|
909 |
|
|
!(|(cache_line_addr_valid & selected_cache_line_r))) |
|
910 |
|
|
(writeback_done & !sync_doing) | invalidate_clean_line_r
|
911 |
|
|
)
|
912 |
|
|
start_fill <= 1;
|
913 |
|
|
else if (start_fill)
|
914 |
|
|
start_fill <= 0;
|
915 |
|
|
|
916 |
|
|
// Hardcoded to 4 lines currently.
|
917 |
|
|
always @(posedge wb_clk)
|
918 |
|
|
if (selected_cache_line_r[0])
|
919 |
|
|
selected_cache_line_enc <= 0;
|
920 |
|
|
else if (selected_cache_line_r[1])
|
921 |
|
|
selected_cache_line_enc <= 1;
|
922 |
|
|
else if (selected_cache_line_r[2])
|
923 |
|
|
selected_cache_line_enc <= 2;
|
924 |
|
|
else if (selected_cache_line_r[3])
|
925 |
|
|
selected_cache_line_enc <= 3;
|
926 |
|
|
|
927 |
|
|
|
928 |
|
|
// Synchronisation control
|
929 |
|
|
|
930 |
|
|
always @(posedge wb_clk)
|
931 |
|
|
if (wb_rst)
|
932 |
|
|
sync_doing <= 0;
|
933 |
|
|
else if (sync_start)
|
934 |
|
|
sync_doing <= 1;
|
935 |
|
|
else if (sync_done)
|
936 |
|
|
sync_doing <= 0;
|
937 |
|
|
|
938 |
|
|
always @(posedge wb_clk)
|
939 |
|
|
if (wb_rst)
|
940 |
|
|
sync_line_counter <= 0;
|
941 |
|
|
else if (sync_start)
|
942 |
|
|
// Set first line to check
|
943 |
|
|
sync_line_counter[0] <= 1'b1;
|
944 |
|
|
else if (sync_line_done)
|
945 |
|
|
// Shift along, check next line
|
946 |
|
|
sync_line_counter <= {sync_line_counter[num_lines-2:0], 1'b0};
|
947 |
|
|
|
948 |
|
|
// Pulse this on finishing of checking lines
|
949 |
|
|
assign sync_done = sync_line_counter[num_lines-1] & sync_line_done;
|
950 |
|
|
|
951 |
|
|
// Pulses when a dirty line is detected and should be written back.
|
952 |
|
|
assign sync_writeback_line = sync_doing &
|
953 |
|
|
sync_line_select_wait_counter_shr[0] &
|
954 |
|
|
cache_line_addr_valid &
|
955 |
|
|
|(sync_line_counter & lines_dirty);
|
956 |
|
|
|
957 |
|
|
always @(posedge wb_clk)
|
958 |
|
|
if (wb_rst)
|
959 |
|
|
sync_line_select_wait_counter_shr <= 0;
|
960 |
|
|
else if (|sync_line_select_wait_counter_shr)
|
961 |
|
|
sync_line_select_wait_counter_shr
|
962 |
|
|
<= {1'b0,sync_line_select_wait_counter_shr[sync_line_check_wait-1:1]};
|
963 |
|
|
else if (sync_start | (sync_line_done & !sync_done))
|
964 |
|
|
sync_line_select_wait_counter_shr[sync_line_check_wait-1] <= 1'b1;
|
965 |
|
|
|
966 |
|
|
always @(posedge wb_clk)
|
967 |
|
|
if (wb_rst)
|
968 |
|
|
sync_line_done <= 1'b0;
|
969 |
|
|
else if (sync_line_done)
|
970 |
|
|
sync_line_done <= 1'b0;
|
971 |
|
|
// Either line doesn't need writeback
|
972 |
|
|
else if (sync_line_select_wait_counter_shr[0] & !sync_writeback_line)
|
973 |
|
|
sync_line_done <= 1'b1;
|
974 |
|
|
// Or writeback finished
|
975 |
|
|
else if (writeback_done & sync_doing)
|
976 |
|
|
sync_line_done <= 1'b1;
|
977 |
|
|
|
978 |
|
|
|
979 |
|
|
endmodule // xilinx_ddr2_wb_if_cache_control
|