aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/sf2/synth_sf2.cc
blob: 6b2a3f9b8306ea344dbe39fa1ad69806f3d13ed7 (plain)
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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "kernel/register.h"
#include "kernel/celltypes.h"
#include "kernel/rtlil.h"
#include "kernel/log.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct SynthSf2Pass : public ScriptPass
{
	SynthSf2Pass() : ScriptPass("synth_sf2", "synthesis for SmartFusion2 and IGLOO2 FPGAs") { }

	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    synth_sf2 [options]\n");
		log("\n");
		log("This command runs synthesis for SmartFusion2 and IGLOO2 FPGAs.\n");
		log("\n");
		log("    -top <module>\n");
		log("        use the specified module as top module\n");
		log("\n");
		log("    -edif <file>\n");
		log("        write the design to the specified EDIF file. writing of an output file\n");
		log("        is omitted if this parameter is not specified.\n");
		log("\n");
		log("    -vlog <file>\n");
		log("        write the design to the specified Verilog file. writing of an output file\n");
		log("        is omitted if this parameter is not specified.\n");
		log("\n");
		log("    -json <file>\n");
		log("        write the design to the specified JSON file. writing of an output file\n");
		log("        is omitted if this parameter is not specified.\n");
		log("\n");
		log("    -run <from_label>:<to_label>\n");
		log("        only run the commands between the labels (see below). an empty\n");
		log("        from label is synonymous to 'begin', and empty to label is\n");
		log("        synonymous to the end of the command list.\n");
		log("\n");
		log("    -noflatten\n");
		log("        do not flatten design before synthesis\n");
		log("\n");
		log("    -noiobs\n");
		log("        run synthesis in \"block mode\", i.e. do not insert IO buffers\n");
		log("\n");
		log("    -clkbuf\n");
		log("        insert direct PAD->global_net buffers\n");
		log("\n");
		log("    -retime\n");
		log("        run 'abc' with '-dff -D 1' options\n");
		log("\n");
		log("\n");
		log("The following commands are executed by this synthesis command:\n");
		help_script();
		log("\n");
	}

	string top_opt, edif_file, vlog_file, json_file;
	bool flatten, retime, iobs, clkbuf;

	void clear_flags() override
	{
		top_opt = "-auto-top";
		edif_file = "";
		vlog_file = "";
		json_file = "";
		flatten = true;
		retime = false;
		iobs = true;
		clkbuf = false;
	}

	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		string run_from, run_to;
		clear_flags();

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			if (args[argidx] == "-top" && argidx+1 < args.size()) {
				top_opt = "-top " + args[++argidx];
				continue;
			}
			if (args[argidx] == "-edif" && argidx+1 < args.size()) {
				edif_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-vlog" && argidx+1 < args.size()) {
				vlog_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-json" && argidx+1 < args.size()) {
				json_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-run" && argidx+1 < args.size()) {
				size_t pos = args[argidx+1].find(':');
				if (pos == std::string::npos)
					break;
				run_from = args[++argidx].substr(0, pos);
				run_to = args[argidx].substr(pos+1);
				continue;
			}
			if (args[argidx] == "-noflatten") {
				flatten = false;
				continue;
			}
			if (args[argidx] == "-retime") {
				retime = true;
				continue;
			}
			if (args[argidx] == "-noiobs") {
				iobs = false;
				continue;
			}
			if (args[argidx] == "-clkbuf") {
				clkbuf = true;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		if (!design->full_selection())
			log_cmd_error("This command only operates on fully selected designs!\n");

		log_header(design, "Executing SYNTH_SF2 pass.\n");
		log_push();

		run_script(design, run_from, run_to);

		log_pop();
	}

	void script() override
	{
		if (check_label("begin"))
		{
			run("read_verilog -lib +/sf2/cells_sim.v");
			run(stringf("hierarchy -check %s", help_mode ? "-top <top>" : top_opt.c_str()));
		}

		if (flatten && check_label("flatten", "(unless -noflatten)"))
		{
			run("proc");
			run("flatten");
			run("tribuf -logic");
			run("deminout");
		}

		if (check_label("coarse"))
		{
			run("synth -run coarse");
		}

		if (check_label("fine"))
		{
			run("opt -fast -mux_undef -undriven -fine");
			run("memory_map");
			run("opt -undriven -fine");
			run("techmap -map +/techmap.v -map +/sf2/arith_map.v");
			run("opt -fast");
			if (retime || help_mode)
				run("abc -dff -D 1", "(only if -retime)");
		}

		if (check_label("map_ffs"))
		{
			run("techmap -D NO_LUT -map +/sf2/cells_map.v");
			run("opt_expr -mux_undef");
			run("simplemap");
			// run("sf2_ffinit");
			// run("sf2_ffssr");
			// run("sf2_opt -full");
		}

		if (check_label("map_luts"))
		{
			run("abc -lut 4");
			run("clean");
		}

		if (check_label("map_cells"))
		{
			run("techmap -map +/sf2/cells_map.v");
			run("clean");
		}

		if (check_label("map_iobs"))
		{
			if (help_mode)
				run("sf2_iobs [-clkbuf]", "(unless -noiobs)");
			else if (iobs)
				run(clkbuf ? "sf2_iobs -clkbuf" : "sf2_iobs");
			run("clean");
		}

		if (check_label("check"))
		{
			run("hierarchy -check");
			run("stat");
			run("check -noinit");
		}

		if (check_label("edif"))
		{
			if (!edif_file.empty() || help_mode)
				run(stringf("write_edif -gndvccy %s", help_mode ? "<file-name>" : edif_file.c_str()));
		}

		if (check_label("vlog"))
		{
			if (!vlog_file.empty() || help_mode)
				run(stringf("write_verilog %s", help_mode ? "<file-name>" : vlog_file.c_str()));
		}

		if (check_label("json"))
		{
			if (!json_file.empty() || help_mode)
				run(stringf("write_json %s", help_mode ? "<file-name>" : json_file.c_str()));
		}
	}
} SynthSf2Pass;

