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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [bpb/] [branch_predict.c] - Blame information for rev 28

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 cvs
/* branch_predict.c -- branch prediction 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
/* Branch prediction functions.
21
   At the moment this functions only simulate functionality of branch
22
   prediction and do not influence on fetche/decode/execute stages.
23
   They are here only to verify performance of various branch
24
   prediction configurations.
25
 
26
 */
27
 
28
#include <stdio.h>
29
#include <string.h>
30
#include <errno.h>
31
#include <stdarg.h>
32
 
33
#include "branch_predict.h"
34
#include "abstract.h"
35
#include "stats.h"
36
 
37
/* Branch prediction buffer */
38
 
39
/* Length of BPB */
40 28 lampret
#define BPB_LEN 64
41 2 cvs
 
42
/* Number of BPB ways (1, 2, 3 etc.). */
43 6 lampret
#define BPB_WAYS 1
44 2 cvs
 
45
/* Number of prediction states (2, 4, 8 etc.). */
46 28 lampret
#define BPB_PSTATES 2
47 2 cvs
 
48
/* Number of usage states (2, 3, 4 etc.). */
49 28 lampret
#define BPB_USTATES 2
50 2 cvs
 
51
/* branch prediction buffer entry */
52
struct bpb_entry {
53
        struct {
54
                unsigned long addr;     /* address of a branch insn */
55
                int taken;              /* taken == 1, not taken == 0  OR */
56
                                        /* strongly taken == 3, taken == 2,
57
                                           not taken == 1, strongly not taken == 0 */
58
                int lru;                /* least recently == 0 */
59
        } way[BPB_WAYS];
60
} bpb[BPB_LEN];
61
 
62 6 lampret
void bpb_info()
63
{
64
        printf("BPB %d bytes: ", BPB_LEN * BPB_WAYS * (BPB_PSTATES + BPB_USTATES) / 8);
65
        printf("%d ways, %d sets, %d bits/prediction\n", BPB_WAYS, BPB_LEN, BPB_PSTATES + BPB_USTATES);
66
}
67
 
68 2 cvs
/* First check if branch is already in the cache and if it is:
69
    - increment BPB hit stats,
70
    - set 'lru' at this way to BPB_USTATES - 1 and
71
      decrement 'lru' of other ways unless they have reached 0,
72
    - increment correct/incorrect stats according to BPB 'taken' field
73
      and 'taken' variable,
74
    - increment or decrement BPB taken field according to 'taken' variable
75
   and if not:
76
    - increment BPB miss stats
77
    - find lru way and entry and replace old address with 'addr' and
78
      'taken' field with (BPB_PSTATES/2 - 1) + 'taken'
79
    - set 'lru' with BPB_USTATES - 1 and decrement 'lru' of other
80
      ways unless they have reached 0
81
*/
82
 
83
void bpb_update(unsigned long addr, int taken)
84
{
85
        int entry, way = -1;
86
        int i;
87
 
88
        /* Calc entry. */
89
        entry = addr % BPB_LEN;
90
 
91
        /* Scan all ways and try to find our addr. */
92
        for (i = 0; i < BPB_WAYS; i++)
93
                if (bpb[entry].way[i].addr == addr)
94
                        way = i;
95
 
96
        /* Did we find our cached branch? */
97
        if (way >= 0) { /* Yes, we did. */
98
                mstats.bpb.hit++;
99
 
100
                for (i = 0; i < BPB_WAYS; i++)
101
                        if (bpb[entry].way[i].lru)
102
                                bpb[entry].way[i].lru--;
103
                bpb[entry].way[way].lru = BPB_USTATES - 1;
104
 
105
                if (bpb[entry].way[way].taken / (BPB_PSTATES / 2) == taken)
106
                        mstats.bpb.correct++;
107
                else
108
                        mstats.bpb.incorrect++;
109
 
110
                if (taken && (bpb[entry].way[way].taken < BPB_PSTATES - 1))
111
                        bpb[entry].way[way].taken++;
112
                else
113
                if (!taken && (bpb[entry].way[way].taken))
114
                        bpb[entry].way[way].taken--;
115
        }
116
        else {  /* No, we didn't. */
117
                int minlru = BPB_USTATES - 1;
118
                int minway = 0;
119
 
120
                mstats.bpb.miss++;
121
 
122
                for (i = 0; i < BPB_WAYS; i++)
123
                        if (bpb[entry].way[i].lru < minlru)
124
                                minway = i;
125
 
126
                bpb[entry].way[minway].addr = addr;
127
                bpb[entry].way[minway].taken = (BPB_PSTATES / 2 - 1) + taken;
128
                for (i = 0; i < BPB_WAYS; i++)
129
                        if (bpb[entry].way[i].lru)
130
                                bpb[entry].way[i].lru--;
131
                bpb[entry].way[minway].lru = BPB_USTATES - 1;
132
        }
133
}
134
 
