Commit 4e04d0665216f704e395c8e53c8f70a7a093a39b

Authored by Brice Colombier
1 parent 26e706e8cc
Exists in master

Locking c432 works

Showing 3 changed files with 223 additions and 227 deletions

AW_decoder/AW_decoder.vhd View file @ 4e04d06
... ... @@ -4,38 +4,29 @@
4 4 ENTITY AW_decoder IS
5 5  
6 6 PORT (
7   - formatted_AW : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
8   - AW : OUT STD_LOGIC_VECTOR(7 DOWNTO 0));
  7 + formatted_AW : IN STD_LOGIC_VECTOR(0 TO 3);
  8 + AW : OUT STD_LOGIC_VECTOR(0 TO 7));
9 9  
10 10 END ENTITY AW_decoder;
11 11  
12 12 ARCHITECTURE rtl OF AW_decoder IS
13 13  
14   - SIGNAL locking_0 : STD_LOGIC;
15   - SIGNAL locking_1 : STD_LOGIC;
16   -
17 14 BEGIN
18 15  
19   - -- Formatted AW: 0101
20   - -- AW: 0000 1100
  16 + -- Formatted AW: 1011
  17 + -- AW: 10100011
21 18  
22   - -- Lock AND
23   - locking_1 <= formatted_AW(2);
24   -
25   - -- Lock OR
26   - locking_0 <= formatted_AW(3);
27   - AW(4) <= locking_0;
28   - AW(5) <= locking_0;
29   - AW(6) <= locking_0;
30   - AW(7) <= locking_0;
31   -
32   - -- Mask ANDs
33   - AW(2) <= formatted_AW(0);
34   - AW(3) <= formatted_AW(0);
35   -
36 19 -- Mask ORs
37 20 AW(1) <= formatted_AW(1);
38   - AW(0) <= formatted_AW(1);
  21 + AW(3) <= formatted_AW(1);
  22 + AW(4) <= formatted_AW(1);
  23 + AW(5) <= formatted_AW(1);
  24 +
  25 + -- Mask ANDs
  26 + AW(0) <= formatted_AW(0);
  27 + AW(2) <= formatted_AW(2);
  28 + AW(6) <= formatted_AW(3);
  29 + AW(7) <= formatted_AW(0);
