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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [support/] [simprintf.c] - Rev 1319

Go to most recent revision | Compare with Previous | Blame | View Log

/* libc.c -- dummy C library simulation
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
 
This file is part of OpenRISC 1000 Architectural Simulator.
 
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 Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.
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
also used to get some output from Dhrystone MIPS benchmark. */
 
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
 
#include "abstract.h"
#include "arch.h"
#include "sim-config.h"
#include "debug.h"
 
/* Length of PRINTF format string */
#define FMTLEN 2000
 
char fmtstr[FMTLEN];
 
char *simgetstr(unsigned long stackaddr, unsigned long regparam)
{
  unsigned long fmtaddr;
  int i;
  int breakpoint = 0;
 
  fmtaddr = regparam;
 
  i = 0;
  while (eval_direct8(fmtaddr,&breakpoint) != '\0') {
    fmtstr[i++] = eval_direct8(fmtaddr,&breakpoint);
    fmtaddr++;
    if (i == FMTLEN - 1)
      break;
  }
  fmtstr[i] = '\0';
 
  return fmtstr;
}
 
void simprintf(unsigned long stackaddr, unsigned long regparam)
{
  FILE *f;
  int breakpoint = 0;
 
  simgetstr(stackaddr, regparam);
 
  debug(6, "simprintf: stackaddr: 0x%.8lx\n", stackaddr);
  if ((f = fopen(config.sim.fstdout, "a+"))) {
    unsigned long arg;
    unsigned long argaddr;
    char *fmtstrend;
    char *fmtstrpart = fmtstr;
    int tee_exe_log;
 
#if STACK_ARGS
    argaddr = stackaddr;
#else
    argaddr = 3;
#endif
    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));
 
    if (tee_exe_log) fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
    debug(6, "simprintf: %s\n", fmtstrpart);
    while(strlen(fmtstrpart)) {
      debug(6, "simprintf(): 1");
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
        *fmtstrend = '\0';
      debug(6," 2");
      if (strstr(fmtstrpart, "%")) {
        char *tmp;
        int string = 0;
        debug(6, " 3");
#if STACK_ARGS
        arg = eval_direct32(argaddr,&breakpoint);
        argaddr += 4;
#else
	{
	  unsigned char regstr[5];
 
          sprintf(regstr, "r%u", ++argaddr);
          arg = evalsim_reg32(atoi(regstr));
        }
#endif
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=%p\n", fmtstrpart, fmtstrpart, arg);
        tmp = fmtstrpart;
        if (*tmp == '%') {
          tmp++;
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
          if (*tmp == 's') string = 1;
        }
        if (string) {
          int len = 0;
          char *str;
          for(; eval_direct8(arg++,&breakpoint); len++);
          len++;  /* for null char */
          arg -= len;
          str = (char *)malloc(len);
          len = 0;
          for(; eval_direct8(arg,&breakpoint); len++)
            *(str+len) = eval_direct8(arg++,&breakpoint);
          *(str+len) = eval_direct8(arg,&breakpoint); /* null ch */
          debug(6, "4a: len=%d str=%s\n", len, str);
          debug(6, "4b:");
          fprintf(f, fmtstrpart, str);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
          free(str);
        } else {
          fprintf(f, fmtstrpart, arg);
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
        }
      } else {
        debug(6, " 5");
        fprintf(f, fmtstrpart);
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
        debug(6, fmtstrpart);
      }
      if (!fmtstrend)
        break;
      debug(6, " 6");
      fmtstrpart = fmtstrend;
      *fmtstrpart = '%';
      debug(6, " 7");
    }
 
    debug(6," 8\n");
    if (fclose(f))
      perror(strerror(errno));
  }
  else
    perror(strerror(errno));
 
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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