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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 63 julius
/////////////////////////////////////////////////////////////////////
2 6 julius
////                                                              ////
3
////  ORPSoC SystemC Testbench                                    ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  ORPSoC Testbench file                                       ////
7
////                                                              ////
8
////  To Do:                                                      ////
9
////                                                              ////
10
////                                                              ////
11
////  Author(s):                                                  ////
12
////      - Jeremy Bennett jeremy.bennett@embecosm.com            ////
13
////      - Julius Baxter jb@orsoc.se                             ////
14
////                                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
 
43
#include "OrpsocMain.h"
44
 
45 64 julius
#include "JtagSC_includes.h"
46 63 julius
 
47 6 julius
#include "Vorpsoc_top.h"
48
#include "OrpsocAccess.h"
49 51 julius
#include "MemoryLoad.h"
50 49 julius
 
51 70 julius
#include <verilated_vcd_c.h>
52 49 julius
 
53 6 julius
#include "ResetSC.h"
54
#include "Or1200MonitorSC.h"
55 63 julius
#include "GdbServerSC.h"
56 6 julius
#include "UartSC.h"
57
 
58 49 julius
int SIM_RUNNING;
59 6 julius
int sc_main (int   argc,
60
             char *argv[] )
61
{
62 49 julius
  sc_set_time_resolution( 1, TIMESCALE_UNIT);
63 6 julius
  // CPU clock (also used as JTAG TCK) and reset (both active high and low)
64
  sc_time  clkPeriod (BENCH_CLK_HALFPERIOD * 2.0, TIMESCALE_UNIT);
65 63 julius
  sc_time   jtagPeriod (JTAG_CLK_HALFPERIOD * 2.0, TIMESCALE_UNIT);
66 6 julius
 
67
  sc_clock             clk ("clk", clkPeriod);
68 63 julius
  sc_clock  jtag_tck ("jtag-clk", jtagPeriod, 0.5, SC_ZERO_TIME, false);
69
 
70 6 julius
  sc_signal<bool>      rst;
71
  sc_signal<bool>      rstn;
72
 
73
  sc_signal<bool>      jtag_tdi;                // JTAG interface
74
  sc_signal<bool>      jtag_tdo;
75
  sc_signal<bool>      jtag_tms;
76
  sc_signal<bool>      jtag_trst;
77
 
78
  sc_signal<bool>      uart_rx;         // External UART
79
  sc_signal<bool>      uart_tx;
80
 
81 49 julius
  SIM_RUNNING = 0;
82 6 julius
 
83 49 julius
  // Setup the name of the VCD dump file
84 63 julius
  bool VCD_enabled = false;
85 49 julius
  string dumpNameDefault("vlt-dump.vcd");
86
  string testNameString;
87
  string vcdDumpFile;
88
  // VCD dump controling vars
89 63 julius
  bool dump_start_delay_set = false, dump_stop_set = false;
90
  bool dumping_now = false;
91 49 julius
  int dump_depth = 99; // Default dump depth
92
  sc_time dump_start,dump_stop, finish_time;
93 63 julius
  bool finish_time_set = false; // By default we will let the simulation finish naturally
94 70 julius
  VerilatedVcdC *verilatorVCDFile;
95 49 julius
 
96 63 julius
  /*int*/double time_val;
97
  bool vcd_file_name_given = false;
98 49 julius
 
99 63 julius
  bool rsp_server_enabled = false;
100
  int rsp_server_port = DEFAULT_RSP_PORT;
101
 
102 51 julius
  // Executable app load variables
103
  int do_program_file_load = 0; // Default: we don't require a file, we use the VMEM
104
  char* program_file; // Old char* style for program name
105
 
106 6 julius
  // Verilator accessor
107
  OrpsocAccess    *accessor;
108
 
109
  // Modules
110
  Vorpsoc_top *orpsoc;          // Verilated ORPSoC
111
 
112 51 julius
  MemoryLoad *memoryload;       // Memory loader
113
 
114 6 julius
  ResetSC          *reset;              // Generate a RESET signal
115
  Or1200MonitorSC  *monitor;            // Handle l.nop x instructions
116 63 julius
  JtagSC           *jtag;               // Generate JTAG signals
117
  GdbServerSC      *gdbServer;          // Map RSP requests to debug unit
118 6 julius
  UartSC          *uart;                // Handle UART signals
119
 
120
  // Instantiate the Verilator model, VCD trace handler and accessor
121
  orpsoc     = new Vorpsoc_top ("orpsoc");
122 51 julius
 
123 6 julius
  accessor   = new OrpsocAccess (orpsoc);
124
 
125 51 julius
  memoryload = new MemoryLoad (accessor);
126
 
127 63 julius
  monitor    = new Or1200MonitorSC ("monitor", accessor, memoryload,
128
                                    argc, argv);
129
 
130 6 julius
  // Instantiate the SystemC modules
131
  reset         = new ResetSC ("reset", BENCH_RESET_TIME);
132 63 julius
 
133
  jtag          = new JtagSC ("jtag");
134
 
135 6 julius
  uart          = new UartSC("uart"); // TODO: Probalby some sort of param
136
 
137 49 julius
  // Parse command line options
138
  // Default is for VCD generation OFF, only turned on if specified on command line
139
 
140 51 julius
  // Search through the command line parameters for options  
141 49 julius
  if (argc > 1)
142
    {
143
      for(int i=1; i<argc; i++)
144
        {
145 63 julius
          if ( (strcmp(argv[i], "-e")==0) ||
146
               (strcmp(argv[i], "--endtime")==0) )
147 49 julius
            {
148 63 julius
              time_val = strtod(argv[i+1], NULL);
149 49 julius
              sc_time opt_end_time(time_val,TIMESCALE_UNIT);
150
              finish_time = opt_end_time;
151 63 julius
              finish_time_set = true;
152 49 julius
            }
153 51 julius
          else if ( (strcmp(argv[i], "-f")==0) ||
154
                    (strcmp(argv[i], "--program")==0) )
155
            {
156
              do_program_file_load = 1; // Enable program loading - will be done after sim init
157
              program_file = argv[i+1]; // Old char* style for program name
158
            }
159 63 julius
          else if ((strcmp(argv[i], "-d")==0) ||
160
                   (strcmp(argv[i], "--vcdfile")==0) ||
161
                   (strcmp(argv[i], "-v")==0) ||
162
                   (strcmp(argv[i], "--vcdon")==0)
163
                   )
164
            {
165
              VCD_enabled = true;
166
              dumping_now = true;
167
              vcdDumpFile = dumpNameDefault;
168
              if (i+1 < argc)
169
                if(argv[i+1][0] != '-')
170
                  {
171
                    testNameString = argv[i+1];
172
                    vcdDumpFile = testNameString;
173
                    i++;
174
                  }
175
            }
176
          else if ( (strcmp(argv[i], "-s")==0) ||
177 49 julius
                    (strcmp(argv[i], "--vcdstart")==0) )
178
            {
179 63 julius
              VCD_enabled = true;
180
              time_val = strtod(argv[i+1], NULL);
181 49 julius
              sc_time dump_start_time(time_val,TIMESCALE_UNIT);
182
              dump_start = dump_start_time;
183 63 julius
              dump_start_delay_set = true;
184
              dumping_now = false;
185 49 julius
            }
186
          else if ( (strcmp(argv[i], "-t")==0) ||
187
                    (strcmp(argv[i], "--vcdstop")==0) )
188
            {
189 63 julius
              VCD_enabled = true;
190
              time_val = strtod(argv[i+1],NULL);
191 49 julius
              sc_time dump_stop_time(time_val,TIMESCALE_UNIT);
192
              dump_stop = dump_stop_time;
193 63 julius
              dump_stop_set = true;
194 49 julius
            }
195 63 julius
          else if ( (strcmp(argv[i], "-r")==0) ||
196
                    (strcmp(argv[i], "--rsp")==0) )
197
            {
198
              rsp_server_enabled = true;
199
              if (i+1 < argc) if(argv[i+1][0] != '-')
200
                                {
201
                                  rsp_server_port = atoi(argv[i+1]);
202
                                  i++;
203
                                }
204
            }
205
          /*
206
             Depth setting of VCD doesn't appear to work, I think it's only
207
             configurable during at compile time .
208
          */
209 49 julius
          /*      else if ( (strcmp(argv[i], "-p")==0) ||
210 51 julius
                  (strcmp(argv[i], "--vcddepth")==0) )
211
                  {
212
                  dump_depth = atoi(argv[i+1]);
213
                  }*/
214 49 julius
          else if ( (strcmp(argv[i], "-h")==0) ||
215
                    (strcmp(argv[i], "--help")==0) )
216
            {
217 63 julius
              printf("Usage: %s [options]\n",argv[0]);
218
              printf("\n  ORPSoCv2 cycle accurate model\n");
219
              printf("  For details visit http://opencores.org/openrisc,orpsocv2\n");
220
              printf("\n");
221
              printf("Options:\n");
222 49 julius
              printf("  -h, --help\t\tPrint this help message\n");
223 63 julius
              printf("\nSimulation control:\n");
224
              printf("  -f, --program <file> \tLoad program from OR32 ELF <file>\n");
225
              printf("  -e, --endtime <val> \tStop the sim at <val> ns\n");
226
              printf("\nVCD generation:\n");
227 49 julius
              printf("  -v, --vcdon\t\tEnable VCD generation\n");
228 63 julius
              printf("  -d, --vcdfile <file>\tEnable and save VCD to <file>\n");
229 49 julius
 
230 63 julius
              printf("  -s, --vcdstart <val>\tEnable and delay VCD generation until <val> ns\n");
231
              printf("  -t, --vcdstop <val> \tEnable and terminate VCD generation at <val> ns\n");
232
              printf("\nRemote debugging:\n");
233
              printf("  -r, --rsp [<port>]\tEnable RSP debugging server, opt. specify <port>\n");
234 49 julius
              monitor->printUsage();
235
              printf("\n");
236
              return 0;
237
            }
238
 
239
        }
240
    }
241 63 julius
 
242 49 julius
  // Determine if we're going to setup a VCD dump:
243 63 julius
  // Pretty much setting any related option will enable VCD dumping.
244
  if (VCD_enabled)
245 49 julius
    {
246
 
247
      cout << "* Enabling VCD trace";
248
 
249 63 julius
      if (dump_start_delay_set)
250 49 julius
        cout << ", on at time " << dump_start.to_string();
251
      if (dump_stop_set)
252 63 julius
        cout << ", off at time " << dump_stop.to_string();
253 49 julius
      cout << endl;
254
    }
255 63 julius
 
256
  if (rsp_server_enabled)
257
    gdbServer     = new GdbServerSC ("gdb-server", FLASH_START, FLASH_END,
258
                                       rsp_server_port, jtag->tapActionQueue);
259
  else
260
      gdbServer = NULL;
261
 
262 6 julius
  // Connect up ORPSoC
263
  orpsoc->clk_pad_i (clk);
264 362 julius
  orpsoc->rst_n_pad_i (rstn);
265 6 julius
 
266 362 julius
  orpsoc->tck_pad_i  (jtag_tck);                // JTAG interface
267
  orpsoc->tdi_pad_i  (jtag_tdi);
268
  orpsoc->tms_pad_i  (jtag_tms);
269
  orpsoc->tdo_pad_o  (jtag_tdo);
270 6 julius
 
271
  orpsoc->uart0_srx_pad_i (uart_rx);            // External UART
272
  orpsoc->uart0_stx_pad_o (uart_tx);
273
 
274
  // Connect up the SystemC  modules
275
  reset->clk (clk);                     // Reset
276
  reset->rst (rst);
277
  reset->rstn (rstn);
278
 
279
  monitor->clk (clk);                   // Monitor
280
 
281 63 julius
  jtag->sysReset (rst);                 // JTAG
282
  jtag->tck (jtag_tck);
283
  jtag->tdi (jtag_tdi);
284
  jtag->tdo (jtag_tdo);
285
  jtag->tms (jtag_tms);
286
  jtag->trst (jtag_trst);
287
 
288 6 julius
  uart->clk (clk); // Uart
289
  uart->uartrx (uart_rx); // orpsoc's receive line
290
  uart->uarttx (uart_tx); // orpsoc's transmit line
291
 
292 63 julius
  // Tie off signals
293
  jtag_tdi      = 1;                    // Tie off the JTAG inputs
294
  jtag_tms      = 1;
295
 
296 49 julius
  if (VCD_enabled)
297
    {
298
      Verilated::traceEverOn (true);
299
 
300
      printf("* VCD dumpfile: %s\n", vcdDumpFile.c_str());
301
 
302
      // Establish a new trace with its correct time resolution, and trace to
303
      // great depth.
304 70 julius
      verilatorVCDFile = new VerilatedVcdC ();
305
      //verilatorVCDFile->verilated()->set_time_resolution (sc_get_time_resolution());
306 49 julius
      //setSpTimeResolution (sc_get_time_resolution ());
307 70 julius
      //traceTarget->trace (verilatorVCDFile, 99);
308
      orpsoc->trace (verilatorVCDFile, dump_depth);
309 49 julius
 
310
      if (dumping_now)
311
        {
312 70 julius
          verilatorVCDFile->open (vcdDumpFile.c_str());
313 49 julius
        }
314
    }
315 362 julius
 
316 52 julius
  //printf("* Beginning test\n");
317 6 julius
 
318
  // Init the UART function
319 354 julius
  uart->initUart(50000000, 115200);
320 6 julius
 
321 51 julius
  if (do_program_file_load) // Did the user specify a file to load?
322
    {
323
      cout << "* Loading program from " << program_file << endl;
324
      if (memoryload->loadcode(program_file,0,0) < 0)
325
        {
326
          cout << "* Error: executable file " << program_file << " not loaded" << endl;
327
        }
328
    }
329
  else // Load SRAM from VMEM file
330
    {
331
      accessor->do_ram_readmemh();
332
    }
333 44 julius
 
334 51 julius
  SIM_RUNNING = 1;
335
 
336 49 julius
  // First check how we should run the sim.
337
  if (VCD_enabled || finish_time_set)
338
    { // We'll run sim with step
339
 
340
      if (!VCD_enabled && finish_time_set)
341
        {
342
          // We just run the sim until the set finish time
343
          sc_start((double)(finish_time.to_double()), TIMESCALE_UNIT);
344
          SIM_RUNNING=0;
345
          sc_stop();
346
          // Print performance summary
347 52 julius
          monitor->perfSummary();
348
          // Do memdump if enabled
349
          monitor->memdump();
350 49 julius
        }
351
      else
352
        {
353 63 julius
          if (dump_start_delay_set)
354 49 julius
            {
355
              // Run the sim until we want to dump
356
              sc_start((double)(dump_start.to_double()),TIMESCALE_UNIT);
357
              // Open the trace file
358 70 julius
              verilatorVCDFile->open (vcdDumpFile.c_str());
359 49 julius
              dumping_now = 1;
360
            }
361 6 julius
 
362 49 julius
          if (dumping_now)
363
            {
364
              // Step the sim and generate the trace
365
                  // Execute until we stop
366
              while(!Verilated::gotFinish())
367
                {
368
                  if (SIM_RUNNING) // Changed by Or1200MonitorSC when finish NOP
369
                    sc_start (1,TIMESCALE_UNIT); // Step the sim
370
                  else
371
                    {
372 70 julius
                      verilatorVCDFile->close();
373 49 julius
                      break;
374
                    }
375
 
376 70 julius
                  verilatorVCDFile->dump (sc_time_stamp().to_double());
377 49 julius
 
378
                  if (dump_stop_set)
379
                    {
380
                      if (sc_time_stamp() >=  dump_stop)
381
                        {
382
                          // Close dump file
383 70 julius
                          verilatorVCDFile->close();
384 49 julius
                          // Now continue on again until the end
385
                          if (!finish_time_set)
386
                            sc_start();
387
                          else
388
                            {
389
                              // Determine how long we should run for
390
                              sc_time sim_time_remaining =
391
                                finish_time - sc_time_stamp();
392
                              sc_start((double)(sim_time_remaining.to_double()),
393
                                       TIMESCALE_UNIT);
394
                              // Officially stop the sim
395
                              sc_stop();
396
                              // Print performance summary
397
                              monitor->perfSummary();
398 52 julius
                              // Do memdump if enabled
399
                              monitor->memdump();
400 49 julius
                            }
401
                          break;
402
                        }
403
                    }
404
                  if (finish_time_set)
405
                    {
406
                      if (sc_time_stamp() >=  finish_time)
407
                        {
408
                          // Officially stop the sim
409
                          sc_stop();
410
                          // Close dump file
411 70 julius
                          verilatorVCDFile->close();
412 52 julius
                          // Do memdump if enabled
413
                          monitor->memdump();
414 49 julius
                          // Print performance summary
415
                          monitor->perfSummary();
416
                          break;
417
                        }
418
                    }
419
                }
420
            }
421
        }
422
    }
423
  else
424
    {
425
      // Simple run case
426 52 julius
      // Ideally a "l.nop 1" will terminate the simulation gracefully
427 64 julius
      // Need to step at clock period / 4, otherwise model appears to skip the monitor and logging functions sometimes (?!?)
428
      while (SIM_RUNNING)
429
        sc_start(BENCH_CLK_HALFPERIOD / 2, TIMESCALE_UNIT);
430
      //sc_start();
431 49 julius
    }
432
 
433
 
434 6 julius
  // Free memory
435 63 julius
  if (rsp_server_enabled)
436
    delete gdbServer;
437
  delete jtag;
438 6 julius
  delete monitor;
439
  delete reset;
440
 
441
  delete accessor;
442
 
443 49 julius
  //delete trace;
444 6 julius
  delete orpsoc;
445
 
446
  return 0;
447
 
448
}       /* sc_main() */

powered by: WebSVN 2.1.0

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