File size: 3,580 Bytes
107d124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2025  King Lok Chung <king.chung@manchester.ac.uk>
 *
 *  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/rtlil.h"
#include "kernel/log.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct ConstmapWorker
{
	RTLIL::Module *module;
	IdString celltype_ref, cell_portname_ref, cell_paramname_ref;

	void operator()(RTLIL::SigSpec &sig)
	{
		if (sig.is_fully_const()){
			RTLIL::SigChunk value = module->addWire(NEW_ID, sig.size());
			RTLIL::Cell *cell = module->addCell(NEW_ID, celltype_ref);
			cell->setParam(cell_paramname_ref, sig.as_const());
			cell->setPort(cell_portname_ref, value);
			sig = value;
		}
	}
};

struct ConstmapPass : public Pass {
	ConstmapPass() : Pass("constmap", "technology mapping of coarse constant value") { }
	void help() override
	{
		log("\n");
		log("    constmap [options] [selection]\n");
		log("\n");
		log("Map constants to a driver cell.\n");
		log("\n");
		log("    -cell <celltype> <portname> <paramname>\n");
		log("        Replace constant bits with this cell.\n");
		log("        The value of the constant will be stored to the parameter specified.\n");
		log("\n");
	}
	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		log_header(design, "Executing CONSTMAP pass (mapping to constant driver).\n");

		std::string celltype, cell_portname, cell_paramname;

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			if (args[argidx] == "-cell" && argidx+3 < args.size()){
				celltype = RTLIL::escape_id(args[++argidx]);
				cell_portname = RTLIL::escape_id(args[++argidx]);
				cell_paramname = RTLIL::escape_id(args[++argidx]);
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		if (celltype.empty())
			log_cmd_error("Missing required option -cell.\n");

		IdString celltype_ref = design->twines().add(celltype);
		IdString cell_portname_ref = design->twines().add(cell_portname);
		IdString cell_paramname_ref = design->twines().add(cell_paramname);

		if (design->has(celltype_ref)) {
			Module *existing = design->module(celltype_ref);
			bool has_port = false;
			for (auto &p : existing->ports){
				if (p == cell_portname_ref){
					has_port = true;
					break;
				}
			}
			if (!has_port)
				log_cmd_error("Cell type '%s' does not have port '%s'.\n", celltype, cell_portname);

			bool has_param = false;
			for (auto &p : existing->avail_parameters){
				if (p == cell_paramname_ref)
					has_param = true;
			}

			if (!has_param)
				log_cmd_error("Cell type '%s' does not have parameter '%s'.\n", celltype, cell_paramname);
		}


		for (auto mod : design->selected_modules())
		{
			ConstmapWorker worker{mod, celltype_ref, cell_portname_ref, cell_paramname_ref};
			mod->rewrite_sigspecs(worker);
		}
	}
} ConstmapPass;

PRIVATE_NAMESPACE_END