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

Subversion Repositories or1k_old

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

powered by: WebSVN 2.1.0

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