-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (184 loc) · 6.55 KB
/
main.cpp
File metadata and controls
218 lines (184 loc) · 6.55 KB
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
/**
* MIT License
*
* Copyright (c) 2018 Américo Dias <americo.dias@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/** @file main.cpp
* @brief Main functions
*
* Contains all the main functions of the pll simulator.
*
* @author Americo Dias
* @date 06/02/2018
*/
/** @cond */
#include <iostream>
#include <fstream>
#include "include/json.hpp"
#include "include/cxxopts.hpp"
#include "systemc.h"
#include "systemc-ams.h"
/** @endcond */
#include "sc_pll.h"
#define VERSION "1.0"
#define NAME "SystemC-AMS PLL simulator"
#define concat(first, second, thirt) first second thirt
using namespace std;
using json = nlohmann::json;
/**
* Read json options from a file
* @param filename: Filename of the file containing the options in json format
* @return json: JSON object containing the parsed options
*/
json get_options(string filename) {
json j;
// read a JSON file
try {
std::ifstream i(filename);
i >> j;
}
catch (json::exception& e)
{
// output exception information
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << std::endl;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return j;
}
/**
* Parse the command line arguments
* @param argc Number of command line arguments
* @param argv Command line arguments
* @return Configuration filename, Output format
*/
std::pair<string, string> parse_opt(int argc, char* argv[]) {
string format;
string config_filename;
try
{
const char* header = concat(NAME, " ", VERSION);
cxxopts::Options opt(argv[0], header);
opt.positional_help("[optional args]").show_positional_help();
opt.add_options()
("c,config", "Configuration file", cxxopts::value<std::string>()->default_value("options.json"))
("f,format", "Output file format (vcd or csv)", cxxopts::value<std::string>()->default_value("vcd"))
("h,help", "Print help");
auto result = opt.parse(argc, argv);
if (result["help"].as<bool>())
{
std::cout << opt.help() << std::endl;
exit(0);
}
config_filename = result["config"].as<std::string>();
format = result["format"].as<std::string>();
ifstream infile(config_filename);
if(!infile.good()) {
cout << "The configuration file is missing!" << endl;
exit(-1);
}
if(format.compare("vcd") != 0 && format.compare("csv") != 0) {
cout << "Invalid output format!" << endl;
exit(-1);
}
} catch (const cxxopts::OptionException& e)
{
std::cout << "error parsing options: " << e.what() << std::endl;
exit(-1);
}
return std::make_pair(config_filename, format);
}
/**
* Main function
* @param argc Number of command line arguments
* @param argv Command line arguments
* @return int: Status
*/
int sc_main(int argc, char* argv[]){
json options; // JSON options
string format; // Output format: vcd, csv
string config_filename; // Configuration filename
sc_core::sc_set_time_resolution(1.0, sc_core::SC_FS);
cout << endl << endl;
std::tie(config_filename,format) = parse_opt(argc, argv); // Parse command line arguments
options = get_options(config_filename); // Parse json options file
// Top level signals:
sc_clock sc_sig_fref ("sig_fref", // Reference clock
sc_time(1/(double)options["system"]["fref"],
sc_core::SC_SEC), 0.5,
sc_time(0.5/(double)options["system"]["fref"],
sc_core::SC_SEC));
sca_tdf::sca_signal<double> sca_tdf_sig_fout_freq; // Output frequency
// Declaration of the pll block:
sc_pll pll("pll",
(double)options["system"]["tstep"],
(double)options["system"]["vdd"],
(double)options["charge_pump"]["current_up"],
(double)options["charge_pump"]["current_dn"],
(double)options["charge_pump"]["current_leak"],
(double)options["charge_pump"]["mosfet_vth"],
(int)options["loop_filter"]["order"],
(double)options["loop_filter"]["c1"],
(double)options["loop_filter"]["r2"],
(double)options["loop_filter"]["c2"],
(double)options["loop_filter"]["r3"],
(double)options["loop_filter"]["c3"],
(double)options["system"]["vcm"],
(double)options["vco"]["kvo"],
(double)options["vco"]["fmin"],
(unsigned int)options["divider"]["n"] );
// Bind the top level signals:
pll.sc_in_fref->bind(sc_sig_fref);
pll.sca_tdf_out_fout_freq->bind(sca_tdf_sig_fout_freq);
// Print some useful information:
cout << "Info: Simulation options:" << endl;
cout << " Simulation time: " << (double)options["system"]["tsim"] << endl;
cout << " Simulation timestep: " << (double)options["system"]["tstep"] << endl;
cout << " Ouput format: " << format << endl;
// Trace signals
sca_trace_file* tr;
if(format.compare("vcd") == 0)
tr = sca_create_vcd_trace_file("output"); // Usual SystemC tracing
else
tr = sca_create_tabular_trace_file("output"); // Usual SystemC tracing
sca_trace(tr, sc_sig_fref ,"sc_sig_fref");
sca_trace(tr, sca_tdf_sig_fout_freq ,"sca_tdf_sig_fout_freq");
sca_trace(tr, pll.sc_sig_up ,"pll.sc_sig_up");
sca_trace(tr, pll.sc_sig_dn ,"pll.sc_sig_dn");
sca_trace(tr, pll.sc_sig_fdiv ,"pll.sc_sig_fdiv");
sca_trace(tr, pll.sca_tdf_sig_vctrl ,"pll.sca_tdf_sig_vctrl");
sca_trace(tr, pll.sca_tdf_sig_vcp ,"pll.sca_tdf_sig_vcp");
sca_trace(tr, pll.sca_tdf_sig_ictrl ,"pll.sca_tdf_sig_ictrl");
sca_trace(tr, pll.sca_tdf_sig_fout ,"pll.sca_tdf_sig_fout");
// Start the simulation
sc_start((double)options["system"]["tsim"], sc_core::SC_SEC);
// Close trace file and exit
if(format.compare("vcd") == 0)
sca_close_vcd_trace_file(tr);
else
sca_close_tabular_trace_file(tr);
cout << "Simulation finished!" << endl;
return 0;
}