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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_64/] [or1ksim/] [cache/] [icache_model.c] - Blame information for rev 428

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

powered by: WebSVN 2.1.0

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