135
/* Branch target instruction cache */
136
 
137
/* Length of BTIC */
138 6 lampret
#define BTIC_LEN 128
139 2 cvs
 
140
/* Number of BTIC ways (1, 2, 3 etc.). */
141
#define BTIC_WAYS 2
142
 
143
/* Number of usage states (2, 3, 4 etc.). */
144
#define BTIC_USTATES 2
145
 
146 6 lampret
/* Target block size in bytes. */
147
#define BTIC_BLOCKSIZE 4
148
 
149 2 cvs
struct btic_entry {
150
        struct {
151
                unsigned long addr;     /* cached target address of a branch */
152
                int lru;                /* least recently used */
153
                char *insn;             /* cached insn at target address (not used currently) */
154
        } way[BTIC_WAYS];
155
} btic[BTIC_LEN];
156
 
157 6 lampret
void btic_info()
158
{
159
        printf("BTIC %d bytes: ", BTIC_LEN * BTIC_WAYS * (BTIC_USTATES + BTIC_BLOCKSIZE * 8) / 8);
160
        printf("%d ways, %d sets, %d bits/target\n", BTIC_WAYS, BTIC_LEN, BTIC_USTATES + BTIC_BLOCKSIZE * 8);
161
}
162
 
163 2 cvs
/* First check if target addr is already in the cache and if it is:
164
    - increment BTIC hit stats,
165
    - set 'lru' at this way to BTIC_USTATES - 1 and
166
      decrement 'lru' of other ways unless they have reached 0,
167
   and if not:
168
    - increment BTIC miss stats
169
    - find lru way and entry and replace old address with 'addr' and
170
      'insn' with NULL
171
    - set 'lru' with BTIC_USTATES - 1 and decrement 'lru' of other
172
      ways unless they have reached 0
173
*/
174
 
175
void btic_update(unsigned long targetaddr)
176
{
177
        int entry, way = -1;
178
        int i;
179
 
180
        /* Calc entry. */
181
        entry = targetaddr % BTIC_LEN;
182
 
183
        /* Scan all ways and try to find our addr. */
184
        for (i = 0; i < BTIC_WAYS; i++)
185
                if (btic[entry].way[i].addr == targetaddr)
186
                        way = i;
187
 
188
        /* Did we find our cached branch? */
189
        if (way >= 0) { /* Yes, we did. */
190
                mstats.btic.hit++;
191
 
192
                for (i = 0; i < BTIC_WAYS; i++)
193
                        if (btic[entry].way[i].lru)
194
                                btic[entry].way[i].lru--;
195
                btic[entry].way[way].lru = BTIC_USTATES - 1;
196
        }
197
        else {  /* No, we didn't. */
198
                int minlru = BTIC_USTATES - 1;
199
                int minway = 0;
200
 
201
                mstats.btic.miss++;
202
 
203
                for (i = 0; i < BTIC_WAYS; i++)
204
                        if (btic[entry].way[i].lru < minlru)
205
                                minway = i;
206
 
207
                btic[entry].way[minway].addr = targetaddr;
208
                btic[entry].way[minway].insn = NULL;
209
                for (i = 0; i < BTIC_WAYS; i++)
210
                        if (btic[entry].way[i].lru)
211
                                btic[entry].way[i].lru--;
212
                btic[entry].way[minway].lru = BTIC_USTATES - 1;
213
        }
214
}

powered by: WebSVN 2.1.0

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