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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_1_0/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1344

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

powered by: WebSVN 2.1.0

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