aboutsummaryrefslogtreecommitdiffstats
path: root/ecp5/lpf.cc
blob: c12b65c4680e092374f9572cc5925de7a7f24b8a (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
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  David Shah <david@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.
 *
 */

#include <boost/algorithm/string.hpp>
#include <sstream>
#include "log.h"

NEXTPNR_NAMESPACE_BEGIN

static const std::unordered_set<std::string> sysconfig_keys = {
        "SLAVE_SPI_PORT",      "MASTER_SPI_PORT", "SLAVE_PARALLEL_PORT",
        "BACKGROUND_RECONFIG", "DONE_EX",         "DONE_OD",
        "DONE_PULL",           "MCCLK_FREQ",      "TRANSFR",
        "CONFIG_IOVOLTAGE",    "CONFIG_SECURE",   "WAKE_UP",
        "COMPRESS_CONFIG",     "CONFIG_MODE",     "INBUF",
};

static const std::unordered_set<std::string> iobuf_keys = {
        "IO_TYPE", "BANK",      "BANK_VCC",     "VREF",      "PULLMODE",   "DRIVE",       "SLEWRATE",
        "CLAMP",   "OPENDRAIN", "DIFFRESISTOR", "DIFFDRIVE", "HYSTERESIS", "TERMINATION",
};

bool Arch::apply_lpf(std::string filename, std::istream &in)
{
    auto isempty = [](const std::string &str) {
        return std::all_of(str.begin(), str.end(), [](char c) { return isblank(c) || c == '\r' || c == '\n'; });
    };
    auto strip_quotes = [](const std::string &str) {
        if (str.at(0) == '"') {
            NPNR_ASSERT(str.back() == '"');
            return str.substr(1, str.size() - 2);
        } else {
            return str;
        }
    };

    try {
        if (!in)
            log_error("failed to open LPF file\n");
        std::string line;
        std::string linebuf;
        int lineno = 0;
        while (std::getline(in, line)) {
            ++lineno;
            size_t cstart = line.find('#');
            if (cstart != std::string::npos)
                line = line.substr(0, cstart);
            cstart = line.find("//");
            if (cstart != std::string::npos)
                line = line.substr(0, cstart);
            if (isempty(line))
                continue;
            linebuf += line;
            // Look for a command up to a semicolon
            size_t scpos = linebuf.find(';');
            while (scpos != std::string::npos) {
                std::string command = linebuf.substr(0, scpos);
                // Split command into words
                std::stringstream ss(command);
                std::vector<std::string> words;
                std::string tmp;
                while (ss >> tmp)
                    words.push_back(tmp);
                if (words.size() >= 0) {
                    std::string verb = words.at(0);
                    if (verb == "BLOCK") {
                        if (words.size() != 2 || (words.at(1) != "ASYNCPATHS" && words.at(1) != "RESETPATHS"))
                            log_warning("    ignoring unsupported LPF command '%s' (on line %d)\n", command.c_str(),
                                        lineno);
                    } else if (verb == "SYSCONFIG") {
                        for (size_t i = 1; i < words.size(); i++) {
                            std::string setting = words.at(i);
                            size_t eqpos = setting.find('=');
                            if (eqpos == std::string::npos)
                                log_error("expected syntax 'SYSCONFIG <attr>=<value>...' (on line %d)\n", lineno);
                            std::string key = setting.substr(0, eqpos), value = setting.substr(eqpos + 1);
                            if (!sysconfig_keys.count(key))
                                log_error("unexpected SYSCONFIG key '%s' (on line %d)\n", key.c_str(), lineno);
                            settings[id("arch.sysconfig." + key)] = value;
                        }
                    } else if (verb == "FREQUENCY") {
                        if (words.size() < 2)
                            log_error("expected object type after FREQUENCY (on line %d)\n", lineno);
                        std::string etype = words.at(1);
                        if (etype == "PORT" || etype == "NET") {
                            if (words.size() < 4)
                                log_error("expected frequency value and unit after 'FREQUENCY %s' (on line %d)\n",
                                          etype.c_str(), lineno);
                            std::string target = strip_quotes(words.at(2));
                            float freq = std::stof(words.at(3));
                            std::string unit = words.at(4);
                            boost::algorithm::to_upper(unit);
                            if (unit == "MHZ")
                                ;
                            else if (unit == "KHZ")
                                freq /= 1.0e3;
                            else if (unit == "HZ")
                                freq /= 1.0e6;
                            else
                                log_error("unsupported frequency unit '%s' (on line %d)\n", unit.c_str(), lineno);
                            addClock(id(target), freq);
                        } else {
                            log_warning("    ignoring unsupported LPF command '%s %s' (on line %d)\n", command.c_str(),
                                        etype.c_str(), lineno);
                        }
                    } else if (verb == "LOCATE") {
                        if (words.size() < 5)
                            log_error("expected syntax 'LOCATE COMP <port name> SITE <pin>' (on line %d)\n", lineno);
                        if (words.at(1) != "COMP")
                            log_error("expected 'COMP' after 'LOCATE' (on line %d)\n", lineno);
                        std::string cell = strip_quotes(words.at(2));
                        if (words.at(3) != "SITE")
                            log_error("expected 'SITE' after 'LOCATE COMP %s' (on line %d)\n", cell.c_str(), lineno);
                        auto fnd_cell = cells.find(id(cell));
                        if (words.size() > 5)
                            log_error("unexpected input following LOCATE clause (on line %d)\n", lineno);
                        if (fnd_cell != cells.end()) {
                            fnd_cell->second->attrs[id("LOC")] = strip_quotes(words.at(4));
                        }
                    } else if (verb == "IOBUF") {
                        if (words.size() < 3)
                            log_error("expected syntax 'IOBUF PORT <port name> <attr>=<value>...' (on line %d)\n",
                                      lineno);
                        if (words.at(1) != "PORT")
                            log_error("expected 'PORT' after 'IOBUF' (on line %d)\n", lineno);
                        std::string cell = strip_quotes(words.at(2));
                        auto fnd_cell = cells.find(id(cell));
                        if (fnd_cell != cells.end()) {
                            for (size_t i = 3; i < words.size(); i++) {
                                std::string setting = words.at(i);
                                size_t eqpos = setting.find('=');
                                if (eqpos == std::string::npos)
                                    log_error(
                                            "expected syntax 'IOBUF PORT <port name> <attr>=<value>...' (on line %d)\n",
                                            lineno);
                                std::string key = setting.substr(0, eqpos), value = setting.substr(eqpos + 1);
                                if (!iobuf_keys.count(key))
                                    log_warning("IOBUF '%s' attribute '%s' is not recognised (on line %d)\n",
                                                cell.c_str(), key.c_str(), lineno);
                                fnd_cell->second->attrs[id(key)] = value;
                            }
                        }
                    }
                }

                linebuf = linebuf.substr(scpos + 1);
                scpos = linebuf.find(';');
            }
        }
        if (!isempty(linebuf))
            log_error("unexpected end of LPF file\n");
        settings[id("input/lpf")] = filename;
        return true;
    } catch (log_execution_error_exception) {
        return false;
    }
}

NEXTPNR_NAMESPACE_END