aboutsummaryrefslogtreecommitdiffstats
path: root/rules.mk
Commit message (Expand)AuthorAgeFilesLines
...
* use distinct build/staging dirs for EABI/OABI buildsNicolas Thill2010-02-271-1/+1
* use a DIR_SUFFIX variable to hold libc/version and use it in build/staging di...Nicolas Thill2010-02-271-4/+5
* remove support for DEBUG_DIR, it has lost its purpose since STAGING_DIR_ROOT ...Felix Fietkau2010-02-241-1/+0
* Add include_mk template which makes it a bit more convienient to include packageLars-Peter Clausen2010-02-241-0/+4
* add a config item to specify arguments to stripNicolas Thill2010-01-221-1/+1
* oops, forgot one small change in the last commitFelix Fietkau2009-12-041-1/+1
* remove the redundant subdirectory from the packages directory variableFelix Fietkau2009-12-041-1/+1
* output firmware files in bin/$(BOARD) instead of bin/Florian Fainelli2009-12-031-1/+1
* fix a typoFelix Fietkau2009-10-221-1/+1
* add some extra stripping options for kernel modules to reduce size even more ...Felix Fietkau2009-10-221-1/+1
* [toolchain] add external toolchain support, enhance native toolchain support ...Nicolas Thill2009-09-231-19/+53
* move the package dir to bin/packages/$(BOARD)_$(LIBC)-$(LIBCV) to prevent mul...Felix Fietkau2009-08-201-1/+1
* add a staging directory for unstripped package files, useful for debugging, w...Felix Fietkau2009-08-071-0/+1
* add -L$(TOOLCHAIN_DIR)/lib to the LIBGCC_S variable, if necessaryFelix Fietkau2009-08-011-1/+1
* [package] do not set anything in DISABLE_IPV6 when set, let the package pick ...Florian Fainelli2009-08-011-1/+1
* [package] globally define IPv6 support or not in OpenWrt, just like for large...Florian Fainelli2009-07-251-3/+11
* add a config option to select the binary stripping method to use (between non...Nicolas Thill2009-07-071-1/+8
* Set TARGET_CXX to 'no' if c++ is not enabled in the toolchain.Lars-Peter Clausen2009-05-301-1/+1
* added BUILD_LOG_DIR variable, make clean will delete the ./logs dirRalph Hempel2009-05-281-0/+1
* get rid of $Id$ - it has never helped us and it has broken too many patches ;)Felix Fietkau2009-04-171-1/+0
* move cflags default setting to target makefilesFelix Fietkau2009-03-141-0/+1
* prepend $(STAGING_DIR)/host to the PATH only in target-package stampfile targ...Felix Fietkau2009-03-091-1/+2
* replace a few unnecessary $(shell) callsFelix Fietkau2009-03-031-5/+3
* speed up metadata scanning a lot by avoiding unnecessary shell commands and m...Felix Fietkau2009-03-031-0/+2
* second part of target rootfs relocationRalph Hempel2009-02-201-1/+2
* remove LINUX_HEADERS_DIR, not used anymoreNicolas Thill2009-01-291-2/+0
* [cosmetic] indent soft-float optionsNicolas Thill2009-01-291-3/+3
* move IPKG_STATE_DIR from ./rules.mk to ./include/package-ipkg.mk and change i...Nicolas Thill2009-01-291-1/+0
* Use multi libc naming scheme for PACKAGE_DIRLars-Peter Clausen2009-01-241-1/+1
* Revert r14071Felix Fietkau2009-01-171-1/+1
* add eabi suffix to the target nameFelix Fietkau2009-01-171-1/+1
* add support for an optional staging directory for debug binariesFelix Fietkau2009-01-131-0/+1
* [massive] add support for alternative C libraries (currently only glibc/eglibc)Nicolas Thill2009-01-081-10/+13
* add the make variable LIBGCC_S, which points to the correct libgcc ldflag (av...Felix Fietkau2009-01-061-0/+1
* fix whitespace order in cflags without CONFIG_DEBUG to prevent breakage in so...Felix Fietkau2008-09-131-1/+1
* Revert r12510. Remove -rpath-link form TARGET_LDFLAGS as it breaks someLars-Peter Clausen2008-09-131-3/+4
* add new option for enabling debugging symbolsFelix Fietkau2008-09-131-2/+2
* add -rpath-link to TARGET_LDFLAGS by defaultFelix Fietkau2008-09-031-1/+1
* add TARGET_CXX variable which points to the c++ cross compilerFelix Fietkau2008-09-011-1/+2
* rework parallel building to get rid of some warningsFelix Fietkau2008-08-161-1/+1
* add a variable that contains the proper architecture-dependent -fPIC/-fpic cf...Felix Fietkau2008-08-061-0/+6
* add helper macro for stripping the last part of a version numberFelix Fietkau2008-07-311-0/+1
* Native toolchain does not support -fhonour-copts, do not add it to the TARGET...Florian Fainelli2008-07-191-5/+6
* adds missing patch for native toolchainJohn Crispin2008-06-101-5/+9
* large improvement for parallel builds. works without V=99 now and without war...Felix Fietkau2008-06-091-5/+6
* $(TARGET) is not anymore, replace it with $(ARCH) in ./bin/packages subdirect...Nicolas Thill2008-04-151-1/+1
* revert ARM to oabi by default, add some eabi patches for fixing up the toolch...Felix Fietkau2008-02-141-2/+2
* add $(TOOLCHAIN_DIR)/lib to $(TARGET_LDFLAGS) to ensure that all packages pro...Felix Fietkau2008-01-141-1/+1
* fixup softfloat handlingImre Kaloz2007-12-281-0/+7
* add $(STAGING_DIR)/host, which contains target specific, but host executable ...Felix Fietkau2007-12-271-1/+1
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Claire Xenia Wolf <claire@yosyshq.com>
 *  Copyright (C) 2018  Serge Bazanski <q3k@q3k.org>
 *
 *  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 <algorithm>
#include <cmath>
#include "cells.h"
#include "embed.h"
#include "gfx.h"
#include "log.h"
#include "nextpnr.h"
#include "placer1.h"
#include "placer_heap.h"
#include "router1.h"
#include "router2.h"
#include "timing_opt.h"
#include "util.h"
NEXTPNR_NAMESPACE_BEGIN

// -----------------------------------------------------------------------

void IdString::initialize_arch(const BaseCtx *ctx)
{
#define X(t) initialize_add(ctx, #t, ID_##t);
#include "constids.inc"
#undef X
}

// -----------------------------------------------------------------------

static const ChipInfoPOD *get_chip_info(ArchArgs::ArchArgsTypes chip)
{
    std::string chipdb;
    if (chip == ArchArgs::LP384) {
        chipdb = "ice40/chipdb-384.bin";
    } else if (chip == ArchArgs::LP1K || chip == ArchArgs::HX1K) {
        chipdb = "ice40/chipdb-1k.bin";
    } else if (chip == ArchArgs::U1K || chip == ArchArgs::U2K || chip == ArchArgs::U4K) {
        chipdb = "ice40/chipdb-u4k.bin";
    } else if (chip == ArchArgs::UP3K || chip == ArchArgs::UP5K) {
        chipdb = "ice40/chipdb-5k.bin";
    } else if (chip == ArchArgs::LP8K || chip == ArchArgs::HX8K || chip == ArchArgs::LP4K || chip == ArchArgs::HX4K) {
        chipdb = "ice40/chipdb-8k.bin";
    } else {
        log_error("Unknown chip\n");
    }

    auto ptr = reinterpret_cast<const RelPtr<ChipInfoPOD> *>(get_chipdb(chipdb));
    if (ptr == nullptr)
        return nullptr;
    return ptr->get();
}

bool Arch::is_available(ArchArgs::ArchArgsTypes chip) { return get_chip_info(chip) != nullptr; }

std::vector<std::string> Arch::get_supported_packages(ArchArgs::ArchArgsTypes chip)
{
    const ChipInfoPOD *chip_info = get_chip_info(chip);
    std::vector<std::string> packages;
    for (auto &pkg : chip_info->packages_data) {
        std::string name = pkg.name.get();
        if (chip == ArchArgs::LP4K || chip == ArchArgs::HX4K) {
            if (name.find(":4k") != std::string::npos)
                name = name.substr(0, name.size() - 3);
            else
                continue;
        }
        packages.push_back(name);
    }
    return packages;
}

// -----------------------------------------------------------------------

Arch::Arch(ArchArgs args) : args(args)
{
    fast_part = (args.type == ArchArgs::HX8K || args.type == ArchArgs::HX4K || args.type == ArchArgs::HX1K);

    chip_info = get_chip_info(args.type);
    if (chip_info == nullptr)
        log_error("Unsupported iCE40 chip type.\n");

    package_info = nullptr;
    std::string package_name = args.package;
    if (args.type == ArchArgs::LP4K || args.type == ArchArgs::HX4K)
        package_name += ":4k";

    for (auto &pkg : chip_info->packages_data) {
        if (pkg.name.get() == package_name) {
            package_info = &pkg;
            break;
        }
    }
    if (package_info == nullptr)
        log_error("Unsupported package '%s'.\n", args.package.c_str());

    for (int i = 0; i < chip_info->width; i++) {
        IdString x_id = id(stringf("X%d", i));
        x_ids.push_back(x_id);
        id_to_x[x_id] = i;
    }
    for (int i = 0; i < chip_info->height; i++) {
        IdString y_id = id(stringf("Y%d", i));
        y_ids.push_back(y_id);
        id_to_y[y_id] = i;
    }

    bel_carry.resize(chip_info->bel_data.size());
    bel_to_cell.resize(chip_info->bel_data.size());
    wire_to_net.resize(chip_info->wire_data.size());
    pip_to_net.resize(chip_info->pip_data.size());
    switches_locked.resize(chip_info->num_switches);

    BaseArch::init_cell_types();
    BaseArch::init_bel_buckets();
}

// -----------------------------------------------------------------------

std::string Arch::getChipName() const
{
    if (args.type == ArchArgs::LP384) {
        return "Lattice iCE40LP384";
    } else if (args.type == ArchArgs::LP1K) {
        return "Lattice iCE40LP1K";
    } else if (args.type == ArchArgs::HX1K) {
        return "Lattice iCE40HX1K";
    } else if (args.type == ArchArgs::UP3K) {
        return "Lattice iCE40UP3K";
    } else if (args.type == ArchArgs::UP5K) {
        return "Lattice iCE40UP5K";
    } else if (args.type == ArchArgs::U1K) {
        return "Lattice iCE5LP1K";
    } else if (args.type == ArchArgs::U2K) {
        return "Lattice iCE5LP2K";
    } else if (args.type == ArchArgs::U4K) {
        return "Lattice iCE5LP4K";
    } else if (args.type == ArchArgs::LP4K) {
        return "Lattice iCE40LP4K";
    } else if (args.type == ArchArgs::LP8K) {
        return "Lattice iCE40LP8K";
    } else if (args.type == ArchArgs::HX4K) {
        return "Lattice iCE40HX4K";
    } else if (args.type == ArchArgs::HX8K) {
        return "Lattice iCE40HX8K";
    } else {
        log_error("Unknown chip\n");
    }
}

// -----------------------------------------------------------------------

IdString Arch::archArgsToId(ArchArgs args) const
{
    if (args.type == ArchArgs::LP384)
        return id_lp384;
    if (args.type == ArchArgs::LP1K)
        return id_lp1k;
    if (args.type == ArchArgs::HX1K)
        return id_hx1k;
    if (args.type == ArchArgs::UP3K)
        return id_up3k;
    if (args.type == ArchArgs::UP5K)
        return id_up5k;
    if (args.type == ArchArgs::U1K)
        return id_u1k;
    if (args.type == ArchArgs::U2K)
        return id_u2k;
    if (args.type == ArchArgs::U4K)
        return id_u4k;
    if (args.type == ArchArgs::LP4K)
        return id_lp4k;
    if (args.type == ArchArgs::LP8K)
        return id_lp8k;
    if (args.type == ArchArgs::HX4K)
        return id_hx4k;
    if (args.type == ArchArgs::HX8K)
        return id_hx8k;
    return IdString();
}

// -----------------------------------------------------------------------

BelId Arch::getBelByName(IdStringList name) const
{
    BelId ret;

    if (bel_by_name.empty()) {
        for (size_t i = 0; i < chip_info->bel_data.size(); i++) {
            BelId b;
            b.index = i;
            bel_by_name[getBelName(b)] = i;
        }
    }

    auto it = bel_by_name.find(name);
    if (it != bel_by_name.end())
        ret.index = it->second;

    return ret;
}

BelId Arch::getBelByLocation(Loc loc) const
{
    BelId bel;

    if (bel_by_loc.empty()) {
        for (size_t i = 0; i < chip_info->bel_data.size(); i++) {
            BelId b;
            b.index = i;
            bel_by_loc[getBelLocation(b)] = i;
        }
    }

    auto it = bel_by_loc.find(loc);
    if (it != bel_by_loc.end())
        bel.index = it->second;

    return bel;
}

BelRange Arch::getBelsByTile(int x, int y) const
{
    // In iCE40 chipdb bels at the same tile are consecutive and dense z ordinates
    // are used
    BelRange br;

    for (int i = 0; i < 4; i++) {
        br.b.cursor = Arch::getBelByLocation(Loc(x, y, i)).index;
        if (br.b.cursor != -1)
            break;
    }
    br.e.cursor = br.b.cursor;

    if (br.e.cursor != -1) {
        while (br.e.cursor < chip_info->bel_data.ssize() && chip_info->bel_data[br.e.cursor].x == x &&
               chip_info->bel_data[br.e.cursor].y == y)
            br.e.cursor++;
    }

    return br;
}

PortType Arch::getBelPinType(BelId bel, IdString pin) const
{
    NPNR_ASSERT(bel != BelId());

    int num_bel_wires = chip_info->bel_data[bel.index].bel_wires.size();
    const BelWirePOD *bel_wires = chip_info->bel_data[bel.index].bel_wires.get();

    if (num_bel_wires < 7) {
        for (int i = 0; i < num_bel_wires; i++) {
            if (bel_wires[i].port == pin.index)
                return PortType(bel_wires[i].type);
        }
    } else {
        int b = 0, e = num_bel_wires - 1;
        while (b <= e) {
            int i = (b + e) / 2;
            if (bel_wires[i].port == pin.index)
                return PortType(bel_wires[i].type);
            if (bel_wires[i].port > pin.index)
                e = i - 1;
            else
                b = i + 1;
        }
    }

    return PORT_INOUT;
}

std::vector<std::pair<IdString, std::string>> Arch::getBelAttrs(BelId bel) const
{
    std::vector<std::pair<IdString, std::string>> ret;

    ret.push_back(std::make_pair(id_INDEX, stringf("%d", bel.index)));

    return ret;
}

WireId Arch::getBelPinWire(BelId bel, IdString pin) const
{
    WireId ret;

    NPNR_ASSERT(bel != BelId());

    int num_bel_wires = chip_info->bel_data[bel.index].bel_wires.size();
    const BelWirePOD *bel_wires = chip_info->bel_data[bel.index].bel_wires.get();

    if (num_bel_wires < 7) {
        for (int i = 0; i < num_bel_wires; i++) {
            if (bel_wires[i].port == pin.index) {
                ret.index = bel_wires[i].wire_index;
                break;
            }
        }
    } else {
        int b = 0, e = num_bel_wires - 1;
        while (b <= e) {
            int i = (b + e) / 2;
            if (bel_wires[i].port == pin.index) {
                ret.index = bel_wires[i].wire_index;
                break;
            }
            if (bel_wires[i].port > pin.index)
                e = i - 1;
            else
                b = i + 1;
        }
    }

    return ret;
}

std::vector<IdString> Arch::getBelPins(BelId bel) const
{
    std::vector<IdString> ret;

    NPNR_ASSERT(bel != BelId());

    for (auto &w : chip_info->bel_data[bel.index].bel_wires)
        ret.push_back(IdString(w.port));

    return ret;
}

bool Arch::is_bel_locked(BelId bel) const
{
    const BelConfigPOD *bel_config = nullptr;
    for (auto &bel_cfg : chip_info->bel_config) {
        if (bel_cfg.bel_index == bel.index) {
            bel_config = &bel_cfg;
            break;
        }
    }
    NPNR_ASSERT(bel_config != nullptr);
    for (auto &entry : bel_config->entries) {
        if (strcmp("LOCKED", entry.cbit_name.get()))
            continue;
        if ("LOCKED_" + archArgs().package == entry.entry_name.get())
            return true;
    }
    return false;
}

// -----------------------------------------------------------------------

WireId Arch::getWireByName(IdStringList name) const
{
    WireId ret;

    if (wire_by_name.empty()) {
        for (int i = 0; i < chip_info->wire_data.ssize(); i++) {
            WireId w;
            w.index = i;
            wire_by_name[getWireName(w)] = i;
        }
    }

    auto it = wire_by_name.find(name);
    if (it != wire_by_name.end())
        ret.index = it->second;

    return ret;
}

IdString Arch::getWireType(WireId wire) const
{
    NPNR_ASSERT(wire != WireId());
    switch (chip_info->wire_data[wire.index].type) {
    case WireInfoPOD::WIRE_TYPE_NONE:
        return IdString();
    case WireInfoPOD::WIRE_TYPE_GLB2LOCAL:
        return id_GLB2LOCAL;
    case WireInfoPOD::WIRE_TYPE_GLB_NETWK:
        return id_GLB_NETWK;
    case WireInfoPOD::WIRE_TYPE_LOCAL:
        return id_LOCAL;
    case WireInfoPOD::WIRE_TYPE_LUTFF_IN:
        return id_LUTFF_IN;
    case WireInfoPOD::WIRE_TYPE_LUTFF_IN_LUT:
        return id_LUTFF_IN_LUT;
    case WireInfoPOD::WIRE_TYPE_LUTFF_LOUT:
        return id_LUTFF_LOUT;
    case WireInfoPOD::WIRE_TYPE_LUTFF_OUT:
        return id_LUTFF_OUT;
    case WireInfoPOD::WIRE_TYPE_LUTFF_COUT:
        return id_LUTFF_COUT;
    case WireInfoPOD::WIRE_TYPE_LUTFF_GLOBAL:
        return id_LUTFF_GLOBAL;
    case WireInfoPOD::WIRE_TYPE_CARRY_IN_MUX:
        return id_CARRY_IN_MUX;
    case WireInfoPOD::WIRE_TYPE_SP4_V:
        return id_SP4_V;
    case WireInfoPOD::WIRE_TYPE_SP4_H:
        return id_SP4_H;
    case WireInfoPOD::WIRE_TYPE_SP12_V:
        return id_SP12_V;
    case WireInfoPOD::WIRE_TYPE_SP12_H:
        return id_SP12_H;
    }
    return IdString();
}

std::vector<std::pair<IdString, std::string>> Arch::getWireAttrs(WireId wire) const
{
    std::vector<std::pair<IdString, std::string>> ret;
    auto &wi = chip_info->wire_data[wire.index];

    ret.push_back(std::make_pair(id_INDEX, stringf("%d", wire.index)));

    ret.push_back(std::make_pair(id_GRID_X, stringf("%d", wi.x)));
    ret.push_back(std::make_pair(id_GRID_Y, stringf("%d", wi.y)));
    ret.push_back(std::make_pair(id_GRID_Z, stringf("%d", wi.z)));

    return ret;
}

// -----------------------------------------------------------------------

PipId Arch::getPipByName(IdStringList name) const
{
    PipId ret;

    if (pip_by_name.empty()) {
        for (int i = 0; i < chip_info->pip_data.ssize(); i++) {
            PipId pip;
            pip.index = i;
            pip_by_name[getPipName(pip)] = i;
        }
    }

    auto it = pip_by_name.find(name);
    if (it != pip_by_name.end())
        ret.index = it->second;

    return ret;
}

IdStringList Arch::getPipName(PipId pip) const
{
    NPNR_ASSERT(pip != PipId());

    int x = chip_info->pip_data[pip.index].x;
    int y = chip_info->pip_data[pip.index].y;

    auto &src_wire = chip_info->wire_data[chip_info->pip_data[pip.index].src];
    auto &dst_wire = chip_info->wire_data[chip_info->pip_data[pip.index].dst];

    std::string src_name = stringf("%d.%d.%s", int(src_wire.name_x), int(src_wire.name_y), src_wire.name.get());
    std::string dst_name = stringf("%d.%d.%s", int(dst_wire.name_x), int(dst_wire.name_y), dst_wire.name.get());

    std::array<IdString, 3> ids{x_ids.at(x), y_ids.at(y), id(src_name + ".->." + dst_name)};
    return IdStringList(ids);
}

IdString Arch::getPipType(PipId pip) const { return IdString(); }

std::vector<std::pair<IdString, std::string>> Arch::getPipAttrs(PipId pip) const
{
    std::vector<std::pair<IdString, std::string>> ret;

    ret.push_back(std::make_pair(id_INDEX, stringf("%d", pip.index)));

    return ret;
}

// -----------------------------------------------------------------------

BelId Arch::get_package_pin_bel(const std::string &pin) const
{
    for (auto &ppin : package_info->pins) {
        if (ppin.name.get() == pin) {
            BelId id;
            id.index = ppin.bel_index;
            return id;
        }
    }
    return BelId();
}

std::string Arch::get_bel_package_pin(BelId bel) const
{
    for (auto &ppin : package_info->pins) {
        if (ppin.bel_index == bel.index) {
            return std::string(ppin.name.get());
        }
    }
    return "";
}

// -----------------------------------------------------------------------

GroupId Arch::getGroupByName(IdStringList name) const
{
    for (auto g : getGroups())
        if (getGroupName(g) == name)
            return g;
    return GroupId();
}

IdStringList Arch::getGroupName(GroupId group) const
{
    std::string suffix;

    switch (group.type) {
    case GroupId::TYPE_FRAME:
        suffix = "tile";
        break;
    case GroupId::TYPE_MAIN_SW:
        suffix = "main_sw";
        break;
    case GroupId::TYPE_LOCAL_SW:
        suffix = "local_sw";
        break;
    case GroupId::TYPE_LC0_SW:
        suffix = "lc0_sw";
        break;
    case GroupId::TYPE_LC1_SW:
        suffix = "lc1_sw";
        break;
    case GroupId::TYPE_LC2_SW:
        suffix = "lc2_sw";
        break;
    case GroupId::TYPE_LC3_SW:
        suffix = "lc3_sw";
        break;
    case GroupId::TYPE_LC4_SW:
        suffix = "lc4_sw";
        break;
    case GroupId::TYPE_LC5_SW:
        suffix = "lc5_sw";
        break;
    case GroupId::TYPE_LC6_SW:
        suffix = "lc6_sw";
        break;
    case GroupId::TYPE_LC7_SW:
        suffix = "lc7_sw";
        break;
    default:
        return IdStringList();
    }

    std::array<IdString, 3> ids{x_ids.at(group.x), y_ids.at(group.y), id(suffix)};
    return IdStringList(ids);
}

std::vector<GroupId> Arch::getGroups() const
{
    std::vector<GroupId> ret;

    for (int y = 0; y < chip_info->height; y++) {
        for (int x = 0; x < chip_info->width; x++) {
            TileType type = chip_info->tile_grid[y * chip_info->width + x];
            if (type == TILE_NONE)
                continue;

            GroupId group;
            group.type = GroupId::TYPE_FRAME;
            group.x = x;
            group.y = y;
            // ret.push_back(group);

            group.type = GroupId::TYPE_MAIN_SW;
            ret.push_back(group);

            group.type = GroupId::TYPE_LOCAL_SW;
            ret.push_back(group);

            if (type == TILE_LOGIC) {
                group.type = GroupId::TYPE_LC0_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC1_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC2_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC3_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC4_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC5_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC6_SW;
                ret.push_back(group);

                group.type = GroupId::TYPE_LC7_SW;
                ret.push_back(group);
            }
        }
    }
    return ret;
}

std::vector<BelId> Arch::getGroupBels(GroupId group) const
{
    std::vector<BelId> ret;
    return ret;
}

std::vector<WireId> Arch::getGroupWires(GroupId group) const
{
    std::vector<WireId> ret;
    return ret;
}

std::vector<PipId> Arch::getGroupPips(GroupId group) const
{
    std::vector<PipId> ret;
    return ret;
}

std::vector<GroupId> Arch::getGroupGroups(GroupId group) const
{
    std::vector<GroupId> ret;
    return ret;
}

// -----------------------------------------------------------------------

bool Arch::getBudgetOverride(const NetInfo *net_info, const PortRef &sink, delay_t &budget) const
{
    const auto &driver = net_info->driver;
    if (driver.port == id_COUT) {
        NPNR_ASSERT(sink.port == id_CIN || sink.port == id_I3);
        NPNR_ASSERT(driver.cell->constr_abs_z);
        bool cin = sink.port == id_CIN;
        bool same_y = driver.cell->constr_z < 7;
        if (cin && same_y)
            budget = 0;
        else {
            switch (args.type) {
            case ArchArgs::HX8K:
            case ArchArgs::HX4K:
            case ArchArgs::HX1K:
                budget = cin ? 190 : (same_y ? 260 : 560);
                break;
            case ArchArgs::LP384:
            case ArchArgs::LP1K:
            case ArchArgs::LP4K:
            case ArchArgs::LP8K:
                budget = cin ? 290 : (same_y ? 380 : 670);
                break;
            case ArchArgs::UP3K:
            case ArchArgs::UP5K:
            case ArchArgs::U1K:
            case ArchArgs::U2K:
            case ArchArgs::U4K:
                budget = cin ? 560 : (same_y ? 660 : 1220);
                break;
            default:
                log_error("Unsupported iCE40 chip type.\n");
            }
        }
        return true;
    }
    return false;
}

// -----------------------------------------------------------------------

bool Arch::place()
{
    std::string placer = str_or_default(settings, id_placer, defaultPlacer);
    if (placer == "heap") {
        PlacerHeapCfg cfg(getCtx());
        cfg.ioBufTypes.insert(id_SB_IO);
        if (!placer_heap(getCtx(), cfg))
            return false;
    } else if (placer == "sa") {
        if (!placer1(getCtx(), Placer1Cfg(getCtx())))
            return false;
    } else {
        log_error("iCE40 architecture does not support placer '%s'\n", placer.c_str());
    }
    bool retVal = true;
    if (bool_or_default(settings, id_opt_timing, false)) {
        TimingOptCfg tocfg(getCtx());
        tocfg.cellTypes.insert(id_ICESTORM_LC);
        retVal = timing_opt(getCtx(), tocfg);
    }
    getCtx()->settings[id_place] = 1;
    archInfoToAttributes();
    return retVal;
}

bool Arch::route()
{
    std::string router = str_or_default(settings, id_router, defaultRouter);
    bool result;
    if (router == "router1") {
        result = router1(getCtx(), Router1Cfg(getCtx()));
    } else if (router == "router2") {
        router2(getCtx(), Router2Cfg(getCtx()));
        result = true;
    } else {
        log_error("iCE40 architecture does not support router '%s'\n", router.c_str());
    }
    getCtx()->settings[id_route] = 1;
    archInfoToAttributes();
    return result;
}

// -----------------------------------------------------------------------

DecalXY Arch::getBelDecal(BelId bel) const
{
    DecalXY decalxy;
    decalxy.decal.type = DecalId::TYPE_BEL;
    decalxy.decal.index = bel.index;
    decalxy.decal.active = bel_to_cell.at(bel.index) != nullptr;
    return decalxy;
}

DecalXY Arch::getWireDecal(WireId wire) const
{
    DecalXY decalxy;
    decalxy.decal.type = DecalId::TYPE_WIRE;
    decalxy.decal.index = wire.index;
    decalxy.decal.active = wire_to_net.at(wire.index) != nullptr;
    return decalxy;
}

DecalXY Arch::getPipDecal(PipId pip) const
{
    DecalXY decalxy;
    decalxy.decal.type = DecalId::TYPE_PIP;
    decalxy.decal.index = pip.index;
    decalxy.decal.active = pip_to_net.at(pip.index) != nullptr;
    return decalxy;
};

DecalXY Arch::getGroupDecal(GroupId group) const
{
    DecalXY decalxy;
    decalxy.decal.type = DecalId::TYPE_GROUP;
    decalxy.decal.index = (group.type << 16) | (group.x << 8) | (group.y);
    decalxy.decal.active = true;
    return decalxy;
};

std::vector<GraphicElement> Arch::getDecalGraphics(DecalId decal) const
{
    std::vector<GraphicElement> ret;

    if (decal.type == DecalId::TYPE_GROUP) {
        int type = (decal.index >> 16) & 255;
        int x = (decal.index >> 8) & 255;
        int y = decal.index & 255;

        if (type == GroupId::TYPE_FRAME) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_LINE;
            el.style = GraphicElement::STYLE_FRAME;

            el.x1 = x + 0.01, el.x2 = x + 0.02, el.y1 = y + 0.01, el.y2 = y + 0.01;
            ret.push_back(el);
            el.x1 = x + 0.01, el.x2 = x + 0.01, el.y1 = y + 0.01, el.y2 = y + 0.02;
            ret.push_back(el);

            el.x1 = x + 0.99, el.x2 = x + 0.98, el.y1 = y + 0.01, el.y2 = y + 0.01;
            ret.push_back(el);
            el.x1 = x + 0.99, el.x2 = x + 0.99, el.y1 = y + 0.01, el.y2 = y + 0.02;
            ret.push_back(el);

            el.x1 = x + 0.99, el.x2 = x + 0.98, el.y1 = y + 0.99, el.y2 = y + 0.99;
            ret.push_back(el);
            el.x1 = x + 0.99, el.x2 = x + 0.99, el.y1 = y + 0.99, el.y2 = y + 0.98;
            ret.push_back(el);

            el.x1 = x + 0.01, el.x2 = x + 0.02, el.y1 = y + 0.99, el.y2 = y + 0.99;
            ret.push_back(el);
            el.x1 = x + 0.01, el.x2 = x + 0.01, el.y1 = y + 0.99, el.y2 = y + 0.98;
            ret.push_back(el);
        }

        if (type == GroupId::TYPE_MAIN_SW) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = GraphicElement::STYLE_FRAME;

            el.x1 = x + main_swbox_x1;
            el.x2 = x + main_swbox_x2;
            el.y1 = y + main_swbox_y1;
            el.y2 = y + main_swbox_y2;
            ret.push_back(el);
        }

        if (type == GroupId::TYPE_LOCAL_SW) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = GraphicElement::STYLE_FRAME;

            el.x1 = x + local_swbox_x1;
            el.x2 = x + local_swbox_x2;
            el.y1 = y + local_swbox_y1;
            el.y2 = y + local_swbox_y2;
            ret.push_back(el);
        }

        if (GroupId::TYPE_LC0_SW <= type && type <= GroupId::TYPE_LC7_SW) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = GraphicElement::STYLE_FRAME;

            el.x1 = x + lut_swbox_x1;
            el.x2 = x + lut_swbox_x2;
            el.y1 = y + logic_cell_y1 + logic_cell_pitch * (type - GroupId::TYPE_LC0_SW);
            el.y2 = y + logic_cell_y2 + logic_cell_pitch * (type - GroupId::TYPE_LC0_SW);
            ret.push_back(el);
        }
    }

    if (decal.type == DecalId::TYPE_WIRE) {
        int n = chip_info->wire_data[decal.index].segments.size();
        const WireSegmentPOD *p = chip_info->wire_data[decal.index].segments.get();

        GraphicElement::style_t style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;

        for (int i = 0; i < n; i++)
            gfxTileWire(ret, p[i].x, p[i].y, chip_info->width, chip_info->height, GfxTileWireId(p[i].index), style);
    }

    if (decal.type == DecalId::TYPE_PIP) {
        const PipInfoPOD &p = chip_info->pip_data[decal.index];
        GraphicElement::style_t style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_HIDDEN;
        gfxTilePip(ret, p.x, p.y, GfxTileWireId(p.src_seg), GfxTileWireId(p.dst_seg), style);
    }

    if (decal.type == DecalId::TYPE_BEL) {
        BelId bel;
        bel.index = decal.index;

        auto bel_type = getBelType(bel);

        if (bel_type == id_ICESTORM_LC) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;
            el.x1 = chip_info->bel_data[bel.index].x + logic_cell_x1;
            el.x2 = chip_info->bel_data[bel.index].x + logic_cell_x2;
            el.y1 = chip_info->bel_data[bel.index].y + logic_cell_y1 +
                    (chip_info->bel_data[bel.index].z) * logic_cell_pitch;
            el.y2 = chip_info->bel_data[bel.index].y + logic_cell_y2 +
                    (chip_info->bel_data[bel.index].z) * logic_cell_pitch;
            ret.push_back(el);
        }

        if (bel_type == id_SB_IO) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;
            el.x1 = chip_info->bel_data[bel.index].x + lut_swbox_x1;
            el.x2 = chip_info->bel_data[bel.index].x + logic_cell_x2;
            el.y1 = chip_info->bel_data[bel.index].y + logic_cell_y1 +
                    (4 * chip_info->bel_data[bel.index].z) * logic_cell_pitch;
            el.y2 = chip_info->bel_data[bel.index].y + logic_cell_y2 +
                    (4 * chip_info->bel_data[bel.index].z + 3) * logic_cell_pitch;
            ret.push_back(el);
        }

        if (bel_type == id_ICESTORM_RAM) {
            for (int i = 0; i < 2; i++) {
                GraphicElement el;
                el.type = GraphicElement::TYPE_BOX;
                el.style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;
                el.x1 = chip_info->bel_data[bel.index].x + lut_swbox_x1;
                el.x2 = chip_info->bel_data[bel.index].x + logic_cell_x2;
                el.y1 = chip_info->bel_data[bel.index].y + logic_cell_y1 + i;
                el.y2 = chip_info->bel_data[bel.index].y + logic_cell_y2 + i + 7 * logic_cell_pitch;
                ret.push_back(el);
            }
        }

        if (bel_type == id_SB_GB) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;
            el.x1 = chip_info->bel_data[bel.index].x + local_swbox_x1 + 0.05;
            el.x2 = chip_info->bel_data[bel.index].x + logic_cell_x2 - 0.05;
            el.y1 = chip_info->bel_data[bel.index].y + main_swbox_y2 - 0.05;
            el.y2 = chip_info->bel_data[bel.index].y + main_swbox_y2 - 0.10;
            ret.push_back(el);
        }

        if (bel_type == id_ICESTORM_PLL || bel_type == id_SB_WARMBOOT) {
            GraphicElement el;
            el.type = GraphicElement::TYPE_BOX;
            el.style = decal.active ? GraphicElement::STYLE_ACTIVE : GraphicElement::STYLE_INACTIVE;
            el.x1 = chip_info->bel_data[bel.index].x + local_swbox_x1 + 0.05;
            el.x2 = chip_info->bel_data[bel.index].x + logic_cell_x2 - 0.05;
            el.y1 = chip_info->bel_data[bel.index].y + main_swbox_y2;
            el.y2 = chip_info->bel_data[bel.index].y + main_swbox_y2 + 0.05;
            ret.push_back(el);
        }
    }

    return ret;
}

