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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1506

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 1506 nogj
  if (!(cpu_state.sprs[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 1486 nogj
uint32_t ic_simulate_fetch(oraddr_t fetchaddr, oraddr_t virt_addr)
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 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP) ||
92
      !(cpu_state.sprs[SPR_SR] & SPR_SR_ICE) || insn_ci) {
93 1486 nogj
    tmp = evalsim_mem32(fetchaddr, virt_addr);
94
    if (cur_area && cur_area->log)
95 1350 nogj
      fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n", fetchaddr,
96
               tmp);
97 992 simons
    return tmp;
98
  }
99 428 markom
 
100
  /* Which set to check out? */
101
  set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
102
  tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
103
 
104
  /* Scan all ways and try to find a matching way. */
105
  for (i = 0; i < config.ic.nways; i++)
106
    if (ic[set].way[i].tagaddr == tagaddr)
107
      way = i;
108
 
109
  /* Did we find our cached instruction? */
110
  if (way >= 0) { /* Yes, we did. */
111
    ic_stats.readhit++;
112
 
113
    for (i = 0; i < config.ic.nways; i++)
114 631 simons
      if (ic[set].way[i].lru > ic[set].way[way].lru)
115 428 markom
        ic[set].way[i].lru--;
116
    ic[set].way[way].lru = config.ic.ustates - 1;
117 884 markom
    runtime.sim.mem_cycles += config.ic.hitdelay;
118 631 simons
    return (ic[set].way[way].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
119 428 markom
  }
120
  else {  /* No, we didn't. */
121
    int minlru = config.ic.ustates - 1;
122
    int minway = 0;
123 5 lampret
 
124 631 simons
    ic_stats.readmiss++;
125 5 lampret
 
126 1085 simons
    for (i = 0; i < config.ic.nways; i++) {
127
      if (ic[set].way[i].lru < minlru) {
128 428 markom
        minway = i;
129 1085 simons
        minlru = ic[set].way[i].lru;
130
      }
131
    }
132 428 markom
 
133 631 simons
    for (i = 0; i < (config.ic.blocksize); i += 4) {
134 992 simons
      tmp = ic[set].way[minway].line[((fetchaddr + i) & (config.ic.blocksize - 1)) >> 2] =
135 1486 nogj
        /* FIXME: What is the virtual address meant to be? (ie. What happens if
136
         * we read out of memory while refilling a cache line?) */
137
        evalsim_mem32((fetchaddr & ~(config.ic.blocksize - 1)) + ((fetchaddr + i) & (config.ic.blocksize - 1)), 0);
138 638 simons
      if(!cur_area) {
139
        ic[set].way[minway].tagaddr = -1;
140
        ic[set].way[minway].lru = 0;
141 631 simons
        return 0;
142 1386 nogj
      } else if (cur_area->log)
143 1350 nogj
        fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n",
144
                 fetchaddr, tmp);
145 631 simons
    }
146
 
147 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
148
    for (i = 0; i < config.ic.nways; i++)
149 631 simons
      if (ic[set].way[i].lru)
150 428 markom
        ic[set].way[i].lru--;
151
    ic[set].way[minway].lru = config.ic.ustates - 1;
152 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
153 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
154 428 markom
  }
155 5 lampret
}
156 102 lampret
 
157
/* First check if data is already in the cache and if it is:
158
    - invalidate block if way isn't locked
159
   otherwise don't do anything.
160
*/
161
 
162 1350 nogj
void ic_inv(oraddr_t dataaddr)
163 102 lampret
{
164 428 markom
  int set, way = -1;
165
  int i;
166 1350 nogj
  oraddr_t tagaddr;
167 102 lampret
 
168 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP))
169 428 markom
    return;
170 102 lampret
 
171 428 markom
  /* Which set to check out? */
172
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
173
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
174 631 simons
 
175 1506 nogj
  if (!(cpu_state.sprs[SPR_SR] & SPR_SR_ICE)) {
176 631 simons
    for (i = 0; i < config.ic.nways; i++) {
177
      ic[set].way[i].tagaddr = -1;
178
      ic[set].way[i].lru = 0;
179
    }
180
    return;
181
  }
182 428 markom
 
183
  /* Scan all ways and try to find a matching way. */
184
  for (i = 0; i < config.ic.nways; i++)
185
    if (ic[set].way[i].tagaddr == tagaddr)
186
      way = i;
187
 
188
  /* Did we find our cached data? */
189 631 simons
  if (way >= 0) { /* Yes, we did. */
190 428 markom
    ic[set].way[way].tagaddr = -1;
191 631 simons
    ic[set].way[way].lru = 0;
192 428 markom
  }
193 102 lampret
}
194
 
195 1358 nogj
/*-----------------------------------------------------[ IC configuration ]---*/
196
void ic_enabled(union param_val val, void *dat)
197
{
198
  config.ic.enabled = val.int_val;
199 1506 nogj
  if(val.int_val)
200
    cpu_state.sprs[SPR_UPR] |= SPR_UPR_ICP;
201
  else
202
    cpu_state.sprs[SPR_UPR] &= ~SPR_UPR_ICP;
203 1358 nogj
}
204
 
205
void ic_nsets(union param_val val, void *dat)
206
{
207 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_SETS){
208 1358 nogj
    config.ic.nsets = val.int_val;
209 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_NCS;
210
    cpu_state.sprs[SPR_ICCFGR] |= log2(val.int_val) << 3;
211 1382 nogj
  }
212 1358 nogj
  else {
213
    char tmp[200];
214
    sprintf (tmp, "value of power of two and lower or equal than %i expected.", MAX_IC_SETS);
215
    CONFIG_ERROR(tmp);
216
  }
217
}
218
 
219
void ic_nways(union param_val val, void *dat)
220
{
221 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_IC_WAYS) {
222 1358 nogj
    config.ic.nways = val.int_val;
223 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_NCW;
224
    cpu_state.sprs[SPR_ICCFGR] |= log2(val.int_val);
225 1382 nogj
  }
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 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_CBS;
239
    cpu_state.sprs[SPR_ICCFGR] |= log2(val.int_val) << 7;
240 1382 nogj
  } else
241 1358 nogj
    CONFIG_ERROR("value of power of two expected.");
242
}
243
 
244
void ic_ustates(union param_val val, void *dat)
245
{
246
  if (val.int_val >= 2 && val.int_val <= 4)
247
    config.ic.ustates = val.int_val;
248
  else
249
    CONFIG_ERROR("invalid USTATE.");
250
}
251
 
252
void ic_missdelay(union param_val val, void *dat)
253
{
254
  config.ic.missdelay = val.int_val;
255
}
256
 
257
void ic_hitdelay(union param_val val, void *dat)
258
{
259
  config.ic.hitdelay = val.int_val;
260
}
261
 
262
void reg_ic_sec(void)
263
{
264 1406 nogj
  struct config_section *sec = reg_config_sec("ic", NULL, NULL);
265 1358 nogj
 
266
  reg_config_param(sec, "enabled", paramt_int, ic_enabled);
267
  reg_config_param(sec, "nsets", paramt_int, ic_nsets);
268
  reg_config_param(sec, "nways", paramt_int, ic_nways);
269
  reg_config_param(sec, "blocksize", paramt_int, ic_blocksize);
270
  reg_config_param(sec, "ustates", paramt_int, ic_ustates);
271
  reg_config_param(sec, "missdelay", paramt_int, ic_missdelay);
272
  reg_config_param(sec, "hitdelay", paramt_int, ic_hitdelay);
273
}

powered by: WebSVN 2.1.0

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