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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [cache/] [dcache_model.c] - Blame information for rev 1555

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 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 5 lampret
#include "dcache_model.h"
40
#include "abstract.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 "sprs.h"
47 428 markom
#include "sim-config.h"
48 1555 nogj
#include "misc.h"
49 5 lampret
 
50
/* Data cache */
51
 
52
struct dc_set {
53 428 markom
  struct {
54 1350 nogj
    uint32_t line[MAX_DC_BLOCK_SIZE];
55
    oraddr_t tagaddr;  /* tag address */
56 428 markom
    int lru;    /* least recently used */
57
  } way[MAX_DC_WAYS];
58
} dc[MAX_DC_SETS];
59 5 lampret
 
60 1506 nogj
void dc_info(void)
61 5 lampret
{
62 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_DCP)) {
63 997 markom
    PRINTF("DCache not implemented. Set UPR[DCP].\n");
64 428 markom
    return;
65
  }
66
 
67 997 markom
  PRINTF("Data cache %dKB: ", config.dc.nsets * config.dc.blocksize * config.dc.nways / 1024);
68
  PRINTF("%d ways, %d sets, block size %d bytes\n", config.dc.nways, config.dc.nsets, config.dc.blocksize);
69 5 lampret
}
70
 
71
/* First check if data is already in the cache and if it is:
72
    - increment DC read hit stats,
73 428 markom
    - set 'lru' at this way to config.dc.ustates - 1 and
74 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
75
   and if not:
76
    - increment DC read miss stats
77
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
78 428 markom
    - set 'lru' with config.dc.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 dc_simulate_read(oraddr_t dataaddr, oraddr_t virt_addr, int width)
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 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_DCP) ||
91
      !(cpu_state.sprs[SPR_SR] & SPR_SR_DCE)   ||
92 638 simons
      data_ci) {
93 631 simons
    if (width == 4)
94 1486 nogj
      tmp = evalsim_mem32(dataaddr, virt_addr);
95 631 simons
    else if (width == 2)
96 1486 nogj
      tmp = evalsim_mem16(dataaddr, virt_addr);
97 631 simons
    else if (width == 1)
98 1486 nogj
      tmp = evalsim_mem8(dataaddr, virt_addr);
99 992 simons
 
100 1486 nogj
    if (cur_area && cur_area->log)
101 1350 nogj
      fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n", dataaddr,
102
               tmp);
103 992 simons
 
104
    return tmp;
105 631 simons
  }
106 102 lampret
 
107 428 markom
  /* Which set to check out? */
108
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
109
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
110
 
111
  /* Scan all ways and try to find a matching way. */
112
  for (i = 0; i < config.dc.nways; i++)
113
    if (dc[set].way[i].tagaddr == tagaddr)
114
      way = i;
115
 
116
  /* Did we find our cached data? */
