CGRA-ME
BitStream.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #include <CGRA/CGRA.h>
12 #include <CGRA/Module.h>
13 #include <CGRA/ModuleElastic.h>
14 
15 
16 #include <numeric>
17 #include <ostream>
18 
19 void BitStream::append(const ConfigCell* ccell, const std::vector<std::vector<BitSetting>>& bits,
20  int con_used) {
21  if (not setting_storage.emplace(ccell, bits).second) {
22  throw cgrame_error("adding duplicate config setting!");
23  }
24  contexts_used.emplace(ccell, con_used);
25  ccell_order.push_back(ccell);
26 }
27 
28 std::ostream& operator<<(std::ostream& os, const BitStream& bs)
29 {
34  os << "BitStream {\n";
35  for (const auto& ccell : bs.ccellOrder()) {
36  for (auto bitSetting : bs.settingFor(ccell)) {
37  os << ccell->getName() << ": " << bitSetting << '\n';
38  }
39  }
40  os << '}';
41  return os;
42 }
43 
44 void BitStream::printTestbench(std::ostream& os, const int& II, bool is_elastic) const
45 {
46  const auto context_storage_size = 1;
47  static const char* const total_num_bits_localparam = "TOTAL_NUM_BITS";
48  static const char* const clock_sig = "clock";
49  static const char* const enable_sig = "enable";
50  static const char* const sync_reset_sig = "sync_reset";
51  static const char* const bitstream_sig = "bitstream";
52  static const char* const done_sig = "done";
53  static const char* const storage = "storage";
54  static const char* const storage_pos = "next_pos";
55  int num_of_elastic_ccells = 0;
56  for (auto ccell_it = rbegin(ccell_order); ccell_it != rend(ccell_order); ++ccell_it) {
57  const auto& ccell = *ccell_it;
58  if (dynamic_cast<const ElasticConfigCell*>(ccell)){
59  num_of_elastic_ccells++;
60  }
61  }
62  if (num_of_elastic_ccells == 0) num_of_elastic_ccells = 1;
63  const int total_num_bits = std::accumulate(begin(setting_storage), end(setting_storage), context_storage_size * num_of_elastic_ccells, [&](auto&& accum, auto&& ccell_and_setting) {
64  return accum + ccell_and_setting.first->getStorageSize() * ccell_and_setting.second.size();
65  });
66 
67  os <<
68  "module CGRA_configurator(\n"
69  " input "<<clock_sig<<",\n"
70  " input "<<enable_sig<<",\n"
71  " input "<<sync_reset_sig<<",\n"
72  "\n"
73  " output reg "<<bitstream_sig<<",\n"
74  " output reg "<<done_sig<<"\n"
75  ");\n"
76  "\n"
77  " localparam "<<total_num_bits_localparam<<" = " << total_num_bits << ";\n"
78  ;
79 
84  os << "\treg [0:"<<total_num_bits_localparam<<"-1] "<<storage<<" = {\n";
85 
86  for (auto ccell_it = rbegin(ccell_order); ccell_it != rend(ccell_order); ++ccell_it) {
87  const auto& ccell = *ccell_it;
88  const auto& setting = setting_storage.at(ccell);
89  os << "\t\t";
90  for (auto bitsetting_cycle_it = rbegin(setting); bitsetting_cycle_it != rend(setting); ++bitsetting_cycle_it) {
91  for (auto bitsetting_it = rbegin(*bitsetting_cycle_it); bitsetting_it != rend(*bitsetting_cycle_it); ++bitsetting_it) {
92 
93  const auto& bitsetting = *bitsetting_it;
94  os << for_verilog(bitsetting);
95  if (std::next(ccell_it) != rend(ccell_order) || std::next(bitsetting_cycle_it) != rend(setting) || std::next(bitsetting_it) != rend(*bitsetting_cycle_it)) {
96  os << ',';
97  }
98  }
99  }
100  if (dynamic_cast<const ElasticConfigCell*>(ccell) && II > 1){
101  auto used_contexts = bitsettings_from_int(contexts_used.at(ccell) > 0 ? contexts_used.at(ccell) -1: 0, context_storage_size);
102  for (int i = used_contexts.size(); i > 0; i-- ) {
103  os << for_verilog(used_contexts.at(i-1));
104  if (std::next(ccell_it) != rend(ccell_order) ) {
105  os << ',';
106  }
107  }
108  }
109  os << " // " << ccell->getAllConnectedPorts().at(0)->getModule().parent->getName() << "::" << ccell->getName() << '\n';
110  }
111 
112  os <<
113  " };\n"
114  "\n"
115  " reg [31:0] "<<storage_pos<<";\n"
116  " always @(posedge "<<clock_sig<<") begin\n"
117  " if (sync_reset) begin\n"
118  " "<<storage_pos<<" <= 0;\n"
119  " "<<bitstream_sig<<" <= 1'b0;\n"
120  " "<<done_sig<<" <= 0;\n"
121  " end else if ("<<storage_pos<<" >= "<<total_num_bits_localparam<<") begin\n"
122  " "<<done_sig<<" <= 1;\n"
123  " "<<bitstream_sig<<" <= 1'b0;\n"
124  " end else if ("<<enable_sig<<") begin\n"
125  " "<<bitstream_sig<<" <= "<<storage<<"["<<storage_pos<<"];\n"
126  " "<<storage_pos<<" <= "<<storage_pos<<" + 1;\n"
127  " end\n"
128  " end\n"
129  "endmodule\n"
130  ;
131 }
ConfigCell
Definition: Module.h:825
BitStream::ccell_order
std::vector< const ConfigCell * > ccell_order
Definition: CGRA.h:69
BitStream
Definition: CGRA.h:34
ModuleElastic.h
BitStream::setting_storage
std::unordered_map< const ConfigCell *, BitArray > setting_storage
Definition: CGRA.h:68
BitStream::settingFor
const BitArray & settingFor(const ConfigCell *cc) const
Definition: CGRA.h:55
begin
auto begin(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:137
Module.h
operator<<
std::ostream & operator<<(std::ostream &os, const BitStream &bs)
Definition: BitStream.cpp:28
BitStream::contexts_used
std::unordered_map< const ConfigCell *, int > contexts_used
Definition: CGRA.h:70
CGRA.h
end
auto end(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:138
ElasticConfigCell
Definition: ModuleElastic.h:318
BitStream::append
void append(const ConfigCell *ccell, const BitArray &bits, int con_used=0)
Definition: BitStream.cpp:19
BitStream::ccellOrder
auto & ccellOrder() const
Definition: CGRA.h:60
for_verilog
BitSettingForVerilog for_verilog(const BitSetting &bs)
Definition: BitSetting.h:40
cgrame_error
Definition: Exception.h:20
BitStream::printTestbench
void printTestbench(std::ostream &os, const int &II, bool is_elastic=false) const
Definition: BitStream.cpp:44
bitsettings_from_int
std::vector< BitSetting > bitsettings_from_int(const INTEGRAL &value, int num_bits)
Definition: BitSetting.h:52