Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-09 23:33:29

0001 #ifndef EventFilter_L1TRawToDigi_AMC_SPEC__h
0002 #define EventFilter_L1TRawToDigi_AMC_SPEC__h
0003 
0004 #include <memory>
0005 #include <vector>
0006 #include <cstdint>
0007 #include <span>
0008 
0009 namespace amc {
0010   static const unsigned int split_block_size = 0x1000;
0011 
0012   // The AMC header within an AMC13 payload block.  Should optimally only
0013   // be used when packing/unpacking AMC payloads into AMC13 blocks.
0014   class BlockHeader {
0015   public:
0016     BlockHeader() : data_(0) {}
0017     BlockHeader(const uint64_t *data) : data_(data[0]) {}
0018     // size is the total size of the AMC payload, not just of the
0019     // block
0020     BlockHeader(unsigned int amc_no, unsigned int board_id, unsigned int size, unsigned int block = 0);
0021 
0022     operator uint64_t() const { return data_; };
0023 
0024     inline uint64_t raw() const { return data_; };
0025 
0026     unsigned int getBlocks() const;
0027     unsigned int getBlockSize() const;
0028 
0029     inline unsigned int getAMCNumber() const { return (data_ >> AmcNo_shift) & AmcNo_mask; };
0030     inline unsigned int getBoardID() const { return (data_ >> BoardID_shift) & BoardID_mask; };
0031     inline unsigned int getSize() const { return (data_ >> Size_shift) & Size_mask; };
0032     inline unsigned int getMore() const { return (data_ >> More_bit_shift) & 1; };
0033     inline unsigned int getSegmented() const { return (data_ >> Segmented_bit_shift) & 1; };
0034 
0035     inline unsigned int validCRC() const { return (data_ >> CRC_bit_shift) & 1; };
0036 
0037   private:
0038     static const unsigned int Size_shift = 32;
0039     static const unsigned int Size_mask = 0xffffff;
0040     static const unsigned int BlkNo_shift = 20;
0041     static const unsigned int BlkNo_mask = 0xff;
0042     static const unsigned int AmcNo_shift = 16;
0043     static const unsigned int AmcNo_mask = 0xf;
0044     static const unsigned int BoardID_shift = 0;
0045     static const unsigned int BoardID_mask = 0xffff;
0046 
0047     static const unsigned int Length_bit_shift = 62;
0048     static const unsigned int More_bit_shift = 61;
0049     static const unsigned int Segmented_bit_shift = 60;
0050     static const unsigned int Enabled_bit_shift = 59;
0051     static const unsigned int Present_bit_shift = 58;
0052     static const unsigned int Valid_bit_shift = 57;
0053     static const unsigned int CRC_bit_shift = 56;
0054 
0055     uint64_t data_;
0056   };
0057 
0058   // The actual header attached to the AMC payload, also contained in the
0059   // AMC payload of an AMC13 packet/block.
0060   class Header {
0061   public:
0062     Header() : data0_(0), data1_(0) {}
0063     Header(const uint64_t *data) : data0_(data[0]), data1_(data[1]) {}
0064     Header(unsigned int amc_no,
0065            unsigned int lv1_id,
0066            unsigned int bx_id,
0067            unsigned int size,
0068            unsigned int or_n,
0069            unsigned int board_id,
0070            unsigned int user);
0071 
0072     inline unsigned int getAMCNumber() const { return (data0_ >> AmcNo_shift) & AmcNo_mask; };
0073     inline unsigned int getBoardID() const { return (data1_ >> BoardID_shift) & BoardID_mask; };
0074     inline unsigned int getLV1ID() const { return (data0_ >> LV1ID_shift) & LV1ID_mask; };
0075     inline unsigned int getBX() const { return (data0_ >> BX_shift) & BX_mask; };
0076     inline unsigned int getOrbitNumber() const { return (data1_ >> OrN_shift) & OrN_mask; };
0077     inline unsigned int getSize() const { return (data0_ >> Size_shift) & Size_mask; };
0078     inline unsigned int getUserData() const { return (data1_ >> User_shift) & User_mask; };
0079 
0080     std::vector<uint64_t> raw() const { return {data0_, data1_}; };
0081 
0082   private:
0083     static const unsigned int Size_shift = 0;
0084     static const unsigned int Size_mask = 0xfffff;
0085     static const unsigned int BX_shift = 20;
0086     static const unsigned int BX_mask = 0xfff;
0087     static const unsigned int LV1ID_shift = 32;
0088     static const unsigned int LV1ID_mask = 0xffffff;
0089     static const unsigned int AmcNo_shift = 56;
0090     static const unsigned int AmcNo_mask = 0xf;
0091 
0092     static const unsigned int BoardID_shift = 0;
0093     static const unsigned int BoardID_mask = 0xffff;
0094     static const unsigned int OrN_shift = 16;
0095     static const unsigned int OrN_mask = 0xffff;
0096     static const unsigned int User_shift = 32;
0097     static const unsigned int User_mask = 0xffffffff;
0098 
0099     uint64_t data0_;
0100     uint64_t data1_;
0101   };
0102 
0103   class Trailer {
0104   public:
0105     Trailer() : data_(0) {}
0106     Trailer(const uint64_t *data) : data_(data[0]) {}
0107     Trailer(unsigned int crc, unsigned int lv1_id, unsigned int size);
0108 
0109     inline unsigned int getCRC() const { return (data_ >> CRC_shift) & CRC_mask; };
0110     inline unsigned int getLV1ID() const { return (data_ >> LV1ID_shift) & LV1ID_mask; };
0111     inline unsigned int getSize() const { return (data_ >> Size_shift) & Size_mask; };
0112 
0113     uint64_t raw() const { return data_; }
0114     bool check(unsigned int crc, unsigned int lv1_id, unsigned int size, bool mtf7_mode = false) const;
0115 
0116     static void writeCRC(const uint64_t *start, uint64_t *end);
0117 
0118   private:
0119     static const unsigned int Size_shift = 0;
0120     static const unsigned int Size_mask = 0xfffff;
0121     static const unsigned int LV1ID_shift = 24;
0122     static const unsigned int LV1ID_mask = 0xff;
0123     static const unsigned int CRC_shift = 32;
0124     static const unsigned int CRC_mask = 0xffffffff;
0125 
0126     uint64_t data_;
0127   };
0128 
0129   class Packet {
0130   public:
0131     Packet(const uint64_t *d) : block_header_(d) {}
0132     Packet(unsigned int amc,
0133            unsigned int board,
0134            unsigned int lv1id,
0135            unsigned int orbit,
0136            unsigned int bx,
0137            const std::vector<uint64_t> &load,
0138            unsigned int user = 0);
0139 
0140     // Add payload fragment from an AMC13 block to the AMC packet
0141     void addPayload(const uint64_t *, unsigned int);
0142     // To be called after the last payload addition.  Removes header
0143     // and trailer from the actual paylod.  Also performs
0144     // cross-checks for data consistency.
0145     void finalize(unsigned int lv1, unsigned int bx, bool legacy_mc = false, bool mtf7_mode = false);
0146 
0147     std::span<const uint64_t> block(unsigned int id) const;
0148     std::span<const uint64_t> data() const {
0149       // Remove 3 words: 2 for the header, 1 for the trailer
0150       return payload_.empty() ? std::span<const uint64_t>() : std::span<const uint64_t>(payload_).subspan(2, size());
0151     };
0152     BlockHeader blockHeader(unsigned int block = 0) const { return block_header_; };
0153     Header header() const { return header_; };
0154     Trailer trailer() const { return trailer_; };
0155 
0156     inline unsigned int blocks() const { return block_header_.getBlocks(); };
0157     // Returns the size of the payload _without_ the headers
0158     inline unsigned int size() const { return payload_.size() - 3; };
0159 
0160   private:
0161     BlockHeader block_header_;
0162     Header header_;
0163     Trailer trailer_;
0164 
0165     std::vector<uint64_t> payload_;
0166   };
0167 }  // namespace amc
0168 
0169 #endif