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

Subversion Repositories openrisc

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

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 57 julius
// $Id$
29 6 julius
 
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 53 julius
                   (strcmp(argv[i], "--memdump")==0))
104 52 julius
            {
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 57 julius
 
145
  // checkInstruction monitors the bus for special NOP instructionsl
146
  SC_METHOD (checkInstruction);
147
  sensitive << clk.pos();
148
  dont_initialize();
149
 
150 52 julius
 
151 51 julius
  if (profiling_enabled)
152
    {
153
      profileFile.open(profileFileName.c_str(), ios::out); // Open profiling log file
154
      if(profileFile.is_open())
155
        {
156
          // If the file was opened OK, then enabled logging and print a message.
157
          profiling_enabled = 1;
158
          cout << "* Execution profiling enabled. Logging to " << profileFileName << endl;
159
        }
160
 
161
      // Setup profiling function
162
      SC_METHOD (callLog);
163
      sensitive << clk.pos();
164
      dont_initialize();
165
      start = clock();
166
    }
167 49 julius
 
168 51 julius
 
169 49 julius
  if(cmdline_name_found==1) // No -log option specified so don't turn on logging
170
    {
171
 
172
      logging_enabled = 0; // Default is logging disabled      
173
      statusFile.open(logfileNameString.c_str(), ios::out ); // open file to write to it
174
 
175
      if(statusFile.is_open())
176
        {
177
          // If we could open the file then turn on logging
178
          logging_enabled = 1;
179
          cout << "* Processor execution logged to file: " << logfileNameString << endl;
180
        }
181
 
182
    }
183 51 julius
  if (logging_enabled)
184
    {
185
      SC_METHOD (displayState);
186
      sensitive << clk.pos();
187
      dont_initialize();
188
      start = clock();
189
    }
190 49 julius
 
191 52 julius
  // Check sizes we were given from memory dump command line options first
192
  if (do_memdump)
193
    {
194
      if ((memdump_start > ORPSOC_SRAM_SIZE) || (memdump_end > ORPSOC_SRAM_SIZE) ||
195
          ((memdump_start > memdump_end) && (memdump_end != 0)))
196
        {
197
          do_memdump = 0;
198
          cout << "* Memory dump addresses range incorrect. Limit of memory is 0x" << hex <<  ORPSOC_SRAM_SIZE << ". Memory dumping disabled." << endl;
199
        }
200
    }
201 49 julius
 
202 52 julius
  if (do_memdump)
203
    {
204
      // Were we given dump addresses? If not, we dump all of the memory
205
      // Size of memory isn't clearly defined in any one place. This could lead to
206
      // big problems when changing size of the RAM in simulation.
207
 
208
      if (memdump_start == 0 && memdump_end == 0)
209
        memdump_end = ORPSOC_SRAM_SIZE;
210
 
211
      if (memdump_start != 0 && memdump_end == 0)
212
        {
213
          // Probably just got the single memorydump param
214
          // Interpet as a length from 0
215
          memdump_end = memdump_start;
216
          memdump_start = 0;
217
        }
218
 
219
      if (memdump_start & 0x3) memdump_start &= ~0x3; // word-align the start address      
220
      if (memdump_end & 0x3) memdump_end = (memdump_end+4) & ~0x3; // word-align the start address
221
 
222
      memdump_start_addr = memdump_start;
223
      memdump_end_addr = memdump_end;
224 57 julius
    }
225 49 julius
 
226 6 julius
}       // Or1200MonitorSC ()
227
 
228 49 julius
//! Print command line switches for the options of this module
229
void
230
Or1200MonitorSC::printSwitches()
231
{
232 52 julius
  printf(" [-l <file>] [-q] [-p [<file>]] [-m [<file>] [<0xstardaddr> <0xendaddr>]]");
233 49 julius
}
234 6 julius
 
