1 |
568 |
julius |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// Xilinx Spartan 3A DSP DDR2 controller from MIG ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// Description ////
|
6 |
|
|
//// ////
|
7 |
|
|
//// To Do: ////
|
8 |
|
|
//// Employ LRU replacement scheme ////
|
9 |
|
|
//// Remove hard-coding of things relating to number of lines ////
|
10 |
|
|
//// ////
|
11 |
|
|
//// Author(s): ////
|
12 |
|
|
//// - Julius Baxter, julius@opencores.org ////
|
13 |
|
|
//// ////
|
14 |
|
|
//// ////
|
15 |
|
|
//////////////////////////////////////////////////////////////////////
|
16 |
|
|
//// ////
|
17 |
|
|
//// Copyright (C) 2011 Authors and OPENCORES.ORG ////
|
18 |
|
|
//// ////
|
19 |
|
|
//// This source file may be used and distributed without ////
|
20 |
|
|
//// restriction provided that this copyright statement is not ////
|
21 |
|
|
//// removed from the file and that any derivative work contains ////
|
22 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
23 |
|
|
//// ////
|
24 |
|
|
//// This source file is free software; you can redistribute it ////
|
25 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
26 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
27 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
28 |
|
|
//// later version. ////
|
29 |
|
|
//// ////
|
30 |
|
|
//// This source is distributed in the hope that it will be ////
|
31 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
32 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
33 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
34 |
|
|
//// details. ////
|
35 |
|
|
//// ////
|
36 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
37 |
|
|
//// Public License along with this source; if not, download it ////
|
38 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
39 |
|
|
//// ////
|
40 |
|
|
//////////////////////////////////////////////////////////////////////
|
41 |
|
|
// Cache generated by coregen, uses 2x18kbit block RAMS, or 4KByte
|
42 |
|
|
// Currently only using 4 lines x 8 words * 32-bits (4bytes) = 128 bytes
|
43 |
|
|
// Could potentially have up to (4096/(8*4)) 128 lines.
|
44 |
|
|
|
45 |
|
|
// Addressing for DDR2 interface is as follows.
|
46 |
|
|
// SDRAM is 128MB, so up to 0x08000000 (28 bits)
|
47 |
|
|
// DDR2 MIG configured in burst length of 8 (8*32 per request)
|
48 |
|
|
// DDR2 MIG takes 25 bit address (row[12:0],col[9:0],ba[1:0])
|
49 |
|
|
|
50 |
|
|
// So for every address on DDR2 MIG address interface, we get 8*32-bit words
|
51 |
|
|
// or (8*4) 32 bytes. 5-bits of address on the Wishbone bus.
|
52 |
|
|
|
53 |
|
|
// Current setting is just 4-lines of cache with 8-words per line
|
54 |
|
|
// We'll have 3-bits for words in the line.
|
55 |
|
|
// Tag will be 28-3-2 (final 2 is due to 4-bytes per word) so 23-bits.
|
56 |
|
|
|
57 |
|
|
// So address to the DDR2 interface should be the 23 bits of tag, and 2'b00
|
58 |
|
|
// for the 4 column addresses we burst over.
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
module xilinx_s3adsp_ddr2_if (
|
62 |
|
|
input [31:0] wb_adr_i,
|
63 |
|
|
input wb_stb_i,
|
64 |
|
|
input wb_cyc_i,
|
65 |
|
|
input [2:0] wb_cti_i,
|
66 |
|
|
input [1:0] wb_bte_i,
|
67 |
|
|
input wb_we_i,
|
68 |
|
|
input [3:0] wb_sel_i,
|
69 |
|
|
input [31:0] wb_dat_i,
|
70 |
|
|
output [31:0] wb_dat_o,
|
71 |
|
|
output reg wb_ack_o,
|
72 |
|
|
|
73 |
|
|
inout [31:0] ddr2_dq,
|
74 |
|
|
output [12:0] ddr2_a,
|
75 |
|
|
output [1:0] ddr2_ba,
|
76 |
|
|
output ddr2_cke,
|
77 |
|
|
output ddr2_cs_n,
|
78 |
|
|
output ddr2_ras_n,
|
79 |
|
|
output ddr2_cas_n,
|
80 |
|
|
output ddr2_we_n,
|
81 |
|
|
output ddr2_odt,
|
82 |
|
|
output [3:0] ddr2_dm,
|
83 |
|
|
inout [3:0] ddr2_dqs,
|
84 |
|
|
inout [3:0] ddr2_dqs_n,
|
85 |
|
|
output [1:0] ddr2_ck,
|
86 |
|
|
output [1:0] ddr2_ck_n,
|
87 |
|
|
input ddr2_rst_dqs_div_in,
|
88 |
|
|
output ddr2_rst_dqs_div_out,
|
89 |
|
|
input clk133_i,
|
90 |
|
|
|
91 |
|
|
input wb_clk,
|
92 |
|
|
input wb_rst);
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
// 128MB, so 0x800_0000
|
96 |
|
|
`define ADDR_BIT_MAX 28
|
97 |
|
|
|
98 |
|
|
/*
|
99 |
|
|
`define DDR2_CACHE_NUM_LINES 16
|
100 |
|
|
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 4 // log2(`DDR2_CACHE_NUM_LINES)
|
101 |
|
|
*/
|
102 |
|
|
|
103 |
|
|
`define DDR2_CACHE_NUM_LINES 4
|
104 |
|
|
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 2 // log2(`DDR2_CACHE_NUM_LINES)
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
//`define DDR2_CACHE_NUM_LINES 32
|
108 |
|
|
//`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 5 // log2(`DDR2_CACHE_NUM_LINES)
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
`define DDR2_CACHE_NUM_WORDS_PER_LINE 8
|
112 |
|
|
`define DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE 3
|
113 |
|
|
`define DDR2_CACHE_TAG_ADDR_WIDTH (32-`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-2)
|
114 |
|
|
|
115 |
|
|
`define DDR2_CACHE_DDR2_SIDE_NUM_WORDS_PER_LINE (`DDR2_CACHE_NUM_WORDS_PER_LINE/2)
|
116 |
|
|
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE (`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE - 1)
|
117 |
|
|
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH (`DDR2_CACHE_NUM_LINES_ENC_WIDTH + `DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE)
|
118 |
|
|
|
119 |
|
|
`define BYTES_PER_WORD 4
|
120 |
|
|
`define BYTES_PER_WORD_ADDR_WIDTH 2
|
121 |
|
|
|
122 |
|
|
// Board has 128MB of memory, so highest address is 27'h7ff_ffff
|
123 |
|
|
`define DDR2_CACHE_TAG_BITS (`ADDR_BIT_MAX-1):(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE + \
|
124 |
|
|
`BYTES_PER_WORD_ADDR_WIDTH)
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
wire wb_req;
|
128 |
|
|
reg wb_req_r;
|
129 |
|
|
reg wb_ack_o_r;
|
130 |
|
|
|
131 |
|
|
wire wb_req_new;
|
132 |
|
|
reg wb_req_new_r;
|
133 |
|
|
|
134 |
|
|
wire wb_req_addr_hit;
|
135 |
|
|
|
136 |
|
|
wire cached_addr_valid;
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
wire [`DDR2_CACHE_TAG_BITS] cache_line_tag;
|
140 |
|
|
|
141 |
|
|
wire cache_write;
|
142 |
|
|
|
143 |
|
|
wire cache_hit;
|
144 |
|
|
|
145 |
|
|
wire wb_cache_en;
|
146 |
|
|
|
147 |
|
|
reg do_writeback;
|
148 |
|
|
|
149 |
|
|
reg do_readfrom;
|
150 |
|
|
reg do_readfrom_ddr2_sync, do_readfrom_ddr2_sync2,
|
151 |
|
|
do_readfrom_ddr2_sync3;
|
152 |
|
|
wire do_readfrom_start_ddr2;
|
153 |
|
|
|
154 |
|
|
reg do_writeback_ddr2_sync, do_writeback_ddr2_sync2,
|
155 |
|
|
do_writeback_ddr2_sync3;
|
156 |
|
|
wire do_writeback_start_ddr2;
|
157 |
|
|
|
158 |
|
|
reg ddr2_readfrom_done;
|
159 |
|
|
reg wb_readfrom_done_sync,wb_readfrom_done_sync2;
|
160 |
|
|
wire wb_readfrom_done;
|
161 |
|
|
|
162 |
|
|
reg ddr2_writeback_done;
|
163 |
|
|
reg wb_writeback_done_sync,wb_writeback_done_sync2;
|
164 |
|
|
wire wb_writeback_done;
|
165 |
|
|
|
166 |
|
|
reg ddr2_readfrom_done_wb_sync, ddr2_readfrom_done_wb_sync2;
|
167 |
|
|
reg ddr2_writeback_done_wb_sync, ddr2_writeback_done_wb_sync2;
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
wire ddr2_if_clk_90;
|
171 |
|
|
wire ddr2_if_clk;
|
172 |
|
|
wire ddr2_rst, ddr2_rst_90, ddr2_rst_180;
|
173 |
|
|
reg [24:0] ddr2_if_input_address;
|
174 |
|
|
wire [63:0] ddr2_if_wdata;
|
175 |
|
|
wire [63:0] ddr2_if_rdata;
|
176 |
|
|
wire ddr2_if_rdata_valid;
|
177 |
|
|
reg [2:0] ddr2_if_cmd;
|
178 |
|
|
|
179 |
|
|
wire ddr2_if_ref_req, ddr2_if_cmd_ack;
|
180 |
|
|
wire ddr2_if_af_done, ddr2_if_burst_done;
|
181 |
|
|
reg ddr2_if_init_sent;
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
// [0] - Waiting for ddr2_if_cmd_ack
|
185 |
|
|
// [4:1] - data0-data3 out from RAM
|
186 |
|
|
// [8:5] - ddr2_if_burst_done high for 4 cycles
|
187 |
|
|
// [9] - finish
|
188 |
|
|
reg [9:0] ddr2_write_state_shr;
|
189 |
|
|
|
190 |
|
|
// [0] - waiting for ddr2_if_cmd_ack
|
191 |
|
|
// [4:1] - asserting ddr2_if_burst_done
|
192 |
|
|
// [5] - waiting for ddr2_if_cmd_ack to be deasserted
|
193 |
|
|
// [6] - finish
|
194 |
|
|
reg [6:0] ddr2_read_state_shr;
|
195 |
|
|
|
196 |
|
|
reg [4:0] ddr2_read_dv_counter_shr;
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
wire phy_init_done;
|
200 |
|
|
|
201 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_validate;
|
202 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_invalidate;
|
203 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_addr_valid;
|
204 |
|
|
wire [`DDR2_CACHE_NUM_LINES - 1 :0] cache_line_hit;
|
205 |
|
|
wire [`DDR2_CACHE_TAG_BITS] cache_line_addr [0:`DDR2_CACHE_NUM_LINES-1] ;
|
206 |
|
|
|
207 |
|
|
// Cache control signals
|
208 |
|
|
// Wishbone side
|
209 |
|
|
|
210 |
|
|
wire [3:0] wb_cache_sel_we;
|
211 |
|
|
// DDR side
|
212 |
|
|
wire ddr2_cache_en;
|
213 |
|
|
wire [7:0] ddr2_cache_we;
|
214 |
|
|
|
215 |
|
|
reg wb_bursting; // Are we bursting?
|
216 |
|
|
reg [2:0] wb_burst_addr; // Burst counter, up to 8
|
217 |
|
|
wire [1:0] wb_burst_addr_4beat;
|
218 |
|
|
wire [2:0] wb_burst_addr_8beat;
|
219 |
|
|
wire wb_burst_addr_incr;
|
220 |
|
|
wire ack_err;
|
221 |
|
|
reg ack_err_r;
|
222 |
|
|
|
223 |
|
|
// Synchronisation signals
|
224 |
|
|
reg sync, sync_r;
|
225 |
|
|
wire sync_start;
|
226 |
|
|
wire sync_done;
|
227 |
|
|
|
228 |
|
|
wire [`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-1:0] wb_cache_word;
|
229 |
|
|
|
230 |
|
|
// Decoded select line
|
231 |
|
|
wire [`DDR2_CACHE_NUM_LINES-1:0] selected_cache_line;
|
232 |
|
|
wire [`DDR2_CACHE_NUM_LINES_ENC_WIDTH-1:0] selected_cache_line_enc;
|
233 |
|
|
reg [`DDR2_CACHE_NUM_LINES_ENC_WIDTH-1:0] selected_cache_line_enc_ddr2_if_clk;
|
234 |
|
|
|
235 |
|
|
reg ddr2_cache_line_word_use_next_addr;
|
236 |
|
|
wire [`DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE - 1:0] ddr2_cache_line_word_addr;
|
237 |
|
|
reg [`DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE - 1:0] ddr2_cache_line_word_addr_r;
|
238 |
|
|
wire [`DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE - 1:0] ddr2_cache_line_word_addr_next;
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
genvar i;
|
242 |
|
|
generate
|
243 |
|
|
for (i=0;i<`DDR2_CACHE_NUM_LINES;i=i+1) begin : cache_addr
|
244 |
|
|
ddr2_wb_if_cache_addr_reg
|
245 |
|
|
#(.full_addr_width(28), // 128MB
|
246 |
|
|
.line_addr_width(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE))
|
247 |
|
|
cache_addr_reg_inst
|
248 |
|
|
( .addr_i(wb_adr_i[`DDR2_CACHE_TAG_BITS]),
|
249 |
|
|
.validate(cache_line_addr_validate[i]),
|
250 |
|
|
.invalidate(cache_line_addr_invalidate[i]),
|
251 |
|
|
.cache_hit(cache_line_hit[i]),
|
252 |
|
|
.addr_valid(cache_line_addr_valid[i]),
|
253 |
|
|
.cached_addr_o(cache_line_addr[i]),
|
254 |
|
|
.clk(wb_clk),
|
255 |
|
|
.rst(wb_rst));
|
256 |
|
|
end
|
257 |
|
|
endgenerate
|
258 |
|
|
|
259 |
|
|
wire start_writeback, start_fill;
|
260 |
|
|
|
261 |
|
|
ddr2_wb_if_cache_control ddr2_wb_if_cache_control0
|
262 |
|
|
(
|
263 |
|
|
// Outputs
|
264 |
|
|
.start_writeback (start_writeback),
|
265 |
|
|
.start_fill (start_fill),
|
266 |
|
|
.cache_line_validate (cache_line_addr_validate),
|
267 |
|
|
.cache_line_invalidate (cache_line_addr_invalidate),
|
268 |
|
|
.selected_cache_line (selected_cache_line),
|
269 |
|
|
.selected_cache_line_enc (selected_cache_line_enc),
|
270 |
|
|
.sync_done (sync_done),
|
271 |
|
|
// Inputs
|
272 |
|
|
.cache_line_addr_valid (cache_line_addr_valid),
|
273 |
|
|
.cache_line_addr_hit (cache_line_hit),
|
274 |
|
|
.wb_req (wb_req),
|
275 |
|
|
.cache_write (cache_write),
|
276 |
|
|
.writeback_done (wb_writeback_done),
|
277 |
|
|
.fill_done (wb_readfrom_done),
|
278 |
|
|
.sync_start (sync_start),
|
279 |
|
|
.wb_clk (wb_clk),
|
280 |
|
|
.wb_rst (wb_rst));
|
281 |
|
|
|
282 |
|
|
defparam ddr2_wb_if_cache_control0.num_lines = `DDR2_CACHE_NUM_LINES;
|
283 |
|
|
defparam ddr2_wb_if_cache_control0.num_lines_log2 = `DDR2_CACHE_NUM_LINES_ENC_WIDTH;
|
284 |
|
|
|
285 |
|
|
assign cache_line_tag = selected_cache_line[0] ? cache_line_addr[0] :
|
286 |
|
|
selected_cache_line[1] ? cache_line_addr[1] :
|
287 |
|
|
selected_cache_line[2] ? cache_line_addr[2] :
|
288 |
|
|
selected_cache_line[3] ? cache_line_addr[3] :
|
289 |
|
|
/*
|
290 |
|
|
selected_cache_line[4] ? cache_line_addr[4] :
|
291 |
|
|
selected_cache_line[5] ? cache_line_addr[5] :
|
292 |
|
|
selected_cache_line[6] ? cache_line_addr[6] :
|
293 |
|
|
selected_cache_line[7] ? cache_line_addr[7] :
|
294 |
|
|
selected_cache_line[8] ? cache_line_addr[8] :
|
295 |
|
|
selected_cache_line[9] ? cache_line_addr[9] :
|
296 |
|
|
selected_cache_line[10] ? cache_line_addr[10] :
|
297 |
|
|
selected_cache_line[11] ? cache_line_addr[11] :
|
298 |
|
|
selected_cache_line[12] ? cache_line_addr[12] :
|
299 |
|
|
selected_cache_line[13] ? cache_line_addr[13] :
|
300 |
|
|
selected_cache_line[14] ? cache_line_addr[14] :
|
301 |
|
|
selected_cache_line[15] ? cache_line_addr[15] :
|
302 |
|
|
selected_cache_line[16] ? cache_line_addr[16] :
|
303 |
|
|
selected_cache_line[17] ? cache_line_addr[17] :
|
304 |
|
|
selected_cache_line[18] ? cache_line_addr[18] :
|
305 |
|
|
selected_cache_line[19] ? cache_line_addr[19] :
|
306 |
|
|
selected_cache_line[20] ? cache_line_addr[20] :
|
307 |
|
|
selected_cache_line[21] ? cache_line_addr[21] :
|
308 |
|
|
selected_cache_line[22] ? cache_line_addr[22] :
|
309 |
|
|
selected_cache_line[23] ? cache_line_addr[23] :
|
310 |
|
|
selected_cache_line[24] ? cache_line_addr[24] :
|
311 |
|
|
selected_cache_line[25] ? cache_line_addr[25] :
|
312 |
|
|
selected_cache_line[26] ? cache_line_addr[26] :
|
313 |
|
|
selected_cache_line[27] ? cache_line_addr[27] :
|
314 |
|
|
selected_cache_line[28] ? cache_line_addr[28] :
|
315 |
|
|
selected_cache_line[29] ? cache_line_addr[29] :
|
316 |
|
|
selected_cache_line[30] ? cache_line_addr[30] :
|
317 |
|
|
selected_cache_line[31] ? cache_line_addr[31] :
|
318 |
|
|
*/
|
319 |
|
|
0;
|
320 |
|
|
|
321 |
|
|
assign cache_write = wb_req & wb_we_i & wb_ack_o;
|
322 |
|
|
|
323 |
|
|
assign cache_hit = |(selected_cache_line & cache_line_hit);
|
324 |
|
|
|
325 |
|
|
assign cached_addr_valid = |(selected_cache_line & cache_line_addr_valid);
|
326 |
|
|
|
327 |
|
|
assign wb_req_addr_hit = (wb_req & cache_hit & cached_addr_valid);
|
328 |
|
|
|
329 |
|
|
// Wishbone request detection
|
330 |
|
|
assign wb_req = wb_stb_i & wb_cyc_i & phy_init_done & !sync;
|
331 |
|
|
|
332 |
|
|
always @(posedge wb_clk)
|
333 |
|
|
wb_req_r <= wb_req;
|
334 |
|
|
|
335 |
|
|
assign wb_req_new = wb_req & !wb_req_r;
|
336 |
|
|
|
337 |
|
|
always @(posedge wb_clk)
|
338 |
|
|
wb_req_new_r <= wb_req_new;
|
339 |
|
|
|
340 |
|
|
always @(posedge wb_clk)
|
341 |
|
|
if (wb_rst)
|
342 |
|
|
wb_bursting <= 0;
|
343 |
|
|
// Reset if acking end of transfer
|
344 |
|
|
else if (wb_ack_o && wb_cti_i == 3'b111)
|
345 |
|
|
wb_bursting <= 0;
|
346 |
|
|
// Set if beginning new transaction and incrementing burst indicated
|
347 |
|
|
// TODO - double check if this burst is going to go over a cache line
|
348 |
|
|
// boundary - if so don't allow burst, fall back to classic cycles.
|
349 |
|
|
else if (wb_req_new)
|
350 |
|
|
wb_bursting <= (wb_cti_i == 3'b010);
|
351 |
|
|
|
352 |
|
|
// Help constrain additions to appropriate bit-width for wrapping
|
353 |
|
|
assign wb_burst_addr_4beat = wb_adr_i[3:2] + 1;
|
354 |
|
|
assign wb_burst_addr_8beat = wb_adr_i[4:2] + 1;
|
355 |
|
|
|
356 |
|
|
// Increment burst address whenever we get a hit when reading, or
|
357 |
|
|
// when acking and writing.
|
358 |
|
|
assign wb_burst_addr_incr = (wb_req_addr_hit & (!wb_we_i |
|
359 |
|
|
(wb_we_i & wb_ack_o)));
|
360 |
|
|
|
361 |
|
|
// Calculate burst address depending on burst type indicator
|
362 |
|
|
always @(posedge wb_clk)
|
363 |
|
|
if (wb_rst)
|
364 |
|
|
wb_burst_addr <= 0;
|
365 |
|
|
else if (wb_req_new)
|
366 |
|
|
// When we have a bursting read to an address which is in cache then
|
367 |
|
|
// initialise the address to the next word in the burst sequence.
|
368 |
|
|
// If it's a miss, or it's a write, then we just take what's on the
|
369 |
|
|
// bus.
|
370 |
|
|
wb_burst_addr <= !(wb_req_addr_hit & !wb_we_i) ? wb_adr_i[4:2] :
|
371 |
|
|
wb_bte_i==2'b01 ? {wb_adr_i[4], wb_burst_addr_4beat }:
|
372 |
|
|
wb_bte_i==2'b10 ? {wb_burst_addr_8beat }:
|
373 |
|
|
/*wb_bte_i==2'b11 ? wb_adr_i[5:2] + 1 :*/
|
374 |
|
|
wb_adr_i[5:2];
|
375 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b01)
|
376 |
|
|
wb_burst_addr[1:0] <= wb_burst_addr[1:0] + 1;
|
377 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b10)
|
378 |
|
|
wb_burst_addr[2:0] <= wb_burst_addr[2:0] + 1;
|
379 |
|
|
/*
|
380 |
|
|
else if (wb_burst_addr_incr & wb_bte_i==2'b11)
|
381 |
|
|
wb_burst_addr[3:0] <= wb_burst_addr[3:0] + 1;
|
382 |
|
|
*/
|
383 |
|
|
assign ack_err = 0;
|
384 |
|
|
always @(posedge wb_clk)
|
385 |
|
|
ack_err_r <= 0;
|
386 |
|
|
|
387 |
|
|
assign wb_err_o = 0;
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
always @(posedge wb_clk)
|
391 |
|
|
if (wb_rst)
|
392 |
|
|
wb_ack_o <= 0;
|
393 |
|
|
else
|
394 |
|
|
wb_ack_o <= wb_req_addr_hit &
|
395 |
|
|
(
|
396 |
|
|
// Simple acks on classic cycles
|
397 |
|
|
(!wb_bursting && !wb_ack_o && !wb_ack_o_r)
|
398 |
|
|
// De-assert ack when we see the final transaction
|
399 |
|
|
|| (wb_bursting && !(wb_cti_i==3'b111))
|
400 |
|
|
);
|
401 |
|
|
|
402 |
|
|
always @(posedge wb_clk)
|
403 |
|
|
wb_ack_o_r <= wb_ack_o;
|
404 |
|
|
|
405 |
|
|
// Logic controling synchronisation
|
406 |
|
|
always @(posedge wb_clk)
|
407 |
|
|
if (wb_rst)
|
408 |
|
|
sync <= 0;
|
409 |
|
|
else if (sync_done) // Sync. done indicator from cache controller
|
410 |
|
|
sync <= 0;
|
411 |
|
|
|
412 |
|
|
always @(posedge wb_clk)
|
413 |
|
|
sync_r <= sync;
|
414 |
|
|
|
415 |
|
|
assign sync_start = sync & !sync_r;
|
416 |
|
|
|
417 |
|
|
task do_sync;
|
418 |
|
|
begin
|
419 |
|
|
// Wait for us to not be doing a transaction.
|
420 |
|
|
while(wb_req)
|
421 |
|
|
@wb_clk;
|
422 |
|
|
// Cache not busy, initiate sync.
|
423 |
|
|
sync = 1;
|
424 |
|
|
end
|
425 |
|
|
endtask // do_sync
|
426 |
|
|
|
427 |
|
|
// Wishbone side of cache enable. Always enabled unless doing DDR2-side
|
428 |
|
|
// things (fill or writeback).
|
429 |
|
|
assign wb_cache_en = !(do_readfrom | do_writeback);
|
430 |
|
|
|
431 |
|
|
// Writeback detect logic
|
432 |
|
|
always @(posedge wb_clk)
|
433 |
|
|
if (wb_rst)
|
434 |
|
|
do_writeback <= 0;
|
435 |
|
|
else if (wb_writeback_done)
|
436 |
|
|
do_writeback <= 0;
|
437 |
|
|
else if (start_writeback)
|
438 |
|
|
do_writeback <= 1;
|
439 |
|
|
|
440 |
|
|
// DDR2 Read detect logic
|
441 |
|
|
always @(posedge wb_clk)
|
442 |
|
|
if (wb_rst)
|
443 |
|
|
do_readfrom <= 0;
|
444 |
|
|
else if (wb_readfrom_done)
|
445 |
|
|
do_readfrom <= 0;
|
446 |
|
|
else if (start_fill)
|
447 |
|
|
do_readfrom <= 1;
|
448 |
|
|
|
449 |
|
|
// Domain cross of WB control signals
|
450 |
|
|
always @(negedge ddr2_if_clk) begin
|
451 |
|
|
do_writeback_ddr2_sync <= do_writeback;
|
452 |
|
|
do_writeback_ddr2_sync2 <= do_writeback_ddr2_sync;
|
453 |
|
|
do_writeback_ddr2_sync3 <= do_writeback_ddr2_sync2;
|
454 |
|
|
do_readfrom_ddr2_sync <= do_readfrom;
|
455 |
|
|
do_readfrom_ddr2_sync2 <= do_readfrom_ddr2_sync;
|
456 |
|
|
do_readfrom_ddr2_sync3 <= do_readfrom_ddr2_sync2;
|
457 |
|
|
end
|
458 |
|
|
|
459 |
|
|
assign do_readfrom_start_ddr2 = !do_readfrom_ddr2_sync3 &
|
460 |
|
|
do_readfrom_ddr2_sync2;
|
461 |
|
|
|
462 |
|
|
assign do_writeback_start_ddr2 = !do_writeback_ddr2_sync3 &
|
463 |
|
|
do_writeback_ddr2_sync2;
|
464 |
|
|
|
465 |
|
|
always @(negedge ddr2_if_clk)
|
466 |
|
|
if (ddr2_rst)
|
467 |
|
|
ddr2_readfrom_done <= 0;
|
468 |
|
|
else if (ddr2_readfrom_done_wb_sync2)
|
469 |
|
|
ddr2_readfrom_done <= 0;
|
470 |
|
|
else if (ddr2_read_state_shr[6])
|
471 |
|
|
ddr2_readfrom_done <= 1;
|
472 |
|
|
|
473 |
|
|
// Get readfrom_done into Wishbone domain
|
474 |
|
|
always @(posedge wb_clk)
|
475 |
|
|
if (wb_rst) begin
|
476 |
|
|
wb_readfrom_done_sync <= 0;
|
477 |
|
|
wb_readfrom_done_sync2 <= 0;
|
478 |
|
|
end
|
479 |
|
|
else begin
|
480 |
|
|
wb_readfrom_done_sync <= ddr2_readfrom_done;
|
481 |
|
|
wb_readfrom_done_sync2 <= wb_readfrom_done_sync;
|
482 |
|
|
end
|
483 |
|
|
|
484 |
|
|
assign wb_readfrom_done = !wb_readfrom_done_sync2 & wb_readfrom_done_sync;
|
485 |
|
|
|
486 |
|
|
// Read this back to DDR2 domain
|
487 |
|
|
always @(negedge ddr2_if_clk)
|
488 |
|
|
if (ddr2_rst) begin
|
489 |
|
|
ddr2_readfrom_done_wb_sync <= 0;
|
490 |
|
|
ddr2_readfrom_done_wb_sync2 <= 0;
|
491 |
|
|
end
|
492 |
|
|
else begin
|
493 |
|
|
ddr2_readfrom_done_wb_sync <= wb_readfrom_done_sync2;
|
494 |
|
|
ddr2_readfrom_done_wb_sync2 <= ddr2_readfrom_done_wb_sync;
|
495 |
|
|
end
|
496 |
|
|
|
497 |
|
|
|
498 |
|
|
// Writeback finished/done logic
|
499 |
|
|
always @(negedge ddr2_if_clk)
|
500 |
|
|
if (ddr2_rst)
|
501 |
|
|
ddr2_writeback_done <= 0;
|
502 |
|
|
else if (ddr2_writeback_done_wb_sync2)
|
503 |
|
|
ddr2_writeback_done <= 0;
|
504 |
|
|
else if (ddr2_write_state_shr[6])
|
505 |
|
|
ddr2_writeback_done <= 1;
|
506 |
|
|
|
507 |
|
|
// Get writeback_done into Wishbone domain
|
508 |
|
|
always @(posedge wb_clk)
|
509 |
|
|
if (wb_rst) begin
|
510 |
|
|
wb_writeback_done_sync <= 0;
|
511 |
|
|
wb_writeback_done_sync2 <= 0;
|
512 |
|
|
end
|
513 |
|
|
else begin
|
514 |
|
|
wb_writeback_done_sync <= ddr2_writeback_done;
|
515 |
|
|
wb_writeback_done_sync2 <= wb_writeback_done_sync;
|
516 |
|
|
end
|
517 |
|
|
|
518 |
|
|
assign wb_writeback_done = !wb_writeback_done_sync2 & wb_writeback_done_sync;
|
519 |
|
|
|
520 |
|
|
// Read this back to DDR2 domain
|
521 |
|
|
always @(negedge ddr2_if_clk)
|
522 |
|
|
if (ddr2_rst) begin
|
523 |
|
|
ddr2_writeback_done_wb_sync <= 0;
|
524 |
|
|
ddr2_writeback_done_wb_sync2 <= 0;
|
525 |
|
|
end
|
526 |
|
|
else begin
|
527 |
|
|
ddr2_writeback_done_wb_sync <= wb_writeback_done_sync2;
|
528 |
|
|
ddr2_writeback_done_wb_sync2 <= ddr2_writeback_done_wb_sync;
|
529 |
|
|
end
|
530 |
|
|
|
531 |
|
|
|
532 |
|
|
// Have we passed init command to the controller?
|
533 |
|
|
always @(negedge ddr2_if_clk or posedge ddr2_rst_180)
|
534 |
|
|
if (ddr2_rst_180)
|
535 |
|
|
ddr2_if_init_sent <= 0;
|
536 |
|
|
else if (!ddr2_rst & !ddr2_rst_90 & !ddr2_rst_180 & !ddr2_if_init_sent)
|
537 |
|
|
ddr2_if_init_sent <= 1;
|
538 |
|
|
|
539 |
|
|
// MIG Command
|
540 |
|
|
always @(negedge ddr2_if_clk or posedge ddr2_rst_180)
|
541 |
|
|
if (ddr2_rst_180)
|
542 |
|
|
ddr2_if_cmd <= 3'b000;
|
543 |
|
|
else if (!phy_init_done) begin
|
544 |
|
|
if (!ddr2_rst & !ddr2_rst_90 & !ddr2_rst_180 & !ddr2_if_init_sent)
|
545 |
|
|
ddr2_if_cmd <= 3'b010; // Init
|
546 |
|
|
else
|
547 |
|
|
ddr2_if_cmd <= 3'b000; // NOP
|
548 |
|
|
end
|
549 |
|
|
else if ((ddr2_if_cmd==3'b000) & do_writeback_ddr2_sync2 &
|
550 |
|
|
!ddr2_if_cmd_ack & !(|ddr2_write_state_shr[9:1]) & !ddr2_writeback_done)
|
551 |
|
|
ddr2_if_cmd <= 3'b100; // Write
|
552 |
|
|
else if (ddr2_write_state_shr[6]) // End of write
|
553 |
|
|
ddr2_if_cmd <= 3'b000; // NOP
|
554 |
|
|
else if ((ddr2_if_cmd==3'b000) & do_readfrom_ddr2_sync2 &
|
555 |
|
|
!ddr2_if_cmd_ack & !(|ddr2_read_state_shr[6:1]) & !ddr2_readfrom_done)
|
556 |
|
|
ddr2_if_cmd <= 3'b110; // Read
|
557 |
|
|
else if (ddr2_read_state_shr[3])
|
558 |
|
|
ddr2_if_cmd <= 3'b000; // NOP
|
559 |
|
|
|
560 |
|
|
// State shift register
|
561 |
|
|
// [0] - Waiting for ddr2_if_cmd_ack
|
562 |
|
|
// [4:1] - data0-data3 out from RAM
|
563 |
|
|
// [8:5] - ddr2_if_burst_done high for 4 cycles
|
564 |
|
|
// [9] - waiting for ddr2_if_cmd_ack to be deasserted
|
565 |
|
|
always @(negedge ddr2_if_clk or posedge ddr2_rst_180)
|
566 |
|
|
if (ddr2_rst_180)
|
567 |
|
|
ddr2_write_state_shr <= 0;
|
568 |
|
|
else if (!(|ddr2_write_state_shr) & !ddr2_if_cmd_ack &
|
569 |
|
|
do_writeback_start_ddr2 & !ddr2_writeback_done )
|
570 |
|
|
ddr2_write_state_shr <= 1;
|
571 |
|
|
else if (((|ddr2_write_state_shr[8:0]) & ddr2_if_cmd_ack) |
|
572 |
|
|
(ddr2_write_state_shr[9] & !ddr2_if_cmd_ack))
|
573 |
|
|
ddr2_write_state_shr <= {ddr2_write_state_shr[8:0],1'b0};
|
574 |
|
|
|
575 |
|
|
// [0] - waiting for ddr2_if_cmd_ack
|
576 |
|
|
// [4:1] - asserting ddr2_if_burst_done
|
577 |
|
|
// [5] - waiting for ddr2_if_cmd_ack to be deasserted
|
578 |
|
|
// [6] - finish
|
579 |
|
|
always @(negedge ddr2_if_clk or posedge ddr2_rst_180)
|
580 |
|
|
if (ddr2_rst_180)
|
581 |
|
|
ddr2_read_state_shr <= 0;
|
582 |
|
|
else if (!(|ddr2_read_state_shr) & do_readfrom_ddr2_sync2 &
|
583 |
|
|
!ddr2_if_cmd_ack & !ddr2_readfrom_done)
|
584 |
|
|
ddr2_read_state_shr <= 1; // Start read
|
585 |
|
|
else if ((ddr2_read_state_shr[0] & ddr2_if_cmd_ack) |
|
586 |
|
|
(|ddr2_read_state_shr[4:1]) | ddr2_read_state_shr[6] |
|
587 |
|
|
(ddr2_read_state_shr[5] & !ddr2_if_cmd_ack & !(|ddr2_read_dv_counter_shr)))
|
588 |
|
|
ddr2_read_state_shr <= {ddr2_read_state_shr[6:0],1'b0};
|
589 |
|
|
|
590 |
|
|
always @(posedge ddr2_if_clk_90 or posedge ddr2_rst_90)
|
591 |
|
|
if (ddr2_rst_90)
|
592 |
|
|
ddr2_read_dv_counter_shr <= 0;
|
593 |
|
|
else if (do_readfrom_start_ddr2)
|
594 |
|
|
ddr2_read_dv_counter_shr <= 1;
|
595 |
|
|
else if (ddr2_read_dv_counter_shr[4] & !ddr2_if_cmd_ack)
|
596 |
|
|
ddr2_read_dv_counter_shr <= 0;
|
597 |
|
|
else if (ddr2_if_rdata_valid)
|
598 |
|
|
ddr2_read_dv_counter_shr <= {ddr2_read_dv_counter_shr[3:0],1'b0};
|
599 |
|
|
|
600 |
|
|
|
601 |
|
|
assign ddr2_cache_line_word_addr_next = ddr2_cache_line_word_addr_r + 1;
|
602 |
|
|
|
603 |
|
|
always @(posedge ddr2_if_clk_90 or posedge ddr2_rst_90)
|
604 |
|
|
if (ddr2_rst_90)
|
605 |
|
|
ddr2_cache_line_word_addr_r <= 0;
|
606 |
|
|
else if (do_writeback_ddr2_sync2) begin
|
607 |
|
|
// Write
|
608 |
|
|
if (do_writeback_start_ddr2)
|
609 |
|
|
ddr2_cache_line_word_addr_r <= 0;
|
610 |
|
|
else if (ddr2_write_state_shr[4:1])
|
611 |
|
|
ddr2_cache_line_word_addr_r <= ddr2_cache_line_word_addr_next;
|
612 |
|
|
end
|
613 |
|
|
else if (do_readfrom_ddr2_sync2) begin
|
614 |
|
|
// Read
|
615 |
|
|
if (do_readfrom_start_ddr2)
|
616 |
|
|
ddr2_cache_line_word_addr_r <= 0;
|
617 |
|
|
else if (ddr2_if_rdata_valid)
|
618 |
|
|
ddr2_cache_line_word_addr_r <= ddr2_cache_line_word_addr_next;
|
619 |
|
|
end
|
620 |
|
|
|
621 |
|
|
always @(posedge ddr2_if_clk_90 or posedge ddr2_rst_90)
|
622 |
|
|
if (ddr2_rst_90)
|
623 |
|
|
ddr2_cache_line_word_use_next_addr <= 0;
|
624 |
|
|
else
|
625 |
|
|
ddr2_cache_line_word_use_next_addr <= ((|ddr2_write_state_shr) & ddr2_if_cmd_ack);
|
626 |
|
|
|
627 |
|
|
// Cache line word address to DDR2-side of cache. When writing, need address earlier,
|
628 |
|
|
// so use the combinatorial value, otherwise the registered value.
|
629 |
|
|
assign ddr2_cache_line_word_addr = ddr2_cache_line_word_use_next_addr ?
|
630 |
|
|
ddr2_cache_line_word_addr_next :
|
631 |
|
|
ddr2_cache_line_word_addr_r;
|
632 |
|
|
|
633 |
|
|
|
634 |
|
|
assign ddr2_if_burst_done = (|ddr2_write_state_shr[7:4]) |
|
635 |
|
|
(|ddr2_read_state_shr[4:1]);
|
636 |
|
|
|
637 |
|
|
// Lower word address uses potentially bursting address counter
|
638 |
|
|
assign wb_cache_word = wb_bursting ?
|
639 |
|
|
wb_burst_addr :
|
640 |
|
|
wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:2];
|
641 |
|
|
/*
|
642 |
|
|
Here `DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE==3, which is < width of wb_burst_addr
|
643 |
|
|
so just use wb_burst_addr.
|
644 |
|
|
If cache lines get longer, would have to do something like this.
|
645 |
|
|
{wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:6],wb_burst_addr}:
|
646 |
|
|
wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:2];
|
647 |
|
|
*/
|
648 |
|
|
|
649 |
|
|
assign wb_cache_sel_we = {4{wb_we_i & wb_ack_o}} & wb_sel_i;
|
650 |
|
|
assign ddr2_cache_en = (ddr2_if_rdata_valid |do_writeback_ddr2_sync3);
|
651 |
|
|
assign ddr2_cache_we = {8{ddr2_if_rdata_valid}};
|
652 |
|
|
|
653 |
|
|
always @(negedge ddr2_if_clk)
|
654 |
|
|
selected_cache_line_enc_ddr2_if_clk <= selected_cache_line_enc;
|
655 |
|
|
|
656 |
|
|
always @(negedge ddr2_if_clk)
|
657 |
|
|
if (do_writeback_start_ddr2)
|
658 |
|
|
ddr2_if_input_address <= {cache_line_tag,3'd0};
|
659 |
|
|
else if (do_readfrom_start_ddr2)
|
660 |
|
|
ddr2_if_input_address <= {wb_adr_i[`DDR2_CACHE_TAG_BITS],3'd0};
|
661 |
|
|
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
// True dual port ram, with dual aspect ration
|
665 |
|
|
// Wishbone side - 32-bit, DDR2 side - 64-bit
|
666 |
|
|
// Is 4KByte, so WB addr is 10-bits, DDR2 9-bits
|
667 |
|
|
s3adsp_ddr2_cache s3adsp_ddr2_cache
|
668 |
|
|
(
|
669 |
|
|
// A-side - Wishbone
|
670 |
|
|
.clka(wb_clk),
|
671 |
|
|
.ena(wb_cache_en),
|
672 |
|
|
.wea(({4{wb_cache_en}}&wb_cache_sel_we)),
|
673 |
|
|
.addra({5'd0,selected_cache_line_enc,wb_cache_word}),
|
674 |
|
|
.dina(wb_dat_i),
|
675 |
|
|
.douta(wb_dat_o),
|
676 |
|
|
|
677 |
|
|
// B-side - DDR2
|
678 |
|
|
.clkb(ddr2_if_clk_90),
|
679 |
|
|
.enb(ddr2_cache_en),
|
680 |
|
|
.web(ddr2_cache_we),
|
681 |
|
|
.addrb({5'd0,selected_cache_line_enc_ddr2_if_clk,ddr2_cache_line_word_addr}),
|
682 |
|
|
.dinb(ddr2_if_rdata),
|
683 |
|
|
.doutb(ddr2_if_wdata)
|
684 |
|
|
);
|
685 |
|
|
|
686 |
|
|
s3adsp_ddr2 s3adsp_ddr2
|
687 |
|
|
(
|
688 |
|
|
.cntrl0_ddr2_dq(ddr2_dq),
|
689 |
|
|
.cntrl0_ddr2_a(ddr2_a),
|
690 |
|
|
.cntrl0_ddr2_ba(ddr2_ba),
|
691 |
|
|
.cntrl0_ddr2_cke(ddr2_cke),
|
692 |
|
|
.cntrl0_ddr2_cs_n(ddr2_cs_n),
|
693 |
|
|
.cntrl0_ddr2_ras_n(ddr2_ras_n),
|
694 |
|
|
.cntrl0_ddr2_cas_n(ddr2_cas_n),
|
695 |
|
|
.cntrl0_ddr2_we_n(ddr2_we_n),
|
696 |
|
|
.cntrl0_ddr2_odt(ddr2_odt),
|
697 |
|
|
.cntrl0_ddr2_dm(ddr2_dm),
|
698 |
|
|
.cntrl0_rst_dqs_div_in(ddr2_rst_dqs_div_in),
|
699 |
|
|
.cntrl0_rst_dqs_div_out(ddr2_rst_dqs_div_out),
|
700 |
|
|
.sys_clk_in(clk133_i),
|
701 |
|
|
.reset_in_n(~wb_rst),
|
702 |
|
|
|
703 |
|
|
.cntrl0_burst_done(ddr2_if_burst_done),
|
704 |
|
|
.cntrl0_init_done(phy_init_done),
|
705 |
|
|
.cntrl0_ar_done(ddr2_if_af_done),
|
706 |
|
|
.cntrl0_user_data_valid(ddr2_if_rdata_valid),
|
707 |
|
|
|
708 |
|
|
.cntrl0_auto_ref_req(ddr2_if_ref_req),
|
709 |
|
|
.cntrl0_user_cmd_ack(ddr2_if_cmd_ack),
|
710 |
|
|
.cntrl0_user_command_register(ddr2_if_cmd),
|
711 |
|
|
|
712 |
|
|
.cntrl0_clk_tb(ddr2_if_clk),
|
713 |
|
|
.cntrl0_clk90_tb(ddr2_if_clk_90),
|
714 |
|
|
|
715 |
|
|
.cntrl0_sys_rst_tb(ddr2_rst),
|
716 |
|
|
.cntrl0_sys_rst90_tb(ddr2_rst_90),
|
717 |
|
|
.cntrl0_sys_rst180_tb(ddr2_rst_180),
|
718 |
|
|
|
719 |
|
|
.cntrl0_user_data_mask(8'h00),
|
720 |
|
|
.cntrl0_user_output_data(ddr2_if_rdata),
|
721 |
|
|
.cntrl0_user_input_data(ddr2_if_wdata),
|
722 |
|
|
.cntrl0_user_input_address({ddr2_if_input_address[22:0],
|
723 |
|
|
ddr2_if_input_address[24:23]}),
|
724 |
|
|
.cntrl0_ddr2_dqs(ddr2_dqs),
|
725 |
|
|
.cntrl0_ddr2_dqs_n(ddr2_dqs_n),
|
726 |
|
|
.cntrl0_ddr2_ck(ddr2_ck),
|
727 |
|
|
.cntrl0_ddr2_ck_n(ddr2_ck_n)
|
728 |
|
|
);
|
729 |
|
|
|
730 |
|
|
endmodule // xilinx_s3adsp_ddr2_if2
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
// Local Variables:
|
734 |
|
|
// verilog-library-directories:("." "ddr2_mig")
|
735 |
|
|
// verilog-library-extensions:(".v" ".h")
|
736 |
|
|
// End:
|
737 |
|
|
|
738 |
|
|
|
739 |
|
|
module ddr2_wb_if_cache_addr_reg
|
740 |
|
|
(addr_i, validate, invalidate,
|
741 |
|
|
cached_addr_o, cache_hit, addr_valid,
|
742 |
|
|
clk, rst);
|
743 |
|
|
|
744 |
|
|
parameter full_addr_width = 32;
|
745 |
|
|
parameter word_addr_width = 2; // 4 bytes per word
|
746 |
|
|
parameter line_addr_width = 5; // 32 words per "line"
|
747 |
|
|
|
748 |
|
|
parameter tag_width = full_addr_width - line_addr_width - word_addr_width;
|
749 |
|
|
|
750 |
|
|
|
751 |
|
|
input [full_addr_width-1: word_addr_width + line_addr_width] addr_i;
|
752 |
|
|
input validate;
|
753 |
|
|
input invalidate;
|
754 |
|
|
output [full_addr_width-1: word_addr_width + line_addr_width] cached_addr_o;
|
755 |
|
|
output cache_hit;
|
756 |
|
|
output reg addr_valid;
|
757 |
|
|
|
758 |
|
|
input clk, rst;
|
759 |
|
|
|
760 |
|
|
reg [tag_width-1:0] cached_adr;
|
761 |
|
|
|
762 |
|
|
assign cached_addr_o = cached_adr;
|
763 |
|
|
|
764 |
|
|
always @(posedge clk)
|
765 |
|
|
if (rst)
|
766 |
|
|
cached_adr <= 0;
|
767 |
|
|
else if (validate)
|
768 |
|
|
cached_adr <= addr_i;
|
769 |
|
|
|
770 |
|
|
always @(posedge clk)
|
771 |
|
|
if (rst)
|
772 |
|
|
addr_valid <= 0;
|
773 |
|
|
else if (validate)
|
774 |
|
|
addr_valid <= 1;
|
775 |
|
|
else if (invalidate)
|
776 |
|
|
addr_valid <= 0;
|
777 |
|
|
|
778 |
|
|
assign cache_hit = (addr_i == cached_adr);
|
779 |
|
|
|
780 |
|
|
endmodule // ddr2_wb_if_cache_addr_reg
|
781 |
|
|
|
782 |
|
|
module ddr2_wb_if_cache_control
|
783 |
|
|
( cache_line_addr_valid, cache_line_addr_hit,
|
784 |
|
|
wb_req,
|
785 |
|
|
cache_write,
|
786 |
|
|
writeback_done, fill_done,
|
787 |
|
|
sync_start, sync_done,
|
788 |
|
|
start_writeback, start_fill,
|
789 |
|
|
cache_line_validate, cache_line_invalidate,
|
790 |
|
|
selected_cache_line, selected_cache_line_enc,
|
791 |
|
|
wb_clk, wb_rst);
|
792 |
|
|
|
793 |
|
|
parameter num_lines = 4;
|
794 |
|
|
parameter num_lines_log2 = 2;
|
795 |
|
|
|
796 |
|
|
input [num_lines-1:0] cache_line_addr_valid;
|
797 |
|
|
input [num_lines-1:0] cache_line_addr_hit;
|
798 |
|
|
|
799 |
|
|
input wb_req;
|
800 |
|
|
input cache_write;
|
801 |
|
|
input writeback_done, fill_done;
|
802 |
|
|
|
803 |
|
|
input sync_start;
|
804 |
|
|
output sync_done;
|
805 |
|
|
|
806 |
|
|
output reg start_writeback;
|
807 |
|
|
output reg start_fill;
|
808 |
|
|
output reg [num_lines-1:0] cache_line_validate;
|
809 |
|
|
output reg [num_lines-1:0] cache_line_invalidate;
|
810 |
|
|
|
811 |
|
|
output [num_lines-1:0] selected_cache_line;
|
812 |
|
|
output reg [num_lines_log2-1:0] selected_cache_line_enc;
|
813 |
|
|
|
814 |
|
|
input wb_clk, wb_rst;
|
815 |
|
|
|
816 |
|
|
reg [num_lines-1:0] lines_dirty;
|
817 |
|
|
|
818 |
|
|
reg [num_lines-1:0] selected_cache_line_from_miss;
|
819 |
|
|
|
820 |
|
|
reg selected_cache_line_new;
|
821 |
|
|
|
822 |
|
|
reg invalidate_clean_line;
|
823 |
|
|
|
824 |
|
|
reg [num_lines-1:0] selected_cache_line_r;
|
825 |
|
|
reg [num_lines-1:0] selected_cache_line_r2;
|
826 |
|
|
|
827 |
|
|
reg wb_req_r;
|
828 |
|
|
|
829 |
|
|
wire wb_req_new;
|
830 |
|
|
reg wb_req_new_r;
|
831 |
|
|
|
832 |
|
|
parameter sync_line_check_wait = 4;
|
833 |
|
|
reg [num_lines-1:0] sync_line_counter;
|
834 |
|
|
reg sync_doing;
|
835 |
|
|
reg [sync_line_check_wait-1:0] sync_line_select_wait_counter_shr;
|
836 |
|
|
reg sync_line_done;
|
837 |
|
|
wire sync_writeback_line;
|
838 |
|
|
|
839 |
|
|
|
840 |
|
|
always @(posedge wb_clk)
|
841 |
|
|
wb_req_r <= wb_req;
|
842 |
|
|
|
843 |
|
|
assign wb_req_new = wb_req & !wb_req_r;
|
844 |
|
|
|
845 |
|
|
always @(posedge wb_clk)
|
846 |
|
|
wb_req_new_r <= wb_req_new;
|
847 |
|
|
|
848 |
|
|
// Select a cache line when we miss. Currently is very simple round robin
|
849 |
|
|
// TODO - if clean lines available on miss, evict them before causing a
|
850 |
|
|
// writeback, to save time on cache miss.
|
851 |
|
|
// TOOD - would benefit a lot from a LRU scheme.
|
852 |
|
|
always @(posedge wb_clk)
|
853 |
|
|
if (wb_rst)
|
854 |
|
|
selected_cache_line_from_miss <= 1;
|
855 |
|
|
else if (wb_req_new_r & !(|selected_cache_line_r)) // miss,no line selected
|
856 |
|
|
// Shift select bit one
|
857 |
|
|
selected_cache_line_from_miss
|
858 |
|
|
<= {selected_cache_line_from_miss[num_lines-2:0],
|
859 |
|
|
selected_cache_line_from_miss[num_lines-1]};
|
860 |
|
|
|
861 |
|
|
|
862 |
|
|
// Line selection logic, when line address is valid and hit, we select
|
863 |
|
|
always @(posedge wb_clk)
|
864 |
|
|
if (wb_rst)
|
865 |
|
|
selected_cache_line_r <= 0;
|
866 |
|
|
else if (wb_req_new)
|
867 |
|
|
selected_cache_line_r <= cache_line_addr_valid & cache_line_addr_hit;
|
868 |
|
|
else if (wb_req_new_r & !(|selected_cache_line_r))
|
869 |
|
|
selected_cache_line_r <= selected_cache_line_from_miss;
|
870 |
|
|
else if (sync_doing)
|
871 |
|
|
selected_cache_line_r <= sync_line_counter;
|
872 |
|
|
|
873 |
|
|
always @(posedge wb_clk)
|
874 |
|
|
selected_cache_line_r2 <= selected_cache_line_r;
|
875 |
|
|
|
876 |
|
|
assign selected_cache_line = selected_cache_line_r2;
|
877 |
|
|
|
878 |
|
|
// A new line of cache has been selected
|
879 |
|
|
always @(posedge wb_clk)
|
880 |
|
|
if (wb_rst)
|
881 |
|
|
selected_cache_line_new <= 0;
|
882 |
|
|
else if (wb_req_new & (&(cache_line_addr_valid & cache_line_addr_hit)))
|
883 |
|
|
// New line address selected
|
884 |
|
|
selected_cache_line_new <= 1;
|
885 |
|
|
else if ((!selected_cache_line_new) & wb_req_new_r)
|
886 |
|
|
// Didn't select one last time, so we must have forced ourselves to
|
887 |
|
|
// select a new one
|
888 |
|
|
selected_cache_line_new <= 1;
|
889 |
|
|
else if (selected_cache_line_new)
|
890 |
|
|
selected_cache_line_new <= 0;
|
891 |
|
|
|
892 |
|
|
always @(posedge wb_clk)
|
893 |
|
|
if (wb_rst)
|
894 |
|
|
lines_dirty <= 0;
|
895 |
|
|
else if (cache_write)
|
896 |
|
|
lines_dirty <= lines_dirty | selected_cache_line_r;
|
897 |
|
|
else if (writeback_done)
|
898 |
|
|
lines_dirty <= lines_dirty & ~(selected_cache_line_r);
|
899 |
|
|
|
900 |
|
|
// Validate the cache line address in the register when line filled
|
901 |
|
|
always @(posedge wb_clk)
|
902 |
|
|
if (wb_rst)
|
903 |
|
|
cache_line_validate <= 0;
|
904 |
|
|
else if (fill_done)
|
905 |
|
|
cache_line_validate <= selected_cache_line_r;
|
906 |
|
|
else if (|cache_line_validate)
|
907 |
|
|
cache_line_validate <= 0;
|
908 |
|
|
|
909 |
|
|
// Invalidate the cache line address in the register when line written back
|
910 |
|
|
always @(posedge wb_clk)
|
911 |
|
|
if (wb_rst)
|
912 |
|
|
cache_line_invalidate <= 0;
|
913 |
|
|
else if ((writeback_done & !sync_doing) | invalidate_clean_line)
|
914 |
|
|
cache_line_invalidate <= selected_cache_line_r;
|
915 |
|
|
else if (|cache_line_invalidate)
|
916 |
|
|
cache_line_invalidate <= 0;
|
917 |
|
|
|
918 |
|
|
// Initiate-writeback logic
|
919 |
|
|
always @(posedge wb_clk)
|
920 |
|
|
if (wb_rst)
|
921 |
|
|
start_writeback <= 0;
|
922 |
|
|
else if (start_writeback)
|
923 |
|
|
start_writeback <= 0;
|
924 |
|
|
else if (selected_cache_line_new &
|
925 |
|
|
(|(lines_dirty & selected_cache_line_r)) &
|
926 |
|
|
(|(selected_cache_line_r & cache_line_addr_valid)) &
|
927 |
|
|
!(|(cache_line_addr_hit & selected_cache_line_r)))
|
928 |
|
|
start_writeback <= 1;
|
929 |
|
|
else if (sync_writeback_line)
|
930 |
|
|
start_writeback <= 1;
|
931 |
|
|
|
932 |
|
|
// Invalidate lines which we haven't written to so we can fill them
|
933 |
|
|
always @(posedge wb_clk)
|
934 |
|
|
if (wb_rst)
|
935 |
|
|
invalidate_clean_line <= 0;
|
936 |
|
|
else if (invalidate_clean_line)
|
937 |
|
|
invalidate_clean_line <= 0;
|
938 |
|
|
else if ((selected_cache_line_new) & // New line selected
|
939 |
|
|
!(|(lines_dirty & selected_cache_line_r)) & // It's not dirty
|
940 |
|
|
// It's valid, but we've selected it so we're trashing it
|
941 |
|
|
(|(selected_cache_line_r & cache_line_addr_valid)) &
|
942 |
|
|
!(|(cache_line_addr_hit & selected_cache_line_r))) // Not a hit
|
943 |
|
|
invalidate_clean_line <= 1;
|
944 |
|
|
|
945 |
|
|
reg invalidate_clean_line_r;
|
946 |
|
|
always @(posedge wb_clk)
|
947 |
|
|
invalidate_clean_line_r <= invalidate_clean_line;
|
948 |
|
|
|
949 |
|
|
|
950 |
|
|
// Initiate-fill logic
|
951 |
|
|
always @(posedge wb_clk)
|
952 |
|
|
if (wb_rst)
|
953 |
|
|
start_fill <= 0;
|
954 |
|
|
else if (((selected_cache_line_new) & // New line selected
|
955 |
|
|
// not valid
|
956 |
|
|
!(|(cache_line_addr_valid & selected_cache_line_r))) |
|
957 |
|
|
(writeback_done & !sync_doing) | invalidate_clean_line_r
|
958 |
|
|
)
|
959 |
|
|
start_fill <= 1;
|
960 |
|
|
else if (start_fill)
|
961 |
|
|
start_fill <= 0;
|
962 |
|
|
|
963 |
|
|
// Hardcoded to 4 lines currently.
|
964 |
|
|
always @(posedge wb_clk)
|
965 |
|
|
if (selected_cache_line_r[0])
|
966 |
|
|
selected_cache_line_enc <= 0;
|
967 |
|
|
else if (selected_cache_line_r[1])
|
968 |
|
|
selected_cache_line_enc <= 1;
|
969 |
|
|
else if (selected_cache_line_r[2])
|
970 |
|
|
selected_cache_line_enc <= 2;
|
971 |
|
|
else if (selected_cache_line_r[3])
|
972 |
|
|
selected_cache_line_enc <= 3;
|
973 |
|
|
/*
|
974 |
|
|
else if (selected_cache_line_r[4])
|
975 |
|
|
selected_cache_line_enc <= 4;
|
976 |
|
|
else if (selected_cache_line_r[5])
|
977 |
|
|
selected_cache_line_enc <= 5;
|
978 |
|
|
else if (selected_cache_line_r[6])
|
979 |
|
|
selected_cache_line_enc <= 6;
|
980 |
|
|
else if (selected_cache_line_r[7])
|
981 |
|
|
selected_cache_line_enc <= 7;
|
982 |
|
|
else if (selected_cache_line_r[8])
|
983 |
|
|
selected_cache_line_enc <= 8;
|
984 |
|
|
else if (selected_cache_line_r[9])
|
985 |
|
|
selected_cache_line_enc <= 9;
|
986 |
|
|
else if (selected_cache_line_r[10])
|
987 |
|
|
selected_cache_line_enc <= 10;
|
988 |
|
|
else if (selected_cache_line_r[11])
|
989 |
|
|
selected_cache_line_enc <= 11;
|
990 |
|
|
else if (selected_cache_line_r[12])
|
991 |
|
|
selected_cache_line_enc <= 12;
|
992 |
|
|
else if (selected_cache_line_r[13])
|
993 |
|
|
selected_cache_line_enc <= 13;
|
994 |
|
|
else if (selected_cache_line_r[14])
|
995 |
|
|
selected_cache_line_enc <= 14;
|
996 |
|
|
else if (selected_cache_line_r[15])
|
997 |
|
|
selected_cache_line_enc <= 15;
|
998 |
|
|
else if (selected_cache_line_r[16])
|
999 |
|
|
selected_cache_line_enc <= 16;
|
1000 |
|
|
else if (selected_cache_line_r[17])
|
1001 |
|
|
selected_cache_line_enc <= 17;
|
1002 |
|
|
else if (selected_cache_line_r[18])
|
1003 |
|
|
selected_cache_line_enc <= 18;
|
1004 |
|
|
else if (selected_cache_line_r[19])
|
1005 |
|
|
selected_cache_line_enc <= 19;
|
1006 |
|
|
else if (selected_cache_line_r[20])
|
1007 |
|
|
selected_cache_line_enc <= 20;
|
1008 |
|
|
else if (selected_cache_line_r[21])
|
1009 |
|
|
selected_cache_line_enc <= 21;
|
1010 |
|
|
else if (selected_cache_line_r[22])
|
1011 |
|
|
selected_cache_line_enc <= 22;
|
1012 |
|
|
else if (selected_cache_line_r[23])
|
1013 |
|
|
selected_cache_line_enc <= 23;
|
1014 |
|
|
else if (selected_cache_line_r[24])
|
1015 |
|
|
selected_cache_line_enc <= 24;
|
1016 |
|
|
else if (selected_cache_line_r[25])
|
1017 |
|
|
selected_cache_line_enc <= 25;
|
1018 |
|
|
else if (selected_cache_line_r[26])
|
1019 |
|
|
selected_cache_line_enc <= 26;
|
1020 |
|
|
else if (selected_cache_line_r[27])
|
1021 |
|
|
selected_cache_line_enc <= 27;
|
1022 |
|
|
else if (selected_cache_line_r[28])
|
1023 |
|
|
selected_cache_line_enc <= 28;
|
1024 |
|
|
else if (selected_cache_line_r[29])
|
1025 |
|
|
selected_cache_line_enc <= 29;
|
1026 |
|
|
else if (selected_cache_line_r[30])
|
1027 |
|
|
selected_cache_line_enc <= 30;
|
1028 |
|
|
else if (selected_cache_line_r[31])
|
1029 |
|
|
selected_cache_line_enc <= 31;
|
1030 |
|
|
*/
|
1031 |
|
|
// Synchronisation control
|
1032 |
|
|
|
1033 |
|
|
always @(posedge wb_clk)
|
1034 |
|
|
if (wb_rst)
|
1035 |
|
|
sync_doing <= 0;
|
1036 |
|
|
else if (sync_start)
|
1037 |
|
|
sync_doing <= 1;
|
1038 |
|
|
else if (sync_done)
|
1039 |
|
|
sync_doing <= 0;
|
1040 |
|
|
|
1041 |
|
|
always @(posedge wb_clk)
|
1042 |
|
|
if (wb_rst)
|
1043 |
|
|
sync_line_counter <= 0;
|
1044 |
|
|
else if (sync_start)
|
1045 |
|
|
// Set first line to check
|
1046 |
|
|
sync_line_counter[0] <= 1'b1;
|
1047 |
|
|
else if (sync_line_done)
|
1048 |
|
|
// Shift along, check next line
|
1049 |
|
|
sync_line_counter <= {sync_line_counter[num_lines-2:0], 1'b0};
|
1050 |
|
|
|
1051 |
|
|
// Pulse this on finishing of checking lines
|
1052 |
|
|
assign sync_done = sync_line_counter[num_lines-1] & sync_line_done;
|
1053 |
|
|
|
1054 |
|
|
// Pulses when a dirty line is detected and should be written back.
|
1055 |
|
|
assign sync_writeback_line = sync_doing &
|
1056 |
|
|
sync_line_select_wait_counter_shr[0] &
|
1057 |
|
|
cache_line_addr_valid &
|
1058 |
|
|
|(sync_line_counter & lines_dirty);
|
1059 |
|
|
|
1060 |
|
|
always @(posedge wb_clk)
|
1061 |
|
|
if (wb_rst)
|
1062 |
|
|
sync_line_select_wait_counter_shr <= 0;
|
1063 |
|
|
else if (|sync_line_select_wait_counter_shr)
|
1064 |
|
|
sync_line_select_wait_counter_shr
|
1065 |
|
|
<= {1'b0,sync_line_select_wait_counter_shr[sync_line_check_wait-1:1]};
|
1066 |
|
|
else if (sync_start | (sync_line_done & !sync_done))
|
1067 |
|
|
sync_line_select_wait_counter_shr[sync_line_check_wait-1] <= 1'b1;
|
1068 |
|
|
|
1069 |
|
|
always @(posedge wb_clk)
|
1070 |
|
|
if (wb_rst)
|
1071 |
|
|
sync_line_done <= 1'b0;
|
1072 |
|
|
else if (sync_line_done)
|
1073 |
|
|
sync_line_done <= 1'b0;
|
1074 |
|
|
// Either line doesn't need writeback
|
1075 |
|
|
else if (sync_line_select_wait_counter_shr[0] & !sync_writeback_line)
|
1076 |
|
|
sync_line_done <= 1'b1;
|
1077 |
|
|
// Or writeback finished
|
1078 |
|
|
else if (writeback_done & sync_doing)
|
1079 |
|
|
sync_line_done <= 1'b1;
|
1080 |
|
|
|
1081 |
|
|
|
1082 |
|
|
endmodule // ddr2_wb_if_cache_control
|