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 1765

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 1398 nogj
  while (eval_direct8(fmtaddr,&breakpoint,1,0) != '\0') {
58
    fmtstr[i++] = eval_direct8(fmtaddr,&breakpoint,1,0);
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 1398 nogj
        arg = eval_direct32(argaddr,&breakpoint,1,0);
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 1398 nogj
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=0x%08"PRIx32"\n",
114
              fmtstrpart, fmtstrpart, arg);
115 1265 markom
        tmp = fmtstrpart;
116
        if (*tmp == '%') {
117
          tmp++;
118
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
119
          if (*tmp == 's') string = 1;
120
        }
121
        if (string) {
122 515 markom
          int len = 0;
123
          char *str;
124 1398 nogj
          for(; eval_direct8(arg++,&breakpoint,1,0); len++);
125 515 markom
          len++;  /* for null char */
126
          arg -= len;
127
          str = (char *)malloc(len);
128
          len = 0;
129 1398 nogj
          for(; eval_direct8(arg,&breakpoint,1,0); len++)
130
            *(str+len) = eval_direct8(arg++,&breakpoint,1,0);
131
          *(str+len) = eval_direct8(arg,&breakpoint,1,0); /* null ch */
132 515 markom
          debug(6, "4a: len=%d str=%s\n", len, str);
133
          debug(6, "4b:");
134
          fprintf(f, fmtstrpart, str);
135 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
136 515 markom
          free(str);
137 704 markom
        } else {
138 515 markom
          fprintf(f, fmtstrpart, arg);
139 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
140
        }
141 515 markom
      } else {
142
        debug(6, " 5");
143
        fprintf(f, fmtstrpart);
144 704 markom
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
145 515 markom
        debug(6, fmtstrpart);
146
      }
147
      if (!fmtstrend)
148
        break;
149
      debug(6, " 6");
150
      fmtstrpart = fmtstrend;
151
      *fmtstrpart = '%';
152
      debug(6, " 7");
153
    }
154
 
155
    debug(6," 8\n");
156
    if (fclose(f))
157
      perror(strerror(errno));
158
  }
159
  else
160
    perror(strerror(errno));
161
 
162 7 jrydberg
}

powered by: WebSVN 2.1.0

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