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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1778

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

Line No. Rev Author Line
1 5 lampret
/* icache_model.c -- instruction cache simulation
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Cache functions.
21
   At the moment this functions only simulate functionality of instruction
22
   caches and do not influence on fetche/decode/execute stages and timings.
23
   They are here only to verify performance of various cache configurations.
24
 */
25
 
26
#include <stdio.h>
27
#include <string.h>
28
#include <errno.h>
29
#include <stdarg.h>
30
 
31 1350 nogj
#include "config.h"
32
 
33
#ifdef HAVE_INTTYPES_H
34
#include <inttypes.h>
35
#endif
36
 
37
#include "port.h"
38
#include "arch.h"
39
#include "abstract.h"
40 5 lampret
#include "icache_model.h"
41 992 simons
#include "except.h"
42 1344 nogj
#include "opcode/or32.h"
43 1432 nogj
#include "spr_defs.h"
44
#include "execute.h"
45 5 lampret
#include "stats.h"
46 102 lampret
#include "sim-config.h"
47 167 markom
#include "sprs.h"
48 428 markom
#include "sim-config.h"
49 5 lampret
 
50 631 simons
extern struct dev_memarea *cur_area;
51 5 lampret
struct ic_set {
52 428 markom
  struct {
53 1350 nogj
    uint32_t line[MAX_IC_BLOCK_SIZE];
54
    oraddr_t tagaddr;  /* tag address */
55 428 markom
    int lru;    /* least recently used */
56
  } way[MAX_IC_WAYS];
57
} ic[MAX_IC_SETS];
58 5 lampret
 
59
void ic_info()
60
{
61 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP)) {
62 997 markom
    PRINTF("ICache not implemented. Set UPR[ICP].\n");
63 428 markom
    return;
64
  }
65 102 lampret
 
66 997 markom
  PRINTF("Instruction cache %dKB: ", config.ic.nsets * config.ic.blocksize * config.ic.nways / 1024);
67
  PRINTF("%d ways, %d sets, block size %d bytes\n", config.ic.nways, config.ic.nsets, config.ic.blocksize);
68 5 lampret
}
69
 
70
/* First check if instruction is already in the cache and if it is:
71
    - increment IC read hit stats,
72 428 markom
    - set 'lru' at this way to config.ic.ustates - 1 and
73 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
74 631 simons
    - read insn from the cache line
75 5 lampret
   and if not:
76
    - increment IC read miss stats
77
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
78 428 markom
    - set 'lru' with config.ic.ustates - 1 and decrement 'lru' of other
79 5 lampret
      ways unless they have reached 0
80 631 simons
    - refill cache line
81 5 lampret
*/
82
 
83 1350 nogj
uint32_t ic_simulate_fetch(oraddr_t fetchaddr)
84 5 lampret
{
85 428 markom
  int set, way = -1;
86
  int i;
87 1350 nogj
  oraddr_t tagaddr;
88
  uint32_t tmp;
89 102 lampret
 
90 428 markom
  /* ICache simulation enabled/disabled. */
91 992 simons
  if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)) || insn_ci) {
92
    tmp = evalsim_mem32(fetchaddr);
93
    if(!cur_area) {
94 1350 nogj
      printf("EXCEPTION: read out of memory (32-bit access to %"PRIxADDR")\n",
95
             fetchaddr);
96 992 simons
      except_handle(EXCEPT_BUSERR, cur_vadd);
97
      return 0;
98 1386 nogj
    } else if (cur_area->log)
99 1350 nogj
      fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n", fetchaddr,
100
               tmp);
101 992 simons
    return tmp;
102
  }
103 428 markom
 
104
  /* Which set to check out? */
105
  set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
106
  tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
107
 
108
  /* Scan all ways and try to find a matching way. */
109
  for (i = 0; i < config.ic.nways; i++)
110
    if (ic[set].way[i].tagaddr == tagaddr)
111
      way = i;
112
 
113
  /* Did we find our cached instruction? */
114
  if (way >= 0) { /* Yes, we did. */
115
    ic_stats.readhit++;
116
 
117
    for (i = 0; i < config.ic.nways; i++)
118 631 simons
      if (ic[set].way[i].lru > ic[set].way[way].lru)
119 428 markom
        ic[set].way[i].lru--;
120
    ic[set].way[way].lru = config.ic.ustates - 1;
121 884 markom
    runtime.sim.mem_cycles += config.ic.hitdelay;
122 631 simons
    return (ic[set].way[way].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
123 428 markom
  }
124
  else {  /* No, we didn't. */
125
    int minlru = config.ic.ustates - 1;
126
    int minway = 0;
127 5 lampret
 
128 631 simons
    ic_stats.readmiss++;
129 5 lampret
 
130 1085 simons
    for (i = 0; i < config.ic.nways; i++) {
131
      if (ic[set].way[i].lru < minlru) {
132 428 markom
        minway = i;
133 1085 simons
        minlru = ic[set].way[i].lru;
134
      }
135
    }
136 428 markom
 
137 631 simons
    for (i = 0; i < (config.ic.blocksize); i += 4) {
138 992 simons
      tmp = ic[set].way[minway].line[((fetchaddr + i) & (config.ic.blocksize - 1)) >> 2] =
139 631 simons
        evalsim_mem32((fetchaddr & ~(config.ic.blocksize - 1)) + ((fetchaddr + i) & (config.ic.blocksize - 1)));
140 638 simons
      if(!cur_area) {
141
        ic[set].way[minway].tagaddr = -1;
142
        ic[set].way[minway].lru = 0;
143 1350 nogj
        printf("EXCEPTION: read out of memory (32-bit access to %"PRIxADDR")\n",
144
               fetchaddr);
145 992 simons
        except_handle(EXCEPT_BUSERR, cur_vadd);
146 631 simons
        return 0;
147 1386 nogj
      } else if (cur_area->log)
148 1350 nogj
        fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n",
149
                 fetchaddr, tmp);
150 631 simons
    }
151
 
152 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
153
    for (i = 0; i < config.ic.nways; i++)
154 631 simons
      if (ic[set].way[i].lru)
155 428 markom
        ic[set].way[i].lru--;
156
    ic[set].way[minway].lru = config.ic.ustates - 1;
157 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
158 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
159 428 markom
  }
