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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [Or1200MonitorSC.cpp] - Blame information for rev 52

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

Line No. Rev Author Line
1 6 julius
// ----------------------------------------------------------------------------
2
 
3
// SystemC OpenRISC 1200 Monitor: implementation
4
 
5
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
6
 
7
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8 44 julius
// Contributor Julius Baxter <jb@orsoc.se>
9 6 julius
 
10
// This file is part of the cycle accurate model of the OpenRISC 1000 based
11
// system-on-chip, ORPSoC, built using Verilator.
12
 
13
// This program is free software: you can redistribute it and/or modify it
14
// under the terms of the GNU Lesser General Public License as published by
15
// the Free Software Foundation, either version 3 of the License, or (at your
16
// option) any later version.
17
 
18
// This program is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21
// License for more details.
22
 
23
// You should have received a copy of the GNU Lesser General Public License
24
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 
26
// ----------------------------------------------------------------------------
27
 
28
// $Id: Or1200MonitorSC.cpp 303 2009-02-16 11:20:17Z jeremy $
29
 
30
#include <iostream>
31
#include <iomanip>
32 44 julius
#include <fstream>
33 52 julius
#include <sys/types.h>
34
#include <netinet/in.h>
35 44 julius
using namespace std;
36
 
37 6 julius
#include "Or1200MonitorSC.h"
38 44 julius
#include "OrpsocMain.h"
39 6 julius
 
40
 
41
SC_HAS_PROCESS( Or1200MonitorSC );
42
 
43
//! Constructor for the OpenRISC 1200 monitor
44
 
45
//! @param[in] name  Name of this module, passed to the parent constructor.
46
//! @param[in] accessor  Accessor class for this Verilated ORPSoC model
47
 
48
Or1200MonitorSC::Or1200MonitorSC (sc_core::sc_module_name   name,
49 49 julius
                                  OrpsocAccess             *_accessor,
50 51 julius
                                  MemoryLoad               *_memoryload,
51 49 julius
                                  int argc,
52
                                  char *argv[]) :
53 6 julius
  sc_module (name),
54 51 julius
  accessor (_accessor),
55
  memoryload(_memoryload)
