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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [bpb/] [branch-predict.c] - Diff between revs 19 and 224

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 19 Rev 224
/* branch-predict.c -- branch prediction simulation
/* branch-predict.c -- branch prediction simulation
 
 
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
   Copyright (C) 2008 Embecosm Limited
   Copyright (C) 2008 Embecosm Limited
 
 
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
 
 
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
 
 
   This program is free software; you can redistribute it and/or modify it
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   under the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3 of the License, or (at your option)
   Software Foundation; either version 3 of the License, or (at your option)
   any later version.
   any later version.
 
 
   This program is distributed in the hope that it will be useful, but WITHOUT
   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.
   more details.
 
 
   You should have received a copy of the GNU General Public License along
   You should have received a copy of the GNU General Public License along
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
/* This program is commented throughout in a fashion suitable for processing
/* This program is commented throughout in a fashion suitable for processing
   with Doxygen. */
   with Doxygen. */
 
 
/* Branch prediction functions.  At the moment this functions only simulate
/* Branch prediction functions.  At the moment this functions only simulate
   functionality of branch prediction and do not influence on
   functionality of branch prediction and do not influence on
   fetche/decode/execute stages.  They are here only to verify performance of
   fetche/decode/execute stages.  They are here only to verify performance of
   various branch prediction configurations. */
   various branch prediction configurations. */
 
 
 
 
