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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [or1ksim/] [or1ksim-0.3.0/] [support/] [simprintf.c] - Diff between revs 19 and 21

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

Rev 19 Rev 21
/* simprintf.c -- Simulator printf implementation
/* simprintf.c -- Simulator printf implementation
 
 
   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 OpenRISC 1000 Architectural Simulator.
   This file is part of 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. */
 
 
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.  I wrote
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.  I wrote
   following code when basic simulator started to work and I was desperate to
   following code when basic simulator started to work and I was desperate to
   use some PRINTFs in my debugged code. And it was also used to get some
   use some PRINTFs in my debugged code. And it was also used to get some
   output from Dhrystone MIPS benchmark. */
   output from Dhrystone MIPS benchmark. */
 
 
 
 
/* 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 <stdlib.h>
#include <stdlib.h>
 
 
/* Package includes */
/* Package includes */
#include "sim-config.h"
#include "sim-config.h"
#include "arch.h"
#include "arch.h"
#include "debug.h"
#include "debug.h"
#include "abstract.h"
#include "abstract.h"
#include "execute.h"
#include "execute.h"
 
 
 
 
/* Should args be passed on stack for simprintf
/* Should args be passed on stack for simprintf
 *
 *
 * FIXME: do not enable this since it causes problems
 * FIXME: do not enable this since it causes problems
 *        in some cases (an example beeing cbasic test
 *        in some cases (an example beeing cbasic test
 *        from orp testbench). the problems is in
 *        from orp testbench). the problems is in
 *
 *
 *        or1k/support/simprintf.c
 *        or1k/support/simprintf.c
 *
 *
 *        #if STACK_ARGS
 *        #if STACK_ARGS
 *                arg = eval_mem32(argaddr,&breakpoint);
 *                arg = eval_mem32(argaddr,&breakpoint);
 *                argaddr += 4;
 *                argaddr += 4;
 *        #else
 *        #else
 *                sprintf(regstr, "r%u", ++argaddr);
 *                sprintf(regstr, "r%u", ++argaddr);
 *                arg = evalsim_reg(atoi(regstr));
 *                arg = evalsim_reg(atoi(regstr));
 *        #endif
 *        #endif
 *
 *
 *        the access to memory should be without any
 *        the access to memory should be without any
 *        checks (ie not like or32 application accessed it)
 *        checks (ie not like or32 application accessed it)
 *
 *
 */
 */
