-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
159 lines (142 loc) · 4.87 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @file main.cpp
*/
#include "top.hpp"
#include "report.hpp"
#include "fpsqrt.hpp"
#include "wallclock.hpp"
#include <string>
#include <map>
#include <vector>
#include <memory>
using namespace sc_core;
namespace {
char const * const MSGID{ "/Doulos Inc/Example/Modern/main" };
double elaboration_cpuTime=-1.0, starting_cpuTime=-1.0, finished_cpuTime=-1.0;
int summary( void );
}
/**
* Globals
*/
std::map<std::string,std::string> cmdline; ///< parsed from command-line
/**
* @brief Entry point for SystemC
*/
int sc_main( [[maybe_unused]]int argc, [[maybe_unused]]char* argv[] )
{
/**...........................................................................
* Scan command-line for options
*/
std::string others;
for( int i=1; i<sc_argc(); ++i ) {
std::string arg(sc_argv()[i]);
if( arg == "--" ) {
// ignore
} else if( arg[0] == '-' and arg.find_first_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == 1 ) {
size_t pos = arg.find_first_of( "=" );
if( pos == std::string::npos ) {
cmdline[ arg ] = "1";
} else {
cmdline[ arg.substr( 0, pos ) ] = arg.substr( pos+1 );
}
} else {
if( others.length() > 0 ) others += '\1';
others += arg;
}
}//endfor
if( others.length() > 0 ) cmdline[ "~" ] = others;
if( cmdline.size() > 0 ) {
MESSAGE( "Command-line options:" );
for( auto const& [option, value] : cmdline ) {
MESSAGE( "\n " << option << " = " << value );
}
MEND( ALWAYS );
}
/**...........................................................................
* @brief Test fpsqrt
*/
{
std::vector<FixedPt_t> sqrs = { 0, 1, 2, 4, 9.5, 6.4, 64.5, 100, 100.5, 95, 10000, 11200 };
for( auto v : sqrs ) {
INFO( ALWAYS, "FP::fpsqrt( " << v.to_double() << " ) = " << FP::fpsqrt( v ).to_double() );
}
}
if( cmdline.count("-x") > 0 ) {
exit( 0 );
}
/**...........................................................................
* Setup reporting
*/
if( cmdline.count("-d") > 0 ) {
sc_report_handler::set_verbosity_level( SC_DEBUG );
}
// Simplify error handling by avoiding the SC_THROW
sc_report_handler::set_actions( SC_ERROR, SC_DISPLAY | SC_LOG );
/**...........................................................................
* Instantiate
*/
elaboration_cpuTime = get_cpu_time(); //< Capture start of elaboration
std::unique_ptr<Top_module> top;
try {
// Construct top-level
top.reset(new Top_module("top"));
} catch (sc_core::sc_exception& e) {
REPORT(INFO,"\n" << e.what() << "\n\n*** Please fix elaboration errors and retry. ***");
return summary();
} catch (...) {
REPORT(INFO,"\n Error: *** Caught unknown exception during elaboration. ***");
return summary();
}//endtry
/**...........................................................................
* Simulate
*/
sc_core::sc_time finished_simTime;
try {
REPORT(INFO,"Starting kernel");
starting_cpuTime = get_cpu_time(); //< Capture start of simulation
sc_core::sc_start();
finished_simTime = sc_core::sc_time_stamp();
finished_cpuTime = get_cpu_time();
} catch (sc_core::sc_exception& e) {
REPORT(WARNING,"\n\nCaught exception during active simulation.\n" << e.what());
} catch (...) {
REPORT(WARNING,"Error: Caught unknown exception during active simulation.");
}//endtry
REPORT(INFO,"Exited kernel at " << finished_simTime);
// Clean up
if ( not sc_end_of_simulation_invoked() )
{
SC_REPORT_ERROR( MSGID, "Simulation stopped without explicit sc_stop()");
sc_stop(); //< invoke end_of_simulation() overrides
}
return summary();
}
#include <iomanip>
namespace {
using namespace std;
// Summarize results
int summary( void )
{
std::string kind = "Simulation";
if ( starting_cpuTime < 0.0 ) {
kind = "Elaboration";
starting_cpuTime = finished_cpuTime = get_cpu_time();
}
if ( finished_cpuTime < 0.0 ) {
finished_cpuTime = get_cpu_time();
}
auto errors = sc_report_handler::get_count(SC_ERROR)
+ sc_report_handler::get_count(SC_FATAL);
REPORT(INFO, "\n" << std::string(80,'#') << "\nSummary for " << sc_argv()[0] << ":\n "
<< "C++ " << __cplusplus << "L\n "
<< "CPU elaboration time " << std::setprecision(4) << (starting_cpuTime - elaboration_cpuTime) << " sec\n "
<< "CPU simulation time " << setprecision(4) << (finished_cpuTime - starting_cpuTime) << " sec\n "
<< setw(2) << sc_report_handler::get_count(SC_INFO) << " informational messages" << "\n "
<< setw(2) << sc_report_handler::get_count(SC_WARNING) << " warnings" << "\n "
<< setw(2) << sc_report_handler::get_count(SC_ERROR) << " errors" << "\n "
<< setw(2) << sc_report_handler::get_count(SC_FATAL) << " fatals" << "\n\n"
<< kind << " " << (errors?"FAILED":"PASSED")
);
return (errors?1:0);
}
}