json: Fix handling of offsets in backend
Signed-off-by: gatecat <gatecat@ds0.me>
This commit is contained in:
parent
79c6840fef
commit
9994ba1d19
@ -60,8 +60,10 @@ void write_parameters(std::ostream &f, Context *ctx, const dict<IdString, Proper
|
|||||||
struct PortGroup
|
struct PortGroup
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
|
std::vector<std::pair<int, int>> grouped_bits; // (index, bit)
|
||||||
std::vector<int> bits;
|
std::vector<int> bits;
|
||||||
PortType dir;
|
PortType dir;
|
||||||
|
int offset = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo> &ports, bool is_cell = false)
|
std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo> &ports, bool is_cell = false)
|
||||||
@ -71,9 +73,11 @@ std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo>
|
|||||||
for (auto &pair : ports) {
|
for (auto &pair : ports) {
|
||||||
std::string name = pair.second.name.str(ctx);
|
std::string name = pair.second.name.str(ctx);
|
||||||
if ((name.back() != ']') || (name.find('[') == std::string::npos)) {
|
if ((name.back() != ']') || (name.find('[') == std::string::npos)) {
|
||||||
groups.push_back({name,
|
groups.push_back(
|
||||||
{is_cell ? (pair.second.net ? pair.second.net->name.index : -1) : pair.first.index},
|
{name,
|
||||||
pair.second.type});
|
{{0, (is_cell ? (pair.second.net ? pair.second.net->name.index : -1) : pair.first.index)}},
|
||||||
|
{},
|
||||||
|
pair.second.type});
|
||||||
} else {
|
} else {
|
||||||
int off1 = int(name.find_last_of('['));
|
int off1 = int(name.find_last_of('['));
|
||||||
std::string basename = name.substr(0, off1);
|
std::string basename = name.substr(0, off1);
|
||||||
@ -81,14 +85,24 @@ std::vector<PortGroup> group_ports(Context *ctx, const dict<IdString, PortInfo>
|
|||||||
|
|
||||||
if (!base_to_group.count(basename)) {
|
if (!base_to_group.count(basename)) {
|
||||||
base_to_group[basename] = groups.size();
|
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]);
|
auto &grp = groups.at(base_to_group[basename]);
|
||||||
if (int(grp.bits.size()) <= index)
|
grp.grouped_bits.emplace_back(index, pair.second.net ? pair.second.net->name.index
|
||||||
grp.bits.resize(index + 1, -1);
|
: (is_cell ? -1 : pair.first.index));
|
||||||
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);
|
}
|
||||||
|
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;
|
return groups;
|
||||||
@ -137,6 +151,9 @@ void write_module(std::ostream &f, Context *ctx)
|
|||||||
f << stringf(" \"direction\": \"%s\",\n", port.dir == PORT_IN ? "input"
|
f << stringf(" \"direction\": \"%s\",\n", port.dir == PORT_IN ? "input"
|
||||||
: port.dir == PORT_INOUT ? "inout"
|
: port.dir == PORT_INOUT ? "inout"
|
||||||
: "output");
|
: "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(" \"bits\": %s\n", format_port_bits(port, dummy_idx).c_str());
|
||||||
f << stringf(" }");
|
f << stringf(" }");
|
||||||
first = false;
|
first = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user