1 #include "DedupChipdb.hpp"
2 #include "Chip.hpp"
3 
4 namespace Trellis {
5 namespace DDChipDb {
6 
checksum() const7 checksum_t LocationData::checksum() const
8 {
9     pair<uint64_t, uint64_t> cs = make_pair(0, 0);
10     const uint64_t magic1 = 0x9e3779b97f4a7c15ULL;
11     const uint64_t magic2 = 0xf476452575661fbeULL;
12     for (const auto &wire : wires) {
13         cs.first = magic1 + std::hash<WireData>()(wire) + (cs.first << 12UL) + (cs.second >> 2UL);
14         cs.second = magic2 + std::hash<WireData>()(wire) + (cs.second << 17UL) + (cs.first >> 1UL);
15     }
16     for (const auto &bel : bels) {
17         cs.first = magic1 + std::hash<BelData>()(bel) + (cs.first << 12UL) + (cs.second >> 2UL);
18         cs.second = magic2 + std::hash<BelData>()(bel) + (cs.second << 17UL) + (cs.first >> 1UL);
19     }
20     for (const auto &arc : arcs) {
21         cs.first = magic1 + std::hash<DdArcData>()(arc) + (cs.first << 12UL) + (cs.second >> 2UL);
22         cs.second = magic2 + std::hash<DdArcData>()(arc) + (cs.second << 17UL) + (cs.first >> 1UL);
23     }
24     return cs;
25 }
26 
DedupChipdb()27 DedupChipdb::DedupChipdb()
28 {
29 
30 }
31 
DedupChipdb(const IdStore & base)32 DedupChipdb::DedupChipdb(const IdStore &base) : IdStore(base)
33 {}
34 
make_dedup_chipdb(Chip & chip)35 shared_ptr<DedupChipdb> make_dedup_chipdb(Chip &chip)
36 {
37     shared_ptr<RoutingGraph> graph = chip.get_routing_graph();
38     for (auto &loc : graph->tiles) {
39         const auto &td = loc.second;
40         // Index bels, wires and arcs
41         int bel_id = 0, wire_id = 0, arc_id = 0;
42         for (auto &bel : td.bels) {
43             RoutingId rid;
44             rid.loc = loc.first;
45             rid.id = bel.first;
46             bel.second.cdb_id = bel_id++;
47         }
48         for (auto &wire : td.wires) {
49             RoutingId rid;
50             rid.loc = loc.first;
51             rid.id = wire.first;
52             wire.second.cdb_id  = wire_id++;
53         }
54         for (auto &arc : td.arcs) {
55             RoutingId rid;
56             rid.loc = loc.first;
57             rid.id = arc.first;
58             arc.second.cdb_id = arc_id++;
59         }
60     }
61     shared_ptr<DedupChipdb> cdb = make_shared<DedupChipdb>(IdStore(*graph));
62     for (const auto &loc : graph->tiles) {
63         int x = loc.first.x, y = loc.first.y;
64         LocationData ld;
65         const auto &td = loc.second;
66         for (const auto &bel : td.bels) {
67             const RoutingBel &rb = bel.second;
68             BelData bd;
69             bd.name = rb.name;
70             bd.type = rb.type;
71             bd.z = rb.z;
72             for (const auto &wire : rb.pins) {
73                 BelWire bw;
74                 bw.pin = wire.first;
75                 bw.wire = RelId{Location(wire.second.first.loc.x - x, wire.second.first.loc.y - y), graph->tiles.at(wire.second.first.loc).wires.at(wire.second.first.id).cdb_id};
76                 bw.dir = wire.second.second;
77                 bd.wires.push_back(bw);
78             }
79             ld.bels.push_back(bd);
80         }
81 
82         for (const auto &arc : td.arcs) {
83             const RoutingArc &ra = arc.second;
84             DdArcData ad;
85             ad.tiletype = ra.tiletype;
86             ad.cls = ra.configurable ? ARC_STANDARD : ARC_FIXED;
87             ad.delay = 1;
88             ad.sinkWire = RelId{Location(ra.sink.loc.x - x, ra.sink.loc.y - y), graph->tiles.at(ra.sink.loc).wires.at(ra.sink.id).cdb_id};
89             ad.srcWire = RelId{Location(ra.source.loc.x - x, ra.source.loc.y - y), graph->tiles.at(ra.source.loc).wires.at(ra.source.id).cdb_id};
90             ld.arcs.push_back(ad);
91         }
92 
93         for (const auto &wire : td.wires) {
94             const RoutingWire &rw = wire.second;
95             WireData wd;
96             wd.name = rw.id;
97             for (const auto &dh : rw.downhill)
98                 wd.arcsDownhill.insert(RelId{Location(dh.loc.x - x, dh.loc.y - y), graph->tiles.at(dh.loc).arcs.at(dh.id).cdb_id});
99             for (const auto &uh : rw.uphill)
100                 wd.arcsUphill.insert(RelId{Location(uh.loc.x - x, uh.loc.y - y), graph->tiles.at(uh.loc).arcs.at(uh.id).cdb_id});
101             for (const auto &bdh : rw.belsDownhill) {
102                 BelPort bp;
103                 bp.pin = bdh.second;
104                 bp.bel = RelId{Location(bdh.first.loc.x - x, bdh.first.loc.y - y), graph->tiles.at(bdh.first.loc).bels.at(bdh.first.id).cdb_id};
105                 wd.belPins.push_back(bp);
106             }
107             assert(rw.belsUphill.size() <= 1);
108             if (rw.belsUphill.size() == 1) {
109                 const auto &buh = rw.belsUphill[0];
110                 BelPort uh;
111                 uh.bel = RelId{Location(buh.first.loc.x - x, buh.first.loc.y - y), graph->tiles.at(buh.first.loc).bels.at(buh.first.id).cdb_id};
112                 uh.pin = buh.second;
113                 wd.belPins.push_back(uh);
114             }
115             ld.wires.push_back(wd);
116         }
117 
118         checksum_t cs = ld.checksum();
119         if (cdb->locationTypes.find(cs) == cdb->locationTypes.end()) {
120             cdb->locationTypes[cs] = ld;
121         } else {
122             if (!(ld == cdb->locationTypes[cs]))
123                 terminate();
124         }
125         cdb->typeAtLocation[loc.first] = cs;
126     }
127 
128     return cdb;
129 }
130 
get_cs_data(checksum_t id)131 LocationData DedupChipdb::get_cs_data(checksum_t id) {
132     return locationTypes.at(id);
133 }
134 
135 }
136 }
137