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

Subversion Repositories rv01_riscv_core

[/] [rv01_riscv_core/] [trunk/] [VHDL/] [RV01_bpu.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 madsilicon
-----------------------------------------------------------------
2
--                                                             --
3
-----------------------------------------------------------------
4
--                                                             --
5
-- Copyright (C) 2016 Stefano Tonello                          --
6
--                                                             --
7
-- This source file may be used and distributed without        --
8
-- restriction provided that this copyright statement is not   --
9
-- removed from the file and that any derivative work contains --
10
-- the original copyright notice and the associated disclaimer.--
11
--                                                             --
12
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY         --
13
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   --
14
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   --
15
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      --
16
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         --
17
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    --
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   --
19
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        --
20
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  --
21
-- LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  --
22
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  --
23
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         --
24
-- POSSIBILITY OF SUCH DAMAGE.                                 --
25
--                                                             --
26
-----------------------------------------------------------------
27
 
28
---------------------------------------------------------------
29
-- RV01 Branch Prediction Unit
30
---------------------------------------------------------------
31
 
32
library IEEE;
33
use IEEE.std_logic_1164.all;
34
use IEEE.numeric_std.all;
35
 
36
library work;
37
use work.RV01_CONSTS_PKG.all;
38
use work.RV01_TYPES_PKG.all;
39
use work.RV01_FUNCS_PKG.all;
40
use work.RV01_ARITH_PKG.all;
41
use work.RV01_OP_PKG.all;
42
 
43
entity RV01_BPU is
44
  generic(
45
    BHT_SIZE : natural := 64;
46
    PXE : std_logic := '1';
47
    NW : natural := 2
48
  );
49
  port(
50
    CLK_i : in std_logic;
51
    RST_i : in std_logic;
52
    INIT_STRT_i : in std_logic;
53
    -- prediction port
54
    IF_V_i : in std_logic_vector(NW-1 downto 0);
55
    IF_PC_i : in ADR_VEC_T(NW-1 downto 0);
56
    IF2_V_i : in std_logic_vector(NW-1 downto 0);
57
    IF2_PC_i : in ADR_VEC_T(NW-1 downto 0);
58
    -- verification port
59
    BHT_BTA_i : in ADR_VEC_T(NW-1 downto 0);
60
    BHT_PC_i : in ADR_VEC_T(NW-1 downto 0);
61
    BHT_CNT0_i : in std_logic_vector(2-1 downto 0);
62
    BHT_CNT1_i : in std_logic_vector(2-1 downto 0);
63
    BHT_WE_i : in std_logic_vector(NW-1 downto 0);
64
 
65
    INIT_END_o : out std_logic;
66
    -- prediction port
67
    PBX_o : out std_logic;
68
    KLL1_o : out std_logic;
69
    PBTA_o : out unsigned(ALEN-1 downto 0);
70
    -- verification port
71
    BPVD0_o : out std_logic_vector(3-1 downto 0);
72
    BPVD1_o : out std_logic_vector(3-1 downto 0)
73
  );
74
end RV01_BPU;
75
 
76
architecture ARC of RV01_BPU is
77
 
78
  component RV01_BHT is
79
    generic(
80
      BHT_SIZE : natural := 64;
81
      PXE : std_logic := '1'
82
    );
83
    port(
84
      CLK_i : in std_logic;
85
      RST_i : in std_logic;
86
      BHTV_WE_i : in std_logic;
87
      BHTV_WADR_i : natural range 0 to BHT_SIZE-1;
88
      -- prediction port
89
      IF_V_i : std_logic;
90
      IF_PC_i : ADR_T;
91
      IF2_V_i : std_logic;
92
      IF2_PC_i : ADR_T;
93
      -- verification port
94
      BHT_BTA_i : in ADR_T;
95
      BHT_PC_i : in ADR_T;
96
      BHT_CNT_i : in std_logic_vector(2-1 downto 0);
97
      BHT_WE_i : in std_logic;
98
 
99
      -- prediction port
100
      PBX_o : out std_logic;
101
      PBTA_o : out unsigned(ALEN-1 downto 0);
102
      -- verification port
103
      BPVD_o : out std_logic_vector(3-1 downto 0)
104
    );
105
  end component;
106
 
107
  signal IF_V : std_logic_vector(NW-1 downto 0);
108
  signal IF_PC : ADR_VEC_T(NW-1 downto 0);
109
  signal IF2_V : std_logic_vector(NW-1 downto 0);
110
  signal IF2_PC : ADR_VEC_T(NW-1 downto 0);
111
  signal BHT_BTA : ADR_VEC_T(NW-1 downto 0);
112
  signal BHT_PC : ADR_VEC_T(NW-1 downto 0);
113
  signal BHT_CNT0,BHT_CNT1 : std_logic_vector(2-1 downto 0);
114
  signal BHT_WE : std_logic_vector(NW-1 downto 0);
115
  signal PBX : std_logic_vector(NW-1 downto 0);
116
  signal PBTA : ADR_VEC_T(NW-1 downto 0);
117
  signal BHT_CNT_q : natural range 0 to BHT_SIZE-1;
118
  signal BHT_INIT_q : std_logic;
119
  signal INIT_END : std_logic;
120
  signal BPVD0,BPVD1 : std_logic_vector(3-1 downto 0);
121
 
122
begin
123
 
124
  ------------------------------------
125
  -- Note
126
  ------------------------------------
127
 
128
  -- This module performs branch and jal instructions
129
  -- predictions using a branch history table (BHT)
130
  -- based on 2-bit saturating counters.
131
  -- The module includes two BHT's, one for even
132
  -- address instructions and one for odd address
133
  -- instructions and is able to predict up to
134
  -- two branch/jal instructions per cycle.
135
 
136
  ------------------------------------
137
  -- BHT initalization
138
  ------------------------------------
139
 
140
  -- BHT address generator
141
  process(CLK_i)
142
  begin
143
    if(CLK_i = '1' and CLK_i'event) then
144
      if(RST_i = '1') then
145
        BHT_CNT_q <= 0;
146
      elsif(BHT_INIT_q = '1' and BHT_CNT_q < BHT_SIZE-1) then
147
        BHT_CNT_q <= BHT_CNT_q + 1;
148
      end if;
149
    end if;
150
  end process;
151
 
152
  -- Initialization end flag
153
  INIT_END <= '1' when BHT_CNT_q = BHT_SIZE-1 else '0';
154
 
155
  INIT_END_o <= INIT_END;
156
 
157
  -- Initialization status
158
  process(CLK_i)
159
  begin
160
    if(CLK_i = '1' and CLK_i'event) then
161
      if(RST_i = '1') then
162
        BHT_INIT_q <= '0';
163
      elsif(INIT_STRT_i = '1') then
164
        BHT_INIT_q <= '1';
165
      elsif(INIT_END = '1') then
166
        BHT_INIT_q <= '0';
167
      end if;
168
    end if;
169
  end process;
170
 
171
  ------------------------------------
172
  -- BHT's
173
  ------------------------------------
174
 
175
  -- BHT #0 provides branch prediction for
176
  -- even word adresss instructions, while BHT #1
177
  -- provides branch prediction for odd word adresss
178
  -- instructions.
179
 
180
  U_BHT0 : RV01_BHT
181
    generic map(
182
      BHT_SIZE => BHT_SIZE,
183
      PXE => PXE
184
    )
185
    port map(
186
      CLK_i => CLK_i,
187
      RST_i => RST_i,
188
      BHTV_WE_i => BHT_INIT_q,
189
      BHTV_WADR_i => BHT_CNT_q,
190
      IF_V_i => IF_V(0),
191
      IF_PC_i => IF_PC(0),
192
      IF2_V_i => IF2_V(0),
193
      IF2_PC_i => IF2_PC(0),
194
      BHT_BTA_i => BHT_BTA(0),
195
      BHT_PC_i => BHT_PC(0),
196
      BHT_CNT_i => BHT_CNT0,
197
      BHT_WE_i => BHT_WE(0),
198
 
199
      PBX_o => PBX(0),
200
      PBTA_o => PBTA(0),
201
      BPVD_o => BPVD0
202
    );
203
 
204
  GPX_0_1 : if PXE = '1' generate
205
 
206
  U_BHT1 : RV01_BHT
207
    generic map(
208
      BHT_SIZE => BHT_SIZE
209
    )
210
    port map(
211
      CLK_i => CLK_i,
212
      RST_i => RST_i,
213
      BHTV_WE_i => BHT_INIT_q,
214
      BHTV_WADR_i => BHT_CNT_q,
215
      IF_V_i => IF_V(1),
216
      IF_PC_i => IF_PC(1),
217
      IF2_V_i => IF2_V(1),
218
      IF2_PC_i => IF2_PC(1),
219
      BHT_BTA_i => BHT_BTA(1),
220
      BHT_PC_i => BHT_PC(1),
221
      BHT_CNT_i => BHT_CNT1,
222
      BHT_WE_i => BHT_WE(1),
223
 
224
      PBX_o => PBX(1),
225
      PBTA_o => PBTA(1),
226
      BPVD_o => BPVD1
227
    );
228
 
229
  end generate; -- GPX_0_1 
230
 
231
  GPX_0_0 : if PXE = '0' generate
232
 
233
  PBX(1) <= '0';
234
  PBTA(1) <= (others => '0');
235
  BPVD1 <= (others => '0');
236
 
237
  end generate; -- GPX_0_0 
238
 
239
  ------------------------------------
240
  -- Prediction input data mux
241
  ------------------------------------
242
 
243
  IF_V(0) <= IF_V_i(0);
244
  IF_PC(0) <= IF_PC_i(0);
245
  IF_V(1) <= IF_V_i(1);
246
  IF_PC(1) <= IF_PC_i(1);
247
  IF2_V(0) <= IF2_V_i(0);
248
  IF2_V(1) <= IF2_V_i(1);
249
  IF2_PC(0) <= IF2_PC_i(0);
250
  IF2_PC(1) <= IF2_PC_i(1);
251
  BPVD0_o <= BPVD0;
252
  BPVD1_o <= BPVD1;
253
 
254
  -- Predicted branch execute flag
255
 
256
  PBX_o <= PBX(0) or PBX(1);
257
 
258
  -- Predicted branch target address
259
  -- (slot #0 takes priority over slot #1)
260
 
261
  PBTA_o <= PBTA(0) when (PBX(0) = '1' or PXE = '0') else PBTA(1);
262
 
263
  -- If slot #0 instruction is a predicted
264
  -- taken branch, slot #1 instruction must
265
  -- be nullified.
266
 
267
  KLL1_o <= PBX(0);
268
 
269
  ------------------------------------
270
  -- BHT update data mux
271
  ------------------------------------
272
 
273
  -- Instructions in IX1 stage (where branch
274
  -- predictions are verified) can be in 
275
  -- even-odd or odd-even address order 
276
  -- requiring proper multiplexing of data
277
  -- input to BHT's.
278
 
279
  process(BHT_PC_i,BHT_BTA_i,BHT_WE_i,BHT_CNT0_i,BHT_CNT1_i)
280
  begin
281
    if(BHT_PC_i(0)(2) = '0') then
282
      -- instructions are in even-odd order
283
      BHT_BTA(0) <= BHT_BTA_i(0);
284
      BHT_PC(0) <= BHT_PC_i(0);
285
      BHT_CNT0 <= BHT_CNT0_i;
286
      BHT_WE(0) <= BHT_WE_i(0);
287
      BHT_BTA(1) <= BHT_BTA_i(1);
288
      BHT_PC(1) <= BHT_PC_i(1);
289
      BHT_CNT1 <= BHT_CNT1_i;
290
      BHT_WE(1) <= BHT_WE_i(1);
291
    else
292
      -- instructions are in odd-even order
293
      BHT_BTA(0) <= BHT_BTA_i(1);
294
      BHT_PC(0) <= BHT_PC_i(1);
295
      BHT_CNT0 <= BHT_CNT1_i;
296
      BHT_WE(0) <= BHT_WE_i(1);
297
      BHT_BTA(1) <= BHT_BTA_i(0);
298
      BHT_PC(1) <= BHT_PC_i(0);
299
      BHT_CNT1 <= BHT_CNT0_i;
300
      BHT_WE(1) <= BHT_WE_i(0);
301
    end if;
302
  end process;
303
 
304
end ARC;
305
 
306
 

powered by: WebSVN 2.1.0

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