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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1358

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 5 lampret
#include "stats.h"
44 102 lampret
#include "sim-config.h"
45
#include "spr_defs.h"
46 167 markom
#include "sprs.h"
47 428 markom
#include "sim-config.h"
48 5 lampret
 
49 631 simons
extern struct dev_memarea *cur_area;
50 5 lampret
struct ic_set {
51 428 markom
  struct {
52 1350 nogj
    uint32_t line[MAX_IC_BLOCK_SIZE];
53
    oraddr_t tagaddr;  /* tag address */
54 428 markom
    int lru;    /* least recently used */
55
  } way[MAX_IC_WAYS];
56
} ic[MAX_IC_SETS];
57 5 lampret
 
58
void ic_info()
59
{
60 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP)) {
61 997 markom
    PRINTF("ICache not implemented. Set UPR[ICP].\n");
62 428 markom
    return;
63
  }
64 102 lampret
 
65 997 markom
  PRINTF("Instruction cache %dKB: ", config.ic.nsets * config.ic.blocksize * config.ic.nways / 1024);
66
  PRINTF("%d ways, %d sets, block size %d bytes\n", config.ic.nways, config.ic.nsets, config.ic.blocksize);
67 5 lampret
}
68
 
69
/* First check if instruction is already in the cache and if it is:
70
    - increment IC read hit stats,
71 428 markom
    - set 'lru' at this way to config.ic.ustates - 1 and
72 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
73 631 simons
    - read insn from the cache line
74 5 lampret
   and if not:
75
    - increment IC read miss stats
76
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
77 428 markom
    - set 'lru' with config.ic.ustates - 1 and decrement 'lru' of other
78 5 lampret
      ways unless they have reached 0
79 631 simons
    - refill cache line
80 5 lampret
*/
81
 
82 1350 nogj
uint32_t ic_simulate_fetch(oraddr_t fetchaddr)
83 5 lampret
{
84 428 markom
  int set, way = -1;
85
  int i;
86 1350 nogj
  oraddr_t tagaddr;
87
  uint32_t tmp;
88 102 lampret
 
89 428 markom
  /* ICache simulation enabled/disabled. */
90 992 simons
  if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)) || insn_ci) {
91
    tmp = evalsim_mem32(fetchaddr);
92
    if(!cur_area) {
93 1350 nogj
      printf("EXCEPTION: read out of memory (32-bit access to %"PRIxADDR")\n",
94
             fetchaddr);
95 992 simons
      except_handle(EXCEPT_BUSERR, cur_vadd);
96
      return 0;
97
    }
98
    if (!pending.valid && 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 638 simons
      }
148 992 simons
      if (!pending.valid && cur_area->log)
149 1350 nogj
        fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n",
150
                 fetchaddr, tmp);
151 631 simons
    }
152
 
153 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
154
    for (i = 0; i < config.ic.nways; i++)
155 631 simons
      if (ic[set].way[i].lru)
156 428 markom
        ic[set].way[i].lru--;
157
    ic[set].way[minway].lru = config.ic.ustates - 1;
158 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
159 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
160 428 markom
  }
161 5 lampret
}
162 102 lampret
 
163
/* First check if data is already in the cache and if it is:
164
    - invalidate block if way isn't locked
165
   otherwise don't do anything.
166
*/
167
 
168 1350 nogj
void ic_inv(oraddr_t dataaddr)
169 102 lampret
{
170 428 markom
  int set, way = -1;
171
  int i;
172 1350 nogj
  oraddr_t tagaddr;
173 102 lampret
 
174 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
175
    return;
176 102 lampret
 
177 428 markom
  /* Which set to check out? */
178
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
179
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
180 631 simons
 
181
  if (!testsprbits(SPR_SR, SPR_SR_ICE)) {
182
    for (i = 0; i < config.ic.nways; i++) {
183
      ic[set].way[i].tagaddr = -1;
184
      ic[set].way[i].lru = 0;
185
    }
186
    return;
187
  }
188 428 markom
 
189
  /* Scan all ways and try to find a matching way. */
190
  for (i = 0; i < config.ic.nways; i++)
191
    if (ic[set].way[i].tagaddr == tagaddr)
192
      way = i;
193
 
194
  /* Did we find our cached data? */
195 631 simons
  if (way >= 0) { /* Yes, we did. */
196 428 markom
    ic[set].way[way].tagaddr = -1;
197 631 simons
    ic[set].way[way].lru = 0;
198 428 markom
  }
199 102 lampret
}
200
 