160 5 lampret
}
161 102 lampret
 
162
/* First check if data is already in the cache and if it is:
163
    - invalidate block if way isn't locked
164
   otherwise don't do anything.
165
*/
166
 
167 1350 nogj
void ic_inv(oraddr_t dataaddr)
168 102 lampret
{
169 428 markom
  int set, way = -1;
170
  int i;
171 1350 nogj
  oraddr_t tagaddr;
172 102 lampret
 
173 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
174
    return;
175 102 lampret
 
176 428 markom
  /* Which set to check out? */
177
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
178
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
179 631 simons
 
180
  if (!testsprbits(SPR_SR, SPR_SR_ICE)) {
181
    for (i = 0; i < config.ic.nways; i++) {
182
      ic[set].way[i].tagaddr = -1;
183
      ic[set].way[i].lru = 0;
184
    }
185
    return;
186
  }
187 428 markom
 
188
  /* Scan all ways and try to find a matching way. */
189
  for (i = 0; i < config.ic.nways; i++)
190
    if (ic[set].way[i].tagaddr == tagaddr)
191
      way = i;
192
 
193
  /* Did we find our cached data? */
194 631 simons
  if (way >= 0) { /* Yes, we did. */
195 428 markom
    ic[set].way[way].tagaddr = -1;
196 631 simons
    ic[set].way[way].lru = 0;
197 428 markom
  }
198 102 lampret
}
199
 
200 1358 nogj
/*-----------------------------------------------------[ IC configuration ]---*/
201
void ic_enabled(union param_val val, void *dat)
202
{
203
  config.ic.enabled = val.int_val;
204
  setsprbits (SPR_UPR, SPR_UPR_ICP, val.int_val ? 1 : 0);
205
}
206
 
207
void ic_nsets(union param_val val, void *dat)
208
{
209 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_SETS){
210 1358 nogj
    config.ic.nsets = val.int_val;
211 1382 nogj
    setsprbits (SPR_ICCFGR, SPR_ICCFGR_NCS,log2(val.int_val));
212
  }
213 1358 nogj
  else {
214
    char tmp[200];
215
    sprintf (tmp, "value of power of two and lower or equal than %i expected.", MAX_IC_SETS);
216
    CONFIG_ERROR(tmp);
217
  }
218
}
219
 
220
void ic_nways(union param_val val, void *dat)
221
{
222 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_WAYS) {
223 1358 nogj
    config.ic.nways = val.int_val;
224 1382 nogj
    setsprbits (SPR_ICCFGR, SPR_ICCFGR_NCW, log2(val.int_val));
225
  }
226
  else {
227
    char tmp[200];
228
    sprintf (tmp, "value of power of two and lower or equal than %i expected.",
229
    MAX_IC_WAYS);
230
    CONFIG_ERROR(tmp);
231
  }
232 1358 nogj
}
233
 
234
void ic_blocksize(union param_val val, void *dat)
235
{
236 1382 nogj
  if (is_power2(val.int_val)){
237 1358 nogj
    config.ic.blocksize = val.int_val;
238 1382 nogj
    setsprbits (SPR_ICCFGR, SPR_ICCFGR_CBS,log2(val.int_val));
239
  } else
240 1358 nogj
    CONFIG_ERROR("value of power of two expected.");
241
}
242
 
243
void ic_ustates(union param_val val, void *dat)
244
{
245
  if (val.int_val >= 2 && val.int_val <= 4)
246
    config.ic.ustates = val.int_val;
247
  else
248
    CONFIG_ERROR("invalid USTATE.");
249
}
250
 
251
void ic_missdelay(union param_val val, void *dat)
252
{
253
  config.ic.missdelay = val.int_val;
254
}
255
 
256
void ic_hitdelay(union param_val val, void *dat)
257
{
258
  config.ic.hitdelay = val.int_val;
259
}
260
 
261
void reg_ic_sec(void)
262
{
263 1406 nogj
  struct config_section *sec = reg_config_sec("ic", NULL, NULL);
264 1358 nogj
 
265
  reg_config_param(sec, "enabled", paramt_int, ic_enabled);
266
  reg_config_param(sec, "nsets", paramt_int, ic_nsets);
267
  reg_config_param(sec, "nways", paramt_int, ic_nways);
268
  reg_config_param(sec, "blocksize", paramt_int, ic_blocksize);
269
  reg_config_param(sec, "ustates", paramt_int, ic_ustates);
270
  reg_config_param(sec, "missdelay", paramt_int, ic_missdelay);
271
  reg_config_param(sec, "hitdelay", paramt_int, ic_hitdelay);
272
}

powered by: WebSVN 2.1.0

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