117
  if (way >= 0) { /* Yes, we did. */
118
    dc_stats.readhit++;
119 541 markom
 
120 428 markom
    for (i = 0; i < config.dc.nways; i++)
121 631 simons
      if (dc[set].way[i].lru > dc[set].way[way].lru)
122 428 markom
        dc[set].way[i].lru--;
123
    dc[set].way[way].lru = config.dc.ustates - 1;
124 884 markom
    runtime.sim.mem_cycles += config.dc.load_hitdelay;
125 631 simons
 
126
    tmp = dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
127
    if (width == 4)
128
      return tmp;
129
    else if (width == 2) {
130 1350 nogj
      tmp = ((tmp >> ((dataaddr & 2) ? 0 : 16)) & 0xffff);
131 631 simons
      return tmp;
132
    }
133
    else if (width == 1) {
134 1350 nogj
      tmp = ((tmp  >> (8 * (3 - (dataaddr & 3)))) & 0xff);
135 631 simons
      return tmp;
136
    }
137 541 markom
  } else {  /* No, we didn't. */
138 428 markom
    int minlru = config.dc.ustates - 1;
139
    int minway = 0;
140 5 lampret
 
141 631 simons
    dc_stats.readmiss++;
142 5 lampret
 
143 1085 simons
    for (i = 0; i < config.dc.nways; i++) {
144
      if (dc[set].way[i].lru < minlru) {
145 428 markom
        minway = i;
146 1085 simons
        minlru = dc[set].way[i].lru;
147
      }
148
    }
149 428 markom
 
150 631 simons
    for (i = 0; i < (config.dc.blocksize); i += 4) {
151
      dc[set].way[minway].line[((dataaddr + i) & (config.dc.blocksize - 1)) >> 2] =
152 1486 nogj
        /* FIXME: What is the virtual address meant to be? (ie. What happens if
153
         * we read out of memory while refilling a cache line?) */
154
        evalsim_mem32((dataaddr & ~(config.dc.blocksize - 1)) + (((dataaddr & ~ADDR_C(3)) + i) & (config.dc.blocksize - 1)), 0);
155 638 simons
      if(!cur_area) {
156
        dc[set].way[minway].tagaddr = -1;
157
        dc[set].way[minway].lru = 0;
158 631 simons
        return 0;
159 1386 nogj
      } else if (cur_area->log)
160 1350 nogj
        fprintf (cur_area->log, "[%"PRIxADDR"] -> read %08"PRIx32"\n", dataaddr,
161
                 tmp);
162 631 simons
    }
163
 
164 428 markom
    dc[set].way[minway].tagaddr = tagaddr;
165
    for (i = 0; i < config.dc.nways; i++)
166
      if (dc[set].way[i].lru)
167
        dc[set].way[i].lru--;
168
    dc[set].way[minway].lru = config.dc.ustates - 1;
169 884 markom
    runtime.sim.mem_cycles += config.dc.load_missdelay;
170 631 simons
 
171
    tmp = dc[set].way[minway].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
172
    if (width == 4)
173
      return tmp;
174
    else if (width == 2) {
175 1350 nogj
      tmp = (tmp >> ((dataaddr & 2) ? 0 : 16)) & 0xffff;
176 631 simons
      return tmp;
177
    }
178
    else if (width == 1) {
179 1350 nogj
      tmp = (tmp  >> (8 * (3 - (dataaddr & 3)))) & 0xff;
180 631 simons
      return tmp;
181
    }
182 428 markom
  }
183 5 lampret
}
184
 
185
/* First check if data is already in the cache and if it is:
186
    - increment DC write hit stats,
187 428 markom
    - set 'lru' at this way to config.dc.ustates - 1 and
188 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
189
   and if not:
190
    - increment DC write miss stats
191
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
192 428 markom
    - set 'lru' with config.dc.ustates - 1 and decrement 'lru' of other
193 5 lampret
      ways unless they have reached 0
194
*/
195
 
196 1486 nogj
void dc_simulate_write(oraddr_t dataaddr, oraddr_t virt_addr, uint32_t data,
197
                       int width)
198 5 lampret
{
199 428 markom
  int set, way = -1;
200
  int i;
201 1350 nogj
  oraddr_t tagaddr;
202
  uint32_t tmp;
203 102 lampret
 
204 631 simons
  if (width == 4)
205 1486 nogj
    setsim_mem32(dataaddr, virt_addr, data);
206 631 simons
  else if (width == 2)
207 1486 nogj
    setsim_mem16(dataaddr, virt_addr, data);
208 631 simons
  else if (width == 1)
209 1486 nogj
    setsim_mem8(dataaddr, virt_addr, data);
210 631 simons
 
211 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_DCP) ||
212
      !(cpu_state.sprs[SPR_SR] & SPR_SR_DCE) ||
213
      data_ci || !cur_area)
214 631 simons
    return;
215 428 markom
 
216
  /* Which set to check out? */
217
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
218
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
219
 
220
  /* Scan all ways and try to find a matching way. */
221
  for (i = 0; i < config.dc.nways; i++)
222
    if (dc[set].way[i].tagaddr == tagaddr)
223
      way = i;
224
 
225
  /* Did we find our cached data? */
226
  if (way >= 0) { /* Yes, we did. */
227
    dc_stats.writehit++;
228
 
229
    for (i = 0; i < config.dc.nways; i++)
230 631 simons
      if (dc[set].way[i].lru > dc[set].way[way].lru)
231 428 markom
        dc[set].way[i].lru--;
232
    dc[set].way[way].lru = config.dc.ustates - 1;
233 884 markom
    runtime.sim.mem_cycles += config.dc.store_hitdelay;
234 631 simons
 
235
    tmp = dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2];
