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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
#include <ctime>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <deque>
#include <optical_signal.h>
#include <time_monitor.h>
#include <tb/alltestbenches.h>
#include <utils/strutils.h>
#include <systemc.h>
#include <args.hxx>
#include <specs.h>
#include "optical_output_port.h"
#include "parser/parse_tree.h"
class OpticalOutputPort;
using namespace std;
using namespace std::chrono;
using namespace literals;
#include "../build/parser/parser.tab.h"
#include "../build/parser/parser.yy.h"
extern int yydebug;
int yywrap(yyscan_t scanner);
static deque<YY_BUFFER_STATE> netlist_buf_fifo;
FILE* open_netlist_file(const string &filename, yyscan_t &scanner)
{
// Check whether the file exists
FILE *f;
if (filename == "-"s) {
cerr << "Error: stdin not supported." << endl;
exit(1);
} else if (!(f = fopen(filename.c_str(), "r"))) {
cerr << "Error: File not found \"" << filename << "\"" << endl;
exit(1);
}
YY_BUFFER_STATE buf = yy_create_buffer(f, YY_BUF_SIZE, scanner);
netlist_buf_fifo.push_back(buf);
return f;
}
int yywrap(yyscan_t scanner)
{
if (netlist_buf_fifo.empty())
return 1;
else
{
YY_BUFFER_STATE buf = netlist_buf_fifo.front();
netlist_buf_fifo.pop_front();
yy_switch_to_buffer(buf, scanner);
return 0;
}
}
int do_list_tests()
{
cout << "Valid testbench identifiers:" << endl;
if (tb_map.empty())
cout << "\t # No test available #" << endl;
for (const auto &test_pair : tb_map) {
cout << "\t" << test_pair.first << endl;
}
return 0;
}
int do_test(const string &testname)
{
// Try to find the test specified by testname from the tb_mab
auto it = tb_map.find(testname);
if (it == tb_map.end()) {
// Not found
cerr << "Test not found: " << testname << endl;
return 1;
}
// Call the testbench function associated with testname
auto start = high_resolution_clock::now();
it->second();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
std::cout << "Total runtime: " << duration.count()/1000.0 << " ms" << std::endl;
return 0;
}
int do_circuit(const string &filename, bool is_dry_run = false, const string& json_filename = "")
{
// Check whether the file exists
FILE *f;
if (filename == "-"s) {
f = NULL;
} else if (!(f = fopen(filename.c_str(), "r"))) {
cerr << "Error: File not found \"" << filename << "\"" << endl;
return 1;
}
cout << "╔═══════════════════╗" << endl;
cout << "║ PARSING CIRCUIT ║" << endl;
cout << "╚═══════════════════╝" << endl;
yyscan_t scanner;
YY_BUFFER_STATE buf;
yylex_init(&scanner);
buf = yy_create_buffer(f, YY_BUF_SIZE, scanner);
yy_switch_to_buffer(buf, scanner);
ParseTree pt("ROOT");
int parsing_result = yyparse(scanner, &pt);
//yy_delete_buffer(buf, scanner);
yylex_destroy(scanner);
// Close the file
if (f)
fclose(f);
// Return if unsuccessful
if (parsing_result != 0) {
return parsing_result;
}
pt.print();
cout << "╔══════════════════════╗" << endl;
cout << "║ BUILDING CIRCUIT ║" << endl;
cout << "╚══════════════════════╝" << endl;
pt.build_circuit();
if (!json_filename.empty())
{
ofstream outfile;
outfile.open(json_filename, ios::out | ios::trunc);
outfile << pt.to_json();
outfile.close();
cout << "Exported flattened circuit as JSON > " << json_filename << endl;
}
if (!is_dry_run)
{
cout << "╔══════════════════════╗" << endl;
cout << "║ SIMULATION ║" << endl;
cout << "╚══════════════════════╝" << endl;
specsGlobalConfig.runAnalysis();
}
else
{
cout << "╔══════════════════════╗" << endl;
cout << "║ SIMULATION SKIPPED ║" << endl;
cout << "╚══════════════════════╝" << endl;
}
cout << "╔═══════════════════╗" << endl;
cout << "║ DONE ║" << endl;
cout << "╚═══════════════════╝" << endl;
return 0;
}
int build_circuit(ParseTree &pt, const vector<string> &filenames)
{
yyscan_t scanner;
yylex_init(&scanner);
vector<FILE*> files;
for(const auto &fname: filenames)
{
FILE * f = open_netlist_file(fname, scanner);
files.push_back(f);
}
// Set up first file
int res = yywrap(scanner);
if(res != 0)
{
cerr << "Expected yywrap to return 0" << endl;
exit(1);
}
cout << "╔═══════════════════╗" << endl;
cout << "║ PARSING CIRCUIT ║" << endl;
cout << "╚═══════════════════╝" << endl;
int parsing_result = yyparse(scanner, &pt);
//yy_delete_buffer(buf, scanner);
yylex_destroy(scanner);
for (auto &f: files)
fclose(f);
// Return if unsuccessful
if (parsing_result != 0) {
return parsing_result;
}
pt.print();
cout << "╔══════════════════════╗" << endl;
cout << "║ BUILDING CIRCUIT ║" << endl;
cout << "╚══════════════════════╝" << endl;
pt.build_circuit();
return 0;
}
int raphael_main() { cout<< "Hello world" << endl; return 0; }
int tests_module_registry_access();
int sc_main(int argc, char *argv[])
{
// Define command line interface using args
args::ArgumentParser parser("The Scalable Photonic Event-driven Circuit Simulator.", "");
args::ValueFlagList<string> file(
parser, "circuit", "Simulate from circuit file", { 'f', "file" });
args::ValueFlag<int> set_timescale(parser,
"set_timescale",
"Set the engine timescale (as log10(timestep/1s))"
"(0: seconds, -3: ms, -6:µs, ..., -15: fs)",
{ 'R', "resolution" });
args::ValueFlag<string> set_simulator_mode(parser,
"set_simulator_mode",
"Set simulator mode. Possible values:\n"
" - time-domain, event-driven, td (default)\n"
" - sampled-time\n"
" - frequency-domain, fd",
{ 'm', "mode"});
args::Flag run_manual_test(parser,
"run_manual_test",
"Run manual test function",
{ 'T', "run-manual-test" });
#if YYDEBUG
args::Flag debug_netlist_parser(parser,
"debug_netlist_parser",
"Run with netlist parser debug information",
{ "debug-parser" });
#endif
args::ValueFlag<string> set_tracefile(parser,
"set_tracefile",
"Set the default trace file",
{ 'o', "output" });
args::ValueFlag<string> export_json(parser,
"export_json",
"Export json of completed circuit to file",
{ "json" });
args::Flag set_dry_run(parser,
"dry_run",
"Do not run simulation directives",
{ 'n', "dryrun" });
// args::ValueFlag<double> set_reltol(parser,
// "set_reltol",
// "Set the value of the rel_tol parameter",
// { "reltol" });
args::ValueFlag<double> set_reltol(parser,
"set_reltol",
"Set the value of the relative tolerance parameter for field",
{ "reltol" });
args::ValueFlag<double> set_abstol(parser,
"set_abstol",
"Set the value of the absolute tolerance parameter for field",
{ "abstol" });
args::ValueFlag<size_t> set_nrings_crow(parser,
"set_nrings_crow",
"temporary",
{ "nrings_crow" });
args::Flag set_verbose_component_initialization(parser,
"set_verbose_component_initialization",
"Print components detail before starting simulation",
{ "vci", "verbose_ci" });
args::Flag list_tests(parser,
"list_tests",
"List available testbenches",
{ 'p', "list-testbenches" });
args::Flag add_time_monitor(parser,
"add_time_monitor",
"Monitor simulation status",
{ "mon" });
args::ValueFlag<string> test(
parser,
"test_name",
"Run the specified testbench (use -p to list available testbenches)",
{ 't', "testbench" });
args::HelpFlag help(parser, "help", "Display this help menu", { 'h', "help" });
args::CompletionFlag completion(parser, { "complete" });
try {
parser.ParseCLI(argc, argv);
} catch (const args::Completion &e) {
cout << e.what();
return 0;
} catch (const args::Help &) {
cout << parser;
return 0;
} catch (const args::ParseError &e) {
cerr << e.what() << endl;
cerr << parser;
return 1;
} catch (const args::ValidationError &e) {
cerr << e.what() << endl;
cerr << parser;
return 1;
}
if (set_simulator_mode) {
const string &s = set_simulator_mode.Get();
if (strutils::iequals(s, "event-driven") || strutils::iequals(s, "time-domain") || strutils::iequals(s, "td"))
{
cout << "Optical ports working in event-driven mode by default" << endl;
specsGlobalConfig.simulation_mode = OpticalOutputPortMode::EVENT_DRIVEN;
}
else if (strutils::iequals(s, "sampled-time"))
{
cout << "Optical ports working in sampled-time mode by default" << endl;
specsGlobalConfig.simulation_mode = OpticalOutputPortMode::SAMPLED_TIME;
}
else if (strutils::iequals(s, "frequency-domain") || strutils::iequals(s, "fd"))
{
cout << "Simulation mode set to frequency domain" << endl;
specsGlobalConfig.simulation_mode = OpticalOutputPortMode::FREQUENCY_DOMAIN;
}
else
{
cerr << "Unknown mode: '" << s << "'" << endl;
return 1;
}
}
if (set_reltol) {
if (set_reltol.Get() > 0) {
specsGlobalConfig.default_reltol = set_reltol.Get();
} else {
cerr << "Invalid abs_tol_phase value" << endl;
return 1;
}
}
if (set_abstol) {
if (set_abstol.Get() > 0) {
specsGlobalConfig.default_abstol = set_abstol.Get();
} else {
cerr << "Invalid abs_tol_power value" << endl;
return 1;
}
}
if (set_verbose_component_initialization) {
specsGlobalConfig.verbose_component_initialization = set_verbose_component_initialization.Get();
}
if (set_nrings_crow) {
#if BUILD_TB == 1
//nrings_crow = set_nrings_crow.Get();
#endif
}
if (set_timescale) {
cout << set_timescale.Get() << endl;
specsGlobalConfig.engine_timescale = SPECSConfig::EngineTimescale(set_timescale.Get());
}
if (run_manual_test) {
return 0;
}
#if YYDEBUG
if (debug_netlist_parser) {
yydebug = 1;
}
#endif
if (list_tests) {
return do_list_tests();
}
if (set_tracefile) {
cout << "Using trace file: " << set_tracefile.Get() << endl;
// TODO: validate filename
specsGlobalConfig.trace_filename = set_tracefile.Get();
}
shared_ptr<TimeMonitor> tm;
if (add_time_monitor) {
if (specsGlobalConfig.simulation_mode == OpticalOutputPortMode::FREQUENCY_DOMAIN)
tm = make_shared<TimeMonitor>("TM", 0, 0.5);
else
tm = make_shared<TimeMonitor>("TM", 1e-14, 0.5);
}
if (file) {
ParseTree pt;
int parse_result = build_circuit(pt, file.Get());
if (parse_result)
{
cerr << "Parsing failed with code " << parse_result << endl;
exit(parse_result);
}
string json_filename = export_json.Get();
if (!json_filename.empty())
{
ofstream outfile;
outfile.open(json_filename, ios::out | ios::trunc);
outfile << pt.to_json();
outfile.close();
cout << "Exported flattened circuit as JSON > " << json_filename << endl;
}
if (!set_dry_run.Get())
{
cout << "╔══════════════════════╗" << endl;
cout << "║ SIMULATION ║" << endl;
cout << "╚══════════════════════╝" << endl;
specsGlobalConfig.runAnalysis();
}
else
{
cout << "╔══════════════════════╗" << endl;
cout << "║ SIMULATION SKIPPED ║" << endl;
cout << "╚══════════════════════╝" << endl;
}
cout << "╔═══════════════════╗" << endl;
cout << "║ DONE ║" << endl;
cout << "╚═══════════════════╝" << endl;
return 0;
}
if (test) {
return do_test(test.Get());
}
// Print help if nothing else was done
cout << parser;
return 1;
}
|