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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [support/] [simprintf.c] - Blame information for rev 515

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

Line No. Rev Author Line
1 7 jrydberg
/* libc.c -- dummy C library simulation
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.
21
I wrote following code when basic simulator started to work and I was
22
desperate to use some printfs in my debugged code. And it was
23
also used to get some output from Dhrystone MIPS benchmark. */
24
 
25
#include <stdio.h>
26
#include <string.h>
27
#include <errno.h>
28
#include <stdarg.h>
29
 
30
#include <abstract.h>
31
#include <arch.h>
32
 
33
/* Length of printf format string */
34
#define FMTLEN 2000
35
 
36 75 lampret
char fmtstr[FMTLEN];
37
 
38
char *simgetstr(unsigned long stackaddr, unsigned long regparam)
39 7 jrydberg
{
40 515 markom
  unsigned long fmtaddr;
41
  FILE *f;
42
  int i;
43
  int breakpoint = 0;
44 123 markom
 
45 515 markom
  fmtaddr = regparam;
46
 
47
  i = 0;
48
  while (eval_mem8(fmtaddr,&breakpoint) != '\0') {
49
    fmtstr[i++] = eval_mem8(fmtaddr,&breakpoint);
50
    fmtaddr++;
51
    if (i == FMTLEN - 1)
52
      break;
53
  }
54
  fmtstr[i] = '\0';
55
 
56
  return fmtstr;
57 75 lampret
}
58
 
59
void simprintf(unsigned long stackaddr, unsigned long regparam)
60
{
61 515 markom
  unsigned long fmtaddr;
62
  FILE *f;
63
  int i = 0;
64
  int breakpoint = 0;
65 123 markom
 
66 515 markom
  simgetstr(stackaddr, regparam);
67
 
68
  debug(6, "simprintf: stackaddr: 0x%.8lx\n", stackaddr);
69
  if ((f = fopen("stdout.txt", "a+"))) {
70
    unsigned long arg;
71
    unsigned long argaddr;
72
    unsigned char regstr[5];
73
    char *fmtstrend;
74
    char *fmtstrpart = fmtstr;
75
 
76 7 jrydberg
#if STACK_ARGS
77 515 markom
    argaddr = stackaddr;
78 7 jrydberg
#else
79 515 markom
    argaddr = 3;
80 7 jrydberg
#endif
81 515 markom
    debug(6, "simprintf: %s\n", fmtstrpart);
82
    while(strlen(fmtstrpart)) {
83
      debug(6, "simprintf(): 1");
84
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
85
        *fmtstrend = '\0';
86
      debug(6," 2");
87
      if (strstr(fmtstrpart, "%")) {
88
        debug(6, " 3");
89 7 jrydberg
#if STACK_ARGS
90 515 markom
        arg = eval_mem32(argaddr,&breakpoint);
91
        argaddr += 4;
92 7 jrydberg
#else
93 515 markom
        sprintf(regstr, "r%u", ++argaddr);
94
        arg = eval_reg(regstr);
95 7 jrydberg
#endif
96 515 markom
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=%p\n", fmtstrpart, fmtstrpart, arg);
97
        if (strncmp(fmtstrpart, "%s", 2) == 0) {
98
          int len = 0;
99
          char *str;
100
          for(; eval_mem8(arg++,&breakpoint); len++);
101
          len++;  /* for null char */
102
          arg -= len;
103
          str = (char *)malloc(len);
104
          len = 0;
105
          for(; eval_mem8(arg,&breakpoint); len++)
106
            *(str+len) = eval_mem8(arg++,&breakpoint);
107
          *(str+len) = eval_mem8(arg,&breakpoint); /* null ch */
108
          debug(6, "4a: len=%d str=%s\n", len, str);
109
          debug(6, "4b:");
110
          fprintf(f, fmtstrpart, str);
111
          free(str);
112
        } else
113
          fprintf(f, fmtstrpart, arg);
114
      } else {
115
        debug(6, " 5");
116
        fprintf(f, fmtstrpart);
117
        debug(6, fmtstrpart);
118
      }
119
      if (!fmtstrend)
120
        break;
121
      debug(6, " 6");
122
      fmtstrpart = fmtstrend;
123
      *fmtstrpart = '%';
124
      debug(6, " 7");
125
    }
126
 
127
    debug(6," 8\n");
128
    if (fclose(f))
129
      perror(strerror(errno));
130
  }
131
  else
132
    perror(strerror(errno));
133
 
134 7 jrydberg
}

powered by: WebSVN 2.1.0

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