56 6 julius
{
57 49 julius
 
58
  // If not -log option, then don't log
59
 
60
  string logfileDefault("vlt-executed.log");
61
  string logfileNameString;
62 52 julius
  profiling_enabled = 0;
63 51 julius
  string profileFileName(DEFAULT_PROF_FILE);
64 52 julius
  memdumpFileName = (DEFAULT_MEMDUMP_FILE);
65
  int memdump_start = 0; int memdump_end = 0;
66
  do_memdump = 0; // Default is not to do a dump of RAM at finish
67
 
68 51 julius
  insn_count=0;
69
  cycle_count=0;
70 49 julius
 
71
  exit_perf_summary_enabled = 1; // Simulation exit performance summary is 
72
                                 // on by default. Turn off with "-q" on the cmd line
73
 
74
  // Parse the command line options
75
  int cmdline_name_found=0;
76
  if (argc > 1)
77
    {
78
      // Search through the command line parameters for the "-log" option
79
      for(int i=1; i < argc; i++)
80
        {
81
          if ((strcmp(argv[i], "-l")==0) ||
82
              (strcmp(argv[i], "--log")==0))
83
            {
84
              logfileNameString = (argv[i+1]);
85
              cmdline_name_found=1;
86
            }
87 51 julius
          else if ((strcmp(argv[i], "-q")==0) ||
88
                   (strcmp(argv[i], "--quiet")==0))
89 49 julius
            {
90
              exit_perf_summary_enabled = 0;
91
            }
92 51 julius
          else if ((strcmp(argv[i], "-p")==0) ||
93
                   (strcmp(argv[i], "--profile")==0))
94
            {
95
              profiling_enabled = 1;
96 52 julius
              // Check for !end of command line and that next thing is not a command
97 51 julius
              if ((i+1 < argc)){
98
                if(argv[i+1][0] != '-')
99
                  profileFileName = (argv[i+1]);
100
              }
101
            }
102 52 julius
          else if ((strcmp(argv[i], "-m")==0) ||
103
                   (strcmp(argv[i], "--mdump")==0))
104
            {
105
              do_memdump = 1;
106
              // Check for !end of command line and that next thing is not a command
107
              // or a memory address
108
              if (i+1 < argc)
109
                {
110
                  if((argv[i+1][0] != '-') && (strncmp("0x", argv[i+1],2) != 0))
111
                    {
112
                      // Hopefully this is the filename we want to use.
113
                      // All addresses should have preceeding hex identifier 0x
114
                      memdumpFileName = argv[i+1];
115
                      // We've used this next index, can safely increment i
116
                      i++;
117
                    }
118
                }
119
              if (i+1 < argc)
120
                {
121
                  if((argv[i+1][0] != '-') && (strncmp("0x", argv[i+1],2) == 0))
122
                    {
123
                      // Hopefully this is is the start address
124
                      // All addresses should have preceeding hex identifier 0x
125
                      sscanf( argv[i+1], "0x%x", &memdump_start);
126
                      i++;
127
                    }
128
                }
129
              if (i+1 < argc)
130
                {
131
                  if((argv[i+1][0] != '-') && (strncmp("0x", argv[i+1],2) == 0))
132
                    {
133
                      // Hopefully this is is the end address
134
                      // All addresses should have preceeding hex identifier 0x
135
                      sscanf( argv[i+1], "0x%x", &memdump_end);
136
                      i++;
137
                    }
138
                }
139
            }
140 49 julius
        }
141
    }
142
 
143
 
144 52 julius
 
145 51 julius
  if (profiling_enabled)
146
    {
147
      profileFile.open(profileFileName.c_str(), ios::out); // Open profiling log file
148
      if(profileFile.is_open())
149
        {
150
          // If the file was opened OK, then enabled logging and print a message.
151
          profiling_enabled = 1;
152
          cout << "* Execution profiling enabled. Logging to " << profileFileName << endl;
153
        }
154
 
155
      // Setup profiling function
156
      SC_METHOD (callLog);
157
      sensitive << clk.pos();
158
      dont_initialize();
159
      start = clock();
160
    }
161 49 julius
 
162 51 julius
 
163 49 julius
  if(cmdline_name_found==1) // No -log option specified so don't turn on logging
164
    {
165
 
166
      logging_enabled = 0; // Default is logging disabled      
167
      statusFile.open(logfileNameString.c_str(), ios::out ); // open file to write to it
168
 
169
      if(statusFile.is_open())
170
        {
171
          // If we could open the file then turn on logging
172
          logging_enabled = 1;
173
          cout << "* Processor execution logged to file: " << logfileNameString << endl;
174
        }
175
 
176
    }
177 51 julius
  if (logging_enabled)
178
    {
179
      SC_METHOD (displayState);
180
      sensitive << clk.pos();
181
      dont_initialize();
182
      start = clock();
183
    }
184 49 julius
 
185 52 julius
  // Check sizes we were given from memory dump command line options first
186
  if (do_memdump)
187
    {
188
      if ((memdump_start > ORPSOC_SRAM_SIZE) || (memdump_end > ORPSOC_SRAM_SIZE) ||
189
          ((memdump_start > memdump_end) && (memdump_end != 0)))
190
        {
191
          do_memdump = 0;
192
          cout << "* Memory dump addresses range incorrect. Limit of memory is 0x" << hex <<  ORPSOC_SRAM_SIZE << ". Memory dumping disabled." << endl;
193
        }
194
    }
195 49 julius
 
196 52 julius
  if (do_memdump)
197
    {
198
      // Were we given dump addresses? If not, we dump all of the memory
199
      // Size of memory isn't clearly defined in any one place. This could lead to
200
      // big problems when changing size of the RAM in simulation.
201
 
202
      if (memdump_start == 0 && memdump_end == 0)
203
        memdump_end = ORPSOC_SRAM_SIZE;
204
 
205
      if (memdump_start != 0 && memdump_end == 0)
206
        {
207
          // Probably just got the single memorydump param
208
          // Interpet as a length from 0
209
          memdump_end = memdump_start;
210
          memdump_start = 0;
211
        }
212
 
213
      if (memdump_start & 0x3) memdump_start &= ~0x3; // word-align the start address      
214
      if (memdump_end & 0x3) memdump_end = (memdump_end+4) & ~0x3; // word-align the start address
215
 
216
      memdump_start_addr = memdump_start;
217
      memdump_end_addr = memdump_end;
218
    }
219
 
220
 
221
 
222 49 julius
 
223 52 julius
 
224 49 julius
  // checkInstruction monitors the bus for special NOP instructionsl
225
  SC_METHOD (checkInstruction);
226 44 julius
  sensitive << clk.pos();
227
  dont_initialize();
228 49 julius
 
229
 
230
 
231 6 julius
}       // Or1200MonitorSC ()
232
 