39 30  
40 31 END ARCHITECTURE rtl;
AW_decoder/gen_AW_decoder.py View file @ 4e04d06
... ... @@ -2,118 +2,14 @@
2 2 from operator import itemgetter
3 3 import math
4 4  
5   -def gen_assoc_mask_ANDs (fan_in_ANDs, pos_1s_output, pos_1s_input):
6   - assoc_mask_ANDs = []
7   - if fan_in_ANDs > 1:
8   - remain = 0
9   - while pos_1s_output:
10   - current_fan_in = int(math.ceil(fan_in_ANDs))
11   - remain+=current_fan_in - fan_in_ANDs
12   - if remain > 1:
13   - remain -= 1
14   - assoc_mask_ANDs.append((pos_1s_input[-current_fan_in+1:], pos_1s_output.pop()))
15   - del pos_1s_input[-current_fan_in+1:]
16   - else:
17   - assoc_mask_ANDs.append((pos_1s_input[-current_fan_in:], pos_1s_output.pop()))
18   - del pos_1s_input[-current_fan_in:]
19   - elif fan_in_ANDs == 1:
20   - assoc_mask_ANDs = list(zip(pos_1s_input, pos_1s_output))
21   - assoc_mask_ANDs = [(x[0], x[1]) for x in assoc_mask_ANDs]
22   - elif fan_in_ANDs < 1 and fan_in_ANDs != 0:
23   - remain = 0
24   - fan_out_ANDs = 1/fan_in_ANDs
25   - while pos_1s_input:
26   - current_fan_out = int(math.ceil(fan_out_ANDs))
27   - remain+=current_fan_out - fan_out_ANDs
28   - if remain > 1:
29   - remain -= 1
30   - assoc_mask_ANDs.append((pos_1s_input.pop(), pos_1s_output[-current_fan_out+1:]))
31   - del pos_1s_output[-current_fan_out+1:]
32   - else:
33   - assoc_mask_ANDs.append((pos_1s_input.pop(), pos_1s_output[-current_fan_out:]))
34   - del pos_1s_output[-current_fan_out:]
35   - else:
36   - raise ValueError("Fan-in zero")
37   - assoc_mask_ANDs = [(x[0], x[1][0]) if (type(x[1]) == list and len(x[1]) == 1) else x for x in assoc_mask_ANDs]
38   - assoc_mask_ANDs = [(x[0][0], x[1]) if (type(x[0]) == list and len(x[0]) == 1) else x for x in assoc_mask_ANDs]
39   - return assoc_mask_ANDs
40   -
41   -def gen_assoc_mask_ORs (fan_in_ORs, pos_0s_output, pos_0s_input):
42   - assoc_mask_ORs = []
43   - if fan_in_ORs > 1:
44   - remain = 0
45   - while pos_0s_output:
46   - current_fan_in = int(math.ceil(fan_in_ORs))
47   - remain+=current_fan_in - fan_in_ORs
48   - if remain > 1:
49   - remain -= 1
50   - assoc_mask_ORs.append((pos_0s_input[-current_fan_in+1:], pos_0s_output.pop()))
51   - del pos_0s_input[-current_fan_in+1:]
52   - else:
53   - assoc_mask_ORs.append((pos_0s_input[-current_fan_in:], pos_0s_output.pop()))
54   - del pos_0s_input[-current_fan_in:]
55   - elif fan_in_ORs == 1:
56   - assoc_mask_ORs = list(zip(pos_0s_input, pos_0s_output))
57   - assoc_mask_ORs = [(x[0], x[1]) for x in assoc_mask_ORs]
58   - elif fan_in_ORs < 1 and fan_in_ORs != 0:
59   - remain = 0
60   - fan_out_ORs = 1/fan_in_ORs
61   - while pos_0s_input:
62   - current_fan_out = int(math.ceil(fan_out_ORs))
63   - remain+=current_fan_out - fan_out_ORs
64   - if remain > 1:
65   - remain -= 1
66   - assoc_mask_ORs.append((pos_0s_input.pop(), pos_0s_output[-current_fan_out+1:]))
67   - del pos_0s_output[-current_fan_out+1:]
68   - else:
69   - assoc_mask_ORs.append((pos_0s_input.pop(), pos_0s_output[-current_fan_out:]))
70   - del pos_0s_output[-current_fan_out:]
71   - else:
72   - raise ValueError("Fan-in zero")
73   - assoc_mask_ORs = [(x[0], x[1][0]) if (type(x[1]) == list and len(x[1]) == 1) else x for x in assoc_mask_ORs]
74   - assoc_mask_ORs = [(x[0][0], x[1]) if (type(x[0]) == list and len(x[0]) == 1) else x for x in assoc_mask_ORs]
75   - return assoc_mask_ORs
76   -
77   -def write_masking_part_to_file(vhd_file, assoc_mask_ANDs, assoc_mask_ORs):
78   - vhd_file.write("\n -- Mask ANDs\n")
79   - for (inputs, outputs) in assoc_mask_ANDs:
80   - if type(inputs) == int and type(outputs) == int:
81   - vhd_file.write(" AW("+str(outputs)+") <= formatted_AW("+str(inputs)+");\n")
82   - elif type(inputs) == int and type(outputs) == list:
83   - for output in outputs:
84   - vhd_file.write(" AW("+str(output)+") <= formatted_AW("+str(inputs)+");\n")
85   - elif type(inputs) == list and type(outputs) == int:
86   - vhd_file.write(" AW("+str(outputs)+") <= "+" AND ".join(["formatted_AW("+str(i)+")" for i in inputs])+";\n")
87   - vhd_file.write("\n -- Mask ORs\n")
88   - for (inputs, outputs) in assoc_mask_ORs:
89   - if type(inputs) == int and type(outputs) == int:
90   - vhd_file.write(" AW("+str(outputs)+") <= formatted_AW("+str(inputs)+");\n")
91   - elif type(inputs) == int and type(outputs) == list:
92   - for output in outputs:
93   - vhd_file.write(" AW("+str(output)+") <= formatted_AW("+str(inputs)+");\n")
94   - elif type(inputs) == list and type(outputs) == int:
95   - vhd_file.write(" AW("+str(outputs)+") <= "+" OR ".join(["formatted_AW("+str(i)+")" for i in inputs])+";\n")
96   -
97   -def write_locking_part_to_file(vhd_file, assoc_lock_ANDs, assoc_lock_ORs):
98   - vhd_file.write("\n -- Lock AND\n")
99   - for (inputs, outputs) in assoc_lock_ANDs:
100   - vhd_file.write(" locking_1 <= "+" AND ".join(["formatted_AW("+str(i)+")" for i in inputs])+";\n")
101   - for output in outputs:
102   - vhd_file.write(" AW("+str(output)+") <= locking_1;\n")
103   - vhd_file.write("\n -- Lock OR\n")
104   - for (inputs, outputs) in assoc_lock_ORs:
105   - vhd_file.write(" locking_0 <= "+" OR ".join(["formatted_AW("+str(i)+")" for i in inputs])+";\n")
106   - for output in outputs:
107   - vhd_file.write(" AW("+str(output)+") <= locking_0;\n")
108   -
109 5 def write_header(vhd_file, locking, width_formatted_AW, len_AW):
110 6 vhd_file.write("LIBRARY ieee;\n")
111 7 vhd_file.write("USE ieee.std_logic_1164.ALL;\n\n")
112 8 # Entity
113 9 vhd_file.write("ENTITY AW_decoder IS\n\n")
114 10 vhd_file.write(" PORT (\n")
115   - vhd_file.write(" formatted_AW : IN STD_LOGIC_VECTOR("+str(width_formatted_AW-1)+" DOWNTO 0);\n")
116   - vhd_file.write(" AW : OUT STD_LOGIC_VECTOR("+str(len_AW-1)+" DOWNTO 0));\n\n")
  11 + vhd_file.write(" formatted_AW : IN STD_LOGIC_VECTOR(0 TO "+str(width_formatted_AW-1)+");\n")
  12 + vhd_file.write(" AW : OUT STD_LOGIC_VECTOR(0 TO "+str(len_AW-1)+"));\n\n")
