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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_67/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 997

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
      fprintf (cur_area->log, "[%08x] -> read %08x\n", fetchaddr, tmp);
90
    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 428 markom
    for (i = 0; i < config.ic.nways; i++)
120
      if (ic[set].way[i].lru < minlru)
121
        minway = i;
122
 
123 631 simons
    for (i = 0; i < (config.ic.blocksize); i += 4) {
124 992 simons
      tmp = ic[set].way[minway].line[((fetchaddr + i) & (config.ic.blocksize - 1)) >> 2] =
125 631 simons
        evalsim_mem32((fetchaddr & ~(config.ic.blocksize - 1)) + ((fetchaddr + i) & (config.ic.blocksize - 1)));
126 638 simons
      if(!cur_area) {
127
        ic[set].way[minway].tagaddr = -1;
128
        ic[set].way[minway].lru = 0;
129 992 simons
        printf("EXCEPTION: read out of memory (32-bit access to %.8lx)\n", fetchaddr);
130
        except_handle(EXCEPT_BUSERR, cur_vadd);
131 631 simons
        return 0;
132 638 simons
      }
133 992 simons
      if (!pending.valid && cur_area->log)
134
        fprintf (cur_area->log, "[%08x] -> read %08x\n", fetchaddr, tmp);
135 631 simons
    }
136
 
137 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
138
    for (i = 0; i < config.ic.nways; i++)
139 631 simons
      if (ic[set].way[i].lru)
140 428 markom
        ic[set].way[i].lru--;
141
    ic[set].way[minway].lru = config.ic.ustates - 1;
142 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
143 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
144 428 markom
  }
145 5 lampret
}
146 102 lampret
 
147
/* First check if data is already in the cache and if it is:
148
    - invalidate block if way isn't locked
149
   otherwise don't do anything.
150
*/
151
 
152
void ic_inv(unsigned long dataaddr)
153
{
154 428 markom
  int set, way = -1;
155
  int i;
156
  unsigned long tagaddr;
157 102 lampret
 
158 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
159
    return;
160 102 lampret
 
161 428 markom
  /* Which set to check out? */
162
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
163
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
164 631 simons
 
165
  if (!testsprbits(SPR_SR, SPR_SR_ICE)) {
166
    for (i = 0; i < config.ic.nways; i++) {
167
      ic[set].way[i].tagaddr = -1;
168
      ic[set].way[i].lru = 0;
169
    }
170
    return;
171
  }
172 428 markom
 
173
  /* Scan all ways and try to find a matching way. */
174
  for (i = 0; i < config.ic.nways; i++)
175
    if (ic[set].way[i].tagaddr == tagaddr)
176
      way = i;
177
 
178
  /* Did we find our cached data? */
179 631 simons
  if (way >= 0) { /* Yes, we did. */
180 428 markom
    ic[set].way[way].tagaddr = -1;
181 631 simons
    ic[set].way[way].lru = 0;
182 428 markom
  }
183 102 lampret
}
184
 
185 261 markom
inline void ic_clock()
186 102 lampret
{
187 428 markom
  unsigned long addr;
188
 
189
  if (addr = mfspr(SPR_ICBPR)) {
190
    ic_simulate_fetch(addr);
191
    mtspr(SPR_ICBPR, 0);
192
  }
193
  if (addr = mfspr(SPR_ICBIR)) {
194
    ic_inv(addr);
195
    mtspr(SPR_ICBIR, 0);
196
  }
197
  if (addr = mfspr(SPR_ICBLR)) {
198
    mtspr(SPR_ICBLR, 0);
199
  }
200 102 lampret
}

powered by: WebSVN 2.1.0

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