File: synth_microchip.cc

package info (click to toggle)
yosys 0.52-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 69,796 kB
  • sloc: ansic: 696,955; cpp: 239,736; python: 14,617; yacc: 3,529; sh: 2,175; makefile: 1,945; lex: 697; perl: 445; javascript: 323; tcl: 162; vhdl: 115
file content (553 lines) | stat: -rw-r--r-- 17,008 bytes parent folder | download
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
ISC License

Copyright (C) 2024 Microchip Technology Inc. and its subsidiaries

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/celltypes.h"
#include "kernel/log.h"
#include "kernel/register.h"
#include "kernel/rtlil.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct SynthMicrochipPass : public ScriptPass {
	SynthMicrochipPass() : ScriptPass("synth_microchip", "synthesis for Microchip FPGAs") {}

	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    synth_microchip [options]\n");
		log("\n");
		log("This command runs synthesis for Microchip FPGAs. This command creates \n");
		log("netlists that are compatible with Microchip PolarFire devices. \n");
		log("\n");
		log("    -top <module>\n");
		log("        use the specified module as the top module\n");
		log("\n");
		log("    -family <family>\n");
		log("        Run synthesis for the specified Microchip architecture. \n");
		log("        Generate the synthesis netlist for the specified family.\n");
		log("        supported values:\n");
		log("        - polarfire: PolarFire\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("    -blif <file>\n");
		log("        Write the design to the specified BLIF 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\n");
		log("        file is omitted if this parameter is not specified.\n");
		log("    -nobram\n");
		log("        Do not use block RAM cells in output netlist\n");
		log("\n");
		log("    -nocarry\n");
		log("        Do not use ARI1 cells in output netlist\n");
		log("\n");
		log("    -nodsp\n");
		log("        Do not use MATH blocks to implement multipliers and associated logic\n");
		log("\n");
		log("    -noiopad\n");
		log("        Disable I/O buffer insertion (useful for hierarchical or \n");
		log("        out-of-context flows)\n");
		log("\n");
		log("    -noclkbuf\n");
		log("        Disable automatic clock buffer insertion\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("    -dff\n");
		log("        Run 'abc'/'abc9' with -dff option\n");
		log("\n");
		log("    -retime\n");
		log("        Run 'abc' with '-D 1' option to enable flip-flop retiming.\n");
		log("        implies -dff.\n");
		log("\n");
		log("    -noabc9\n");
		log("        Use classic ABC flow instead of ABC9\n");
		log("\n");
		log("    -discard-ffinit\n");
		log("        discard FF init value instead of emitting an error\n");
		log("\n");
		log("\n");
		log("The following commands are executed by this synthesis command:\n");
		help_script();
		log("\n");
	}

	std::string top_opt, edif_file, blif_file, vlog_file, family;
	bool flatten, retime, noiopad, noclkbuf, nobram, nocarry, nowidelut, nodsp;
	bool abc9, dff;
	bool discard_ffinit;
	int lut_size;

	// debug dump switches
	bool debug_memory, debug_carry;

	void clear_flags() override
	{
		top_opt = "-auto-top";
		edif_file.clear();
		blif_file.clear();
		vlog_file.clear();
		family = "polarfire";
		flatten = true;
		retime = false;
		noiopad = false;
		noclkbuf = false;
		nocarry = false;
		nobram = false;
		nowidelut = false;
		nodsp = false;
		abc9 = true;
		dff = false;
		lut_size = 4;
		discard_ffinit = false;

		debug_memory = false;
		debug_carry = false;
	}

	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		std::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] == "-family" || args[argidx] == "-arch") && argidx + 1 < args.size()) {
				family = args[++argidx];
				continue;
			}
			if (args[argidx] == "-edif" && argidx + 1 < args.size()) {
				edif_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-blif" && argidx + 1 < args.size()) {
				blif_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-vlog" && argidx + 1 < args.size()) {
				vlog_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") {
				dff = true;
				retime = true;
				continue;
			}
			if (args[argidx] == "-nocarry") {
				nocarry = true;
				continue;
			}
			if (args[argidx] == "-nowidelut") {
				nowidelut = true;
				continue;
			}
			if (args[argidx] == "-iopad") {
				continue;
			}
			if (args[argidx] == "-noiopad") {
				noiopad = true;
				continue;
			}
			if (args[argidx] == "-noclkbuf") {
				noclkbuf = true;
				continue;
			}
			if (args[argidx] == "-nocarry") {
				nocarry = true;
				continue;
			}
			if (args[argidx] == "-nobram") {
				nobram = true;
				continue;
			}
			if (args[argidx] == "-noabc9") {
				abc9 = false;
				continue;
			}
			if (args[argidx] == "-nodsp") {
				nodsp = true;
				continue;
			}
			if (args[argidx] == "-dff") {
				dff = true;
				continue;
			}
			if (args[argidx] == "-debug_memory") {
				debug_memory = true;
				continue;
			}
			if (args[argidx] == "-debug_carry") {
				debug_carry = true;
				continue;
			}
			if (args[argidx] == "-discard-ffinit") {
				discard_ffinit = true;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		if (family == "polarfire") {
			lut_size = 4;
		} else {
			log_cmd_error("Invalid Microchip -family setting: '%s'.\n", family.c_str());
		}

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

		if (abc9 && retime)
			log_cmd_error("-retime option not currently compatible with -abc9!\n");

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

		run_script(design, run_from, run_to);

		log_pop();
	}

	void script() override
	{
		std::string lut_size_s = std::to_string(lut_size);
		if (help_mode)
			lut_size_s = "[4]";

		if (check_label("begin")) {
			std::string read_args;
			read_args += " -lib -specify +/microchip/cells_sim.v";
			run("read_verilog" + read_args);

			run(stringf("hierarchy -check %s", top_opt.c_str()));
		}

		if (check_label("prepare")) {
			run("proc");
			if (flatten || help_mode)
				run("flatten", "(with '-flatten')");
			if (active_design)
				active_design->scratchpad_unset("tribuf.added_something");
			run("tribuf -logic");
			if (noiopad && active_design && active_design->scratchpad_get_bool("tribuf.added_something"))
				log_error("Tristate buffers are unsupported without the '-iopad' option.\n");
			run("deminout");
			run("opt_expr");
			run("opt_clean");
			run("check");
			run("opt -nodffe -nosdff");
			run("fsm");
			run("opt");

			run("wreduce");
			run("peepopt");
			run("opt_clean");
		}

		if (check_label("map_dsp", "(skip if '-nodsp')")) {
			if (!nodsp || help_mode) {
				run("memory_dff"); // microchip_dsp will merge registers, reserve memory port registers first
				if (help_mode)
					run("techmap -map +/mul2dsp.v -map +/microchip/{family}_dsp_map.v {options}");
				else if (family == "polarfire") // Microchip - map multipliers to DSP
					run("techmap -map +/mul2dsp.v -map +/microchip/polarfire_dsp_map.v -D DSP_A_MAXWIDTH=18 -D DSP_B_MAXWIDTH=18 "
					    "-D DSP_A_MAXWIDTH_PARTIAL=18 " // Partial multipliers are intentionally
									    // limited to 18x18 in order to take
									    // advantage of the (PCOUT >> 17) -> PCIN
									    // dedicated cascade chain capability
					    "-D DSP_A_MINWIDTH=2 -D DSP_B_MINWIDTH=2 " // Blocks Nx1 multipliers
					    "-D DSP_Y_MINWIDTH=9 "
					    "-D DSP_SIGNEDONLY=1 -D DSP_NAME=$__MUL18X18");

				run("select a:mul2dsp");
				run("setattr -unset mul2dsp");
				run("opt_expr -fine");
				run("wreduce");
				run("select -clear");
				if (help_mode)
					run("microchip_dsp -family <family>");
				else if (family == "polarfire") // Microchip - absorb cells into DSP
					run("microchip_dsp -family " + family);

				run("chtype -set $mul t:$__soft_mul");
			}
		}

		if (check_label("coarse")) {
			run("techmap -map +/cmp2lut.v -map +/cmp2lcu.v -D LUT_WIDTH=" + lut_size_s);
			run("alumacc");
			run("share");
			run("opt");
			run("memory -nomap");
			run("opt_clean");
			if (discard_ffinit || help_mode)
				run("attrmap -remove init", "(only if -discard-ffinit)");
		}

		if (check_label("map_memory")) {
			std::string params = "";
			std::string LSRAM_map = "+/microchip/LSRAM_map.v";
			std::string uSRAM_map = "+/microchip/uSRAM_map.v";
			if (debug_memory)
				run("write_verilog -noexpr memory_map_pre.vm");
			if (help_mode) {
				params = " [...]";
			} else {

				if (family == "polarfire") {
					// cost of a single bit for memory lowered to soft logic
					params += " -logic-cost-rom 0.015625";

					params += " -lib +/microchip/LSRAM.txt";
					params += " -lib +/microchip/uSRAM.txt";
					LSRAM_map = "+/microchip/LSRAM_map.v";
					uSRAM_map = "+/microchip/uSRAM_map.v";
				}
				if (nobram)
					params += " -no-auto-block";
			}

			// transform memories into intermediate cells
			// Cost based transformation. The cost is assigned by us for each cell.
			run("memory_libmap" + params);
			if (debug_memory)
				run("write_verilog -noexpr memory_map_libmap.vm");

			// map intermediate cells to actual RAM macros
			// NOTE: order doesnt matter here
			run("techmap -map " + LSRAM_map);
			run("techmap -map " + uSRAM_map);
			if (debug_memory)
				run("write_verilog -noexpr memory_map_final.vm");
		}

		if (check_label("map_ffram")) {
			run("opt -fast -full");

			// blast unmapped RAM to flops or LUTs
			run("memory_map");
		}

		if (check_label("fine")) {
			run("opt -full");

			if (debug_carry)
				run("write_verilog -noexpr ARI1_cells.vm");

			if (!nocarry) {
				// converts $mux -> $_MUX_ to allow muxcover to work
				run("simplemap t:$mux");

				// converts $and/$or/$xor to gate representation for extract_reduce to work
				run("simplemap t:$xor"); // only mapping reduce_xor

				// mapping based on Yosys internal gates
				if (debug_carry)
					run("write_verilog -noexpr ARI1_pre.vm");

				// collapse $_AND_/$_OR_/$_XOR_ chains into reduction cells
				run("extract_reduce");

				if (debug_carry)
					run("write_verilog -noexpr ARI1_extract_reduce.vm");

				// pack mux trees into $_MUX4_
				run("muxcover -nodecode -mux4=220");

				if (debug_carry)
					run("write_verilog -noexpr ARI1_muxcover.vm");

				run("techmap -map +/microchip/arith_map.v");
				if (debug_carry)
					run("write_verilog -noexpr ARI1_post.vm");
			}

			// convert all remaining cells to gates
			run("techmap -map +/techmap.v");

			run("opt -fast");
		}

		if (check_label("map_cells")) {
			// Needs to be done before logic optimization, so that inverters (inserted
			// 			here because of negative-polarity output enable) are handled.
			if (help_mode || !noiopad) {
				run("iopadmap -bits -inpad INBUF Y:PAD -outpad OUTBUF D:PAD -toutpad TRIBUFF E:D:PAD -tinoutpad BIBUF E:Y:D:PAD",
				    "(unless -noiobs)");
			}

			std::string techmap_args = "-map +/techmap.v -map +/microchip/cells_map.v";
			run("techmap " + techmap_args);
			run("clean");
		}

		if (check_label("map_ffs")) {
			// dfflegalize : Converts FFs to types supported by the target
			// 		this can convert less capable cells into more capable cells (e.g. dff -> dffe)

			// Based on PolarFire® FPGA Macro Library Guide
			// D-flop:
			// 		active high enable
			// 		active low clear or active low set
			// Latch:
			//		active low clear or active low set
			// SLE (can implement D-flop/Latch):
			//		active high EN
			//		active low async load (set/reset) with static load configuration via ADn (Q = ~ADn)
			//		active low sync load (set/reset) with static load configuration via SD
			//		static latch configuration bit
			// init not supported

			// Yosys internal cell description
			// 		see: https://yosyshq.readthedocs.io/projects/yosys/en/latest/yosys_internals/formats/cell_library.html
			//		see: common/simcells.v
			// $_DFF_[NP]_ 						(regular dff)
			// $_DFFE_[NP][NP]_ 				(enable)
			// $_DFF_[NP][NP][01]_ 				(async reset to 0/1)
			// $_DFFE_[NP][NP][01][NP]_ 		(async reset to 0/1 + enable)
			// $_ALDFF_[NP][NP]_				(async load)
			// $_ALDFFE_[NP][NP][NP]_			(async load + enable)
			// $_DFFSR_[NP][NP][NP]_			(async set & reset)
			// $_DFFSRE_[NP][NP][NP][NP]_		(async set & reset + enable)
			// $_SDFF_[NP][NP][01]_				(sync reset to 0/1)
			// $_SDFFE_[NP][NP][01][NP]_		(sync reset to 0/1 + enable, reset prioritize over enable)
			// $_SDFFCE_[NP][NP][01][NP]_		(sync reset to 0/1 + enable, enable prioritize over reset)
			// $_SR_[NP][NP]_					(set/reset latch)
			// $_DLATCH_[NP]_					(D-latch)
			// $_DLATCH_[NP][NP][01]_			(D-latch + reset to 0/1)
			// $_DLATCHSR_[NP][NP][NP]_			(D-latch + set + reset)

			if (family == "polarfire") {
				std::string params = "";

				// D-flop with async reset and enable
				// posedge CLK, active low reset to 1 or 0, active high EN
				params += " -cell $_DFFE_PN?P_ x";

				// D-flop with sync reset and enable, enable takes priority over reset
				// posedge CLK, active low reset to 1 or 0, active high EN
				params += " -cell $_SDFFCE_PN?P_ x";

				// D-latch + reset to 0/1
				// posedge CLK, active low reset to 1 or 0
				params += " -cell $_DLATCH_PN?_ x";

				run("dfflegalize" + params, "(Converts FFs to supported types)");
			}

			if (abc9 || help_mode) {
				if (dff || help_mode)
					run("zinit -all w:* t:$_SDFFCE_*", "('-dff' only)");
				run("techmap -D NO_LUT -map +/microchip/cells_map.v", "('-abc9' only)");
			}
		}

		if (check_label("map_luts")) {
			run("opt_expr -mux_undef -noclkinv");
			if (help_mode)
				run("abc -luts 2:2,3,6:5[,10,20] [-dff] [-D 1]", "(option for '-nowidelut', '-dff', '-retime')");
			else if (abc9) {

				std::string abc9_opts;
				// for the if command in abc to specify wire delay between adjacent LUTs (default = 0)
				// NOTE: should not have 0 wire delay between LUTs,
				//		 otherwise abc might use LUT2+LUT3 instead of single LUT4
				abc9_opts += " -W 300";
				if (nowidelut)
					abc9_opts += stringf(" -maxlut %d", lut_size);
				if (dff)
					abc9_opts += " -dff";
				run("abc9" + abc9_opts);
			} else {
				std::string abc_opts = " -lut " + lut_size_s;
				if (dff)
					abc_opts += " -dff";
				if (retime)
					abc_opts += " -D 1";
				run("abc" + abc_opts);
			}
			run("clean");

			if (help_mode || !abc9)
				run("techmap -D NO_LUT -map +/microchip/cells_map.v", "(only if not '-abc9')");
			std::string techmap_args = "-map +/microchip/cells_map.v -D FINAL_MAP";
			techmap_args += " -D LUT_WIDTH=" + lut_size_s;
			run("techmap " + techmap_args);

			if (help_mode || lut_size == 4)
				run("microchip_dffopt");
		}

		run("clkbufmap -buf CLKINT Y:A -inpad CLKBUF Y:PAD");

		run("clean -purge");

		if (check_label("check")) {
			run("hierarchy -check");
			run("stat");
			run("check -noinit");
			run("blackbox =A:whitebox");
		}

		if (check_label("edif")) {
			if (!edif_file.empty() || help_mode)
				run(stringf("write_edif -pvector bra %s", edif_file.c_str()));
		}

		if (check_label("blif")) {
			if (!blif_file.empty() || help_mode)
				run(stringf("write_blif %s", blif_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()));
		}
	}
} SynthMicrochipPass;

PRIVATE_NAMESPACE_END