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 1308

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
  while (eval_mem8(fmtaddr,&breakpoint) != '\0') {
51
    fmtstr[i++] = eval_mem8(fmtaddr,&breakpoint);
52
    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 515 markom
        arg = eval_mem32(argaddr,&breakpoint);
97
        argaddr += 4;
98 7 jrydberg
#else
99 515 markom
        sprintf(regstr, "r%u", ++argaddr);
100 1095 lampret
        arg = evalsim_reg32(atoi(regstr));
101 7 jrydberg
#endif
102 515 markom
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=%p\n", fmtstrpart, fmtstrpart, arg);
103 1265 markom
        tmp = fmtstrpart;
104
        if (*tmp == '%') {
105
          tmp++;
106
          while (*tmp == '-' || *tmp >= '0' && *tmp <= '9') tmp++;
107
          if (*tmp == 's') string = 1;
108
        }
109
        if (string) {
110 515 markom
          int len = 0;
111
          char *str;
112
          for(; eval_mem8(arg++,&breakpoint); len++);
113
          len++;  /* for null char */
114
          arg -= len;
115
          str = (char *)malloc(len);
116
          len = 0;
117
          for(; eval_mem8(arg,&breakpoint); len++)
118
            *(str+len) = eval_mem8(arg++,&breakpoint);
119
          *(str+len) = eval_mem8(arg,&breakpoint); /* null ch */
120
          debug(6, "4a: len=%d str=%s\n", len, str);
121
          debug(6, "4b:");
122
          fprintf(f, fmtstrpart, str);
123 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
124 515 markom
          free(str);
125 704 markom
        } else {
126 515 markom
          fprintf(f, fmtstrpart, arg);
127 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
128
        }
129 515 markom
      } else {
130
        debug(6, " 5");
131
        fprintf(f, fmtstrpart);
132 704 markom
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
133 515 markom
        debug(6, fmtstrpart);
134
      }
135
      if (!fmtstrend)
136
        break;
137
      debug(6, " 6");
138
      fmtstrpart = fmtstrend;
139
      *fmtstrpart = '%';
140
      debug(6, " 7");
141
    }
142
 
143
    debug(6," 8\n");
144
    if (fclose(f))
145
      perror(strerror(errno));
146
  }
147
  else
148
    perror(strerror(errno));
149
 
150 7 jrydberg
}

powered by: WebSVN 2.1.0

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