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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_59/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 541

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
struct ic_set {
40 428 markom
  struct {
41
    unsigned long tagaddr;  /* tag address */
42
    int lru;    /* least recently used */
43
  } way[MAX_IC_WAYS];
44
} ic[MAX_IC_SETS];
45 5 lampret
 
46
void ic_info()
47
{
48 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP)) {
49
    printf("ICache not implemented. Set UPR[ICP].\n");
50
    return;
51
  }
52 102 lampret
 
53 428 markom
  printf("Instruction cache %dKB: ", config.ic.nsets * config.ic.blocksize * config.ic.nways / 1024);
54
  printf("%d ways, %d sets, block size %d bytes\n", config.ic.nways, config.ic.nsets, config.ic.blocksize);
55 5 lampret
}
56
 
57
/* First check if instruction is already in the cache and if it is:
58
    - increment IC read hit stats,
59 428 markom
    - set 'lru' at this way to config.ic.ustates - 1 and
60 5 lampret
      decrement 'lru' of other ways unless they have reached 0,
61
   and if not:
62
    - increment IC read miss stats
63
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
64 428 markom
    - set 'lru' with config.ic.ustates - 1 and decrement 'lru' of other
65 5 lampret
      ways unless they have reached 0
66
*/
67
 
68 76 lampret
void ic_simulate_fetch(unsigned long fetchaddr)
69 5 lampret
{
70 428 markom
  int set, way = -1;
71
  int i;
72
  unsigned long tagaddr;
73 541 markom
  extern int mem_cycles;
74 102 lampret
 
75 428 markom
  /* ICache simulation enabled/disabled. */
76
  if ((!testsprbits(SPR_UPR, SPR_UPR_ICP)) || (!testsprbits(SPR_SR, SPR_SR_ICE)))
77
    return;
78
 
79
  /* Which set to check out? */
80
  set = (fetchaddr / config.ic.blocksize) % config.ic.nsets;
81
  tagaddr = (fetchaddr / config.ic.blocksize) / config.ic.nsets;
82
 
83
  /* Scan all ways and try to find a matching way. */
84
  for (i = 0; i < config.ic.nways; i++)
85
    if (ic[set].way[i].tagaddr == tagaddr)
86
      way = i;
87
 
88
  /* Did we find our cached instruction? */
89
  if (way >= 0) { /* Yes, we did. */
90
    ic_stats.readhit++;
91
 
92
    for (i = 0; i < config.ic.nways; i++)
93
      if (ic[set].way[i].lru)
94
        ic[set].way[i].lru--;
95
    ic[set].way[way].lru = config.ic.ustates - 1;
96 541 markom
    mem_cycles += config.ic.hitdelay;
97 428 markom
  }
98
  else {  /* No, we didn't. */
99
    int minlru = config.ic.ustates - 1;
100
    int minway = 0;
101 5 lampret
 
102
                ic_stats.readmiss++;
103
 
104 428 markom
    for (i = 0; i < config.ic.nways; i++)
105
      if (ic[set].way[i].lru < minlru)
106
        minway = i;
107
 
108
    ic[set].way[minway].tagaddr = tagaddr;
109
    for (i = 0; i < config.ic.nways; i++)
110
      if ((ic[set].way[i].lru) &&
111
          (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << i)))
112
        ic[set].way[i].lru--;
113
    ic[set].way[minway].lru = config.ic.ustates - 1;
114 541 markom
    mem_cycles += config.ic.missdelay;
115 428 markom
  }
116 5 lampret
}
117 102 lampret
 
118
/* First check if data is already in the cache and if it is:
119
    - invalidate block if way isn't locked
120
   otherwise don't do anything.
121
*/
122
 
123
void ic_inv(unsigned long dataaddr)
124
{
125 428 markom
  int set, way = -1;
126
  int i;
127
  unsigned long tagaddr;
128 102 lampret
 
129 428 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_ICP))
130
    return;
131 102 lampret
 
132 428 markom
  /* Which set to check out? */
133
  set = (dataaddr / config.ic.blocksize) % config.ic.nsets;
134
  tagaddr = (dataaddr / config.ic.blocksize) / config.ic.nsets;
135
 
136
  /* Scan all ways and try to find a matching way. */
137
  for (i = 0; i < config.ic.nways; i++)
138
    if (ic[set].way[i].tagaddr == tagaddr)
139
      way = i;
140
 
141
  /* Did we find our cached data? */
142
  if ((way >= 0) && (getsprbits(SPR_ICCR, SPR_ICCR_EW) & (1 << way))) { /* Yes, we did. */
143
    ic[set].way[way].tagaddr = -1;
144
  }
145 102 lampret
}
146
 
147 261 markom
inline void ic_clock()
148 102 lampret
{
149 428 markom
  unsigned long addr;
150
 
151
  if (addr = mfspr(SPR_ICBPR)) {
152
    ic_simulate_fetch(addr);
153
    mtspr(SPR_ICBPR, 0);
154
  }
155
  if (addr = mfspr(SPR_ICBIR)) {
156
    ic_inv(addr);
157
    mtspr(SPR_ICBIR, 0);
158
  }
159
  if (addr = mfspr(SPR_ICBLR)) {
160
    mtspr(SPR_ICBLR, 0);
161
  }
162 102 lampret
}

powered by: WebSVN 2.1.0

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