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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1781

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 1555 nogj
#include "misc.h"
50 5 lampret
 
51 631 simons
extern struct dev_memarea *cur_area;
52 5 lampret
struct ic_set {
53 428 markom
  struct {
54 1350 nogj
    uint32_t line[MAX_IC_BLOCK_SIZE];
55
    oraddr_t tagaddr;  /* tag address */
56 428 markom
    int lru;    /* least recently used */
57
  } way[MAX_IC_WAYS];
58
} ic[MAX_IC_SETS];
59 5 lampret
 
60
void ic_info()
61
{
62 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP)) {
63 997 markom
    PRINTF("ICache not implemented. Set UPR[ICP].\n");
64 428 markom
    return;
65
  }
66 102 lampret
 
67 997 markom
  PRINTF("Instruction cache %dKB: ", config.ic.nsets * config.ic.blocksize * config.ic.nways / 1024);
68
  PRINTF("%d ways, %d sets, block size %d bytes\n", config.ic.nways, config.ic.nsets, config.ic.blocksize);
69 5 lampret
}
70
 
71
/* First check if instruction is already in the cache and if it is:
72
    - increment IC read hit stats,
73 428 markom
    - set 'lru' at this way to config.ic.ustates - 1 and
74 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
75 631 simons
    - read insn from the cache line
76 5 lampret
   and if not:
77
    - increment IC read miss stats
78
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
79 428 markom
    - set 'lru' with config.ic.ustates - 1 and decrement 'lru' of other
80 5 lampret
      ways unless they have reached 0
81 631 simons
    - refill cache line
82 5 lampret
*/
83
 
84 1486 nogj
uint32_t ic_simulate_fetch(oraddr_t fetchaddr, oraddr_t virt_addr)
85 5 lampret
{
86 428 markom
  int set, way = -1;
87
  int i;
88 1350 nogj
  oraddr_t tagaddr;
89
  uint32_t tmp;
90 102 lampret
 
91 428 markom
  /* ICache simulation enabled/disabled. */
92 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP) ||
93
      !(cpu_state.sprs[SPR_SR] & SPR_SR_ICE) || insn_ci) {
94 1486 nogj
    tmp = evalsim_mem32(fetchaddr, virt_addr);
95
    if (cur_area && cur_area->log)
96 1350 nogj
      fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n", fetchaddr,
97
               tmp);
98 992 simons
    return tmp;
99
  }
100 428 markom
 
101
  /* Which set to check out? */
102
  set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
103
  tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
104
 
105
  /* Scan all ways and try to find a matching way. */
106
  for (i = 0; i < config.ic.nways; i++)
107
    if (ic[set].way[i].tagaddr == tagaddr)
108
      way = i;
109
 
110
  /* Did we find our cached instruction? */
111
  if (way >= 0) { /* Yes, we did. */
112
    ic_stats.readhit++;
113
 
114
    for (i = 0; i < config.ic.nways; i++)
115 631 simons
      if (ic[set].way[i].lru > ic[set].way[way].lru)
116 428 markom
        ic[set].way[i].lru--;
117
    ic[set].way[way].lru = config.ic.ustates - 1;
118 884 markom
    runtime.sim.mem_cycles += config.ic.hitdelay;
119 631 simons
    return (ic[set].way[way].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
120 428 markom
  }
121
  else {  /* No, we didn't. */
122
    int minlru = config.ic.ustates - 1;
123
    int minway = 0;
124 5 lampret
 
125 631 simons
    ic_stats.readmiss++;
126 5 lampret
 
127 1085 simons
    for (i = 0; i < config.ic.nways; i++) {
128
      if (ic[set].way[i].lru < minlru) {
129 428 markom
        minway = i;
130 1085 simons
        minlru = ic[set].way[i].lru;
131
      }
132
    }
133 428 markom
 
134 631 simons
    for (i = 0; i < (config.ic.blocksize); i += 4) {
135 992 simons
      tmp = ic[set].way[minway].line[((fetchaddr + i) & (config.ic.blocksize - 1)) >> 2] =
136 1486 nogj
        /* FIXME: What is the virtual address meant to be? (ie. What happens if
137
         * we read out of memory while refilling a cache line?) */
138
        evalsim_mem32((fetchaddr & ~(config.ic.blocksize - 1)) + ((fetchaddr + i) & (config.ic.blocksize - 1)), 0);
139 638 simons
      if(!cur_area) {
140
        ic[set].way[minway].tagaddr = -1;
141
        ic[set].way[minway].lru = 0;
142 631 simons
        return 0;
143 1386 nogj
      } else if (cur_area->log)
144 1350 nogj
        fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n",
145
                 fetchaddr, tmp);
146 631 simons
    }
147
 
148 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
149
    for (i = 0; i < config.ic.nways; i++)
150 631 simons
      if (ic[set].way[i].lru)
151 428 markom
        ic[set].way[i].lru--;
152
    ic[set].way[minway].lru = config.ic.ustates - 1;
153 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
154 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
155 428 markom
  }