// -----------------------------------------------------------------------

bool Arch::getCellDelay(const CellInfo *cell, IdString fromPort, IdString toPort, DelayQuad &delay) const
{
    if (cell->type == id_ICESTORM_LC && cell->lcInfo.dffEnable) {
        if (toPort == id_O)
            return false;
    } else if (cell->type == id_ICESTORM_RAM || cell->type == id_ICESTORM_SPRAM) {
        return false;
    }
    return get_cell_delay_internal(cell, fromPort, toPort, delay);
}

bool Arch::get_cell_delay_internal(const CellInfo *cell, IdString fromPort, IdString toPort, DelayQuad &delay) const
{
    for (auto &tc : chip_info->cell_timing) {
        if (tc.type == cell->type.index) {
            for (auto &path : tc.path_delays) {
                if (path.from_port == fromPort.index && path.to_port == toPort.index) {
                    if (fast_part)
                        delay = DelayQuad(path.fast_delay);
                    else
                        delay = DelayQuad(path.slow_delay);
                    return true;
                }
            }
            break;
        }
    }
    return false;
}

// Get the port class, also setting clockPort to associated clock if applicable
TimingPortClass Arch::getPortTimingClass(const CellInfo *cell, IdString port, int &clockInfoCount) const
{
    clockInfoCount = 0;
    if (cell->type == id_ICESTORM_LC) {
        if (port == id_CLK)
            return TMG_CLOCK_INPUT;
        if (port == id_CIN)
            return TMG_COMB_INPUT;
        if (port == id_COUT || port == id_LO)
            return TMG_COMB_OUTPUT;
        if (port == id_O) {
            // LCs with no inputs are constant drivers
            if (cell->lcInfo.inputCount == 0)
                return TMG_IGNORE;
            if (cell->lcInfo.dffEnable) {
                clockInfoCount = 1;
                return TMG_REGISTER_OUTPUT;
            } else
                return TMG_COMB_OUTPUT;
        } else {
            if (cell->lcInfo.dffEnable) {
                clockInfoCount = 1;
                return TMG_REGISTER_INPUT;
            } else
                return TMG_COMB_INPUT;
        }
    } else if (cell->type == id_ICESTORM_RAM) {

        if (port == id_RCLK || port == id_WCLK)
            return TMG_CLOCK_INPUT;

        clockInfoCount = 1;

        if (cell->ports.at(port).type == PORT_OUT)
            return TMG_REGISTER_OUTPUT;
        else
            return TMG_REGISTER_INPUT;
    } else if (cell->type == id_ICESTORM_DSP || cell->type == id_ICESTORM_SPRAM) {
        if (port == id_CLK || port == id_CLOCK)
            return TMG_CLOCK_INPUT;
        else {
            clockInfoCount = 1;
            if (cell->ports.at(port).type == PORT_OUT)
                return TMG_REGISTER_OUTPUT;
            else
                return TMG_REGISTER_INPUT;
        }
    } else if (cell->type == id_SB_IO) {
        if (port == id_INPUT_CLK || port == id_OUTPUT_CLK)
            return TMG_CLOCK_INPUT;
        if (port == id_CLOCK_ENABLE) {
            clockInfoCount = 2;
            return TMG_REGISTER_INPUT;
        }
        if ((port == id_D_IN_0 && !(cell->ioInfo.pintype & 0x1)) || port == id_D_IN_1) {
            clockInfoCount = 1;
            return TMG_REGISTER_OUTPUT;
        } else if (port == id_D_IN_0) {
            return TMG_STARTPOINT;
        }
        if (port == id_D_OUT_0 || port == id_D_OUT_1) {
            if ((cell->ioInfo.pintype & 0xC) == 0x8) {
                return TMG_ENDPOINT;
            } else {
                clockInfoCount = 1;
                return TMG_REGISTER_INPUT;
            }
        }
        if (port == id_OUTPUT_ENABLE) {
            if ((cell->ioInfo.pintype & 0x30) == 0x30) {
                return TMG_REGISTER_INPUT;
            } else {
                return TMG_ENDPOINT;
            }
        }

        return TMG_IGNORE;
    } else if (cell->type == id_ICESTORM_PLL) {
        if (port == id_PLLOUT_A || port == id_PLLOUT_B || port == id_PLLOUT_A_GLOBAL || port == id_PLLOUT_B_GLOBAL)
            return TMG_GEN_CLOCK;
        return TMG_IGNORE;
    } else if (cell->type == id_ICESTORM_LFOSC) {
        if (port == id_CLKLF)
            return TMG_GEN_CLOCK;
        return TMG_IGNORE;
    } else if (cell->type == id_ICESTORM_HFOSC) {
        if (port == id_CLKHF)
            return TMG_GEN_CLOCK;
        return TMG_IGNORE;
    } else if (cell->type == id_SB_GB) {
        if (port == id_GLOBAL_BUFFER_OUTPUT)
            return cell->gbInfo.forPadIn ? TMG_GEN_CLOCK : TMG_COMB_OUTPUT;
        return TMG_COMB_INPUT;
    } else if (cell->type == id_SB_WARMBOOT) {
        return TMG_ENDPOINT;
    } else if (cell->type == id_SB_LED_DRV_CUR) {
        if (port == id_LEDPU)
            return TMG_IGNORE;
        return TMG_ENDPOINT;
    } else if (cell->type == id_SB_RGB_DRV) {
        if (port == id_RGB0 || port == id_RGB1 || port == id_RGB2 || port == id_RGBPU)
            return TMG_IGNORE;
        return TMG_ENDPOINT;
    } else if (cell->type == id_SB_RGBA_DRV) {
        if (port == id_RGB0 || port == id_RGB1 || port == id_RGB2)
            return TMG_IGNORE;
        return TMG_ENDPOINT;
    } else if (cell->type == id_SB_LEDDA_IP) {
        if (port == id_CLK || port == id_CLOCK)
            return TMG_CLOCK_INPUT;
        return TMG_IGNORE;
    } else if (cell->type == id_SB_I2C || cell->type == id_SB_SPI) {
        if (port == id_SBCLKI)
            return TMG_CLOCK_INPUT;

        clockInfoCount = 1;

        if (cell->ports.at(port).type == PORT_OUT)
            return TMG_REGISTER_OUTPUT;
        else
            return TMG_REGISTER_INPUT;
    }
    log_error("cell type '%s' is unsupported (instantiated as '%s')\n", cell->type.c_str(this), cell->name.c_str(this));
}

