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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [utils/] [binlog2readable.cpp] - Blame information for rev 305

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 63 julius
// ----------------------------------------------------------------------------
2
 
3
// Interprets ORPSoC's Cycle Accurate model binary format log files
4
 
5
// Contributor Julius Baxter <jb@orsoc.se>
6
 
7
// This program is free software: you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License as published by
9
// the Free Software Foundation, either version 3 of the License, or (at your
10
// option) any later version.
11
 
12
// This program is distributed in the hope that it will be useful, but WITHOUT
13
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15
// License for more details.
16
 
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
20
// ----------------------------------------------------------------------------
21
 
22
// $Id$
23
 
24
 
25
#include <string.h>
26
#include <iostream>
27
#include <iomanip>
28
#include <cstdlib>
29
#include <stdint.h> 
30
#include <fstream>
31
#include <sys/types.h>
32
 
33
using namespace std;
34
 
35
 
36
struct s_binary_output_buffer{
37
  long long insn_count;
38
  uint32_t pc;
39
  uint32_t insn;
40
  char exception;
41
  uint32_t regs[32];
42
  uint32_t sr;
43
  uint32_t epcr0;
44
  uint32_t eear0;
45
  uint32_t eser0;
46
} __attribute__((__packed__));
47
 
48
struct s_binary_output_buffer_sans_regs{
49
  long long insn_count;
50
  uint32_t pc;
51
  uint32_t insn;
52
  char exception;
53
} __attribute__((__packed__));
54
 
55
void printUsage()
56
{
57
  cerr << endl << "Error: No input file specified." << endl;
58
  cerr << endl;
59
  cerr << "Usage: binlog2readable <options> <file>" << endl;
60
  cerr << endl;
61
  cerr << "Convert binary formatted ORPSoC execution log <file> to human readable format" << endl;
62
 
63
  cerr << "Options:" << endl;
64
  cerr << "  -o <file>\tOutput file, if not specified stdout is used" << endl;
65
  cerr << "  --skip <num>\tSkip <num> instructions from the front of the log" << endl;
66
  cerr << "  --last <num>\tPrint the last <num> instructions from the end of the log" << endl;
67
  cerr << endl;
68
 
69
}
70
 