117 13 vhd_file.write("END ENTITY AW_decoder;\n\n")
118 14 # Architecture
119 15 vhd_file.write("ARCHITECTURE rtl OF AW_decoder IS\n\n")
... ... @@ -123,6 +19,94 @@
123 19 vhd_file.write("BEGIN\n\n")
124 20  
125 21  
  22 +def match_mask(pos_0s_formatted_masking_word, pos_1s_formatted_masking_word, pos_0s_masking_word, pos_1s_masking_word):
  23 + match_0s_mask = {}
  24 + match_1s_mask = {}
  25 + if len(pos_0s_formatted_masking_word) > len(pos_0s_masking_word):
  26 + # There are more 0s available than needed, they must be ORed
  27 + rd.shuffle(pos_0s_formatted_masking_word)
  28 + ratio = math.ceil(float(len(pos_0s_formatted_masking_word))/len(pos_0s_masking_word))
  29 + # How many times more ?
  30 + pos_0s_masking_word = int(ratio)*pos_0s_masking_word
  31 + # Round up the ratio and duplicate the list accordingly
  32 + assocs = [[a, b] for a, b in zip(pos_0s_formatted_masking_word, pos_0s_masking_word)]
  33 + # Zip to make associations
  34 + for i in set(pos_0s_masking_word):
  35 + match_0s_mask[i] = [pos_for for [pos_for, pos] in assocs if pos == i]
  36 + # Group associations to the same position in the formatted word
  37 + elif len(pos_0s_formatted_masking_word) < len(pos_0s_masking_word):
  38 + # There are not enough 0s available, they must be shared
  39 + rd.shuffle(pos_0s_masking_word)
  40 + ratio = math.ceil(float(len(pos_0s_masking_word))/len(pos_0s_formatted_masking_word))
  41 + pos_0s_formatted_masking_word = int(ratio)*pos_0s_formatted_masking_word
  42 + assocs = [[a, b] for a, b in zip(pos_0s_formatted_masking_word, pos_0s_masking_word)]
  43 + for i in set(pos_0s_masking_word):
  44 + match_0s_mask[i] = [pos_for for [pos_for, pos] in assocs if pos == i]
  45 + elif len(pos_0s_formatted_masking_word) == len(pos_0s_masking_word):
  46 + # Perfect match
  47 + for a, b in zip(pos_0s_formatted_masking_word, pos_0s_masking_word):
  48 + match_0s_mask[b] = [a]
  49 + if len(pos_1s_formatted_masking_word) > len(pos_1s_masking_word):
  50 + # There are more 1s available than needed, they must be ORed
  51 + rd.shuffle(pos_1s_formatted_masking_word)
  52 + ratio = math.ceil(float(len(pos_1s_formatted_masking_word))/len(pos_1s_masking_word))
  53 + pos_1s_masking_word = int(ratio)*pos_1s_masking_word
  54 + assocs = [[a, b] for a, b in zip(pos_1s_formatted_masking_word, pos_1s_masking_word)]
  55 + for i in set(pos_1s_masking_word):
  56 + match_1s_mask[i] = [pos_for for [pos_for, pos] in assocs if pos == i]
  57 + elif len(pos_1s_formatted_masking_word) < len(pos_1s_masking_word):
  58 + # There are not enough 1s available, they must be shared
  59 + rd.shuffle(pos_1s_masking_word)
  60 + ratio = math.ceil(float(len(pos_1s_masking_word))/len(pos_1s_formatted_masking_word))
  61 + pos_1s_formatted_masking_word = int(ratio)*pos_1s_formatted_masking_word
  62 + assocs = [[a, b] for a, b in zip(pos_1s_formatted_masking_word, pos_1s_masking_word)]
  63 + for i in set(pos_1s_masking_word):
  64 + match_1s_mask[i] = [pos_for for [pos_for, pos] in assocs if pos == i]
  65 + elif len(pos_1s_formatted_masking_word) == len(pos_1s_masking_word):
  66 + # Perfect match
  67 + for a, b in zip(pos_1s_formatted_masking_word, pos_1s_masking_word):
  68 + match_1s_mask[b] = [a]
  69 + # Os must be ORed
  70 + # 1s must be ANDed
  71 + for i in match_0s_mask:
  72 + print i
  73 + return match_0s_mask, match_1s_mask
  74 +
  75 +def write_masking_part_to_file(vhd_file,
  76 + match_0s_mask,
  77 + match_1s_mask):
  78 + print "Masking part"
  79 + vhd_file.write("\n -- Mask ORs\n")
  80 + for AW_index in match_0s_mask:
  81 + if len(match_0s_mask[AW_index]) > 1:
  82 + # Must be ORed
  83 + vhd_file.write(" AW("+str(AW_index)+") <= "+" OR ".join(["formatted_AW("+str(i)+")" for i in match_0s_mask[AW_index]])+";\n")
  84 + else:
  85 + vhd_file.write(" AW("+str(AW_index)+") <= formatted_AW("+str(match_0s_mask[AW_index][0])+");\n")
  86 + vhd_file.write("\n -- Mask ANDs\n")
  87 + for AW_index in match_1s_mask:
  88 + if len(match_1s_mask[AW_index]) > 1:
  89 + # Must be ORed
  90 + vhd_file.write(" AW("+str(AW_index)+") <= "+" AND ".join(["formatted_AW("+str(i)+")" for i in match_1s_mask[AW_index]])+";\n")
  91 + else:
  92 + vhd_file.write(" AW("+str(AW_index)+") <= formatted_AW("+str(match_1s_mask[AW_index][0])+");\n")
  93 +
  94 +def write_locking_part_to_file(vhd_file,
  95 + pos_0s_formatted_locking_word, pos_0s_locking_word,
  96 + pos_1s_formatted_locking_word, pos_1s_locking_word):
  97 + vhd_file.write("\n -- Lock AND\n")
  98 + # locking_1 = 1 if all pos_1s_formatted_locking_word = 1
  99 + vhd_file.write(" locking_1 <= "+" AND ".join(["formatted_AW("+str(i)+")" for i in pos_1s_formatted_locking_word])+";\n")
  100 + for AW_index in pos_1s_locking_word:
  101 + # Distribute locking_1 to all AW outputs
  102 + vhd_file.write(" AW("+str(AW_index)+") <= locking_1;\n")
  103 + vhd_file.write("\n -- Lock OR\n")
  104 + # locking_0 = 0 if all pos_0s_formatted_locking_word = 0
  105 + vhd_file.write(" locking_0 <= "+" OR ".join(["formatted_AW("+str(i)+")" for i in pos_0s_formatted_locking_word])+";\n")
  106 + for AW_index in pos_0s_locking_word:
  107 + # Distribute locking_0 to all AW outputs
  108 + vhd_file.write(" AW("+str(AW_index)+") <= locking_0;\n")
  109 +
