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

Subversion Repositories rv01_riscv_core

[/] [rv01_riscv_core/] [trunk/] [VHDL/] [RV01_bjxlog.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) 2017 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/Jump eXecute Logic
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_BJXLOG is
44
  generic(
45
    JRPE : std_logic := '1'
46
  );
47
  port(
48
    CLK_i : in std_logic;
49
    RST_i : in std_logic;
50
    BJ_OP_i : in BJ_OP_T;
51
    SU_i : in std_logic;
52
    PC_i : in unsigned(ALEN-1 downto 0);
53
    OPA_i : in SDWORD_T;
54
    OPB_i : in SDWORD_T;
55
    IMM_i : in SDWORD_T;
56
    IV_i : in std_logic;
57
    FSTLL_i : in std_logic;
58
    MPJRX_i : in std_logic;
59
 
60
    BJX_o : out std_logic;
61
    BJTA_o : out unsigned(ALEN-1 downto 0)
62
  );
63
end RV01_BJXLOG;
64
 
65
architecture ARC of RV01_BJXLOG is
66
 
67
  signal BJX,BJX_q : std_logic;
68
  signal BJTA,BJTA_q : unsigned(SDLEN-1 downto 0);
69
  signal OPB_N,SUB_RES : SDWORD_T;
70
  signal AEQB,ALTB : std_logic;
71
  signal BJAL_TA,JALR_TA,PCP4 : ADR_T;
72
  signal BX,JX : std_logic;
73
 
74
begin
75
 
76
  ------------------------------------
77
  -- Calculate B/J target addresses
78
  ------------------------------------
79
 
80
  -- Calculate B/J target addresses
81
 
82
  process(OPA_i,PC_i,IMM_i,BJ_OP_i)
83
    variable IOPA,IPC,IIMM : signed(SDLEN downto 0);
84
    variable TA0,TA1 : signed(SDLEN downto 0);
85
  begin
86
 
87
    -- sign-extend OPA_i by one bit
88
    if(OPA_i >= 0) then
89
      IOPA := '0' & OPA_i;
90
    else
91
      IOPA := '1' & OPA_i;
92
    end if;
93
 
94
    -- sign-extend IMM_i by one bit
95
    if(IMM_i >= 0) then
96
      IIMM := '0' & IMM_i;
97
    else
98
      IIMM := '1' & IMM_i;
99
    end if;
100
 
101
    -- sign-extend PC_i by one bit
102
    IPC := '0' & to_signed(PC_i);
103
 
104
    TA0 := IOPA + IIMM;
105
 
106
    TA1 := IPC + IIMM;
107
 
108
    -- Jalr target address
109
    JALR_TA <= to_unsigned(TA0(SDLEN-1 downto 0));
110
 
111
    -- branch/jal target address
112
    BJAL_TA <= to_unsigned(TA1(SDLEN-1 downto 0));
113
 
114
  end process;
115
 
116
  -- Address of instruction following the branch/jal
117
  PCP4 <= PC_i + 4;
118
 
119
  -- Select address to branch at in case of
120
  -- misprediction
121
 
122
  process(BX,JX,BJX_q,PCP4,BJAL_TA,JALR_TA,BJTA_q)
123
  begin
124
    if(BX = '1' and BJX_q = '0') then
125
      BJTA <= BJAL_TA;
126
    elsif(JX = '1' and BJX_q = '0') then
127
      BJTA <= JALR_TA(SDLEN-1 downto 1) & '0' ;
128
    elsif(BJX_q = '0') then
129
      BJTA <= PCP4;
130
    else
131
      BJTA <= BJTA_q;
132
    end if;
133
  end process;
134
 
135
  ------------------------------------
136
  -- Calculate OPA_i - OPB_i (for blt* and bge*)
137
  ------------------------------------
138
 
139
  SUB_RES <= OPA_i - OPB_i;
140
 
141
  ------------------------------------
142
  -- Branch control flags
143
  ------------------------------------
