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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [support/] [simprintf.c] - Diff between revs 1429 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1429 Rev 1765
/* libc.c -- dummy C library simulation
/* libc.c -- dummy C library simulation
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
 
 
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
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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.
I wrote following code when basic simulator started to work and I was
I wrote following code when basic simulator started to work and I was
desperate to use some PRINTFs in my debugged code. And it was
desperate to use some PRINTFs in my debugged code. And it was
also used to get some output from Dhrystone MIPS benchmark. */
also used to get some output from Dhrystone MIPS benchmark. */
 
 
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <errno.h>
#include <stdarg.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
#include "config.h"
#include "config.h"
 
 
#ifdef HAVE_INTTYPES_H
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#include <inttypes.h>
#endif
#endif
 
 
#include "port.h"
#include "port.h"
#include "arch.h"
#include "arch.h"
#include "abstract.h"
#include "abstract.h"
#include "sim-config.h"
#include "sim-config.h"
#include "debug.h"
#include "debug.h"
 
 
/* Length of PRINTF format string */
/* Length of PRINTF format string */
#define FMTLEN 2000
#define FMTLEN 2000
 
 
char fmtstr[FMTLEN];
char fmtstr[FMTLEN];
 
 
char *simgetstr(oraddr_t stackaddr, unsigned long regparam)
char *simgetstr(oraddr_t stackaddr, unsigned long regparam)
{
{
  oraddr_t fmtaddr;
  oraddr_t fmtaddr;
  int i;
  int i;
  int breakpoint = 0;
  int breakpoint = 0;
 
 
  fmtaddr = regparam;
  fmtaddr = regparam;
 
 
  i = 0;
  i = 0;
  while (eval_direct8(fmtaddr,&breakpoint,1,0) != '\0') {
  while (eval_direct8(fmtaddr,&breakpoint,1,0) != '\0') {
    fmtstr[i++] = eval_direct8(fmtaddr,&breakpoint,1,0);
    fmtstr[i++] = eval_direct8(fmtaddr,&breakpoint,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 simprintf(oraddr_t stackaddr, unsigned long regparam)
void simprintf(oraddr_t stackaddr, unsigned long regparam)
{
{
  FILE *f;
  FILE *f;
  int breakpoint = 0;
  int breakpoint = 0;
 
 
  simgetstr(stackaddr, regparam);
  simgetstr(stackaddr, regparam);
 
 
  debug(6, "simprintf: stackaddr: 0x%"PRIxADDR"\n", stackaddr);
  debug(6, "simprintf: stackaddr: 0x%"PRIxADDR"\n", stackaddr);
  if ((f = fopen(config.sim.fstdout, "a+"))) {
  if ((f = fopen(config.sim.fstdout, "a+"))) {
    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;
 
 
#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 && (config.sim.exe_log_type == EXE_LOG_SOFTWARE || config.sim.exe_log_type == EXE_LOG_SIMPLE)
    tee_exe_log = (config.sim.exe_log && (config.sim.exe_log_type == EXE_LOG_SOFTWARE || config.sim.exe_log_type == EXE_LOG_SIMPLE)
       && config.sim.exe_log_start <= runtime.cpu.instructions && (config.sim.exe_log_end <= 0 || runtime.cpu.instructions <= config.sim.exe_log_end));
       && config.sim.exe_log_start <= runtime.cpu.instructions && (config.sim.exe_log_end <= 0 || runtime.cpu.instructions <= config.sim.exe_log_end));
 
 
    if (tee_exe_log) fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
    if (tee_exe_log) fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
    debug(6, "simprintf: %s\n", fmtstrpart);
    debug(6, "simprintf: %s\n", fmtstrpart);
    while(strlen(fmtstrpart)) {
    while(strlen(fmtstrpart)) {
      debug(6, "simprintf(): 1");
      debug(6, "simprintf(): 1");
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
        *fmtstrend = '\0';
        *fmtstrend = '\0';
      debug(6," 2");
      debug(6," 2");
      if (strstr(fmtstrpart, "%")) {
      if (strstr(fmtstrpart, "%")) {
        char *tmp;
        char *tmp;
        int string = 0;
        int string = 0;
        debug(6, " 3");
        debug(6, " 3");
#if STACK_ARGS
#if STACK_ARGS
        arg = eval_direct32(argaddr,&breakpoint,1,0);
        arg = eval_direct32(argaddr,&breakpoint,1,0);
        argaddr += 4;
        argaddr += 4;
#else
#else
        {
        {
          unsigned char regstr[5];
          unsigned char regstr[5];
 
 
          sprintf(regstr, "r%"PRIxADDR, ++argaddr);
          sprintf(regstr, "r%"PRIxADDR, ++argaddr);
          arg = evalsim_reg(atoi(regstr));
          arg = evalsim_reg(atoi(regstr));
        }
        }
#endif
#endif
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=0x%08"PRIx32"\n",
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=0x%08"PRIx32"\n",
              fmtstrpart, fmtstrpart, arg);
              fmtstrpart, fmtstrpart, arg);
        tmp = fmtstrpart;
        tmp = fmtstrpart;
        if (*tmp == '%') {
        if (*tmp == '%') {
          tmp++;
          tmp++;
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
          if (*tmp == 's') string = 1;
          if (*tmp == 's') string = 1;
        }
        }
        if (string) {
        if (string) {
          int len = 0;
          int len = 0;
          char *str;
          char *str;
          for(; eval_direct8(arg++,&breakpoint,1,0); len++);
          for(; eval_direct8(arg++,&breakpoint,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,&breakpoint,1,0); len++)
          for(; eval_direct8(arg,&breakpoint,1,0); len++)
            *(str+len) = eval_direct8(arg++,&breakpoint,1,0);
            *(str+len) = eval_direct8(arg++,&breakpoint,1,0);
          *(str+len) = eval_direct8(arg,&breakpoint,1,0); /* null ch */
          *(str+len) = eval_direct8(arg,&breakpoint,1,0); /* null ch */
          debug(6, "4a: len=%d str=%s\n", len, str);
          debug(6, "4a: len=%d str=%s\n", len, str);
          debug(6, "4b:");
          debug(6, "4b:");
          fprintf(f, fmtstrpart, str);
          fprintf(f, fmtstrpart, str);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
          free(str);
          free(str);
        } else {
        } else {
          fprintf(f, fmtstrpart, arg);
          fprintf(f, fmtstrpart, arg);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
        }
        }
      } else {
      } else {
        debug(6, " 5");
        debug(6, " 5");
        fprintf(f, fmtstrpart);
        fprintf(f, fmtstrpart);
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
        debug(6, fmtstrpart);
        debug(6, fmtstrpart);
      }
      }
      if (!fmtstrend)
      if (!fmtstrend)
        break;
        break;
      debug(6, " 6");
      debug(6, " 6");
      fmtstrpart = fmtstrend;
      fmtstrpart = fmtstrend;
      *fmtstrpart = '%';
      *fmtstrpart = '%';
      debug(6, " 7");
      debug(6, " 7");
    }
    }
 
 
    debug(6," 8\n");
    debug(6," 8\n");
    if (fclose(f))
    if (fclose(f))
      perror(strerror(errno));
      perror(strerror(errno));
  }
  }
  else
  else
    perror(strerror(errno));
    perror(strerror(errno));
 
 
}
}
 
 

powered by: WebSVN 2.1.0

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