Bitstream Interpretation Library (BIL)  0.1
Type1PacketRawData.cpp
Go to the documentation of this file.
1 
9 
10 using namespace bil;
11 
12 const size_t HEADERTYPE_BITSHIFT = 29;
13 const boost::uint32_t HEADERTYPE_VALUE = 0x1;
14 
15 const size_t OPCODE_BITSHIFT = 27;
16 const boost::uint32_t OPCODE_BITMASK = 0x3;
17 
18 const size_t REGISTERADDRESS_BITSHIFT = 13;
19 const boost::uint32_t REGISTERADDRESS_BITMASK = 0x3fff;
20 
21 const boost::uint32_t WORDCOUNT_BITMASK = 0x7ff;
22 
23 
24 size_t bil::isType1Packet(const boost::uint32_t* words, size_t wordCount)
25 {
26  // packet header is one word
27  if (1 > wordCount) return 0;
28 
29  // extract packet header
30  boost::uint32_t header = *words;
31  // check header type
32  if (HEADERTYPE_VALUE != (header >> HEADERTYPE_BITSHIFT)) return 0;
33  // if packet opcode is a write op, then the packet header is followed by the
34  // data to be written
36  return 1 + (header & WORDCOUNT_BITMASK);
37  else return 1;
38 }
39 
40 
41 size_t bil::type1PacketWordCount(const Type1Packet& type1Packet)
42 {
43  // if packet opcode is REGISTER_WRITE, then the one word big packet header
44  // is followed by the packet data. Otherwise packet is header only.
45  if (PacketOpcode::REGISTER_WRITE == (type1Packet.opcode()))
46  return 1 + type1Packet.wordCount();
47  else return 1;
48 }
49 
50 
51 size_t bil::writeType1Packet(const Type1Packet& type1Packet, boost::uint32_t* words, size_t wordCount)
52 {
53  // packet header is one word
54  if (1 > wordCount) throw Exception();
55 
56  // check packet
57  if (type1Packet.opcode() > OPCODE_BITMASK) throw Exception();
58  if (type1Packet.registerAddress() > REGISTERADDRESS_BITMASK) throw Exception();
59  if (type1Packet.wordCount() > WORDCOUNT_BITMASK) throw Exception();
60 
61  // write packet header
62  *words++ =
64  (type1Packet.opcode() << OPCODE_BITSHIFT) |
65  (type1Packet.registerAddress() << REGISTERADDRESS_BITSHIFT) |
66  (type1Packet.wordCount());
67 
68  // if packet opcode is REGISTER_WRITE, then packet data follows
69  if (PacketOpcode::REGISTER_WRITE != (type1Packet.opcode())) return 1;
70  // check data length
71  if ((1 + type1Packet.wordCount()) > wordCount) throw Exception();
72  // write packet data
73  memcpy(words, type1Packet.dataWords(), type1Packet.wordCount() << 2);
74 
75  // return written size
76  return 1 + type1Packet.wordCount();
77 }
78 
79 
80 size_t bil::readType1Packet(Type1Packet& type1Packet, const boost::uint32_t* words, size_t wordCount)
81 {
82  // packet header is one word
83  if (1 > wordCount) throw Exception();
84 
85  // extract packet header and read its data
86  boost::uint32_t header = *words++;
87  // check packet header type
88  if (HEADERTYPE_VALUE != (header >> HEADERTYPE_BITSHIFT)) throw Exception();
89  // extract opcode
90  type1Packet.opcode((header >> OPCODE_BITSHIFT) & OPCODE_BITMASK);
91  // extract register address
92  type1Packet.registerAddress(static_cast<RegisterAddress::address_t>(
94  // extract word count
95  type1Packet.wordCount(header & WORDCOUNT_BITMASK);
96 
97  // if packet opcode is REGISTER_WRITE, then packet data follows
98  if (PacketOpcode::REGISTER_WRITE != (type1Packet.opcode())) return 1;
99  // check packet length
100  if ((1 + type1Packet.wordCount()) > wordCount) throw Exception();
101  // load data
102  memcpy(type1Packet.dataWords(), words, type1Packet.wordCount() << 2);
103  return 1 + type1Packet.wordCount();
104 }