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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 102

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
#include "icache_model.h"
32
#include "abstract.h"
33
#include "stats.h"
34 102 lampret
#include "sim-config.h"
35
#include "spr_defs.h"
36 5 lampret
 
37
/* Instruction cache */
38
 
39
/* Number of IC sets (power of 2) */
40 76 lampret
#define IC_SETS 512
41 5 lampret
 
42
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
43 26 lampret
#define IC_BLOCK_SIZE 16
44 5 lampret
 
45
/* Number of IC ways (1, 2, 3 etc.). */
46
#define IC_WAYS 1
47
 
48
/* Number of usage states (2, 3, 4 etc.). */
49
#define IC_USTATES 2
50
 
51
struct ic_set {
52
        struct {
53
                unsigned long tagaddr;  /* tag address */
54
                int lru;                /* least recently used */
55
        } way[IC_WAYS];
56
} ic[IC_SETS];
57
 
58
void ic_info()
59
{
60 102 lampret
        if (!getsprbits(SPR_UPR, SPR_UPR_ICP)) {
61
                        printf("ICache not implemented. Set UPR[ICP].\n");
62
                        return;
63
        }
64
 
65 5 lampret
        printf("Instruction cache %dKB: ", IC_SETS * IC_BLOCK_SIZE * IC_WAYS / 1024);
66
        printf("%d ways, %d sets, block size %d bytes\n", IC_WAYS, IC_SETS, IC_BLOCK_SIZE);
67
}
68
 
69
/* First check if instruction is already in the cache and if it is:
70
    - increment IC read hit stats,
71
    - set 'lru' at this way to IC_USTATES - 1 and
72
      decrement 'lru' of other ways unless they have reached 0,
73
   and if not:
74
    - increment IC read miss stats
75
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
76
    - set 'lru' with IC_USTATES - 1 and decrement 'lru' of other
77
      ways unless they have reached 0
78
*/
79
 
80 76 lampret
void ic_simulate_fetch(unsigned long fetchaddr)
81 5 lampret
{
82
        int set, way = -1;
83
        int i;
84
        unsigned long tagaddr;
85 102 lampret
 
86
        /* ICache simulation enabled/disabled. */
87
        if ((!getsprbits(SPR_UPR, SPR_UPR_ICP)) || (!getsprbits(SPR_SR, SPR_SR_ICE)))
88
                return;
89 5 lampret
 
90
        /* Which set to check out? */
91
        set = (fetchaddr / IC_BLOCK_SIZE) % IC_SETS;
92
        tagaddr = (fetchaddr / IC_BLOCK_SIZE) / IC_SETS;
93
 
94
        /* Scan all ways and try to find a matching way. */
95
        for (i = 0; i < IC_WAYS; i++)
96
                if (ic[set].way[i].tagaddr == tagaddr)
97
                        way = i;
98
 
99
        /* Did we find our cached instruction? */
100
        if (way >= 0) { /* Yes, we did. */
101
                ic_stats.readhit++;
102
 
103
                for (i = 0; i < IC_WAYS; i++)
104
                        if (ic[set].way[i].lru)
105
                                ic[set].way[i].lru--;
106
                ic[set].way[way].lru = IC_USTATES - 1;
107
        }
108
        else {  /* No, we didn't. */
109
                int minlru = IC_USTATES - 1;
110
                int minway = 0;
111
 
112
                ic_stats.readmiss++;
113
 
114
                for (i = 0; i < IC_WAYS; i++)
115
                        if (ic[set].way[i].lru < minlru)
116
                                minway = i;
117
 
118
                ic[set].way[minway].tagaddr = tagaddr;
119
                for (i = 0; i < IC_WAYS; i++)
120 102 lampret
                        if ((ic[set].way[i].lru) &&
121
                            (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << i)))
122 5 lampret
                                ic[set].way[i].lru--;
123
                ic[set].way[minway].lru = IC_USTATES - 1;
124
        }
125
}
126 102 lampret
 
127
/* First check if data is already in the cache and if it is:
128
    - invalidate block if way isn't locked
129
   otherwise don't do anything.
130
*/
131
 
132
void ic_inv(unsigned long dataaddr)
133
{
134
        int set, way = -1;
135
        int i;
136
        unsigned long tagaddr;
137
 
138
        if (!getsprbits(SPR_UPR, SPR_UPR_ICP))
139
                return;
140
 
141
        /* Which set to check out? */
142
        set = (dataaddr / IC_BLOCK_SIZE) % IC_SETS;
143
        tagaddr = (dataaddr / IC_BLOCK_SIZE) / IC_SETS;
144
 
145
        /* Scan all ways and try to find a matching way. */
146
        for (i = 0; i < IC_WAYS; i++)
147
                if (ic[set].way[i].tagaddr == tagaddr)
148
                        way = i;
149
 
150
        /* Did we find our cached data? */
151
        if ((way >= 0) && (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << way))) { /* Yes, we did. */
152
                ic[set].way[way].tagaddr = -1;
153
        }
154
}
155
 
156
void ic_clock()
157
{
158
        unsigned long addr;
159
 
160
        if (addr = mfspr(SPR_ICBPR)) {
161
                ic_simulate_read(addr);
162
                mtspr(SPR_ICBPR, 0);
163
        }
164
        if (addr = mfspr(SPR_ICBIR)) {
165
                ic_inv(addr);
166
                mtspr(SPR_ICBIR, 0);
167
        }
168
        if (addr = mfspr(SPR_ICBLR)) {
169
                mtspr(SPR_ICBLR, 0);
170
        }
171
}

powered by: WebSVN 2.1.0

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