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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [twoway/] [FT64_BranchPredicator.v] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 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 58 robfinch
    xisBranch0, xisBranch1, xisBranch2,
29
    pcA, pcB, pcC, pcD, pcE, pcF, xpc0, xpc1, xpc2, takb0, takb1, takb2,
30 57 robfinch
    predict_takenA, predict_takenB, predict_takenC, predict_takenD,
31
    predict_takenE, predict_takenF);
32 48 robfinch
parameter DBW=32;
33
input rst;
34
input clk;
35
input en;
36
input xisBranch0;
37
input xisBranch1;
38 58 robfinch
input xisBranch2;
39 48 robfinch
input [DBW-1:0] pcA;
40
input [DBW-1:0] pcB;
41
input [DBW-1:0] pcC;
42
input [DBW-1:0] pcD;
43 57 robfinch
input [DBW-1:0] pcE;
44
input [DBW-1:0] pcF;
45 48 robfinch
input [DBW-1:0] xpc0;
46
input [DBW-1:0] xpc1;
47 58 robfinch
input [DBW-1:0] xpc2;
48 48 robfinch
input takb0;
49
input takb1;
50 58 robfinch
input takb2;
51 48 robfinch
output predict_takenA;
52
output predict_takenB;
53
output predict_takenC;
54
output predict_takenD;
55 57 robfinch
output predict_takenE;
56
output predict_takenF;
57 48 robfinch
 
58
integer n;
59
reg [31:0] pcs [0:31];
60
reg [31:0] pc;
61
reg takb;
62
reg [4:0] pcshead,pcstail;
63
reg wrhist;
64
reg [2:0] gbl_branch_hist;
65
reg [1:0] branch_history_table [511:0];
66
// For simulation only, initialize the history table to zeros.
67
// In the real world we don't care.
68
initial begin
69
    gbl_branch_hist = 3'b000;
70
        for (n = 0; n < 512; n = n + 1)
71
                branch_history_table[n] = 3;
72
end
73 58 robfinch
wire [8:0] bht_wa = {pc[7:1],gbl_branch_hist[2:1]};              // write address
74
wire [8:0] bht_raA = {pcA[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
75
wire [8:0] bht_raB = {pcB[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
76
wire [8:0] bht_raC = {pcC[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
77
wire [8:0] bht_raD = {pcD[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
78
wire [8:0] bht_raE = {pcE[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
79
wire [8:0] bht_raF = {pcF[7:1],gbl_branch_hist[2:1]};    // read address (IF stage)
80 48 robfinch
wire [1:0] bht_xbits = branch_history_table[bht_wa];
81
wire [1:0] bht_ibitsA = branch_history_table[bht_raA];
82
wire [1:0] bht_ibitsB = branch_history_table[bht_raB];
83
wire [1:0] bht_ibitsC = branch_history_table[bht_raC];
84
wire [1:0] bht_ibitsD = branch_history_table[bht_raD];
85 57 robfinch
wire [1:0] bht_ibitsE = branch_history_table[bht_raE];
86
wire [1:0] bht_ibitsF = branch_history_table[bht_raF];
87 48 robfinch
assign predict_takenA = (bht_ibitsA==2'd0 || bht_ibitsA==2'd1) && en;
88
assign predict_takenB = (bht_ibitsB==2'd0 || bht_ibitsB==2'd1) && en;
89
assign predict_takenC = (bht_ibitsC==2'd0 || bht_ibitsC==2'd1) && en;
90
assign predict_takenD = (bht_ibitsD==2'd0 || bht_ibitsD==2'd1) && en;
91 57 robfinch
assign predict_takenE = (bht_ibitsE==2'd0 || bht_ibitsE==2'd1) && en;
92
assign predict_takenF = (bht_ibitsF==2'd0 || bht_ibitsF==2'd1) && en;
93 48 robfinch
 
94
always @(posedge clk)
95
if (rst)
96
        pcstail <= 5'd0;
97
else begin
98 58 robfinch
        case({xisBranch0,xisBranch1,xisBranch2})
99
        3'b000: ;
100
        3'b001:
101
                begin
102
                pcs[pcstail] <= {xpc2[31:1],takb2};
103
                pcstail <= pcstail + 5'd1;
104
                end
105
        3'b010:
106
                begin
107
                pcs[pcstail] <= {xpc1[31:1],takb1};
108
                pcstail <= pcstail + 5'd1;
109
                end
110
        3'b011:
111
                begin
112
                pcs[pcstail] <= {xpc1[31:1],takb1};
113
                pcs[pcstail+1] <= {xpc2[31:1],takb2};
114
                pcstail <= pcstail + 5'd2;
115
                end
116
        3'b100:
117
                begin
118 48 robfinch
                pcs[pcstail] <= {xpc0[31:1],takb0};
119 58 robfinch
                pcstail <= pcstail + 5'd1;
120
                end
121
        3'b101:
122
                begin
123
                pcs[pcstail] <= {xpc0[31:1],takb0};
124
                pcs[pcstail+1] <= {xpc2[31:1],takb2};
125
                pcstail <= pcstail + 5'd2;
126
                end
127
        3'b110:
128
                begin
129
                pcs[pcstail] <= {xpc0[31:1],takb0};
130 48 robfinch
                pcs[pcstail+1] <= {xpc1[31:1],takb1};
131
                pcstail <= pcstail + 5'd2;
132 58 robfinch
                end
133
        3'b111:
134
                begin
135 48 robfinch
                pcs[pcstail] <= {xpc0[31:1],takb0};
136 58 robfinch
                pcs[pcstail+1] <= {xpc1[31:1],takb1};
137
                pcs[pcstail+2] <= {xpc2[31:1],takb2};
138
                pcstail <= pcstail + 5'd3;
139
                end
140
        endcase
141 48 robfinch
end
142
 
143
always @(posedge clk)
144
if (rst)
145
        pcshead <= 5'd0;
146
else begin
147
        wrhist <= 1'b0;
148
        if (pcshead != pcstail) begin
149
                pc <= pcs[pcshead];
150
                takb <= pcs[pcshead][0];
151
                wrhist <= 1'b1;
152
                pcshead <= pcshead + 5'd1;
153
        end
154
end
155
 
156
// Two bit saturating counter
157
// If taking a branch in commit0 then a following branch
158
// in commit1 is never encountered. So only update for
159
// commit1 if commit0 is not taken.
160
reg [1:0] xbits_new;
161
always @*
162 52 robfinch
if (wrhist) begin
163
        if (takb) begin
164
                if (bht_xbits != 2'd1)
165
                        xbits_new <= bht_xbits + 2'd1;
166
                else
167
                        xbits_new <= bht_xbits;
168
        end
169
        else begin
170
                if (bht_xbits != 2'd2)
171
                        xbits_new <= bht_xbits - 2'd1;
172
                else
173
                        xbits_new <= bht_xbits;
174
        end
175 48 robfinch
end
176 52 robfinch
else
177
        xbits_new <= bht_xbits;
178 48 robfinch
 
179
always @(posedge clk)
180
if (rst)
181
        gbl_branch_hist <= 3'b000;
182
else begin
183 58 robfinch
  if (en) begin
184
    if (wrhist) begin
185
      gbl_branch_hist <= {gbl_branch_hist[1:0],takb};
186
      branch_history_table[bht_wa] <= xbits_new;
187
    end
188 48 robfinch
        end
189
end
190
 
191
endmodule
192
 

powered by: WebSVN 2.1.0

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