233 49 julius
//! Print command line switches for the options of this module
234
void
235
Or1200MonitorSC::printSwitches()
236
{
237 52 julius
  printf(" [-l <file>] [-q] [-p [<file>]] [-m [<file>] [<0xstardaddr> <0xendaddr>]]");
238 49 julius
}
239 6 julius
 
240 49 julius
//! Print usage for the options of this module
241
void
242
Or1200MonitorSC::printUsage()
243
{
244 51 julius
  printf("  -p, --profile\t\tEnable execution profiling output to file (default "DEFAULT_PROF_FILE")\n");
245 49 julius
  printf("  -l, --log\t\tLog processor execution to file\n");
246
  printf("  -q, --quiet\t\tDisable the performance summary at end of simulation\n");
247 52 julius
  printf("  -m, --memdump\t\tDump data from the system's RAM to a file on finish\n\n");
248 49 julius
}
249
 
250 6 julius
//! Method to handle special instrutions
251
 
252
//! These are l.nop instructions with constant values. At present the
253
//! following are implemented:
254
 
255
//! - l.nop 1  Terminate the program
256
//! - l.nop 2  Report the value in R3
257
//! - l.nop 3  Printf the string with the arguments in R3, etc
258
//! - l.nop 4  Print a character
259 49 julius
extern int SIM_RUNNING;
260 6 julius
void
261
Or1200MonitorSC::checkInstruction()
262
{
263
  uint32_t  r3;
264
  double    ts;
265 51 julius
 
266 52 julius
  cycle_count++;
267 6 julius
  // Check the instruction when the freeze signal is low.
268 52 julius
  //if (!accessor->getWbFreeze())
269
  if ((!accessor->getWbFreeze()) && (accessor->getExceptType() == 0))
270 6 julius
    {
271 52 julius
 
272
      // Increment instruction counter
273
      insn_count++;
274
 
275 6 julius
      // Do something if we have l.nop
276
      switch (accessor->getWbInsn())
277
        {
278
        case NOP_EXIT:
279
          r3 = accessor->getGpr (3);
280
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
281
          std::cout << std::fixed << std::setprecision (2) << ts;
282
          std::cout << " ns: Exiting (" << r3 << ")" << std::endl;
283 49 julius
          perfSummary();
284 52 julius
          if (logging_enabled) statusFile.close();
285
          if (profiling_enabled) profileFile.close();
286
          memdump();
287 49 julius
          SIM_RUNNING=0;
288 6 julius
          sc_stop();
289
          break;
290
 
291
        case NOP_REPORT:
292
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
293
          r3 = accessor->getGpr (3);
294
          std::cout << std::fixed << std::setprecision (2) << ts;
295
          std::cout << " ns: report (" << hex << r3 << ")" << std::endl;
296
          break;
297
 
298
        case NOP_PRINTF:
299
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
300
          std::cout << std::fixed << std::setprecision (2) << ts;
301
          std::cout << " ns: printf" << std::endl;
302
          break;
303
 
304
        case NOP_PUTC:
305
          r3 = accessor->getGpr (3);
306
          std::cout << (char)r3 << std::flush;
307
          break;
308
 
309
        default:
310
          break;
311
        }
312
    }
313
 
314
}       // checkInstruction()
315
 
316 44 julius
 
317 51 julius
//! Method to log execution in terms of calls and returns
318
 
