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 1350

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

powered by: WebSVN 2.1.0

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