Skip to content

Commit

Permalink
json: Fix handling of offsets in backend
Browse files Browse the repository at this point in the history
Signed-off-by: gatecat <[email protected]>
  • Loading branch information
gatecat committed Sep 4, 2023
1 parent 79c6840 commit 269923d
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions json/jsonwrite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ void write_parameters(std::ostream &f, Context *ctx, const dict<IdString, Proper
struct PortGroup
{
std::string name;
std::vector<std::pair<int, int>> grouped_bits; // (index, bit)
std::vector<int> bits;
PortType dir;
int offset = 0;
};

std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo> &ports, bool is_cell = false)
Expand All @@ -71,24 +73,36 @@ std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo>
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);
int index = std::stoi(name.substr(off1 + 1, name.size() - (off1 + 2)));

if (!base_to_group.count(basename)) {
base_to_group[basename] = groups.size();
groups.push_back({basename, std::vector<int>(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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 269923d

Please sign in to comment.