144
 
145
  -- OPA_i less-than OPB_i flag
146
  AEQB <= '1' when (OPA_i = OPB_i) else '0';
147
 
148
  -- if OPA > 0 and OPB > 0, then ALTB = sign(SUB_RES)
149
  -- if OPA < 0 and OPB < 0, then ALTB = sign(SUB_RES)
150
  -- if OPA > 0 and OPB < 0, then ALTB = '0'
151
  -- if OPA < 0 and OPB > 0, then ALTB = '1'
152
 
153
  -- OPA_i less-than OPB_i flag
154
  ALTB <=
155
    SUB_RES(SDLEN-1) when (OPA_i(SDLEN-1) = OPB_i(SDLEN-1)) else
156
    OPA_i(SDLEN-1) when (SU_i = '1') else
157
    OPB_i(SDLEN-1) ;
158
 
159
  ------------------------------------
160
  -- Set branch/jump execute flag
161
  ------------------------------------
162
 
163
  process(BJ_OP_i,AEQB,ALTB,IV_i)
164
  begin
165
 
166
    if(ALTB = '1') then
167
 
168
      case BJ_OP_i is
169
 
170
        when BJ_BEQ =>
171
          BX <= AEQB and IV_i;
172
 
173
        when BJ_BNE =>
174
          BX <= not(AEQB) and IV_i;
175
 
176
        when BJ_BLT =>
177
          BX <= IV_i;
178
 
179
        when BJ_BGE =>
180
          BX <= '0';
181
 
182
        when BJ_JAL =>
183
          BX <= IV_i;
184
 
185
        when others =>
186
          BX <= '0';
187
 
188
      end case;
189
 
190
    else
191
 
192
      case BJ_OP_i is
193
 
194
        when BJ_BEQ =>
195
          BX <= AEQB and IV_i;
196
 
197
        when BJ_BNE =>
198
          BX <= not(AEQB) and IV_i;
199
 
200
        when BJ_BLT =>
201
          BX <= '0';
202
 
203
        when BJ_BGE =>
204
          BX <= IV_i;
205
 
206
        when BJ_JAL =>
207
          BX <= IV_i;
208
 
209
        when others =>
210
          BX <= '0';
211
 
212
      end case;
213
 
214
    end if;
215
  end process;
216
 
217
  ------------------------------------
218
  -- Set jalr execute flag
219
  ------------------------------------
220
 
221
  GJRPE0_1 : if(JRPE = '1') generate
222
 
223
  JX <= MPJRX_i;
224
 
225
  end generate;
226
 
227
  GJRPE0_0 : if(JRPE = '0') generate
228
 
229
  JX <= IV_i when (BJ_OP_i = BJ_JALR) else '0';
230
 
231
  end generate;
232
 
233
  ------------------------------------
234
  -- Set B/J execute flag
235
  ------------------------------------
236
 
237
  BJX <= (BX or JX);
238
 
239
  -- B/J execute flag and target address register.
240
  -- These registers are needed when a B/J is taken
241
  -- while fetch is stalled: in such condition B/J
242
  -- must deferred to first un-stalled cycle.
243
 
244
  process(CLK_i)
245
  begin
246
    if(CLK_i = '1' and CLK_i'event) then
247
      if(FSTLL_i = '1') then
248
        BJTA_q <= BJTA;
249
      end if;
250
      if(RST_i = '1' or FSTLL_i = '0') then
251
        BJX_q <= '0';
252
      elsif(FSTLL_i = '1') then
253
        BJX_q <= (BJX and IV_i);
254
      end if;
255
    end if;
256
  end process;
257
 
258
  -- A branch/jump is actually executed if:
259
  -- 1) there's a valid B/J instruction in IX1 stage, OR
260
  -- 2) there's a pending B/J.
261
 
262
  BJX_o <= BJX or BJX_q;
263
 
264
  BJTA_o <= BJTA;
265
 
266
end ARC;

powered by: WebSVN 2.1.0

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