126 110 def gen_AW_decoder (AW, width_formatted_AW, locking=True, masking=True, path = "./", formatted_AW=""):
127 111 """Generates a decoder for the Activation Word
128 112  
129 113  
130 114  
131 115  
132 116  
133 117  
134 118  
135 119  
136 120  
137 121  
138 122  
... ... @@ -130,113 +114,131 @@
130 114 width_formatted_AW: width of the formatted AW
131 115 """
132 116  
133   - unlocking_word = ""
134   - unmasking_word = ""
  117 + path = "./user_space/"
  118 +
  119 + locking_word = ""
  120 + masking_word = ""
135 121 if locking:
136 122 if masking:
137   - try:
138   - unlocking_word, unmasking_word = AW.split(" ")
139   - print unlocking_word
140   - print unmasking_word
141   - except ValueError:
142   - raise ValueError("Incorrect AW provided, must be \"AW_lock AW_mask\"")
  123 + # Read locking word and masking word from AW and invert them to get activation words
  124 + locking_word = ''.join('1' if x == '0' else '0' for x in AW.strip().split(" ")[0])
  125 + masking_word = ''.join('1' if x == '0' else '0' for x in AW.strip().split(" ")[1])
143 126 else:
144   - if len(AW.split()) == 1:
145   - unlocking_word = AW
146   - else:
147   - raise ValueError("AW provided comes in multiple parts, should have only one")
148   - elif masking:
149   - if len(AW.split()) == 1:
150   - unmasking_word = AW
151   - else:
152   - raise ValueError("AW provided comes in multiple parts, should have only one")
  127 + locking_word = ''.join('1' if x == '0' else '0' for x in AW.strip())