235 49 julius
//! Print usage for the options of this module
236
void
237
Or1200MonitorSC::printUsage()
238
{
239 51 julius
  printf("  -p, --profile\t\tEnable execution profiling output to file (default "DEFAULT_PROF_FILE")\n");
240 49 julius
  printf("  -l, --log\t\tLog processor execution to file\n");
241
  printf("  -q, --quiet\t\tDisable the performance summary at end of simulation\n");
242 52 julius
  printf("  -m, --memdump\t\tDump data from the system's RAM to a file on finish\n\n");
243 49 julius
}
244
 
245 6 julius
//! Method to handle special instrutions
246
 
247
//! These are l.nop instructions with constant values. At present the
248
//! following are implemented:
249
 
250
//! - l.nop 1  Terminate the program
251
//! - l.nop 2  Report the value in R3
252
//! - l.nop 3  Printf the string with the arguments in R3, etc
253
//! - l.nop 4  Print a character
254 57 julius
 
255
//#define OR1200_OR32_NOP_BITS_31_TO_26               6'b000101
256
#define OR1200_OR32_NOP               0x14000000
257
 
258 49 julius
extern int SIM_RUNNING;
259 6 julius
void
260
Or1200MonitorSC::checkInstruction()
261
{
262
  uint32_t  r3;
263
  double    ts;
264 51 julius
 
265 52 julius
  cycle_count++;
266 57 julius
 
267
  /* Check if this counts as an "executed" instruction */
268
  if (!accessor->getWbFreeze())
269
    if ((((accessor->getWbInsn() & 0xfc000000) != (uint32_t) OR1200_OR32_NOP) || !(accessor->getWbInsn() & (1<<16))) && !(accessor->getExceptFlushpipe() && accessor->getExDslot()))
270
      insn_count++;
271
    else
272
      // Exception version
273
      if (accessor->getExceptFlushpipe())
274
        insn_count++;
275
 
276 6 julius
  // Check the instruction when the freeze signal is low.
277 52 julius
  //if (!accessor->getWbFreeze())
278
  if ((!accessor->getWbFreeze()) && (accessor->getExceptType() == 0))
279 6 julius
    {
280
      // Do something if we have l.nop
281
      switch (accessor->getWbInsn())
282
        {
283
        case NOP_EXIT:
284
          r3 = accessor->getGpr (3);
285
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
286
          std::cout << std::fixed << std::setprecision (2) << ts;
287
          std::cout << " ns: Exiting (" << r3 << ")" << std::endl;
288 49 julius
          perfSummary();
289 52 julius
          if (logging_enabled) statusFile.close();
290
          if (profiling_enabled) profileFile.close();
291
          memdump();
292 49 julius
          SIM_RUNNING=0;
293 6 julius
          sc_stop();
294
          break;
295
 
296
        case NOP_REPORT:
297
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
298
          r3 = accessor->getGpr (3);
299
          std::cout << std::fixed << std::setprecision (2) << ts;
300
          std::cout << " ns: report (" << hex << r3 << ")" << std::endl;
301
          break;
302
 
303
        case NOP_PRINTF:
304
          ts = sc_time_stamp().to_seconds() * 1000000000.0;
305
          std::cout << std::fixed << std::setprecision (2) << ts;
306
          std::cout << " ns: printf" << std::endl;
307
          break;
308
 
309
        case NOP_PUTC:
310
          r3 = accessor->getGpr (3);
311
          std::cout << (char)r3 << std::flush;
312
          break;
313
 
314
        default:
315
          break;
316
        }
317
    }
318
 
319
}       // checkInstruction()
320
 
321 44 julius
 
322 51 julius
//! Method to log execution in terms of calls and returns
323
 
