Fletchgen
The Fletcher Design Generator
fletchgen.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "fletchgen/fletchgen.h"
16 
17 #include <cerata/api.h>
18 #include <cerata/dot/dot.h>
19 #include <cerata/vhdl/vhdl.h>
20 #include <fletcher/common.h>
21 
22 #include <fstream>
23 #include <thread>
24 
25 #include "fletchgen/options.h"
26 #include "fletchgen/design.h"
27 #include "fletchgen/utils.h"
28 #include "fletchgen/srec/recordbatch.h"
29 #include "fletchgen/top/sim.h"
30 #include "fletchgen/top/axi.h"
31 #include "fletchgen/static_vhdl.h"
32 
33 namespace fletchgen {
34 
35 int fletchgen(int argc, char **argv) {
36 
37  // Start logging
38  std::string program_name = fletchgen::GetProgramName(argv[0]);
39  fletcher::StartLogging(program_name, FLETCHER_LOG_DEBUG, program_name + ".log");
40 
41  // Enable Cerata to log into the Fletcher logger through the callback function.
42  cerata::logger().enable(fletchgen::LogCerata);
43 
44  // Parse options
45  auto options = std::make_shared<fletchgen::Options>();
46  if (!fletchgen::Options::Parse(options.get(), argc, argv)) {
47  FLETCHER_LOG(ERROR, "Error parsing arguments. Exiting Fletchgen.");
48  return -1;
49  }
50 
51  // Show program version.
52  if (options->version) {
53  std::cout << version() << " (using " << cerata::version() << ")" << std::endl;
54  }
55 
56  // Quit the program early.
57  if (options->quit) {
58  fletcher::StopLogging();
59  return 0;
60  }
61 
62  // Load input files
63  if (!options->LoadRecordBatches()) return false;
64  if (!options->LoadSchemas()) return false;
65 
66  // Potential RecordBatch descriptors for simulation models.
67  std::vector<fletcher::RecordBatchDescription> srec_batch_desc;
68 
69  // Generate designs in Cerata
70  if (!options->MustGenerateDesign()) {
71  FLETCHER_LOG(INFO, "No schemas or recordbatches were supplied. No design was generated.");
72  exit(0);
73  }
74 
75  // Generate the whole Cerata design.
76  fletchgen::Design design(options);
77  // Run vhdmmio to generate the mmio infrastructure.
78  std::thread vhdmmio(Design::RunVhdmmio, design.all_regs, design.mmio_spec);
79 
80 
81  // Generate SREC output
82  if (options->MustGenerateSREC()) {
83  FLETCHER_LOG(INFO, "Generating SREC output.");
84  auto srec_out = std::ofstream(options->srec_out_path);
85  fletchgen::srec::GenerateReadSREC(design.batch_desc, &srec_batch_desc, &srec_out, 64);
86  srec_out.close();
87  }
88 
89  auto &l = options->languages;
90 
91  // Generate DOT output.
92  if (options->MustGenerate("dot")) {
93  FLETCHER_LOG(INFO, "Generating DOT output.");
94  auto dot = cerata::dot::DOTOutputGenerator(options->output_dir, design.GetOutputSpec());
95  dot.Generate();
96  // Remove dot from the list of target languages
97  l.erase(std::remove(l.begin(), l.end(), std::string("dot")), l.end());
98  }
99 
100  // Generate VHDL output
101  if (options->MustGenerate("vhdl")) {
102  FLETCHER_LOG(INFO, "Generating VHDL output.");
103  auto vhdl = cerata::vhdl::VHDLOutputGenerator(options->output_dir,
104  design.GetOutputSpec(),
106  vhdl.Generate();
107  // Remove vhdl from the list of target languages
108  l.erase(std::remove(l.begin(), l.end(), std::string("vhdl")), l.end());
109  }
110 
111  // Check if any other languages were requested; they are not supported.
112  // Generate warnings.
113  if (!l.empty()) {
114  // Print all unsupported languages
115  for (const auto &t : l) {
116  FLETCHER_LOG(WARNING, "Unknown target language: " << t);
117  }
118  }
119 
120  // Generate simulation top level
121  if (options->MustGenerateDesign() && options->sim_top) {
122  std::ofstream sim_file;
123  std::string sim_file_path = options->output_dir + "/vhdl/SimTop_tc.gen.vhd";
124  FLETCHER_LOG(INFO, "Saving simulation top-level design to: " + sim_file_path);
125  sim_file = std::ofstream(sim_file_path);
126  // If the srec simulation dump path doesn't exist, it can't be canonicalized later on.
127  if (!cerata::FileExists(options->srec_sim_dump)) {
128  // Just touch the file.
129  std::ofstream srec_out(options->srec_sim_dump);
130  srec_out.close();
131  }
132  fletchgen::top::GenerateSimTop(design,
133  {&sim_file},
134  options->srec_out_path,
135  options->srec_sim_dump,
136  srec_batch_desc);
137  sim_file.close();
138  }
139 
140  // Generate AXI top level
141  if (options->axi_top) {
142  std::ofstream axi_file;
143  std::string axi_file_path = options->output_dir + "/vhdl/AxiTop.gen.vhd";
144  FLETCHER_LOG(INFO, "Saving AXI top-level design to: " + axi_file_path);
145  axi_file = std::ofstream(axi_file_path);
146  fletchgen::top::GenerateAXITop(*design.mantle_comp,
147  *design.schema_set,
148  design.mmio_spec,
149  design.external,
150  {&axi_file});
151  axi_file.close();
152  }
153 
154  // Generate Vivado HLS template
155  if (options->vivado_hls) {
156  FLETCHER_LOG(WARNING, "Vivado HLS template output not yet implemented.");
157  /*
158  auto hls_template_path = options->output_dir + "/vivado_hls/" + options->kernel_name + ".cpp";
159  FLETCHER_LOG(INFO, "Generating Vivado HLS output: " + hls_template_path);
160  cerata::CreateDir(options->output_dir + "/vivado_hls");
161  auto hls_template_file = std::ofstream(hls_template_path);
162  hls_template_file << fletchgen::hls::GenerateVivadoHLSTemplate(*design.kernel);
163  */
164  }
165 
166  // Write static VHDL support files for Fletcher.
167  if (options->static_vhdl) {
168  write_static_vhdl("vhdl/support");
169  }
170 
171  // Wait for vhdmmio.
172  if (!vhdmmio.joinable()) {
173  FLETCHER_LOG(INFO, "Waiting for vhdmmio to complete...");
174  }
175  vhdmmio.join();
176 
177  FLETCHER_LOG(INFO, program_name + " completed.");
178 
179  // Shut down logging
180  fletcher::StopLogging();
181  return 0;
182 }
183 
184 } // namespace fletchgen
Contains all classes and functions related to Fletchgen.
Definition: array.cc:29
void LogCerata(cerata::LogLevel level, std::string const &message, char const *source_function, char const *source_file, int line_number)
Callback function for the Cerata logger.
Definition: utils.cc:43
constexpr char DEFAULT_NOTICE[]
Default copyright notice.
Definition: utils.h:46
std::string GetProgramName(char *argv0)
Return the name of this program executable.
Definition: utils.cc:25
void write_static_vhdl(const std::string &real_dir, const std::string &emb_dir)
Writes Fletcher's static VHDL files to the given directory.
Definition: static_vhdl.cc:27
std::string version()
Return Fletchgen version string.
Definition: utils.cc:72
int fletchgen(int argc, char **argv)
Fletchgen main entry. Used to wrap into PyFletchgen.
Definition: fletchgen.cc:35
A structure for all components in a Fletcher design.
Definition: design.h:36
std::shared_ptr< Mantle > mantle_comp
Definition: design.h:79
std::vector< cerata::OutputSpec > GetOutputSpec()
Obtain a Cerata OutputSpec from this design for Cerata back-ends to generate output.
Definition: design.cc:224
static void RunVhdmmio(const std::vector< std::vector< MmioReg > * > &regs, Axi4LiteSpec axi_spec)
Generate vhdmmio yaml and run it.
Definition: design.cc:210
std::vector< std::vector< MmioReg > * > all_regs
Pointers to all registers vectors.
Definition: design.h:62
std::shared_ptr< SchemaSet > schema_set
The SchemaSet to base the design on.
Definition: design.h:51
std::optional< std::shared_ptr< Type > > external
External signals type.
Definition: design.h:67
std::vector< fletcher::RecordBatchDescription > batch_desc
The RecordBatchDescriptions to use in SREC generation.
Definition: design.h:70
static bool Parse(Options *options, int argc, char **argv)
Parse command line options and store the result.
Definition: options.cc:22