File: ql_bram_types.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 (165 lines) | stat: -rw-r--r-- 5,624 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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2023  N. Engelhardt <nak@yosyshq.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/log.h"
#include "kernel/register.h"
#include "kernel/rtlil.h"
#include "kernel/sigtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

// ============================================================================


struct QlBramTypesPass : public Pass {
	
	QlBramTypesPass() : Pass("ql_bram_types", "Change TDP36K type to subtypes") {}

	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    ql_bram_types [selection]\n");
		log("\n");
		log("    This pass changes the type of TDP36K cells to different types based on the\n");
		log("    configuration of the cell.\n");
		log("\n");
	}

	int width_for_mode(int mode){
		// 1: mode = 3'b101;
		// 2: mode = 3'b110;
		// 4: mode = 3'b100;
		// 8,9: mode = 3'b001;
		// 16, 18: mode = 3'b010;
		// 32, 36: mode = 3'b011;
		switch (mode)
		{
		case 1:
			return 9;
		case 2:
			return 18;
		case 3:
			return 36;
		case 4:
			return 4;
		case 5:
			return 1;
		case 6:
			return 2;
		default:
			log_error("Invalid mode: %x", mode);
		}
	}

	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		log_header(design, "Executing QL_BRAM_TYPES pass.\n");

		size_t argidx = 1;
		extra_args(args, argidx, design);

		for (RTLIL::Module* module : design->selected_modules())
			for (RTLIL::Cell* cell: module->selected_cells())
			{
				if (cell->type != ID(TDP36K) || !cell->hasParam(ID(MODE_BITS)))
					continue;
				
				RTLIL::Const mode_bits = cell->getParam(ID(MODE_BITS));

				bool split = mode_bits.extract(80).as_bool();

				bool FMODE1_i = mode_bits.extract(13).as_bool();
				bool FMODE2_i = mode_bits.extract(54).as_bool();
				if (FMODE1_i != FMODE2_i) {
					log_debug("Can't change type of mixed use TDP36K block: FMODE1_i = %s, FMODE2_i = %s\n", FMODE1_i ? "true" : "false", FMODE2_i ? "true" : "false");
					continue;
				}
				bool is_fifo = FMODE1_i;

				bool SYNC_FIFO1_i = mode_bits.extract(0).as_bool();
				bool SYNC_FIFO2_i = mode_bits.extract(41).as_bool();
				if (SYNC_FIFO1_i != SYNC_FIFO2_i) {
					log_debug("Can't change type of mixed use TDP36K block: SYNC_FIFO1_i = %s, SYNC_FIFO2_i = %s\n", SYNC_FIFO1_i ? "true" : "false", SYNC_FIFO2_i ? "true" : "false");
					continue;
				}
				bool sync_fifo = SYNC_FIFO1_i;

				int RMODE_A1_i = mode_bits.extract(1, 3).as_int();
				int RMODE_B1_i = mode_bits.extract(4, 3).as_int();
				int WMODE_A1_i = mode_bits.extract(7, 3).as_int();
				int WMODE_B1_i = mode_bits.extract(10, 3).as_int();

				int RMODE_A2_i = mode_bits.extract(42, 3).as_int();
				int RMODE_B2_i = mode_bits.extract(45, 3).as_int();
				int WMODE_A2_i = mode_bits.extract(48, 3).as_int();
				int WMODE_B2_i = mode_bits.extract(51, 3).as_int();

				// TODO: should these be a warning or an error?
				if (RMODE_A1_i != WMODE_A1_i) {
					log_warning("Can't change type of misconfigured TDP36K block: Port A1 configured with read width = %d different from write width = %d\n", width_for_mode(RMODE_A1_i), width_for_mode(WMODE_A1_i));
					continue;
				}
				if (RMODE_B1_i != WMODE_B1_i) {
					log_warning("Can't change type of misconfigured TDP36K block: Port B1 configured with read width = %d different from write width = %d\n", width_for_mode(RMODE_B1_i), width_for_mode(WMODE_B1_i));
					continue;
				}
				if (RMODE_A2_i != WMODE_A2_i) {
					log_warning("Can't change type of misconfigured TDP36K block: Port A2 configured with read width = %d different from write width = %d\n", width_for_mode(RMODE_A2_i), width_for_mode(WMODE_A2_i));
					continue;
				}
				if (RMODE_B2_i != WMODE_B2_i) {
					log_warning("Can't change type of misconfigured TDP36K block: Port B2 configured with read width = %d different from write width = %d\n", width_for_mode(RMODE_B2_i), width_for_mode(WMODE_B2_i));
					continue;
				}

				// TODO: For nonsplit blocks, should RMODE_A1_i == RMODE_A2_i etc be checked/enforced?

				std::string type = "TDP36K";
				if (is_fifo) {
					type += "_FIFO_";
					if (sync_fifo)
						type += "SYNC_";
					else
						type += "ASYNC_";
				} else 
					type += "_BRAM_";

				if (split) {
					type += stringf("A1_X%d_", width_for_mode(RMODE_A1_i));
					type += stringf("B1_X%d_", width_for_mode(RMODE_B1_i));
					type += stringf("A2_X%d_", width_for_mode(RMODE_A2_i));
					type += stringf("B2_X%d_", width_for_mode(RMODE_B2_i));
					type += "split";
				} else {
					type += stringf("A_X%d_", width_for_mode(RMODE_A1_i));
					type += stringf("B_X%d_", width_for_mode(RMODE_B1_i));
					type += "nonsplit";
				}

				cell->type = RTLIL::escape_id(type);
				log_debug("Changed type of memory cell %s to %s\n", log_id(cell->name), log_id(cell->type));
			}
	}


} QlBramMergePass;

PRIVATE_NAMESPACE_END