/* 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 <abstract.h>
|
#include <abstract.h>
|
#include <arch.h>
|
#include <arch.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(unsigned long stackaddr, unsigned long regparam)
|
char *simgetstr(unsigned long stackaddr, unsigned long regparam)
|
{
|
{
|
unsigned long fmtaddr;
|
unsigned long fmtaddr;
|
FILE *f;
|
FILE *f;
|
int i;
|
int i;
|
|
int breakpoint = 0;
|
|
|
#if STACK_ARGS
|
#if STACK_ARGS
|
fmtaddr = eval_mem32(stackaddr);
|
fmtaddr = eval_mem32(stackaddr,&breakpoint);
|
#else
|
#else
|
fmtaddr = regparam;
|
fmtaddr = regparam;
|
#endif
|
#endif
|
|
|
i = 0;
|
i = 0;
|
while (eval_mem8(fmtaddr) != '\0') {
|
while (eval_mem8(fmtaddr,&breakpoint) != '\0') {
|
fmtstr[i++] = eval_mem8(fmtaddr);
|
fmtstr[i++] = eval_mem8(fmtaddr,&breakpoint);
|
fmtaddr++;
|
fmtaddr++;
|
if (i == FMTLEN - 1)
|
if (i == FMTLEN - 1)
|
break;
|
break;
|
}
|
}
|
fmtstr[i] = '\0';
|
fmtstr[i] = '\0';
|
|
|
return fmtstr;
|
return fmtstr;
|
}
|
}
|
|
|
void simprintf(unsigned long stackaddr, unsigned long regparam)
|
void simprintf(unsigned long stackaddr, unsigned long regparam)
|
{
|
{
|
unsigned long fmtaddr;
|
unsigned long fmtaddr;
|
FILE *f;
|
FILE *f;
|
int i = 0;
|
int i = 0;
|
|
int breakpoint = 0;
|
|
|
simgetstr(stackaddr, regparam);
|
simgetstr(stackaddr, regparam);
|
|
|
debug("simprintf: stackaddr: 0x%.8lx", stackaddr);
|
debug("simprintf: stackaddr: 0x%.8lx", stackaddr);
|
if ((f = fopen("stdout.txt", "a+"))) {
|
if ((f = fopen("stdout.txt", "a+"))) {
|
unsigned long arg;
|
unsigned long arg;
|
unsigned long argaddr;
|
unsigned long argaddr;
|
unsigned char regstr[5];
|
unsigned char regstr[5];
|
char *fmtstrend;
|
char *fmtstrend;
|
char *fmtstrpart = fmtstr;
|
char *fmtstrpart = fmtstr;
|
|
|
#if STACK_ARGS
|
#if STACK_ARGS
|
argaddr = stackaddr;
|
argaddr = stackaddr;
|
#else
|
#else
|
argaddr = 3;
|
argaddr = 3;
|
#endif
|
#endif
|
debug("simprintf: %s", fmtstrpart);
|
debug("simprintf: %s", fmtstrpart);
|
while(strlen(fmtstrpart)) {
|
while(strlen(fmtstrpart)) {
|
debug("simprintf(): 1");
|
debug("simprintf(): 1");
|
if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
|
if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
|
*fmtstrend = '\0';
|
*fmtstrend = '\0';
|
debug(" 2");
|
debug(" 2");
|
if (strstr(fmtstrpart, "%")) {
|
if (strstr(fmtstrpart, "%")) {
|
debug(" 3");
|
debug(" 3");
|
#if STACK_ARGS
|
#if STACK_ARGS
|
argaddr += 4;
|
argaddr += 4;
|
arg = eval_mem32(argaddr);
|
arg = eval_mem32(argaddr,&breakpoint);
|
#else
|
#else
|
sprintf(regstr, "r%u", ++argaddr);
|
sprintf(regstr, "r%u", ++argaddr);
|
arg = eval_reg(regstr);
|
arg = eval_reg(regstr);
|
#endif
|
#endif
|
debug(" 4: fmtstrpart=%p fmtstrpart=%s arg=%p", fmtstrpart, fmtstrpart, arg);
|
debug(" 4: fmtstrpart=%p fmtstrpart=%s arg=%p", fmtstrpart, fmtstrpart, arg);
|
if (strncmp(fmtstrpart, "%s", 2) == 0) {
|
if (strncmp(fmtstrpart, "%s", 2) == 0) {
|
int len = 0;
|
int len = 0;
|
char *str;
|
char *str;
|
for(; eval_mem8(arg++); len++);
|
for(; eval_mem8(arg++,&breakpoint); 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_mem8(arg); len++)
|
for(; eval_mem8(arg,&breakpoint); len++)
|
*(str+len) = eval_mem8(arg++);
|
*(str+len) = eval_mem8(arg++,&breakpoint);
|
*(str+len) = eval_mem8(arg); /* null ch */
|
*(str+len) = eval_mem8(arg,&breakpoint); /* null ch */
|
/* debug("4a: len=%d str=%s\n", len, str);
|
/* debug("4a: len=%d str=%s\n", len, str);
|
debug("4b:"); */
|
debug("4b:"); */
|
fprintf(f, fmtstrpart, str);
|
fprintf(f, fmtstrpart, str);
|
free(str);
|
free(str);
|
} else
|
} else
|
fprintf(f, fmtstrpart, arg);
|
fprintf(f, fmtstrpart, arg);
|
} else {
|
} else {
|
debug(" 5");
|
debug(" 5");
|
fprintf(f, fmtstrpart);
|
fprintf(f, fmtstrpart);
|
debug(fmtstrpart);
|
debug(fmtstrpart);
|
}
|
}
|
if (!fmtstrend)
|
if (!fmtstrend)
|
break;
|
break;
|
debug(" 6");
|
debug(" 6");
|
fmtstrpart = fmtstrend;
|
fmtstrpart = fmtstrend;
|
*fmtstrpart = '%';
|
*fmtstrpart = '%';
|
debug(" 7");
|
debug(" 7");
|
}
|
}
|
|
|
debug(" 8");
|
debug(" 8");
|
if (fclose(f))
|
if (fclose(f))
|
perror(strerror(errno));
|
perror(strerror(errno));
|
}
|
}
|
else
|
else
|
perror(strerror(errno));
|
perror(strerror(errno));
|
|
|
}
|
}
|
|
|