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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 884

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 102 lampret
 
78 428 markom
  /* ICache simulation enabled/disabled. */
79 638 simons
  if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)) || insn_ci)
80 631 simons
    return evalsim_mem32(fetchaddr);
81 428 markom
 
82
  /* Which set to check out? */
83
  set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
84
  tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
85
 
86
  /* Scan all ways and try to find a matching way. */
87
  for (i = 0; i < config.ic.nways; i++)
88
    if (ic[set].way[i].tagaddr == tagaddr)
89
      way = i;
90
 
91
  /* Did we find our cached instruction? */
92
  if (way >= 0) { /* Yes, we did. */
93
    ic_stats.readhit++;
94
 
95
    for (i = 0; i < config.ic.nways; i++)
96 631 simons
      if (ic[set].way[i].lru > ic[set].way[way].lru)
97 428 markom
        ic[set].way[i].lru--;
98
    ic[set].way[way].lru = config.ic.ustates - 1;
99 884 markom
    runtime.sim.mem_cycles += config.ic.hitdelay;
100 631 simons
    return (ic[set].way[way].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
101 428 markom
  }
102
  else {  /* No, we didn't. */
103
    int minlru = config.ic.ustates - 1;
104
    int minway = 0;
105 5 lampret
 
106 631 simons
    ic_stats.readmiss++;
107 5 lampret
 
108 428 markom
    for (i = 0; i < config.ic.nways; i++)
109
      if (ic[set].way[i].lru < minlru)
110
        minway = i;
111
 
112 631 simons
    for (i = 0; i < (config.ic.blocksize); i += 4) {
113
      ic[set].way[minway].line[((fetchaddr + i) & (config.ic.blocksize - 1)) >> 2] =
114
        evalsim_mem32((fetchaddr & ~(config.ic.blocksize - 1)) + ((fetchaddr + i) & (config.ic.blocksize - 1)));
115 638 simons
      if(!cur_area) {
116
        ic[set].way[minway].tagaddr = -1;
117
        ic[set].way[minway].lru = 0;
118 631 simons
        return 0;
119 638 simons
      }
120 631 simons
    }
121
 
122 428 markom
    ic[set].way[minway].tagaddr = tagaddr;
123
    for (i = 0; i < config.ic.nways; i++)
124 631 simons
      if (ic[set].way[i].lru)
125 428 markom
        ic[set].way[i].lru--;
126
    ic[set].way[minway].lru = config.ic.ustates - 1;
127 884 markom
    runtime.sim.mem_cycles += config.ic.missdelay;
128 631 simons
    return (ic[set].way[minway].line[(fetchaddr & (config.ic.blocksize - 1)) >> 2]);
129 428 markom
  }
130 5 lampret
}
131 102 lampret
 
132
/* First check if data is already in the cache and if it is:
133
    - invalidate block if way isn't locked
134
   otherwise don't do anything.
135
*/
136
 
137
void ic_inv(unsigned long dataaddr)
138
{
139 428 markom
  int set, way = -1;
140
  int i;
141
  unsigned long tagaddr;
142 102 lampret
 
143 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
144
    return;
145 102 lampret
 
146 428 markom
  /* Which set to check out? */
147
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
148
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
149 631 simons
 
150
  if (!testsprbits(SPR_SR, SPR_SR_ICE)) {
151
    for (i = 0; i < config.ic.nways; i++) {
152
      ic[set].way[i].tagaddr = -1;
153
      ic[set].way[i].lru = 0;
154
    }
155
    return;
156
  }
157 428 markom
 
158
  /* Scan all ways and try to find a matching way. */
159
  for (i = 0; i < config.ic.nways; i++)
160
    if (ic[set].way[i].tagaddr == tagaddr)
161
      way = i;
162
 
163
  /* Did we find our cached data? */
164 631 simons
  if (way >= 0) { /* Yes, we did. */
165 428 markom
    ic[set].way[way].tagaddr = -1;
166 631 simons
    ic[set].way[way].lru = 0;
167 428 markom
  }
168 102 lampret
}
169
 
170 261 markom
inline void ic_clock()
171 102 lampret
{
172 428 markom
  unsigned long addr;
173
 
174
  if (addr = mfspr(SPR_ICBPR)) {
175
    ic_simulate_fetch(addr);
176
    mtspr(SPR_ICBPR, 0);
177
  }
178
  if (addr = mfspr(SPR_ICBIR)) {
179
    ic_inv(addr);
180
    mtspr(SPR_ICBIR, 0);
181
  }
182
  if (addr = mfspr(SPR_ICBLR)) {
183
    mtspr(SPR_ICBLR, 0);
184
  }
185 102 lampret
}

powered by: WebSVN 2.1.0

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