319
void
320
Or1200MonitorSC::callLog()
321
{
322
  uint32_t  exinsn, delaypc;
323
  uint32_t o_a; // operand a
324
  uint32_t o_b; // operand b
325
  struct label_entry *tmp;
326
 
327
  // Instructions should be valid when freeze is low and there are no exceptions
328
  //if (!accessor->getExFreeze())
329
  if ((!accessor->getWbFreeze()) && (accessor->getExceptType() == 0))
330
    {
331
      //exinsn = accessor->getExInsn();// & 0x3ffffff;
332
      exinsn = accessor->getWbInsn();
333
      // Check the instruction
334
      switch((exinsn >> 26) & 0x3f) { // Check Opcode - top 6 bits
335
      case 0x1:
336
        /* Instruction: l.jal */
337
        o_a = (exinsn >> 0) & 0x3ffffff;
338
        if(o_a & 0x02000000) o_a |= 0xfe000000;
339
 
340
        //delaypc = accessor->getExPC() + (o_a * 4); // PC we're jumping to
341
        delaypc = accessor->getWbPC() + (o_a * 4); // PC we're jumping to
342
        // Now we have info about where we're jumping to. Output the info, with label if possible
343
        // We print the PC we're jumping from + 8 which is the return address
344
        if ( tmp = memoryload->get_label (delaypc) )
345
          profileFile << "+" << std::setfill('0') << hex << std::setw(8) << cycle_count << " " << hex << std::setw(8) << accessor->getWbPC() + 8 << " " << hex << std::setw(8) << delaypc << " " << tmp->name << endl;
346
        else
347
          profileFile << "+" << std::setfill('0') << hex << std::setw(8) << cycle_count << " " << hex << std::setw(8) << accessor->getWbPC() + 8 << " " << hex << std::setw(8) << delaypc << " @" << hex << std::setw(8) << delaypc << endl;
348
 
349
        break;
350
      case 0x11:
351
        /* Instruction: l.jr */
352
        // Bits 15-11 contain register number
353
        o_b = (exinsn >> 11) & 0x1f;
354
        if (o_b == 9) // l.jr r9 is typical return
355
          {
356
            // Now get the value in this register
357
            delaypc = accessor->getGpr(o_b);
358
            // Output this jump
359
            profileFile << "-" << std::setfill('0') << hex << std::setw(8) << cycle_count << " "  << hex << std::setw(8) << delaypc << endl;
360
          }
361
        break;
362
      case 0x12:
363
        /* Instruction: l.jalr */
364
        o_b = (exinsn >> 11) & 0x1f;
365
        // Now get the value in this register
366
        delaypc = accessor->getGpr(o_b);
367
        // Now we have info about where we're jumping to. Output the info, with label if possible
368
        // We print the PC we're jumping from + 8 which is the return address
369
        if ( tmp = memoryload->get_label (delaypc) )
370
          profileFile << "+" << std::setfill('0') << hex << std::setw(8) << cycle_count << " " << hex << std::setw(8) << accessor->getWbPC() + 8 << " " << hex << std::setw(8) << delaypc << " " << tmp->name << endl;
371
        else
372
          profileFile << "+" << std::setfill('0') << hex << std::setw(8) << cycle_count << " " << hex << std::setw(8) << accessor->getWbPC() + 8 << " " << hex << std::setw(8) << delaypc << " @" << hex << std::setw(8) << delaypc << endl;
373
 
374
        break;
375
 
376
      }
377
    }
378
}       // checkInstruction()
379
 
380
 
381 44 julius
//! Method to output the state of the processor
382
 
383
//! This function will output to a file, if enabled, the status of the processor
384
//! For now, it's just the PPC and instruction.
385 51 julius
#define PRINT_REGS 1
386 44 julius
void
387
Or1200MonitorSC::displayState()
388
{
389
  uint32_t  wbinsn;
390
 
391
  // Calculate how many instructions we've actually calculated by ignoring cycles where we're frozen, delay slots and flushpipe cycles
392
  if ((!accessor->getWbFreeze()) && !(accessor->getExceptFlushpipe() && accessor->getExDslot()))
393
 
394
  if (logging_enabled == 0)
395
        return; // If we didn't inialise a file, then just return.
396
 
397
  // Output the state if we're not frozen and not flushing during a delay slot
398
  if ((!accessor->getWbFreeze()) && !(accessor->getExceptFlushpipe() && accessor->getExDslot()))
399
    {
400 51 julius
      // Print PC, instruction
401
      statusFile << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << insn_count << "): " << std::setfill('0') << hex << std::setw(8) << accessor->getWbPC() << ": " << hex << accessor->getWbInsn() <<  endl;
402 49 julius
#if PRINT_REGS
403 44 julius
        // Print general purpose register contents
404
        for (int i=0; i<32; i++)
405 49 julius
          {
406 44 julius
                if ((i%4 == 0)&&(i>0)) statusFile << endl;
407
                statusFile << std::setfill('0');
408
                statusFile << "GPR" << dec << std::setw(2) << i << ": " <<  hex << std::setw(8) << (uint32_t) accessor->getGpr(i) << "  ";
409
        }
410
        statusFile << endl;
411
 
412
        statusFile << "SR   : " <<  hex << std::setw(8) << (uint32_t) accessor->getSprSr() << "  ";
413
        statusFile << "EPCR0: " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEpcr() << "  ";
414
        statusFile << "EEAR0: " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEear() << "  ";
415
        statusFile << "ESR0 : " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEsr() << endl;
416 49 julius
#endif
417 44 julius
 
418
    }
419
 
420
  return;
421
 
422
}       // displayState()
423
 
