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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_49/] [or1ksim/] [cache/] [dcache_model.c] - Blame information for rev 26

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
#include "dcache_model.h"
32
#include "abstract.h"
33
#include "stats.h"
34
 
35
/* Data cache */
36
 
37
/* Number of DC sets (power of 2) */
38 26 lampret
#define DC_SETS 128
39 5 lampret
 
40
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
41 26 lampret
#define DC_BLOCK_SIZE 16
42 5 lampret
 
43
/* Number of DC ways (1, 2, 3 etc.). */
44
#define DC_WAYS 1
45
 
46
/* Number of usage states (2, 3, 4 etc.). */
47
#define DC_USTATES 2
48
 
49
struct dc_set {
50
        struct {
51
                unsigned long tagaddr;  /* tag address */
52
                int lru;                /* least recently used */
53
        } way[DC_WAYS];
54
} dc[DC_SETS];
55
 
56
void dc_info()
57
{
58
        printf("Data cache %dKB: ", DC_SETS * DC_BLOCK_SIZE * DC_WAYS / 1024);
59
        printf("%d ways, %d sets, block size %d bytes\n", DC_WAYS, DC_SETS, DC_BLOCK_SIZE);
60
}
61
 
62
/* First check if data is already in the cache and if it is:
63
    - increment DC read hit stats,
64
    - set 'lru' at this way to DC_USTATES - 1 and
65
      decrement 'lru' of other ways unless they have reached 0,
66
   and if not:
67
    - increment DC read miss stats
68
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
69
    - set 'lru' with DC_USTATES - 1 and decrement 'lru' of other
70
      ways unless they have reached 0
71
*/
72
 
73
void dc_simulate_read(unsigned long dataaddr)
74
{
75
        int set, way = -1;
76
        int i;
77
        unsigned long tagaddr;
78
 
79
        /* Which set to check out? */
80
        set = (dataaddr / DC_BLOCK_SIZE) % DC_SETS;
81
        tagaddr = (dataaddr / DC_BLOCK_SIZE) / DC_SETS;
82
 
83
        /* Scan all ways and try to find a matching way. */
84
        for (i = 0; i < DC_WAYS; i++)
85
                if (dc[set].way[i].tagaddr == tagaddr)
86
                        way = i;
87
 
88
        /* Did we find our cached data? */
89
        if (way >= 0) { /* Yes, we did. */
90
                dc_stats.readhit++;
91
 
92
                for (i = 0; i < DC_WAYS; i++)
93
                        if (dc[set].way[i].lru)
94
                                dc[set].way[i].lru--;
95
                dc[set].way[way].lru = DC_USTATES - 1;
96
        }
97
        else {  /* No, we didn't. */
98
                int minlru = DC_USTATES - 1;
99
                int minway = 0;
100
 
101
                dc_stats.readmiss++;
102
 
103
                for (i = 0; i < DC_WAYS; i++)
104
                        if (dc[set].way[i].lru < minlru)
105
                                minway = i;
106
 
107
                dc[set].way[minway].tagaddr = tagaddr;
108
                for (i = 0; i < DC_WAYS; i++)
109
                        if (dc[set].way[i].lru)
110
                                dc[set].way[i].lru--;
111
                dc[set].way[minway].lru = DC_USTATES - 1;
112
        }
113
}
114
 
115
/* First check if data is already in the cache and if it is:
116
    - increment DC write hit stats,
117
    - set 'lru' at this way to DC_USTATES - 1 and
118
      decrement 'lru' of other ways unless they have reached 0,
119
   and if not:
120
    - increment DC write miss stats
121
    - find lru way and entry and replace old tag with tag of the 'dataaddr'
122
    - set 'lru' with DC_USTATES - 1 and decrement 'lru' of other
123
      ways unless they have reached 0
124
*/
125
 
126
void dc_simulate_write(unsigned long dataaddr)
127
{
128
        int set, way = -1;
129
        int i;
130
        unsigned long tagaddr;
131
 
132
        /* Which set to check out? */
133
        set = (dataaddr / DC_BLOCK_SIZE) % DC_SETS;
134
        tagaddr = (dataaddr / DC_BLOCK_SIZE) / DC_SETS;
135
 
136
        /* Scan all ways and try to find a matching way. */
137
        for (i = 0; i < DC_WAYS; i++)
138
                if (dc[set].way[i].tagaddr == tagaddr)
139
                        way = i;
140
 
141
        /* Did we find our cached data? */
142
        if (way >= 0) { /* Yes, we did. */
143
                dc_stats.writehit++;
144
 
145
                for (i = 0; i < DC_WAYS; i++)
146
                        if (dc[set].way[i].lru)
147
                                dc[set].way[i].lru--;
148
                dc[set].way[way].lru = DC_USTATES - 1;
149
        }
150
        else {  /* No, we didn't. */
151
                int minlru = DC_USTATES - 1;
152
                int minway = 0;
153
 
154
                dc_stats.writemiss++;
155
 
156
                for (i = 0; i < DC_WAYS; i++)
157
                        if (dc[set].way[i].lru < minlru)
158
                                minway = i;
159
 
160
                dc[set].way[minway].tagaddr = tagaddr;
161
                for (i = 0; i < DC_WAYS; i++)
162
                        if (dc[set].way[i].lru)
163
                                dc[set].way[i].lru--;
164
                dc[set].way[minway].lru = DC_USTATES - 1;
165
        }
166
}

powered by: WebSVN 2.1.0

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