File indexing completed on 2024-04-06 12:19:51
0001 #ifndef L1GCTLUT_H_
0002 #define L1GCTLUT_H_
0003
0004 #include <iomanip>
0005 #include <sstream>
0006 #include <cstdint>
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 template <int NAddressBits, int NDataBits>
0019 class L1GctLut {
0020 public:
0021 static const uint16_t MAX_ADDRESS_BITMASK;
0022 static const uint16_t MAX_DATA_BITMASK;
0023
0024 virtual ~L1GctLut();
0025
0026
0027 friend std::ostream& operator<<(std::ostream& os, const L1GctLut<NAddressBits, NDataBits>& lut) {
0028
0029
0030
0031
0032 static const int maxAddress = L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK;
0033 static const int width = L1GctLut<NAddressBits, NDataBits>::printWidth;
0034
0035 os << lut.printHeader();
0036
0037 for (int a = 0; a <= maxAddress; a += width) {
0038 os << lut.printLine(a);
0039 }
0040 return os;
0041
0042
0043 }
0044
0045
0046 uint16_t lutValue(const uint16_t lutAddress) const;
0047
0048
0049 uint16_t operator[](const uint16_t lutAddress) const { return lutValue(lutAddress); }
0050
0051
0052 template <int KAddressBits, int KDataBits>
0053 int operator==(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const {
0054 return equalityCheck(rhsLut);
0055 }
0056
0057
0058 template <int KAddressBits, int KDataBits>
0059 int operator!=(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const {
0060 return !equalityCheck(rhsLut);
0061 }
0062
0063 bool setupOk() { return m_setupOk; }
0064
0065
0066 void setVerbose() { m_verbose = true; }
0067 void setTerse() { m_verbose = false; }
0068
0069 protected:
0070 L1GctLut();
0071
0072 virtual uint16_t value(const uint16_t lutAddress) const = 0;
0073
0074 template <int KAddressBits, int KDataBits>
0075 bool equalityCheck(const L1GctLut<KAddressBits, KDataBits>& c) const;
0076
0077 bool m_setupOk;
0078 bool m_verbose;
0079
0080 private:
0081
0082 static const int printWidth;
0083 std::string printHeader() const;
0084 std::string printLine(const int add) const;
0085 };
0086
0087 template <int NAddressBits, int NDataBits>
0088 const uint16_t L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK = (1 << NAddressBits) - 1;
0089 template <int NAddressBits, int NDataBits>
0090 const uint16_t L1GctLut<NAddressBits, NDataBits>::MAX_DATA_BITMASK = (1 << NDataBits) - 1;
0091
0092 template <int NAddressBits, int NDataBits>
0093 const int L1GctLut<NAddressBits, NDataBits>::printWidth = 16;
0094
0095 template <int NAddressBits, int NDataBits>
0096 L1GctLut<NAddressBits, NDataBits>::L1GctLut() : m_setupOk(false) {}
0097
0098 template <int NAddressBits, int NDataBits>
0099 L1GctLut<NAddressBits, NDataBits>::~L1GctLut() {}
0100
0101 template <int NAddressBits, int NDataBits>
0102 uint16_t L1GctLut<NAddressBits, NDataBits>::lutValue(const uint16_t lutAddress) const {
0103 if (!m_setupOk)
0104 return (uint16_t)0;
0105 uint16_t address = (lutAddress & MAX_ADDRESS_BITMASK);
0106 uint16_t data = (value(address) & MAX_DATA_BITMASK);
0107 return data;
0108 }
0109
0110 template <int NAddressBits, int NDataBits>
0111 template <int KAddressBits, int KDataBits>
0112 bool L1GctLut<NAddressBits, NDataBits>::equalityCheck(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const {
0113 if (KAddressBits == NAddressBits && KDataBits == NDataBits) {
0114 bool match = true;
0115 for (uint16_t address = 0; address <= MAX_ADDRESS_BITMASK; address++) {
0116 if (this->lutValue(address) != rhsLut.lutValue(address)) {
0117 match = false;
0118 break;
0119 }
0120 }
0121 return match;
0122 } else {
0123 return false;
0124 }
0125 }
0126
0127 template <int NAddressBits, int NDataBits>
0128 std::string L1GctLut<NAddressBits, NDataBits>::printHeader() const {
0129 std::stringstream ss;
0130 ss << std::hex << std::showbase;
0131 ss << std::setw(8) << "|";
0132 for (int a = 0; ((a < printWidth) && (a <= MAX_ADDRESS_BITMASK)); ++a) {
0133 ss << std::setw(7) << a;
0134 }
0135 ss << std::endl;
0136 ss << std::setfill('-') << std::setw(8) << "+";
0137 for (int a = 0; ((a < printWidth) && (a <= MAX_ADDRESS_BITMASK)); ++a) {
0138 ss << std::setw(7) << "-";
0139 }
0140 ss << std::endl;
0141
0142 return ss.str();
0143 }
0144
0145 template <int NAddressBits, int NDataBits>
0146 std::string L1GctLut<NAddressBits, NDataBits>::printLine(const int add) const {
0147 std::stringstream ss;
0148 ss << std::hex << std::showbase;
0149 int a = add;
0150 ss << std::setw(7) << a << "|";
0151 for (int c = 0; ((c < printWidth) && (a <= MAX_ADDRESS_BITMASK)); ++c) {
0152 uint16_t address = static_cast<uint16_t>(a++);
0153 ss << std::setw(7) << lutValue(address);
0154 }
0155 ss << std::endl;
0156
0157 return ss.str();
0158 }
0159
0160 #endif