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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_1_x/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1768

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

powered by: WebSVN 2.1.0

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