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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [or1ksim/] [support/] [simprintf.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1446 nogj
/* simprintf.c -- Simulator printf implementation
2 1744 jeremybenn
 
3 7 jrydberg
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
4 1744 jeremybenn
   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 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25
 
26 1744 jeremybenn
/* 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 7 jrydberg
 
31
 
32 1748 jeremybenn
/* Autoconf and/or portability configuration */
33 1350 nogj
#include "config.h"
34 1748 jeremybenn
#include "port.h"
35 1350 nogj
 
36 1748 jeremybenn
/* System includes */
37
#include <stdlib.h>
38 1350 nogj
 
39 1748 jeremybenn
/* Package includes */
40
#include "sim-config.h"
41 1350 nogj
#include "arch.h"
42 1748 jeremybenn
#include "debug.h"
43 1308 phoenix
#include "abstract.h"
44 1557 nogj
#include "execute.h"
45 7 jrydberg
 
46 1748 jeremybenn
 
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 997 markom
/* Length of PRINTF format string */
70 7 jrydberg
#define FMTLEN 2000
71
 
72 75 lampret
char fmtstr[FMTLEN];
73
 
74 1748 jeremybenn
static char *
75
simgetstr (oraddr_t stackaddr, unsigned long regparam)
76 7 jrydberg
{
77 1350 nogj
  oraddr_t fmtaddr;
78 515 markom
  int i;
79 123 markom
 
80 515 markom
  fmtaddr = regparam;
81 1748 jeremybenn
 
82 515 markom
  i = 0;
83 1748 jeremybenn
  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 515 markom
  fmtstr[i] = '\0';
91 1748 jeremybenn
 
92 515 markom
  return fmtstr;
93 75 lampret
}
94
 
95 1748 jeremybenn
void
96
simprintf (oraddr_t stackaddr, unsigned long regparam)
97 75 lampret
{
98 1748 jeremybenn
  uint32_t arg;
99
  oraddr_t argaddr;
100
  char *fmtstrend;
101
  char *fmtstrpart = fmtstr;
102
  int tee_exe_log;
103 123 markom
 
104 1748 jeremybenn
  simgetstr (stackaddr, regparam);
105
 
106 7 jrydberg
#if STACK_ARGS
107 1748 jeremybenn
  argaddr = stackaddr;
108 7 jrydberg
#else
109 1748 jeremybenn
  argaddr = 3;
110 7 jrydberg
#endif
111 1748 jeremybenn
  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 7 jrydberg
#if STACK_ARGS
129 1748 jeremybenn
          arg = eval_direct32 (argaddr, 1, 0);
130
          argaddr += 4;
131 7 jrydberg
#else
132 1748 jeremybenn
          {
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 1744 jeremybenn
 
137 1748 jeremybenn
            /* orig:  char regstr[5]; */
138
            /* orig:  */
139
            /* orig:  sprintf(regstr, "r%"PRIxADDR, ++argaddr); */
140
            /* orig:  arg = evalsim_reg(atoi(regstr)); */
141 1744 jeremybenn
 
142 1748 jeremybenn
            arg = evalsim_reg (++argaddr);
143
          }
144 7 jrydberg
#endif
145 1748 jeremybenn
          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 515 markom
      if (!fmtstrend)
185 1748 jeremybenn
        break;
186 515 markom
      fmtstrpart = fmtstrend;
187
      *fmtstrpart = '%';
188
    }
189 7 jrydberg
}

powered by: WebSVN 2.1.0

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