1 |
5 |
poppy |
------------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- Copyright (c) 2004, Hangouet Samuel --
|
4 |
|
|
-- , Jan Sebastien --
|
5 |
|
|
-- , Mouton Louis-Marie --
|
6 |
|
|
-- , Schneider Olivier all rights reserved --
|
7 |
|
|
-- --
|
8 |
|
|
-- This file is part of miniMIPS. --
|
9 |
|
|
-- --
|
10 |
|
|
-- miniMIPS is free software; you can redistribute it and/or modify --
|
11 |
|
|
-- it under the terms of the GNU Lesser General Public License as published by --
|
12 |
|
|
-- the Free Software Foundation; either version 2.1 of the License, or --
|
13 |
|
|
-- (at your option) any later version. --
|
14 |
|
|
-- --
|
15 |
|
|
-- miniMIPS is distributed in the hope that it will be useful, --
|
16 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
17 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
18 |
|
|
-- GNU Lesser General Public License for more details. --
|
19 |
|
|
-- --
|
20 |
|
|
-- You should have received a copy of the GNU Lesser General Public License --
|
21 |
|
|
-- along with miniMIPS; if not, write to the Free Software --
|
22 |
|
|
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
|
23 |
|
|
-- --
|
24 |
|
|
------------------------------------------------------------------------------------
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
-- If you encountered any problem, please contact :
|
28 |
|
|
--
|
29 |
|
|
-- lmouton@enserg.fr
|
30 |
|
|
-- oschneid@enserg.fr
|
31 |
|
|
-- shangoue@enserg.fr
|
32 |
|
|
--
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
--------------------------------------------------------------------------
|
37 |
|
|
-- --
|
38 |
|
|
-- --
|
39 |
|
|
-- miniMIPS Processor : Branch prediction --
|
40 |
|
|
-- --
|
41 |
|
|
-- --
|
42 |
|
|
-- --
|
43 |
|
|
-- Author : Olivier Schneider --
|
44 |
|
|
-- --
|
45 |
|
|
-- june 2004 --
|
46 |
|
|
--------------------------------------------------------------------------
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
library ieee;
|
51 |
|
|
use ieee.std_logic_1164.all;
|
52 |
|
|
use IEEE.numeric_std.all;
|
53 |
|
|
|
54 |
|
|
library work;
|
55 |
|
|
use work.pack_mips.all;
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
entity predict is
|
59 |
|
|
generic (
|
60 |
|
|
nb_record : integer := 3
|
61 |
|
|
);
|
62 |
|
|
port (
|
63 |
|
|
|
64 |
|
|
clock : in std_logic;
|
65 |
|
|
reset : in std_logic;
|
66 |
|
|
|
67 |
|
|
-- Datas from PF pipeline stage
|
68 |
|
|
PF_pc : in std_logic_vector(31 downto 0); -- PC of the current instruction extracted
|
69 |
|
|
|
70 |
|
|
-- Datas from DI pipeline stage
|
71 |
|
|
DI_bra : in std_logic; -- Branch detected
|
72 |
|
|
DI_adr : in std_logic_vector(31 downto 0); -- Address of the branch
|
73 |
|
|
|
74 |
|
|
-- Datas from EX pipeline stage
|
75 |
|
|
EX_bra_confirm : in std_logic; -- Confirm if the branch test is ok
|
76 |
|
|
EX_adr : in std_logic_vector(31 downto 0); -- Address of the branch
|
77 |
|
|
EX_adresse : in std_logic_vector(31 downto 0); -- Result of the branch
|
78 |
|
|
EX_uncleared : in std_logic; -- Define if the EX stage is cleared
|
79 |
|
|
|
80 |
|
|
-- Outputs to PF pipeline stage
|
81 |
|
|
PR_bra_cmd : out std_logic; -- Defined a branch
|
82 |
|
|
PR_bra_bad : out std_logic; -- Defined a branch to restore from a bad prediction
|
83 |
|
|
PR_bra_adr : out std_logic_vector(31 downto 0); -- New PC
|
84 |
|
|
|
85 |
|
|
-- Clear the three pipeline stage : EI, DI, EX
|
86 |
|
|
PR_clear : out std_logic
|
87 |
|
|
);
|
88 |
|
|
end entity;
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
architecture rtl of predict is
|
92 |
|
|
|
93 |
|
|
-- Record contained in the table of prediction
|
94 |
|
|
type pred_type is
|
95 |
|
|
record
|
96 |
|
|
is_affected : std_logic; -- Check if the record is affected
|
97 |
|
|
last_bra : std_logic; -- The last branch confirmation result
|
98 |
|
|
code_adr : std_logic_vector(31 downto 0); -- Branch instruction address
|
99 |
|
|
bra_adr : std_logic_vector(31 downto 0); -- Branch result
|
100 |
|
|
end record;
|
101 |
|
|
|
102 |
|
|
type pred_tab_type is array(1 to nb_record) of pred_type;
|
103 |
|
|
|
104 |
|
|
-- Table of predictions
|
105 |
|
|
signal pred_tab : pred_tab_type;
|
106 |
|
|
signal pre_pred_tab : pred_tab_type;
|
107 |
|
|
|
108 |
|
|
signal next_out : integer range 1 to nb_record := 1; -- Next record to be erased in the table
|
109 |
|
|
signal add_record : std_logic;
|
110 |
|
|
|
111 |
|
|
begin
|
112 |
|
|
|
113 |
|
|
-- Do the predictions
|
114 |
|
|
process(reset, PF_pc, DI_bra, DI_adr, EX_adr, EX_adresse, EX_bra_confirm, pred_tab)
|
115 |
|
|
|
116 |
|
|
variable index : integer range 0 to nb_record; -- Table index if a code_adr match with an instruction address
|
117 |
|
|
variable index2 : integer range 0 to nb_record; -- Table index if a code_adr match with an instruction address
|
118 |
|
|
variable index3 : integer range 0 to nb_record; -- Table index if a code_adr match with an instruction address
|
119 |
|
|
|
120 |
|
|
variable bad_pred : std_logic; -- Flag of bad prediction
|
121 |
|
|
|
122 |
|
|
begin
|
123 |
|
|
|
124 |
|
|
-- Default signal affectations
|
125 |
|
|
index := 0;
|
126 |
|
|
index2 := 0;
|
127 |
|
|
index3 := 0;
|
128 |
|
|
pre_pred_tab <= pred_tab; -- No modification in table of prediction by default
|
129 |
|
|
PR_bra_cmd <= '0';
|
130 |
|
|
PR_bra_bad <= '0';
|
131 |
|
|
PR_bra_adr <= (others => '0');
|
132 |
|
|
PR_clear <= '0';
|
133 |
|
|
bad_pred := '0';
|
134 |
|
|
add_record <= '0';
|
135 |
|
|
|
136 |
|
|
-- Check a match in the table
|
137 |
|
|
for i in 1 to nb_record loop
|
138 |
|
|
if pred_tab(i).is_affected = '1' then
|
139 |
|
|
if PF_pc = pred_tab(i).code_adr then
|
140 |
|
|
index3 := i;
|
141 |
|
|
end if;
|
142 |
|
|
if DI_adr = pred_tab(i).code_adr then
|
143 |
|
|
index := i;
|
144 |
|
|
end if;
|
145 |
|
|
if EX_adr = pred_tab(i).code_adr then
|
146 |
|
|
index2 := i;
|
147 |
|
|
end if;
|
148 |
|
|
end if;
|
149 |
|
|
end loop;
|
150 |
|
|
|
151 |
|
|
-- Branch prediciton
|
152 |
|
|
if index3 /= 0 then
|
153 |
|
|
PR_bra_cmd <= '1';
|
154 |
|
|
PR_bra_adr <= pred_tab(index3).bra_adr;
|
155 |
|
|
end if;
|
156 |
|
|
|
157 |
|
|
-- Check if the prediction is ok
|
158 |
|
|
if EX_uncleared = '1' then
|
159 |
|
|
if index2 /= 0 then
|
160 |
|
|
if pred_tab(index2).last_bra /= EX_bra_confirm then -- Bad test result prediction
|
161 |
|
|
|
162 |
|
|
if EX_bra_confirm = '1' then
|
163 |
|
|
pre_pred_tab(index2).last_bra <= '1';
|
164 |
|
|
pre_pred_tab(index2).bra_adr <= EX_adresse;
|
165 |
|
|
else
|
166 |
|
|
pre_pred_tab(index2).last_bra <= '0';
|
167 |
|
|
pre_pred_tab(index2).bra_adr <= std_logic_vector(unsigned(pred_tab(index2).code_adr)+4);
|
168 |
|
|
end if;
|
169 |
|
|
|
170 |
|
|
bad_pred := '1';
|
171 |
|
|
|
172 |
|
|
elsif pred_tab(index2).bra_adr /= EX_adresse then -- Bad adress result prediction
|
173 |
|
|
|
174 |
|
|
pre_pred_tab(index2).bra_adr <= EX_adresse;
|
175 |
|
|
bad_pred := '1';
|
176 |
|
|
|
177 |
|
|
end if;
|
178 |
|
|
end if;
|
179 |
|
|
end if;
|
180 |
|
|
|
181 |
|
|
-- Clear the pipeline and branch to the new instruction
|
182 |
|
|
if bad_pred = '1' then
|
183 |
|
|
PR_bra_bad <= '1';
|
184 |
|
|
PR_bra_adr <= pre_pred_tab(index2).bra_adr;
|
185 |
|
|
PR_clear <= '1';
|
186 |
|
|
end if;
|
187 |
|
|
|
188 |
|
|
-- Add a record in the table
|
189 |
|
|
if DI_bra = '1' then
|
190 |
|
|
if index = 0 then
|
191 |
|
|
add_record <= '1';
|
192 |
|
|
pre_pred_tab(next_out).is_affected <= '1'; -- The record is affected
|
193 |
|
|
pre_pred_tab(next_out).last_bra <= '0'; -- Can't predict the branch the first time
|
194 |
|
|
pre_pred_tab(next_out).code_adr <= DI_adr; -- Save the branch address
|
195 |
|
|
pre_pred_tab(next_out).bra_adr <= std_logic_vector(unsigned(DI_adr)+4); -- Branch result
|
196 |
|
|
end if;
|
197 |
|
|
end if;
|
198 |
|
|
|
199 |
|
|
end process;
|
200 |
|
|
|
201 |
|
|
-- Update the table of prediction
|
202 |
|
|
process(clock, reset)
|
203 |
|
|
begin
|
204 |
|
|
if reset = '1' then
|
205 |
|
|
|
206 |
|
|
next_out <= 1; -- At the beginning the first record must be chosen to be filled
|
207 |
|
|
|
208 |
|
|
for i in 1 to nb_record loop
|
209 |
|
|
pred_tab(i).is_affected <= '0';
|
210 |
|
|
end loop;
|
211 |
|
|
|
212 |
|
|
elsif rising_edge(clock) then
|
213 |
|
|
|
214 |
|
|
pred_tab <= pre_pred_tab;
|
215 |
|
|
|
216 |
|
|
if add_record = '1' then
|
217 |
|
|
if next_out = nb_record then
|
218 |
|
|
next_out <= 1;
|
219 |
|
|
else
|
220 |
|
|
next_out <= next_out+1; -- Next record to be erased
|
221 |
|
|
end if;
|
222 |
|
|
end if;
|
223 |
|
|
|
224 |
|
|
end if;
|
225 |
|
|
end process;
|
226 |
|
|
end rtl;
|
227 |
|
|
|