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 1344

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

powered by: WebSVN 2.1.0

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