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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [cache/] [dcache_model.c] - Blame information for rev 992

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

Line No. Rev Author Line
1 5 lampret
/* dcache_model.c -- data 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 data
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
#include "dcache_model.h"
32
#include "abstract.h"
33 992 simons
#include "except.h"
34 5 lampret
#include "stats.h"
35 102 lampret
#include "spr_defs.h"
36
#include "sprs.h"
37 428 markom
#include "sim-config.h"
38 5 lampret
 
39
/* Data cache */
40
 
41
struct dc_set {
42 428 markom
  struct {
43 631 simons
    unsigned long line[MAX_DC_BLOCK_SIZE];
44 428 markom
    unsigned long tagaddr;  /* tag address */
45
    int lru;    /* least recently used */
46
  } way[MAX_DC_WAYS];
47
} dc[MAX_DC_SETS];
48 5 lampret
 
49
void dc_info()
50
{
51 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DCP)) {
52
    printf("DCache not implemented. Set UPR[DCP].\n");
53
    return;
54
  }
55
 
56
  printf("Data cache %dKB: ", config.dc.nsets * config.dc.blocksize * config.dc.nways / 1024);
57
  printf("%d ways, %d sets, block size %d bytes\n", config.dc.nways, config.dc.nsets, config.dc.blocksize);
58 5 lampret
}
59
 
60
/* First check if data is already in the cache and if it is:
61
    - increment DC read hit stats,
62 428 markom
    - set 'lru' at this way to config.dc.ustates - 1 and
63 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
64
   and if not:
65
    - increment DC read miss stats
66
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
67 428 markom
    - set 'lru' with config.dc.ustates - 1 and decrement 'lru' of other
68 5 lampret
      ways unless they have reached 0
69 631 simons
    - refill cache line
70 5 lampret
*/
71
 
72 631 simons
unsigned long dc_simulate_read(unsigned long dataaddr, int width)
73 5 lampret
{
74 428 markom
  int set, way = -1;
75
  int i;
76
  unsigned long tagaddr;
77 631 simons
  unsigned long tmp;
78 102 lampret
 
79 638 simons
  if ((!testsprbits(SPR_UPR, SPR_UPR_DCP)) ||
80
      (!testsprbits(SPR_SR, SPR_SR_DCE))   ||
81
      data_ci) {
82 631 simons
    if (width == 4)
83 992 simons
      tmp = evalsim_mem32(dataaddr);
84 631 simons
    else if (width == 2)
85 992 simons
      tmp = (unsigned long)evalsim_mem16(dataaddr);
86 631 simons
    else if (width == 1)
87 992 simons
      tmp = (unsigned long)evalsim_mem8(dataaddr);
88
 
89
    if(!cur_area) {
90
      if (width == 4)
91
        printf("EXCEPTION: read out of memory (32-bit access to %.8lx)\n", dataaddr);
92
      else if (width == 2)
93
        printf("EXCEPTION: read out of memory (16-bit access to %.8lx)\n", dataaddr);
94
      else if (width == 1)
95
        printf("EXCEPTION: read out of memory (8-bit access to %.8lx)\n", dataaddr);
96
      except_handle(EXCEPT_BUSERR, cur_vadd);
97
      return 0;
98
    }
99
 
100
    if (!pending.valid && cur_area->log)
101
      fprintf (cur_area->log, "[%08x] -> read %08x\n", dataaddr, tmp);
102
 
103
    return tmp;
104 631 simons
  }
105 102 lampret
 
106 428 markom
  /* Which set to check out? */
107
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
108
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
109
 
110
  /* Scan all ways and try to find a matching way. */
111
  for (i = 0; i < config.dc.nways; i++)
112
    if (dc[set].way[i].tagaddr == tagaddr)
113
      way = i;
114
 
115
  /* Did we find our cached data? */
116
  if (way >= 0) { /* Yes, we did. */
117
    dc_stats.readhit++;
118 541 markom
 
119 428 markom
    for (i = 0; i < config.dc.nways; i++)
120 631 simons
      if (dc[set].way[i].lru > dc[set].way[way].lru)
121 428 markom
        dc[set].way[i].lru--;
122
    dc[set].way[way].lru = config.dc.ustates - 1;
123 884 markom
    runtime.sim.mem_cycles += config.dc.load_hitdelay;
124 631 simons
 
125
    tmp = dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
126
    if (width == 4)
127
      return tmp;
128
    else if (width == 2) {
129
      tmp = (unsigned long)((tmp >> ((dataaddr & 2) ? 0 : 16)) & 0xffff);
130
      return tmp;
131
    }
132
    else if (width == 1) {
133
      tmp = (unsigned long)((tmp  >> (8 * (3 - (dataaddr & 3)))) & 0xff);
134
      return tmp;
135
    }
136 541 markom
  } else {  /* No, we didn't. */
137 428 markom
    int minlru = config.dc.ustates - 1;
138
    int minway = 0;
139 5 lampret
 
140 631 simons
    dc_stats.readmiss++;
141 5 lampret
 
142 428 markom
    for (i = 0; i < config.dc.nways; i++)
143 631 simons
      if (dc[set].way[i].lru < minlru)
144 428 markom
        minway = i;
145
 
146 631 simons
    for (i = 0; i < (config.dc.blocksize); i += 4) {
147
      dc[set].way[minway].line[((dataaddr + i) & (config.dc.blocksize - 1)) >> 2] =
148
        evalsim_mem32((dataaddr & ~(config.dc.blocksize - 1)) + (((dataaddr & ~3ul)+ i) & (config.dc.blocksize - 1)));
149 638 simons
      if(!cur_area) {
150
        dc[set].way[minway].tagaddr = -1;
151
        dc[set].way[minway].lru = 0;
152 992 simons
        printf("EXCEPTION: read out of memory (32-bit access to %.8lx)\n", dataaddr);
153
        except_handle(EXCEPT_BUSERR, cur_vadd);
154 631 simons
        return 0;
155 638 simons
      }
156 992 simons
      if (!pending.valid && cur_area->log)
157
        fprintf (cur_area->log, "[%08x] -> read %08x\n", dataaddr, tmp);
158 631 simons
    }
159
 
160 428 markom
    dc[set].way[minway].tagaddr = tagaddr;
161
    for (i = 0; i < config.dc.nways; i++)
162
      if (dc[set].way[i].lru)
163
        dc[set].way[i].lru--;
164
    dc[set].way[minway].lru = config.dc.ustates - 1;
165 884 markom
    runtime.sim.mem_cycles += config.dc.load_missdelay;
166 631 simons
 
167
    tmp = dc[set].way[minway].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
168
    if (width == 4)
169
      return tmp;
170
    else if (width == 2) {
171
      tmp = (unsigned long)((tmp >> ((dataaddr & 2) ? 0 : 16)) & 0xffff);
172
      return tmp;
173
    }
174
    else if (width == 1) {
175
      tmp = (unsigned long)((tmp  >> (8 * (3 - (dataaddr & 3)))) & 0xff);
176
      return tmp;
177
    }
178 428 markom
  }
179 5 lampret
}
180
 