324
void
325
Or1200MonitorSC::callLog()
326
{
327
  uint32_t  exinsn, delaypc;
328
  uint32_t o_a; // operand a
329
  uint32_t o_b; // operand b
330
  struct label_entry *tmp;
331
 
332
  // Instructions should be valid when freeze is low and there are no exceptions
333
  //if (!accessor->getExFreeze())
334
  if ((!accessor->getWbFreeze()) && (accessor->getExceptType() == 0))
335
    {
336
      //exinsn = accessor->getExInsn();// & 0x3ffffff;
337
      exinsn = accessor->getWbInsn();
338
      // Check the instruction
339
      switch((exinsn >> 26) & 0x3f) { // Check Opcode - top 6 bits
340
      case 0x1:
341
        /* Instruction: l.jal */
342
        o_a = (exinsn >> 0) & 0x3ffffff;
343
        if(o_a & 0x02000000) o_a |= 0xfe000000;
344
 
345
        //delaypc = accessor->getExPC() + (o_a * 4); // PC we're jumping to
346
        delaypc = accessor->getWbPC() + (o_a * 4); // PC we're jumping to
347
        // Now we have info about where we're jumping to. Output the info, with label if possible
348
        // We print the PC we're jumping from + 8 which is the return address
349
        if ( tmp = memoryload->get_label (delaypc) )
350
          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;
351
        else
352
          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;
353
 
354
        break;
355
      case 0x11:
356
        /* Instruction: l.jr */
357
        // Bits 15-11 contain register number
358
        o_b = (exinsn >> 11) & 0x1f;
359
        if (o_b == 9) // l.jr r9 is typical return
360
          {
361
            // Now get the value in this register
362
            delaypc = accessor->getGpr(o_b);
363
            // Output this jump
364
            profileFile << "-" << std::setfill('0') << hex << std::setw(8) << cycle_count << " "  << hex << std::setw(8) << delaypc << endl;
365
          }
366
        break;
367
      case 0x12:
368
        /* Instruction: l.jalr */
369
        o_b = (exinsn >> 11) & 0x1f;
370
        // Now get the value in this register
371
        delaypc = accessor->getGpr(o_b);
372
        // Now we have info about where we're jumping to. Output the info, with label if possible
373
        // We print the PC we're jumping from + 8 which is the return address
374
        if ( tmp = memoryload->get_label (delaypc) )
375
          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;
376
        else
377
          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;
378
 
379
        break;
380
 
381
      }
382
    }
383 57 julius
}       // callLog()
384 51 julius
 
385
 
386 44 julius
//! Method to output the state of the processor
387
 
388
//! This function will output to a file, if enabled, the status of the processor
389 57 julius
//! This copies what the verilog testbench module, or1200_monitor does in it its
390
//! process which calls the display_arch_state tasks. This is designed to be 
391
//! identical to that process, so the output is identical
392 51 julius
#define PRINT_REGS 1
393 44 julius
void
394
Or1200MonitorSC::displayState()
395
{
396 57 julius
  bool printregs = false;
397 44 julius
  // Output the state if we're not frozen and not flushing during a delay slot
398 57 julius
  if (!accessor->getWbFreeze())
399 44 julius
    {
400 57 julius
      if ((((accessor->getWbInsn() & 0xfc000000) != (uint32_t) OR1200_OR32_NOP) || !(accessor->getWbInsn() & (1<<16))) && !(accessor->getExceptFlushpipe() && accessor->getExDslot()))
401
        {
402
          // Print PC, instruction
403
          statusFile << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << insn_count << "): " << std::setfill('0') << hex << std::setw(8) << accessor->getWbPC() << ":  " << hex << std::setw(8) << accessor->getWbInsn() <<  endl;
404 49 julius
#if PRINT_REGS
405 57 julius
          printregs = true;
406
#endif
407 44 julius
        }
408 57 julius
      else
409
        {
410
          // Exception version
411
          if (accessor->getExceptFlushpipe())
412
            {
413
              // Print PC, instruction, indicate it caused an exception
414
              statusFile << "\nEXECUTED("<< std::setfill(' ') << std::setw(11) << dec << insn_count << "): " << std::setfill('0') << hex << std::setw(8) << accessor->getExPC() << ":  " << hex << std::setw(8) << accessor->getExInsn() << "  (exception)" << endl;
415
#if PRINT_REGS
416
              printregs = true;
417 49 julius
#endif
418 44 julius
 
419 57 julius
            }
420
        }
421
 
422
      if (printregs)
423
        {
424
          // Print general purpose register contents
425
          for (int i=0; i<32; i++)
426
            {
427
              if ((i%4 == 0)&&(i>0)) statusFile << endl;
428
              statusFile << std::setfill('0');
429
              statusFile << "GPR" << dec << std::setw(2) << i << ": " <<  hex << std::setw(8) << (uint32_t) accessor->getGpr(i) << "  ";
430
            }
431
          statusFile << endl;
432
 
433
          statusFile << "SR   : " <<  hex << std::setw(8) << (uint32_t) accessor->getSprSr() << "  ";
434
          statusFile << "EPCR0: " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEpcr() << "  ";
435
          statusFile << "EEAR0: " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEear() << "  ";
436
          statusFile << "ESR0 : " <<  hex << std::setw(8) << (uint32_t) accessor->getSprEsr() << endl;
437
 
438
        }
439 44 julius
    }
440 57 julius
 
441 44 julius
  return;
442 57 julius
 
443 44 julius
}       // displayState()
444
 
