Add Freescale Nexus decoder implementation
[babeltrace.git] / converter / nexus / NxPacketSet.cpp
1 /*
2 * Copyright (C) 2013 Freescale Semiconductor, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23 #include "NxPacketSet.h"
24
25 #include <assert.h>
26 #include <iostream>
27
28 using std::cout;
29 using std::endl;
30
31 NxPacketSet::NxPacketSet() :
32 verbose_(false)
33 {
34 reset();
35 }
36
37 void NxPacketSet::reset()
38 {
39 // pointer to the first packet
40 current_packet_index_ = 0;
41 current_packet_ = &packet_[current_packet_index_];
42
43 for (uint32_t i = 0; i < MAX_NUM_PACKETS; ++i) {
44 packet_[i].reset();
45 }
46 }
47
48 // add len bits of mdo to the current packet
49 bool NxPacketSet::addToCurrent(uint32_t mdo)
50 {
51 bool ret = current_packet_->add(mdo);
52 if (verbose_) {
53 if (ret) {
54 for (uint32_t i = 0; i <= current_packet_index_; ++i) {
55 cout << "Packet[" << i << "]: "
56 << packet_[i].packet_ << endl;
57 }
58 } else {
59 cout << "Error adding MDO to Packet["
60 << current_packet_index_ << "]" << endl;
61 }
62 }
63 return ret;
64 }
65
66 // advance to the next packet
67 bool NxPacketSet::nextPacket()
68 {
69 assert(current_packet_index_ < (MAX_NUM_PACKETS - 1));
70
71 bool ret = current_packet_index_ < (MAX_NUM_PACKETS - 1);
72 if (ret) {
73 current_packet_index_++;
74 current_packet_ = &packet_[current_packet_index_];
75 }
76 return ret;
77 }
78
79 // get the value of len size from start (LSB) pos from packet at index
80 uint32_t NxPacketSet::getField32(uint32_t index, uint32_t start_pos,
81 uint32_t len) const
82 {
83 assert(index < MAX_NUM_PACKETS);
84 return packet_[index].getField32(start_pos, len);
85
86 }
87
88 // get the value of len size from start (LSB) pos from packet at index
89 uint64_t NxPacketSet::getField64(uint32_t index, uint32_t start_pos,
90 uint32_t len) const
91 {
92 assert(index < MAX_NUM_PACKETS);
93 return packet_[index].getField64(start_pos, len);
94 }
95
96 // get the number of MDO from packet at index
97 uint32_t NxPacketSet::getNumMDO(uint32_t index) const
98 {
99 assert(index < MAX_NUM_PACKETS);
100 return packet_[index].getNumMDO();
101 }
102
103 bool NxPacketSet::packetAtIndexExists(uint32_t index) const
104 {
105 assert(index < MAX_NUM_PACKETS);
106 return index <= current_packet_index_;
107 }
108
109 // return the TCODE
110 uint32_t NxPacketSet::tcode() const
111 {
112 // index=0, start_pos=0, length=6
113 uint32_t ret = getField32(0, 0, 6);
114 return ret;
115 }
116
117 // return the TCODE
118 uint32_t NxPacketSet::src() const
119 {
120 // index=0, start_pos=6, length=6
121 uint32_t ret = getField32(0, 6, 6);
122 return ret;
123 }
124
125 // NxPacket class
126
127 NxPacket::NxPacket()
128 {
129 reset();
130 }
131
132 // return the packet to the initial state
133 void NxPacket::reset()
134 {
135 packet_.reset();
136 current_pos_ = 0;
137 num_mdo_ = 0;
138 }
139
140 // add len bits of mdo to the packet
141 bool NxPacket::add(uint32_t mdo)
142 {
143 assert((mdo & 0xc0000000) == 0);
144 assert(num_mdo_ <= MAX_NUM_MDO_PER_PACKET);
145
146 bool ret = true;
147 PktBits mdo_bits(mdo);
148 mdo_bits <<= current_pos_;
149 packet_ |= mdo_bits;
150 current_pos_ += MAX_NUM_MDO_BITS;
151 num_mdo_++;
152
153 return ret;
154 }
155
156 // get the value of len size from start (LSB) pos from packet
157 uint32_t NxPacket::getField32(uint32_t start_pos, uint32_t len) const
158 {
159 assert(start_pos + len < MAX_NUM_PACKET_BITS);
160 assert(len <= 32);
161
162 uint32_t val = 0;
163 uint32_t pos = start_pos + len - 1;
164 for (uint32_t i = 0; i < len; ++i) {
165 val <<= 1;
166 val |= packet_[pos];
167 pos--;
168 }
169 return val;
170 }
171
172 // get the value of len size from start (LSB) pos from packet
173 uint64_t NxPacket::getField64(uint32_t start_pos, uint32_t len) const
174 {
175 assert(start_pos + len < MAX_NUM_PACKET_BITS);
176 assert(len <= 64);
177
178 uint64_t val = 0;
179 uint32_t pos = start_pos + len - 1;
180 for (uint32_t i = 0; i < len; ++i) {
181 val <<= 1;
182 val |= packet_[pos];
183 pos--;
184 }
185 return val;
186 }
This page took 0.041382 seconds and 4 git commands to generate.