/* * nextpnr -- Next Generation Place and Route * * Copyright (C) 2018 Claire Xenia Wolf * Copyright (C) 2018 Serge Bazanski * * 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. * */ #ifndef BASE_ARCH_H #define BASE_ARCH_H #include #include #include "arch_api.h" #include "base_clusterinfo.h" #include "idstring.h" #include "nextpnr_types.h" NEXTPNR_NAMESPACE_BEGIN namespace { // For several functions; such as bel/wire/pip attributes; the trivial implementation is to return an empty vector // But an arch might want to do something fancy with a custom range type that doesn't provide a constructor // So some cursed C++ is needed to return an empty object if possible; or error out if not; is needed template typename std::enable_if::value, Tc>::type empty_if_possible() { return Tc(); } template typename std::enable_if::value, Tc>::type empty_if_possible() { NPNR_ASSERT_FALSE("attempting to use default implementation of range-returning function with range type lacking " "default constructor!"); } // Provide a default implementation of bel bucket name if typedef'd to IdString template typename std::enable_if::value, IdString>::type bbid_to_name(Tbbid id) { return id; } template typename std::enable_if::value, IdString>::type bbid_to_name(Tbbid id) { NPNR_ASSERT_FALSE("getBelBucketName must be implemented when BelBucketId is a type other than IdString!"); } template typename std::enable_if::value, BelBucketId>::type bbid_from_name(IdString name) { return name; } template typename std::enable_if::value, BelBucketId>::type bbid_from_name(IdString name) { NPNR_ASSERT_FALSE("getBelBucketByName must be implemented when BelBucketId is a type other than IdString!"); } // For the cell type and bel type ranges; we want to return our stored vectors only if the type matches template typename std::enable_if::value, Tret>::type return_if_match(Tret r) { return r; } template typename std::enable_if::value, Tc>::type return_if_match(Tret r) { NPNR_ASSERT_FALSE("default implementations of cell type and bel bucket range functions only available when the " "respective range types are 'const std::vector&'"); } // Default implementations of the clustering functions template typename std::enable_if::value, CellInfo *>::type get_cluster_root(const BaseCtx *ctx, Tid cluster) { return ctx->cells.at(cluster).get(); } template typename std::enable_if::value, CellInfo *>::type get_cluster_root(const BaseCtx *ctx, Tid cluster) { NPNR_ASSERT_FALSE("default implementation of getClusterRootCell requires ClusterId to be IdString"); } // Executes the lambda with the base cluster data, only if the derivation works template typename std::enable_if::value, Tret>::type if_using_basecluster(const Tcell *cell, Tfunc func) { return func(static_cast(cell)); } template typename std::enable_if::value, Tret>::type if_using_basecluster(const Tcell *cell, Tfunc func) { NPNR_ASSERT_FALSE( "default implementation of cluster functions requires ArchCellInfo to derive from BaseClusterInfo"); } } // namespace // This contains the relevant range types for the default implementations of Arch functions struct BaseArchRanges { // Bels using CellBelPinRangeT = std::array; // Attributes using BelAttrsRangeT = std::vector>; using WireAttrsRangeT = std::vector>; using PipAttrsRangeT = std::vector>; // Groups using AllGroupsRangeT = std::vector; using GroupBelsRangeT = std::vector; using GroupWiresRangeT = std::vector; using GroupPipsRangeT = std::vector; using GroupGroupsRangeT = std::vector; // Decals using DecalGfxRangeT = std::vector; // Placement validity using CellTypeRangeT = const std::vector &; using BelBucketRangeT = const std::vector &; using BucketBelRangeT = const std::vector &; }; template struct BaseArch : ArchAPI { // -------------------------------------------------------------- // Default, trivial, implementations of Arch API functions for arches that don't need complex behaviours // Basic config virtual IdString archId() const override { return this->id(NPNR_STRINGIFY(ARCHNAME)); } virtual IdString archArgsToId(typename R::ArchArgsT args) const override { return IdString(); } virtual int getTilePipDimZ(int x, int y) const override { return 1; } virtual char getNameDelimiter() const override { return ' '; } // Bel methods virtual uint32_t getBelChecksum(BelId bel) const override { return bel.hash(); } virtual void bindBel(BelId bel, CellInfo *cell, PlaceStrength strength) override { NPNR_ASSERT(bel != BelId()); auto &entry = base_bel2cell[bel]; NPNR_ASSERT(entry == nullptr); cell->bel = bel; cell->belStrength = strength; entry = cell; this->refreshUiBel(bel); } virtual void unbindBel(BelId bel) override { NPNR_ASSERT(bel != BelId()); auto &entry = base_bel2cell[bel]; NPNR_ASSERT(entry != nullptr); entry->bel = BelId(); entry->belStrength = STRENGTH_NONE; entry = nullptr; this->refreshUiBel(bel); } virtual bool getBelHidden(BelId bel) const override { return false; } virtual bool getBelGlobalBuf(BelId bel) const override { return false; } virtual bool checkBelAvail(BelId bel) const override { return getBoundBelCell(bel) == nullptr; }; virtual CellInfo *getBoundBelCell(BelId bel) const override { auto fnd = base_bel2cell.find(bel); return fnd == base_bel2cell.end() ? nullptr : fnd->second; } virtual CellInfo *getConflictingBelCell(BelId bel) const override { return getBoundBelCell(bel); } virtual typename R::BelAttrsRangeT getBelAttrs(BelId bel) const override { return empty_if_possible(); } virtual typename R::CellBelPinRangeT getBelPinsForCellPin(const CellInfo *cell_info, IdString pin) const override { return return_if_match, typename R::CellBelPinRangeT>({pin}); } // Wire methods virtual IdString getWireType(WireId wire) const override { return IdString(); } virtual typename R::WireAttrsRangeT getWireAttrs(WireId) const override { return empty_if_possible(); } virtual uint32_t getWireChecksum(WireId wire) const override { return wire.hash(); } virtual void bindWire(WireId wire, NetInfo *net, PlaceStrength strength) override { NPNR_ASSERT(wire != WireId()); auto &w2n_entry = base_wire2net[wire]; NPNR_ASSERT(w2n_entry == nullptr); net->wires[wire].pip = PipId(); net->wires[wire].strength = strength; w2n_entry = net; this->refreshUiWire(wire); } virtual void unbindWire(WireId wire) override { NPNR_ASSERT(wire != WireId()); auto &w2n_entry = base_wire2net[wire]; NPNR_ASSERT(w2n_entry != nullptr); auto &net_wires = w2n_entry->wires; auto it = net_wires.find(wire); NPNR_ASSERT(it != net_wires.end()); auto pip = it->second.pip; if (pip != PipId()) { base_pip2net[pip] = nullptr; } net_wires.erase(it); base_wire2net[wire] = nullptr; w2n_entry = nullptr; this->refreshUiWire(wire); } virtual bool checkWireAvail(WireId wire) const override { return getBoundWireNet(wire) == nullptr; } virtual NetInfo *getBoundWireNet(WireId wire) const override { auto fnd = base_wire2net.find(wire); return fnd == base_wire2net.end() ? nullptr : fnd->second; } virtual WireId getConflictingWireWire(WireId wire) const override { return wire; }; virtual NetInfo *getConflictingWireNet(WireId wire) const override { return getBoundWireNet(wire); } // Pip methods virtual IdString getPipType(PipId pip) const override { return IdString(); } virtual typename R::PipAttrsRangeT getPipAttrs(PipId) const override { return empty_if_possible(); } virtual uint32_t getPipChecksum(PipId pip) const override { return pip.hash(); } virtual void bindPip(PipId pip, NetInfo *net, PlaceStrength strength) override { NPNR_ASSERT(pip != PipId()); auto &p2n_entry = base_pip2net[pip]; NPNR_ASSERT(p2n_entry == nullptr); p2n_entry = net; WireId dst = this->getPipDstWire(pip); auto &w2n_entry = base_wire2net[dst]; NPNR_ASSERT(w2n_entry == nullptr); w2n_entry = net; net->wires[dst].pip = pip; net->wires[dst].strength = strength; } virtual void unbindPip(PipId pip) override { NPNR_ASSERT(pip != PipId()); auto &p2n_entry = base_pip2net[pip]; NPNR_ASSERT(p2n_entry != nullptr); WireId dst = this->getPipDstWire(pip); auto &w2n_entry = base_wire2net[dst]; NPNR_ASSERT(w2n_entry != nullptr); w2n_entry = nullptr; p2n_entry->wires.erase(dst); p2n_entry = nullptr; } virtual bool checkPipAvail(PipId pip) const override { return getBoundPipNet(pip) == nullptr; } virtual bool checkPipAvailForNet(PipId pip, const NetInfo *net) const override { NetInfo *bound_net = getBoundPipNet(pip); return bound_net == nullptr || bound_net == net; } virtual NetInfo *getBoundPipNet(PipId pip) const override { auto fnd = base_pip2net.find(pip); return fnd == base_pip2net.end() ? nullptr : fnd->second; } virtual WireId getConflictingPipWire(PipId pip) const override { return WireId(); } virtual NetInfo *getConflictingPipNet(PipId pip) const override { return getBoundPipNet(pip); } // Group methods virtual GroupId getGroupByName(IdStringList name) const override { return GroupId(); }; virtual IdStringList getGroupName(GroupId group) const override { return IdStringList(); }; virtual typename R::AllGroupsRangeT getGroups() const override { return empty_if_possible(); } // Default implementation of these assumes no groups so never called virtual typename R::GroupBelsRangeT getGroupBels(GroupId group) const override { NPNR_ASSERT_FALSE("unreachable"); }; virtual typename R::GroupWiresRangeT getGroupWires(GroupId group) const override { NPNR_ASSERT_FALSE("unreachable"); }; virtual typename R::GroupPipsRangeT getGroupPips(GroupId group) const override { NPNR_ASSERT_FALSE("unreachable"); }; virtual typename R::GroupGroupsRangeT getGroupGroups(GroupId group) const override { NPNR_ASSERT_FALSE("unreachable"); }; // Delay methods virtual bool getBudgetOverride(const NetInfo *net_info, const PortRef &sink, delay_t &budget) const override { return false; } // Decal methods virtual typename R::DecalGfxRangeT getDecalGraphics(DecalId decal) const override { return empty_if_possible(); }; virtual DecalXY getBelDecal(BelId bel) const override { return DecalXY(); } virtual DecalXY getWireDecal(WireId wire) const override { return DecalXY(); } virtual DecalXY getPipDecal(PipId pip) const override { return DecalXY(); } virtual DecalXY getGroupDecal(GroupId group) const override { return DecalXY(); } // Cell timing methods virtual bool getCellDelay(const CellInfo *cell, IdString fromPort, IdString toPort, DelayQuad &delay) const override { return false; } virtual TimingPortClass getPortTimingClass(const CellInfo *cell, IdString port, int &clockInfoCount) const override { return TMG_IGNORE; } virtual TimingClockingInfo getPortClockingInfo(const CellInfo *cell, IdString port, int index) const override { NPNR_ASSERT_FALSE("unreachable"); } // Placement validity checks virtual bool isValidBelForCellType(IdString cell_type, BelId bel) const override { return cell_type == this->getBelType(bel); } virtual IdString getBelBucketName(BelBucketId bucket) const override { return bbid_to_name(bucket); } virtual BelBucketId getBelBucketByName(IdString name) const override { return bbid_from_name(name); } virtual BelBucketId getBelBucketForBel(BelId bel) const override { return getBelBucketForCellType(this->getBelType(bel)); }; virtual BelBucketId getBelBucketForCellType(IdString cell_type) const override { return getBelBucketByName(cell_type); }; virtual bool isBelLocationValid(BelId bel, bool explain_invalid = false) const override { return true; } virtual typename R::CellTypeRangeT getCellTypes() const override { NPNR_ASSERT(cell_types_initialised); return return_if_match &, typename R::CellTypeRangeT>(cell_types); } virtual typename R::BelBucketRangeT getBelBuckets() const override { NPNR_ASSERT(bel_buckets_initialised); return return_if_match &, typename R::BelBucketRangeT>(bel_buckets); } virtual typename R::BucketBelRangeT getBelsInBucket(BelBucketId bucket) const override { NPNR_ASSERT(bel_buckets_initialised); return return_if_match &, typename R::BucketBelRangeT>(bucket_bels.at(bucket)); } // Cluster methods virtual CellInfo *getClusterRootCell(ClusterId cluster) const override { return get_cluster_root(this, cluster); } virtual BoundingBox getClusterBounds(ClusterId cluster) const override { return if_using_basecluster(get_cluster_root(this, cluster), [](const BaseClusterInfo *cluster) { BoundingBox bounds(0, 0, 0, 0); for (auto child : cluster->constr_children) { if_using_basecluster(child, [&](const BaseClusterInfo *child) { bounds.x0 = std::min(bounds.x0, child->constr_x); bounds.y0 = std::min(bounds.y0, child->constr_y); bounds.x1 = std::max(bounds.x1, child->constr_x); bounds.y1 = std::max(bounds.y1, child->constr_y); }); } return bounds; }); } virtual Loc getClusterOffset(const CellInfo *cell) const override { return if_using_basecluster(cell, [](const BaseClusterInfo *c) { return Loc(c->constr_x, c->constr_y, 0); }); } virtual bool isClusterStrict(const CellInfo *cell) const override { return true; } virtual bool getClusterPlacement(ClusterId cluster, BelId root_bel, std::vector> &placement) const override { CellInfo *root_cell = get_cluster_root(this, cluster); return if_using_basecluster(root_cell, [&](const BaseClusterInfo *cluster) -> bool { placement.clear(); NPNR_ASSERT(root_bel != BelId()); Loc root_loc = this->getBelLocation(root_bel); if (cluster->constr_abs_z) { // Coerce root to absolute z constraint root_loc.z = cluster->constr_z; root_bel = this->getBelByLocation(root_loc); if (root_bel == BelId() || !this->isValidBelForCellType(root_cell->type, root_bel)) return false; } placement.emplace_back(root_cell, root_bel); for (auto child : cluster->constr_children) { Loc child_loc = if_using_basecluster(child, [&](const BaseClusterInfo *child) { Loc result; result.x = root_loc.x + child->constr_x; result.y = root_loc.y + child->constr_y; result.z = child->constr_abs_z ? child->constr_z : (root_loc.z + child->constr_z); return result; }); BelId child_bel = this->getBelByLocation(child_loc); if (child_bel == BelId() || !this->isValidBelForCellType(child->type, child_bel)) return false; placement.emplace_back(child, child_bel); } return true; }); } // Flow methods virtual void assignArchInfo() override{}; // -------------------------------------------------------------- // These structures are used to provide default implementations of bel/wire/pip binding. Arches might want to // replace them with their own, for example to use faster access structures than dict. Arches might also // want to add extra checks around these functions dict base_bel2cell; dict base_wire2net; dict base_pip2net; // For the default cell/bel bucket implementations std::vector cell_types; std::vector bel_buckets; dict> bucket_bels; // Arches that want to use the default cell types and bel buckets *must* call these functions in their constructor bool cell_types_initialised = false; bool bel_buckets_initialised = false; void init_cell_types() { pool bel_types; for (auto bel : this->getBels()) bel_types.insert(this->getBelType(bel)); std::copy(bel_types.begin(), bel_types.end(), std::back_inserter(cell_types)); std::sort(cell_types.begin(), cell_types.end()); cell_types_initialised = true; } void init_bel_buckets() { for (auto cell_type : this->getCellTypes()) { auto bucket = this->getBelBucketForCellType(cell_type); bucket_bels[bucket]; // create empty bucket } for (auto bel : this->getBels()) { auto bucket = this->getBelBucketForBel(bel); bucket_bels[bucket].push_back(bel); } for (auto &b : bucket_bels) bel_buckets.push_back(b.first); std::sort(bel_buckets.begin(), bel_buckets.end()); bel_buckets_initialised = true; } }; NEXTPNR_NAMESPACE_END #endif /* BASE_ARCH_H */ a> 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 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