153 128 else:
154   - raise ValueError("At least one of locking or masking must be set to True")
  129 + if masking:
  130 + masking_word = ''.join('1' if x == '0' else '0' for x in AW.strip())
155 131  
156   - with open(path+"AW_decoder.vhd", "w") as vhd_file:
157   - # Header
158   -
159   - if (masking and not locking) or (locking and not masking):
  132 + AW = locking_word+masking_word
  133 + if locking and masking:
  134 + pos_0s_formatted_locking_word = []
  135 + pos_0s_formatted_masking_word = []
  136 + pos_1s_formatted_locking_word = []
  137 + pos_1s_formatted_masking_word = []
  138 +
  139 + pos_0s_locking_word = [i[0] for i in enumerate(locking_word) if i[1] == "0"]
  140 + pos_1s_locking_word = [i[0] for i in enumerate(locking_word) if i[1] == "1"]
  141 + pos_0s_masking_word = [i[0] for i in enumerate(masking_word, start = len(locking_word)) if i[1] == "0"]
  142 + pos_1s_masking_word = [i[0] for i in enumerate(masking_word, start = len(locking_word)) if i[1] == "1"]
  143 + while not(pos_0s_formatted_locking_word and
  144 + pos_0s_formatted_masking_word and
  145 + pos_1s_formatted_locking_word and
  146 + pos_1s_formatted_masking_word):
  147 + # Generate random formatted AW with 0s and 1s in both halfs
  148 + formatted_AW = ''.join([rd.choice(["0", "1"]) for n in xrange(width_formatted_AW)])
  149 + formatted_locking_word = formatted_AW[:len(formatted_AW)/2]
  150 + formatted_masking_word = formatted_AW[len(formatted_AW)/2:]
  151 + pos_0s_formatted_locking_word = [i[0] for i in enumerate(formatted_locking_word) if i[1] == "0"]
  152 + pos_1s_formatted_locking_word = [i[0] for i in enumerate(formatted_locking_word) if i[1] == "1"]
  153 + pos_0s_formatted_masking_word = [i[0] for i in enumerate(formatted_masking_word, start = len(formatted_locking_word)) if i[1] == "0"]
  154 + pos_1s_formatted_masking_word = [i[0] for i in enumerate(formatted_masking_word, start = len(formatted_locking_word)) if i[1] == "1"]
  155 + print "Formatted AW", formatted_AW
  156 + print AW
  157 + print "pos_0s_locking_word", pos_0s_locking_word
  158 + print "pos_0s_formatted_locking_word", pos_0s_formatted_locking_word
  159 + print "pos_1s_locking_word", pos_1s_locking_word
  160 + print "pos_1s_formatted_locking_word", pos_1s_formatted_locking_word
  161 + print "pos_0s_formatted_masking_word", pos_0s_formatted_masking_word
  162 + print "pos_0s_masking_word", pos_0s_masking_word
  163 + print "pos_1s_formatted_masking_word", pos_1s_formatted_masking_word
  164 + print "pos_1s_masking_word", pos_1s_masking_word
  165 + match_0s_mask, match_1s_mask = match_mask(pos_0s_formatted_masking_word, pos_1s_formatted_masking_word, pos_0s_masking_word, pos_1s_masking_word)
  166 + print match_0s_mask, match_1s_mask
  167 + with open(path+"AW_decoder.vhd", "w") as vhd_file:
