aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/ecp5/ecp5_gsr.cc
blob: 2bc714b6fee6815954e7d7f82cfb2a055cbe0278 (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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *  Copyright (C) 2019  David Shah <david@symbioticeda.com>
 *
 *  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/yosys.h"
#include "kernel/sigtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct Ecp5GsrPass : public Pass {
	Ecp5GsrPass() : Pass("ecp5_gsr", "ECP5: handle GSR") { }
	void help() YS_OVERRIDE
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    ecp5_gsr [options] [selection]\n");
		log("\n");
		log("Trim active low async resets connected to GSR and resolve GSR parameter,\n");
		log("if a GSR or SGSR primitive is used in the design.\n");
		log("\n");
		log("If any cell has the GSR parameter set to \"AUTO\", this will be resolved\n");
		log("to \"ENABLED\" if a GSR primitive is present and the (* nogsr *) attribute\n");
		log("is not set, otherwise it will be resolved to \"DISABLED\".\n");
		log("\n");
	}
	void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
	{
		log_header(design, "Executing ECP5_GSR pass (implement FF init values).\n");

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			// if (args[argidx] == "-singleton") {
			// 	singleton_mode = true;
			// 	continue;
			// }
			break;
		}
		extra_args(args, argidx, design);

		for (auto module : design->selected_modules())
		{
			log("Handling GSR in %s.\n", log_id(module));

			SigMap sigmap(module);

			SigBit gsr;
			bool found_gsr = false;

			for (auto cell : module->selected_cells())
			{
				if (cell->type != ID(GSR) && cell->type != ID(SGSR))
					continue;
				if (found_gsr)
					log_error("Found more than one GSR or SGSR cell in module %s.\n", log_id(module));
				found_gsr = true;
				SigSpec sig_gsr = cell->getPort(ID(GSR));
				if (GetSize(sig_gsr) < 1)
					log_error("GSR cell %s has disconnected GSR input.\n", log_id(cell));
				gsr = sigmap(sig_gsr[0]);
			}

			// Resolve GSR parameter

			for (auto cell : module->selected_cells())
			{
				if (!cell->hasParam(ID(GSR)) || cell->getParam(ID(GSR)).decode_string() != "AUTO")
					continue;
				
				bool gsren = found_gsr;
				if (cell->get_bool_attribute("\\nogsr"))
					gsren = false;
				cell->setParam(ID(GSR), gsren ? Const("ENABLED") : Const("DISABLED"));
				
			}

			if (!found_gsr)
				continue;

			// For finding active low FF inputs
			pool<SigBit> inverted_gsr;

			log_debug("GSR net in module %s is %s.\n", log_id(module), log_signal(gsr));
			for (auto cell : module->selected_cells())
			{
				if (cell->type != ID($_NOT_))
					continue;
				SigSpec sig_a = cell->getPort(ID(A)), sig_y = cell->getPort(ID(Y));
				if (GetSize(sig_a) < 1 || GetSize(sig_y) < 1)
					continue;
				SigBit a = sigmap(sig_a[0]);
				if (a == gsr)
					inverted_gsr.insert(sigmap(sig_y[0]));
			}

			for (auto cell : module->selected_cells())
			{
				if (cell->type != ID(TRELLIS_FF))
					continue;
				if (!cell->hasParam(ID(GSR)) || cell->getParam(ID(GSR)).decode_string() != "ENABLED")
					continue;
				if (!cell->hasParam(ID(SRMODE)) || cell->getParam(ID(SRMODE)).decode_string() != "ASYNC")
					continue;
				SigSpec sig_lsr = cell->getPort(ID(LSR));
				if (GetSize(sig_lsr) < 1)
					continue;
				SigBit lsr = sigmap(sig_lsr[0]);
				if (!inverted_gsr.count(lsr))
					continue;
				cell->setParam(ID(SRMODE), Const("LSR_OVER_CE"));
				cell->unsetPort(ID(LSR));
			}

		}
	}
} Ecp5GsrPass;

PRIVATE_NAMESPACE_END