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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [or1ksim/] [or1ksim-0.3.0/] [support/] [simprintf.c] - Blame information for rev 403

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

Line No. Rev Author Line
1 19 jeremybenn
/* simprintf.c -- Simulator printf implementation
2
 
3
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
4
   Copyright (C) 2008 Embecosm Limited
5
 
6
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7
 
8
   This file is part of OpenRISC 1000 Architectural Simulator.
9
 
10
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14
 
15
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19
 
20
   You should have received a copy of the GNU General Public License along
21
   with this program.  If not, see <http://www.gnu.org/licenses/>. */
22
 
23
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25
 
26
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.  I wrote
27
   following code when basic simulator started to work and I was desperate to
28
   use some PRINTFs in my debugged code. And it was also used to get some
29
   output from Dhrystone MIPS benchmark. */
30
 
31
 
32
/* Autoconf and/or portability configuration */
33
#include "config.h"
34
#include "port.h"
35
 
36
/* System includes */
37
#include <stdlib.h>
38
 
39
/* Package includes */
40
#include "sim-config.h"
41
#include "arch.h"
42
#include "debug.h"
43
#include "abstract.h"
44
#include "execute.h"
45
 
46
 
47
/* Should args be passed on stack for simprintf
48
 *
49
 * FIXME: do not enable this since it causes problems
50
 *        in some cases (an example beeing cbasic test
51
 *        from orp testbench). the problems is in
52
 *
53
 *        or1k/support/simprintf.c
54
 *
55
 *        #if STACK_ARGS
56
 *                arg = eval_mem32(argaddr,&breakpoint);
57
 *                argaddr += 4;
58
 *        #else
59
 *                sprintf(regstr, "r%u", ++argaddr);
60
 *                arg = evalsim_reg(atoi(regstr));
61
 *        #endif
62
 *
63
 *        the access to memory should be without any
64
 *        checks (ie not like or32 application accessed it)
65
 *
66
 */
67
#define STACK_ARGS 0
68
 
69
/* Length of PRINTF format string */
70
#define FMTLEN 2000
71
 
72
char fmtstr[FMTLEN];
73
 
74
static char *
75
simgetstr (oraddr_t stackaddr, unsigned long regparam)
76
{
77
  oraddr_t fmtaddr;
78
  int i;
79
 
80
  fmtaddr = regparam;
81
 
82
  i = 0;
83
  while (eval_direct8 (fmtaddr, 1, 0) != '\0')
84
    {
85
      fmtstr[i++] = eval_direct8 (fmtaddr, 1, 0);
86
      fmtaddr++;
87
      if (i == FMTLEN - 1)
88
        break;
89
    }
90
  fmtstr[i] = '\0';
91
 
92
  return fmtstr;
93
}
94
 
95
void
96
simprintf (oraddr_t stackaddr, unsigned long regparam)
97
{
98
  uint32_t arg;
99
  oraddr_t argaddr;
100
  char *fmtstrend;
101
  char *fmtstrpart = fmtstr;
102
  int tee_exe_log;
103
 
104
  simgetstr (stackaddr, regparam);
105
 
106
#if STACK_ARGS
107
  argaddr = stackaddr;
108
#else
109
  argaddr = 3;
110
#endif
111
  tee_exe_log = (config.sim.exe_log
112
                 && (config.sim.exe_log_type == EXE_LOG_SOFTWARE
113
                     || config.sim.exe_log_type == EXE_LOG_SIMPLE)
114
                 && config.sim.exe_log_start <= runtime.cpu.instructions
115
                 && (config.sim.exe_log_end <= 0
116
                     || runtime.cpu.instructions <= config.sim.exe_log_end));
117
 
118
  if (tee_exe_log)
119
    fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
120
  while (strlen (fmtstrpart))
121
    {
122
      if ((fmtstrend = strstr (fmtstrpart + 1, "%")))
123
        *fmtstrend = '\0';
124
      if (strstr (fmtstrpart, "%"))
125
        {
126
          char *tmp;
127
          int string = 0;
128
#if STACK_ARGS
129
          arg = eval_direct32 (argaddr, 1, 0);
130
          argaddr += 4;
131
#else
132
          {
133
            /* JPB. I can't see how the original code ever worked. It does trash
134
               the file pointer by overwriting the end of regstr. In any case
135
               why create a string, only to turn it back into an integer! */
136
 
137
            /* orig:  char regstr[5]; */
138
            /* orig:  */
139
            /* orig:  sprintf(regstr, "r%"PRIxADDR, ++argaddr); */
140
            /* orig:  arg = evalsim_reg(atoi(regstr)); */
141
 
142
            arg = evalsim_reg (++argaddr);
143
          }
144
#endif
145
          tmp = fmtstrpart;
146
          if (*tmp == '%')
147
            {
148
              tmp++;
149
              while (*tmp == '-' || (*tmp >= '0' && *tmp <= '9'))
150
                tmp++;
151
              if (*tmp == 's')
152
                string = 1;
153
            }
154
          if (string)
155
            {
156
              int len = 0;
157
              char *str;
158
              for (; eval_direct8 (arg++, 1, 0); len++);
159
              len++;            /* for null char */
160
              arg -= len;
161
              str = (char *) malloc (len);
162
              len = 0;
163
              for (; eval_direct8 (arg, 1, 0); len++)
164
                *(str + len) = eval_direct8 (arg++, 1, 0);
165
              *(str + len) = eval_direct8 (arg, 1, 0);   /* null ch */
166
              printf (fmtstrpart, str);
167
              if (tee_exe_log)
168
                fprintf (runtime.sim.fexe_log, fmtstrpart, str);
169
              free (str);
170
            }
171
          else
172
            {
173
              printf (fmtstrpart, arg);
174
              if (tee_exe_log)
175
                fprintf (runtime.sim.fexe_log, fmtstrpart, arg);
176
            }
177
        }
178
      else
179
        {
180
          printf (fmtstrpart);
181
          if (tee_exe_log)
182
            fprintf (runtime.sim.fexe_log, fmtstrpart);
183
        }
184
      if (!fmtstrend)
185
        break;
186
      fmtstrpart = fmtstrend;
187
      *fmtstrpart = '%';
188
    }
189
}

powered by: WebSVN 2.1.0

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