160 168 write_header(vhd_file, locking, width_formatted_AW, len(AW))
161   - if not formatted_AW:
162   - formatted_AW = ''.join([str(int(2*rd.random())) for i in xrange(width_formatted_AW)])
163   - elif len(formatted_AW) != width_formatted_AW:
164   - raise ValueError("Length of provided formatted AW does not match width_formatted_AW")
165 169 vhd_file.write(" -- Formatted AW: "+formatted_AW+"\n")
166 170 vhd_file.write(" -- AW: "+AW+"\n")
167   -
168   - pos_0s_AW = [len(AW)-1-x for x in range(len(AW)) if AW[x] == '0']
169   - rd.shuffle(pos_0s_AW)
170   - pos_1s_AW = [len(AW)-1-x for x in range(len(AW)) if AW[x] == '1']
171   - rd.shuffle(pos_1s_AW)
172   - pos_0s_formatted_AW = [len(AW)-1-x for x in range(len(formatted_AW)) if formatted_AW[x] == '0']
173   - rd.shuffle(pos_0s_formatted_AW)
174   - pos_1s_formatted_AW = [len(AW)-1-x for x in range(len(formatted_AW)) if formatted_AW[x] == '1']
175   - rd.shuffle(pos_1s_formatted_AW)
176   - if locking and not masking:
177   - assoc_lock_ANDs = [(pos_1s_formatted_AW, pos_1s_AW)]
178   - assoc_lock_ORs = [(pos_0s_formatted_AW, pos_0s_AW)]
179   - # for (inputs, outputs) in
180   - write_locking_part_to_file(vhd_file, assoc_lock_ANDs, assoc_lock_ORs)
181   - if masking and not locking:
182   - fan_in_ANDs = float(len([x for x in formatted_AW if x == '1']))/(len([x for x in AW if x == '1']))
183   - fan_in_ORs = float(len([x for x in formatted_AW if x == '0']))/(len([x for x in AW if x == '0']))
184   - assoc_mask_ANDs = gen_assoc_mask_ANDs (fan_in_ANDs, pos_1s_AW, pos_1s_formatted_AW)
185   - assoc_mask_ORs = gen_assoc_mask_ORs (fan_in_ORs, pos_0s_AW, pos_0s_formatted_AW)
186   - write_masking_part_to_file(vhd_file, assoc_mask_ANDs, assoc_mask_ORs)
187   - if masking and locking:
188   - write_header(vhd_file, locking, width_formatted_AW, len(AW)-1)
189   - formatted_AW_locking = ""
190   - formatted_AW_masking = ""
191   - if not formatted_AW:
192   - while set(formatted_AW_locking) != set(['0', '1']):
193   - formatted_AW_locking = ''.join([str(int(2*rd.random())) for i in xrange(width_formatted_AW/2)])
194   - while set(formatted_AW_masking) != set(['0', '1']):
195   - formatted_AW_masking = ''.join([str(int(2*rd.random())) for i in xrange(width_formatted_AW/2)])
196   - formatted_AW = formatted_AW_masking + formatted_AW_locking
197   - elif len(formatted_AW) != width_formatted_AW:
198   - raise ValueError("Length of provided formatted AW does not match width_formatted_AW")
199   - else:
200   - formatted_AW_locking = formatted_AW[len(formatted_AW)/2:]
201   - formatted_AW_masking = formatted_AW[:len(formatted_AW)/2]
202   - vhd_file.write(" -- Formatted AW: "+formatted_AW_locking+formatted_AW_masking+"\n")
  171 + write_masking_part_to_file(vhd_file, match_0s_mask, match_1s_mask)
  172 + write_locking_part_to_file(vhd_file,
  173 + pos_0s_formatted_locking_word, pos_0s_locking_word,
  174 + pos_1s_formatted_locking_word, pos_1s_locking_word)
  175 + vhd_file.write("\nEND ARCHITECTURE rtl;\n")
  176 + elif locking and not masking:
  177 + pos_0s_formatted_locking_word = []
  178 + pos_1s_formatted_locking_word = []
  179 +
  180 + pos_0s_locking_word = [i[0] for i in enumerate(locking_word) if i[1] == "0"]
  181 + pos_1s_locking_word = [i[0] for i in enumerate(locking_word) if i[1] == "1"]
  182 + while not(pos_0s_formatted_locking_word and
  183 + pos_1s_formatted_locking_word):
  184 + # Generate random formatted AW with 0s and 1s in both halfs
  185 + formatted_AW = ''.join([rd.choice(["0", "1"]) for n in xrange(width_formatted_AW)])
  186 + formatted_locking_word = formatted_AW
  187 + pos_0s_formatted_locking_word = [i[0] for i in enumerate(formatted_locking_word) if i[1] == "0"]
  188 + pos_1s_formatted_locking_word = [i[0] for i in enumerate(formatted_locking_word) if i[1] == "1"]
  189 + print "Formatted AW", formatted_AW
  190 + print AW
  191 + print "pos_0s_locking_word", pos_0s_locking_word
  192 + print "pos_0s_formatted_locking_word", pos_0s_formatted_locking_word
  193 + print "pos_1s_locking_word", pos_1s_locking_word
  194 + print "pos_1s_formatted_locking_word", pos_1s_formatted_locking_word
  195 + with open(path+"AW_decoder.vhd", "w") as vhd_file:
  196 + write_header(vhd_file, locking, width_formatted_AW, len(AW))
  197 + vhd_file.write(" -- Formatted AW: "+formatted_AW+"\n")
