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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1308

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

powered by: WebSVN 2.1.0

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