445
//! Function to calculate the number of instructions performed and the time taken
446
void
447
Or1200MonitorSC::perfSummary()
448
{
449 49 julius
  if (exit_perf_summary_enabled)
450
    {
451
      double ts;
452
      ts = sc_time_stamp().to_seconds() * 1000000000.0;
453
      int cycles = ts / (BENCH_CLK_HALFPERIOD*2); // Number of clock cycles we had
454
 
455
      clock_t finish = clock();
456
      double elapsed_time = (double(finish)-double(start))/CLOCKS_PER_SEC;
457
      // It took elapsed_time seconds to do insn_count instructions. Divide insn_count by the time to get instructions/second.
458
      double ips = (insn_count/elapsed_time);
459
      double mips = (insn_count/elapsed_time)/1000000;
460
      int hertz = (int) ((cycles/elapsed_time)/1000);
461 57 julius
      std::cout << "* Or1200Monitor: simulated " << sc_time_stamp() << ", time elapsed: " << elapsed_time << " seconds" << endl;
462 49 julius
      std::cout << "* Or1200Monitor: simulated " << dec << cycles << " clock cycles, executed at approx " << hertz << "kHz" << endl;
463
      std::cout << "* Or1200Monitor: simulated " << insn_count << " instructions, insn/sec. = " << ips << ", mips = " << mips << endl;
464
    }
465
  return;
466
}       // perfSummary
467 44 julius
 
468 52 julius
 
469
//! Dump contents of simulation's RAM to file
470
void
471
Or1200MonitorSC::memdump()
472
{
473
  if (!do_memdump) return;
474
  uint32_t current_word;
475
  int size_words = (memdump_end_addr/4) - (memdump_start_addr/4);
476
  if (!(size_words > 0)) return;
477
 
478
  // First try opening the file
479
  memdumpFile.open(memdumpFileName.c_str(), ios::binary); // Open memorydump file
480
  if(memdumpFile.is_open())
481
    {
482
      // If we could open the file then turn on logging
483
      cout << "* Dumping system RAM from  0x" << hex << memdump_start_addr << "-0x" << hex << memdump_end_addr << " to file " << memdumpFileName << endl;
484
 
485
      // Convert memdump_start_addr to word address
486
      memdump_start_addr = memdump_start_addr / 4;
487
      while (size_words)
488
        {
489
          // Read the data from the simulation memory
490
          current_word = accessor->get_mem(memdump_start_addr);
491
          //cout << hex << current_word << " ";
492
          /*
493
          cout << hex << ((current_word >> 24 ) & 0xff) << " ";
494
          cout << hex << ((current_word >> 16) & 0xff) << " ";
495
          cout << hex << ((current_word >> 8 ) & 0xff) << " " ;
496
          cout << hex << ((current_word >> 0 ) & 0xff) << " ";
497
          */
498
          // Change from whatever endian the host is (most
499
          // cases little) to big endian
500
          current_word = htonl(current_word);
501
          memdumpFile.write((char*) &current_word, 4);
502
          memdump_start_addr++; size_words--;
503
        }
504
 
505
      // Ideally we've now finished piping out the data
506
      // not 100% about the endianess of this.
507
    }
508
  memdumpFile.close();
509
 
510
}

powered by: WebSVN 2.1.0

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