-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimulation.py
343 lines (294 loc) · 10.6 KB
/
simulation.py
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
# -*- coding: utf-8 -*-
__author__ = 'IPPM RAS: https://github.com/IDMIPPM/'
import random
import itertools
import copy
import utils as u
from sys import platform
import read_write as rw
import os
def random_stimulus(inp_num, capacity):
stimulus = [random.randrange(0, 2 ** capacity - 1) for _ in range(inp_num)]
return stimulus
def exhaustive_stimulus(inp_num):
capacity = 2**inp_num
tmp=1
res = []
for i in range(inp_num-1, -1, -1):
res.append((2 ** (2 ** i) - 1)*tmp)
tmp = tmp*(1+2**(2**i))
return (res, capacity)
def pseudo_random_stimulus(inp_num):
if inp_num < 16:
return exhaustive_stimulus(inp_num)
exh_part = exhaustive_stimulus(13)
rnd_part = random_stimulus(inp_num-13, 2**13)
res = exh_part[0] + rnd_part
return res, exh_part[1]
def simulate_outputs(scheme, capacity, stimulus=[]):
"""
function to simulate outputs
:param scheme: scheme under test in scheme_alt format
:param capacity: number of bits for simultaneous modeling
:return: returns list of tuples [(node_name, num), ...] num - decimal reaction on stimulus
"""
if stimulus == []:
stimulus = random_stimulus(scheme.inputs(), capacity)
reaction = scheme.process(stimulus, [], capacity)
res = {}
for i in range(len(reaction)):
react = "{0:b}".format(reaction[i])
react = '0'*(capacity - len(react)) + react
res[scheme.__outputs__[i]] = react
#res = list(zip(scheme.__outputs__, reaction))
return res
def form_target_array(scheme, etalon, capacity, tgts, stimulus):
etalon_reply = simulate_outputs(etalon, capacity, stimulus)
reply = {}
for vector in itertools.product((0, 1), repeat=len(tgts)):
#print('Target vector: {}'.format(vector))
# Forming design under test
dut = copy.deepcopy(scheme)
for i in range(len(tgts)):
if vector[i] == 0:
dut.__elements__[tgts[i]] = ('GND', [])
if vector[i] == 1:
dut.__elements__[tgts[i]] = ('VCC', [])
reply[vector] = simulate_outputs(dut, capacity, stimulus)
outputs = list(etalon_reply.keys())
length = len(etalon_reply[outputs[0]])
tgt_vecs = list(reply.keys())
target_array = []
for i in range(length):
etal = ''
for out in outputs:
etal += etalon_reply[out][i]
fits = []
for tgt_vec in tgt_vecs:
cand = ''
for out in outputs:
cand += reply[tgt_vec][out][i]
if cand == etal:
fits.append(tgt_vec)
target_array.append(fits)
return target_array
def reduce_stimulus(stimulus, target_array, capacity):
input_number = len(stimulus)
max_d_c = 2**len(target_array[0][0])
new_target_array = []
new_stimulus = [0]*input_number
new_capacity = 0
for i in range(len(target_array) - 1, -1, -1):
target_variant = target_array[i]
if len(target_variant) != max_d_c:
new_target_array.append(target_variant)
cap2 = (1 << new_capacity)
for j in range(input_number):
current_bit = (stimulus[j] >> (capacity - i - 1)) & 1
if current_bit:
new_stimulus[j] += cap2
new_capacity += 1
new_target_array = new_target_array[::-1]
return new_stimulus, new_target_array, new_capacity
def critical_stimulus(stimulus, target_array, capacity):
input_number = len(stimulus)
new_stimulus = [0]*input_number
new_capacity = 0
for i in range(len(target_array) - 1, -1, -1):
target_variant = target_array[i]
if len(target_variant) == 0:
cap2 = (1 << new_capacity)
for j in range(input_number):
current_bit = (stimulus[j] >> (capacity - i - 1)) & 1
if current_bit:
new_stimulus[j] += cap2
new_capacity += 1
return new_stimulus, new_capacity
def form_nodes_list(scheme, tgts):
nodes_list = []
for target in tgts:
nodes_list += u.cone_to_outs(scheme, target)
exclude = list(set(nodes_list))
nodes_list = [item for item in scheme.element_labels() if item not in exclude]
full_nodes_list = nodes_list + scheme.input_labels()
#print('Total elements:', scheme.elements(), ' Used wires: ', len(nodes_list))
return full_nodes_list
def form_nodes_list2(scheme, tgts, all_sign_inps):
nodes_list = []
for target in tgts:
nodes_list += u.cone_to_outs(scheme, target)
exclude = list(set(nodes_list))
infl_list = u.cone_to_outs_v2(scheme, all_sign_inps)
nodes_list = [item for item in infl_list if item not in exclude]
full_nodes_list = nodes_list + scheme.input_labels()
#print('Total elements:', scheme.elements(), ' Used wires: ', len(nodes_list))
return nodes_list
def form_dut(scheme, dut, etalon, patches):
# Forming design under test
inp_order = dut.__inputs__
dut = copy.deepcopy(scheme)
tgts = sorted(patches)
for tgt in tgts:
if patches[tgt] == None:
dut.__elements__[tgt] = ('GND', [])
else:
dut = u.patch_circuit(dut, patches[tgt][0])
dut.__inputs__ = inp_order
etalon.__inputs__ = inp_order
return dut, etalon
def simulate_all_nodes(scheme, capacity, stimulus):
"""
function to simulate all nodes
:param scheme: scheme under test in scheme_alt format
:param capacity: number of bits for simultaneous modeling
:return: returns list of tuples [(node_name, num), ...] num - decimal reaction on stimulus
"""
if capacity == 0:
return 0
init_outs = copy.deepcopy(scheme.__outputs__)
scheme.__outputs__ = scheme.element_labels()
reaction = scheme.process(stimulus, [], capacity)
res = {}
for i in range(len(reaction)):
res[scheme.__outputs__[i]] = reaction[i]
for i in range(scheme.inputs()):
res[scheme.__inputs__[i]] = stimulus[i]
scheme.__outputs__ = copy.deepcopy(init_outs)
return res
def get_target_vector(target_array, capacity, pos):
target_vector = '' # Basic vector for target
for i in range(capacity):
# forming target_vector from target_array
tmp = []
for tgt_variant in target_array[i]:
tmp.append(tgt_variant[pos])
if 0 in tmp:
if 1 in tmp:
target_vector += 'x'
else:
target_vector += '0'
elif 1 in tmp:
target_vector += '1'
else:
print('ERROR: UNABLE TO BUILD TARGET VECTOR')
exit()
return target_vector
# We just replace 'x' on '0'
def prepare_reduced_arrays_v6(signatures, target_vector):
'''
:param signatures: dict of big python integers
:param target_vector: vector of following form '001010xx0x1'
:return: return signatures with bits removed in positions where target == 'x'
'''
# Invert target vector
target_vector = target_vector[::-1]
target_vector = target_vector.replace('x', '0')
capacity = len(target_vector)
new_cap_checker = capacity - target_vector.count('x')
# Recalculate target
target = 0
new_capacity = 0
for i in range(capacity):
if target_vector[i] == 'x':
continue
cap2 = (1 << new_capacity)
if target_vector[i] == '1':
target |= cap2
new_capacity += 1
if new_capacity != new_cap_checker:
print('Some error here!')
exit()
return copy.deepcopy(signatures), target, new_capacity
# Change only inputs, then simulate from begining
def prepare_reduced_arrays_v7(scheme, signatures, target_vector):
'''
:param signatures: dict of big python integers
:param target_vector: vector of following form '001010xx0x1'
:return: return signatures with bits removed in positions where target == 'x'
'''
tv = target_vector[::-1]
capacity = len(tv)
reply = dict()
for el in signatures:
reply[el] = 0
new_cap_checker = capacity - target_vector.count('x')
stimulus = [0]*len(scheme.__inputs__)
# Recreate target and remove x
new_capacity = 0
target = 0
for i in range(capacity):
if tv[i] == 'x':
continue
cap2 = (1 << new_capacity)
if tv[i] == '1':
target |= cap2
for j in range(len(scheme.__inputs__)):
el = scheme.__inputs__[j]
current_bit = (signatures[el] >> i) & 1
if current_bit:
stimulus[j] |= cap2
new_capacity += 1
if new_capacity != new_cap_checker:
print('Some error here!')
exit()
reply = simulate_all_nodes(scheme, new_capacity, stimulus)
return reply, target, new_capacity
def reduce_target_array(scheme, signatures, target_vector):
if 'x' not in target_vector:
reply, target, capacity = prepare_reduced_arrays_v6(signatures, target_vector)
else:
reply, target, capacity = prepare_reduced_arrays_v7(scheme, signatures, target_vector)
return reply, target, capacity
def simulate_miter(capacity, input_order):
ostype = "win32"
if platform == "linux":
ostype = "linux"
path = os.path.join("equiv_check", ostype, 'miter.v')
inp, sch = rw.read_AIG_verilog(path)
#print('Number of conflicted inputs in miter: {} out of {}'.format(len(inp), len(input_order)))
print('...')
sch.__inputs__ = input_order
if 2**len(inp) < capacity:
print('Generate all possible variants in mitter...')
(stimulus, capacity) = exhaustive_stimulus(len(inp))
else:
stimulus = random_stimulus(len(inp), capacity)
inp_vector = []
matrix = []
for i in sch.__inputs__:
if i in inp:
stimul = stimulus.pop()
inp_vector.append(stimul)
vec = "{0:b}".format(stimul)
vec = '0' * (capacity - len(vec)) + vec
matrix.append(vec)
else:
inp_vector.append(0)
matrix.append(0)
if sch.__elements__ == {}:
return []
miter = sch.process(inp_vector, [], capacity)
miter = "{0:b}".format(miter[0])
miter = '0' * (capacity - len(miter)) + miter
ind = 0
result = []
for i in miter:
if i == '1':
stimul = ''
for vec in matrix:
if vec == 0:
stimul += '0'
else:
stimul += vec[ind]
result.append(stimul)
ind += 1
return result
def convert_stimuli(sim):
stimuli = [0]*len(sim[0])
for i in range(len(sim)):
s = sim[i]
sum = 1 << i
for j in range(len(stimuli)):
if s[j] == '1':
stimuli[j] += sum
return stimuli, len(sim)