203 198 vhd_file.write(" -- AW: "+AW+"\n")
  199 + write_locking_part_to_file(vhd_file,
  200 + pos_0s_formatted_locking_word, pos_0s_locking_word,
  201 + pos_1s_formatted_locking_word, pos_1s_locking_word)
  202 + vhd_file.write("\nEND ARCHITECTURE rtl;\n")
  203 + elif not locking and masking:
  204 + pos_0s_formatted_masking_word = []
  205 + pos_1s_formatted_masking_word = []
  206 +
  207 + pos_0s_masking_word = [i[0] for i in enumerate(masking_word) if i[1] == "0"]
  208 + pos_1s_masking_word = [i[0] for i in enumerate(masking_word) if i[1] == "1"]
  209 + while not(pos_0s_formatted_masking_word and
  210 + pos_1s_formatted_masking_word):
  211 + # Generate random formatted AW with 0s and 1s in both halfs
  212 + formatted_AW = ''.join([rd.choice(["0", "1"]) for n in xrange(width_formatted_AW)])
  213 + formatted_locking_word = formatted_AW
  214 + formatted_masking_word = formatted_AW
  215 + pos_0s_formatted_masking_word = [i[0] for i in enumerate(formatted_masking_word) if i[1] == "0"]
  216 + pos_1s_formatted_masking_word = [i[0] for i in enumerate(formatted_masking_word) if i[1] == "1"]
  217 + print "Formatted AW", formatted_AW
  218 + print AW
  219 + print "pos_0s_formatted_masking_word", pos_0s_formatted_masking_word
  220 + print "pos_0s_masking_word", pos_0s_masking_word
  221 + print "pos_1s_formatted_masking_word", pos_1s_formatted_masking_word
  222 + print "pos_1s_masking_word", pos_1s_masking_word
  223 + match_0s_mask, match_1s_mask = match_mask(pos_0s_formatted_masking_word, pos_1s_formatted_masking_word, pos_0s_masking_word, pos_1s_masking_word)
  224 + print match_0s_mask, match_1s_mask
  225 + with open(path+"AW_decoder.vhd", "w") as vhd_file:
  226 + write_header(vhd_file, locking, width_formatted_AW, len(AW))
  227 + vhd_file.write(" -- Formatted AW: "+formatted_AW+"\n")
  228 + vhd_file.write(" -- AW: "+AW+"\n")
  229 + write_masking_part_to_file(vhd_file, match_0s_mask, match_1s_mask)
  230 + vhd_file.write("\nEND ARCHITECTURE rtl;\n")