181
/* First check if data is already in the cache and if it is:
182
    - increment DC write hit stats,
183 428 markom
    - set 'lru' at this way to config.dc.ustates - 1 and
184 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
185
   and if not:
186
    - increment DC write miss stats
187
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
188 428 markom
    - set 'lru' with config.dc.ustates - 1 and decrement 'lru' of other
189 5 lampret
      ways unless they have reached 0
190
*/
191
 
192 631 simons
void dc_simulate_write(unsigned long dataaddr, unsigned long data, int width)
193 5 lampret
{
194 428 markom
  int set, way = -1;
195
  int i;
196
  unsigned long tagaddr;
197 631 simons
  unsigned long tmp;
198 102 lampret
 
199 631 simons
  if (width == 4)
200
    setsim_mem32(dataaddr, data);
201
  else if (width == 2)
202
    setsim_mem16(dataaddr, (unsigned short)data);
203
  else if (width == 1)
204
    setsim_mem8(dataaddr, (unsigned char)data);
205
 
206 638 simons
  if ((!testsprbits(SPR_UPR, SPR_UPR_DCP)) ||
207
      (!testsprbits(SPR_SR, SPR_SR_DCE)) ||
208
      data_ci ||
209
      (!cur_area))
210 631 simons
    return;
211 428 markom
 
212
  /* Which set to check out? */
213
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
214
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
215
 
216
  /* Scan all ways and try to find a matching way. */
217
  for (i = 0; i < config.dc.nways; i++)
218
    if (dc[set].way[i].tagaddr == tagaddr)
219
      way = i;
220
 
221
  /* Did we find our cached data? */
222
  if (way >= 0) { /* Yes, we did. */
223
    dc_stats.writehit++;
224
 
225
    for (i = 0; i < config.dc.nways; i++)
226 631 simons
      if (dc[set].way[i].lru > dc[set].way[way].lru)
227 428 markom
        dc[set].way[i].lru--;
228
    dc[set].way[way].lru = config.dc.ustates - 1;
229 884 markom
    runtime.sim.mem_cycles += config.dc.store_hitdelay;
230 631 simons
 
231
    tmp = dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
232
    if (width == 4)
233
      tmp = data;
234
    else if (width == 2) {
235
      tmp &= 0xffff << ((dataaddr & 2) ? 16 : 0);
236
      tmp |= (unsigned long)(data & 0xffff) << ((dataaddr & 2) ? 0 : 16);
237
    }
238
    else if (width == 1) {
239
      tmp &= ~(0xff << (8 * (3 - (dataaddr & 3))));
240
      tmp |= (unsigned long)(data & 0xff) << (8 * (3 - (dataaddr & 3)));
241
    }
242
    dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2] = tmp;
243 428 markom
  }