156 5 lampret
}
157 102 lampret
 
158
/* First check if data is already in the cache and if it is:
159
    - invalidate block if way isn't locked
160
   otherwise don't do anything.
161
*/
162
 
163 1350 nogj
void ic_inv(oraddr_t dataaddr)
164 102 lampret
{
165 428 markom
  int set, way = -1;
166
  int i;
167 1350 nogj
  oraddr_t tagaddr;
168 102 lampret
 
169 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP))
170 428 markom
    return;
171 102 lampret
 
172 428 markom
  /* Which set to check out? */
173
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
174
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
175 631 simons
 
176 1506 nogj
  if (!(cpu_state.sprs[SPR_SR] & SPR_SR_ICE)) {
177 631 simons
    for (i = 0; i < config.ic.nways; i++) {
178
      ic[set].way[i].tagaddr = -1;
179
      ic[set].way[i].lru = 0;
180
    }
181
    return;
182
  }
183 428 markom
 
184
  /* Scan all ways and try to find a matching way. */
185
  for (i = 0; i < config.ic.nways; i++)
186
    if (ic[set].way[i].tagaddr == tagaddr)
187
      way = i;
188
 
189
  /* Did we find our cached data? */
190 631 simons
  if (way >= 0) { /* Yes, we did. */
191 428 markom
    ic[set].way[way].tagaddr = -1;
192 631 simons
    ic[set].way[way].lru = 0;
193 428 markom
  }
194 102 lampret
}
195
 
196 1358 nogj
/*-----------------------------------------------------[ IC configuration ]---*/
197
void ic_enabled(union param_val val, void *dat)
198
{
199
  config.ic.enabled = val.int_val;
200 1506 nogj
  if(val.int_val)
201
    cpu_state.sprs[SPR_UPR] |= SPR_UPR_ICP;
202
  else
203
    cpu_state.sprs[SPR_UPR] &= ~SPR_UPR_ICP;
204 1358 nogj
}
205
 
206
void ic_nsets(union param_val val, void *dat)
207
{
208 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_SETS){
209 1358 nogj
    config.ic.nsets = val.int_val;
210 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_NCS;
211 1555 nogj
    cpu_state.sprs[SPR_ICCFGR] |= log2_int(val.int_val) << 3;
212 1382 nogj
  }
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 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_NCW;
225 1555 nogj
    cpu_state.sprs[SPR_ICCFGR] |= log2_int(val.int_val);
226 1382 nogj
  }
227
  else {
228
    char tmp[200];
229
    sprintf (tmp, "value of power of two and lower or equal than %i expected.",
230
    MAX_IC_WAYS);
231
    CONFIG_ERROR(tmp);
232
  }
233 1358 nogj
}
234
 
235
void ic_blocksize(union param_val val, void *dat)
236
{
237 1382 nogj
  if (is_power2(val.int_val)){
238 1358 nogj
    config.ic.blocksize = val.int_val;
239 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_CBS;
240 1555 nogj
    cpu_state.sprs[SPR_ICCFGR] |= log2_int(val.int_val) << 7;
241 1382 nogj
  } else
242 1358 nogj
    CONFIG_ERROR("value of power of two expected.");
243
}
244
 
245
void ic_ustates(union param_val val, void *dat)
246
{
247
  if (val.int_val >= 2 && val.int_val <= 4)
248
    config.ic.ustates = val.int_val;
249
  else
250
    CONFIG_ERROR("invalid USTATE.");
251
}
252
 
253
void ic_missdelay(union param_val val, void *dat)
254
{
255
  config.ic.missdelay = val.int_val;
256
}
257
 
258
void ic_hitdelay(union param_val val, void *dat)
259
{
260
  config.ic.hitdelay = val.int_val;
261
}
262
 
263
void reg_ic_sec(void)
264
{
265 1406 nogj
  struct config_section *sec = reg_config_sec("ic", NULL, NULL);
266 1358 nogj
 
267
  reg_config_param(sec, "enabled", paramt_int, ic_enabled);
268
  reg_config_param(sec, "nsets", paramt_int, ic_nsets);
269
  reg_config_param(sec, "nways", paramt_int, ic_nways);
270
  reg_config_param(sec, "blocksize", paramt_int, ic_blocksize);
271
  reg_config_param(sec, "ustates", paramt_int, ic_ustates);
272
  reg_config_param(sec, "missdelay", paramt_int, ic_missdelay);
273
  reg_config_param(sec, "hitdelay", paramt_int, ic_hitdelay);
274
}

powered by: WebSVN 2.1.0

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