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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_1_x/] [or1ksim/] [support/] [simprintf.c] - Blame information for rev 1095

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

powered by: WebSVN 2.1.0

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