-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfault_modeling.py
More file actions
383 lines (309 loc) · 10.8 KB
/
Copy pathfault_modeling.py
File metadata and controls
383 lines (309 loc) · 10.8 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from math import comb
import config as config
import LogicOperation as logicop
from ProbCalc import calculate_probability
TRd_size = config.TRd_size
bit_length = config.bit_length
memory_size = config.memory_size
# Initializing single Local Buffer for all DBC's
Local_row_buffer = [0] * (bit_length)
word_size = (bit_length // 8)
n = word_size + 8
fault_matrix = [0] * bit_length
fault_matrix_word = {}
faultpercent = 10**(-6)
def fault_modeling(faultpercent):
k1 = 1
k2 = 2
## Probability for 0,1,2, 3 plus bit error:
p_0 = (1 - faultpercent)**(n)
p_1 = comb(n, k1)*(faultpercent**k1)
p_2 = comb(n, k2)*(faultpercent**k2)
p_3plus = 1 - (p_0 + p_1 + p_2)
# print('p_0,p_1,p_2,p_3plus',p_0,p_1,p_2,p_3plus)
def f_percent_model(memory, row_number, nanowire_num_start_pos, nanowire_num_end_pos):
# word_size_start = 0
# word_size_end = word_size
# while word_size_end < nanowire_num_end_pos:
# divide the memory into words
for i in range(nanowire_num_start_pos, nanowire_num_end_pos):
count = 0
# count no of 1's between TRd heads
for j in range(row_number, TRd_size + row_number):
if memory[j][i] == "1":
count+=1
fault_matrix[i] = count
# divide the memory into words
word_size_start = 0
word_size_end = word_size
k = 0
while word_size_end <= nanowire_num_end_pos+1:
failure_rate_50 = 0
failure_rate_100 = 0
p_50_success = 0
p_100_success = 0
p_success_word = 0
for i in range(word_size_start, word_size_end):
if fault_matrix[i] == config.TRd_size or fault_matrix[i] == 0:
failure_rate_50 += 1
else:
failure_rate_100 += 1
key_word = 'word_{}'.format(k)
# p_50_success = (1 - 0.5*faultpercent)**failure_rate_50
# p_100_success = (1 - faultpercent)**failure_rate_100
# p_success_word = p_50_success*p_100_success
E = failure_rate_50
probabilities = calculate_probability(E)
fault_matrix_word[key_word] = sum(probabilities[0:3])
fault_matrix_word[key_word] *= fault_matrix_word[key_word]
# # Display the probabilities for F=0 to F=max_fault
# for i, prob in enumerate(probabilities):
# print(f"P(F = {i}) = {prob:.5e}")
word_size_start = word_size_end
word_size_end = word_size_start + word_size
k += 1
print(fault_matrix_word)
def fault_addition(memory, row_number, nanowire_num_start_pos, nanowire_num_end_pos):
TRd_head = int(row_number)
TRd_end_loc = TRd_head + TRd_size - 1
result = ''
# Fill AP0 and AP1 with 0's
for i in range(nanowire_num_start_pos, nanowire_num_end_pos):
memory[TRd_head][i] = '0'
memory[TRd_end_loc][i] = '0'
# display after appending zeros
# display(memory, 0, 'AP0')
for i in range(nanowire_num_start_pos, nanowire_num_end_pos - 1):
carry = carry_add(memory, TRd_head, i, i)
# write carry at next nanowire at AP1
memory[TRd_end_loc][i + 1] = carry
for i in range(nanowire_num_start_pos, nanowire_num_end_pos - 2):
carry_prime = carry_prime_add(memory, TRd_head, i, i)
# write carry prime at next to next nanowire at AP0
memory[TRd_head][i + 2] = carry_prime
for i in range(nanowire_num_start_pos, nanowire_num_end_pos):
sum = xor_add(memory, TRd_head, i, i)
# write sum at the same nanowire at AP0
memory[TRd_head][i] = sum
result += sum
# display(memory, TRd_head, 'AP0')
# print(result)
# Converting binary data at TRd head to Hex for verification/visualization
count = 0
s = ''
hex_num = '0x'
for i in range(0, len(result)):
s += str(result[i])
count += 1
if count == 4:
num = int(s, 2)
string_hex_num = format(num, 'x')
hex_num += (string_hex_num)
s = ''
count = 0
return hex_num
def fault_mult():
TRd_head = int(row_number)
TRd_end_loc = TRd_head + TRd_size - 1
# display(memory, TRd_head, 'AP0')
result = ''
carry = ''
carry_prime = ''
sum = ''
# call carry, carry prime and xor till three operand
l = nanowire_num_end_pos
while (TRd_size - 2) < (l - TRd_head):
for i in range(0, 511):
carry += carry_add(memory, TRd_head, i, i)
carry_prime += carry_prime_add(memory, TRd_head, i, i)
sum += xor_add(memory, TRd_head, i, i)
# write c, c' and sum
for i in range(0, 510):
memory[l][i + 1] = carry[i]
for i in range(0, 509):
memory[l + 1][i + 2] = carry_prime[i]
for i in range(0, 511):
memory[l + 2][i] = sum[i]
# display(memory, TRd_head, 'AP0')
TRd_head += TRd_size
l += 3
# Call ADD function
result = addition(memory, TRd_head - 1, nanowire_num_start_pos, 16)
# print('result', result)
# # Converting binary data at TRd head to Hex for verification/visualization
# count = 0
# s = ''
# hex_num = '0x'
# for i in range(0, len(result)):
# s += str(result[i])
# count += 1
# if count == 4:
# num = int(s, 2)
# string_hex_num = format(num, 'x')
# hex_num += (string_hex_num)
# s = ''
# count = 0
return result
def xor_add(memory, row_number, nanowire_num_start_pos, nanowire_num_end_pos):
TRd_head = int(row_number)
TRd_end_loc = TRd_head + TRd_size - 1
for i in range(nanowire_num_start_pos, nanowire_num_end_pos + 1):
c = 0
for j in range(TRd_head, TRd_end_loc + 1):
if memory[j][i] == '1':
c += 1
if (c % 2 == 0):
val = '0'
else:
val = '1'
return val
def carry_add(memory, row_number, nanowire_num_start_pos, nanowire_num_end_pos):
TRd_head = int(row_number)
TRd_end_loc = TRd_head + TRd_size - 1
for i in range(nanowire_num_start_pos, nanowire_num_end_pos + 1):
c = 0
for j in range(TRd_head, TRd_end_loc + 1):
if memory[j][i] == '1':
c += 1
if (c == 2 or c == 3 or c == 6 or c == 7):
val = '1'
else:
val = '0'
return val
def carry_prime_add(memory, row_number, nanowire_num_start_pos, nanowire_num_end_pos):
TRd_head = int(row_number)
TRd_end_loc = TRd_head + TRd_size - 1
for i in range(nanowire_num_start_pos, nanowire_num_end_pos + 1):
count = 0
for j in range(TRd_head, TRd_end_loc + 1):
if memory[j][i] == '1':
count += 1
if (count == 4 or count == 5 or count == 6 or count == 7):
val = '1'
else:
val = '0'
return val
def shifted_by_one(data, bit_length):
shifted_A = [[('0') for _ in range(bit_length)] for _ in range(bit_length)]
output = data
for i in range(0, bit_length):
# call function to shift data by 1
output = shift(output)
for j in range(0, bit_length):
shifted_A[i][j] = output[j]
return shifted_A
def shift(data):
bit_length = len(data)
# Logical shift
n = 1
output = [0] * bit_length
count = 0
for i in range(n, bit_length):
output[count] = data[i]
count += 1
for i in range(count, bit_length):
output[i] = '0'
return output
## Driver Code:
fault_modeling(faultpercent)
'''
# def parity_checking(memory, TRd_head, nanowire_num_start_pos, nanowire_num_end_pos):
# """
# Parity checking is a method for detecting errors in data. It works by generating a parity bit,
# which is a single bit that is calculated from the data. The parity bit is then used to check
# if there are any errors in the data.
#
# Args:
# data: The data to be checked.
#
# Returns:
# True if there are no errors in the data, False otherwise.
# """
#
#
#
#
#
# # Shifting the data within the TRd space to right and writing at the TRd head
# for i in range(TRd_head, TRd_head + TRd_size - 1):
# # Calculate the parity bit.
# parity_bit = 0
# for j in range(nanowire_num_start_pos, nanowire_num_end_pos + 1):
# bit = memory[i][j]
# # parity_bit ^= bit
# Local_buffer = logicop.Xor(memory, TRd_head, nanowire_num_start_pos, nanowire_num_end_pos)
#
# # # Check if the parity bit is 0.
# # for i in range(0, len(Local_buffer)):
# # if i == 0:
# # return True
# # # call reed solomon
# # #error_correction
# # # call
# # else:
# # return False
#
# return Local_buffer
#
# def reed_solomon(data, k, n):
# """
# Reed-Solomon error correction is a method for correcting errors in data. It works by generating a parity code, which is a redundant representation of the data. The parity code is then used to detect and correct errors in the data.
#
# Args:
# data: The data to be protected.
# k: The number of data bits.
# n: The total number of bits.
#
# Returns:
# The corrected data.
# """
#
# # Check if the parameters are valid.
# if k > n:
# raise ValueError("The number of data bits must be less than or equal to the total number of bits.")
#
# # Generate the parity code.
# parity_code = np.zeros(n - k, dtype=int)
# for i in range(n - k):
# for j in range(k):
# parity_code[i] ^= data[j] * (2 ** i)
#
# # Check if there are any errors in the data.
# if np.any(data != np.bitwise_xor(data, parity_code)):
# # There are errors in the data.
# # Correct the errors using the parity code.
# for i in range(n - k):
# if data[i] != np.bitwise_xor(data[i], parity_code[i]):
# # There is an error in the current bit.
# # Correct the error by flipping the bit.
# data[i] = 1 - data[i]
#
# # Return the corrected data.
# return data
#
#
# def corrective_shift(memory, TRd_head, nanowire_num_start_pos, nanowire_num_end_pos):
# """
# Error correction is a method for correcting errors in data. It works by comparing the data to a known good copy of the data.
# The errors are then corrected using a variety of techniques, such as majority voting and Reed-Solomon error correction.
#
# Args:
# data: The data to be corrected.
# shift_distance: The distance that the data has been shifted.
#
# Returns:
# The corrected data.
# """
# # Calling parity checking method to determine single bit error:
# Local_buffer = parity_checking(memory, TRd_head, nanowire_num_start_pos, nanowire_num_end_pos)
# data = memory[TRd_head][:]
# # # Check if the parity bit is 0.
# for i in range(0, len(Local_buffer)):
# if i == 1:
# # There is an error in the current bit.
# # Correct the error by flipping the bit.
# data[i][:] = 1 - data[i][:]
#
# # Return the corrected data.
# return data
'''