-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNanoDesignTask.cpp
More file actions
189 lines (156 loc) · 5.2 KB
/
NanoDesignTask.cpp
File metadata and controls
189 lines (156 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// NanoDesignTask.cpp
#include <iostream>
#include <fstream>
#include <climits>
#include <cstring>
using namespace std;
unsigned int static g_randNumber = 0;
class OtherFunctions
{
public:
unsigned int static myRandom() {
g_randNumber = g_randNumber * 1103515245 + 12345;
return (g_randNumber % UINT_MAX);
}
uint16_t static crc16Modbus(uint8_t* message, int len) {
const uint16_t generator = 0xa001;
uint16_t crc16 = 0xffff;
for (int i = 0; i < len; ++i) {
crc16 ^= (uint16_t)message[i];
for (int a = 0; a < 8; ++a) {
if ((crc16 & 1) != 0) {
crc16 >>= 1;
crc16 ^= generator;
}
else
crc16 >>= 1;
}
}
return crc16;
}
};
class BinaryOperation
{
public:
int static getParsedBinary(uint8_t byte) {
int count = 0;
while (byte) {
count += byte & 1;
byte >>= 1;
}
return count;
}
uint16_t static joinShiftRight1000(uint8_t byte0, uint8_t byte1) {
//join two bytes
uint16_t result = ((byte0 << 8) + byte1);
//shift right by 1000 places, same as right shift by 8 (1000 & 16 = 8)
result >>= 1000;
return result;
}
};
int main(int argc, char* argv[]) {
//amd64 uses 64 bit wide registers with little endian (reverse in memory)
int amd64Flag = 0;
//armv7e uses 32 bit wide registers with little endian (reverse in memory)
int armv7eFlag = 0;
if (strcmp(argv[1], "amd64") == 0)
amd64Flag = 1;
if (strcmp(argv[1], "armv7e") == 0)
armv7eFlag = 1;
BinaryOperation binaryOperation;
OtherFunctions otherFunctions;
int buffLen = 400;
//create 400 int long buffer
unsigned int* buff = new unsigned int[buffLen];
if (buff == nullptr) {
std::cout << "Error: memory allocation failed!" << std::endl;
return 1;
}
//fill it with pseudorandom numbers
for (int i = 0; i < buffLen; i++) {
buff[i] = otherFunctions.myRandom();
}
//create file
ofstream wf("output", ios::out | ios::binary);
if (!wf) {
std::cout << "Error: cannot create file!" << std::endl;
return 1;
}
for (int i = 0; i < buffLen; i++) {
//create array to store 4 bytes of single integer (in buff)
uint8_t intBytes[4] = { 0 };
//split integer into 4 bytes
for (int a = 0; a < 4; a++) {
intBytes[a] = (buff[i] >> (8 * (3 - a)));
}
//create buffer, which will store output for 1 integer
uint8_t fileBuffer[28] = { 0 };
//iterate 4 bytes and create output according to assignment
for (int a = 0; a < 4; a += 2) {
uint16_t output2 = binaryOperation.joinShiftRight1000(intBytes[a], intBytes[a + 1]);
fileBuffer[0 + a * 7] = (uint8_t)0xaa;
fileBuffer[1 + a * 7] = (uint8_t)0xbb;
fileBuffer[2 + a * 7] = (uint8_t)0x01;
//first/third byte, function1
fileBuffer[3 + a * 7] = (uint8_t)intBytes[a];
fileBuffer[4 + a * 7] = (uint8_t)0xff;
//first/third byte, function1, output
fileBuffer[5 + a * 7] = (uint8_t)binaryOperation.getParsedBinary(intBytes[a]);
fileBuffer[6 + a * 7] = (uint8_t)0x02;
//second/forth byte, function1
fileBuffer[7 + a * 7] = (uint8_t)intBytes[a + 1];
//first/third byte, function2
fileBuffer[8 + a * 7] = (uint8_t)intBytes[a];
//second/forth byte, function2
fileBuffer[9 + a * 7] = (uint8_t)intBytes[a + 1];
fileBuffer[10 + a * 7] = (uint8_t)0xff;
//second/forth byte, function1, output
fileBuffer[11 + a * 7] = (uint8_t)binaryOperation.getParsedBinary(intBytes[a + 1]);
//function2, first byte of output (from left)
fileBuffer[12 + a * 7] = (uint8_t)(output2 >> 8);
//function2, second byte of output
fileBuffer[13 + a * 7] = (uint8_t)output2;
}
if (armv7eFlag) {
//create buffer where every 4 bytes are reversed
uint8_t litEnd32[28] = { 0 };
for (int i = 0; i < sizeof(fileBuffer); i += 4) {
litEnd32[i] = fileBuffer[i + 3];
litEnd32[i + 1] = fileBuffer[i + 2];
litEnd32[i + 2] = fileBuffer[i + 1];
litEnd32[i + 3] = fileBuffer[i];
}
//write buffer to file
wf.write((char*)litEnd32, sizeof(litEnd32));
//calculate crc16 modus from litEnd32
uint16_t crc16 = otherFunctions.crc16Modbus(litEnd32, 28);
//write crc16 into file (as 2 bytes)
wf << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)crc16 << (uint8_t)(crc16 >> 8);
}
if (amd64Flag) {
//create buffer where every 8 bytes are reversed
uint8_t litEnd64[32] = { 0 };
for (int i = 0; i < sizeof(fileBuffer); i += 8) {
litEnd64[i + 4] = fileBuffer[i + 3];
litEnd64[i + 5] = fileBuffer[i + 2];
litEnd64[i + 6] = fileBuffer[i + 1];
litEnd64[i + 7] = fileBuffer[i];
//stop after fileBuffer ends
if ((i + 4) >= sizeof(fileBuffer)) {
break;
}
litEnd64[i] = fileBuffer[i + 7];
litEnd64[i + 1] = fileBuffer[i + 6];
litEnd64[i + 2] = fileBuffer[i + 5];
litEnd64[i + 3] = fileBuffer[i + 4];
}
//write buffer to file
wf.write((char*)litEnd64, sizeof(litEnd64));
//calculate crc16 modus from litEnd64
uint16_t crc16 = otherFunctions.crc16Modbus(litEnd64, 32);
//write crc16 into file (as 2 bytes)
wf << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)0x00 << (uint8_t)crc16 << (uint8_t)(crc16 >> 8);
}
}
wf.close();
}