source: trunk/IPs/systemC/processor/Morpheo/Behavioural/Core/Multi_Front_end/Front_end/Prediction_unit/Prediction_unit_Glue/src/Prediction_unit_Glue_genMealy_predict.cpp @ 106

Last change on this file since 106 was 106, checked in by rosiere, 15 years ago

1) RAT : Fix bug when update and event in same cycle
2) Context State : Compute depth
3) Load Store Unit : In check logic, translate all access in little endian. More easy to check
4) UFPT : End Event

File size: 21.0 KB
Line 
1#ifdef SYSTEMC
2/*
3 * $Id$
4 *
5 * [ Description ]
6 *
7 */
8
9#include "Behavioural/Core/Multi_Front_end/Front_end/Prediction_unit/Prediction_unit_Glue/include/Prediction_unit_Glue.h"
10
11namespace morpheo                    {
12namespace behavioural {
13namespace core {
14namespace multi_front_end {
15namespace front_end {
16namespace prediction_unit {
17namespace prediction_unit_glue {
18
19
20#undef  FUNCTION
21#define FUNCTION "Prediction_unit_Glue::genMealy_predict"
22  void Prediction_unit_Glue::genMealy_predict (void)
23  {
24    log_begin(Prediction_unit_Glue,FUNCTION);
25    log_function(Prediction_unit_Glue,FUNCTION,_name.c_str());
26
27    if (PORT_READ(in_NRESET))
28      {
29    // Init
30    Tcontrol_t ack [_param->_nb_context];
31    for (uint32_t i=0; i<_param->_nb_context; i++)
32      ack [i] = 0;
33
34    for (uint32_t i=0; i<_param->_nb_inst_branch_predict; i++)
35      {
36        log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"  * PREDICT [%d]",i);
37       
38        // No access
39        Tcontrol_t btb_val = false;
40        Tcontrol_t dir_val = false;
41        Tcontrol_t ras_val = false;
42        Tcontrol_t upt_val = false;
43
44        // Get ack
45        Tcontrol_t btb_ack = PORT_READ(in_PREDICT_BTB_ACK [i]);
46        Tcontrol_t dir_ack = PORT_READ(in_PREDICT_DIR_ACK [i]);
47        Tcontrol_t ras_ack = PORT_READ(in_PREDICT_RAS_ACK [i]);
48        Tcontrol_t upt_ack = PORT_READ(in_PREDICT_UPT_ACK [i]);
49
50        // Read context_id
51        Tcontext_t context = (reg_PREDICT_PRIORITY+i)%_param->_nb_context; // priority
52        log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * context    : %d",context);
53
54        // Now : ack transaction
55        ack [context] = 1;
56
57        if (PORT_READ(in_PREDICT_VAL[context]) == 0)
58          {
59            // Nothing
60            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * not valid ...");
61
62//             btb_val = false;
63//             dir_val = false;
64//             ras_val = false;
65//             upt_val = false;
66          }
67        else
68          {
69            // Have transaction
70            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * valid ...");
71
72            // Read information (PC)
73            Taddress_t          pc_previous           = PORT_READ(in_PREDICT_PC_PREVIOUS           [context]);
74            Taddress_t          pc_current            = PORT_READ(in_PREDICT_PC_CURRENT            [context]);
75            Tcontrol_t          pc_current_is_ds_take = PORT_READ(in_PREDICT_PC_CURRENT_IS_DS_TAKE [context]);
76
77            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * pc_previous           : 0x%.8x (0x%.8x)",pc_previous,pc_previous<<2);
78            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * pc_current            : 0x%.8x (0x%.8x)",pc_current ,pc_current <<2);
79            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * pc_current_is_ds_take : %d"    ,pc_current_is_ds_take);
80
81            Taddress_t          pc_next                     ;
82            Tcontrol_t          pc_next_is_ds_take          ;
83            Tbranch_state_t     branch_state                ;
84//          Tprediction_ptr_t   branch_update_prediction_id ;
85            Tinst_ifetch_ptr_t  inst_ifetch_ptr             ;
86           
87            // STEP (1) - Compute the address source
88            //   -> if pc_current is a ds take, then pc_previous is a branchement
89            //      get branchement address to send at the BTB
90            Taddress_t          address     = (pc_current_is_ds_take)?pc_previous:pc_current;
91            // Address_lsb = position in fetch packet
92            Taddress_t          address_lsb = pc_current%_param->_nb_instruction [context]; //if pc_current_is_ds_take, then pc_current%_param->_nb_instruction [context] == 0
93            Taddress_t          address_msb;
94
95            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * address               : 0x%.8x (0x%.8x)",address,address<<2);
96            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * address_lsb           : %d"    ,address_lsb);
97
98            // STEP (2) - Test if branch (access at branch_target_buffer)
99            // Access at the btb
100            btb_val = true;
101
102            // Create the request
103            if (_param->_have_port_context_id)
104            PORT_WRITE(out_PREDICT_BTB_CONTEXT_ID [i],context);
105            PORT_WRITE(out_PREDICT_BTB_ADDRESS    [i],address);
106
107            // Transaction can be ack if btb is not busy
108            ack [context] &= btb_ack;
109
110            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * btb_ack               : %d"    ,btb_ack);
111
112            // BTB_ack = 0 ? else can continue
113            if (not btb_ack)
114              continue;
115
116            // Test a special case :
117            //  if pc_current is a delay slot, then pc_previous is a branchement instruction, also hit must be set.
118            //  else : an another branch instruction have eject this branch : can't accurate
119            Tcontrol_t          hit         = PORT_READ(in_PREDICT_BTB_HIT[i]);
120            Tcontrol_t          is_accurate = PORT_READ(in_PREDICT_BTB_IS_ACCURATE  [i]) and not (pc_current_is_ds_take and not hit);
121
122            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * hit                   : %d"    ,hit);
123            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * is_accurate           : %d"    ,is_accurate);
124
125            // STEP (3) : Test if BTB find a branch instruction in the packet
126            if (hit == 1)
127              {
128                log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * BTB hit : no sequential order");
129
130                // STEP (3a) : branch - test condition
131                bool                use_dir      = false;
132                bool                use_ras      = false;
133                bool                use_upt      = false;
134
135                Taddress_t          address_src      = PORT_READ(in_PREDICT_BTB_ADDRESS_SRC  [i]);
136                Taddress_t          address_src_lsb  = address_src%_param->_nb_instruction [context];
137
138                inst_ifetch_ptr = address_src_lsb;
139
140                log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * address_src     : 0x%.8x (0x%.8x)",address_src,address_src<<2);
141                log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * address_src_lsb : %d",address_src_lsb);
142
143                // Special case :
144                //   * BTB hit and the branchement is the PC current and it's the last slot.
145                //     -> next pc must be the delay slot
146                if ((not pc_current_is_ds_take) and // if pc_current is ds_take, then pc_next is the destination of branchement
147                    (address_src_lsb == (_param->_nb_instruction [context]-1)))
148                  {
149                    // branch is in the last slot of the packet
150                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * branch is in the last slot of the packet");
151               
152                    // Branch is the last slot : next paquet is the delay slot
153                    pc_next            = address_src+1; // sequential
154                    pc_next_is_ds_take = 1;
155                    address_msb        = _param->_nb_instruction [context]; // == (address_src_lsb+1)
156                    branch_state       = BRANCH_STATE_NONE;
157                  }
158                else
159                  {
160                    Tbranch_condition_t condition    = PORT_READ(in_PREDICT_BTB_CONDITION    [i]);
161                    Taddress_t          address_dest = PORT_READ(in_PREDICT_BTB_ADDRESS_DEST [i]);
162                    Tcontrol_t          push     ;
163                    Tcontrol_t          direction;
164                   
165                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * condition             : %s"    ,toString(condition).c_str());
166                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * address_src           : 0x%.8x (0x%.8x)",address_src ,address_src <<2);
167                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * address_dest          : 0x%.8x (0x%.8x)",address_dest,address_dest<<2);
168                   
169                    switch (condition)
170                      {
171                      case BRANCH_CONDITION_NONE_WITHOUT_WRITE_STACK          : // l.j
172                        {
173                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_NONE_WITHOUT_WRITE_STACK");
174                         
175                          // use none unit (dir and ras)
176                          use_upt      = true;
177                          direction    = true;
178                          pc_next      = address_dest;
179                          branch_state = BRANCH_STATE_NSPEC_TAKE;
180                          break;
181                        }
182                      case BRANCH_CONDITION_NONE_WITH_WRITE_STACK             : // l.jal
183                        {
184                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_NONE_WITH_WRITE_STACK");
185                         
186                          use_upt      = true;
187                          use_ras      = true;
188                          push         = true;
189                          direction    = true;
190                          pc_next      = address_dest;
191                          branch_state = BRANCH_STATE_NSPEC_TAKE;
192                          break;
193                        }
194                      case BRANCH_CONDITION_FLAG_UNSET                        : // l.bnf
195                      case BRANCH_CONDITION_FLAG_SET                          : // l.bf
196                        {
197                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_FLAG");
198                         
199                          use_upt      = true;
200                          use_dir      = true;
201                          // Test direction
202                          direction = PORT_READ(in_PREDICT_DIR_DIRECTION [i]); // Direction is not the "flag predict" ... also flag_unset and flag_set is the same
203                          if (direction = 1)
204                            {
205                              branch_state = BRANCH_STATE_SPEC_TAKE;
206                              pc_next      = address_dest;
207                            }
208                          else
209                            {
210                              branch_state = BRANCH_STATE_SPEC_NTAKE;
211                              pc_next      = address_src+2; // +1 = delay slot
212                            }
213                          break;
214                        }
215                      case BRANCH_CONDITION_READ_REGISTER_WITHOUT_WRITE_STACK : // l.jr (rb!=9)
216                        {
217                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_READ_REGISTER_WITHOUT_WRITE_STACK");
218                         
219                          use_upt      = true;
220                          use_ras      = true;
221                          push         = true;
222                          direction    = true;
223                          pc_next      = address_dest;
224                          branch_state = BRANCH_STATE_SPEC_TAKE;
225                          break;
226                        }
227                      case BRANCH_CONDITION_READ_REGISTER_WITH_WRITE_STACK    : // l.jalr
228                        {
229                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_READ_REGISTER_WITH_WRITE_STACK");
230                         
231                          use_upt      = true;
232                          use_ras      = true;
233                          push         = true;
234                          direction    = true;
235                          pc_next      = address_dest;
236                          branch_state = BRANCH_STATE_NSPEC_TAKE;
237                          break;
238                        }
239                      case BRANCH_CONDITION_READ_STACK                        : // l.jr (rb==9)
240                        {
241                          log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * BRANCH_CONDITION_READ_STACK");
242                          use_upt      = true;
243                          use_ras      = true;
244                          push         = false;
245                          direction    = true;
246                          pc_next      = PORT_READ(in_PREDICT_RAS_ADDRESS_POP  [i]);
247                          branch_state = BRANCH_STATE_SPEC_TAKE;
248                          break;
249                        }
250                      default :
251                        {
252                          ERRORMORPHEO(FUNCTION,"Unknow Condition");
253                          break;
254                        }
255                      }
256                   
257                    if (use_dir)
258                      {
259                        ack[context] &= dir_ack;
260                        PORT_WRITE(out_PREDICT_DIR_ADDRESS_SRC [i], address_src);
261                        PORT_WRITE(out_PREDICT_DIR_STATIC      [i], address_dest<address_src); // if destination is previous : the static direction is take
262//                      PORT_WRITE(out_PREDICT_DIR_LAST_TAKE   [i], PORT_READ(in_PREDICT_BTB_LAST_TAKE [i]));
263                      }
264                   
265                    if (use_ras)
266                      {
267                        ack[context] &= ras_ack;
268                        if (_param->_have_port_context_id)
269                        PORT_WRITE(out_PREDICT_RAS_CONTEXT_ID   [i], context);
270                        PORT_WRITE(out_PREDICT_RAS_PUSH         [i], push); 
271                        PORT_WRITE(out_PREDICT_RAS_ADDRESS_PUSH [i], address_src+2); 
272                       
273                        is_accurate &= PORT_READ(in_PREDICT_RAS_HIT [i]); // if miss - prediction is not accurate
274                      }
275                   
276                    if (use_upt)
277                      {
278                        ack[context] &= upt_ack;
279                       
280                        if (_param->_have_port_context_id)
281                        PORT_WRITE(out_PREDICT_UPT_CONTEXT_ID       [i],context);
282                        PORT_WRITE(out_PREDICT_UPT_BTB_ADDRESS_SRC  [i],address_src);
283                        PORT_WRITE(out_PREDICT_UPT_BTB_ADDRESS_DEST [i],address_dest);
284                        PORT_WRITE(out_PREDICT_UPT_BTB_CONDITION    [i],condition);
285                        PORT_WRITE(out_PREDICT_UPT_BTB_LAST_TAKE    [i],direction);
286                        PORT_WRITE(out_PREDICT_UPT_BTB_IS_ACCURATE  [i],is_accurate);
287//                      PORT_WRITE(out_PREDICT_UPT_DIR_HISTORY      [i],PORT_READ(in_PREDICT_DIR_HISTORY      [i]));
288                        PORT_WRITE(out_PREDICT_UPT_RAS_ADDRESS      [i],PORT_READ(in_PREDICT_RAS_ADDRESS_POP  [i]));
289//                      PORT_WRITE(out_PREDICT_UPT_RAS_INDEX        [i],PORT_READ(in_PREDICT_RAS_INDEX        [i]));
290                      }
291                   
292                    // ack = 1 if :
293                    //   *             btb_ack
294                    //   * use_dir and dir_ack
295                    //   * use_ras and ras_ack
296                    //   * use_upt and upt_ack
297//                  ack [context] = (btb_ack and
298//                                   (use_dir and dir_ack) and
299//                                   (use_ras and ras_ack) and
300//                                   (use_upt and upt_ack));
301
302                    dir_val = (btb_ack and
303                               use_dir and
304//                             use_ras and
305//                             use_upt and
306//                             (not use_dir or (use_dir and dir_ack)) and
307                               (not use_ras or (use_ras and ras_ack)) and
308                               (not use_upt or (use_upt and upt_ack)));
309                   
310                    ras_val = (btb_ack and
311//                             use_dir and
312                               use_ras and
313//                             use_upt and
314                               (not use_dir or (use_dir and dir_ack)) and
315//                             (not use_ras or (use_ras and ras_ack)) and
316                               (not use_upt or (use_upt and upt_ack)));
317                   
318                    upt_val = (btb_ack and
319//                             use_dir and
320//                             use_ras and
321                               use_upt and
322                               (not use_dir or (use_dir and dir_ack)) and
323                               (not use_ras or (use_ras and ras_ack))//  and
324//                             (not use_upt or (use_upt and upt_ack))
325                               );
326                   
327                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * btb_{     val, ack}   :    %d, %d",        btb_val, btb_ack);
328                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * dir_{use, val, ack}   : %d, %d, %d",use_dir,dir_val, dir_ack);
329                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * ras_{use, val, ack}   : %d, %d, %d",use_ras,ras_val, ras_ack);
330                    log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * upt_{use, val, ack}   : %d, %d, %d",use_upt,upt_val, upt_ack);
331
332                   
333//                  pc_next      - is previously computed
334//                  branch_state - is previously computed
335
336
337                    // branch is in the last slot of the packet
338                    address_msb        = (address_src_lsb+2); // +1 == delayed slot
339                    pc_next_is_ds_take = 0;
340                  }
341
342//              branch_update_prediction_id = (_param->_have_port_depth)?((PORT_READ(in_DEPTH_UPT_TAIL[context])+PORT_READ(in_DEPTH_UPT_NB_BRANCH [context]))%_param->_array_size_depth[context]):0;
343              }
344            else
345              {
346                // STEP (3b) : Sequential order : compute next paquet
347                log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * BTB miss : sequential order");
348
349                // Take the address packet base and add new packet
350                pc_next                     = pc_current-address_lsb+_param->_nb_instruction [context]; // sequential
351                pc_next_is_ds_take          = 0; // no branch, also no delay slot
352                inst_ifetch_ptr             = 0;
353                branch_state                = BRANCH_STATE_NONE;
354//              branch_update_prediction_id = 0;
355
356                address_msb = _param->_nb_instruction [context];
357              }
358
359            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * address_msb           : %d",address_msb);
360            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * pc_next               : 0x%.8x (0x%.8x)",pc_next,pc_next<<2);
361            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * pc_next_is_ds_take    : %d"    ,pc_next_is_ds_take);
362           
363            // Write Output
364            PORT_WRITE(out_PREDICT_PC_NEXT                     [context]   , pc_next                    );
365            PORT_WRITE(out_PREDICT_PC_NEXT_IS_DS_TAKE          [context]   , pc_next_is_ds_take         );
366
367            // Create enable mask
368            Taddress_t address_limit_min = address_lsb;
369            Taddress_t address_limit_max = ((pc_current_is_ds_take)?(address_lsb+1):address_msb);
370
371            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"    * instruction enable :");
372            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * nb_inst : %d",_param->_nb_instruction [context]);
373            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * [0:%d[ = 0" ,address_limit_min);
374            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * [%d:%d[ = 1",address_limit_min,address_limit_max);
375            log_printf(TRACE,Prediction_unit_Glue,FUNCTION,"      * [%d:%d[ = 0",address_limit_max,_param->_nb_instruction [context]);
376
377            for (uint32_t j=0; j<address_limit_min; j++)
378            PORT_WRITE(out_PREDICT_INSTRUCTION_ENABLE          [context][j], 0); // Before the address : not valid
379            for (uint32_t j=address_limit_min; j<address_limit_max; j++)
380            PORT_WRITE(out_PREDICT_INSTRUCTION_ENABLE          [context][j], 1); // Valid packet
381            for (uint32_t j=address_limit_max; j<_param->_nb_instruction [context]; j++)
382            PORT_WRITE(out_PREDICT_INSTRUCTION_ENABLE          [context][j], 0); // After last address (branch) : not valid
383
384            if (_param->_have_port_inst_ifetch_ptr)
385            PORT_WRITE(out_PREDICT_INST_IFETCH_PTR             [context]   , inst_ifetch_ptr            );
386            PORT_WRITE(out_PREDICT_BRANCH_STATE                [context]   , branch_state               );
387            if (_param->_have_port_depth)
388            PORT_WRITE(out_PREDICT_BRANCH_UPDATE_PREDICTION_ID [context]   , PORT_READ(in_PREDICT_UPT_BRANCH_UPDATE_PREDICTION_ID [i]));
389          }
390
391        // Write output
392        PORT_WRITE(out_PREDICT_BTB_VAL [i], btb_val);
393        PORT_WRITE(out_PREDICT_DIR_VAL [i], dir_val);
394        PORT_WRITE(out_PREDICT_RAS_VAL [i], ras_val);
395        PORT_WRITE(out_PREDICT_UPT_VAL [i], upt_val);
396      }
397   
398    // Write output
399    for (uint32_t i=0; i<_param->_nb_context; i++)
400      PORT_WRITE(out_PREDICT_ACK[i],ack[i]);
401      }
402
403    log_end(Prediction_unit_Glue,FUNCTION);
404  };
405
406}; // end namespace prediction_unit_glue
407}; // end namespace prediction_unit
408}; // end namespace front_end
409}; // end namespace multi_front_end
410}; // end namespace core
411
412}; // end namespace behavioural
413}; // end namespace morpheo             
414#endif
Note: See TracBrowser for help on using the repository browser.