Line 32... |
Line 32... |
#include "abstract.h"
|
#include "abstract.h"
|
#include "stats.h"
|
#include "stats.h"
|
#include "sim-config.h"
|
#include "sim-config.h"
|
#include "spr_defs.h"
|
#include "spr_defs.h"
|
#include "sprs.h"
|
#include "sprs.h"
|
|
#include "sim-config.h"
|
|
|
/* Instruction cache */
|
/* Instruction cache */
|
|
#define MAX_IC_SETS 8192
|
/* Number of IC sets (power of 2) */
|
#define MAX_IC_WAYS 4
|
#define IC_SETS 512
|
|
|
|
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
|
|
#define IC_BLOCK_SIZE 16
|
|
|
|
/* Number of IC ways (1, 2, 3 etc.). */
|
|
#define IC_WAYS 1
|
|
|
|
/* Number of usage states (2, 3, 4 etc.). */
|
|
#define IC_USTATES 2
|
|
|
|
struct ic_set {
|
struct ic_set {
|
struct {
|
struct {
|
unsigned long tagaddr; /* tag address */
|
unsigned long tagaddr; /* tag address */
|
int lru; /* least recently used */
|
int lru; /* least recently used */
|
} way[IC_WAYS];
|
} way[MAX_IC_WAYS];
|
} ic[IC_SETS];
|
} ic[MAX_IC_SETS];
|
|
|
void ic_info()
|
void ic_info()
|
{
|
{
|
if (!testsprbits(SPR_UPR, SPR_UPR_ICP)) {
|
if (!testsprbits(SPR_UPR, SPR_UPR_ICP)) {
|
printf("ICache not implemented. Set UPR[ICP].\n");
|
printf("ICache not implemented. Set UPR[ICP].\n");
|
return;
|
return;
|
}
|
}
|
|
|
printf("Instruction cache %dKB: ", IC_SETS * IC_BLOCK_SIZE * IC_WAYS / 1024);
|
printf("Instruction cache %dKB: ", config.ic.nsets * config.ic.blocksize * config.ic.nways / 1024);
|
printf("%d ways, %d sets, block size %d bytes\n", IC_WAYS, IC_SETS, IC_BLOCK_SIZE);
|
printf("%d ways, %d sets, block size %d bytes\n", config.ic.nways, config.ic.nsets, config.ic.blocksize);
|
}
|
}
|
|
|
/* First check if instruction is already in the cache and if it is:
|
/* First check if instruction is already in the cache and if it is:
|
- increment IC read hit stats,
|
- increment IC read hit stats,
|
- set 'lru' at this way to IC_USTATES - 1 and
|
- set 'lru' at this way to config.ic.ustates - 1 and
|
decrement 'lru' of other ways unless they have reached 0,
|
decrement 'lru' of other ways unless they have reached 0,
|
and if not:
|
and if not:
|
- increment IC read miss stats
|
- increment IC read miss stats
|
- find lru way and entry and replace old tag with tag of the 'fetchaddr'
|
- find lru way and entry and replace old tag with tag of the 'fetchaddr'
|
- set 'lru' with IC_USTATES - 1 and decrement 'lru' of other
|
- set 'lru' with config.ic.ustates - 1 and decrement 'lru' of other
|
ways unless they have reached 0
|
ways unless they have reached 0
|
*/
|
*/
|
|
|
void ic_simulate_fetch(unsigned long fetchaddr)
|
void ic_simulate_fetch(unsigned long fetchaddr)
|
{
|
{
|
Line 87... |
Line 78... |
/* ICache simulation enabled/disabled. */
|
/* ICache simulation enabled/disabled. */
|
if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)))
|
if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)))
|
return;
|
return;
|
|
|
/* Which set to check out? */
|
/* Which set to check out? */
|
set = (fetchaddr / IC_BLOCK_SIZE) % IC_SETS;
|
set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
|
tagaddr = (fetchaddr / IC_BLOCK_SIZE) / IC_SETS;
|
tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
|
|
|
/* Scan all ways and try to find a matching way. */
|
/* Scan all ways and try to find a matching way. */
|
for (i = 0; i < IC_WAYS; i++)
|
for (i = 0; i < config.ic.nways; i++)
|
if (ic[set].way[i].tagaddr == tagaddr)
|
if (ic[set].way[i].tagaddr == tagaddr)
|
way = i;
|
way = i;
|
|
|
/* Did we find our cached instruction? */
|
/* Did we find our cached instruction? */
|
if (way >= 0) { /* Yes, we did. */
|
if (way >= 0) { /* Yes, we did. */
|
ic_stats.readhit++;
|
ic_stats.readhit++;
|
|
|
for (i = 0; i < IC_WAYS; i++)
|
for (i = 0; i < config.ic.nways; i++)
|
if (ic[set].way[i].lru)
|
if (ic[set].way[i].lru)
|
ic[set].way[i].lru--;
|
ic[set].way[i].lru--;
|
ic[set].way[way].lru = IC_USTATES - 1;
|
ic[set].way[way].lru = config.ic.ustates - 1;
|
}
|
}
|
else { /* No, we didn't. */
|
else { /* No, we didn't. */
|
int minlru = IC_USTATES - 1;
|
int minlru = config.ic.ustates - 1;
|
int minway = 0;
|
int minway = 0;
|
|
|
ic_stats.readmiss++;
|
ic_stats.readmiss++;
|
|
|
for (i = 0; i < IC_WAYS; i++)
|
for (i = 0; i < config.ic.nways; i++)
|
if (ic[set].way[i].lru < minlru)
|
if (ic[set].way[i].lru < minlru)
|
minway = i;
|
minway = i;
|
|
|
ic[set].way[minway].tagaddr = tagaddr;
|
ic[set].way[minway].tagaddr = tagaddr;
|
for (i = 0; i < IC_WAYS; i++)
|
for (i = 0; i < config.ic.nways; i++)
|
if ((ic[set].way[i].lru) &&
|
if ((ic[set].way[i].lru) &&
|
(getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << i)))
|
(getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << i)))
|
ic[set].way[i].lru--;
|
ic[set].way[i].lru--;
|
ic[set].way[minway].lru = IC_USTATES - 1;
|
ic[set].way[minway].lru = config.ic.ustates - 1;
|
}
|
}
|
}
|
}
|
|
|
/* First check if data is already in the cache and if it is:
|
/* First check if data is already in the cache and if it is:
|
- invalidate block if way isn't locked
|
- invalidate block if way isn't locked
|
Line 138... |
Line 129... |
|
|
if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
|
if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
|
return;
|
return;
|
|
|
/* Which set to check out? */
|
/* Which set to check out? */
|
set = (dataaddr / IC_BLOCK_SIZE) % IC_SETS;
|
set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
|
tagaddr = (dataaddr / IC_BLOCK_SIZE) / IC_SETS;
|
tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
|
|
|
/* Scan all ways and try to find a matching way. */
|
/* Scan all ways and try to find a matching way. */
|
for (i = 0; i < IC_WAYS; i++)
|
for (i = 0; i < config.ic.nways; i++)
|
if (ic[set].way[i].tagaddr == tagaddr)
|
if (ic[set].way[i].tagaddr == tagaddr)
|
way = i;
|
way = i;
|
|
|
/* Did we find our cached data? */
|
/* Did we find our cached data? */
|
if ((way >= 0) && (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << way))) { /* Yes, we did. */
|
if ((way >= 0) && (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << way))) { /* Yes, we did. */
|