TimingClockingInfo Arch::getPortClockingInfo(const CellInfo *cell, IdString port, int index) const
{
    TimingClockingInfo info;
    if (cell->type == id_ICESTORM_LC) {
        info.clock_port = id_CLK;
        info.edge = cell->lcInfo.negClk ? FALLING_EDGE : RISING_EDGE;
        if (port == id_O) {
            bool has_clktoq = get_cell_delay_internal(cell, id_CLK, id_O, info.clockToQ);
            NPNR_ASSERT(has_clktoq);
        } else {
            if (port == id_I0 || port == id_I1 || port == id_I2 || port == id_I3) {
                DelayQuad dlut;
                bool has_ld = get_cell_delay_internal(cell, port, id_O, dlut);
                NPNR_ASSERT(has_ld);
                if (args.type == ArchArgs::LP1K || args.type == ArchArgs::LP4K || args.type == ArchArgs::LP8K ||
                    args.type == ArchArgs::LP384) {
                    info.setup = DelayPair(30 + dlut.maxDelay());
                } else if (args.type == ArchArgs::UP3K || args.type == ArchArgs::UP5K || args.type == ArchArgs::U4K ||
                           args.type == ArchArgs::U1K || args.type == ArchArgs::U2K) { // XXX verify u4k
                    info.setup = DelayPair(dlut.maxDelay() - 50);
                } else {
                    info.setup = DelayPair(20 + dlut.maxDelay());
                }
            } else {
                info.setup = DelayPair(100);
            }
            info.hold = DelayPair(0);
        }
    } else if (cell->type == id_ICESTORM_RAM) {
        if (port.str(this)[0] == 'R') {
            info.clock_port = id_RCLK;
            info.edge = bool_or_default(cell->params, id_NEG_CLK_R) ? FALLING_EDGE : RISING_EDGE;
        } else {
            info.clock_port = id_WCLK;
            info.edge = bool_or_default(cell->params, id_NEG_CLK_W) ? FALLING_EDGE : RISING_EDGE;
        }
        if (cell->ports.at(port).type == PORT_OUT) {
            bool has_clktoq = get_cell_delay_internal(cell, info.clock_port, port, info.clockToQ);
            NPNR_ASSERT(has_clktoq);
        } else {
            info.setup = DelayPair(100);
            info.hold = DelayPair(0);
        }
    } else if (cell->type == id_SB_IO) {
        delay_t io_setup = 80, io_clktoq = 140;
        if (args.type == ArchArgs::LP1K || args.type == ArchArgs::LP8K || args.type == ArchArgs::LP384) {
            io_setup = 115;
            io_clktoq = 210;
        } else if (args.type == ArchArgs::UP3K || args.type == ArchArgs::UP5K || args.type == ArchArgs::U4K ||
                   args.type == ArchArgs::U1K || args.type == ArchArgs::U2K) {
            io_setup = 205;
            io_clktoq = 1005;
        }
        if (port == id_CLOCK_ENABLE) {
            info.clock_port = (index == 1) ? id_OUTPUT_CLK : id_INPUT_CLK;
            info.edge = cell->ioInfo.negtrig ? FALLING_EDGE : RISING_EDGE;
            info.setup = DelayPair(io_setup);
            info.hold = DelayPair(0);
        } else if (port == id_D_OUT_0 || port == id_OUTPUT_ENABLE) {
            info.clock_port = id_OUTPUT_CLK;
            info.edge = cell->ioInfo.negtrig ? FALLING_EDGE : RISING_EDGE;
            info.setup = DelayPair(io_setup);
            info.hold = DelayPair(0);
        } else if (port == id_D_OUT_1) {
            info.clock_port = id_OUTPUT_CLK;
            info.edge = cell->ioInfo.negtrig ? RISING_EDGE : FALLING_EDGE;
            info.setup = DelayPair(io_setup);
            info.hold = DelayPair(0);
        } else if (port == id_D_IN_0) {
            info.clock_port = id_INPUT_CLK;
            info.edge = cell->ioInfo.negtrig ? FALLING_EDGE : RISING_EDGE;
            info.clockToQ = DelayQuad(io_clktoq);
        } else if (port == id_D_IN_1) {
            info.clock_port = id_INPUT_CLK;
            info.edge = cell->ioInfo.negtrig ? RISING_EDGE : FALLING_EDGE;
            info.clockToQ = DelayQuad(io_clktoq);
        } else {
            NPNR_ASSERT_FALSE("no clock data for IO cell port");
        }
    } else if (cell->type == id_ICESTORM_DSP || cell->type == id_ICESTORM_SPRAM) {
        info.clock_port = cell->type == id_ICESTORM_SPRAM ? id_CLOCK : id_CLK;
        info.edge = RISING_EDGE;
        if (cell->ports.at(port).type == PORT_OUT) {
            bool has_clktoq = get_cell_delay_internal(cell, info.clock_port, port, info.clockToQ);
            if (!has_clktoq)
                info.clockToQ = DelayQuad(100);
        } else {
            info.setup = DelayPair(100);
            info.hold = DelayPair(0);
        }
    } else if (cell->type == id_SB_I2C || cell->type == id_SB_SPI) {
        info.clock_port = id_SBCLKI;
        info.edge = RISING_EDGE;
        if (cell->ports.at(port).type == PORT_OUT) {
            /* Dummy number */
            info.clockToQ = DelayQuad(1500);
        } else {
            /* Dummy number */
            info.setup = DelayPair(1500);
            info.hold = DelayPair(0);
        }
    } else {
        NPNR_ASSERT_FALSE("unhandled cell type in getPortClockingInfo");
    }
    return info;
}

