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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_34/] [or1ksim/] [cache/] [icache_model.c] - Diff between revs 102 and 110

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 102 Rev 110
/* icache_model.c -- instruction cache simulation
/* icache_model.c -- instruction cache simulation
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
 
 
This file is part of OpenRISC 1000 Architectural Simulator.
This file is part of OpenRISC 1000 Architectural Simulator.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 
/* Cache functions.
/* Cache functions.
   At the moment this functions only simulate functionality of instruction
   At the moment this functions only simulate functionality of instruction
   caches and do not influence on fetche/decode/execute stages and timings.
   caches and do not influence on fetche/decode/execute stages and timings.
   They are here only to verify performance of various cache configurations.
   They are here only to verify performance of various cache configurations.
 */
 */
 
 
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <errno.h>
#include <stdarg.h>
#include <stdarg.h>
 
 
#include "icache_model.h"
#include "icache_model.h"
#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"
 
 
/* Instruction cache */
/* Instruction cache */
 
 
/* Number of IC sets (power of 2) */
/* Number of IC sets (power of 2) */
#define IC_SETS 512
#define IC_SETS 512
 
 
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
#define IC_BLOCK_SIZE 16
#define IC_BLOCK_SIZE 16
 
 
/* Number of IC ways (1, 2, 3 etc.). */
/* Number of IC ways (1, 2, 3 etc.). */
#define IC_WAYS 1
#define IC_WAYS 1
 
 
/* Number of usage states (2, 3, 4 etc.). */
/* Number of usage states (2, 3, 4 etc.). */
#define IC_USTATES 2
#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[IC_WAYS];
} ic[IC_SETS];
} ic[IC_SETS];
 
 
void ic_info()
void ic_info()
{
{
        if (!getsprbits(SPR_UPR, SPR_UPR_ICP)) {
        if (!getsprbits(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: ", IC_SETS * IC_BLOCK_SIZE * IC_WAYS / 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", IC_WAYS, IC_SETS, IC_BLOCK_SIZE);
}
}
 
 
/* 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 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 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)
{
{
        int set, way = -1;
        int set, way = -1;
        int i;
        int i;
        unsigned long tagaddr;
        unsigned long tagaddr;
 
 
        /* ICache simulation enabled/disabled. */
        /* ICache simulation enabled/disabled. */
        if ((!getsprbits(SPR_UPR, SPR_UPR_ICP)) || (!getsprbits(SPR_SR, SPR_SR_ICE)))
        if ((!getsprbits(SPR_UPR, SPR_UPR_ICP)) || (!getsprbits(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 / IC_BLOCK_SIZE) % IC_SETS;
        tagaddr = (fetchaddr / IC_BLOCK_SIZE) / IC_SETS;
        tagaddr = (fetchaddr / IC_BLOCK_SIZE) / IC_SETS;
 
 
        /* 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 < IC_WAYS; 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 < IC_WAYS; 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 = IC_USTATES - 1;
        }
        }
        else {  /* No, we didn't. */
        else {  /* No, we didn't. */
                int minlru = IC_USTATES - 1;
                int minlru = 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 < IC_WAYS; 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 < IC_WAYS; 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 = 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
   otherwise don't do anything.
   otherwise don't do anything.
*/
*/
 
 
void ic_inv(unsigned long dataaddr)
void ic_inv(unsigned long dataaddr)
{
{
        int set, way = -1;
        int set, way = -1;
        int i;
        int i;
        unsigned long tagaddr;
        unsigned long tagaddr;
 
 
        if (!getsprbits(SPR_UPR, SPR_UPR_ICP))
        if (!getsprbits(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 / IC_BLOCK_SIZE) % IC_SETS;
        tagaddr = (dataaddr / IC_BLOCK_SIZE) / IC_SETS;
        tagaddr = (dataaddr / IC_BLOCK_SIZE) / IC_SETS;
 
 
        /* 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 < IC_WAYS; 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. */
                ic[set].way[way].tagaddr = -1;
                ic[set].way[way].tagaddr = -1;
        }
        }
}
}
 
 
void ic_clock()
void ic_clock()
{
{
        unsigned long addr;
        unsigned long addr;
 
 
        if (addr = mfspr(SPR_ICBPR)) {
        if (addr = mfspr(SPR_ICBPR)) {
                ic_simulate_read(addr);
                ic_simulate_fetch(addr);
                mtspr(SPR_ICBPR, 0);
                mtspr(SPR_ICBPR, 0);
        }
        }
        if (addr = mfspr(SPR_ICBIR)) {
        if (addr = mfspr(SPR_ICBIR)) {
                ic_inv(addr);
                ic_inv(addr);
                mtspr(SPR_ICBIR, 0);
                mtspr(SPR_ICBIR, 0);
        }
        }
        if (addr = mfspr(SPR_ICBLR)) {
        if (addr = mfspr(SPR_ICBLR)) {
                mtspr(SPR_ICBLR, 0);
                mtspr(SPR_ICBLR, 0);
        }
        }
}
}
 
 

powered by: WebSVN 2.1.0

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