aboutsummaryrefslogtreecommitdiffstats
path: root/passes/techmap/dffunmap.cc
blob: 8703bf1a03766f7db04299052eb532b69e12f804 (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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2020  Marcelina Kościelnicka <mwk@0x04.net>
 *
 *  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"
#include "kernel/ff.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct DffunmapPass : public Pass {
	DffunmapPass() : Pass("dffunmap", "unmap clock enable and synchronous reset from FFs") { }
	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    dffunmap [options] [selection]\n");
		log("\n");
		log("This pass transforms FF types with clock enable and/or synchronous reset into\n");
		log("their base type (with neither clock enable nor sync reset) by emulating the\n");
		log("clock enable and synchronous reset with multiplexers on the cell input.\n");
		log("\n");
		log("    -ce-only\n");
		log("        unmap only clock enables, leave synchronous resets alone.\n");
		log("\n");
		log("    -srst-only\n");
		log("        unmap only synchronous resets, leave clock enables alone.\n");
		log("\n");
	}
	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		log_header(design, "Executing DFFUNMAP pass (unmap clock enable and synchronous reset from FFs).\n");

		bool ce_only = false;
		bool srst_only = false;

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++) {
			if (args[argidx] == "-ce-only") {
				ce_only = true;
				continue;
			}
			if (args[argidx] == "-srst-only") {
				srst_only = true;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		if (ce_only && srst_only)
			log_cmd_error("Options -ce-only and -srst-only are mutually exclusive!\n");

		for (auto mod : design->selected_modules())
		{
			SigMap sigmap(mod);
			FfInitVals initvals(&sigmap, mod);

			for (auto cell : mod->selected_cells())
			{
				if (!RTLIL::builtin_ff_cell_types().count(cell->type))
					continue;

				FfData ff(&initvals, cell);
				IdString name = cell->name;

				if (!ff.has_clk)
					continue;

				if (ce_only) {
					if (!ff.has_ce)
						continue;
					ff.unmap_ce();
				} else if (srst_only) {
					if (!ff.has_srst)
						continue;
					ff.unmap_srst();
				} else {
					if (!ff.has_ce && !ff.has_srst)
						continue;
					ff.unmap_ce_srst();
				}

				ff.emit();
			}
		}
	}
} DffunmapPass;

PRIVATE_NAMESPACE_END