201 1350 nogj
void ic_clock()
202 102 lampret
{
203 1350 nogj
  oraddr_t addr;
204 428 markom
 
205 1308 phoenix
  if ((addr = mfspr(SPR_ICBPR))) {
206 428 markom
    ic_simulate_fetch(addr);
207
    mtspr(SPR_ICBPR, 0);
208
  }
209 1308 phoenix
  if ((addr = mfspr(SPR_ICBIR))) {
210 428 markom
    ic_inv(addr);
211
    mtspr(SPR_ICBIR, 0);
212
  }
213 1308 phoenix
  if ((addr = mfspr(SPR_ICBLR))) {
214 428 markom
    mtspr(SPR_ICBLR, 0);
215
  }
216 102 lampret
}
217 1358 nogj
 
218
/*-----------------------------------------------------[ IC configuration ]---*/
219
void ic_enabled(union param_val val, void *dat)
220
{
221
  config.ic.enabled = val.int_val;
222
  setsprbits (SPR_UPR, SPR_UPR_ICP, val.int_val ? 1 : 0);
223
}
224
 
225
void ic_nsets(union param_val val, void *dat)
226
{
227
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_SETS)
228
    config.ic.nsets = val.int_val;
229
  else {
230
    char tmp[200];
231
    sprintf (tmp, "value of power of two and lower or equal than %i expected.", MAX_IC_SETS);
232
    CONFIG_ERROR(tmp);
233
  }
234
}
235
 
236
void ic_nways(union param_val val, void *dat)
237
{
238
  if (val.int_val >= 1 && val.int_val <= MAX_IC_WAYS)
239
    config.ic.nways = val.int_val;
240
  else
241
    CONFIG_ERROR("value 1, 2, 3 or 4 expected.");
242
}
243
 
244
void ic_blocksize(union param_val val, void *dat)
245
{
246
  if (is_power2(val.int_val))
247
    config.ic.blocksize = val.int_val;
248
  else
249
    CONFIG_ERROR("value of power of two expected.");
250
}
251
 
252
void ic_ustates(union param_val val, void *dat)
253
{
254
  if (val.int_val >= 2 && val.int_val <= 4)
255
    config.ic.ustates = val.int_val;
256
  else
257
    CONFIG_ERROR("invalid USTATE.");
258
}
259
 
260
void ic_missdelay(union param_val val, void *dat)
261
{
262
  config.ic.missdelay = val.int_val;
263
}
264
 
265
void ic_hitdelay(union param_val val, void *dat)
266
{
267
  config.ic.hitdelay = val.int_val;
268
}
269
 
270
void reg_ic_sec(void)
271
{
272
  struct config_sections *sec = reg_config_sec("ic", NULL, NULL);
273
 
274
  reg_config_param(sec, "enabled", paramt_int, ic_enabled);
275
  reg_config_param(sec, "nsets", paramt_int, ic_nsets);
276
  reg_config_param(sec, "nways", paramt_int, ic_nways);
277
  reg_config_param(sec, "blocksize", paramt_int, ic_blocksize);
278
  reg_config_param(sec, "ustates", paramt_int, ic_ustates);
279
  reg_config_param(sec, "missdelay", paramt_int, ic_missdelay);
280
  reg_config_param(sec, "hitdelay", paramt_int, ic_hitdelay);
281
}

powered by: WebSVN 2.1.0

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