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 823

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
desperate to use some printfs in my debugged code. And it was
23
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
/* Length of printf format string */
35
#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
    extern long instructions;
78 515 markom
 
79 7 jrydberg
#if STACK_ARGS
80 515 markom
    argaddr = stackaddr;
81 7 jrydberg
#else
82 515 markom
    argaddr = 3;
83 7 jrydberg
#endif
84 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)
85
       && config.sim.exe_log_start <= instructions && (config.sim.exe_log_end <= 0 || instructions <= config.sim.exe_log_end));
86
 
87
    if (tee_exe_log)
88
      fprintf (runtime.sim.fexe_log, "SIMPRINTF: ");
89 515 markom
    debug(6, "simprintf: %s\n", fmtstrpart);
90
    while(strlen(fmtstrpart)) {
91
      debug(6, "simprintf(): 1");
92
      if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
93
        *fmtstrend = '\0';
94
      debug(6," 2");
95
      if (strstr(fmtstrpart, "%")) {
96
        debug(6, " 3");
97 7 jrydberg
#if STACK_ARGS
98 515 markom
        arg = eval_mem32(argaddr,&breakpoint);
99
        argaddr += 4;
100 7 jrydberg
#else
101 515 markom
        sprintf(regstr, "r%u", ++argaddr);
102
        arg = eval_reg(regstr);
103 7 jrydberg
#endif
104 515 markom
        debug(6, " 4: fmtstrpart=%p fmtstrpart=%s arg=%p\n", fmtstrpart, fmtstrpart, arg);
105
        if (strncmp(fmtstrpart, "%s", 2) == 0) {
106
          int len = 0;
107
          char *str;
108
          for(; eval_mem8(arg++,&breakpoint); len++);
109
          len++;  /* for null char */
110
          arg -= len;
111
          str = (char *)malloc(len);
112
          len = 0;
113
          for(; eval_mem8(arg,&breakpoint); len++)
114
            *(str+len) = eval_mem8(arg++,&breakpoint);
115
          *(str+len) = eval_mem8(arg,&breakpoint); /* null ch */
116
          debug(6, "4a: len=%d str=%s\n", len, str);
117
          debug(6, "4b:");
118
          fprintf(f, fmtstrpart, str);
119 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, str);
120 515 markom
          free(str);
121 704 markom
        } else {
122 515 markom
          fprintf(f, fmtstrpart, arg);
123 704 markom
          if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart, arg);
124
        }
125 515 markom
      } else {
126
        debug(6, " 5");
127
        fprintf(f, fmtstrpart);
128 704 markom
        if (tee_exe_log) fprintf(runtime.sim.fexe_log, fmtstrpart);
129 515 markom
        debug(6, fmtstrpart);
130
      }
131
      if (!fmtstrend)
132
        break;
133
      debug(6, " 6");
134
      fmtstrpart = fmtstrend;
135
      *fmtstrpart = '%';
136
      debug(6, " 7");
137
    }
138
 
139
    debug(6," 8\n");
140
    if (fclose(f))
141
      perror(strerror(errno));
142
  }
143
  else
144
    perror(strerror(errno));
145
 
146 7 jrydberg
}

powered by: WebSVN 2.1.0

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