lm32_icache.v

changeset 24
c336e674a37e
parent 23
252df75c8f67
child 25
7422134cbfea
     1.1 --- a/lm32_icache.v	Sun Mar 06 21:17:31 2011 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,494 +0,0 @@
     1.4 -// =============================================================================
     1.5 -//                           COPYRIGHT NOTICE
     1.6 -// Copyright 2006 (c) Lattice Semiconductor Corporation
     1.7 -// ALL RIGHTS RESERVED
     1.8 -// This confidential and proprietary software may be used only as authorised by
     1.9 -// a licensing agreement from Lattice Semiconductor Corporation.
    1.10 -// The entire notice above must be reproduced on all authorized copies and
    1.11 -// copies may only be made to the extent permitted by a licensing agreement from
    1.12 -// Lattice Semiconductor Corporation.
    1.13 -//
    1.14 -// Lattice Semiconductor Corporation        TEL : 1-800-Lattice (USA and Canada)
    1.15 -// 5555 NE Moore Court                            408-826-6000 (other locations)
    1.16 -// Hillsboro, OR 97124                     web  : http://www.latticesemi.com/
    1.17 -// U.S.A                                   email: techsupport@latticesemi.com
    1.18 -// =============================================================================/
    1.19 -//                         FILE DETAILS
    1.20 -// Project          : LatticeMico32
    1.21 -// File             : lm32_icache.v
    1.22 -// Title            : Instruction cache
    1.23 -// Dependencies     : lm32_include.v
    1.24 -// 
    1.25 -// Version 3.5
    1.26 -// 1. Bug Fix: Instruction cache flushes issued from Instruction Inline Memory
    1.27 -//    cause segmentation fault due to incorrect fetches.
    1.28 -//
    1.29 -// Version 3.1
    1.30 -// 1. Feature: Support for user-selected resource usage when implementing
    1.31 -//    cache memory. Additional parameters must be defined when invoking module
    1.32 -//    lm32_ram. Instruction cache miss mechanism is dependent on branch
    1.33 -//    prediction being performed in D stage of pipeline.
    1.34 -//
    1.35 -// Version 7.0SP2, 3.0
    1.36 -// No change
    1.37 -// =============================================================================
    1.38 -					  
    1.39 -`include "lm32_include.v"
    1.40 -
    1.41 -`ifdef CFG_ICACHE_ENABLED
    1.42 -
    1.43 -`define LM32_IC_ADDR_OFFSET_RNG          addr_offset_msb:addr_offset_lsb
    1.44 -`define LM32_IC_ADDR_SET_RNG             addr_set_msb:addr_set_lsb
    1.45 -`define LM32_IC_ADDR_TAG_RNG             addr_tag_msb:addr_tag_lsb
    1.46 -`define LM32_IC_ADDR_IDX_RNG             addr_set_msb:addr_offset_lsb
    1.47 -
    1.48 -`define LM32_IC_TMEM_ADDR_WIDTH          addr_set_width
    1.49 -`define LM32_IC_TMEM_ADDR_RNG            (`LM32_IC_TMEM_ADDR_WIDTH-1):0
    1.50 -`define LM32_IC_DMEM_ADDR_WIDTH          (addr_offset_width+addr_set_width)
    1.51 -`define LM32_IC_DMEM_ADDR_RNG            (`LM32_IC_DMEM_ADDR_WIDTH-1):0
    1.52 -
    1.53 -`define LM32_IC_TAGS_WIDTH               (addr_tag_width+1)
    1.54 -`define LM32_IC_TAGS_RNG                 (`LM32_IC_TAGS_WIDTH-1):0
    1.55 -`define LM32_IC_TAGS_TAG_RNG             (`LM32_IC_TAGS_WIDTH-1):1
    1.56 -`define LM32_IC_TAGS_VALID_RNG           0
    1.57 -
    1.58 -`define LM32_IC_STATE_RNG                3:0
    1.59 -`define LM32_IC_STATE_FLUSH_INIT         4'b0001
    1.60 -`define LM32_IC_STATE_FLUSH              4'b0010
    1.61 -`define LM32_IC_STATE_CHECK              4'b0100
    1.62 -`define LM32_IC_STATE_REFILL             4'b1000
    1.63 -
    1.64 -/////////////////////////////////////////////////////
    1.65 -// Module interface
    1.66 -/////////////////////////////////////////////////////
    1.67 -
    1.68 -module lm32_icache ( 
    1.69 -    // ----- Inputs -----
    1.70 -    clk_i,
    1.71 -    rst_i,    
    1.72 -    stall_a,
    1.73 -    stall_f,
    1.74 -    address_a,
    1.75 -    address_f,
    1.76 -    read_enable_f,
    1.77 -    refill_ready,
    1.78 -    refill_data,
    1.79 -    iflush,
    1.80 -`ifdef CFG_IROM_ENABLED
    1.81 -    select_f,
    1.82 -`endif
    1.83 -    valid_d,
    1.84 -    branch_predict_taken_d,
    1.85 -    // ----- Outputs -----
    1.86 -    stall_request,
    1.87 -    restart_request,
    1.88 -    refill_request,
    1.89 -    refill_address,
    1.90 -    refilling,
    1.91 -    inst
    1.92 -    );
    1.93 -
    1.94 -/////////////////////////////////////////////////////
    1.95 -// Parameters
    1.96 -/////////////////////////////////////////////////////
    1.97 -
    1.98 -parameter associativity = 1;                            // Associativity of the cache (Number of ways)
    1.99 -parameter sets = 512;                                   // Number of sets
   1.100 -parameter bytes_per_line = 16;                          // Number of bytes per cache line
   1.101 -parameter base_address = 0;                             // Base address of cachable memory
   1.102 -parameter limit = 0;                                    // Limit (highest address) of cachable memory
   1.103 -
   1.104 -localparam addr_offset_width = clogb2(bytes_per_line)-1-2;
   1.105 -localparam addr_set_width = clogb2(sets)-1;
   1.106 -localparam addr_offset_lsb = 2;
   1.107 -localparam addr_offset_msb = (addr_offset_lsb+addr_offset_width-1);
   1.108 -localparam addr_set_lsb = (addr_offset_msb+1);
   1.109 -localparam addr_set_msb = (addr_set_lsb+addr_set_width-1);
   1.110 -localparam addr_tag_lsb = (addr_set_msb+1);
   1.111 -localparam addr_tag_msb = clogb2(`CFG_ICACHE_LIMIT-`CFG_ICACHE_BASE_ADDRESS)-1;
   1.112 -localparam addr_tag_width = (addr_tag_msb-addr_tag_lsb+1);
   1.113 -
   1.114 -/////////////////////////////////////////////////////
   1.115 -// Inputs
   1.116 -/////////////////////////////////////////////////////
   1.117 -
   1.118 -input clk_i;                                        // Clock 
   1.119 -input rst_i;                                        // Reset
   1.120 -
   1.121 -input stall_a;                                      // Stall instruction in A stage
   1.122 -input stall_f;                                      // Stall instruction in F stage
   1.123 -
   1.124 -input valid_d;                                      // Valid instruction in D stage
   1.125 -input branch_predict_taken_d;                       // Instruction in D stage is a branch and is predicted taken
   1.126 -   
   1.127 -input [`LM32_PC_RNG] address_a;                     // Address of instruction in A stage
   1.128 -input [`LM32_PC_RNG] address_f;                     // Address of instruction in F stage
   1.129 -input read_enable_f;                                // Indicates if cache access is valid
   1.130 -
   1.131 -input refill_ready;                                 // Next word of refill data is ready
   1.132 -input [`LM32_INSTRUCTION_RNG] refill_data;          // Data to refill the cache with
   1.133 -
   1.134 -input iflush;                                       // Flush the cache
   1.135 -`ifdef CFG_IROM_ENABLED
   1.136 -input select_f;                                     // Instruction in F stage is mapped through instruction cache
   1.137 -`endif
   1.138 -   
   1.139 -/////////////////////////////////////////////////////
   1.140 -// Outputs
   1.141 -/////////////////////////////////////////////////////
   1.142 -
   1.143 -output stall_request;                               // Request to stall the pipeline
   1.144 -wire   stall_request;
   1.145 -output restart_request;                             // Request to restart instruction that caused the cache miss
   1.146 -reg    restart_request;
   1.147 -output refill_request;                              // Request to refill a cache line
   1.148 -wire   refill_request;
   1.149 -output [`LM32_PC_RNG] refill_address;               // Base address of cache refill
   1.150 -reg    [`LM32_PC_RNG] refill_address;               
   1.151 -output refilling;                                   // Indicates the instruction cache is currently refilling
   1.152 -reg    refilling;
   1.153 -output [`LM32_INSTRUCTION_RNG] inst;                // Instruction read from cache
   1.154 -wire   [`LM32_INSTRUCTION_RNG] inst;
   1.155 -
   1.156 -/////////////////////////////////////////////////////
   1.157 -// Internal nets and registers 
   1.158 -/////////////////////////////////////////////////////
   1.159 -
   1.160 -wire enable;
   1.161 -wire [0:associativity-1] way_mem_we;
   1.162 -wire [`LM32_INSTRUCTION_RNG] way_data[0:associativity-1];
   1.163 -wire [`LM32_IC_TAGS_TAG_RNG] way_tag[0:associativity-1];
   1.164 -wire [0:associativity-1] way_valid;
   1.165 -wire [0:associativity-1] way_match;
   1.166 -wire miss;
   1.167 -
   1.168 -wire [`LM32_IC_TMEM_ADDR_RNG] tmem_read_address;
   1.169 -wire [`LM32_IC_TMEM_ADDR_RNG] tmem_write_address;
   1.170 -wire [`LM32_IC_DMEM_ADDR_RNG] dmem_read_address;
   1.171 -wire [`LM32_IC_DMEM_ADDR_RNG] dmem_write_address;
   1.172 -wire [`LM32_IC_TAGS_RNG] tmem_write_data;
   1.173 -
   1.174 -reg [`LM32_IC_STATE_RNG] state;
   1.175 -wire flushing;
   1.176 -wire check;
   1.177 -wire refill;
   1.178 -
   1.179 -reg [associativity-1:0] refill_way_select;
   1.180 -reg [`LM32_IC_ADDR_OFFSET_RNG] refill_offset;
   1.181 -wire last_refill;
   1.182 -reg [`LM32_IC_TMEM_ADDR_RNG] flush_set;
   1.183 -
   1.184 -genvar i;
   1.185 -
   1.186 -/////////////////////////////////////////////////////
   1.187 -// Functions
   1.188 -/////////////////////////////////////////////////////
   1.189 -
   1.190 -`include "lm32_functions.v"
   1.191 -
   1.192 -/////////////////////////////////////////////////////
   1.193 -// Instantiations
   1.194 -/////////////////////////////////////////////////////
   1.195 -
   1.196 -   generate
   1.197 -      for (i = 0; i < associativity; i = i + 1)
   1.198 -	begin : memories
   1.199 -	   
   1.200 -	   lm32_ram 
   1.201 -	     #(
   1.202 -	       // ----- Parameters -------
   1.203 -	       .data_width                 (32),
   1.204 -	       .address_width              (`LM32_IC_DMEM_ADDR_WIDTH)
   1.205 -`ifdef PLATFORM_LATTICE
   1.206 -			,
   1.207 - `ifdef CFG_ICACHE_DAT_USE_DP_TRUE
   1.208 -	       .RAM_IMPLEMENTATION         ("EBR"),
   1.209 -	       .RAM_TYPE                   ("RAM_DP_TRUE")
   1.210 - `else
   1.211 -  `ifdef CFG_ICACHE_DAT_USE_DP
   1.212 -	       .RAM_IMPLEMENTATION         ("EBR"),
   1.213 -	       .RAM_TYPE                   ("RAM_DP")
   1.214 -  `else
   1.215 -   `ifdef CFG_ICACHE_DAT_USE_SLICE
   1.216 -	       .RAM_IMPLEMENTATION         ("SLICE")
   1.217 -   `else
   1.218 -	       .RAM_IMPLEMENTATION         ("AUTO")
   1.219 -   `endif
   1.220 -  `endif
   1.221 - `endif
   1.222 -`endif
   1.223 -	       ) 
   1.224 -	   way_0_data_ram 
   1.225 -	     (
   1.226 -	      // ----- Inputs -------
   1.227 -	      .read_clk                   (clk_i),
   1.228 -	      .write_clk                  (clk_i),
   1.229 -	      .reset                      (rst_i),
   1.230 -	      .read_address               (dmem_read_address),
   1.231 -	      .enable_read                (enable),
   1.232 -	      .write_address              (dmem_write_address),
   1.233 -	      .enable_write               (`TRUE),
   1.234 -	      .write_enable               (way_mem_we[i]),
   1.235 -	      .write_data                 (refill_data),    
   1.236 -	      // ----- Outputs -------
   1.237 -	      .read_data                  (way_data[i])
   1.238 -	      );
   1.239 -	   
   1.240 -	   lm32_ram 
   1.241 -	     #(
   1.242 -	       // ----- Parameters -------
   1.243 -	       .data_width                 (`LM32_IC_TAGS_WIDTH),
   1.244 -	       .address_width              (`LM32_IC_TMEM_ADDR_WIDTH)
   1.245 -`ifdef PLATFORM_LATTICE
   1.246 -			,
   1.247 - `ifdef CFG_ICACHE_DAT_USE_DP_TRUE
   1.248 -	       .RAM_IMPLEMENTATION         ("EBR"),
   1.249 -	       .RAM_TYPE                   ("RAM_DP_TRUE")
   1.250 - `else
   1.251 -  `ifdef CFG_ICACHE_DAT_USE_DP
   1.252 -	       .RAM_IMPLEMENTATION         ("EBR"),
   1.253 -	       .RAM_TYPE                   ("RAM_DP")
   1.254 -  `else
   1.255 -   `ifdef CFG_ICACHE_DAT_USE_SLICE
   1.256 -	       .RAM_IMPLEMENTATION         ("SLICE")
   1.257 -   `else
   1.258 -	       .RAM_IMPLEMENTATION         ("AUTO")
   1.259 -   `endif
   1.260 -  `endif
   1.261 - `endif
   1.262 -`endif
   1.263 -	       ) 
   1.264 -	   way_0_tag_ram 
   1.265 -	     (
   1.266 -	      // ----- Inputs -------
   1.267 -	      .read_clk                   (clk_i),
   1.268 -	      .write_clk                  (clk_i),
   1.269 -	      .reset                      (rst_i),
   1.270 -	      .read_address               (tmem_read_address),
   1.271 -	      .enable_read                (enable),
   1.272 -	      .write_address              (tmem_write_address),
   1.273 -	      .enable_write               (`TRUE),
   1.274 -	      .write_enable               (way_mem_we[i] | flushing),
   1.275 -	      .write_data                 (tmem_write_data),
   1.276 -	      // ----- Outputs -------
   1.277 -	      .read_data                  ({way_tag[i], way_valid[i]})
   1.278 -	      );
   1.279 -	   
   1.280 -	end
   1.281 -endgenerate
   1.282 -
   1.283 -/////////////////////////////////////////////////////
   1.284 -// Combinational logic
   1.285 -/////////////////////////////////////////////////////
   1.286 -
   1.287 -// Compute which ways in the cache match the address address being read
   1.288 -generate
   1.289 -    for (i = 0; i < associativity; i = i + 1)
   1.290 -    begin : match
   1.291 -assign way_match[i] = ({way_tag[i], way_valid[i]} == {address_f[`LM32_IC_ADDR_TAG_RNG], `TRUE});
   1.292 -    end
   1.293 -endgenerate
   1.294 -
   1.295 -// Select data from way that matched the address being read     
   1.296 -generate
   1.297 -    if (associativity == 1)
   1.298 -    begin : inst_1
   1.299 -assign inst = way_match[0] ? way_data[0] : 32'b0;
   1.300 -    end
   1.301 -    else if (associativity == 2)
   1.302 -	 begin : inst_2
   1.303 -assign inst = way_match[0] ? way_data[0] : (way_match[1] ? way_data[1] : 32'b0);
   1.304 -    end
   1.305 -endgenerate
   1.306 -
   1.307 -// Compute address to use to index into the data memories
   1.308 -generate 
   1.309 -    if (bytes_per_line > 4)
   1.310 -assign dmem_write_address = {refill_address[`LM32_IC_ADDR_SET_RNG], refill_offset};
   1.311 -    else
   1.312 -assign dmem_write_address = refill_address[`LM32_IC_ADDR_SET_RNG];
   1.313 -endgenerate
   1.314 -    
   1.315 -assign dmem_read_address = address_a[`LM32_IC_ADDR_IDX_RNG];
   1.316 -
   1.317 -// Compute address to use to index into the tag memories                        
   1.318 -assign tmem_read_address = address_a[`LM32_IC_ADDR_SET_RNG];
   1.319 -assign tmem_write_address = flushing 
   1.320 -                                ? flush_set
   1.321 -                                : refill_address[`LM32_IC_ADDR_SET_RNG];
   1.322 -
   1.323 -// Compute signal to indicate when we are on the last refill accesses
   1.324 -generate 
   1.325 -    if (bytes_per_line > 4)                            
   1.326 -assign last_refill = refill_offset == {addr_offset_width{1'b1}};
   1.327 -    else
   1.328 -assign last_refill = `TRUE;
   1.329 -endgenerate
   1.330 -
   1.331 -// Compute data and tag memory access enable
   1.332 -assign enable = (stall_a == `FALSE);
   1.333 -
   1.334 -// Compute data and tag memory write enables
   1.335 -generate
   1.336 -    if (associativity == 1) 
   1.337 -    begin : we_1     
   1.338 -assign way_mem_we[0] = (refill_ready == `TRUE);
   1.339 -    end
   1.340 -    else
   1.341 -    begin : we_2
   1.342 -assign way_mem_we[0] = (refill_ready == `TRUE) && (refill_way_select[0] == `TRUE);
   1.343 -assign way_mem_we[1] = (refill_ready == `TRUE) && (refill_way_select[1] == `TRUE);
   1.344 -    end
   1.345 -endgenerate                     
   1.346 -
   1.347 -// On the last refill cycle set the valid bit, for all other writes it should be cleared
   1.348 -assign tmem_write_data[`LM32_IC_TAGS_VALID_RNG] = last_refill & !flushing;
   1.349 -assign tmem_write_data[`LM32_IC_TAGS_TAG_RNG] = refill_address[`LM32_IC_ADDR_TAG_RNG];
   1.350 -
   1.351 -// Signals that indicate which state we are in
   1.352 -assign flushing = |state[1:0];
   1.353 -assign check = state[2];
   1.354 -assign refill = state[3];
   1.355 -
   1.356 -assign miss = (~(|way_match)) && (read_enable_f == `TRUE) && (stall_f == `FALSE) && !(valid_d && branch_predict_taken_d);
   1.357 -assign stall_request = (check == `FALSE);
   1.358 -assign refill_request = (refill == `TRUE);
   1.359 -                      
   1.360 -/////////////////////////////////////////////////////
   1.361 -// Sequential logic
   1.362 -/////////////////////////////////////////////////////
   1.363 -
   1.364 -// Record way selected for replacement on a cache miss
   1.365 -generate
   1.366 -    if (associativity >= 2) 
   1.367 -    begin : way_select      
   1.368 -always @(posedge clk_i `CFG_RESET_SENSITIVITY)
   1.369 -begin
   1.370 -    if (rst_i == `TRUE)
   1.371 -        refill_way_select <= {{associativity-1{1'b0}}, 1'b1};
   1.372 -    else
   1.373 -    begin        
   1.374 -        if (miss == `TRUE)
   1.375 -            refill_way_select <= {refill_way_select[0], refill_way_select[1]};
   1.376 -    end
   1.377 -end
   1.378 -    end
   1.379 -endgenerate
   1.380 -
   1.381 -// Record whether we are refilling
   1.382 -always @(posedge clk_i `CFG_RESET_SENSITIVITY)
   1.383 -begin
   1.384 -    if (rst_i == `TRUE)
   1.385 -        refilling <= `FALSE;
   1.386 -    else
   1.387 -        refilling <= refill;
   1.388 -end
   1.389 -
   1.390 -// Instruction cache control FSM
   1.391 -always @(posedge clk_i `CFG_RESET_SENSITIVITY)
   1.392 -begin
   1.393 -    if (rst_i == `TRUE)
   1.394 -    begin
   1.395 -        state <= `LM32_IC_STATE_FLUSH_INIT;
   1.396 -        flush_set <= {`LM32_IC_TMEM_ADDR_WIDTH{1'b1}};
   1.397 -        refill_address <= {`LM32_PC_WIDTH{1'bx}};
   1.398 -        restart_request <= `FALSE;
   1.399 -    end
   1.400 -    else 
   1.401 -    begin
   1.402 -        case (state)
   1.403 -
   1.404 -        // Flush the cache for the first time after reset
   1.405 -        `LM32_IC_STATE_FLUSH_INIT:
   1.406 -        begin            
   1.407 -            if (flush_set == {`LM32_IC_TMEM_ADDR_WIDTH{1'b0}})
   1.408 -                state <= `LM32_IC_STATE_CHECK;
   1.409 -            flush_set <= flush_set - 1'b1;
   1.410 -        end
   1.411 -
   1.412 -        // Flush the cache in response to an write to the ICC CSR
   1.413 -        `LM32_IC_STATE_FLUSH:
   1.414 -        begin            
   1.415 -            if (flush_set == {`LM32_IC_TMEM_ADDR_WIDTH{1'b0}})
   1.416 -`ifdef CFG_IROM_ENABLED
   1.417 -	      if (select_f)
   1.418 -                state <= `LM32_IC_STATE_REFILL;
   1.419 -	      else
   1.420 -`endif
   1.421 -		state <= `LM32_IC_STATE_CHECK;
   1.422 -	   
   1.423 -            flush_set <= flush_set - 1'b1;
   1.424 -        end
   1.425 -        
   1.426 -        // Check for cache misses
   1.427 -        `LM32_IC_STATE_CHECK:
   1.428 -        begin            
   1.429 -            if (stall_a == `FALSE)
   1.430 -                restart_request <= `FALSE;
   1.431 -            if (iflush == `TRUE)
   1.432 -            begin
   1.433 -                refill_address <= address_f;
   1.434 -                state <= `LM32_IC_STATE_FLUSH;
   1.435 -            end
   1.436 -            else if (miss == `TRUE)
   1.437 -            begin
   1.438 -                refill_address <= address_f;
   1.439 -                state <= `LM32_IC_STATE_REFILL;
   1.440 -            end
   1.441 -        end
   1.442 -
   1.443 -        // Refill a cache line
   1.444 -        `LM32_IC_STATE_REFILL:
   1.445 -        begin            
   1.446 -            if (refill_ready == `TRUE)
   1.447 -            begin
   1.448 -                if (last_refill == `TRUE)
   1.449 -                begin
   1.450 -                    restart_request <= `TRUE;
   1.451 -                    state <= `LM32_IC_STATE_CHECK;
   1.452 -                end
   1.453 -            end
   1.454 -        end
   1.455 -
   1.456 -        endcase        
   1.457 -    end
   1.458 -end
   1.459 -
   1.460 -generate 
   1.461 -    if (bytes_per_line > 4)
   1.462 -    begin
   1.463 -// Refill offset
   1.464 -always @(posedge clk_i `CFG_RESET_SENSITIVITY)
   1.465 -begin
   1.466 -    if (rst_i == `TRUE)
   1.467 -        refill_offset <= {addr_offset_width{1'b0}};
   1.468 -    else 
   1.469 -    begin
   1.470 -        case (state)
   1.471 -        
   1.472 -        // Check for cache misses
   1.473 -        `LM32_IC_STATE_CHECK:
   1.474 -        begin            
   1.475 -            if (iflush == `TRUE)
   1.476 -                refill_offset <= {addr_offset_width{1'b0}};
   1.477 -            else if (miss == `TRUE)
   1.478 -                refill_offset <= {addr_offset_width{1'b0}};
   1.479 -        end
   1.480 -
   1.481 -        // Refill a cache line
   1.482 -        `LM32_IC_STATE_REFILL:
   1.483 -        begin            
   1.484 -            if (refill_ready == `TRUE)
   1.485 -                refill_offset <= refill_offset + 1'b1;
   1.486 -        end
   1.487 -
   1.488 -        endcase        
   1.489 -    end
   1.490 -end
   1.491 -    end
   1.492 -endgenerate
   1.493 -   
   1.494 -endmodule
   1.495 -
   1.496 -`endif
   1.497 -