aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/quicklogic/synth_quicklogic.cc
blob: 94bd44db008a02b76d08f7daaf17646a995eb891 (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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2021 QuickLogic Corp.
 *
 *  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 SynthQuickLogicPass : public ScriptPass {

	SynthQuickLogicPass() : ScriptPass("synth_quicklogic", "Synthesis for QuickLogic FPGAs") {}

	void help() override
	{
		log("\n");
		log("   synth_quicklogic [options]\n");
		log("This command runs synthesis for QuickLogic FPGAs\n");
		log("\n");
		log("    -top <module>\n");
		log("         use the specified module as top module\n");
		log("\n");
		log("    -family <family>\n");
		log("        run synthesis for the specified QuickLogic architecture\n");
		log("        generate the synthesis netlist for the specified family.\n");
		log("        supported values:\n");
		log("        - pp3: PolarPro 3 \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("    -verilog <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("\n");
		log("    -abc\n");
		log("        use old ABC flow, which has generally worse mapping results but is less\n");
		log("        likely to have bugs.\n");
		log("\n");
		log("The following commands are executed by this synthesis command:\n");
		help_script();
		log("\n");
	}

	string top_opt, blif_file, family, currmodule, verilog_file;
	bool abc9;

	void clear_flags() override
	{
		top_opt = "-auto-top";
		blif_file = "";
		verilog_file = "";
		currmodule = "";
		family = "pp3";
		abc9 = true;
	}

	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] == "-family" && argidx+1 < args.size()) {
				family = args[++argidx];
				continue;
			}
			if (args[argidx] == "-blif" && argidx+1 < args.size()) {
				blif_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-verilog" && argidx+1 < args.size()) {
				verilog_file = args[++argidx];
				continue;
			}
			if (args[argidx] == "-abc") {
				abc9 = false;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

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

		if (family != "pp3")
			log_cmd_error("Invalid family specified: '%s'\n", family.c_str());

		if (abc9 && design->scratchpad_get_int("abc9.D", 0) == 0) {
			log_warning("delay target has not been set via SDC or scratchpad; assuming 12 MHz clock.\n");
			design->scratchpad_set_int("abc9.D", 41667); // 12MHz = 83.33.. ns; divided by two to allow for interconnect delay.
		}

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

		run_script(design, run_from, run_to);

		log_pop();
	}

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

		if (check_label("coarse")) {
			run("proc");
			run("flatten");
			run("tribuf -logic");
			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");
			run("share");
			run("techmap -map +/cmp2lut.v -D LUT_WIDTH=4");
			run("opt_expr");
			run("opt_clean");
			run("alumacc");
			run("pmuxtree");
			run("opt");
			run("memory -nomap");
			run("opt_clean");
		}

		if (check_label("map_ffram")) {
			run("opt -fast -mux_undef -undriven -fine");
			run("memory_map -iattr -attr !ram_block -attr !rom_block -attr logic_block "
				"-attr syn_ramstyle=auto -attr syn_ramstyle=registers "
				"-attr syn_romstyle=auto -attr syn_romstyle=logic");
			run("opt -undriven -fine");
		}

		if (check_label("map_gates")) {
			run("techmap");
			run("opt -fast");
			run("muxcover -mux8 -mux4");
		}

		if (check_label("map_ffs")) {
			run("opt_expr");
			run("dfflegalize -cell $_DFFSRE_PPPP_ 0 -cell $_DLATCH_?_ x");

			run(stringf("techmap -map +/quicklogic/%s_cells_map.v -map +/quicklogic/%s_ffs_map.v", family.c_str(), family.c_str()));

			run("opt_expr -mux_undef");
		}

		if (check_label("map_luts")) {
			run(stringf("techmap -map +/quicklogic/%s_latches_map.v", family.c_str()));
			if (abc9) {
				run("read_verilog -lib -specify -icells +/quicklogic/abc9_model.v");
				run("techmap -map +/quicklogic/abc9_map.v");
				run("abc9 -maxlut 4 -dff");
				run("techmap -map +/quicklogic/abc9_unmap.v");
			} else {
				run("abc -luts 1,2,2,4 -dress");
			}
			run("clean");
		}

		if (check_label("map_cells")) {
			run(stringf("techmap -map +/quicklogic/%s_lut_map.v", family.c_str()));
			run("clean");
		}

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

		if (check_label("iomap")) {
			run("clkbufmap -inpad ckpad Q:P");
			run("iopadmap -bits -outpad outpad A:P -inpad inpad Q:P -tinoutpad bipad EN:Q:A:P A:top");
		}

		if (check_label("finalize")) {
			run("setundef -zero -params -undriven");
			run("hilomap -hicell logic_1 A -locell logic_0 A -singleton A:top");
			run("opt_clean -purge");
			run("check");
			run("blackbox =A:whitebox");
		}

		if (check_label("blif")) {
			if (!blif_file.empty() || help_mode) {
				run(stringf("write_blif -attr -param %s %s", top_opt.c_str(), blif_file.c_str()));
			}
		}

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

} SynthQuicklogicPass;

