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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_65/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 631

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

powered by: WebSVN 2.1.0

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