71
int main(int argc, char **argv)
72
{
73
  ifstream inFile;
74
  ofstream outFile; /* Anyone know how to make these things into stdouts?? */
75
  unsigned long long filesize;
76
  int using_std_out = 1; /* Default out is to stdout */
77
  char reg_vals_included;
78
  int skip_insns = 0;
79
  int skip_insns_from_end = -1;
80
  struct s_binary_output_buffer buf;
81
  struct s_binary_output_buffer_sans_regs buf_sans_regs;
82
 
83
  if (argc < 2)
84
    {
85
      printUsage();
86
      return 1;
87
    }
88
 
89
  for(int i=1; i < argc; i++)
90
    {
91
      if ((strcmp(argv[i], "-s")==0) ||
92
          (strcmp(argv[i], "--skip")==0))
93
        {
94
          if (i+1 < argc)
95
            if(argv[i+1][0] != '-')
96
              {
97
                skip_insns = atoi(argv[i+1]);
98
                skip_insns_from_end = 0;
99
                i++;
100
              }
101
        }
102
      else if (strcmp(argv[i], "--last")==0)
103
        {
104
          if (i+1 < argc)
105
            if(argv[i+1][0] != '-')
106
              {
107
                skip_insns = atoi(argv[i+1]);
108
                skip_insns_from_end = 1;
109
                i++;
110
              }
111
        }
112
      else if (strcmp(argv[i], "-o")==0)
113
        {
114
          if (i+1 < argc)
115
            if(argv[i+1][0] != '-')
116
              {
117
                /* Were given a file to output to */
118
                outFile.open(argv[i+1], ios::out);
119
                if (!outFile.is_open())
120
                  {
121
                    cerr << "Error: unable to open file " << argv[2] << " for writing." << endl;
122
                    if (inFile.is_open()) inFile.close();
123
                    return 1;
124
 
125
                  }
126
 
127
                using_std_out = 0;
128
 
129
              }
130
        }
131
      else
132
        {
133
          /* Input file */
134
          if (!inFile.is_open())
135
            {
136
              inFile.open(argv[i], ios::in | ios::binary);
137
              if (!inFile.is_open())
138
                {
139
                  cerr << "Error: unable to open file " << argv[1] << " for reading." << endl;
140
                  return 1;
141
                }
142
            }
143
 
144
        }
145
    }
146
 
147
 
148
  /* First byte contains whether we've got register values included or
149
  just executed instruction, pc and number*/
150
  inFile.seekg(0); /* Position getpointer to start of file*/
151
  inFile.read(&reg_vals_included, 1);
152
 
153
  if (skip_insns)
154
    {
155
      /* Position the file pointer to the right place before printing out */
156
      if (skip_insns_from_end == -1)
157
        goto close_exit;
158
      if (skip_insns_from_end)
159
        {
160
 
161
          // go to end
162
          inFile.seekg(0,ios::end);
163
          filesize = inFile.tellg();
164
          inFile.seekg(0);
165
          //cout << "filesize: " << filesize << endl;
166
          // seek backwards
167
          if (reg_vals_included)
168
            for (int i=0;i<skip_insns;i++)
169
              inFile.seekg(filesize-skip_insns*sizeof(struct s_binary_output_buffer));
170
          else
171
            for (int i=0;i<skip_insns;i++)
172
              inFile.seekg(filesize-(skip_insns*sizeof(struct s_binary_output_buffer_sans_regs)));
173
 
174
        }
175
      else
176
        {
177
          // skip from start
178
          inFile.seekg(1);
179
          if (reg_vals_included)
180
            inFile.seekg(skip_insns*sizeof(struct s_binary_output_buffer),ios::cur);
181
          else
182
            inFile.seekg((skip_insns*sizeof(struct s_binary_output_buffer_sans_regs)),ios::cur);
183
 
184
        }
185
    }
186
  else
187
    inFile.seekg(1);
188
 
189
  //cout << "starting at: " << inFile.tellg() << endl;
190
 
191
  if (reg_vals_included)
192
    {
193
      if (using_std_out)
194
        {
195
          while (1)
196
            {
197
 
198
              inFile.read((char*)&buf, sizeof(struct s_binary_output_buffer));
199
 
200
              if(!inFile.eof())
201
                {
202
                  cout << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << buf.insn_count << "): " << std::setfill('0') << hex << std::setw(8) << buf.pc << ":  " << hex << std::setw(8) << buf.insn;
203
                  if(buf.exception) cout << "  (exception)";
204
                  cout << endl;
205
 
206
                  // Print general purpose register contents
207
                  for (int i=0; i<32; i++)
208
                    {
209
                      if ((i%4 == 0)&&(i>0)) cout << endl;
210
                      cout << std::setfill('0');
211
                      cout << "GPR" << dec << std::setw(2) << i << ": " <<  hex << std::setw(8) << buf.regs[i] << "  ";
212
                    }
213
                  cout << endl;
214
                  cout << "SR   : " <<  hex << std::setw(8) << buf.sr << "  ";
215
                  cout << "EPCR0: " <<  hex << std::setw(8) << buf.epcr0 << "  ";
216
                  cout << "EEAR0: " <<  hex << std::setw(8) << buf.eear0 << "  ";
217
                  cout << "ESR0 : " <<  hex << std::setw(8) << buf.eser0 << endl;
218
                }
219
              else
220
                break;
221
 
222
            }
223
        }
224
      else
225
        {
226
          while(1)
227
            {
228
              /* Outputting to a file */
229
              inFile.read((char*)&buf, sizeof(struct s_binary_output_buffer));
230
              //inFile.seekg(sizeof(struct s_binary_output_buffer), ios::cur);
231
              if(!inFile.eof())
232
                {
233
                  outFile << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << buf.insn_count << "): " << std::setfill('0') << hex << std::setw(8) << buf .pc << ":  " << hex << std::setw(8) << buf.insn;
234
                  if(buf.exception) outFile << "  (exception)";
235
                  outFile << endl;
236
 
237
                  // Print general purpose register contents
238
                  for (int i=0; i<32; i++)
239
                    {
240
                      if ((i%4 == 0)&&(i>0)) outFile << endl;
241
                      outFile << std::setfill('0');
242
                      outFile << "GPR" << dec << std::setw(2) << i << ": " <<  hex << std::setw(8) << buf.regs[i] << "  ";
243
                    }
244
                  outFile << endl;
245
                  outFile << "SR   : " <<  hex << std::setw(8) << buf.sr << "  ";
246
                  outFile << "EPCR0: " <<  hex << std::setw(8) << buf.epcr0 << "  ";
247
                  outFile << "EEAR0: " <<  hex << std::setw(8) << buf.eear0 << "  ";
248
                  outFile << "ESR0 : " <<  hex << std::setw(8) << buf.eser0 << endl;
249
                }
250
              else
251
                break;
252
 
253
            }
254
        }
255
    }
256
      else /* No regs in data */
257
    {
258
      if (using_std_out)
259
        {
260
          while(1)
261
            {
262
              inFile.read((char*)&buf_sans_regs, sizeof(struct s_binary_output_buffer_sans_regs));
263
              if (!inFile.eof())
264
                {
265
                  cout << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << buf_sans_regs.insn_count << "): " << std::setfill('0') << hex << std::setw(8) << buf_sans_regs.pc << ":  " << hex << std::setw(8) << buf_sans_regs.insn;
266
                  if(buf_sans_regs.exception) cout << "  (exception)";
267
                  cout << endl;
268
                }
269
              else
270
                break;
271
            }
272
          cout << endl;
273
        }
274
      else
275
        {
276
          /* Outputting to a file */
277
          while(!inFile.eof())
278
            {
279
              inFile.read((char*)&buf_sans_regs, sizeof(struct s_binary_output_buffer_sans_regs));
280
              //inFile.seekg(sizeof(struct s_binary_output_buffer_sans_regs), ios::cur);
281
              outFile << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << buf_sans_regs.insn_count << "): " << std::setfill('0') << hex << std::setw(8) << buf_sans_regs.pc << ":  " << hex << std::setw(8) << buf_sans_regs.insn;
282
              if(buf_sans_regs.exception) outFile << "  (exception)" << endl;
283
              outFile << endl;
284
 
285
            }
286
          outFile << endl;
287
        }
288
    }
289
 
290
 close_exit:
291
  inFile.close();
292
  if (!using_std_out) outFile.close();
293
 
294
}

powered by: WebSVN 2.1.0

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