diff --git a/json/jsonwrite.cc b/json/jsonwrite.cc index c35df4ae0b..b7294cb38a 100644 --- a/json/jsonwrite.cc +++ b/json/jsonwrite.cc @@ -60,8 +60,10 @@ void write_parameters(std::ostream &f, Context *ctx, const dict> grouped_bits; // (index, bit) std::vector bits; PortType dir; + int offset = 0; }; std::vector group_ports(Context *ctx, const dict &ports, bool is_cell = false) @@ -71,9 +73,11 @@ std::vector group_ports(Context *ctx, const dict for (auto &pair : ports) { std::string name = pair.second.name.str(ctx); if ((name.back() != ']') || (name.find('[') == std::string::npos)) { - groups.push_back({name, - {is_cell ? (pair.second.net ? pair.second.net->name.index : -1) : pair.first.index}, - pair.second.type}); + groups.push_back( + {name, + {{0, (is_cell ? (pair.second.net ? pair.second.net->name.index : -1) : pair.first.index)}}, + {}, + pair.second.type}); } else { int off1 = int(name.find_last_of('[')); std::string basename = name.substr(0, off1); @@ -81,14 +85,24 @@ std::vector group_ports(Context *ctx, const dict if (!base_to_group.count(basename)) { base_to_group[basename] = groups.size(); - groups.push_back({basename, std::vector(index + 1, -1), pair.second.type}); + groups.push_back({basename, {}, {}, pair.second.type}); } auto &grp = groups.at(base_to_group[basename]); - if (int(grp.bits.size()) <= index) - grp.bits.resize(index + 1, -1); - NPNR_ASSERT(grp.bits.at(index) == -1); - grp.bits.at(index) = pair.second.net ? pair.second.net->name.index : (is_cell ? -1 : pair.first.index); + grp.grouped_bits.emplace_back(index, pair.second.net ? pair.second.net->name.index + : (is_cell ? -1 : pair.first.index)); + } + } + for (auto &group : groups) { + NPNR_ASSERT(!group.grouped_bits.empty()); + // find offset + group.offset = std::min_element(group.grouped_bits.begin(), group.grouped_bits.end())->first; + for (auto bit : group.grouped_bits) { + int vec_idx = bit.first - group.offset; + if (vec_idx >= int(group.bits.size())) + group.bits.resize(vec_idx + 1, -1); + NPNR_ASSERT(group.bits.at(vec_idx) == -1); + group.bits.at(vec_idx) = bit.second; } } return groups; @@ -137,6 +151,9 @@ void write_module(std::ostream &f, Context *ctx) f << stringf(" \"direction\": \"%s\",\n", port.dir == PORT_IN ? "input" : port.dir == PORT_INOUT ? "inout" : "output"); + if (port.offset != 0) { + f << stringf(" \"offset\": %d,\n", port.offset); + } f << stringf(" \"bits\": %s\n", format_port_bits(port, dummy_idx).c_str()); f << stringf(" }"); first = false;