| 1 |
30 |
robfinch |
`include "Raptor64_opcodes.v"
|
| 2 |
|
|
`timescale 1ns / 1ps
|
| 3 |
|
|
//=============================================================================
|
| 4 |
|
|
// __
|
| 5 |
41 |
robfinch |
// \\__/ o\ (C) 2011-2013 Robert Finch, Stratford
|
| 6 |
30 |
robfinch |
// \ __ / All rights reserved.
|
| 7 |
|
|
// \/_// robfinch<remove>@opencores.org
|
| 8 |
|
|
// ||
|
| 9 |
|
|
//
|
| 10 |
|
|
// Raptor64_BranchHistory.v
|
| 11 |
|
|
//
|
| 12 |
|
|
//
|
| 13 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
| 14 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
| 15 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
| 16 |
|
|
// (at your option) any later version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// This source file is distributed in the hope that it will be useful,
|
| 19 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 20 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 21 |
|
|
// GNU General Public License for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 25 |
|
|
//
|
| 26 |
|
|
//
|
| 27 |
|
|
//=============================================================================
|
| 28 |
|
|
//
|
| 29 |
48 |
robfinch |
module Raptor64_BranchHistory(rst, clk, advanceX, xIR, pc, xpc, takb, predict_taken);
|
| 30 |
30 |
robfinch |
input rst;
|
| 31 |
|
|
input clk;
|
| 32 |
|
|
input advanceX;
|
| 33 |
41 |
robfinch |
input [31:0] xIR;
|
| 34 |
30 |
robfinch |
input [63:0] pc;
|
| 35 |
|
|
input [63:0] xpc;
|
| 36 |
|
|
input takb;
|
| 37 |
|
|
output predict_taken;
|
| 38 |
|
|
|
| 39 |
|
|
integer n;
|
| 40 |
|
|
reg [2:0] gbl_branch_hist;
|
| 41 |
|
|
reg [1:0] branch_history_table [255:0];
|
| 42 |
|
|
// For simulation only, initialize the history table to zeros.
|
| 43 |
|
|
// In the real world we don't care.
|
| 44 |
|
|
initial begin
|
| 45 |
|
|
for (n = 0; n < 256; n = n + 1)
|
| 46 |
|
|
branch_history_table[n] = 0;
|
| 47 |
|
|
end
|
| 48 |
41 |
robfinch |
wire [7:0] bht_wa = {xpc[7:2],gbl_branch_hist[2:1]}; // write address
|
| 49 |
|
|
wire [7:0] bht_ra1 = {xpc[7:2],gbl_branch_hist[2:1]}; // read address (EX stage)
|
| 50 |
|
|
wire [7:0] bht_ra2 = {pc[7:2],gbl_branch_hist[2:1]}; // read address (IF stage)
|
| 51 |
30 |
robfinch |
wire [1:0] bht_xbits = branch_history_table[bht_ra1];
|
| 52 |
|
|
wire [1:0] bht_ibits = branch_history_table[bht_ra2];
|
| 53 |
|
|
assign predict_taken = bht_ibits==2'd0 || bht_ibits==2'd1;
|
| 54 |
|
|
|
| 55 |
41 |
robfinch |
wire [6:0] xOpcode = xIR[31:25];
|
| 56 |
30 |
robfinch |
wire isxBranchI = (xOpcode==`BEQI || xOpcode==`BNEI ||
|
| 57 |
|
|
xOpcode==`BLTI || xOpcode==`BLEI || xOpcode==`BGTI || xOpcode==`BGEI ||
|
| 58 |
|
|
xOpcode==`BLTUI || xOpcode==`BLEUI || xOpcode==`BGTUI || xOpcode==`BGEUI)
|
| 59 |
|
|
;
|
| 60 |
48 |
robfinch |
wire isxBranch = (isxBranchI || xOpcode==`TRAPcc || xOpcode==`TRAPcci || xOpcode==`BTRI || xOpcode==`BTRR);
|
| 61 |
30 |
robfinch |
|
| 62 |
|
|
// Two bit saturating counter
|
| 63 |
|
|
reg [1:0] xbits_new;
|
| 64 |
|
|
always @(takb or bht_xbits)
|
| 65 |
|
|
if (takb) begin
|
| 66 |
|
|
if (bht_xbits != 2'd1)
|
| 67 |
|
|
xbits_new <= bht_xbits + 2'd1;
|
| 68 |
|
|
else
|
| 69 |
|
|
xbits_new <= bht_xbits;
|
| 70 |
|
|
end
|
| 71 |
|
|
else begin
|
| 72 |
|
|
if (bht_xbits != 2'd2)
|
| 73 |
|
|
xbits_new <= bht_xbits - 2'd1;
|
| 74 |
|
|
else
|
| 75 |
|
|
xbits_new <= bht_xbits;
|
| 76 |
|
|
end
|
| 77 |
|
|
|
| 78 |
|
|
always @(posedge clk)
|
| 79 |
|
|
if (rst)
|
| 80 |
|
|
gbl_branch_hist <= 3'b000;
|
| 81 |
|
|
else begin
|
| 82 |
|
|
if (advanceX) begin
|
| 83 |
|
|
if (isxBranch) begin
|
| 84 |
|
|
gbl_branch_hist <= {gbl_branch_hist[1:0],takb};
|
| 85 |
|
|
branch_history_table[bht_wa] <= xbits_new;
|
| 86 |
|
|
end
|
| 87 |
|
|
end
|
| 88 |
|
|
end
|
| 89 |
|
|
|
| 90 |
|
|
endmodule
|
| 91 |
|
|
|