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

Subversion Repositories or1k

[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 1765

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

powered by: WebSVN 2.1.0

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