204 231  
205   - pos_0s_unlocking_word = [len(AW)-2-x for x in range(len(unlocking_word)) if unlocking_word[x] == '0']
206   - rd.shuffle(pos_0s_unlocking_word)
207   - pos_1s_unlocking_word = [len(AW)-2-x for x in range(len(unlocking_word)) if unlocking_word[x] == '1']
208   - rd.shuffle(pos_1s_unlocking_word)
209   - pos_0s_unmasking_word = [len(AW)-len(unlocking_word)-2-x for x in range(len(unmasking_word)) if unmasking_word[x] == '0']
210   - rd.shuffle(pos_0s_unmasking_word)
211   - pos_1s_unmasking_word = [len(AW)-len(unlocking_word)-2-x for x in range(len(unmasking_word)) if unmasking_word[x] == '1']
212   - rd.shuffle(pos_1s_unmasking_word)
213   - pos_0s_formatted_AW_locking = [len(formatted_AW_locking)+len(formatted_AW_masking)-1-x for x in range(len(formatted_AW_locking)) if formatted_AW_locking[x] == '0']
214   - rd.shuffle(pos_0s_formatted_AW_locking)
215   - pos_1s_formatted_AW_locking = [len(formatted_AW_locking)+len(formatted_AW_masking)-1-x for x in range(len(formatted_AW_locking)) if formatted_AW_locking[x] == '1']
216   - rd.shuffle(pos_1s_formatted_AW_locking)
217   - pos_0s_formatted_AW_masking = [len(formatted_AW_masking)-1-x for x in range(len(formatted_AW_masking)) if formatted_AW_masking[x] == '0']
218   - rd.shuffle(pos_0s_formatted_AW_masking)
219   - pos_1s_formatted_AW_masking = [len(formatted_AW_masking)-1-x for x in range(len(formatted_AW_masking)) if formatted_AW_masking[x] == '1']
220   - rd.shuffle(pos_1s_formatted_AW_masking)
  232 + return AW
221 233  
222   - # Locking
223   - assoc_lock_ANDs = [(pos_1s_formatted_AW_locking, pos_1s_unlocking_word)]
224   - assoc_lock_ORs = [(pos_0s_formatted_AW_locking, pos_0s_unlocking_word)]
225   - write_locking_part_to_file(vhd_file, assoc_lock_ANDs, assoc_lock_ORs)
226   -
227   - # Masking
228   - fan_in_ANDs = float(len([x for x in formatted_AW_masking if x == '1']))/(len([x for x in unmasking_word if x == '1']))
229   - fan_in_ORs = float(len([x for x in formatted_AW_masking if x == '0']))/(len([x for x in unmasking_word if x == '0']))
230   - assoc_mask_ANDs = gen_assoc_mask_ANDs (fan_in_ANDs, pos_1s_unmasking_word, pos_1s_formatted_AW_masking)
231   - assoc_mask_ORs = gen_assoc_mask_ORs (fan_in_ORs, pos_0s_unmasking_word, pos_0s_formatted_AW_masking)
232   - write_masking_part_to_file(vhd_file, assoc_mask_ANDs, assoc_mask_ORs)
233   -
234   - vhd_file.write("\nEND ARCHITECTURE rtl;\n")
235   - print "Formatted AW:", formatted_AW
236   - return formatted_AW
237   -
238 234 if __name__ == "__main__":
239   - # print gen_AW_decoder("00000011", 8, locking=False, masking=True)
240   - # print gen_AW_decoder("01010101", 8, locking=True, masking=False)
241   - print gen_AW_decoder("0000 1100", 4, locking=True, masking=True)
  235 + # print gen_AW_decoder("0101 1100", 16, locking=True, masking=True)
  236 + # print gen_AW_decoder("01011100", 16, locking=True, masking=False)
  237 + # print gen_AW_decoder("01011100", 16, locking=False, masking=True)
  238 + # print gen_AW_decoder("0101 1100", 8, locking=True, masking=True)
  239 + # print gen_AW_decoder("01011100", 8, locking=True, masking=False)
  240 + # print gen_AW_decoder("01011100", 8, locking=False, masking=True)
  241 + # print gen_AW_decoder("0101 1100", 4, locking=True, masking=True)
  242 + # print gen_AW_decoder("01011100", 4, locking=True, masking=False)
  243 + print gen_AW_decoder("01011100", 4, locking=False, masking=True)
... ... @@ -380,10 +380,13 @@
380 380 self.master.config(cursor="wait")
381 381 self.graph_info_label.configure(foreground="black")
382 382 self.master.update()
  383 + print self.associated_activation_word.get()[-1]
383 384 if self.associated_activation_word.get()[-1] == " ":
  385 + self.associated_activation_word.set(self.associated_activation_word.get().strip())
384 386 locking = True
385 387 masking = False
386 388 elif self.associated_activation_word.get()[0] == " ":
  389 + self.associated_activation_word.set(self.associated_activation_word.get().strip())
387 390 masking = True
388 391 locking = False
389 392 elif len(self.associated_activation_word.get().split(" ")) == 2:
... ... @@ -392,6 +395,7 @@
392 395 print locking
393 396 print masking
394 397 try:
  398 + print self.associated_activation_word.get()
395 399 self.formatted_AW = gen_AW_decoder(self.associated_activation_word.get(),
396 400 128, #128-bit responses
397 401 locking = locking,