[11] | 1 | module branchPredictionBuffer(clock,stall,inst_addr,update,branch_result,buffer_addr,buffer_offset, prediction); |
---|
| 2 | parameter PRED_BUFFER_SIZE = 4; // 128 lines with 4 entries/line |
---|
| 3 | parameter PRED_BUFFER_BITS = 2; |
---|
| 4 | input [PRED_BUFFER_BITS-1:0] inst_addr; // We need to decode up to 4 instructions. |
---|
| 5 | input branch_result; // Result of a branch calculation. |
---|
| 6 | input [PRED_BUFFER_BITS-1:0] buffer_addr; |
---|
| 7 | input [1:0] buffer_offset; |
---|
| 8 | input clock; |
---|
| 9 | input stall; // Stalls when instructions are not available. |
---|
| 10 | input update; // Tells the buffer that an update is ready. |
---|
| 11 | output [3:0] prediction; // Prediction bits sent out to decoder stage. |
---|
| 12 | |
---|
| 13 | reg [3:0] prediction; |
---|
| 14 | reg [1:0] state_bank0 [PRED_BUFFER_SIZE-1:0]; |
---|
| 15 | reg [1:0] state_bank1 [PRED_BUFFER_SIZE-1:0]; |
---|
| 16 | reg [1:0] state_bank2 [PRED_BUFFER_SIZE-1:0]; |
---|
| 17 | reg [1:0] state_bank3 [PRED_BUFFER_SIZE-1:0]; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | integer i; |
---|
| 21 | |
---|
| 22 | // synopsys translate_off |
---|
| 23 | // Always begin in a weak, not taken state |
---|
| 24 | initial begin |
---|
| 25 | for (i=0; i<PRED_BUFFER_SIZE; i=i+1) begin |
---|
| 26 | state_bank0[i] = 2'b01; |
---|
| 27 | state_bank1[i] = 2'b01; |
---|
| 28 | state_bank2[i] = 2'b01; |
---|
| 29 | state_bank3[i] = 2'b01; |
---|
| 30 | end // for (i=0; i<PRED_BUFFER_SIZE; i=i+1) |
---|
| 31 | prediction = 4'b0000; |
---|
| 32 | end // initial begin |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | // synopsys translate_on |
---|
| 36 | |
---|
| 37 | always @(posedge clock) begin |
---|
| 38 | if (!stall) begin |
---|
| 39 | if (state_bank3[inst_addr] > 1) |
---|
| 40 | prediction[3] = 1; |
---|
| 41 | else |
---|
| 42 | prediction[3] = 0; |
---|
| 43 | if (state_bank2[inst_addr] > 1) |
---|
| 44 | prediction[2] = 1; |
---|
| 45 | else |
---|
| 46 | prediction[2] = 0; |
---|
| 47 | if (state_bank1[inst_addr] > 1) |
---|
| 48 | prediction[1] = 1; |
---|
| 49 | else |
---|
| 50 | prediction[1] = 0; |
---|
| 51 | if (state_bank0[inst_addr] > 1) |
---|
| 52 | prediction[0] = 1; |
---|
| 53 | else |
---|
| 54 | prediction[0] = 0; |
---|
| 55 | end // if (!stall) |
---|
| 56 | end // always @ (posedge clock && !stall) |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | // Assuming here that reading and updating occur at different parts of |
---|
| 60 | // the clock cycle. We only need to update one location at a time. |
---|
| 61 | always @(negedge clock) begin |
---|
| 62 | if (update) begin |
---|
| 63 | if (branch_result) begin // The branch was taken. |
---|
| 64 | if (buffer_offset == 0) |
---|
| 65 | state_bank0[buffer_addr] = state_bank0[buffer_addr] + 1; |
---|
| 66 | else if (buffer_offset == 1) |
---|
| 67 | state_bank1[buffer_addr] = state_bank1[buffer_addr] + 1; |
---|
| 68 | else if (buffer_offset == 2) |
---|
| 69 | state_bank2[buffer_addr] = state_bank2[buffer_addr] + 1; |
---|
| 70 | else |
---|
| 71 | state_bank3[buffer_addr] = state_bank3[buffer_addr] + 1; |
---|
| 72 | end // if (branch_result) |
---|
| 73 | |
---|
| 74 | else begin |
---|
| 75 | if (buffer_offset == 0) |
---|
| 76 | state_bank0[buffer_addr] = state_bank0[buffer_addr] - 1; |
---|
| 77 | else if (buffer_offset == 1) |
---|
| 78 | state_bank1[buffer_addr] = state_bank1[buffer_addr] - 1; |
---|
| 79 | else if (buffer_offset == 2) |
---|
| 80 | state_bank2[buffer_addr] = state_bank2[buffer_addr] - 1; |
---|
| 81 | else |
---|
| 82 | state_bank3[buffer_addr] = state_bank3[buffer_addr] - 1; |
---|
| 83 | end // else: !if(branch_result) |
---|
| 84 | end // if (update) |
---|
| 85 | end // always @ (negedge clock && !stall && update) |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | endmodule // branchPredictionBuffer |
---|