cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
SimAppData.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 -- 2021 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
30 #ifndef _SIM_APP_DATA_
31 #define _SIM_APP_DATA_
32 
33 #include <iostream>
34 #include <iomanip>
35 #include <deque>
36 #include <cstdlib>
37 
38 #include "nts_utils.hpp"
39 #include "SimNtsUtils.hpp"
40 
41 using namespace std;
42 using namespace hls;
43 
44 
53 class SimAppData {
54 
55  private:
56  int len; // In bytes
57  std::deque<AxisApp> appQ; // A double-ended queue to store APP chunks.
58  const char *myName;
59 
60  // Set the length of this APP data (in bytes)
61  void setLen(int appLen) {
62  this->len = appLen;
63  }
64  // Get the length of this APP data (in bytes)
65  int getLen() {
66  return this->len;
67  }
68  // Clear the content of the APP data queue
69  void clear() {
70  this->appQ.clear();
71  this->len = 0;
72  }
73  // Return the back chunk element of the APP data queue but do not remove it from the queue
74  AxisApp back() {
75  return this->appQ.front();
76  }
77  // Return the front chunk element of the APP data queue but do not remove it from the queue
78  AxisApp front() {
79  return this->appQ.front();
80  }
81  // Remove the back chunk element of the APP data queue
82  void pop_back() {
83  this->appQ.pop_back();
84  }
85  // Remove the first chunk element of the APP data queue
86  void pop_front() {
87  this->appQ.pop_front();
88  }
89  // Add an element at the end of the APP data queue
90  void push_back(AxisApp appChunk) {
91  this->appQ.push_back(appChunk);
92  }
93 
94  public:
95 
96  // Default Constructor: Constructs an APP data of 'datLen' bytes.
97  SimAppData(int datLen) {
98  this->myName = "SimAppData";
99  setLen(0);
100  if (datLen > 0 and datLen <= 65536) { // 64KB = 2^16)
101  int noBytes = datLen;
102  while(noBytes > 8) {
103  pushChunk(AxisApp(0x0000000000000000, 0xFF, 0));
104  noBytes -= 8;
105  }
106  pushChunk(AxisApp(0x0000000000000000, lenToLE_tKeep(noBytes), TLAST));
107  }
108  }
110  this->myName = "SimAppData";
111  this->len = 0;
112  }
113 
114  // Add a chunk of bytes at the end of the double-ended queue
115  void pushChunk(AxisApp appChunk) {
116  if (this->size() > 0) {
117  // Always clear 'TLAST' bit of previous chunck
118  this->appQ[this->size()-1].setLE_TLast(0);
119  }
120  this->push_back(appChunk);
121  this->setLen(this->getLen() + appChunk.getLen());
122  }
123 
124  // Return the chunk at the front of the queue and remove that chunk from the queue
126  AxisApp headingChunk = this->front();
127  this->pop_front();
128  setLen(getLen() - headingChunk.getLen());
129  return headingChunk;
130  }
131 
132  // Return the length of the APP data (in bytes)
133  int length() {
134  return this->len;
135  }
136 
137  // Return the number of chunks in the APP data (in axis-words)
138  int size() {
139  return this->appQ.size();
140  }
141 
142 
146  void clone(SimAppData &appDat) {
147  AxisApp newAxisApp;
148  for (int i=0; i<appDat.appQ.size(); i++) {
149  newAxisApp = appDat.appQ[i];
150  this->appQ.push_back(newAxisApp);
151  }
152  this->setLen(appDat.getLen());
153  }
154 
155 
158  void dump() {
159  string datStr;
160  for (int q=0; q < this->size(); q++) {
161  AxisApp axisData = this->appQ[q];
162  for (int b=7; b >= 0; b--) {
163  if (axisData.getTKeep().bit(b)) {
164  int hi = ((b*8) + 7);
165  int lo = ((b*8) + 0);
166  ap_uint<8> octet = axisData.getTData().range(hi, lo);
167  datStr += myUint8ToStrHex(octet);
168  }
169  }
170  }
171  bool endOfDat = false;
172  int i = 0;
173  int offset = 0;
174  char *ptr;
175  do {
176  string hexaStr;
177  string asciiStr;
178  for (int c=0; c < 16*2; c+=2) {
179  if (i < datStr.length()) {
180  hexaStr += datStr.substr(i, 2);
181  char ch = std::strtoul(datStr.substr(i, 2).c_str(), &ptr, 16);
182  if ((int)ch > 0x1F)
183  asciiStr += ch;
184  else
185  asciiStr += '.';
186 
187  }
188  else {
189  hexaStr += " ";
190  endOfDat = true;
191  }
192  hexaStr += " ";
193  i += 2;
194  }
195  printf("%4.4X %s %s \n", offset, hexaStr.c_str(), asciiStr.c_str());
196  offset += 16;
197  } while (not endOfDat);
198  }
199 
200 
206  bool writeAxisAppToFile(AxisApp &axisApp, ofstream &outFileStream) {
207  if (!outFileStream.is_open()) {
208  printError(myName, "File is not opened.\n");
209  return false;
210  }
211  outFileStream << std::uppercase;
212  outFileStream << hex << noshowbase << setfill('0') << setw(16) << axisApp.getLE_TData().to_uint64();
213  outFileStream << " ";
214  outFileStream << setw(1) << axisApp.getLE_TLast().to_int();
215  outFileStream << " ";
216  outFileStream << hex << noshowbase << setfill('0') << setw(2) << axisApp.getLE_TKeep().to_int() << "\n";
217  if (axisApp.getLE_TLast()) {
218  outFileStream << "\n";
219  }
220  return(true);
221  }
222 
223 
228  bool writeToDatFile(ofstream &outFileStream) {
229  for (int i=0; i < this->size(); i++) {
230  AxisApp axisApp = this->appQ[i];
231  if (not this->writeAxisAppToFile(axisApp, outFileStream)) {
232  return false;
233  }
234  }
235  return true;
236  }
237 
238 }; // End-of: SimAppData
239 
240 #endif
241 
: Utilities for the simulation of the Network-Transport-Stack (NTS) components.
tData getTData(int leHi=64 -1, int leLo=0) const
Definition: AxisRaw.hpp:191
LE_tKeep getLE_TKeep(int leHi=64/8-1, int leLo=0) const
Definition: AxisRaw.hpp:264
LE_tData getLE_TData(int leHi=64 -1, int leLo=0) const
Definition: AxisRaw.hpp:260
int getLen() const
Definition: AxisRaw.hpp:411
tKeep getTKeep(int leHi=64/8-1, int leLo=0) const
Definition: AxisRaw.hpp:207
LE_tLast getLE_TLast() const
Definition: AxisRaw.hpp:268
Class App Data.
Definition: SimAppData.hpp:53
int length()
Definition: SimAppData.hpp:133
void pushChunk(AxisApp appChunk)
Definition: SimAppData.hpp:115
SimAppData(int datLen)
Definition: SimAppData.hpp:97
void clone(SimAppData &appDat)
Clone an APP data.
Definition: SimAppData.hpp:146
AxisApp pullChunk()
Definition: SimAppData.hpp:125
bool writeToDatFile(ofstream &outFileStream)
Dump this APP data as raw of AxisApp chunks into a file.
Definition: SimAppData.hpp:228
void dump()
Dump this APP data as HEX and ASCII characters to screen.
Definition: SimAppData.hpp:158
bool writeAxisAppToFile(AxisApp &axisApp, ofstream &outFileStream)
Dump an AxisApp chunk to a file.
Definition: SimAppData.hpp:206
string myUint8ToStrHex(ap_uint< 8 > inputNumber)
Converts an UINT8 into a string of 2 HEX characters.
int writeAxisAppToFile(AxisApp &axisApp, ofstream &outFile)
Dump a TCP or UDP application data chunk into a file. The data are stored as a stream of bytes which ...
#define printError(callerName, format,...)
A macro to print an error message.
Definition: nts_utils.hpp:195
LE_tKeep lenToLE_tKeep(ap_uint< 4 > noValidBytes)
A function to set a number of '1' in an 8-bit field. It is used here to set the number of valid bytes...
Definition: nts_utils.cpp:307
AxisRaw AxisApp
Definition: nts.hpp:51
#define TLAST
Definition: AxisRaw.hpp:116
void uppercase(ap_uint< 32 > *pi_rank, ap_uint< 32 > *pi_size, stream< NetworkWord > &siSHL_This_Data, stream< NetworkWord > &soTHIS_Shl_Data, stream< NetworkMetaStream > &siNrc_meta, stream< NetworkMetaStream > &soNrc_meta, ap_uint< 32 > *po_rx_ports)
Main process of the Uppercase Application directives.
Definition: uppercase.cpp:335
: Utilities and helpers for the Network-Transport-Stack (NTS) components.
ap_uint< 32 > size