/* Autoconf and/or portability configuration */
/* Autoconf and/or portability configuration */
#include "config.h"
#include "config.h"
#include "port.h"
#include "port.h"
 
 
/* System includes */
/* System includes */
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
#include <errno.h>
#include <stdarg.h>
#include <stdarg.h>
 
 
/* Package includes */
/* Package includes */
#include "sim-config.h"
#include "sim-config.h"
#include "arch.h"
#include "arch.h"
#include "stats.h"
#include "stats.h"
 
 
/* Branch prediction buffer */
/* Branch prediction buffer */
 
 
/* Length of BPB */
/* Length of BPB */
#define BPB_LEN 64
#define BPB_LEN 64
 
 
/* Number of BPB ways (1, 2, 3 etc.). */
/* Number of BPB ways (1, 2, 3 etc.). */
#define BPB_WAYS 1
#define BPB_WAYS 1
 
 
/* Number of prediction states (2, 4, 8 etc.). */
/* Number of prediction states (2, 4, 8 etc.). */
#define BPB_PSTATES 2
#define BPB_PSTATES 2
 
 
/* Number of usage states (2, 3, 4 etc.). */
/* Number of usage states (2, 3, 4 etc.). */
#define BPB_USTATES 2
#define BPB_USTATES 2
 
 
/* branch prediction buffer entry */
/* branch prediction buffer entry */
struct bpb_entry
struct bpb_entry
{
{
  struct
  struct
  {
  {
    oraddr_t addr;              /* address of a branch insn */
    oraddr_t addr;              /* address of a branch insn */
    int taken;                  /* taken == 1, not taken == 0  OR */
    int taken;                  /* taken == 1, not taken == 0  OR */
    /* strongly taken == 3, taken == 2,
    /* strongly taken == 3, taken == 2,
       not taken == 1, strongly not taken == 0 */
       not taken == 1, strongly not taken == 0 */
    int lru;                    /* least recently == 0 */
    int lru;                    /* least recently == 0 */
  } way[BPB_WAYS];
  } way[BPB_WAYS];
} bpb[BPB_LEN];
} bpb[BPB_LEN];
 
 
void
void
bpb_info ()
bpb_info ()
{
{
  if (!config.bpb.enabled)
  if (!config.bpb.enabled)
    {
    {
      PRINTF ("BPB not simulated. Check -bpb option.\n");
      PRINTF ("BPB not simulated. Check -bpb option.\n");
      return;
      return;
    }
    }
 
 
  PRINTF ("BPB %d bytes: ",
  PRINTF ("BPB %d bytes: ",
          BPB_LEN * BPB_WAYS * (BPB_PSTATES + BPB_USTATES) / 8);
          BPB_LEN * BPB_WAYS * (BPB_PSTATES + BPB_USTATES) / 8);
  PRINTF ("%d ways, %d sets, %d bits/prediction\n", BPB_WAYS, BPB_LEN,
  PRINTF ("%d ways, %d sets, %d bits/prediction\n", BPB_WAYS, BPB_LEN,
          BPB_PSTATES + BPB_USTATES);
          BPB_PSTATES + BPB_USTATES);
}
}
 
 
/* First check if branch is already in the cache and if it is:
/* First check if branch is already in the cache and if it is:
    - increment BPB hit stats,
    - increment BPB hit stats,
    - set 'lru' at this way to BPB_USTATES - 1 and
    - set 'lru' at this way to BPB_USTATES - 1 and
      decrement 'lru' of other ways unless they have reached 0,
      decrement 'lru' of other ways unless they have reached 0,
    - increment correct/incorrect stats according to BPB 'taken' field
    - increment correct/incorrect stats according to BPB 'taken' field
      and 'taken' variable,
      and 'taken' variable,
    - increment or decrement BPB taken field according to 'taken' variable
    - increment or decrement BPB taken field according to 'taken' variable
   and if not:
   and if not:
    - increment BPB miss stats
    - increment BPB miss stats
    - find lru way and entry and replace old address with 'addr' and
    - find lru way and entry and replace old address with 'addr' and
      'taken' field with (BPB_PSTATES/2 - 1) + 'taken'
      'taken' field with (BPB_PSTATES/2 - 1) + 'taken'
    - set 'lru' with BPB_USTATES - 1 and decrement 'lru' of other
    - set 'lru' with BPB_USTATES - 1 and decrement 'lru' of other
      ways unless they have reached 0
      ways unless they have reached 0
*/
*/
 
 
void
void
bpb_update (oraddr_t addr, int taken)
bpb_update (oraddr_t addr, int taken)
{
{
  int entry, way = -1;
  int entry, way = -1;
  int i;
  int i;
 
 
  /* BPB simulation enabled/disabled. */
  /* BPB simulation enabled/disabled. */
  if (!config.bpb.enabled)
  if (!config.bpb.enabled)
    return;
    return;
 
 
  /* Calc entry. */
  /* Calc entry. */
  entry = addr % BPB_LEN;
  entry = addr % BPB_LEN;
 
 
  /* Scan all ways and try to find our addr. */
  /* Scan all ways and try to find our addr. */
  for (i = 0; i < BPB_WAYS; i++)
  for (i = 0; i < BPB_WAYS; i++)
    if (bpb[entry].way[i].addr == addr)
    if (bpb[entry].way[i].addr == addr)
      way = i;
      way = i;
 
 
  /* Did we find our cached branch? */
  /* Did we find our cached branch? */
  if (way >= 0)
  if (way >= 0)
    {                           /* Yes, we did. */
    {                           /* Yes, we did. */
      or1k_mstats.bpb.hit++;
      or1k_mstats.bpb.hit++;
 
 
      for (i = 0; i < BPB_WAYS; i++)
      for (i = 0; i < BPB_WAYS; i++)
        if (bpb[entry].way[i].lru)
        if (bpb[entry].way[i].lru)
          bpb[entry].way[i].lru--;
          bpb[entry].way[i].lru--;
      bpb[entry].way[way].lru = BPB_USTATES - 1;
      bpb[entry].way[way].lru = BPB_USTATES - 1;
 
 
      if (bpb[entry].way[way].taken / (BPB_PSTATES / 2) == taken)
      if (bpb[entry].way[way].taken / (BPB_PSTATES / 2) == taken)
        or1k_mstats.bpb.correct++;
        or1k_mstats.bpb.correct++;
      else
      else
        or1k_mstats.bpb.incorrect++;
        or1k_mstats.bpb.incorrect++;
 
 
      if (taken && (bpb[entry].way[way].taken < BPB_PSTATES - 1))
      if (taken && (bpb[entry].way[way].taken < BPB_PSTATES - 1))
        bpb[entry].way[way].taken++;
        bpb[entry].way[way].taken++;
      else if (!taken && (bpb[entry].way[way].taken))
      else if (!taken && (bpb[entry].way[way].taken))
        bpb[entry].way[way].taken--;
        bpb[entry].way[way].taken--;
    }
    }
  else
  else
    {                           /* No, we didn't. */
    {                           /* No, we didn't. */
      int minlru = BPB_USTATES - 1;
      int minlru = BPB_USTATES - 1;
      int minway = 0;
      int minway = 0;
 
 
      or1k_mstats.bpb.miss++;
      or1k_mstats.bpb.miss++;
 
 
      for (i = 0; i < BPB_WAYS; i++)
      for (i = 0; i < BPB_WAYS; i++)
        if (bpb[entry].way[i].lru < minlru)
        if (bpb[entry].way[i].lru < minlru)
          minway = i;
          minway = i;
 
 
      bpb[entry].way[minway].addr = addr;
      bpb[entry].way[minway].addr = addr;
      bpb[entry].way[minway].taken = (BPB_PSTATES / 2 - 1) + taken;
      bpb[entry].way[minway].taken = (BPB_PSTATES / 2 - 1) + taken;
      for (i = 0; i < BPB_WAYS; i++)
      for (i = 0; i < BPB_WAYS; i++)
        if (bpb[entry].way[i].lru)
        if (bpb[entry].way[i].lru)
          bpb[entry].way[i].lru--;
          bpb[entry].way[i].lru--;
      bpb[entry].way[minway].lru = BPB_USTATES - 1;
      bpb[entry].way[minway].lru = BPB_USTATES - 1;
    }
    }
}
}
 
 
/* Branch target instruction cache */
/* Branch target instruction cache */
 
 
/* Length of BTIC */
/* Length of BTIC */
#define BTIC_LEN 128
#define BTIC_LEN 128
 
 
/* Number of BTIC ways (1, 2, 3 etc.). */
/* Number of BTIC ways (1, 2, 3 etc.). */
#define BTIC_WAYS 2
#define BTIC_WAYS 2
 
 
/* Number of usage states (2, 3, 4 etc.). */
/* Number of usage states (2, 3, 4 etc.). */
#define BTIC_USTATES 2
#define BTIC_USTATES 2
 
 
/* Target block size in bytes. */
/* Target block size in bytes. */
#define BTIC_BLOCKSIZE 4
#define BTIC_BLOCKSIZE 4
 
 
struct btic_entry
struct btic_entry
{
{
  struct
  struct
  {
  {
    oraddr_t addr;              /* cached target address of a branch */
    oraddr_t addr;              /* cached target address of a branch */
    int lru;                    /* least recently used */
    int lru;                    /* least recently used */
    char *insn;                 /* cached insn at target address (not used currently) */
    char *insn;                 /* cached insn at target address (not used currently) */
  } way[BTIC_WAYS];
  } way[BTIC_WAYS];
} btic[BTIC_LEN];
} btic[BTIC_LEN];
 
 
void
void
btic_info ()
btic_info ()
{
{
  if (!config.bpb.btic)
  if (!config.bpb.btic)
    {
    {
      PRINTF ("BTIC not simulated. Check --btic option.\n");
      PRINTF ("BTIC not simulated. Check --btic option.\n");
      return;
      return;
    }
    }
 
 
  PRINTF ("BTIC %d bytes: ",
  PRINTF ("BTIC %d bytes: ",
          BTIC_LEN * BTIC_WAYS * (BTIC_USTATES + BTIC_BLOCKSIZE * 8) / 8);
          BTIC_LEN * BTIC_WAYS * (BTIC_USTATES + BTIC_BLOCKSIZE * 8) / 8);
  PRINTF ("%d ways, %d sets, %d bits/target\n", BTIC_WAYS, BTIC_LEN,
  PRINTF ("%d ways, %d sets, %d bits/target\n", BTIC_WAYS, BTIC_LEN,
          BTIC_USTATES + BTIC_BLOCKSIZE * 8);
          BTIC_USTATES + BTIC_BLOCKSIZE * 8);
}
}
 
 
/* First check if target addr is already in the cache and if it is:
/* First check if target addr is already in the cache and if it is:
    - increment BTIC hit stats,
    - increment BTIC hit stats,
    - set 'lru' at this way to BTIC_USTATES - 1 and
    - set 'lru' at this way to BTIC_USTATES - 1 and
      decrement 'lru' of other ways unless they have reached 0,
      decrement 'lru' of other ways unless they have reached 0,
   and if not:
   and if not:
    - increment BTIC miss stats
    - increment BTIC miss stats
    - find lru way and entry and replace old address with 'addr' and
    - find lru way and entry and replace old address with 'addr' and
      'insn' with NULL
      'insn' with NULL
    - set 'lru' with BTIC_USTATES - 1 and decrement 'lru' of other
    - set 'lru' with BTIC_USTATES - 1 and decrement 'lru' of other
      ways unless they have reached 0
      ways unless they have reached 0
*/
*/
 
 
void
void
btic_update (oraddr_t targetaddr)
btic_update (oraddr_t targetaddr)
{
{
  int entry, way = -1;
  int entry, way = -1;
  int i;
  int i;
 
 
  /* BTIC simulation enabled/disabled. */
  /* BTIC simulation enabled/disabled. */
  if (!config.bpb.btic)
  if (!config.bpb.btic)
    return;
    return;
 
 
  /* Calc entry. */
  /* Calc entry. */
  entry = targetaddr % BTIC_LEN;
  entry = targetaddr % BTIC_LEN;
 
 
  /* Scan all ways and try to find our addr. */
  /* Scan all ways and try to find our addr. */
  for (i = 0; i < BTIC_WAYS; i++)
  for (i = 0; i < BTIC_WAYS; i++)
    if (btic[entry].way[i].addr == targetaddr)
    if (btic[entry].way[i].addr == targetaddr)
      way = i;
      way = i;
 
 
  /* Did we find our cached branch? */
  /* Did we find our cached branch? */
  if (way >= 0)
  if (way >= 0)
    {                           /* Yes, we did. */
    {                           /* Yes, we did. */
      or1k_mstats.btic.hit++;
      or1k_mstats.btic.hit++;
 
 
      for (i = 0; i < BTIC_WAYS; i++)
      for (i = 0; i < BTIC_WAYS; i++)
        if (btic[entry].way[i].lru)
        if (btic[entry].way[i].lru)
          btic[entry].way[i].lru--;
          btic[entry].way[i].lru--;
      btic[entry].way[way].lru = BTIC_USTATES - 1;
      btic[entry].way[way].lru = BTIC_USTATES - 1;
    }
    }
  else
  else
    {                           /* No, we didn't. */
    {                           /* No, we didn't. */
      int minlru = BTIC_USTATES - 1;
      int minlru = BTIC_USTATES - 1;
      int minway = 0;
      int minway = 0;
 
 
      or1k_mstats.btic.miss++;
      or1k_mstats.btic.miss++;
 
 
      for (i = 0; i < BTIC_WAYS; i++)
      for (i = 0; i < BTIC_WAYS; i++)
        if (btic[entry].way[i].lru < minlru)
        if (btic[entry].way[i].lru < minlru)
          minway = i;
          minway = i;
 
 
      btic[entry].way[minway].addr = targetaddr;
      btic[entry].way[minway].addr = targetaddr;
      btic[entry].way[minway].insn = NULL;
      btic[entry].way[minway].insn = NULL;
      for (i = 0; i < BTIC_WAYS; i++)
      for (i = 0; i < BTIC_WAYS; i++)
        if (btic[entry].way[i].lru)
        if (btic[entry].way[i].lru)
          btic[entry].way[i].lru--;
          btic[entry].way[i].lru--;
      btic[entry].way[minway].lru = BTIC_USTATES - 1;
      btic[entry].way[minway].lru = BTIC_USTATES - 1;
    }
    }
}
}
 
 
/*----------------------------------------------------[ BPB configuration ]---*/
/*----------------------------------------------------[ BPB configuration ]---*/
static void
static void
bpb_enabled (union param_val val, void *dat)
bpb_enabled (union param_val val, void *dat)
{
{
  config.bpb.enabled = val.int_val;
  config.bpb.enabled = val.int_val;
}
}
 
 
static void
static void
bpb_btic (union param_val val, void *dat)
bpb_btic (union param_val val, void *dat)
{
{
  config.bpb.btic = val.int_val;
  config.bpb.btic = val.int_val;
}
}
 
 
static void
static void
bpb_sbp_bnf_fwd (union param_val val, void *dat)
bpb_sbp_bnf_fwd (union param_val val, void *dat)
{
{
  config.bpb.sbp_bnf_fwd = val.int_val;
  config.bpb.sbp_bnf_fwd = val.int_val;
}
}
 
 
static void
static void
bpb_sbp_bf_fwd (union param_val val, void *dat)
bpb_sbp_bf_fwd (union param_val val, void *dat)
{
{
  config.bpb.sbp_bf_fwd = val.int_val;
  config.bpb.sbp_bf_fwd = val.int_val;
}
}
 
 
static void
static void
bpb_missdelay (union param_val val, void *dat)
bpb_missdelay (union param_val val, void *dat)
{
{
  config.bpb.missdelay = val.int_val;
  config.bpb.missdelay = val.int_val;
}
}
 
 
static void
static void
bpb_hitdelay (union param_val val, void *dat)
bpb_hitdelay (union param_val val, void *dat)
{
{
  config.bpb.hitdelay = val.int_val;
  config.bpb.hitdelay = val.int_val;
}
}
 
 
void
void
reg_bpb_sec ()
reg_bpb_sec ()
{
{
  struct config_section *sec = reg_config_sec ("bpb", NULL, NULL);
  struct config_section *sec = reg_config_sec ("bpb", NULL, NULL);
 
 
  reg_config_param (sec, "enabled", paramt_int, bpb_enabled);
  reg_config_param (sec, "enabled",     PARAMT_INT, bpb_enabled);
  reg_config_param (sec, "btic", paramt_int, bpb_btic);
  reg_config_param (sec, "btic",        PARAMT_INT, bpb_btic);
  reg_config_param (sec, "sbp_bnf_fwd", paramt_int, bpb_sbp_bnf_fwd);
  reg_config_param (sec, "sbp_bnf_fwd", PARAMT_INT, bpb_sbp_bnf_fwd);
  reg_config_param (sec, "sbp_bf_fwd", paramt_int, bpb_sbp_bf_fwd);
  reg_config_param (sec, "sbp_bf_fwd",  PARAMT_INT, bpb_sbp_bf_fwd);
  reg_config_param (sec, "missdelay", paramt_int, bpb_missdelay);
  reg_config_param (sec, "missdelay",   PARAMT_INT, bpb_missdelay);
  reg_config_param (sec, "hitdelay", paramt_int, bpb_hitdelay);
  reg_config_param (sec, "hitdelay",    PARAMT_INT, bpb_hitdelay);
}
}
 
 

powered by: WebSVN 2.1.0

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