#define STACK_ARGS 0
#define STACK_ARGS 0
 
 
/* Length of PRINTF format string */
/* Length of PRINTF format string */
#define FMTLEN 2000
#define FMTLEN 2000
 
 
char fmtstr[FMTLEN];
char fmtstr[FMTLEN];
 
 
static char *
static char *
simgetstr (oraddr_t stackaddr, unsigned long regparam)
simgetstr (oraddr_t stackaddr, unsigned long regparam)
{
{
  oraddr_t fmtaddr;
  oraddr_t fmtaddr;
  int i;
  int i;
 
 
  fmtaddr = regparam;
  fmtaddr = regparam;
 
 
  i = 0;
  i = 0;
  while (eval_direct8 (fmtaddr, 1, 0) != '\0')
  while (eval_direct8 (fmtaddr, 1, 0) != '\0')
    {
    {
      fmtstr[i++] = eval_direct8 (fmtaddr, 1, 0);
      fmtstr[i++] = eval_direct8 (fmtaddr, 1, 0);
      fmtaddr++;
      fmtaddr++;
      if (i == FMTLEN - 1)
      if (i == FMTLEN - 1)
        break;
        break;
    }
    }
  fmtstr[i] = '\0';
  fmtstr[i] = '\0';
 
 
  return fmtstr;
  return fmtstr;
}
}
 
 
void
void
simprintf (oraddr_t stackaddr, unsigned long regparam)
simprintf (oraddr_t stackaddr, unsigned long regparam)
{
{
  uint32_t arg;
  uint32_t arg;
  oraddr_t argaddr;
  oraddr_t argaddr;
  char *fmtstrend;
  char *fmtstrend;
  char *fmtstrpart = fmtstr;
  char *fmtstrpart = fmtstr;
  int tee_exe_log;
  int tee_exe_log;
 
 
  simgetstr (stackaddr, regparam);
  simgetstr (stackaddr, regparam);
 
 
#if STACK_ARGS
#if STACK_ARGS
  argaddr = stackaddr;
  argaddr = stackaddr;
#else
#else
  argaddr = 3;
  argaddr = 3;
#endif
#endif
  tee_exe_log = (config.sim.exe_log
  tee_exe_log = (config.sim.exe_log
                 && (config.sim.exe_log_type == EXE_LOG_SOFTWARE
                 && (config.sim.exe_log_type == EXE_LOG_SOFTWARE
                     || config.sim.exe_log_type == EXE_LOG_SIMPLE)
                     || config.sim.exe_log_type == EXE_LOG_SIMPLE)
                 && config.sim.exe_log_start <= runtime.cpu.instructions
                 && config.sim.exe_log_start <= runtime.cpu.instructions
                 && (config.sim.exe_log_end <= 0
                 && (config.sim.exe_log_end <= 0
                     || runtime.cpu.instructions <= config.sim.exe_log_end));
                     || runtime.cpu.instructions <= config.sim.exe_log_end));
 
 
  if (tee_exe_log)
  if (tee_exe_log)
    fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
    fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
  while (strlen (fmtstrpart))
  while (strlen (fmtstrpart))
    {
    {
      if ((fmtstrend = strstr (fmtstrpart + 1, "%")))
      if ((fmtstrend = strstr (fmtstrpart + 1, "%")))
        *fmtstrend = '\0';
        *fmtstrend = '\0';
      if (strstr (fmtstrpart, "%"))
      if (strstr (fmtstrpart, "%"))
        {
        {
          char *tmp;
          char *tmp;
          int string = 0;
          int string = 0;
#if STACK_ARGS
#if STACK_ARGS
          arg = eval_direct32 (argaddr, 1, 0);
          arg = eval_direct32 (argaddr, 1, 0);
          argaddr += 4;
          argaddr += 4;
#else
#else
          {
          {
            /* JPB. I can't see how the original code ever worked. It does trash
            /* JPB. I can't see how the original code ever worked. It does trash
               the file pointer by overwriting the end of regstr. In any case
               the file pointer by overwriting the end of regstr. In any case
               why create a string, only to turn it back into an integer! */
               why create a string, only to turn it back into an integer! */
 
 
            /* orig:  char regstr[5]; */
            /* orig:  char regstr[5]; */
            /* orig:  */
            /* orig:  */
            /* orig:  sprintf(regstr, "r%"PRIxADDR, ++argaddr); */
            /* orig:  sprintf(regstr, "r%"PRIxADDR, ++argaddr); */
            /* orig:  arg = evalsim_reg(atoi(regstr)); */
            /* orig:  arg = evalsim_reg(atoi(regstr)); */
 
 
            arg = evalsim_reg (++argaddr);
            arg = evalsim_reg (++argaddr);
          }
          }
#endif
#endif
          tmp = fmtstrpart;
          tmp = fmtstrpart;
          if (*tmp == '%')
          if (*tmp == '%')
            {
            {
              tmp++;
              tmp++;
              while (*tmp == '-' || (*tmp >= '0' && *tmp <= '9'))
              while (*tmp == '-' || (*tmp >= '0' && *tmp <= '9'))
                tmp++;
                tmp++;
              if (*tmp == 's')
              if (*tmp == 's')
                string = 1;
                string = 1;
            }
            }
          if (string)
          if (string)
            {
            {
              int len = 0;
              int len = 0;
              char *str;
              char *str;
              for (; eval_direct8 (arg++, 1, 0); len++);
              for (; eval_direct8 (arg++, 1, 0); len++);
              len++;            /* for null char */
              len++;            /* for null char */
              arg -= len;
              arg -= len;
              str = (char *) malloc (len);
              str = (char *) malloc (len);
              len = 0;
              len = 0;
              for (; eval_direct8 (arg, 1, 0); len++)
              for (; eval_direct8 (arg, 1, 0); len++)
                *(str + len) = eval_direct8 (arg++, 1, 0);
                *(str + len) = eval_direct8 (arg++, 1, 0);
              *(str + len) = eval_direct8 (arg, 1, 0);   /* null ch */
              *(str + len) = eval_direct8 (arg, 1, 0);   /* null ch */
              printf (fmtstrpart, str);
              printf (fmtstrpart, str);
              if (tee_exe_log)
              if (tee_exe_log)
                fprintf (runtime.sim.fexe_log, fmtstrpart, str);
                fprintf (runtime.sim.fexe_log, fmtstrpart, str);
              free (str);
              free (str);
            }
            }
          else
          else
            {
            {
              printf (fmtstrpart, arg);
              printf (fmtstrpart, arg);
              if (tee_exe_log)
              if (tee_exe_log)
                fprintf (runtime.sim.fexe_log, fmtstrpart, arg);
                fprintf (runtime.sim.fexe_log, fmtstrpart, arg);
            }
            }
        }
        }
      else
      else
        {
        {
          printf (fmtstrpart);
          printf (fmtstrpart);
          if (tee_exe_log)
          if (tee_exe_log)
            fprintf (runtime.sim.fexe_log, fmtstrpart);
            fprintf (runtime.sim.fexe_log, fmtstrpart);
        }
        }
      if (!fmtstrend)
      if (!fmtstrend)
        break;
        break;
      fmtstrpart = fmtstrend;
      fmtstrpart = fmtstrend;
      *fmtstrpart = '%';
      *fmtstrpart = '%';
    }
    }
}
}
 
 

powered by: WebSVN 2.1.0

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