File: ecppack.cpp

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (342 lines) | stat: -rw-r--r-- 13,065 bytes parent folder | download
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "ChipConfig.hpp"
#include "Bitstream.hpp"
#include "Chip.hpp"
#include "Database.hpp"
#include "DatabasePath.hpp"
#include "Tile.hpp"
#include "BitDatabase.hpp"
#include "version.hpp"
#include "wasmexcept.hpp"
#include <iostream>
#include <boost/program_options.hpp>
#include <stdexcept>
#include <streambuf>
#include <fstream>
#include <iomanip>

using namespace std;

uint8_t reverse_byte(uint8_t byte) {
    uint8_t rev = 0;
    for (int i = 0; i < 8; i++)
        if (byte & (1 << i))
            rev |= (1 << (7 - i));
    return rev;
}

uint32_t convert_hexstring(std::string value_str)
{
    return uint32_t(strtoul(value_str.c_str(), nullptr, 0));
}

int main(int argc, char *argv[])
{
    using namespace Trellis;
    namespace po = boost::program_options;

    std::string database_folder = get_database_path();

    po::options_description options("Allowed options");
    options.add_options()("help,h", "show help");
    options.add_options()("verbose,v", "verbose output");
    options.add_options()("db", po::value<std::string>(), "Trellis database folder location");
    options.add_options()("usercode", po::value<uint32_t>(), "USERCODE to set in bitstream");
    options.add_options()("idcode", po::value<std::string>(), "IDCODE to override in bitstream");
    options.add_options()("freq", po::value<std::string>(), "config frequency in MHz");
    options.add_options()("svf", po::value<std::string>(), "output SVF file");
    options.add_options()("svf-rowsize", po::value<int>(), "SVF row size in bits (default 8000)");
    options.add_options()("compress", "compress bitstream to reduce size");
    options.add_options()("spimode", po::value<std::string>(), "SPI Mode to use (fast-read, dual-spi, qspi)");
    options.add_options()("background", "enable background reconfiguration in bitstream");
    options.add_options()("delta", po::value<std::string>(), "create a delta partial bitstream given a reference config");
    options.add_options()("bootaddr", po::value<std::string>(), "set next BOOTADDR in bitstream and enable multi-boot");
    po::positional_options_description pos;
    options.add_options()("input", po::value<std::string>()->required(), "input textual configuration");
    pos.add("input", 1);
    options.add_options()("bit", po::value<std::string>(), "output bitstream file");
    pos.add("bit", 1);
    options.add_options()("version", "show current version and exit");

    po::variables_map vm;

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

        if (vm.count("version")) {
            cerr << "Project Trellis ecppack Version " << git_describe_str << endl;
            return 0;
        }

        po::notify(vm);
    }
    catch (po::required_option& e) {
        cerr << "Error: input file is mandatory." << endl << endl;
        goto help;
    }
    catch (std::exception& e) {
        cerr << "Error: " << e.what() << endl << endl;
        goto help;
    }

    if (vm.count("help")) {
help:
        cerr << "Project Trellis - Open Source Tools for ECP5 FPGAs" << endl;
        cerr << "Version " << git_describe_str << endl;
        cerr << argv[0] << ": ECP5 bitstream packer" << endl;
        cerr << endl;
        cerr << "Copyright (C) 2018 gatecat <gatecat@ds0.me>" << endl;
        cerr << endl;
        cerr << "Usage: " << argv[0] << " input.config [output.bit] [options]" << endl;
        cerr << options << endl;
        return vm.count("help") ? 0 : 1;
    }

    ifstream config_file(vm["input"].as<string>());
    if (!config_file) {
        cerr << "Failed to open input file" << endl;
        return 1;
    }

    if (vm.count("db")) {
        database_folder = vm["db"].as<string>();
    }

    try {
        load_database(database_folder);
    } catch (runtime_error &e) {
        cerr << "Failed to load Trellis database: " << e.what() << endl;
        return 1;
    }

    string textcfg((std::istreambuf_iterator<char>(config_file)), std::istreambuf_iterator<char>());

    ChipConfig cc;
    try {
        cc = ChipConfig::from_string(textcfg);
    } catch (runtime_error &e) {
        cerr << "Failed to process input config: " << e.what() << endl;
        return 1;
    }

    Chip c = cc.to_chip();
    if (vm.count("usercode"))
        c.usercode = vm["usercode"].as<uint32_t>();

    if (vm.count("idcode")) {
        string idcode_str = vm["idcode"].as<string>();
        uint32_t idcode = uint32_t(strtoul(idcode_str.c_str(), nullptr, 0));
        if (idcode == 0) {
            cerr << "Invalid idcode: " << idcode_str << endl;
            return 1;
        }
        c.info.idcode = idcode;
    }

    map<string, string> bitopts;

    // Apply options passed from nextpnr via SYSCONFIG
    if (cc.sysconfig.count("MCCLK_FREQ")) {
        std::string freq = cc.sysconfig.at("MCCLK_FREQ");
        if (freq == "62")
            freq = "62.0";
        bitopts["freq"] = freq;
    }

    if (cc.sysconfig.count("COMPRESS_CONFIG") && cc.sysconfig.at("COMPRESS_CONFIG") == "ON")
        bitopts["compress"] = "yes";

    // Override with command line options
    if (vm.count("freq"))
        bitopts["freq"] = vm["freq"].as<string>();

    if (vm.count("spimode"))
        bitopts["spimode"] = vm["spimode"].as<string>();

    if (vm.count("compress"))
        bitopts["compress"] = "yes";

    if (vm.count("background")) {
        auto tile_db = get_tile_bitdata(TileLocator{c.info.family, c.info.name, "EFB0_PICB0"});
        auto esb = tile_db->get_data_for_enum("SYSCONFIG.BACKGROUND_RECONFIG");
        auto tile = c.get_tiles_by_type("EFB0_PICB0");
        for (const auto &bit : esb.options["ON"].bits)
            tile[0]->cram.set_bit(bit.frame, bit.bit, bit.inv ? 0 : 1);
        bitopts["background"] = "yes";
    }

    if (vm.count("bootaddr")) {
        uint32_t bootaddr = convert_hexstring(vm["bootaddr"].as<string>());

        if (bootaddr & 0xffff) {
            cerr << "Error: Boot Address must be 64k aligned !" << endl;
            return 1;
        }

        bootaddr = (bootaddr & 0x00ff0000) >> 16;

        auto tile_db = get_tile_bitdata(TileLocator{c.info.family, c.info.name, "EFB1_PICB1"});
        WordSettingBits wsb = tile_db->get_data_for_setword("BOOTADDR");
        auto tile = c.get_tiles_by_type("EFB1_PICB1");

        if (tile.size() != 1) {
            cerr << "EFB1_PICB1 Frame is wrong size. Can't proceed" << endl;
            return 1;
        }

        for(uint32_t j=0; j < wsb.bits.size(); j++) {
            auto bg = wsb.bits.at(j);
            for (auto bit :  bg.bits) {
                bool value = (bootaddr & (1 << j)) > 0;
                tile[0]->cram.set_bit(bit.frame, bit.bit, value);
            }
        }

        bitopts["multiboot"] = "yes";
    }

    bool partial_mode = false;
    vector<uint32_t> partial_frames;
    if (vm.count("delta")) {
        ifstream delta_file(vm["delta"].as<string>());
        if (!delta_file) {
            cerr << "Failed to open reference config file" << endl;
            return 1;
        }
        string refcfg((std::istreambuf_iterator<char>(delta_file)), std::istreambuf_iterator<char>());

        ChipConfig ref_cc;
        try {
            ref_cc = ChipConfig::from_string(refcfg);
        } catch (runtime_error &e) {
            cerr << "Failed to process reference config: " << e.what() << endl;
            return 1;
        }
        Chip ref_c = ref_cc.to_chip();
        for (int frame = 0; frame < c.cram.frames(); frame++) {
            if (ref_c.cram.data->at(frame) != c.cram.data->at(frame)) {
                partial_frames.push_back(frame);
            }
        }

        partial_mode = true;
    }

    Bitstream b = partial_mode ? Bitstream::serialise_chip_partial(c, partial_frames, bitopts) : Bitstream::serialise_chip(c, bitopts);
    if (vm.count("bit")) {
        ofstream bit_file(vm["bit"].as<string>(), ios::binary);
        if (!bit_file) {
            cerr << "Failed to open output file" << endl;
            return 1;
        }
        b.write_bit(bit_file);
    }

    if (vm.count("svf")) {
        // Create JTAG bitstream without SPI flash related settings, as these
        // seem to confuse the chip sometimes when configuring over JTAG
        if (!bitopts.empty() && !(bitopts.size() == 1 && bitopts.count("compress"))) {
            bitopts.erase("spimode");
            bitopts.erase("freq");
            b = Bitstream::serialise_chip(c, bitopts);
        }

        vector<uint8_t> bitstream = b.get_bytes();
        int max_row_size = 8000;
        if (vm.count("svf-rowsize"))
            max_row_size = vm["svf-rowsize"].as<int>();
        if ((max_row_size % 8) != 0 || max_row_size <= 0) {
            cerr << "SVF row size must be an exact positive number of bytes" << endl;
            return 1;
        }
        ofstream svf_file(vm["svf"].as<string>());
        if (!svf_file) {
            cerr << "Failed to open output SVF file" << endl;
            return 1;
        }
        svf_file << "HDR\t0;" << endl;
        svf_file << "HIR\t0;" << endl;
        svf_file << "TDR\t0;" << endl;
        svf_file << "TIR\t0;" << endl;
        svf_file << "ENDDR\tDRPAUSE;" << endl;
        svf_file << "ENDIR\tIRPAUSE;" << endl;
        svf_file << "STATE\tIDLE;" << endl;
        svf_file << "SIR\t8\tTDI  (E0);" << endl;
        svf_file << "SDR\t32\tTDI  (00000000)" << endl;
        svf_file << "\t\t\tTDO  (" << setw(8) << hex << uppercase << setfill('0') << c.info.idcode << ")" << endl;
        svf_file << "\t\t\tMASK (FFFFFFFF);" << endl;
        svf_file << endl;
        if (!partial_mode) {
            svf_file << "SIR\t8\tTDI  (1C);" << endl;
            svf_file << "SDR\t510\tTDI  (3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" << endl;
            svf_file << "\t\t\t\tFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);" << endl;
            svf_file << endl;
            svf_file << "SIR\t8\tTDI  (C6);" << endl;
            svf_file << "SDR\t8\tTDI  (00);" << endl;
            svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
            svf_file << endl;
            svf_file << "SIR\t8\tTDI  (0E);" << endl;
            svf_file << "SDR\t8\tTDI  (01);" << endl;
            svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
            svf_file << endl;
            svf_file << "SIR\t8\tTDI  (3C);" << endl;
            svf_file << "SDR\t32\tTDI  (00000000)" << endl;
            svf_file << "\t\t\tTDO  (00000000)" << endl;
            svf_file << "\t\t\tMASK (0000B000);" << endl;
            svf_file << endl;
        } else {
            svf_file << "SIR\t8\tTDI  (79);" << endl;
            svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
            svf_file << "SIR\t8\tTDI  (74);" << endl;
            svf_file << "SDR\t8\tTDI  (00);" << endl;
            svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
        }
        svf_file << "SIR\t8\tTDI  (46);" << endl;
        svf_file << "SDR\t8\tTDI  (01);" << endl;
        svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
        svf_file << endl;
        svf_file << "SIR\t8\tTDI  (7A);" << endl;
        svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-02 SEC;" << endl;
        size_t i = 0;
        while(i < bitstream.size()) {
           size_t len = min(size_t(max_row_size / 8), bitstream.size() - i);
           if (len == 0)
               break;
           svf_file << "SDR\t" << setw(0) << dec << (8 * len) << "\tTDI  (";
           svf_file << hex << uppercase << setw(2) << setfill('0');
           for (int j = len - 1; j >= 0; j--) {
                svf_file << setw(2) << unsigned(reverse_byte(uint8_t(bitstream[j + i])));
                if (j % 40 == 0 && j != 0)
                    svf_file << endl << "\t\t\t";
           }
           svf_file << ");" << endl;
           i += len;
        }
        if (!partial_mode) {
            svf_file << endl;
            svf_file << "SIR\t8\tTDI  (FF);" << endl;
            svf_file << "RUNTEST\tIDLE\t100 TCK\t1.00E-02 SEC;" << endl;
            svf_file << endl;
            svf_file << "SIR\t8\tTDI  (C0);" << endl;
            svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-03 SEC;" << endl;
            svf_file << "SDR\t32\tTDI  (00000000)" << endl;
            svf_file << "\t\t\tTDO  (00000000)" << endl;
            svf_file << "\t\t\tMASK (FFFFFFFF);" << endl;
            svf_file << endl;
        }
        svf_file << "SIR\t8\tTDI  (26);" << endl;
        svf_file << "RUNTEST\tIDLE\t2 TCK\t2.00E-01 SEC;" << endl;
        svf_file << endl;
        svf_file << "SIR\t8\tTDI  (FF);" << endl;
        svf_file << "RUNTEST\tIDLE\t2 TCK\t1.00E-03 SEC;" << endl;
        svf_file << endl;
        if (!partial_mode) {
            svf_file << "SIR\t8\tTDI  (3C);" << endl;
            svf_file << "SDR\t32\tTDI  (00000000)" << endl;
            svf_file << "\t\t\tTDO  (00000100)" << endl;
            svf_file << "\t\t\tMASK (00002100);" << endl;
        }
    }

    return 0;
}