Sun, 04 Apr 2010 20:52:32 +0100
Disable Lattice-specific stuff by default
To build on Lattice platforms, `define PLATFORM_LATTICE in lm32_include.v.
Otherwise, non-optimal "platform independent" HDL will be used.
This means LM32 can now be built for non-Lattice FPGAs.
1 // =============================================================================
2 // COPYRIGHT NOTICE
3 // Copyright 2006 (c) Lattice Semiconductor Corporation
4 // ALL RIGHTS RESERVED
5 // This confidential and proprietary software may be used only as authorised by
6 // a licensing agreement from Lattice Semiconductor Corporation.
7 // The entire notice above must be reproduced on all authorized copies and
8 // copies may only be made to the extent permitted by a licensing agreement from
9 // Lattice Semiconductor Corporation.
10 //
11 // Lattice Semiconductor Corporation TEL : 1-800-Lattice (USA and Canada)
12 // 5555 NE Moore Court 408-826-6000 (other locations)
13 // Hillsboro, OR 97124 web : http://www.latticesemi.com/
14 // U.S.A email: techsupport@latticesemi.com
15 // =============================================================================/
16 // FILE DETAILS
17 // Project : LatticeMico32
18 // File : lm32_icache.v
19 // Title : Instruction cache
20 // Dependencies : lm32_include.v
21 //
22 // Version 3.5
23 // 1. Bug Fix: Instruction cache flushes issued from Instruction Inline Memory
24 // cause segmentation fault due to incorrect fetches.
25 //
26 // Version 3.1
27 // 1. Feature: Support for user-selected resource usage when implementing
28 // cache memory. Additional parameters must be defined when invoking module
29 // lm32_ram. Instruction cache miss mechanism is dependent on branch
30 // prediction being performed in D stage of pipeline.
31 //
32 // Version 7.0SP2, 3.0
33 // No change
34 // =============================================================================
36 `include "lm32_include.v"
38 `ifdef CFG_ICACHE_ENABLED
40 `define LM32_IC_ADDR_OFFSET_RNG addr_offset_msb:addr_offset_lsb
41 `define LM32_IC_ADDR_SET_RNG addr_set_msb:addr_set_lsb
42 `define LM32_IC_ADDR_TAG_RNG addr_tag_msb:addr_tag_lsb
43 `define LM32_IC_ADDR_IDX_RNG addr_set_msb:addr_offset_lsb
45 `define LM32_IC_TMEM_ADDR_WIDTH addr_set_width
46 `define LM32_IC_TMEM_ADDR_RNG (`LM32_IC_TMEM_ADDR_WIDTH-1):0
47 `define LM32_IC_DMEM_ADDR_WIDTH (addr_offset_width+addr_set_width)
48 `define LM32_IC_DMEM_ADDR_RNG (`LM32_IC_DMEM_ADDR_WIDTH-1):0
50 `define LM32_IC_TAGS_WIDTH (addr_tag_width+1)
51 `define LM32_IC_TAGS_RNG (`LM32_IC_TAGS_WIDTH-1):0
52 `define LM32_IC_TAGS_TAG_RNG (`LM32_IC_TAGS_WIDTH-1):1
53 `define LM32_IC_TAGS_VALID_RNG 0
55 `define LM32_IC_STATE_RNG 3:0
56 `define LM32_IC_STATE_FLUSH_INIT 4'b0001
57 `define LM32_IC_STATE_FLUSH 4'b0010
58 `define LM32_IC_STATE_CHECK 4'b0100
59 `define LM32_IC_STATE_REFILL 4'b1000
61 /////////////////////////////////////////////////////
62 // Module interface
63 /////////////////////////////////////////////////////
65 module lm32_icache (
66 // ----- Inputs -----
67 clk_i,
68 rst_i,
69 stall_a,
70 stall_f,
71 address_a,
72 address_f,
73 read_enable_f,
74 refill_ready,
75 refill_data,
76 iflush,
77 `ifdef CFG_IROM_ENABLED
78 select_f,
79 `endif
80 valid_d,
81 branch_predict_taken_d,
82 // ----- Outputs -----
83 stall_request,
84 restart_request,
85 refill_request,
86 refill_address,
87 refilling,
88 inst
89 );
91 /////////////////////////////////////////////////////
92 // Parameters
93 /////////////////////////////////////////////////////
95 parameter associativity = 1; // Associativity of the cache (Number of ways)
96 parameter sets = 512; // Number of sets
97 parameter bytes_per_line = 16; // Number of bytes per cache line
98 parameter base_address = 0; // Base address of cachable memory
99 parameter limit = 0; // Limit (highest address) of cachable memory
101 localparam addr_offset_width = clogb2(bytes_per_line)-1-2;
102 localparam addr_set_width = clogb2(sets)-1;
103 localparam addr_offset_lsb = 2;
104 localparam addr_offset_msb = (addr_offset_lsb+addr_offset_width-1);
105 localparam addr_set_lsb = (addr_offset_msb+1);
106 localparam addr_set_msb = (addr_set_lsb+addr_set_width-1);
107 localparam addr_tag_lsb = (addr_set_msb+1);
108 localparam addr_tag_msb = clogb2(`CFG_ICACHE_LIMIT-`CFG_ICACHE_BASE_ADDRESS)-1;
109 localparam addr_tag_width = (addr_tag_msb-addr_tag_lsb+1);
111 /////////////////////////////////////////////////////
112 // Inputs
113 /////////////////////////////////////////////////////
115 input clk_i; // Clock
116 input rst_i; // Reset
118 input stall_a; // Stall instruction in A stage
119 input stall_f; // Stall instruction in F stage
121 input valid_d; // Valid instruction in D stage
122 input branch_predict_taken_d; // Instruction in D stage is a branch and is predicted taken
124 input [`LM32_PC_RNG] address_a; // Address of instruction in A stage
125 input [`LM32_PC_RNG] address_f; // Address of instruction in F stage
126 input read_enable_f; // Indicates if cache access is valid
128 input refill_ready; // Next word of refill data is ready
129 input [`LM32_INSTRUCTION_RNG] refill_data; // Data to refill the cache with
131 input iflush; // Flush the cache
132 `ifdef CFG_IROM_ENABLED
133 input select_f; // Instruction in F stage is mapped through instruction cache
134 `endif
136 /////////////////////////////////////////////////////
137 // Outputs
138 /////////////////////////////////////////////////////
140 output stall_request; // Request to stall the pipeline
141 wire stall_request;
142 output restart_request; // Request to restart instruction that caused the cache miss
143 reg restart_request;
144 output refill_request; // Request to refill a cache line
145 wire refill_request;
146 output [`LM32_PC_RNG] refill_address; // Base address of cache refill
147 reg [`LM32_PC_RNG] refill_address;
148 output refilling; // Indicates the instruction cache is currently refilling
149 reg refilling;
150 output [`LM32_INSTRUCTION_RNG] inst; // Instruction read from cache
151 wire [`LM32_INSTRUCTION_RNG] inst;
153 /////////////////////////////////////////////////////
154 // Internal nets and registers
155 /////////////////////////////////////////////////////
157 wire enable;
158 wire [0:associativity-1] way_mem_we;
159 wire [`LM32_INSTRUCTION_RNG] way_data[0:associativity-1];
160 wire [`LM32_IC_TAGS_TAG_RNG] way_tag[0:associativity-1];
161 wire [0:associativity-1] way_valid;
162 wire [0:associativity-1] way_match;
163 wire miss;
165 wire [`LM32_IC_TMEM_ADDR_RNG] tmem_read_address;
166 wire [`LM32_IC_TMEM_ADDR_RNG] tmem_write_address;
167 wire [`LM32_IC_DMEM_ADDR_RNG] dmem_read_address;
168 wire [`LM32_IC_DMEM_ADDR_RNG] dmem_write_address;
169 wire [`LM32_IC_TAGS_RNG] tmem_write_data;
171 reg [`LM32_IC_STATE_RNG] state;
172 wire flushing;
173 wire check;
174 wire refill;
176 reg [associativity-1:0] refill_way_select;
177 reg [`LM32_IC_ADDR_OFFSET_RNG] refill_offset;
178 wire last_refill;
179 reg [`LM32_IC_TMEM_ADDR_RNG] flush_set;
181 genvar i;
183 /////////////////////////////////////////////////////
184 // Functions
185 /////////////////////////////////////////////////////
187 `include "lm32_functions.v"
189 /////////////////////////////////////////////////////
190 // Instantiations
191 /////////////////////////////////////////////////////
193 generate
194 for (i = 0; i < associativity; i = i + 1)
195 begin : memories
197 lm32_ram
198 #(
199 // ----- Parameters -------
200 .data_width (32),
201 .address_width (`LM32_IC_DMEM_ADDR_WIDTH),
202 `ifdef PLATFORM_LATTICE
203 `ifdef CFG_ICACHE_DAT_USE_DP_TRUE
204 .RAM_IMPLEMENTATION ("EBR"),
205 .RAM_TYPE ("RAM_DP_TRUE")
206 `else
207 `ifdef CFG_ICACHE_DAT_USE_DP
208 .RAM_IMPLEMENTATION ("EBR"),
209 .RAM_TYPE ("RAM_DP")
210 `else
211 `ifdef CFG_ICACHE_DAT_USE_SLICE
212 .RAM_IMPLEMENTATION ("SLICE")
213 `else
214 .RAM_IMPLEMENTATION ("AUTO")
215 `endif
216 `endif
217 `endif
218 `endif
219 )
220 way_0_data_ram
221 (
222 // ----- Inputs -------
223 .read_clk (clk_i),
224 .write_clk (clk_i),
225 .reset (rst_i),
226 .read_address (dmem_read_address),
227 .enable_read (enable),
228 .write_address (dmem_write_address),
229 .enable_write (`TRUE),
230 .write_enable (way_mem_we[i]),
231 .write_data (refill_data),
232 // ----- Outputs -------
233 .read_data (way_data[i])
234 );
236 lm32_ram
237 #(
238 // ----- Parameters -------
239 .data_width (`LM32_IC_TAGS_WIDTH),
240 .address_width (`LM32_IC_TMEM_ADDR_WIDTH),
241 `ifdef CFG_ICACHE_DAT_USE_DP_TRUE
242 .RAM_IMPLEMENTATION ("EBR"),
243 .RAM_TYPE ("RAM_DP_TRUE")
244 `else
245 `ifdef CFG_ICACHE_DAT_USE_DP
246 .RAM_IMPLEMENTATION ("EBR"),
247 .RAM_TYPE ("RAM_DP")
248 `else
249 `ifdef CFG_ICACHE_DAT_USE_SLICE
250 .RAM_IMPLEMENTATION ("SLICE")
251 `else
252 .RAM_IMPLEMENTATION ("AUTO")
253 `endif
254 `endif
255 `endif
256 )
257 way_0_tag_ram
258 (
259 // ----- Inputs -------
260 .read_clk (clk_i),
261 .write_clk (clk_i),
262 .reset (rst_i),
263 .read_address (tmem_read_address),
264 .enable_read (enable),
265 .write_address (tmem_write_address),
266 .enable_write (`TRUE),
267 .write_enable (way_mem_we[i] | flushing),
268 .write_data (tmem_write_data),
269 // ----- Outputs -------
270 .read_data ({way_tag[i], way_valid[i]})
271 );
273 end
274 endgenerate
276 /////////////////////////////////////////////////////
277 // Combinational logic
278 /////////////////////////////////////////////////////
280 // Compute which ways in the cache match the address address being read
281 generate
282 for (i = 0; i < associativity; i = i + 1)
283 begin : match
284 assign way_match[i] = ({way_tag[i], way_valid[i]} == {address_f[`LM32_IC_ADDR_TAG_RNG], `TRUE});
285 end
286 endgenerate
288 // Select data from way that matched the address being read
289 generate
290 if (associativity == 1)
291 begin : inst_1
292 assign inst = way_match[0] ? way_data[0] : 32'b0;
293 end
294 else if (associativity == 2)
295 begin : inst_2
296 assign inst = way_match[0] ? way_data[0] : (way_match[1] ? way_data[1] : 32'b0);
297 end
298 endgenerate
300 // Compute address to use to index into the data memories
301 generate
302 if (bytes_per_line > 4)
303 assign dmem_write_address = {refill_address[`LM32_IC_ADDR_SET_RNG], refill_offset};
304 else
305 assign dmem_write_address = refill_address[`LM32_IC_ADDR_SET_RNG];
306 endgenerate
308 assign dmem_read_address = address_a[`LM32_IC_ADDR_IDX_RNG];
310 // Compute address to use to index into the tag memories
311 assign tmem_read_address = address_a[`LM32_IC_ADDR_SET_RNG];
312 assign tmem_write_address = flushing
313 ? flush_set
314 : refill_address[`LM32_IC_ADDR_SET_RNG];
316 // Compute signal to indicate when we are on the last refill accesses
317 generate
318 if (bytes_per_line > 4)
319 assign last_refill = refill_offset == {addr_offset_width{1'b1}};
320 else
321 assign last_refill = `TRUE;
322 endgenerate
324 // Compute data and tag memory access enable
325 assign enable = (stall_a == `FALSE);
327 // Compute data and tag memory write enables
328 generate
329 if (associativity == 1)
330 begin : we_1
331 assign way_mem_we[0] = (refill_ready == `TRUE);
332 end
333 else
334 begin : we_2
335 assign way_mem_we[0] = (refill_ready == `TRUE) && (refill_way_select[0] == `TRUE);
336 assign way_mem_we[1] = (refill_ready == `TRUE) && (refill_way_select[1] == `TRUE);
337 end
338 endgenerate
340 // On the last refill cycle set the valid bit, for all other writes it should be cleared
341 assign tmem_write_data[`LM32_IC_TAGS_VALID_RNG] = last_refill & !flushing;
342 assign tmem_write_data[`LM32_IC_TAGS_TAG_RNG] = refill_address[`LM32_IC_ADDR_TAG_RNG];
344 // Signals that indicate which state we are in
345 assign flushing = |state[1:0];
346 assign check = state[2];
347 assign refill = state[3];
349 assign miss = (~(|way_match)) && (read_enable_f == `TRUE) && (stall_f == `FALSE) && !(valid_d && branch_predict_taken_d);
350 assign stall_request = (check == `FALSE);
351 assign refill_request = (refill == `TRUE);
353 /////////////////////////////////////////////////////
354 // Sequential logic
355 /////////////////////////////////////////////////////
357 // Record way selected for replacement on a cache miss
358 generate
359 if (associativity >= 2)
360 begin : way_select
361 always @(posedge clk_i `CFG_RESET_SENSITIVITY)
362 begin
363 if (rst_i == `TRUE)
364 refill_way_select <= {{associativity-1{1'b0}}, 1'b1};
365 else
366 begin
367 if (miss == `TRUE)
368 refill_way_select <= {refill_way_select[0], refill_way_select[1]};
369 end
370 end
371 end
372 endgenerate
374 // Record whether we are refilling
375 always @(posedge clk_i `CFG_RESET_SENSITIVITY)
376 begin
377 if (rst_i == `TRUE)
378 refilling <= `FALSE;
379 else
380 refilling <= refill;
381 end
383 // Instruction cache control FSM
384 always @(posedge clk_i `CFG_RESET_SENSITIVITY)
385 begin
386 if (rst_i == `TRUE)
387 begin
388 state <= `LM32_IC_STATE_FLUSH_INIT;
389 flush_set <= {`LM32_IC_TMEM_ADDR_WIDTH{1'b1}};
390 refill_address <= {`LM32_PC_WIDTH{1'bx}};
391 restart_request <= `FALSE;
392 end
393 else
394 begin
395 case (state)
397 // Flush the cache for the first time after reset
398 `LM32_IC_STATE_FLUSH_INIT:
399 begin
400 if (flush_set == {`LM32_IC_TMEM_ADDR_WIDTH{1'b0}})
401 state <= `LM32_IC_STATE_CHECK;
402 flush_set <= flush_set - 1'b1;
403 end
405 // Flush the cache in response to an write to the ICC CSR
406 `LM32_IC_STATE_FLUSH:
407 begin
408 if (flush_set == {`LM32_IC_TMEM_ADDR_WIDTH{1'b0}})
409 `ifdef CFG_IROM_ENABLED
410 if (select_f)
411 state <= `LM32_IC_STATE_REFILL;
412 else
413 `endif
414 state <= `LM32_IC_STATE_CHECK;
416 flush_set <= flush_set - 1'b1;
417 end
419 // Check for cache misses
420 `LM32_IC_STATE_CHECK:
421 begin
422 if (stall_a == `FALSE)
423 restart_request <= `FALSE;
424 if (iflush == `TRUE)
425 begin
426 refill_address <= address_f;
427 state <= `LM32_IC_STATE_FLUSH;
428 end
429 else if (miss == `TRUE)
430 begin
431 refill_address <= address_f;
432 state <= `LM32_IC_STATE_REFILL;
433 end
434 end
436 // Refill a cache line
437 `LM32_IC_STATE_REFILL:
438 begin
439 if (refill_ready == `TRUE)
440 begin
441 if (last_refill == `TRUE)
442 begin
443 restart_request <= `TRUE;
444 state <= `LM32_IC_STATE_CHECK;
445 end
446 end
447 end
449 endcase
450 end
451 end
453 generate
454 if (bytes_per_line > 4)
455 begin
456 // Refill offset
457 always @(posedge clk_i `CFG_RESET_SENSITIVITY)
458 begin
459 if (rst_i == `TRUE)
460 refill_offset <= {addr_offset_width{1'b0}};
461 else
462 begin
463 case (state)
465 // Check for cache misses
466 `LM32_IC_STATE_CHECK:
467 begin
468 if (iflush == `TRUE)
469 refill_offset <= {addr_offset_width{1'b0}};
470 else if (miss == `TRUE)
471 refill_offset <= {addr_offset_width{1'b0}};
472 end
474 // Refill a cache line
475 `LM32_IC_STATE_REFILL:
476 begin
477 if (refill_ready == `TRUE)
478 refill_offset <= refill_offset + 1'b1;
479 end
481 endcase
482 end
483 end
484 end
485 endgenerate
487 endmodule
489 `endif