236
    if (width == 4)
237
      tmp = data;
238
    else if (width == 2) {
239
      tmp &= 0xffff << ((dataaddr & 2) ? 16 : 0);
240 1350 nogj
      tmp |= (data & 0xffff) << ((dataaddr & 2) ? 0 : 16);
241 631 simons
    }
242
    else if (width == 1) {
243
      tmp &= ~(0xff << (8 * (3 - (dataaddr & 3))));
244 1350 nogj
      tmp |= (data & 0xff) << (8 * (3 - (dataaddr & 3)));
245 631 simons
    }
246
    dc[set].way[way].line[(dataaddr & (config.dc.blocksize - 1)) >> 2] = tmp;
247 428 markom
  }
248
  else {  /* No, we didn't. */
249
    int minlru = config.dc.ustates - 1;
250
    int minway = 0;
251 5 lampret
 
252 631 simons
    dc_stats.writemiss++;
253 5 lampret
 
254 428 markom
    for (i = 0; i < config.dc.nways; i++)
255 631 simons
      if (dc[set].way[i].lru < minlru)
256 428 markom
        minway = i;
257
 
258 631 simons
    for (i = 0; i < (config.dc.blocksize); i += 4) {
259
      dc[set].way[minway].line[((dataaddr + i) & (config.dc.blocksize - 1)) >> 2] =
260 1486 nogj
        /* FIXME: Same comment as in dc_simulate_read */
261
        evalsim_mem32((dataaddr & ~(config.dc.blocksize - 1)) + (((dataaddr & ~3ul)+ i) & (config.dc.blocksize - 1)), 0);
262 638 simons
      if(!cur_area) {
263
        dc[set].way[minway].tagaddr = -1;
264
        dc[set].way[minway].lru = 0;
265 631 simons
        return;
266 638 simons
      }
267 631 simons
    }
268
 
269 428 markom
    dc[set].way[minway].tagaddr = tagaddr;
270
    for (i = 0; i < config.dc.nways; i++)
271
      if (dc[set].way[i].lru)
272
        dc[set].way[i].lru--;
273
    dc[set].way[minway].lru = config.dc.ustates - 1;
274 884 markom
    runtime.sim.mem_cycles += config.dc.store_missdelay;
275 428 markom
  }
276 5 lampret
}
277 102 lampret
 
278
/* First check if data is already in the cache and if it is:
279
    - invalidate block if way isn't locked
280
   otherwise don't do anything.
281
*/
282
 
283 1350 nogj
void dc_inv(oraddr_t dataaddr)
284 102 lampret
{
285 428 markom
  int set, way = -1;
286
  int i;
287 1350 nogj
  oraddr_t tagaddr;
288 102 lampret
 
289 1506 nogj
  if (!(cpu_state.sprs[SPR_UPR] & SPR_UPR_DCP))
290 428 markom
    return;
291 102 lampret
 
292 428 markom
  /* Which set to check out? */
293
  set = (dataaddr / config.dc.blocksize) % config.dc.nsets;
294
  tagaddr = (dataaddr / config.dc.blocksize) / config.dc.nsets;
295
 
296 1506 nogj
  if (!(cpu_state.sprs[SPR_SR] & SPR_SR_DCE)) {
297 631 simons
    for (i = 0; i < config.dc.nways; i++) {
298
      dc[set].way[i].tagaddr = -1;
299
      dc[set].way[i].lru = 0;
300
    }
301
    return;
302
  }
303
   /* Scan all ways and try to find a matching way. */
304 428 markom
  for (i = 0; i < config.dc.nways; i++)
305
    if (dc[set].way[i].tagaddr == tagaddr)
306
      way = i;
307
 
308
  /* Did we find our cached data? */
309 631 simons
  if (way >= 0) { /* Yes, we did. */
310 428 markom
    dc[set].way[way].tagaddr = -1;
311 631 simons
    dc[set].way[way].lru = 0;
312 428 markom
  }
313 102 lampret
}
314
 