bool Arch::is_global_net(const NetInfo *net) const
{
    if (net == nullptr)
        return false;
    return net->driver.cell != nullptr && net->driver.port == id_GLOBAL_BUFFER_OUTPUT;
}

// Assign arch arg info
void Arch::assignArchInfo()
{
    for (auto &net : getCtx()->nets) {
        NetInfo *ni = net.second.get();
        if (is_global_net(ni))
            ni->is_global = true;
        ni->is_enable = false;
        ni->is_reset = false;
        for (auto usr : ni->users) {
            if (is_enable_port(this, usr))
                ni->is_enable = true;
            if (is_reset_port(this, usr))
                ni->is_reset = true;
        }
    }
    for (auto &cell : getCtx()->cells) {
        CellInfo *ci = cell.second.get();
        assignCellInfo(ci);
    }
}

void Arch::assignCellInfo(CellInfo *cell)
{
    if (cell->type == id_ICESTORM_LC) {
        cell->lcInfo.dffEnable = bool_or_default(cell->params, id_DFF_ENABLE);
        cell->lcInfo.carryEnable = bool_or_default(cell->params, id_CARRY_ENABLE);
        cell->lcInfo.negClk = bool_or_default(cell->params, id_NEG_CLK);
        cell->lcInfo.clk = cell->getPort(id_CLK);
        cell->lcInfo.cen = cell->getPort(id_CEN);
        cell->lcInfo.sr = cell->getPort(id_SR);
        cell->lcInfo.inputCount = 0;
        if (cell->getPort(id_I0))
            cell->lcInfo.inputCount++;
        if (cell->getPort(id_I1))
            cell->lcInfo.inputCount++;
        if (cell->getPort(id_I2))
            cell->lcInfo.inputCount++;
        if (cell->getPort(id_I3))
            cell->lcInfo.inputCount++;
    } else if (cell->type == id_SB_IO) {
        cell->ioInfo.lvds = str_or_default(cell->params, id_IO_STANDARD, "SB_LVCMOS") == "SB_LVDS_INPUT";
        cell->ioInfo.global = bool_or_default(cell->attrs, id_GLOBAL);
        cell->ioInfo.pintype = int_or_default(cell->params, id_PIN_TYPE);
        cell->ioInfo.negtrig = bool_or_default(cell->params, id_NEG_TRIGGER);

    } else if (cell->type == id_SB_GB) {
        cell->gbInfo.forPadIn = bool_or_default(cell->attrs, id_FOR_PAD_IN);
    }
}

ArcBounds Arch::getRouteBoundingBox(WireId src, WireId dst) const
{
    ArcBounds bb;

    int src_x = chip_info->wire_data[src.index].x;
    int src_y = chip_info->wire_data[src.index].y;
    int dst_x = chip_info->wire_data[dst.index].x;
    int dst_y = chip_info->wire_data[dst.index].y;

    bb.x0 = src_x;
    bb.y0 = src_y;
    bb.x1 = src_x;
    bb.y1 = src_y;

    auto extend = [&](int x, int y) {
        bb.x0 = std::min(bb.x0, x);
        bb.x1 = std::max(bb.x1, x);
        bb.y0 = std::min(bb.y0, y);
        bb.y1 = std::max(bb.y1, y);
    };
    extend(dst_x, dst_y);
    return bb;
}

#ifdef WITH_HEAP
const std::string Arch::defaultPlacer = "heap";
#else
const std::string Arch::defaultPlacer = "sa";
#endif

const std::vector<std::string> Arch::availablePlacers = {"sa",
#ifdef WITH_HEAP
                                                         "heap"
#endif
};

const std::string Arch::defaultRouter = "router1";
const std::vector<std::string> Arch::availableRouters = {"router1", "router2"};

NEXTPNR_NAMESPACE_END