data/full_repos/permissive/211235278/06. Tournament Prediction CPU/TournamentPredictor.v
1: b'%Error: Cannot find file containing module: Tournament\n        ... Looked in:\n             data/full_repos/permissive/211235278/06./Tournament\n             data/full_repos/permissive/211235278/06./Tournament.v\n             data/full_repos/permissive/211235278/06./Tournament.sv\n             Tournament\n             Tournament.v\n             Tournament.sv\n             obj_dir/Tournament\n             obj_dir/Tournament.v\n             obj_dir/Tournament.sv\n%Error: Cannot find file containing module: Prediction\n%Error: Cannot find file containing module: CPU,data/full_repos/permissive/211235278\n%Error: Cannot find file containing module: data/full_repos/permissive/211235278/06.\n%Error: Cannot find file containing module: CPU/TournamentPredictor.v\n%Error: Exiting due to 5 error(s)\n'
module TournamentPredictor (
    input clk,
    input reset_n,
    input IsControl,
    input [`WORD_SIZE-1:0] PC,
    input JumpResolved, BranchResolved, BranchTaken,
    input [`WORD_SIZE-1:0] ResolvedJumpPC, ResolvedBranchPC,
    input [`WORD_SIZE-1:0] ActualJumpTarget, ActualBranchTarget,
    output [`WORD_SIZE-1:0] Prediction
    );

    

    integer i, j;
    

    reg [`WORD_SIZE-1:0] BTB [255:0];
    reg [7:0] TagTable [255:0];
    

    reg [7:0] LHT [255:0];
    reg [1:0] LP [255:0];
    

    reg [11:0] GHSR;
    

    reg [1:0] GP [1023:0];
    

    reg [1:0] CP [1023:0];
    

    wire GlobalPredictTaken, LocalPredictTaken, ChosenPredictTaken, PredictTaken;
    

    reg [1:0] LocalPredictionHistory, ChoicePredictionHistory;
    
    always @(posedge clk) begin
        if (!reset_n) begin
            for (i=0; i<256; i=i+1) begin
                BTB[i] <= 0;
                TagTable[i] <= 8'hff;
                LHT[i] <= 0;
                LP[i] <= 1;
            end
            for (j=0; j<1024; j=j+1) begin
                GP[j] <= 1;
                CP[j] <= 1;
            end
            GHSR <= 0;
            LocalPredictionHistory <= 0;
            ChoicePredictionHistory <= 0;
        end else begin
            if (IsControl) GHSR <= {GHSR[10:0], PredictTaken};
            LocalPredictionHistory <= {LocalPredictionHistory[0], LocalPredictTaken};
            ChoicePredictionHistory <= {ChoicePredictionHistory[0], ChosenPredictTaken};
        end
    end
    

    assign GlobalPredictTaken = (GP[GHSR[9:0]] >= 2);
    assign LocalPredictTaken = (LP[LHT[PC[7:0]]] >= 2);
    assign ChosenPredictTaken = (CP[GHSR[9:0]] >= 2 ? GlobalPredictTaken : LocalPredictTaken);
    assign PredictTaken = (TagTable[PC[7:0]] == PC[15:8]) && ChosenPredictTaken;
    assign Prediction = PredictTaken ? BTB[PC[7:0]] : PC+1;
    

    always @(negedge clk) begin
        if (BranchResolved && JumpResolved) begin
            if (BranchTaken) begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] + 1;
                end

                if (GP[GHSR[11:2]] != 3) begin
                    GP[GHSR[11:2]] <= GP[GHSR[11:2]] + 1;
                end
            end else begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 0) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] - 1;
                end

                if (GP[GHSR[11:2]] != 0) begin
                    GP[GHSR[11:2]] <= GP[GHSR[11:2]] - 1;
                end
            end
            

            if (ChoicePredictionHistory[1] == GHSR[1]) begin
                if (GHSR[1] != BranchTaken && LocalPredictionHistory[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 0) CP[GHSR[11:2]] <= CP[GHSR[11:2]] - 1;
                end
            end else if (ChoicePredictionHistory[1] == LocalPredictionHistory[1]) begin
                if (LocalPredictionHistory[1] != BranchTaken && GHSR[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 3) CP[GHSR[11:2]] <= CP[GHSR[11:2]] + 1;
                end
            end
            

            LHT[ResolvedBranchPC[7:0]] <= { LHT[ResolvedBranchPC[7:0]][6:0], BranchTaken};
            

            TagTable[ResolvedBranchPC[7:0]] <= ResolvedBranchPC[15:8];
            BTB[ResolvedBranchPC[7:0]] <= ActualBranchTarget;
            

            GHSR[1] <= BranchTaken;
            
            if (BranchTaken == GHSR[1]) begin

                if (LP[LHT[ResolvedJumpPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedJumpPC[7:0]]] <= LP[LHT[ResolvedJumpPC[7:0]]] + 1;
                end

                if (GP[GHSR[10:1]] != 3) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
                end

                if (CP[GHSR[10:1]] != 3) begin
                    CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                end
                

                if (ChoicePredictionHistory[0] == GHSR[0]) begin
                    if (GHSR[0] != BranchTaken && LocalPredictionHistory[0] == BranchTaken) begin
                        if (CP[GHSR[10:1]] != 0) CP[GHSR[10:1]] <= CP[GHSR[10:1]] - 1;
                    end
                end else if (ChoicePredictionHistory[0] == LocalPredictionHistory[0]) begin
                    if (LocalPredictionHistory[0] != BranchTaken && GHSR[0] == BranchTaken) begin
                        if (CP[GHSR[10:1]] != 3) CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                    end
                end
                

                LHT[ResolvedJumpPC[7:0]] <= { LHT[ResolvedJumpPC[7:0]][6:0], 1'b1};
                

                TagTable[ResolvedJumpPC[7:0]] <= ResolvedJumpPC[15:8];
                BTB[ResolvedJumpPC[7:0]] <= ActualJumpTarget;
                

                GHSR[0] <= 1'b1;
            end
            
        end
        
        else if (BranchResolved && !JumpResolved) begin
            if (BranchTaken) begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] + 1;
                end

                if (GP[GHSR[10:1]] != 3) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
                end
            end else begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 0) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] - 1;
                end

                if (GP[GHSR[10:1]] != 0) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] - 1;
                end
            end
            

            if (ChoicePredictionHistory[1] == GHSR[1]) begin
                if (GHSR[1] != BranchTaken && LocalPredictionHistory[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 0) CP[GHSR[11:2]] <= CP[GHSR[11:2]] - 1;
                end
            end else if (ChoicePredictionHistory[1] == LocalPredictionHistory[1]) begin
                if (LocalPredictionHistory[1] != BranchTaken && GHSR[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 3) CP[GHSR[11:2]] <= CP[GHSR[11:2]] + 1;
                end
            end
            

            LHT[ResolvedBranchPC[7:0]] <= { LHT[ResolvedBranchPC[7:0]][6:0], BranchTaken};
            

            TagTable[ResolvedBranchPC[7:0]] <= ResolvedBranchPC[15:8];
            BTB[ResolvedBranchPC[7:0]] <= ActualBranchTarget;
            

            GHSR[1] <= BranchTaken;
        end
        
        else if (JumpResolved && !BranchResolved) begin

            if (LP[LHT[ResolvedJumpPC[7:0]]] != 3) begin
                LP[LHT[ResolvedJumpPC[7:0]]] <= LP[LHT[ResolvedJumpPC[7:0]]] + 1;
            end

            if (GP[GHSR[10:1]] != 3) begin
                GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
            end

            if (CP[GHSR[10:1]] != 3) begin
                CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
            end
            

            if (ChoicePredictionHistory[0] == GHSR[0]) begin
                if (GHSR[0] != BranchTaken && LocalPredictionHistory[0] == BranchTaken) begin
                    if (CP[GHSR[10:1]] != 0) CP[GHSR[10:1]] <= CP[GHSR[10:1]] - 1;
                end
            end else if (ChoicePredictionHistory[0] == LocalPredictionHistory[0]) begin
                if (LocalPredictionHistory[0] != BranchTaken && GHSR[0] == BranchTaken) begin
                    if (CP[GHSR[10:1]] != 3) CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                end
            end
            

            LHT[ResolvedJumpPC[7:0]] <= { LHT[ResolvedJumpPC[7:0]][6:0], 1'b1};
            

            TagTable[ResolvedJumpPC[7:0]] <= ResolvedJumpPC[15:8];
            BTB[ResolvedJumpPC[7:0]] <= ActualJumpTarget;
            

            GHSR[0] <= 1'b1;
        end
    end
    
endmodule
module TournamentPredictor (
    input clk,
    input reset_n,
    input IsControl,
    input [`WORD_SIZE-1:0] PC,
    input JumpResolved, BranchResolved, BranchTaken,
    input [`WORD_SIZE-1:0] ResolvedJumpPC, ResolvedBranchPC,
    input [`WORD_SIZE-1:0] ActualJumpTarget, ActualBranchTarget,
    output [`WORD_SIZE-1:0] Prediction
    );


    

    integer i, j;
    

    reg [`WORD_SIZE-1:0] BTB [255:0];
    reg [7:0] TagTable [255:0];
    

    reg [7:0] LHT [255:0];
    reg [1:0] LP [255:0];
    

    reg [11:0] GHSR;
    

    reg [1:0] GP [1023:0];
    

    reg [1:0] CP [1023:0];
    

    wire GlobalPredictTaken, LocalPredictTaken, ChosenPredictTaken, PredictTaken;
    

    reg [1:0] LocalPredictionHistory, ChoicePredictionHistory;
    
    always @(posedge clk) begin
        if (!reset_n) begin
            for (i=0; i<256; i=i+1) begin
                BTB[i] <= 0;
                TagTable[i] <= 8'hff;
                LHT[i] <= 0;
                LP[i] <= 1;
            end
            for (j=0; j<1024; j=j+1) begin
                GP[j] <= 1;
                CP[j] <= 1;
            end
            GHSR <= 0;
            LocalPredictionHistory <= 0;
            ChoicePredictionHistory <= 0;
        end else begin
            if (IsControl) GHSR <= {GHSR[10:0], PredictTaken};
            LocalPredictionHistory <= {LocalPredictionHistory[0], LocalPredictTaken};
            ChoicePredictionHistory <= {ChoicePredictionHistory[0], ChosenPredictTaken};
        end
    end
    

    assign GlobalPredictTaken = (GP[GHSR[9:0]] >= 2);
    assign LocalPredictTaken = (LP[LHT[PC[7:0]]] >= 2);
    assign ChosenPredictTaken = (CP[GHSR[9:0]] >= 2 ? GlobalPredictTaken : LocalPredictTaken);
    assign PredictTaken = (TagTable[PC[7:0]] == PC[15:8]) && ChosenPredictTaken;
    assign Prediction = PredictTaken ? BTB[PC[7:0]] : PC+1;
    

    always @(negedge clk) begin
        if (BranchResolved && JumpResolved) begin
            if (BranchTaken) begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] + 1;
                end

                if (GP[GHSR[11:2]] != 3) begin
                    GP[GHSR[11:2]] <= GP[GHSR[11:2]] + 1;
                end
            end else begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 0) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] - 1;
                end

                if (GP[GHSR[11:2]] != 0) begin
                    GP[GHSR[11:2]] <= GP[GHSR[11:2]] - 1;
                end
            end
            

            if (ChoicePredictionHistory[1] == GHSR[1]) begin
                if (GHSR[1] != BranchTaken && LocalPredictionHistory[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 0) CP[GHSR[11:2]] <= CP[GHSR[11:2]] - 1;
                end
            end else if (ChoicePredictionHistory[1] == LocalPredictionHistory[1]) begin
                if (LocalPredictionHistory[1] != BranchTaken && GHSR[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 3) CP[GHSR[11:2]] <= CP[GHSR[11:2]] + 1;
                end
            end
            

            LHT[ResolvedBranchPC[7:0]] <= { LHT[ResolvedBranchPC[7:0]][6:0], BranchTaken};
            

            TagTable[ResolvedBranchPC[7:0]] <= ResolvedBranchPC[15:8];
            BTB[ResolvedBranchPC[7:0]] <= ActualBranchTarget;
            

            GHSR[1] <= BranchTaken;
            
            if (BranchTaken == GHSR[1]) begin

                if (LP[LHT[ResolvedJumpPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedJumpPC[7:0]]] <= LP[LHT[ResolvedJumpPC[7:0]]] + 1;
                end

                if (GP[GHSR[10:1]] != 3) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
                end

                if (CP[GHSR[10:1]] != 3) begin
                    CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                end
                

                if (ChoicePredictionHistory[0] == GHSR[0]) begin
                    if (GHSR[0] != BranchTaken && LocalPredictionHistory[0] == BranchTaken) begin
                        if (CP[GHSR[10:1]] != 0) CP[GHSR[10:1]] <= CP[GHSR[10:1]] - 1;
                    end
                end else if (ChoicePredictionHistory[0] == LocalPredictionHistory[0]) begin
                    if (LocalPredictionHistory[0] != BranchTaken && GHSR[0] == BranchTaken) begin
                        if (CP[GHSR[10:1]] != 3) CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                    end
                end
                

                LHT[ResolvedJumpPC[7:0]] <= { LHT[ResolvedJumpPC[7:0]][6:0], 1'b1};
                

                TagTable[ResolvedJumpPC[7:0]] <= ResolvedJumpPC[15:8];
                BTB[ResolvedJumpPC[7:0]] <= ActualJumpTarget;
                

                GHSR[0] <= 1'b1;
            end
            
        end
        
        else if (BranchResolved && !JumpResolved) begin
            if (BranchTaken) begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 3) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] + 1;
                end

                if (GP[GHSR[10:1]] != 3) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
                end
            end else begin

                if (LP[LHT[ResolvedBranchPC[7:0]]] != 0) begin
                    LP[LHT[ResolvedBranchPC[7:0]]] <= LP[LHT[ResolvedBranchPC[7:0]]] - 1;
                end

                if (GP[GHSR[10:1]] != 0) begin
                    GP[GHSR[10:1]] <= GP[GHSR[10:1]] - 1;
                end
            end
            

            if (ChoicePredictionHistory[1] == GHSR[1]) begin
                if (GHSR[1] != BranchTaken && LocalPredictionHistory[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 0) CP[GHSR[11:2]] <= CP[GHSR[11:2]] - 1;
                end
            end else if (ChoicePredictionHistory[1] == LocalPredictionHistory[1]) begin
                if (LocalPredictionHistory[1] != BranchTaken && GHSR[1] == BranchTaken) begin
                    if (CP[GHSR[11:2]] != 3) CP[GHSR[11:2]] <= CP[GHSR[11:2]] + 1;
                end
            end
            

            LHT[ResolvedBranchPC[7:0]] <= { LHT[ResolvedBranchPC[7:0]][6:0], BranchTaken};
            

            TagTable[ResolvedBranchPC[7:0]] <= ResolvedBranchPC[15:8];
            BTB[ResolvedBranchPC[7:0]] <= ActualBranchTarget;
            

            GHSR[1] <= BranchTaken;
        end
        
        else if (JumpResolved && !BranchResolved) begin

            if (LP[LHT[ResolvedJumpPC[7:0]]] != 3) begin
                LP[LHT[ResolvedJumpPC[7:0]]] <= LP[LHT[ResolvedJumpPC[7:0]]] + 1;
            end

            if (GP[GHSR[10:1]] != 3) begin
                GP[GHSR[10:1]] <= GP[GHSR[10:1]] + 1;
            end

            if (CP[GHSR[10:1]] != 3) begin
                CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
            end
            

            if (ChoicePredictionHistory[0] == GHSR[0]) begin
                if (GHSR[0] != BranchTaken && LocalPredictionHistory[0] == BranchTaken) begin
                    if (CP[GHSR[10:1]] != 0) CP[GHSR[10:1]] <= CP[GHSR[10:1]] - 1;
                end
            end else if (ChoicePredictionHistory[0] == LocalPredictionHistory[0]) begin
                if (LocalPredictionHistory[0] != BranchTaken && GHSR[0] == BranchTaken) begin
                    if (CP[GHSR[10:1]] != 3) CP[GHSR[10:1]] <= CP[GHSR[10:1]] + 1;
                end
            end
            

            LHT[ResolvedJumpPC[7:0]] <= { LHT[ResolvedJumpPC[7:0]][6:0], 1'b1};
            

            TagTable[ResolvedJumpPC[7:0]] <= ResolvedJumpPC[15:8];
            BTB[ResolvedJumpPC[7:0]] <= ActualJumpTarget;
            

            GHSR[0] <= 1'b1;
        end
    end
    
endmodule