aboutsummaryrefslogtreecommitdiffstats
path: root/ecp5/main.cc
blob: a6128d0f28bb6f4e6c217b4d76862fe883530a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Clifford Wolf <clifford@symbioticeda.com>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifdef MAIN_EXECUTABLE

#ifndef NO_GUI
#include <QApplication>
#include "application.h"
#include "mainwindow.h"
#endif
#ifndef NO_PYTHON
#include "pybindings.h"
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#include <iostream>

#include "Chip.hpp"
#include "Database.hpp"
#include "Tile.hpp"

#include "log.h"
#include "nextpnr.h"
#include "version.h"

#include "bitstream.h"
#include "design_utils.h"
#include "jsonparse.h"
#include "pack.h"
#include "place_sa.h"
#include "timing.h"

USING_NEXTPNR_NAMESPACE

int main(int argc, char *argv[])
{
    try {

        namespace po = boost::program_options;
        int rc = 0;

        log_files.push_back(stdout);

        po::options_description options("Allowed options");
        options.add_options()("help,h", "show help");
        options.add_options()("verbose,v", "verbose output");
        options.add_options()("force,f", "keep running after errors");
#ifndef NO_GUI
        options.add_options()("gui", "start gui");
#endif
        options.add_options()("json", po::value<std::string>(), "JSON design file to ingest");
        options.add_options()("seed", po::value<int>(), "seed value for random number generator");

        options.add_options()("basecfg", po::value<std::string>(), "base chip configuration in Trellis text format");
        options.add_options()("bit", po::value<std::string>(), "bitstream file to write");
        options.add_options()("textcfg", po::value<std::string>(), "textual configuration in Trellis format to write");

        po::positional_options_description pos;
#ifndef NO_PYTHON
        options.add_options()("run", po::value<std::vector<std::string>>(), "python file to execute");
        pos.add("run", -1);
#endif
        options.add_options()("version,V", "show version");

        po::variables_map vm;
        try {
            po::parsed_options parsed = po::command_line_parser(argc, argv).options(options).positional(pos).run();

            po::store(parsed, vm);

            po::notify(vm);
        }

        catch (std::exception &e) {
            std::cout << e.what() << "\n";
            return 1;
        }

        if (vm.count("help") || argc == 1) {
            std::cout << boost::filesystem::basename(argv[0])
                      << " -- Next Generation Place and Route (git "
                         "sha1 " GIT_COMMIT_HASH_STR ")\n";
            std::cout << "\n";
            std::cout << options << "\n";
            return argc != 1;
        }

        if (vm.count("version")) {
            std::cout << boost::filesystem::basename(argv[0])
                      << " -- Next Generation Place and Route (git "
                         "sha1 " GIT_COMMIT_HASH_STR ")\n";
            return 1;
        }

        Trellis::load_database(TRELLIS_ROOT "/database");

        ArchArgs args;
        args.type = ArchArgs::LFE5U_45F;
        args.package = "CABGA381";
        args.speed = 6;
        Context ctx(args);

        if (vm.count("verbose")) {
            ctx.verbose = true;
        }

        if (vm.count("force")) {
            ctx.force = true;
        }

        if (vm.count("seed")) {
            ctx.rngseed(vm["seed"].as<int>());
        }

        if (vm.count("json")) {
            std::string filename = vm["json"].as<std::string>();
            std::ifstream f(filename);
            if (!parse_json_file(f, filename, &ctx))
                log_error("Loading design failed.\n");

            if (!pack_design(&ctx) && !ctx.force)
                log_error("Packing design failed.\n");
            if (vm.count("freq"))
                ctx.target_freq = vm["freq"].as<double>() * 1e6;
            assign_budget(&ctx);
            ctx.check();
            print_utilisation(&ctx);
            ctx.timing_driven = true;
            if (vm.count("no-tmdriv"))
                ctx.timing_driven = false;

            if (!place_design_sa(&ctx) && !ctx.force)
                log_error("Placing design failed.\n");
            ctx.check();
            if (!ctx.route() && !ctx.force)
                log_error("Routing design failed.\n");

            std::string basecfg;
            if (vm.count("basecfg"))
                basecfg = vm["basecfg"].as<std::string>();

            std::string bitstream;
            if (vm.count("bit"))
                bitstream = vm["bit"].as<std::string>();

            std::string textcfg;
            if (vm.count("textcfg"))
                textcfg = vm["textcfg"].as<std::string>();
            write_bitstream(&ctx, basecfg, textcfg, bitstream);
        }

#ifndef NO_PYTHON
        if (vm.count("run")) {
            init_python(argv[0], true);
            python_export_global("ctx", ctx);

            std::vector<std::string> files = vm["run"].as<std::vector<std::string>>();
            for (auto filename : files)
                execute_python_file(filename.c_str());

            deinit_python();
        }
#endif

#ifndef NO_GUI
        if (vm.count("gui")) {
            Application a(argc, argv);
            MainWindow w;
            w.show();

            rc = a.exec();
        }
#endif
        return rc;
    } catch (log_execution_error_exception) {
#if defined(_MSC_VER)
        _exit(EXIT_FAILURE);
#else
        _Exit(EXIT_FAILURE);
#endif
    }
}

#endif