aboutsummaryrefslogtreecommitdiffstats
path: root/ice40/main.cc
blob: 224b027577c491fbf59cb5f4d85eb49b531a1c9c (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Claire Xenia Wolf <claire@yosyshq.com>
 *  Copyright (C) 2018  Miodrag Milanovic <micko@yosyshq.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

#include <fstream>
#include "bitstream.h"
#include "command.h"
#include "design_utils.h"
#include "log.h"
#include "pcf.h"
#include "timing.h"

USING_NEXTPNR_NAMESPACE

class Ice40CommandHandler : public CommandHandler
{
  public:
    Ice40CommandHandler(int argc, char **argv);
    virtual ~Ice40CommandHandler(){};
    std::unique_ptr<Context> createContext(dict<std::string, Property> &values) override;
    void setupArchContext(Context *ctx) override;
    void validate() override;
    void customAfterLoad(Context *ctx) override;
    void customBitstream(Context *ctx) override;

  protected:
    po::options_description getArchOptions() override;
};

Ice40CommandHandler::Ice40CommandHandler(int argc, char **argv) : CommandHandler(argc, argv) {}

po::options_description Ice40CommandHandler::getArchOptions()
{
    po::options_description specific("Architecture specific options");
    if (Arch::is_available(ArchArgs::LP384))
        specific.add_options()("lp384", "set device type to iCE40LP384");
    if (Arch::is_available(ArchArgs::LP1K))
        specific.add_options()("lp1k", "set device type to iCE40LP1K");
    if (Arch::is_available(ArchArgs::LP4K))
        specific.add_options()("lp4k", "set device type to iCE40LP4K");
    if (Arch::is_available(ArchArgs::LP8K))
        specific.add_options()("lp8k", "set device type to iCE40LP8K");
    if (Arch::is_available(ArchArgs::HX1K))
        specific.add_options()("hx1k", "set device type to iCE40HX1K");
    if (Arch::is_available(ArchArgs::HX8K))
        specific.add_options()("hx4k", "set device type to iCE40HX4K");
    if (Arch::is_available(ArchArgs::HX4K))
        specific.add_options()("hx8k", "set device type to iCE40HX8K");
    if (Arch::is_available(ArchArgs::UP3K))
        specific.add_options()("up3k", "set device type to iCE40UP3K");
    if (Arch::is_available(ArchArgs::UP5K))
        specific.add_options()("up5k", "set device type to iCE40UP5K");
    if (Arch::is_available(ArchArgs::U1K))
        specific.add_options()("u1k", "set device type to iCE5LP1K");
    if (Arch::is_available(ArchArgs::U2K))
        specific.add_options()("u2k", "set device type to iCE5LP2K");
    if (Arch::is_available(ArchArgs::U4K))
        specific.add_options()("u4k", "set device type to iCE5LP4K");
    specific.add_options()("package", po::value<std::string>(), "set device package");
    specific.add_options()("pcf", po::value<std::string>(), "PCF constraints file to ingest");
    specific.add_options()("asc", po::value<std::string>(), "asc bitstream file to write");
    specific.add_options()("read", po::value<std::string>(), "asc bitstream file to read");
    specific.add_options()("promote-logic",
                           "enable promotion of 'logic' globals (in addition to clk/ce/sr by default)");
    specific.add_options()("no-promote-globals", "disable all global promotion");
    specific.add_options()("opt-timing", "run post-placement timing optimisation pass (experimental)");
    specific.add_options()("tmfuzz", "run path delay estimate fuzzer");
    specific.add_options()("pcf-allow-unconstrained", "don't require PCF to constrain all IO");

    return specific;
}
void Ice40CommandHandler::validate()
{
    conflicting_options(vm, "read", "json");
    if ((vm.count("lp384") + vm.count("lp1k") + vm.count("lp4k") + vm.count("lp8k") + vm.count("hx1k") +
         vm.count("hx4k") + vm.count("hx8k") + vm.count("up3k") + vm.count("up5k") + vm.count("u1k") + vm.count("u2k") +
         vm.count("u4k")) > 1)
        log_error("Only one device type can be set\n");
}

void Ice40CommandHandler::customAfterLoad(Context *ctx)
{
    if (vm.count("pcf")) {
        std::string filename = vm["pcf"].as<std::string>();
        std::ifstream pcf(filename);
        if (!apply_pcf(ctx, filename, pcf))
            log_error("Loading PCF failed.\n");
    } else {
        log_warning("No PCF file specified; IO pins will be placed automatically\n");
    }
}
void Ice40CommandHandler::customBitstream(Context *ctx)
{
    if (vm.count("asc")) {
        std::string filename = vm["asc"].as<std::string>();
        std::ofstream f(filename);
        write_asc(ctx, f);
    }
}

void Ice40CommandHandler::setupArchContext(Context *ctx)
{
    if (vm.count("tmfuzz"))
        ice40DelayFuzzerMain(ctx);

    if (vm.count("read")) {
        std::string filename = vm["read"].as<std::string>();
        std::ifstream f(filename);
        if (!read_asc(ctx, f))
            log_error("Loading ASC failed.\n");
    }
}

std::unique_ptr<Context> Ice40CommandHandler::createContext(dict<std::string, Property> &values)
{
    ArchArgs chipArgs;
    chipArgs.type = ArchArgs::NONE;
    if (vm.count("lp384")) {
        chipArgs.type = ArchArgs::LP384;
        chipArgs.package = "qn32";
    }

    if (vm.count("lp1k")) {
        chipArgs.type = ArchArgs::LP1K;
        chipArgs.package = "tq144";
    }

    if (vm.count("lp4k")) {
        chipArgs.type = ArchArgs::LP4K;
        chipArgs.package = "tq144";
    }

    if (vm.count("lp8k")) {
        chipArgs.type = ArchArgs::LP8K;
        chipArgs.package = "ct256";
    }

    if (vm.count("hx1k")) {
        chipArgs.type = ArchArgs::HX1K;
        chipArgs.package = "tq144";
    }

    if (vm.count("hx4k")) {
        chipArgs.type = ArchArgs::HX4K;
        chipArgs.package = "tq144";
    }

    if (vm.count("hx8k")) {
        chipArgs.type = ArchArgs::HX8K;
        chipArgs.package = "ct256";
    }

    if (vm.count("up3k")) {
        chipArgs.type = ArchArgs::UP3K;
        chipArgs.package = "sg48";
    }

    if (vm.count("up5k")) {
        chipArgs.type = ArchArgs::UP5K;
        chipArgs.package = "sg48";
    }

    if (vm.count("u1k")) {
        chipArgs.type = ArchArgs::U1K;
        chipArgs.package = "sg48";
    }

    if (vm.count("u2k")) {
        chipArgs.type = ArchArgs::U2K;
        chipArgs.package = "sg48";
    }

    if (vm.count("u4k")) {
        chipArgs.type = ArchArgs::U4K;
        chipArgs.package = "sg48";
    }

    if (vm.count("package"))
        chipArgs.package = vm["package"].as<std::string>();
    else
        log_warning("Use of default value for --package is deprecated. Please add '--package %s' to arguments.\n",
                    chipArgs.package.c_str());

    if (values.find("arch.name") != values.end()) {
        std::string arch_name = values["arch.name"].as_string();
        if (arch_name != "ice40")
            log_error("Unsupported architecture '%s'.\n", arch_name.c_str());
    }
    if (values.find("arch.type") != values.end()) {
        std::string arch_type = values["arch.type"].as_string();
        if (chipArgs.type != ArchArgs::NONE)
            log_error("Overriding architecture is unsupported.\n");

        if (arch_type == "lp384") {
            chipArgs.type = ArchArgs::LP384;
        }
        if (arch_type == "lp1k") {
            chipArgs.type = ArchArgs::LP1K;
        }
        if (arch_type == "lp4k") {
            chipArgs.type = ArchArgs::LP4K;
        }
        if (arch_type == "lp8k") {
            chipArgs.type = ArchArgs::LP8K;
        }
        if (arch_type == "hx1k") {
            chipArgs.type = ArchArgs::HX1K;
        }
        if (arch_type == "hx4k") {
            chipArgs.type = ArchArgs::HX4K;
        }
        if (arch_type == "hx8k") {
            chipArgs.type = ArchArgs::HX8K;
        }
        if (arch_type == "up3k") {
            chipArgs.type = ArchArgs::UP3K;
        }
        if (arch_type == "up5k") {
            chipArgs.type = ArchArgs::UP5K;
        }
        if (arch_type == "u1k") {
            chipArgs.type = ArchArgs::U1K;
        }
        if (arch_type == "u2k") {
            chipArgs.type = ArchArgs::U2K;
        }
        if (arch_type == "u4k") {
            chipArgs.type = ArchArgs::U4K;
        }
        if (chipArgs.type == ArchArgs::NONE)
            log_error("Unsupported FPGA type '%s'.\n", arch_type.c_str());
    }
    if (values.find("arch.package") != values.end()) {
        if (vm.count("package"))
            log_error("Overriding architecture is unsupported.\n");
        chipArgs.package = values["arch.package"].as_string();
    }

    if (chipArgs.type == ArchArgs::NONE) {
        chipArgs.type = ArchArgs::HX1K;
        chipArgs.package = "tq144";
    }

    auto ctx = std::unique_ptr<Context>(new Context(chipArgs));
    for (auto &val : values)
        ctx->settings[ctx->id(val.first)] = val.second;

    ctx->settings[ctx->id("arch.package")] = ctx->archArgs().package;
    if (vm.count("promote-logic"))
        ctx->settings[id_promote_logic] = Property::State::S1;
    if (vm.count("no-promote-globals"))
        ctx->settings[id_no_promote_globals] = Property::State::S1;
    if (vm.count("opt-timing"))
        ctx->settings[id_opt_timing] = Property::State::S1;
    if (vm.count("pcf-allow-unconstrained"))
        ctx->settings[id_pcf_allow_unconstrained] = Property::State::S1;
    return ctx;
}

int main(int argc, char *argv[])
{
    Ice40CommandHandler handler(argc, argv);
    return handler.exec();
}

#endif