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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [support/] [simprintf.c] - Blame information for rev 1487

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

Line No. Rev Author Line
1 1446 nogj
/* simprintf.c -- Simulator printf implementation
2 7 jrydberg
   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 997 markom
desperate to use some PRINTFs in my debugged code. And it was
23 7 jrydberg
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 1308 phoenix
#include <stdlib.h>
30 7 jrydberg
 
31 1350 nogj
#include "config.h"
32
 
33
#ifdef HAVE_INTTYPES_H
34
#include <inttypes.h>
35
#endif
36
 
37
#include "port.h"
38
#include "arch.h"
39 1308 phoenix
#include "abstract.h"
40 704 markom
#include "sim-config.h"
41 1308 phoenix
#include "debug.h"
42 7 jrydberg
 
43 997 markom
/* Length of PRINTF format string */
44 7 jrydberg
#define FMTLEN 2000
45
 
46 75 lampret
char fmtstr[FMTLEN];
47
 
48 1350 nogj
char *simgetstr(oraddr_t stackaddr, unsigned long regparam)
49 7 jrydberg
{
50 1350 nogj
  oraddr_t fmtaddr;
51 515 markom
  int i;
52 123 markom
 
53 515 markom
  fmtaddr = regparam;
54
 
55
  i = 0;
56 1487 nogj
  while (eval_direct8(fmtaddr,1,0) != '\0') {
57
    fmtstr[i++] = eval_direct8(fmtaddr,1,0);
58 515 markom
    fmtaddr++;
59
    if (i == FMTLEN - 1)
60
      break;
61
  }
62
  fmtstr[i] = '\0';
63
 
64
  return fmtstr;
65 75 lampret
}
66
 
67 1350 nogj
void simprintf(oraddr_t stackaddr, unsigned long regparam)
68 75 lampret
{
69 515 markom
  FILE *f;
70 123 markom
 
71 515 markom
  simgetstr(stackaddr, regparam);
72
 
73 1350 nogj
  debug(6, "simprintf: stackaddr: 0x%"PRIxADDR"\n", stackaddr);
74 823 ivang
  if ((f = fopen(config.sim.fstdout, "a+"))) {
75 1350 nogj
    uint32_t arg;
76
    oraddr_t argaddr;
77 515 markom
    char *fmtstrend;
78
    char *fmtstrpart = fmtstr;
79 704 markom
    int tee_exe_log;
80 515 markom
 
81 7 jrydberg
#if STACK_ARGS
82 515 markom
    argaddr = stackaddr;
83 7 jrydberg
#else
84 515 markom
    argaddr = 3;
85 7 jrydberg
#endif
86 704 markom
    tee_exe_log = (config.sim.exe_log && (config.sim.exe_log_type == EXE_LOG_SOFTWARE || config.sim.exe_log_type == EXE_LOG_SIMPLE)
87 884 markom
       && config.sim.exe_log_start <= runtime.cpu.instructions && (config.sim.exe_log_end <= 0 || runtime.cpu.instructions <= config.sim.exe_log_end));
88 704 markom
 
89 1265 markom
    if (tee_exe_log) fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
90 515 markom
    debug(6, "simprintf: %s\n", fmtstrpart);
91
    while(strlen(fmtstrpart)) {
92
      debug(6, "simprintf(): 1");
93
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
94
        *fmtstrend = '\0';
95
      debug(6," 2");
96
      if (strstr(fmtstrpart, "%")) {
97 1265 markom
        char *tmp;
98
        int string = 0;
99 515 markom
        debug(6, " 3");
100 7 jrydberg
#if STACK_ARGS
101 1487 nogj
        arg = eval_direct32(argaddr,1,0);
102 515 markom
        argaddr += 4;
103 7 jrydberg
#else
104 1315 phoenix
        {
105
          unsigned char regstr[5];
106
 
107 1350 nogj
          sprintf(regstr, "r%"PRIxADDR, ++argaddr);
108
          arg = evalsim_reg(atoi(regstr));
109 1315 phoenix
        }
110 7 jrydberg
#endif
111 1398 nogj
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=0x%08"PRIx32"\n",
112
              fmtstrpart, fmtstrpart, arg);
113 1265 markom
        tmp = fmtstrpart;
114
        if (*tmp == '%') {
115
          tmp++;
116
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
117
          if (*tmp == 's') string = 1;
118
        }
119
        if (string) {
120 515 markom
          int len = 0;
121
          char *str;
122 1487 nogj
          for(; eval_direct8(arg++,1,0); len++);
123 515 markom
          len++;  /* for null char */
124
          arg -= len;
125
          str = (char *)malloc(len);
126
          len = 0;
127 1487 nogj
          for(; eval_direct8(arg,1,0); len++)
128
            *(str+len) = eval_direct8(arg++,1,0);
129
          *(str+len) = eval_direct8(arg,1,0); /* null ch */
130 515 markom
          debug(6, "4a: len=%d str=%s\n", len, str);
131
          debug(6, "4b:");
132
          fprintf(f, fmtstrpart, str);
133 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
134 515 markom
          free(str);
135 704 markom
        } else {
136 515 markom
          fprintf(f, fmtstrpart, arg);
137 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
138
        }
139 515 markom
      } else {
140
        debug(6, " 5");
141
        fprintf(f, fmtstrpart);
142 704 markom
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
143 515 markom
        debug(6, fmtstrpart);
144
      }
145
      if (!fmtstrend)
146
        break;
147
      debug(6, " 6");
148
      fmtstrpart = fmtstrend;
149
      *fmtstrpart = '%';
150
      debug(6, " 7");
151
    }
152
 
153
    debug(6," 8\n");
154
    if (fclose(f))
155
      perror(strerror(errno));
156
  }
157
  else
158
    perror(strerror(errno));
159
 
160 7 jrydberg
}

powered by: WebSVN 2.1.0

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