| 1 |
43 |
robfinch |
//=============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2013-2018 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// FT64_BranchPredictor.v
|
| 9 |
|
|
//
|
| 10 |
|
|
//
|
| 11 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
| 12 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
| 13 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
| 14 |
|
|
// (at your option) any later version.
|
| 15 |
|
|
//
|
| 16 |
|
|
// This source file is distributed in the hope that it will be useful,
|
| 17 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 18 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 19 |
|
|
// GNU General Public License for more details.
|
| 20 |
|
|
//
|
| 21 |
|
|
// You should have received a copy of the GNU General Public License
|
| 22 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 23 |
|
|
//
|
| 24 |
|
|
//
|
| 25 |
|
|
//=============================================================================
|
| 26 |
|
|
//
|
| 27 |
|
|
module FT64_BranchPredictor(rst, clk, en,
|
| 28 |
|
|
xisBranch0, xisBranch1,
|
| 29 |
|
|
pcA, pcB, pcC, pcD, xpc0, xpc1, takb0, takb1,
|
| 30 |
|
|
predict_takenA, predict_takenB, predict_takenC, predict_takenD);
|
| 31 |
|
|
parameter DBW=32;
|
| 32 |
|
|
input rst;
|
| 33 |
|
|
input clk;
|
| 34 |
|
|
input en;
|
| 35 |
|
|
input xisBranch0;
|
| 36 |
|
|
input xisBranch1;
|
| 37 |
|
|
input [DBW-1:0] pcA;
|
| 38 |
|
|
input [DBW-1:0] pcB;
|
| 39 |
|
|
input [DBW-1:0] pcC;
|
| 40 |
|
|
input [DBW-1:0] pcD;
|
| 41 |
|
|
input [DBW-1:0] xpc0;
|
| 42 |
|
|
input [DBW-1:0] xpc1;
|
| 43 |
|
|
input takb0;
|
| 44 |
|
|
input takb1;
|
| 45 |
|
|
output predict_takenA;
|
| 46 |
|
|
output predict_takenB;
|
| 47 |
|
|
output predict_takenC;
|
| 48 |
|
|
output predict_takenD;
|
| 49 |
|
|
|
| 50 |
|
|
integer n;
|
| 51 |
|
|
reg [31:0] pcs [0:31];
|
| 52 |
|
|
reg [31:0] pc;
|
| 53 |
|
|
reg takb;
|
| 54 |
|
|
reg [4:0] pcshead,pcstail;
|
| 55 |
|
|
reg wrhist;
|
| 56 |
|
|
reg [2:0] gbl_branch_hist;
|
| 57 |
|
|
reg [1:0] branch_history_table [511:0];
|
| 58 |
|
|
// For simulation only, initialize the history table to zeros.
|
| 59 |
|
|
// In the real world we don't care.
|
| 60 |
|
|
initial begin
|
| 61 |
|
|
gbl_branch_hist = 3'b000;
|
| 62 |
|
|
for (n = 0; n < 512; n = n + 1)
|
| 63 |
|
|
branch_history_table[n] = 3;
|
| 64 |
|
|
end
|
| 65 |
|
|
wire [8:0] bht_wa = {pc[8:2],gbl_branch_hist[2:1]}; // write address
|
| 66 |
|
|
wire [8:0] bht_raA = {pcA[8:2],gbl_branch_hist[2:1]}; // read address (IF stage)
|
| 67 |
|
|
wire [8:0] bht_raB = {pcB[8:2],gbl_branch_hist[2:1]}; // read address (IF stage)
|
| 68 |
|
|
wire [8:0] bht_raC = {pcC[8:2],gbl_branch_hist[2:1]}; // read address (IF stage)
|
| 69 |
|
|
wire [8:0] bht_raD = {pcD[8:2],gbl_branch_hist[2:1]}; // read address (IF stage)
|
| 70 |
|
|
wire [1:0] bht_xbits = branch_history_table[bht_wa];
|
| 71 |
|
|
wire [1:0] bht_ibitsA = branch_history_table[bht_raA];
|
| 72 |
|
|
wire [1:0] bht_ibitsB = branch_history_table[bht_raB];
|
| 73 |
|
|
wire [1:0] bht_ibitsC = branch_history_table[bht_raC];
|
| 74 |
|
|
wire [1:0] bht_ibitsD = branch_history_table[bht_raD];
|
| 75 |
|
|
assign predict_takenA = (bht_ibitsA==2'd0 || bht_ibitsA==2'd1) && en;
|
| 76 |
|
|
assign predict_takenB = (bht_ibitsB==2'd0 || bht_ibitsB==2'd1) && en;
|
| 77 |
|
|
assign predict_takenC = (bht_ibitsC==2'd0 || bht_ibitsC==2'd1) && en;
|
| 78 |
|
|
assign predict_takenD = (bht_ibitsD==2'd0 || bht_ibitsD==2'd1) && en;
|
| 79 |
|
|
|
| 80 |
|
|
always @(posedge clk)
|
| 81 |
|
|
if (rst)
|
| 82 |
|
|
pcstail <= 5'd0;
|
| 83 |
|
|
else begin
|
| 84 |
|
|
if (xisBranch0 & xisBranch1) begin
|
| 85 |
|
|
pcs[pcstail] <= {xpc0[31:1],takb0};
|
| 86 |
|
|
pcs[pcstail+1] <= {xpc1[31:1],takb1};
|
| 87 |
|
|
pcstail <= pcstail + 5'd2;
|
| 88 |
|
|
end
|
| 89 |
|
|
else if (xisBranch0) begin
|
| 90 |
|
|
pcs[pcstail] <= {xpc0[31:1],takb0};
|
| 91 |
|
|
pcstail <= pcstail + 5'd1;
|
| 92 |
|
|
end
|
| 93 |
|
|
else if (xisBranch1) begin
|
| 94 |
|
|
pcs[pcstail] <= {xpc1[31:1],takb1};
|
| 95 |
|
|
pcstail <= pcstail + 5'd1;
|
| 96 |
|
|
end
|
| 97 |
|
|
end
|
| 98 |
|
|
|
| 99 |
|
|
always @(posedge clk)
|
| 100 |
|
|
if (rst)
|
| 101 |
|
|
pcshead <= 5'd0;
|
| 102 |
|
|
else begin
|
| 103 |
|
|
wrhist <= 1'b0;
|
| 104 |
|
|
if (pcshead != pcstail) begin
|
| 105 |
|
|
pc <= pcs[pcshead];
|
| 106 |
|
|
takb <= pcs[pcshead][0];
|
| 107 |
|
|
wrhist <= 1'b1;
|
| 108 |
|
|
pcshead <= pcshead + 5'd1;
|
| 109 |
|
|
end
|
| 110 |
|
|
end
|
| 111 |
|
|
|
| 112 |
|
|
// Two bit saturating counter
|
| 113 |
|
|
// If taking a branch in commit0 then a following branch
|
| 114 |
|
|
// in commit1 is never encountered. So only update for
|
| 115 |
|
|
// commit1 if commit0 is not taken.
|
| 116 |
|
|
reg [1:0] xbits_new;
|
| 117 |
|
|
always @*
|
| 118 |
|
|
if (takb & wrhist) begin
|
| 119 |
|
|
if (bht_xbits != 2'd1)
|
| 120 |
|
|
xbits_new <= bht_xbits + 2'd1;
|
| 121 |
|
|
else
|
| 122 |
|
|
xbits_new <= bht_xbits;
|
| 123 |
|
|
end
|
| 124 |
|
|
else begin
|
| 125 |
|
|
if (bht_xbits != 2'd2)
|
| 126 |
|
|
xbits_new <= bht_xbits - {1'b0,wrhist};
|
| 127 |
|
|
else
|
| 128 |
|
|
xbits_new <= bht_xbits;
|
| 129 |
|
|
end
|
| 130 |
|
|
|
| 131 |
|
|
always @(posedge clk)
|
| 132 |
|
|
if (rst)
|
| 133 |
|
|
gbl_branch_hist <= 3'b000;
|
| 134 |
|
|
else begin
|
| 135 |
|
|
if (en) begin
|
| 136 |
|
|
if (wrhist) begin
|
| 137 |
|
|
gbl_branch_hist <= {gbl_branch_hist[1:0],takb};
|
| 138 |
|
|
branch_history_table[bht_wa] <= xbits_new;
|
| 139 |
|
|
end
|
| 140 |
|
|
end
|
| 141 |
|
|
end
|
| 142 |
|
|
|
| 143 |
|
|
endmodule
|
| 144 |
|
|
|