424
//! Function to calculate the number of instructions performed and the time taken
425
void
426
Or1200MonitorSC::perfSummary()
427
{
428 49 julius
  if (exit_perf_summary_enabled)
429
    {
430
      double ts;
431
      ts = sc_time_stamp().to_seconds() * 1000000000.0;
432
      int cycles = ts / (BENCH_CLK_HALFPERIOD*2); // Number of clock cycles we had
433
 
434
      clock_t finish = clock();
435
      double elapsed_time = (double(finish)-double(start))/CLOCKS_PER_SEC;
436
      // It took elapsed_time seconds to do insn_count instructions. Divide insn_count by the time to get instructions/second.
437
      double ips = (insn_count/elapsed_time);
438
      double mips = (insn_count/elapsed_time)/1000000;
439
      int hertz = (int) ((cycles/elapsed_time)/1000);
440
      std::cout << "* Or1200Monitor: simulated " << sc_time_stamp() << ",time elapsed: " << elapsed_time << " seconds" << endl;
441
      std::cout << "* Or1200Monitor: simulated " << dec << cycles << " clock cycles, executed at approx " << hertz << "kHz" << endl;
442
      std::cout << "* Or1200Monitor: simulated " << insn_count << " instructions, insn/sec. = " << ips << ", mips = " << mips << endl;
443
    }
444
  return;
445
}       // perfSummary
446 44 julius
 
447 52 julius
 
448
//! Dump contents of simulation's RAM to file
449
void
450
Or1200MonitorSC::memdump()
451
{
452
  if (!do_memdump) return;
453
  uint32_t current_word;
454
  int size_words = (memdump_end_addr/4) - (memdump_start_addr/4);
455
  if (!(size_words > 0)) return;
456
 
457
  // First try opening the file
458
  memdumpFile.open(memdumpFileName.c_str(), ios::binary); // Open memorydump file
459
  if(memdumpFile.is_open())
460
    {
461
      // If we could open the file then turn on logging
462
      cout << "* Dumping system RAM from  0x" << hex << memdump_start_addr << "-0x" << hex << memdump_end_addr << " to file " << memdumpFileName << endl;
463
 
464
      // Convert memdump_start_addr to word address
465
      memdump_start_addr = memdump_start_addr / 4;
466
      while (size_words)
467
        {
468
          // Read the data from the simulation memory
469
          current_word = accessor->get_mem(memdump_start_addr);
470
          //cout << hex << current_word << " ";
471
          /*
472
          cout << hex << ((current_word >> 24 ) & 0xff) << " ";
473
          cout << hex << ((current_word >> 16) & 0xff) << " ";
474
          cout << hex << ((current_word >> 8 ) & 0xff) << " " ;
475
          cout << hex << ((current_word >> 0 ) & 0xff) << " ";
476
          */
477
          // Change from whatever endian the host is (most
478
          // cases little) to big endian
479
          current_word = htonl(current_word);
480
          memdumpFile.write((char*) &current_word, 4);
481
          memdump_start_addr++; size_words--;
482
        }
483
 
484
      // Ideally we've now finished piping out the data
485
      // not 100% about the endianess of this.
486
    }
487
  memdumpFile.close();
488
 
489
}

powered by: WebSVN 2.1.0

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