244
  else {  /* No, we didn't. */
245
    int minlru = config.dc.ustates - 1;
246
    int minway = 0;
247 5 lampret
 
248 631 simons
    dc_stats.writemiss++;
249 5 lampret
 
250 428 markom
    for (i = 0; i < config.dc.nways; i++)
251 631 simons
      if (dc[set].way[i].lru < minlru)
252 428 markom
        minway = i;
253
 
254 631 simons
    for (i = 0; i < (config.dc.blocksize); i += 4) {
255
      dc[set].way[minway].line[((dataaddr + i) & (config.dc.blocksize - 1)) >> 2] =
256
        evalsim_mem32((dataaddr & ~(config.dc.blocksize - 1)) + (((dataaddr & ~3ul)+ i) & (config.dc.blocksize - 1)));
257 638 simons
      if(!cur_area) {
258
        dc[set].way[minway].tagaddr = -1;
259
        dc[set].way[minway].lru = 0;
260 631 simons
        return;
261 638 simons
      }
262 631 simons
    }
263
 
264 428 markom
    dc[set].way[minway].tagaddr = tagaddr;
265
    for (i = 0; i < config.dc.nways; i++)
266
      if (dc[set].way[i].lru)
267
        dc[set].way[i].lru--;
268
    dc[set].way[minway].lru = config.dc.ustates - 1;
269 884 markom
    runtime.sim.mem_cycles += config.dc.store_missdelay;
270 428 markom
  }
271 5 lampret
}
272 102 lampret
 
273
/* First check if data is already in the cache and if it is:
274
    - invalidate block if way isn't locked
275
   otherwise don't do anything.
276
*/
277
 
278
void dc_inv(unsigned long dataaddr)
279
{
280 428 markom
  int set, way = -1;
281
  int i;
282
  unsigned long tagaddr;
283 102 lampret
 
284 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DCP))
285
    return;
286 102 lampret
 
287 428 markom
  /* Which set to check out? */
288
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
289
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
290
 
291 631 simons
  if (!testsprbits(SPR_SR, SPR_SR_DCE)) {
292
    for (i = 0; i < config.dc.nways; i++) {
293
      dc[set].way[i].tagaddr = -1;
294
      dc[set].way[i].lru = 0;
295
    }
296
    return;
297
  }
298
   /* Scan all ways and try to find a matching way. */
299 428 markom
  for (i = 0; i < config.dc.nways; i++)
300
    if (dc[set].way[i].tagaddr == tagaddr)
301
      way = i;
302
 
303
  /* Did we find our cached data? */
304 631 simons
  if (way >= 0) { /* Yes, we did. */
305 428 markom
    dc[set].way[way].tagaddr = -1;
306 631 simons
    dc[set].way[way].lru = 0;
307 428 markom
  }
308 102 lampret
}
309
 
310 261 markom
inline void dc_clock()
311 102 lampret
{
312 428 markom
  unsigned long addr;
313
 
314
  if (addr = mfspr(SPR_DCBPR)) {
315 631 simons
    dc_simulate_read(addr, 4);
316 428 markom
    mtspr(SPR_DCBPR, 0);
317
  }
318 631 simons
  if ((addr = mfspr(SPR_DCBFR)) != -1) {
319 428 markom
    dc_inv(addr);
320 631 simons
    mtspr(SPR_DCBFR, -1);
321 428 markom
  }
322
  if (addr = mfspr(SPR_DCBIR)) {
323
    dc_inv(addr);
324
    mtspr(SPR_DCBIR, 0);
325
  }
326
  if (addr = mfspr(SPR_DCBWR)) {
327
    mtspr(SPR_DCBWR, 0);
328
  }
329
  if (addr = mfspr(SPR_DCBLR)) {
330
    mtspr(SPR_DCBLR, 0);
331
  }
332 102 lampret
}

powered by: WebSVN 2.1.0

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