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

Subversion Repositories or1k

[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 261

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

powered by: WebSVN 2.1.0

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