PRIVATE_NAMESPACE_END
="p">) && ((*(s+2) >> 6) == 0x02)); case 4: /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */ return ((*s == 0xF0) && ((*(s+1) >> 4) == 0x08) && ((*(s+2) >> 6) == 0x02) && ((*(s+3) >> 6) == 0x02)); } return 0; } static inline int utf8seq_is_surrogate(char *s, int n) { return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF)); } static inline int utf8seq_is_illegal(char *s, int n) { return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) && (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF)); } static inline int utf8dec_wchar(wchar_t *c, unsigned char *in, size_t inb) { int i; int n = -1; /* trivial char */ if (*in <= 0x7F) { *c = *in; return 1; } /* find utf8 sequence length */ if ((*in & 0xE0) == 0xC0) n = 2; else if ((*in & 0xF0) == 0xE0) n = 3; else if ((*in & 0xF8) == 0xF0) n = 4; else if ((*in & 0xFC) == 0xF8) n = 5; else if ((*in & 0xFE) == 0xFC) n = 6; /* starved? */ if (n > inb) return -2; /* decode ... */ if (n > 1 && n < 5) { /* reject invalid sequences */ if (utf8seq_is_overlong(in, n) || utf8seq_is_surrogate(in, n) || utf8seq_is_illegal(in, n)) return -1; /* decode ... */ *c = (char)(*in++ & (0x7F >> n)); for (i = 1; i < n; i++) { /* illegal continuation byte */ if (*in < 0x80 || *in > 0xBF) return -1; *c = (*c << 6) | (*in++ & 0x3F); } return n; } /* unmapped sequence (> 4) */ return -1; } static inline wchar_t latin9_translit(wchar_t c) { /* a number of trivial iso-8859-15 <> utf-8 transliterations */ switch (c) { case 0x20AC: return 0xA4; /* Euro */ case 0x0160: return 0xA6; /* S caron */ case 0x0161: return 0xA8; /* s caron */ case 0x017D: return 0xB4; /* Z caron */ case 0x017E: return 0xB8; /* z caron */ case 0x0152: return 0xBC; /* OE */ case 0x0153: return 0xBD; /* oe */ case 0x0178: return 0xBE; /* Y diaeresis */ default: return 0xFFFD; /* cannot translate */ } } size_t iconv(iconv_t cd, char **in, size_t *inb, char **out, size_t *outb) { size_t x=0; unsigned char to = (cd>>1)&127; unsigned char from = 255; const unsigned char *map = 0; char tmp[MB_LEN_MAX]; wchar_t c, d; size_t k, l; int err; if (!in || !*in || !*inb) return 0; if (cd & 1) map = charmaps[cd>>8].map; else from = cd>>8; for (; *inb; *in+=l, *inb-=l) { c = *(unsigned char *)*in; l = 1; if (from >= UTF_8 && c < 0x80) goto charok; switch (from) { case WCHAR_T: l = sizeof(wchar_t); if (*inb < l) goto starved; c = *(wchar_t *)*in; break; case UTF_8: l = utf8dec_wchar(&c, *in, *inb); if (!l) l++; else if (l == (size_t)-1) goto ilseq; else if (l == (size_t)-2) goto starved; break; case US_ASCII: goto ilseq; case LATIN_9: if ((unsigned)c - 0xa4 <= 0xbe - 0xa4) { static const unsigned char map[] = { 0, 0x60, 0, 0x61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7d, 0, 0, 0, 0x7e, 0, 0, 0, 0x52, 0x53, 0x78 }; if (c == 0xa4) c = 0x20ac; else if (map[c-0xa5]) c = 0x100 | map[c-0xa5]; } case LATIN_1: goto charok; case TIS_620: if (c >= 0xa1) c += 0x0e01-0xa1; goto charok; case JIS_0201: if (c >= 0xa1) { if (c <= 0xdf) c += 0xff61-0xa1; else goto ilseq; } goto charok; case UTF_16BE: case UTF_16LE: l = 2; if (*inb < 2) goto starved; c = get_16(*in, from); if ((unsigned)(c-0xdc00) < 0x400) goto ilseq; if ((unsigned)(c-0xd800) < 0x400) { l = 4; if (*inb < 4) goto starved; d = get_16(*in + 2, from); if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq; c = ((c-0xd800)<<10) | (d-0xdc00); } break; case UTF_32BE: case UTF_32LE: l = 4; if (*inb < 4) goto starved; // FIXME // c = get_32(*in, from); break; default: /* only support ascii supersets */ if (c < 0x80) break; switch (map[0]) { case UCS2_8BIT: c -= 0x80; break; case EUC: if ((unsigned)c - 0xa1 >= 94) goto ilseq; if ((unsigned)in[0][1] - 0xa1 >= 94) goto ilseq; c = (c-0xa1)*94 + (in[0][1]-0xa1); l = 2; break; case SHIFT_JIS: if ((unsigned)c - 0xa1 <= 0xdf-0xa1) { c += 0xff61-0xa1; goto charok; } // FIXME... l = 2; break; default: goto badf; } c = get_16(map + 4 + 2*c, 0); if (c == 0xffff) goto ilseq; goto charok; } if ((unsigned)c - 0xd800 < 0x800 || (unsigned)c >= 0x110000) goto ilseq; charok: switch (to) { case WCHAR_T: if (*outb < sizeof(wchar_t)) goto toobig; *(wchar_t *)*out = c; *out += sizeof(wchar_t); *outb -= sizeof(wchar_t); break; case UTF_8: if (*outb < 4) { k = utf8enc_wchar(tmp, c); if (*outb < k) goto toobig; memcpy(*out, tmp, k); } else k = utf8enc_wchar(*out, c); *out += k; *outb -= k; break; case US_ASCII: if (c > 0x7f) c = 0xfffd; /* fall thru and count replacement in latin1 case */ case LATIN_9: if (c >= 0x100 && c != 0xfffd) c = latin9_translit(c); /* fall through */ case LATIN_1: if (c > 0xff) goto ilseq; if (!*outb) goto toobig; **out = c; ++*out; --*outb; break; case UTF_16BE: case UTF_16LE: if (c < 0x10000) { if (*outb < 2) goto toobig; put_16(*out, c, to); *out += 2; *outb -= 2; break; } if (*outb < 4) goto toobig; put_16(*out, (c>>10)|0xd800, to); put_16(*out + 2, (c&0x3ff)|0xdc00, to); *out += 4; *outb -= 4; break; default: goto badf; } } return x; ilseq: err = EILSEQ; x = -1; goto end; badf: err = EBADF; x = -1; goto end; toobig: err = E2BIG; x = -1; goto end; starved: err = EINVAL; end: errno = err; return x; }