Guest User

Untitled

a guest
May 17th, 2016
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From 9472b6f88f8d74841be7ce2fe1ebef28a20beb23 Mon Sep 17 00:00:00 2001
  2. From: "Alex L. White" <space.monkey.delivers@gmail.com>
  3. Date: Tue, 17 May 2016 13:13:15 +0300
  4. Subject: [PATCH] Introducing 4 state branch predictor
  5.  
  6. ---
  7.  rtl/verilog/mor1kx_branch_prediction.v | 84 ++++++++++++++++++++++++++--------
  8.  rtl/verilog/mor1kx_cpu_cappuccino.v    |  7 ++-
  9.  2 files changed, 71 insertions(+), 20 deletions(-)
  10.  
  11. diff --git a/rtl/verilog/mor1kx_branch_prediction.v b/rtl/verilog/mor1kx_branch_prediction.v
  12. index a82c1f9..dfce694 100644
  13. --- a/rtl/verilog/mor1kx_branch_prediction.v
  14. +++ b/rtl/verilog/mor1kx_branch_prediction.v
  15. @@ -18,35 +18,83 @@
  16.  
  17.  module mor1kx_branch_prediction
  18.    #(
  19. -    parameter OPTION_OPERAND_WIDTH = 32
  20. +    parameter OPTION_OPERAND_WIDTH = 32,
  21. +    parameter FEATURE_4_STATE_PREDICTOR = "NONE"
  22.      )
  23.     (
  24.      input  clk,
  25.      input  rst,
  26.  
  27.      // Signals belonging to the stage where the branch is predicted.
  28. -    input  op_bf_i,
  29. -    input  op_bnf_i,
  30. -    input [9:0] immjbr_upper_i,
  31. -    output     predicted_flag_o,
  32. +    input  op_bf_i,               // branch if flag
  33. +    input  op_bnf_i,              // branch if not flag
  34. +    input [9:0] immjbr_upper_i,    // branch offset
  35. +    output     predicted_flag_o,      //result of predictor
  36.  
  37.      // Signals belonging to the stage where the branch is resolved.
  38. -    input  prev_op_brcond_i,
  39. -    input  prev_predicted_flag_i,
  40. -    input  flag_i,
  41. +    input  prev_op_brcond_i,      // prev op was cond brn
  42. +    input  prev_predicted_flag_i, // prev insn predicated flag
  43. +    input  flag_i,                // prev insn real flag
  44.  
  45.      // Branch misprediction indicator
  46. -    output     branch_mispredict_o
  47. +    output     branch_mispredict_o    // result of prediction
  48.      );
  49.  
  50. -   // Compare the real flag with the previously predicted flag and signal a
  51. -   // misprediction in case of a mismatch.
  52. -   assign branch_mispredict_o = prev_op_brcond_i &
  53. -               (flag_i != prev_predicted_flag_i);
  54. -
  55. -   // Static branch prediction - backward branches are predicted as taken,
  56. -   // forward branches as not taken.
  57. -   assign predicted_flag_o = op_bf_i & immjbr_upper_i[9] |
  58. -                op_bnf_i & !immjbr_upper_i[9];
  59. +  // Compare the real flag with the previously predicted flag and signal a
  60. +  // misprediction in case of a mismatch.
  61. +  assign branch_mispredict_o = prev_op_brcond_i &
  62. +    (flag_i != prev_predicted_flag_i);
  63. +  
  64. +generate
  65. +if (FEATURE_4_STATE_PREDICTOR!="NONE") begin : branch_predictor_gen
  66. +  localparam [1:0]
  67. +    STATE_STRONGLY_NOT_TAKEN = 2'b00,
  68. +    STATE_WEAKLY_NOT_TAKEN   = 2'b01,
  69. +    STATE_WEAKLY_TAKEN       = 2'b10,
  70. +    STATE_STRONGLY_TAKEN     = 2'b11;
  71. +  
  72. +  reg [1:0] state = STATE_WEAKLY_TAKEN;
  73. +  
  74. +  assign predicted_flag_o = state[1];
  75. +  
  76. +  always @(posedge clk) begin
  77. +    if (rst) begin
  78. +      // set default state to STATE_WEAKLY_TAKEN
  79. +      state <= STATE_WEAKLY_TAKEN;
  80. +    end else begin
  81. +      // if prev insn was a branch
  82. +      if (prev_op_brcond_i) begin
  83. +        // if it was mispredicted
  84. +        if (branch_mispredict_o) begin
  85. +          // change fsm state:
  86. +          //   STATE_STRONGLY_TAKEN -> STATE_WEAKLY_TAKEN
  87. +          //   STATE_WEAKLY_TAKEN -> STATE_WEAKLY_NOT_TAKEN
  88. +          //   STATE_WEAKLY_NOT_TAKEN -> STATE_STRONGLY_NOT_TAKEN
  89. +          //   STATE_STRONGLY_NOT_TAKEN -> STATE_STRONGLY_NOT_TAKEN
  90. +          state <= (state == STATE_STRONGLY_TAKEN) ? STATE_WEAKLY_TAKEN :
  91. +                   (state == STATE_WEAKLY_TAKEN)   ? STATE_WEAKLY_NOT_TAKEN
  92. +                                                   : STATE_STRONGLY_NOT_TAKEN;
  93. +        // if prev insn was predicted correctly                                        
  94. +        end else begin
  95. +          // change fsm state:
  96. +          //   STATE_STRONGLY_NOT_TAKEN -> STATE_WEAKLY_NOT_TAKEN
  97. +          //   STATE_WEAKLY_NOT_TAKEN -> STATE_WEAKLY_TAKEN
  98. +          //   STATE_WEAKLY_TAKEN -> STATE_STRONGLY_TAKEN
  99. +          //   STATE_STRONGLY_TAKEN -> STATE_STRONGLY_TAKEN
  100. +          state <= (state == STATE_STRONGLY_NOT_TAKEN) ? STATE_WEAKLY_NOT_TAKEN :
  101. +                   (state == STATE_WEAKLY_NOT_TAKEN)   ? STATE_WEAKLY_TAKEN
  102. +                                                       : STATE_STRONGLY_TAKEN;
  103. +        end
  104. +      end
  105. +    end
  106. +  end
  107. +  
  108. +end else begin
  109. +  // Static branch prediction - backward branches are predicted as taken,
  110. +  // forward branches as not taken.
  111. +  assign predicted_flag_o = op_bf_i & immjbr_upper_i[9] |
  112. +    op_bnf_i & !immjbr_upper_i[9];
  113. +end
  114. +endgenerate
  115.  
  116.  endmodule
  117. diff --git a/rtl/verilog/mor1kx_cpu_cappuccino.v b/rtl/verilog/mor1kx_cpu_cappuccino.v
  118. index f282374..2af018e 100644
  119. --- a/rtl/verilog/mor1kx_cpu_cappuccino.v
  120. +++ b/rtl/verilog/mor1kx_cpu_cappuccino.v
  121. @@ -96,7 +96,9 @@ module mor1kx_cpu_cappuccino
  122.  
  123.      parameter FEATURE_MULTICORE = "NONE",
  124.  
  125. -    parameter FEATURE_TRACEPORT_EXEC = "NONE"
  126. +    parameter FEATURE_TRACEPORT_EXEC = "NONE",
  127. +    
  128. +    parameter FEATURE_4_STATE_PREDICTOR = "NONE"
  129.      )
  130.     (
  131.      input                clk,
  132. @@ -752,7 +754,8 @@ module mor1kx_cpu_cappuccino
  133.      );*/
  134.     mor1kx_branch_prediction
  135.       #(
  136. -       .OPTION_OPERAND_WIDTH(OPTION_OPERAND_WIDTH)
  137. +       .OPTION_OPERAND_WIDTH(OPTION_OPERAND_WIDTH),
  138. +       .FEATURE_4_STATE_PREDICTOR(FEATURE_4_STATE_PREDICTOR)
  139.         )
  140.     mor1kx_branch_prediction
  141.       (/*AUTOINST*/
  142. --
  143. 1.9.1
RAW Paste Data