-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
343 lines (282 loc) · 11.4 KB
/
script.js
File metadata and controls
343 lines (282 loc) · 11.4 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
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
// Mode switching
document.querySelectorAll('.mode-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.mode-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.section').forEach(s => s.classList.remove('active'));
btn.classList.add('active');
const mode = btn.dataset.mode;
document.getElementById(`${mode}-section`).classList.add('active');
});
});
// ==================== CRC GENERATOR ====================
async function generateCRC() {
const data = document.getElementById('crc-data').value.replace(/\s/g, '');
const divisor = document.getElementById('crc-divisor').value.replace(/\s/g, '');
if (!data || !divisor) {
alert('Please enter both data and divisor');
return;
}
if (!/^[01]+$/.test(data) || !/^[01]+$/.test(divisor)) {
alert('Please enter valid binary strings (only 0s and 1s)');
return;
}
const animationContainer = document.getElementById('crc-animation');
const outputContainer = document.getElementById('crc-output');
animationContainer.innerHTML = '<h3>CRC Generation Process</h3>';
outputContainer.innerHTML = '';
// Calculate number of padding zeros
const padding = divisor.length - 1;
const paddedData = data + '0'.repeat(padding);
// Display initial setup
animationContainer.innerHTML += `
<div class="crc-step">
<div class="step-title">Step 1: Append zeros to data</div>
<div>Original Data: <strong>${data}</strong></div>
<div>Divisor: <strong>${divisor}</strong></div>
<div>Number of zeros to append: ${padding} (divisor length - 1)</div>
<div>Padded Data: <strong>${paddedData}</strong></div>
</div>
`;
await sleep(1000);
// Perform division with animation
let dividend = paddedData;
let result = '';
let stepCount = 0;
animationContainer.innerHTML += `
<div class="crc-step">
<div class="step-title">Step 2: Polynomial Division</div>
<div class="division-box" id="division-visualization"></div>
</div>
`;
const divVisualization = document.getElementById('division-visualization');
// Perform binary division
while (dividend.length >= divisor.length) {
stepCount++;
// Find the first '1' in dividend
if (dividend[0] === '0') {
dividend = dividend.substring(1);
continue;
}
// Perform XOR operation
let xorResult = '';
let stepDiv = '';
for (let i = 0; i < divisor.length; i++) {
const divBit = divisor[i];
const divBitValue = parseInt(dividend[i] || '0');
const genBitValue = parseInt(divBit);
const xorValue = divBitValue ^ genBitValue;
xorResult += xorValue;
stepDiv += divBit;
}
// Create visualization for this step
const stepDivHTML = `
<div class="divisor-row" id="step-${stepCount}">
<span style="margin-right: 20px;">Step ${stepCount}:</span>
${dividend.substring(0, divisor.length).split('').map((bit, idx) =>
`<span class="bit active" style="animation-delay: ${idx * 0.1}s">${bit}</span>`
).join('')}
<span style="margin: 0 20px;">⊕</span>
${stepDiv.split('').map((bit, idx) =>
`<span class="bit" style="animation-delay: ${(divisor.length + idx) * 0.1}s">${bit}</span>`
).join('')}
<span style="margin: 0 20px;">=</span>
${xorResult.split('').map((bit, idx) =>
`<span class="bit xor" style="animation-delay: ${(divisor.length * 2 + idx) * 0.1}s">${bit}</span>`
).join('')}
</div>
`;
divVisualization.innerHTML += stepDivHTML;
// Highlight the current step
const stepElement = document.getElementById(`step-${stepCount}`);
stepElement.classList.add('active');
await sleep(1500);
// Remove leading zeros from XOR result
xorResult = xorResult.replace(/^0+/, '') || '0';
// Get remaining bits from dividend
const remaining = dividend.substring(divisor.length);
// New dividend
dividend = xorResult + remaining;
if (dividend.length < divisor.length) {
break;
}
await sleep(500);
}
// Calculate remainder
const remainder = dividend.padStart(padding, '0');
const crcCode = data + remainder;
await sleep(1000);
// Display final result
divVisualization.innerHTML += `
<div class="divisor-line"></div>
<div class="remainder-box">
<strong>Remainder (CRC):</strong> ${remainder}
</div>
`;
await sleep(1000);
outputContainer.innerHTML = `
<div class="result-box">
<h2>CRC Generation Complete!</h2>
<div class="code-display">Original Data: ${data}</div>
<div class="code-display">CRC Remainder: ${remainder}</div>
<div class="code-display">Final Code: ${crcCode}</div>
<div style="margin-top: 15px;">
<div>Data Length: ${data.length} bits</div>
<div>CRC Length: ${remainder.length} bits</div>
<div>Total Length: ${crcCode.length} bits</div>
</div>
</div>
`;
}
// ==================== HAMMING CODE ====================
function generateHamming() {
const data = document.getElementById('hamming-data').value.replace(/\s/g, '');
if (!data) {
alert('Please enter binary data');
return;
}
if (!/^[01]+$/.test(data)) {
alert('Please enter valid binary string (only 0s and 1s)');
return;
}
if (data.length !== 4 && data.length !== 8) {
alert('Please enter 4 or 8 bits of data');
return;
}
const animationContainer = document.getElementById('hamming-animation');
const outputContainer = document.getElementById('hamming-output');
animationContainer.innerHTML = '<h3>Hamming Code Generation Process</h3>';
outputContainer.innerHTML = '';
// Calculate number of parity bits
const dataBits = data.length;
let m = dataBits;
let r = 1;
while (Math.pow(2, r) < m + r + 1) {
r++;
}
const totalBits = m + r;
// Create positions array
const positions = Array(totalBits).fill(null);
let dataIndex = 0;
// Place data bits in non-power-of-2 positions
for (let i = 1; i <= totalBits; i++) {
if (!isPowerOfTwo(i)) {
positions[i - 1] = {
value: data[dataIndex++],
type: 'data',
position: i
};
}
}
// Calculate parity bits
const parityBits = [];
for (let p = 0; p < r; p++) {
const parityPos = Math.pow(2, p);
const checkPositions = getCheckPositions(parityPos, totalBits);
// Count ones in positions to check
let ones = 0;
checkPositions.forEach(pos => {
if (positions[pos - 1] && positions[pos - 1].value === '1') {
ones++;
}
});
// Set parity bit (even parity)
const parityValue = (ones % 2 === 0) ? '0' : '1';
positions[parityPos - 1] = {
value: parityValue,
type: 'parity',
position: parityPos,
checkedPositions: checkPositions
};
parityBits.push({ position: parityPos, value: parityValue });
}
// Display animation
displayHammingAnimation(animationContainer, positions, parityBits, data);
// Generate final code
const hammingCode = positions.map(p => p.value).join('');
setTimeout(() => {
outputContainer.innerHTML = `
<div class="result-box">
<h2>Hamming Code Generation Complete!</h2>
<div class="code-display">Original Data: ${data}</div>
<div class="code-display">Hamming Code: ${hammingCode}</div>
<div style="margin-top: 15px;">
<div>Data Bits: ${dataBits}</div>
<div>Parity Bits: ${r}</div>
<div>Total Bits: ${totalBits}</div>
<div style="margin-top: 10px;">
<strong>Parity Bits:</strong><br>
${parityBits.map(p => `P${p.position} = ${p.value}`).join('<br>')}
</div>
</div>
</div>
`;
}, 2000 + parityBits.length * 1000);
}
function displayHammingAnimation(container, positions, parityBits, originalData) {
const grid = document.createElement('div');
grid.className = 'hamming-grid';
grid.id = 'hamming-grid';
// Create initial grid
positions.forEach((pos, index) => {
const bitDiv = document.createElement('div');
bitDiv.className = `hamming-bit ${pos.type}`;
bitDiv.id = `bit-${index + 1}`;
bitDiv.innerHTML = `
<div>${pos.value || '?'}</div>
<div class="bit-label">${isPowerOfTwo(index + 1) ? `P${index + 1}` : `D${index + 1}`}</div>
`;
grid.appendChild(bitDiv);
});
container.appendChild(grid);
// Animate step by step
setTimeout(async () => {
// Show data bits
positions.forEach((pos, index) => {
if (pos.type === 'data') {
setTimeout(() => {
const bitEl = document.getElementById(`bit-${index + 1}`);
bitEl.classList.add('active');
}, index * 200);
}
});
// Calculate and show parity bits
for (let i = 0; i < parityBits.length; i++) {
const parity = parityBits[i];
await sleep(1500);
const bitEl = document.getElementById(`bit-${parity.position}`);
bitEl.classList.remove('active');
bitEl.classList.add('calculated');
// Highlight checked positions
parity.checkedPositions.forEach(pos => {
if (pos !== parity.position) {
const checkEl = document.getElementById(`bit-${pos}`);
checkEl.style.boxShadow = '0 0 10px #ff6b6b';
}
});
await sleep(1000);
// Remove highlights
parity.checkedPositions.forEach(pos => {
const checkEl = document.getElementById(`bit-${pos}`);
checkEl.style.boxShadow = '';
});
bitEl.classList.remove('calculated');
bitEl.classList.add('active');
}
}, 500);
}
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}
function getCheckPositions(parityPos, totalBits) {
const positions = [];
for (let i = parityPos; i <= totalBits; i++) {
if ((i & parityPos) !== 0) {
positions.push(i);
}
}
return positions;
}
// Utility function
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}