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 1319

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

powered by: WebSVN 2.1.0

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