torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VirtexPacket.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the VirtexPacket class.
18 
19 #ifndef TORC_BITSTREAM_VIRTEXPACKET_HPP
20 #define TORC_BITSTREAM_VIRTEXPACKET_HPP
21 
22 #include "torc/common/Endian.hpp"
23 #include <boost/smart_ptr.hpp>
24 #include <vector>
25 #include <istream>
26 
27 #include <iostream>
28 
29 namespace torc {
30 namespace bitstream {
31 
32 namespace bitstream { class VirtexPacketUnitTest; }
33 
34  /// \brief Bitstream packet constants for Virtex class architectures.
36  public:
37  // enumerations
38  /// \brief Packet type enumeration.
39  /// \see packet type: UG071, v1.10, April 8, 2008, Tables 7-2 and 7-4.
40  /// \see packet type: XAPP151, v1.7, October 20, 2004, Figure 13, 14.
42  //
43  /// \brief Packet opcode enumeration.
44  /// \see Opcode Format: UG191, v3.7, June 24, 2009, Table 6-3.
46  /// \brief Packet subfields.
47  /// \see type 1 packet format: UG071, v1.10, April 8, 2008, Table 7-2.
48  /// \see type 2 packet format: UG071, v1.10, April 8, 2008, Table 7-4.
49  /// \see type 1 packet format: XAPP151, v1.7, October 20, 2004, Figure 13.
50  /// \see type 2 packet format: XAPP151, v1.7, October 20, 2004, Figure 14.
51  enum EPacket {
52  // generic packet subfields
53  ePacketMaskType = 0xe0000000, ePacketShiftType = 29,
55  // type 1 packet subfields
59  // type 2 packet subfields
61  };
62  //
63  /// \brief Synchronization words.
64  /// \see Configuration Sequence: UG191, v3.7, June 24, 2009, Table 6-15.
65  /// \see Configuration Sequence: UG071, v1.10, April 8, 2008, Table 7-11.
66  /// \see Configuration Sequence: XAPP511, v1.7, October 20, 2004, Figure 23.
68  eSynchronizationDummy = 0xffffffff,
69  eSynchronizationSync = 0xaa995566,
72  };
73  };
74 
75  /// \brief Bitstream packet for Virtex class architectures.
78  protected:
79  // typedefs
80  /// \brief Imported type name.
81  typedef boost::uint32_t uint32_t;
82  /// \brief Word shared array type.
83  typedef boost::shared_array<uint32_t> WordSharedArray;
84  // functions
85  void initialize(void) {
88  if(mType == ePacketType1)
90  }
91  public:
92  //protected:
93  // members
100  int mAddress;
101  /// \brief Packet type names.
102  static const char* sPacketTypeName[ePacketTypeCount];
103  /// \brief Packet opcode names.
104  static const char* sOpcodeName[eOpcodeCount];
105  public:
106  // constructors
107  /// \brief Null constructor.
108  VirtexPacket(void) : mHeader(0), mCount(0), mWord(0), mWords(), mType(EPacketType(0)),
109  mOpcode(eOpcodeNOP), mAddress(0) {}
110  /// \brief Full constructor.
111  VirtexPacket(uint32_t inHeader, uint32_t inCount, uint32_t inWord, uint32_t* inWords)
112  : mHeader(inHeader), mCount(inCount), mWord(inWord), mWords(WordSharedArray(inWords)),
114  initialize();
115  }
116  /// \brief Header plus single word constructor.
117  VirtexPacket(uint32_t inHeader, uint32_t inWord) : mHeader(inHeader), mCount(1),
118  mWord(inWord), mWords(), mType(EPacketType(0)), mOpcode(eOpcodeNOP), mAddress(0) {
119  initialize();
120  }
121  /// \brief Header only constructor.
122  VirtexPacket(uint32_t inHeader) : mHeader(inHeader), mCount(0), mWord(0), mWords(),
124  initialize();
125  }
126  /// \brief Copy constructor.
129  mAddress(0) {
130  initialize();
131  }
132  // functions
133  /// \brief Read a packet.
134  static VirtexPacket read(std::istream& inStream) {
135  uint32_t count = 0;
136  uint32_t header = 0;
137  uint32_t word = 0;
138  uint32_t* raw_words = 0;
139  // read the header
140  inStream.read((char*) &header, sizeof(header));
141  header = ntohl(header);
142  // process synchronization packets
143  if(header == eSynchronizationDummy || header == eSynchronizationSync
144  || header == eSynchronizationBusWidthSync
145  || header == eSynchronizationBusWidthDetect) {
146  return VirtexPacket(header, 0, 0, 0);
147  }
148  // determine the payload length
150  switch(type) {
151  case ePacketType1:
152  count = (header & ePacketMaskType1Count) >> ePacketShiftType1Count;
153  break;
154  case ePacketType2:
155  count = (header & ePacketMaskType2Count) >> ePacketShiftType2Count;
156  break;
157  default:
158  /// \todo we should throw an exception on invalid packet types
159  count = 0;
160  break;
161  }
162  // read the packet payload
163  if(count == 1) {
164  inStream.read((char*) &word, sizeof(word));
165  word = ntohl(word);
166  } else if(count > 1) {
167  raw_words = new uint32_t[count];
168  inStream.read((char*) raw_words, count << 2);
169  uint32_t* wordPtr = raw_words;
170  for(uint32_t i = 0; i < count; i++, wordPtr++) *wordPtr = ntohl(*wordPtr);
171  }
172  // create and return the packet
173  return VirtexPacket(header, count, word, raw_words);
174  }
175  /// \brief Write a packet.
176  void write(std::ostream& inStream) const {
177  uint32_t size = getWordSize();
178  for(uint32_t i = 0; i < size; i++) {
179  uint32_t word = htonl(operator[](i));
180  inStream.write((char*) &word, sizeof(word));
181  }
182  }
183  /// \brief Construct a null type 1 write packet.
184  /// \details A type 1 write packet to the FDRI register is generally only useful to specify
185  /// the current register in preparation for a type 2 write. The type 2 write header
186  /// includes a size but specifies no target register, hence the prior null type 1
187  /// packet.
189  return VirtexPacket(makeHeader(ePacketType1, eOpcodeWrite, inAddress, 0), 0, 0, 0);
190  }
191  /// \brief Construct a type 1 write packet.
192  static VirtexPacket makeType1Write(uint32_t inAddress, uint32_t inWord) {
193  return VirtexPacket(makeHeader(ePacketType1, eOpcodeWrite, inAddress, 1), 1, inWord, 0);
194  }
195  /// \brief Construct a multi-word type 1 write packet.
196  static VirtexPacket makeType1Write(uint32_t inAddress, uint32_t inCount, uint32_t* inWords)
197  {
198  return VirtexPacket(makeHeader(ePacketType1, eOpcodeWrite, inAddress, inCount),
199  inCount, 0, inWords);
200  }
201  /// \brief Construct a type 2 write packet.
202  static VirtexPacket makeType2Write(uint32_t inCount, uint32_t* inWords) {
203  return VirtexPacket(makeHeader(ePacketType2, eOpcodeWrite, 0, inCount), inCount, 0,
204  inWords);
205  }
206 // /// \brief Construct a type 1 write packet header.
207 // static uint32_t makeType1WriteHeader(uint32_t inAddress, uint32_t inCount) {
208 // return makeHeader(ePacketType1, eOpcodeWrite, inAddress, inCount);
209 // }
210 // /// \brief Construct a type 2 write packet header.
211 // static uint32_t makeType2WriteHeader(uint32_t inCount) {
212 // return makeHeader(ePacketType2, eOpcodeWrite, 0, inCount);
213 // }
214  /// \brief Construct a packet header.
215  static uint32_t makeHeader(EPacketType inType, EOpcode inOpcode, uint32_t inAddress,
216  uint32_t inCount) {
217  // type 1 packets
218  if(inType == ePacketType1) return
219  ((inType << ePacketShiftType) & ePacketMaskType) |
220  ((inOpcode << ePacketShiftOpcode) & ePacketMaskOpcode) |
223  // type 2 packets
224  else if(inType == ePacketType2) return
225  ((inType << ePacketShiftType) & ePacketMaskType) |
226  ((inOpcode << ePacketShiftOpcode) & ePacketMaskOpcode) |
228  // undefined packet types
229  else
230  /// \todo Generate an error or throw an exception on invalid packet types?
231  return 0;
232  }
233  // operators
234  uint32_t operator[] (size_t inIndex) const {
235  if(inIndex == 0) return mHeader;
236  if(inIndex == 1 && mCount == 1) return mWord;
237  if(inIndex <= mCount) return mWords[inIndex-1];
238  return 0;
239  }
240  // accessors
241  EPacketType getType(void) const { return mType; }
242  EOpcode getOpcode(void) const { return mOpcode; }
243  int getAddress(void) const { return mAddress; }
244  uint32_t getHeader(void) const { return mHeader; }
245  /// \brief Returns the number of payload words in the packet, excluding the header word.
246  uint32_t getWordCount(void) const { return mCount; }
247  /// \brief Returns the total number of words in the packet, including the header word.
248  uint32_t getWordSize(void) const { return mCount + 1; }
249  /// brief Returns the raw packet words, including the header word.
250  const WordSharedArray getWords(void) const { return mWords; }
251  // tests
252  bool isType1(void) const { return mType == ePacketType1; }
253  bool isType2(void) const { return mType == ePacketType2; }
254  bool isNop(void) const { return mOpcode == eOpcodeNOP; }
255  bool isReserved(void) const { return mOpcode == eOpcodeReserved; }
256  bool isRead(void) const { return mOpcode == eOpcodeRead; }
257  bool isWrite(void) const { return mOpcode == eOpcodeWrite; }
258  bool isDummyWord(void) const { return mHeader == eSynchronizationDummy; }
259  bool isSyncWord(void) const { return mHeader == eSynchronizationSync; }
262  };
263 
264  /// \brief Vector of Virtex packets.
265  typedef std::vector<VirtexPacket> VirtexPacketVector;
266 
267 } // namespace bitstream
268 } // namespace torc
269 
270 #endif // TORC_BITSTREAM_VIRTEXPACKET_HPP
void write(std::ostream &inStream) const
Write a packet.
VirtexPacket(void)
Null constructor.
VirtexPacket(uint32_t inHeader, uint32_t inCount, uint32_t inWord, uint32_t *inWords)
Full constructor.
static VirtexPacket makeNullType1Write(uint32_t inAddress)
Construct a null type 1 write packet.
EOpcode
Packet opcode enumeration.
friend class torc::bitstream::bitstream::VirtexPacketUnitTest
boost::shared_array< uint32_t > WordSharedArray
Word shared array type.
uint32_t getWordSize(void) const
Returns the total number of words in the packet, including the header word.
static VirtexPacket makeType2Write(uint32_t inCount, uint32_t *inWords)
Construct a type 2 write packet.
VirtexPacket(uint32_t inHeader, uint32_t inWord)
Header plus single word constructor.
uint32_t getWordCount(void) const
Returns the number of payload words in the packet, excluding the header word.
VirtexPacket(const VirtexPacket &rhs)
Copy constructor.
static const char * sPacketTypeName[ePacketTypeCount]
Packet type names.
static const char * sOpcodeName[eOpcodeCount]
Packet opcode names.
bool isBusWidthSyncWord(void) const
Bitstream packet constants for Virtex class architectures.
std::vector< VirtexPacket > VirtexPacketVector
Vector of Virtex packets.
ESynchronization
Synchronization words.
static VirtexPacket makeType1Write(uint32_t inAddress, uint32_t inWord)
Construct a type 1 write packet.
const WordSharedArray getWords(void) const
brief Returns the raw packet words, including the header word.
static VirtexPacket read(std::istream &inStream)
Read a packet.
EOpcode getOpcode(void) const
boost::uint32_t uint32_t
Imported type name.
EPacketType
Packet type enumeration.
EPacketType getType(void) const
bool isBusWidthDetectWord(void) const
Bitstream packet for Virtex class architectures.
static uint32_t makeHeader(EPacketType inType, EOpcode inOpcode, uint32_t inAddress, uint32_t inCount)
Construct a packet header.
uint32_t operator[](size_t inIndex) const
uint32_t getHeader(void) const
Header for endian conversion.
VirtexPacket(uint32_t inHeader)
Header only constructor.
static VirtexPacket makeType1Write(uint32_t inAddress, uint32_t inCount, uint32_t *inWords)
Construct a multi-word type 1 write packet.