PRIVATE_NAMESPACE_END
2918937600 27 # 1 Jul 1992 2950473600 28 # 1 Jul 1993 2982009600 29 # 1 Jul 1994 3029443200 30 # 1 Jan 1996 3076704000 31 # 1 Jul 1997 3124137600 32 # 1 Jan 1999 3345062400 33 # 1 Jan 2006 3439756800 34 # 1 Jan 2009 3550089600 35 # 1 Jul 2012 3644697600 36 # 1 Jul 2015 3692217600 37 # 1 Jan 2017 */ } double astro_convert_jd_to_julian_millenia_since_j2000(double jd) { return (jd - 2451545.0) / 365250.0; } astro_cartesian_coordinates_t astro_subtract_cartesian(astro_cartesian_coordinates_t a, astro_cartesian_coordinates_t b) { astro_cartesian_coordinates_t retval; retval.x = a.x - b.x; retval.y = a.y - b.y; retval.z = a.z - b.z; return retval; } // Performs the rotation from ecliptic coordinates to J2000 coordinates for the given vector x astro_cartesian_coordinates_t astro_rotate_from_vsop_to_J2000(astro_cartesian_coordinates_t c) { /* From VSOP87.doc X +1.000000000000 +0.000000440360 -0.000000190919 X Y = -0.000000479966 +0.917482137087 -0.397776982902 Y Z FK5 0.000000000000 +0.397776982902 +0.917482137087 Z VSOP87A */ astro_cartesian_coordinates_t t; t.x = c.x + c.y * 0.000000440360 + c.z * -0.000000190919; t.y = c.x * -0.000000479966 + c.y * 0.917482137087 + c.z * -0.397776982902; t.z = c.y * 0.397776982902 + c.z * 0.917482137087; return t; } double astro_get_GMST(double ut1) { double D = ut1 - 2451545.0; double T = D/36525.0; double gmst = fmod((280.46061837 + 360.98564736629*D + 0.000387933*T*T - T*T*T/38710000.0), 360.0); if(gmst<0) { gmst+=360; } return gmst/15; } static astro_matrix_t _astro_get_empty_matrix() { astro_matrix_t t; for(uint8_t i = 0; i < 3 ; i++) { for(uint8_t j = 0 ; j < 3 ; j++) { t.elements[i][j] = 0; } } return t; } //Gets a rotation matrix about the x axis. Angle R is in radians astro_matrix_t astro_get_x_rotation_matrix(double r) { astro_matrix_t t = _astro_get_empty_matrix(); t.elements[0][0]=1; t.elements[0][1]=0; t.elements[0][2]=0; t.elements[1][0]=0; t.elements[1][1]=cos(r); t.elements[1][2]=sin(r); t.elements[2][0]=0; t.elements[2][1]=-sin(r); t.elements[2][2]=cos(r); return t; } //Gets a rotation matrix about the y axis. Angle R is in radians astro_matrix_t astro_get_y_rotation_matrix(double r) { astro_matrix_t t = _astro_get_empty_matrix(); t.elements[0][0]=cos(r); t.elements[0][1]=0; t.elements[0][2]=-sin(r); t.elements[1][0]=0; t.elements[1][1]=1; t.elements[1][2]=0; t.elements[2][0]=sin(r); t.elements[2][1]=0; t.elements[2][2]=cos(r); return t; } //Gets a rotation matrix about the z axis. Angle R is in radians astro_matrix_t astro_get_z_rotation_matrix(double r) { astro_matrix_t t = _astro_get_empty_matrix(); t.elements[0][0]=cos(r); t.elements[0][1]=sin(r); t.elements[0][2]=0; t.elements[1][0]=-sin(r); t.elements[1][1]=cos(r); t.elements[1][2]=0; t.elements[2][0]=0; t.elements[2][1]=0; t.elements[2][2]=1; return t; } void astro_print_matrix(char * title, astro_matrix_t matrix); void astro_print_matrix(char * title, astro_matrix_t matrix) { printf("%s\n", title); for(uint8_t i = 0; i < 3 ; i++) { printf("\t"); for(uint8_t j = 0 ; j < 3 ; j++) { printf("%12f", matrix.elements[i][j]); } printf("\n"); } printf("\n"); } astro_matrix_t astro_dot_product(astro_matrix_t a, astro_matrix_t b) { astro_matrix_t retval; for(uint8_t i = 0; i < 3 ; i++) { for(uint8_t j = 0 ; j < 3 ; j++) { double temp = 0; for(uint8_t k = 0; k < 3 ; k++) { temp += a.elements[i][k] * b.elements[k][j]; } retval.elements[i][j]=temp; } } return retval; } astro_matrix_t astro_transpose_matrix(astro_matrix_t m) { astro_matrix_t retval; for(uint8_t i = 0; i < 3 ; i++) { for(uint8_t j = 0 ; j < 3 ; j++) { retval.elements[i][j] = m.elements[j][i]; } } return retval; } astro_matrix_t astro_get_precession_matrix(double jd) { //2006 IAU Precession. Implemented from IERS Technical Note No 36 ch5. //https://www.iers.org/SharedDocs/Publikationen/EN/IERS/Publications/tn/TechnNote36/tn36_043.pdf?__blob=publicationFile&v=1 double t = (jd - 2451545.0) / 36525.0; //5.2 const double Arcsec2Radians = M_PI/180.0/60.0/60.0; //Converts arc seconds used in equations below to radians double e0 = 84381.406 * Arcsec2Radians; //5.6.4 double omegaA = e0 + ((-0.025754 + (0.0512623 + (-0.00772503 + (-0.000000467 + 0.0000003337*t) * t) * t) * t) * t) * Arcsec2Radians; //5.39 double psiA = ((5038.481507 + (-1.0790069 + (-0.00114045 + (0.000132851 - 0.0000000951*t) * t) * t) * t) * t) * Arcsec2Radians; //5.39 double chiA = ((10.556403 + (-2.3814292 + (-0.00121197 + (0.000170663 - 0.0000000560*t) * t) * t) * t) * t) * Arcsec2Radians; //5.40 //Rotation matrix from 5.4.5 //(R1(−e0) · R3(psiA) · R1(omegaA) · R3(−chiA)) //Above eq rotates from "of date" to J2000, so we reverse the signs to go from J2000 to "of date" astro_matrix_t m1 = astro_get_x_rotation_matrix(e0); astro_matrix_t m2 = astro_get_z_rotation_matrix(-psiA); astro_matrix_t m3 = astro_get_x_rotation_matrix(-omegaA); astro_matrix_t m4 = astro_get_z_rotation_matrix(chiA); astro_matrix_t m5 = astro_dot_product(m4, m3); astro_matrix_t m6 = astro_dot_product(m5, m2); astro_matrix_t precessionMatrix = astro_dot_product(m6, m1); return precessionMatrix; } astro_cartesian_coordinates_t astro_matrix_multiply(astro_cartesian_coordinates_t v, astro_matrix_t m) { astro_cartesian_coordinates_t t; t.x = v.x*m.elements[0][0] + v.y*m.elements[0][1] + v.z*m.elements[0][2]; t.y = v.x*m.elements[1][0] + v.y*m.elements[1][1] + v.z*m.elements[1][2]; t.z = v.x*m.elements[2][0] + v.y*m.elements[2][1] + v.z*m.elements[2][2]; return t; } //Converts cartesian XYZ coordinates to polar (e.g. J2000 xyz to Right Accention and Declication) astro_equatorial_coordinates_t astro_convert_cartesian_to_polar(astro_cartesian_coordinates_t xyz) { astro_equatorial_coordinates_t t; t.distance = sqrt(xyz.x * xyz.x + xyz.y * xyz.y + xyz.z * xyz.z); t.declination = acos(xyz.z / t.distance); t.right_ascension = atan2(xyz.y, xyz.x); if(t.declination < 0) t.declination += 2 * M_PI; if(t.right_ascension < 0) t.right_ascension += 2 * M_PI; return t; } //Convert Geodedic Lat Lon to geocentric XYZ position vector //All angles are input as radians astro_cartesian_coordinates_t astro_convert_geodedic_latlon_to_ITRF_XYZ(double lat, double lon, double height) { //Algorithm from Explanatory Supplement to the Astronomical Almanac 3rd ed. P294 const double a = 6378136.6; const double f = 1 / 298.25642; const double C = sqrt(((cos(lat)*cos(lat)) + (1.0-f)*(1.0-f) * (sin(lat)*sin(lat)))); const double S = (1-f)*(1-f)*C; double h = height; astro_cartesian_coordinates_t r; r.x = (a*C+h) * cos(lat) * cos(lon); r.y = (a*C+h) * cos(lat) * sin(lon); r.z = (a*S+h) * sin(lat); return r; } //Convert position vector to celestial "of date" system. //g(t)=R3(-GAST) r //(Remember to use UT1 for GAST, not ET) //All angles are input and output as radians astro_cartesian_coordinates_t astro_convert_ITRF_to_GCRS(astro_cartesian_coordinates_t r, double ut1) { //This is a simple rotation matrix implemenation about the Z axis, rotation angle is -GMST double GMST = astro_get_GMST(ut1); GMST =- GMST * 15.0 * M_PI / 180.0; astro_matrix_t m = astro_get_z_rotation_matrix(GMST); astro_cartesian_coordinates_t t = astro_matrix_multiply(r, m); return t; } astro_cartesian_coordinates_t astro_convert_coordinates_from_meters_to_AU(astro_cartesian_coordinates_t c) { astro_cartesian_coordinates_t t; t.x = c.x / 1.49597870691E+11; t.y = c.y / 1.49597870691E+11; t.z = c.z / 1.49597870691E+11; return t; } astro_cartesian_coordinates_t astro_get_observer_geocentric_coords(double jd, double lat, double lon) { astro_cartesian_coordinates_t r = astro_convert_geodedic_latlon_to_ITRF_XYZ(lat, lon,0); r = astro_convert_ITRF_to_GCRS(r, jd); r = astro_convert_coordinates_from_meters_to_AU(r); return r; } //Returns a body's cartesian coordinates centered on the Sun. //Requires vsop87a_milli_js, if you wish to use a different version of VSOP87, replace the class name vsop87a_milli below astro_cartesian_coordinates_t astro_get_body_coordinates(astro_body_t body, double et) { astro_cartesian_coordinates_t retval = {0}; double coords[3]; switch(body) { case ASTRO_BODY_SUN: return retval; //Sun is at the center for vsop87a case ASTRO_BODY_MERCURY: vsop87a_milli_getMercury(et, coords); break; case ASTRO_BODY_VENUS: vsop87a_milli_getVenus(et, coords); break; case ASTRO_BODY_EARTH: vsop87a_milli_getEarth(et, coords); break; case ASTRO_BODY_MARS: vsop87a_milli_getMars(et, coords); break; case ASTRO_BODY_JUPITER: vsop87a_milli_getJupiter(et, coords); break; case ASTRO_BODY_SATURN: vsop87a_milli_getSaturn(et, coords); break; case ASTRO_BODY_URANUS: vsop87a_milli_getUranus(et, coords); break; case ASTRO_BODY_NEPTUNE: vsop87a_milli_getNeptune(et, coords); break; case ASTRO_BODY_EMB: vsop87a_milli_getEmb(et, coords); break; case ASTRO_BODY_MOON: { double earth_coords[3]; double emb_coords[3]; vsop87a_milli_getEarth(et, earth_coords); vsop87a_milli_getEmb(et, emb_coords); vsop87a_milli_getMoon(earth_coords, emb_coords, coords); } break; } retval.x = coords[0]; retval.y = coords[1]; retval.z = coords[2]; return retval; } astro_cartesian_coordinates_t astro_get_body_coordinates_light_time_adjusted(astro_body_t body, astro_cartesian_coordinates_t origin, double t) { //Get current position of body astro_cartesian_coordinates_t body_coords = astro_get_body_coordinates(body, t); double newT = t; for(uint8_t i = 0 ; i < 2 ; i++) { //Calculate light time to body body_coords = astro_subtract_cartesian(body_coords, origin); double distance = sqrt(body_coords.x*body_coords.x + body_coords.y*body_coords.y + body_coords.z*body_coords.z); distance *= 1.496e+11; //Convert from AU to meters double lightTime = distance / 299792458.0; //Convert light time to Julian Millenia, and subtract it from the original value of t newT -= lightTime / 24.0 / 60.0 / 60.0 / 365250.0; //Recalculate body position adjusted for light time body_coords = astro_get_body_coordinates(body, newT); } return body_coords; } astro_horizontal_coordinates_t astro_ra_dec_to_alt_az(double jd, double lat, double lon, double ra, double dec) { double GMST = astro_get_GMST(jd) * M_PI/180.0 * 15.0; double h = GMST + lon - ra; double sina = sin(dec)*sin(lat) + cos(dec)*cos(h)*cos(lat); double a = asin(sina); double cosAz = (sin(dec)*cos(lat) - cos(dec)*cos(h)*sin(lat)) / cos(a); double Az = acos(cosAz); if(sin(h) > 0) Az = 2.0*M_PI - Az; astro_horizontal_coordinates_t retval; retval.altitude = a; retval.azimuth = Az; return retval; } double astro_degrees_to_radians(double degrees) { return degrees * M_PI / 180; } double astro_radians_to_degrees(double radians) { return radians * 180.0 / M_PI; } astro_angle_dms_t astro_radians_to_dms(double radians) { astro_angle_dms_t retval; int8_t sign = (radians < 0) ? -1 : 1; double degrees = fabs(astro_radians_to_degrees(radians)); retval.degrees = (uint16_t)degrees; double temp = 60.0 * (degrees - retval.degrees); retval.minutes = (uint8_t)temp; retval.seconds = (uint8_t)round(60.0 * (temp - retval.minutes)); if (retval.seconds > 59) { retval.seconds = 0.0; retval.minutes++; } if (retval.minutes > 59) { retval.minutes = 0; retval.degrees++; } degrees *= sign; return retval; } astro_angle_hms_t astro_radians_to_hms(double radians) { astro_angle_hms_t retval; double degrees = astro_radians_to_degrees(radians); double temp = degrees / 15.0; retval.hours = (uint8_t)temp; temp = 60.0 * (temp - retval.hours); retval.minutes = (uint8_t)temp; retval.seconds = (uint8_t)round(60.0 * (temp - retval.minutes)); if (retval.seconds > 59) { retval.seconds = 0; retval.minutes++; } if (retval.minutes > 59) { retval.minutes = 0; retval.hours++; } return retval; }