Merge pull request #1465 from YosysHQ/dave/ice40_timing_sim
[yosys.git] / passes / techmap / iopadmap.cc
index 4acbf7c0ddb73e96b1bc1983ba61589da9936263..c868b9a875e1c0aaf5eb23cf33a19661e95bd031 100644 (file)
@@ -34,7 +34,7 @@ void split_portname_pair(std::string &port1, std::string &port2)
 
 struct IopadmapPass : public Pass {
        IopadmapPass() : Pass("iopadmap", "technology mapping of i/o pads (or buffers)") { }
-       virtual void help()
+       void help() YS_OVERRIDE
        {
                log("\n");
                log("    iopadmap [options] [selection]\n");
@@ -64,6 +64,11 @@ struct IopadmapPass : public Pass {
                log("        of the tristate driver and the 2nd portname is the internal output\n");
                log("        buffering the external signal.\n");
                log("\n");
+               log("    -ignore <celltype> <portname>[:<portname>]*\n");
+               log("        Skips mapping inputs/outputs that are already connected to given\n");
+               log("        ports of the given cell.  Can be used multiple times.  This is in\n");
+               log("        addition to the cells specified as mapping targets.\n");
+               log("\n");
                log("    -widthparam <param_name>\n");
                log("        Use the specified parameter name to set the port width.\n");
                log("\n");
@@ -78,7 +83,7 @@ struct IopadmapPass : public Pass {
                log("Tristate PADS (-toutpad, -tinoutpad) always operate in -bits mode.\n");
                log("\n");
        }
-       virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
+       void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
        {
                log_header(design, "Executing IOPADMAP pass (mapping inputs/outputs to IO-PAD cells).\n");
 
@@ -88,6 +93,7 @@ struct IopadmapPass : public Pass {
                std::string toutpad_celltype, toutpad_portname, toutpad_portname2, toutpad_portname3;
                std::string tinoutpad_celltype, tinoutpad_portname, tinoutpad_portname2, tinoutpad_portname3, tinoutpad_portname4;
                std::string widthparam, nameparam;
+               pool<pair<IdString, IdString>> ignore;
                bool flag_bits = false;
 
                size_t argidx;
@@ -127,6 +133,18 @@ struct IopadmapPass : public Pass {
                                split_portname_pair(tinoutpad_portname3, tinoutpad_portname4);
                                continue;
                        }
+                       if (arg == "-ignore" && argidx+2 < args.size()) {
+                               std::string ignore_celltype = args[++argidx];
+                               std::string ignore_portname = args[++argidx];
+                               std::string ignore_portname2;
+                               while (!ignore_portname.empty()) {
+                                       split_portname_pair(ignore_portname, ignore_portname2);
+                                       ignore.insert(make_pair(RTLIL::escape_id(ignore_celltype), RTLIL::escape_id(ignore_portname)));
+
+                                       ignore_portname = ignore_portname2;
+                               }
+                               continue;
+                       }
                        if (arg == "-widthparam" && argidx+1 < args.size()) {
                                widthparam = args[++argidx];
                                continue;
@@ -143,18 +161,44 @@ struct IopadmapPass : public Pass {
                }
                extra_args(args, argidx, design);
 
+               if (!inpad_portname2.empty())
+                       ignore.insert(make_pair(RTLIL::escape_id(inpad_celltype), RTLIL::escape_id(inpad_portname2)));
+               if (!outpad_portname2.empty())
+                       ignore.insert(make_pair(RTLIL::escape_id(outpad_celltype), RTLIL::escape_id(outpad_portname2)));
+               if (!inoutpad_portname2.empty())
+                       ignore.insert(make_pair(RTLIL::escape_id(inoutpad_celltype), RTLIL::escape_id(inoutpad_portname2)));
+               if (!toutpad_portname3.empty())
+                       ignore.insert(make_pair(RTLIL::escape_id(toutpad_celltype), RTLIL::escape_id(toutpad_portname3)));
+               if (!tinoutpad_portname4.empty())
+                       ignore.insert(make_pair(RTLIL::escape_id(tinoutpad_celltype), RTLIL::escape_id(tinoutpad_portname4)));
+
+               for (auto module : design->modules())
+                       if (module->get_blackbox_attribute())
+                               for (auto wire : module->wires())
+                                       if (wire->get_bool_attribute("\\iopad_external_pin"))
+                                               ignore.insert(make_pair(module->name, wire->name));
+
                for (auto module : design->selected_modules())
                {
                        dict<IdString, pool<int>> skip_wires;
+                       pool<SigBit> skip_wire_bits;
+                       SigMap sigmap(module);
+
+                       for (auto cell : module->cells())
+                       for (auto port : cell->connections())
+                               if (ignore.count(make_pair(cell->type, port.first)))
+                                       for (auto bit : sigmap(port.second))
+                                               skip_wire_bits.insert(bit);
 
                        if (!toutpad_celltype.empty() || !tinoutpad_celltype.empty())
                        {
-                               SigMap sigmap(module);
                                dict<SigBit, pair<IdString, pool<IdString>>> tbuf_bits;
+                               pool<pair<IdString, IdString>> norewrites;
+                               SigMap rewrites;
 
                                for (auto cell : module->cells())
-                                       if (cell->type == "$_TBUF_") {
-                                               SigBit bit = sigmap(cell->getPort("\\Y").as_bit());
+                                       if (cell->type == ID($_TBUF_)) {
+                                               SigBit bit = sigmap(cell->getPort(ID::Y).as_bit());
                                                tbuf_bits[bit].first = cell->name;
                                        }
 
@@ -177,14 +221,17 @@ struct IopadmapPass : public Pass {
                                                if (tbuf_bits.count(mapped_wire_bit) == 0)
                                                        continue;
 
+                                               if (skip_wire_bits.count(mapped_wire_bit))
+                                                       continue;
+
                                                auto &tbuf_cache = tbuf_bits.at(mapped_wire_bit);
                                                Cell *tbuf_cell = module->cell(tbuf_cache.first);
 
                                                if (tbuf_cell == nullptr)
                                                        continue;
 
-                                               SigBit en_sig = tbuf_cell->getPort("\\E").as_bit();
-                                               SigBit data_sig = tbuf_cell->getPort("\\A").as_bit();
+                                               SigBit en_sig = tbuf_cell->getPort(ID(E)).as_bit();
+                                               SigBit data_sig = tbuf_cell->getPort(ID::A).as_bit();
 
                                                if (wire->port_input && !tinoutpad_celltype.empty())
                                                {
@@ -197,7 +244,7 @@ struct IopadmapPass : public Pass {
                                                        cell->setPort(RTLIL::escape_id(tinoutpad_portname2), owire);
                                                        cell->setPort(RTLIL::escape_id(tinoutpad_portname3), data_sig);
                                                        cell->setPort(RTLIL::escape_id(tinoutpad_portname4), wire_bit);
-                                                       cell->attributes["\\keep"] = RTLIL::Const(1);
+                                                       cell->attributes[ID::keep] = RTLIL::Const(1);
 
                                                        for (auto cn : tbuf_cache.second) {
                                                                auto c = module->cell(cn);
@@ -219,6 +266,9 @@ struct IopadmapPass : public Pass {
 
                                                        module->remove(tbuf_cell);
                                                        skip_wires[wire->name].insert(i);
+
+                                                       norewrites.insert(make_pair(cell->name, RTLIL::escape_id(tinoutpad_portname4)));
+                                                       rewrites.add(sigmap(wire_bit), owire);
                                                        continue;
                                                }
 
@@ -231,7 +281,7 @@ struct IopadmapPass : public Pass {
                                                        cell->setPort(RTLIL::escape_id(toutpad_portname), en_sig);
                                                        cell->setPort(RTLIL::escape_id(toutpad_portname2), data_sig);
                                                        cell->setPort(RTLIL::escape_id(toutpad_portname3), wire_bit);
-                                                       cell->attributes["\\keep"] = RTLIL::Const(1);
+                                                       cell->attributes[ID::keep] = RTLIL::Const(1);
 
                                                        for (auto cn : tbuf_cache.second) {
                                                                auto c = module->cell(cn);
@@ -256,6 +306,22 @@ struct IopadmapPass : public Pass {
                                                }
                                        }
                                }
+
+                               if (GetSize(norewrites))
+                               {
+                                       for (auto cell : module->cells())
+                                       for (auto port : cell->connections())
+                                       {
+                                               if (norewrites.count(make_pair(cell->name, port.first)))
+                                                       continue;
+
+                                               SigSpec orig_sig = sigmap(port.second);
+                                               SigSpec new_sig = rewrites(orig_sig);
+
+                                               if (orig_sig != new_sig)
+                                                       cell->setPort(port.first, new_sig);
+                                       }
+                               }
                        }
 
                        for (auto wire : module->selected_wires())
@@ -272,6 +338,13 @@ struct IopadmapPass : public Pass {
                                        skip_bit_indices = skip_wires.at(wire->name);
                                }
 
+                               for (int i = 0; i < GetSize(wire); i++)
+                                       if (skip_wire_bits.count(sigmap(SigBit(wire, i))))
+                                               skip_bit_indices.insert(i);
+
+                               if (GetSize(wire) == GetSize(skip_bit_indices))
+                                       continue;
+
                                if (wire->port_input && !wire->port_output) {
                                        if (inpad_celltype.empty()) {
                                                log("Don't map input port %s.%s: Missing option -inpad.\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(wire->name));
@@ -335,7 +408,7 @@ struct IopadmapPass : public Pass {
                                                        cell->parameters[RTLIL::escape_id(widthparam)] = RTLIL::Const(1);
                                                if (!nameparam.empty())
                                                        cell->parameters[RTLIL::escape_id(nameparam)] = RTLIL::Const(stringf("%s[%d]", RTLIL::id2cstr(wire->name), i));
-                                               cell->attributes["\\keep"] = RTLIL::Const(1);
+                                               cell->attributes[ID::keep] = RTLIL::Const(1);
                                        }
                                }
                                else
@@ -348,7 +421,7 @@ struct IopadmapPass : public Pass {
                                                cell->parameters[RTLIL::escape_id(widthparam)] = RTLIL::Const(wire->width);
                                        if (!nameparam.empty())
                                                cell->parameters[RTLIL::escape_id(nameparam)] = RTLIL::Const(RTLIL::id2cstr(wire->name));
-                                       cell->attributes["\\keep"] = RTLIL::Const(1);
+                                       cell->attributes[ID::keep] = RTLIL::Const(1);
                                }
 
                                wire->port_id = 0;