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 5

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
 
35
/* Instruction cache */
36
 
37
/* Number of IC sets (power of 2) */
38
#define IC_SETS 256
39
 
40
/* Block size in bytes (1, 2, 4, 8, 16, 32 etc.) */
41
#define IC_BLOCK_SIZE 4
42
 
43
/* Number of IC ways (1, 2, 3 etc.). */
44
#define IC_WAYS 1
45
 
46
/* Number of usage states (2, 3, 4 etc.). */
47
#define IC_USTATES 2
48
 
49
struct ic_set {
50
        struct {
51
                unsigned long tagaddr;  /* tag address */
52
                int lru;                /* least recently used */
53
        } way[IC_WAYS];
54
} ic[IC_SETS];
55
 
56
void ic_info()
57
{
58
        printf("Instruction cache %dKB: ", IC_SETS * IC_BLOCK_SIZE * IC_WAYS / 1024);
59
        printf("%d ways, %d sets, block size %d bytes\n", IC_WAYS, IC_SETS, IC_BLOCK_SIZE);
60
}
61
 
62
/* First check if instruction is already in the cache and if it is:
63
    - increment IC read hit stats,
64
    - set 'lru' at this way to IC_USTATES - 1 and
65
      decrement 'lru' of other ways unless they have reached 0,
66
   and if not:
67
    - increment IC read miss stats
68
    - find lru way and entry and replace old tag with tag of the 'fetchaddr'
69
    - set 'lru' with IC_USTATES - 1 and decrement 'lru' of other
70
      ways unless they have reached 0
71
*/
72
 
73
void ic_simulate(unsigned long fetchaddr)
74
{
75
        int set, way = -1;
76
        int i;
77
        unsigned long tagaddr;
78
 
79
        /* Which set to check out? */
80
        set = (fetchaddr / IC_BLOCK_SIZE) % IC_SETS;
81
        tagaddr = (fetchaddr / IC_BLOCK_SIZE) / IC_SETS;
82
 
83
        /* Scan all ways and try to find a matching way. */
84
        for (i = 0; i < IC_WAYS; 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 < IC_WAYS; i++)
93
                        if (ic[set].way[i].lru)
94
                                ic[set].way[i].lru--;
95
                ic[set].way[way].lru = IC_USTATES - 1;
96
        }
97
        else {  /* No, we didn't. */
98
                int minlru = IC_USTATES - 1;
99
                int minway = 0;
100
 
101
                ic_stats.readmiss++;
102
 
103
                for (i = 0; i < IC_WAYS; i++)
104
                        if (ic[set].way[i].lru < minlru)
105
                                minway = i;
106
 
107
                ic[set].way[minway].tagaddr = tagaddr;
108
                for (i = 0; i < IC_WAYS; i++)
109
                        if (ic[set].way[i].lru)
110
                                ic[set].way[i].lru--;
111
                ic[set].way[minway].lru = IC_USTATES - 1;
112
        }
113
}

powered by: WebSVN 2.1.0

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