-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_05.cpp
358 lines (328 loc) · 12.9 KB
/
lab_05.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// C++ program CRC and checksum calculator and error detector
// Archeticture ECCE5232
// Muslim Albalushi (114860)
#include<stdio.h>
#include<iostream>
#include<math.h>
#include <fstream>
#include <string.h>
using namespace std;
string Remainder, Codeword; char a[32], sum[32],comp[32];
/////
void checksum2(char checksum[4]){
char carr='0';
for(int t=3; t>=0; t--){
if(checksum[t]=='0'&&sum[t]=='0'&&carr=='0')
{sum[t]='0'; }
else if(checksum[t]=='0'&&sum[t]=='1'&&carr=='0')
{sum[t]='0'; }
else if(checksum[t]=='1'&&sum[t]=='0'&&carr=='0')
{sum[t]='0'; }
else if(checksum[t]=='1'&&sum[t]=='1'&&carr=='0')
{sum[t]='0'; carr='1';}
else if(checksum[t]=='0'&&sum[t]=='0'&&carr=='1')
{sum[t]='1'; }
else if(checksum[t]=='0'&&sum[t]=='1'&&carr=='1')
{sum[t]='0'; carr='1';}
else if(checksum[t]=='1'&&sum[t]=='0'&&carr=='1')
{sum[t]='0'; carr='1';}
else if(checksum[t]=='1'&&sum[t]=='1'&&carr=='1')
{sum[t]='1'; carr='1';}
}
if(carr=='1'){
if(sum[3]=='0')
sum[3]='1';
else if(sum[3]=='1'&&sum[2]=='0')
{sum[2]='1'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='0')
{sum[1]='1'; sum[2]='0'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='1'&&sum[0]=='0')
{sum[0]='1'; sum[1]='0'; sum[2]='0'; sum[3]='0';}
else
carr='0';
}
}
////
void checksum(){
char carry='0';
int i, ov=0, count=3;
int length=strlen(a);
// cout<<"\n"<<a<<" "<<length;
if(strlen(a)==8||strlen(a)==16||strlen(a)==24||strlen(a)==32){
for(i=length-1 ; i>=length-4 ; i--){
//cout<<endl<<a[i-4]<<endl;
if(length==8 && a[i]=='0' && a[i-4]=='0' && carry=='0')
sum[count]='0';
else if(length==8 && a[i]=='0' && a[i-4]=='1' && carry=='0')
sum[count]='1';
else if(length==8 && a[i]=='1' && a[i-4]=='0' && carry=='0')
sum[count]='1';
else if(length==8 && a[i]=='1' && a[i-4]=='1' && carry=='0')
{ sum[count]='0'; carry='1'; }
//
else if(length==8 && a[i]=='0' && a[i-4]=='0' && carry=='1')
{sum[count]='1'; carry='0'; }
else if(length==8 && a[i]=='0' && a[i-4]=='1' && carry=='1')
{ sum[count]='0'; carry='1'; }
else if(length==8 && a[i]=='1' && a[i-4]=='0' && carry=='1')
{ sum[count]='0'; carry='1'; }
else if(length==8 && a[i]=='1' && a[i-4]=='1' && carry=='1')
{sum[count]='1'; carry='1'; }
/////////////////////
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='0' && carry=='0')
sum[count]='0';
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='0')
sum[count]='1';
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='0')
sum[count]='1';
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='0')
sum[count]='0';
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='0')
{ sum[count]='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='0')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='0')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='0')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='0')
{sum[count]='0'; carry='0', ov++;}
//
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='0' && carry=='1')
{sum[count]='1'; carry='0';}
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='1')
{sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='1')
{sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='0' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='0'; carry='0'; ov++;}
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='0'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='0' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='0'; carry='0'; ov++;}
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='1'; carry='1'; }
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='0' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='0'; carry='0'; ov++;}
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='0'&& carry=='1')
{ sum[count]='0'; carry='0'; ov++;}
else if(length==16 && a[i]=='1' && a[i-4]=='1' &&a[i-8]=='1' &&a[i-12]=='1'&& carry=='1')
{ sum[count]='1'; carry='0'; ov++;}
else
break;
//
count--;
} //for loop end
}
/*
for (int k=3 ; k>=0 ; k--){
cout <<"\n"<<sum[k];
} */
if(carry=='1'){
if(sum[3]=='0')
sum[3]='1';
else if(sum[3]=='1'&&sum[2]=='0')
{sum[2]='1'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='0')
{sum[1]='1'; sum[2]='0'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='1'&&sum[0]=='0')
{sum[0]='1'; sum[1]='0'; sum[2]='0'; sum[3]='0';}
else
carry='0';
}
for(ov ; ov>0 ; ov--){
if(sum[3]=='0')
sum[3]='1';
else if(sum[3]=='1'&&sum[2]=='0')
{sum[2]='1'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='0')
{sum[1]='1'; sum[2]='0'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='1'&&sum[0]=='0')
{sum[0]='1'; sum[1]='0'; sum[2]='0'; sum[3]='0';}
else if(sum[3]=='1'&&sum[2]=='1'&&sum[1]=='1'&&sum[0]=='1')
{cout<<"\nOverflow\n";}
}
//complement checksum
for(int t=3; t>=0; t--){
if(sum[t]=='0')
comp[t]='1';
else
comp[t]='0';
}
}
// function to convert integer to binary string
string toBin(long long int num){
string bin = "";
while (num){
if (num & 1)
bin = "1" + bin;
else
bin = "0" + bin;
num = num>>1;
}
if (bin=="")
bin="0000";
return bin;
}
// function to convert binary string to decimal
long long int toDec(string bin){
long long int num = 0;
for (int i=0; i<bin.length(); i++){
if (bin.at(i)=='1')
num += 1 << (bin.length() - i - 1);
}
return num;
}
// function to compute CRC and codeword
void CRC(string dataword, string generator){
int l_gen = generator.length();
long long int gen = toDec(generator);
long long int dword = toDec(dataword);
// append 0s to dividend
long long int dividend = dword << (l_gen-1);
// shft specifies the no. of least
// significant bits not being XORed
int shft = (int) ceill(log2l(dividend+1)) - l_gen;
long long int rem;
while ((dividend >= gen) || (shft >= 0)){
// bitwise XOR the MSBs of dividend with generator
// replace the operated MSBs from the dividend with
// remainder generated
rem = (dividend >> shft) ^ gen;
dividend = (dividend & ((1 << shft) - 1)) | (rem << shft);
// change shft variable
shft = (int) ceill(log2l(dividend + 1)) - l_gen;
}
// finally, AND the initial dividend with the remainder (=dividend)
long long int codeword = (dword << (l_gen - 1)) | dividend;
Codeword=toBin(codeword);
Remainder=toBin(dividend);
}
int main(){
char choice;
string dataword, generator="11101";
START:
cout << "=================================================\n";
cout << "Please choose the operation you want to do:" << '\n';
cout << "(a) store the data to the memory\n";
cout << "(b) check the data from the memory" << '\n';
cout << "Please Enter option here:";
cin >> choice;
switch (choice) {
case 'a':
case 'A':
{
int CheckType;
cout<<"\n==============================================="<<endl;
cout<<"1) CRC\n";
cout<<"2) Checksum\n";
cout<<"Choose Error Detection type(1-2):";
cin>>CheckType;
if (CheckType==1) {
cout << "\nPlease Enter the data in binary (to store) in memory :";
cin >> dataword;
CRC(dataword, generator);
ofstream fout("Memory.txt");
fout<<dataword<<" "<<Codeword<<" "<<Remainder;
fout.close();
cout<<"\n==============================================="<<endl;
cout << "Entered Data: " << dataword<<endl;
cout << "Remainder: " << Remainder<<endl;
cout << "Encoded Data (Data + Remainder) : " << Codeword<<endl;
//goto START;
return 0;
}
if(CheckType==2){
cout << "\nPlease Enter the data in binary (to store) in memory :";
cin >> a;
checksum();
cout<<"\n=================================================";
cout<<"\nData Entered :"<<a<<endl;
cout<<"Sum :"<<sum[0]<<sum[1]<<sum[2]<<sum[3];
cout<<"\nChecksum: :"<<comp[0]<<comp[1]<<comp[2]<<comp[3]<<endl;
return 0;
}
}
break;
case 'b':
case 'B':
{
int CheckType;
cout<<"\n==============================================="<<endl;
cout<<"1) CRC\n";
cout<<"2) Checksum\n";
cout<<"Choose Error Detection type used(1-2):";
cin>>CheckType;
if (CheckType==1) {
cout << "\nPlease Enter the [data+Remainder] in binary (read from) memory :";
cin >> dataword;
CRC(dataword, generator);
cout<<"\n==============================================="<<endl;
cout << "Entered Data: " << dataword<<endl;
cout << "Remainder: " << Remainder<<endl;
if(Remainder=="0000")
cout<<"Remainder is ZERO No Error in Data reading\n";
else
cout<<"Remainder is NOT ZERO There is an Error in Data reading\n";
//goto START;
return 0;
}
if(CheckType==2){
char test[4];
cout << "\nPlease Enter the data in binary (read from) memory :";
cin >> a;
cout<<"\nEnter the checksum used(recieved):";
cin>>test;
checksum();
checksum2(test);
cout<<"\n=================================================";
cout<<"\nData Entered :"<<a<<endl;
cout<<"Sum :"<<sum[0]<<sum[1]<<sum[2]<<sum[3]<<endl;
if(sum[0]=='0'&&sum[1]=='0'&&sum[2]=='0'&&sum[3]=='0')
cout<<"\n** No ERROR since Sum is ZERO **\n";
else
cout<<"\n!! There is an ERROR since Sum is not ZERO !!\n";
return 0;
}
} //
break;
case 'q': case 'Q':
{
cout <<"\nExiting . . .";
return 0;
}
break;
default :
{
cout<<"\nNot a valid choice try again\n";
goto START;
}
}
return 0;
}