315 1358 nogj
/*-----------------------------------------------------[ DC configuration ]---*/
316
void dc_enabled(union param_val val, void *dat)
317
{
318
  config.dc.enabled = val.int_val;
319 1506 nogj
  if(val.int_val)
320
    cpu_state.sprs[SPR_UPR] |= SPR_UPR_DCP;
321
  else
322
    cpu_state.sprs[SPR_UPR] &= ~SPR_UPR_DCP;
323 1358 nogj
}
324
 
325
void dc_nsets(union param_val val, void *dat)
326
{
327 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_DC_SETS){
328 1358 nogj
    config.dc.nsets = val.int_val;
329 1506 nogj
    cpu_state.sprs[SPR_DCCFGR] &= ~SPR_DCCFGR_NCS;
330 1555 nogj
    cpu_state.sprs[SPR_DCCFGR] |= log2_int(val.int_val) << 3;
331 1382 nogj
  }
332 1358 nogj
  else {
333
    char tmp[200];
334
    sprintf (tmp, "value of power of two and lower or equal than %i expected.", MAX_DC_SETS);
335
    CONFIG_ERROR(tmp);
336
  }
337
}
338
 
339
void dc_nways(union param_val val, void *dat)
340
{
341 1382 nogj
  if (is_power2(val.int_val) && val.int_val <= MAX_DC_WAYS){
342 1358 nogj
    config.dc.nways = val.int_val;
343 1506 nogj
    cpu_state.sprs[SPR_DCCFGR] &= ~SPR_DCCFGR_NCW;
344 1555 nogj
    cpu_state.sprs[SPR_DCCFGR] |= log2_int(val.int_val);
345 1382 nogj
  }
346
  else{
347
    char tmp[200];
348
    sprintf (tmp, "value of power of two and lower or equal than %i expected.",
349
    MAX_DC_WAYS);
350
    CONFIG_ERROR(tmp);
351
  }
352 1358 nogj
}
353
 
354
void dc_blocksize(union param_val val, void *dat)
355
{
356 1382 nogj
  if (is_power2(val.int_val)) {
357 1358 nogj
    config.dc.blocksize = val.int_val;
358 1506 nogj
    cpu_state.sprs[SPR_ICCFGR] &= ~SPR_ICCFGR_CBS;
359 1555 nogj
    cpu_state.sprs[SPR_ICCFGR] |= log2_int(val.int_val) << 7;
360 1382 nogj
  } else
361 1358 nogj
    CONFIG_ERROR("value of power of two expected.");
362
}
363
 
364
void dc_ustates(union param_val val, void *dat)
365
{
366
  if (val.int_val >= 2 && val.int_val <= 4)
367
    config.dc.ustates = val.int_val;
368
  else
369
    CONFIG_ERROR("invalid USTATE.");
370
}
371
 
372
void dc_load_missdelay(union param_val val, void *dat)
373
{
374
  config.dc.load_missdelay = val.int_val;
375
}
376
 
377
void dc_load_hitdelay(union param_val val, void *dat)
378
{
379
  config.dc.load_hitdelay = val.int_val;
380
}
381
 
382
void dc_store_missdelay(union param_val val, void *dat)
383
{
384
  config.dc.store_missdelay = val.int_val;
385
}
386
 
387
void dc_store_hitdelay(union param_val val, void *dat)
388
{
389
  config.dc.store_hitdelay = val.int_val;
390
}
391
 
392
void reg_dc_sec(void)
393
{
394
  struct config_section *sec = reg_config_sec("dc", NULL, NULL);
395
 
396
  reg_config_param(sec, "enabled", paramt_int, dc_enabled);
397
  reg_config_param(sec, "nsets", paramt_int, dc_nsets);
398
  reg_config_param(sec, "nways", paramt_int, dc_nways);
399
  reg_config_param(sec, "blocksize", paramt_int, dc_blocksize);
400
  reg_config_param(sec, "ustates", paramt_int, dc_ustates);
401
  reg_config_param(sec, "load_missdelay", paramt_int, dc_load_missdelay);
402
  reg_config_param(sec, "load_hitdelay", paramt_int, dc_load_hitdelay);
403
  reg_config_param(sec, "store_missdelay", paramt_int, dc_store_missdelay);
404
  reg_config_param(sec, "store_hitdelay", paramt_int, dc_store_hitdelay);
405
}

powered by: WebSVN 2.1.0

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