kernel/mem: Add a Mem::narrow helper to split up wide ports.
authorMarcelina Kościelnicka <mwk@0x04.net>
Mon, 24 May 2021 22:58:17 +0000 (00:58 +0200)
committerMarcelina Kościelnicka <mwk@0x04.net>
Tue, 25 May 2021 00:07:25 +0000 (02:07 +0200)
kernel/mem.cc
kernel/mem.h

index de8ae8b1be40b0bee6bfb8df55787a7b2943f160..f2c8dd953b0b2cb204ece34a4e88a58201e7bacb 100644 (file)
@@ -548,3 +548,54 @@ Cell *Mem::extract_rdff(int idx, FfInitVals *initvals) {
 
        return c;
 }
+
+void Mem::narrow() {
+       std::vector<MemRd> new_rd_ports;
+       std::vector<MemWr> new_wr_ports;
+       std::vector<std::pair<int, int>> new_rd_map;
+       std::vector<std::pair<int, int>> new_wr_map;
+       for (int i = 0; i < GetSize(rd_ports); i++) {
+               auto &port = rd_ports[i];
+               for (int sub = 0; sub < (1 << port.wide_log2); sub++) {
+                       new_rd_map.push_back(std::make_pair(i, sub));
+               }
+       }
+       for (int i = 0; i < GetSize(wr_ports); i++) {
+               auto &port = wr_ports[i];
+               for (int sub = 0; sub < (1 << port.wide_log2); sub++) {
+                       new_wr_map.push_back(std::make_pair(i, sub));
+               }
+       }
+       for (auto &it : new_rd_map) {
+               MemRd &orig = rd_ports[it.first];
+               MemRd port = orig;
+               if (it.second != 0)
+                       port.cell = nullptr;
+               if (port.wide_log2) {
+                       port.data = port.data.extract(it.second * width, width);
+                       for (int i = 0; i < port.wide_log2; i++)
+                               port.addr[i] = State(it.second >> i & 1);
+                       port.wide_log2 = 0;
+               }
+               new_rd_ports.push_back(port);
+       }
+       for (auto &it : new_wr_map) {
+               MemWr &orig = wr_ports[it.first];
+               MemWr port = orig;
+               if (it.second != 0)
+                       port.cell = nullptr;
+               if (port.wide_log2) {
+                       port.data = port.data.extract(it.second * width, width);
+                       port.en = port.en.extract(it.second * width, width);
+                       for (int i = 0; i < port.wide_log2; i++)
+                               port.addr[i] = State(it.second >> i & 1);
+                       port.wide_log2 = 0;
+               }
+               port.priority_mask.clear();
+               for (auto &it2 : new_wr_map)
+                       port.priority_mask.push_back(orig.priority_mask[it2.first]);
+               new_wr_ports.push_back(port);
+       }
+       std::swap(rd_ports, new_rd_ports);
+       std::swap(wr_ports, new_wr_ports);
+}
index e0d8c277f49fe84b646eb1af4d2fbb1d169027ef..214086ac47e649752fb8c621321dfb6e1ef4c532 100644 (file)
@@ -75,6 +75,8 @@ struct Mem {
        static std::vector<Mem> get_all_memories(Module *module);
        static std::vector<Mem> get_selected_memories(Module *module);
        Cell *extract_rdff(int idx, FfInitVals *initvals);
+       void narrow();
+
        Mem(Module *module, IdString memid, int width, int start_offset, int size) : module(module), memid(memid), packed(false), mem(nullptr), cell(nullptr), width(width), start_offset(start_offset), size(size) {}
 };