-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInstructionEncoder.py
More file actions
340 lines (306 loc) · 7.63 KB
/
InstructionEncoder.py
File metadata and controls
340 lines (306 loc) · 7.63 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
PC =[0,0,0,1]
IR =[1,1,1,1]
MAR =[1,1,0,0]
AC =[1,0,1,1]
R1 =[0,1,0,0]
R2 =[0,1,0,1]
R3 =[0,1,1,0]
SOR =[1,0,0,1]
DSTR =[1,0,1,0]
COUN =[1,1,1,0]
NA =[0,0,0,0]
count = 0
M1 = 3 #First Memory Location of Orginal Image
M2 = 4 #First Memory Location of Destination Image
M3 = 5 #Last Memory Location of Original Image
M4 = 6 #Memory Location containing width of original image
f = open("Inst_mem_test.mem", "w+")
def Print(code):
global f
#print (len(code))
global count
#print (count, end=" ")
print (" memory[",count,"] <=32'b",end='')
#f.write(" memory[")
#f.write(str(count))
#f.write("] = 32'b")
for i in code:
print (i, end="")
f.write(str(i))
print ()
#f.write(';\n')
f.write('\n')
count+=1
return
def END():
code = [0]*32
code[0:5] = [0,0,0,1,1]
Print (code)
return
def NOP():
#global PC,IR,MAR,AC,R1,R2,R3,SOR,DSTR,COUN
code = [0]*32
code[0:4] = [0,0,0,1]
Print (code)
return
def RSET(C=0,D=0,S=0,M=0,A=0):
#global PC,IR,MAR,AC,R1,R2,R3,SOR,DSTR,COUN
code = [0]*32
code[0:4] = [0,0,1,0]
code[5:10]=[C,D,S,M,A]
Print (code)
return
def LOAD(J=0, A=0):
#global PC,IR,MAR,AC,R1,R2,R3,SOR,DSTR,COUN
code = [0]*32
code[0:4] = [0,0,1,1]
if J ==0:
Print (code)
return
else:
code[4]=1
A=[int(x) for x in str(bin(A)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def STOR(J=0, A=0):
#global PC,IR,MAR,AC,R1,R2,R3,SOR,DSTR,COUN
code = [0]*32
code[0:4] = [0,1,0,0]
if J ==0:
Print (code)
return
else:
code[4]=1
A=[int(x) for x in str(bin(A)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def MVAR(J=0):
#global PC,IR,MAR,AC,R1,R2,R3,SOR,DSTR,COUN
code = [0]*32
code[0:4] = [0,1,0,1]
code[4]=J
Print (code)
return
def MVAO(Reg):
code = [0]*32
code[0:4] = [0,1,1,0]
code[5:9] = Reg
Print (code)
return
def MVAI(Reg):
code = [0]*32
code[0:4] = [0,1,1,1]
code[4]=int(not(Reg[0]))
code[5:9] = Reg
Print (code)
return
def INC(C=0,D=0,S=0,M=0):
code = [0]*32
code[0:4] = [1,0,0,0]
code[5:9]=[C,D,S,M]
Print (code)
return
def JUMP(N=0,Z=0,Reg1=NA,Reg2=NA,T=0):
code = [0]*32
code[0:4] = [1,1,0,0]
code[5:9] = Reg1
code[9:13] = Reg2
code[13] = N
code[14] = Z
A=[int(x) for x in str(bin(T)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def ADD(Reg1, Reg2 = NA,J=1, K=0):
code = [0]*32
code[0:4] = [1,0,0,1]
code[4]=J
code[5:9] = Reg1
code[9:13]=Reg2
A=[int(x) for x in str(bin(K)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def SUB(Reg1, Reg2 = NA, J=1, K=0):
code = [0]*32
code[0:4] = [1,1,1,1]
code[4]=J
code[5:9] = Reg1
code[9:13]=Reg2
A=[int(x) for x in str(bin(K)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def MUL(Reg1, Reg2 = NA, J=1, K=0):
code = [0]*32
code[0:4] = [1,1,0,1]
code[4]=J
code[5:9] = Reg1
code[9:13]=Reg2
A=[int(x) for x in str(bin(K)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def DIV(Reg1, Reg2 = NA, J=1, K=0):
code = [0]*32
code[0:4] = [1,1,1,0]
code[4]=J
code[5:9] = Reg1
code[9:13]=Reg2
A=[int(x) for x in str(bin(K)[2:])]
for i in range (len(A)):
code[-i-1] = A[-i-1]
Print (code)
return
def SFTR():
code = [0]*32
code[0:4] = [1,0,1,0]
Print (code)
return
def SFTL():
code = [0]*32
code[0:4] = [1,0,1,1]
Print (code)
return
print ("Algorithm in Binary")
RSET(1,1,1,1,1) #0
LOAD(J=1, A = M1) #1
MVAO(SOR) #2
LOAD(J=1,A=M2) #3
MVAO(DSTR) #4
LOAD(J=1,A=M3) #5
MVAO(R2) #6
LOAD(J=1,A=M4) #7
SUB (AC, J=0, K = 2)#8 Changed to sort out line length issue
MVAO (R3) #9 Changed to sort out line length issue
MVAR (0) #10
LOAD() #11
MVAO (R1) #12
INC (M=1) #13
LOAD() #14
SFTL () #15
ADD (AC,R1) #16
MVAO (R1) #17
INC (M=1) #18
LOAD() #19
ADD (AC,R1) #20
SFTR() #21
SFTR() #22
JUMP(Z=1, N=0, Reg1 =MAR, Reg2 = R2, T = 33)#23
MVAR (1) #24
STOR () #25
INC (S=1,D=1,C=1) #26 Changed to sort out line length issue
JUMP(Z=1, N=0, Reg1 =COUN, Reg2 = R3, T = 29) #27 Changed to sort out line length issue
JUMP (Z=0, N=0, T = 10)#28 Changed to updated instruction number
#If reached end of line, should run, at this point, C = L-2, and SOR is pointing at the pixel before the last on that line
INC (S=1) #29
INC (S=1) #30
RSET(C=1) #31
JUMP(T = 10) #32
#If end of image is reached, should run
MVAR (1) #33
STOR() #34
MVAI (DSTR) #35
MVAO (R2) #36
LOAD (J=1, A = M2) #37
MVAO (SOR) #38
LOAD (J=1, A = M1) #39
MVAO (DSTR) #40
JUMP (Z=0, N=0, T = 42)#41
#Begin Vertical Filtering
MVAR (0) #42
LOAD() #43
MVAO (R1) #44
ADD (MAR,R3) #45
LOAD() #46
SFTL () #47
ADD (AC,R1) #48
MVAO (R1) #49
ADD (MAR, R3) #50
LOAD() #51
ADD (AC,R1) #52
SFTR() #53
SFTR() #54
JUMP (Z=1, N=0, Reg1 = MAR, Reg2 = R2, T = 60)#55
MVAR (1) #56
STOR () #57
INC (S=1, D=1) #58
JUMP (Z=0, N=0, T = 42)#59
#Runs when vertical filtering finishes
MVAR (1) #60
STOR() #61
MVAI (DSTR) #62
MVAO (R2) #63
LOAD (J=1, A = M1) #64
MVAO (SOR) #65
LOAD (J=1, A = M2) #66
MVAO (DSTR) #67
RSET(C=1) #68
JUMP (Z=0, N=0, T = 70)#69 Changed to updated instruction numbers
#Downsampling begins
MVAR(0) #70
LOAD() #71
MVAR(1) #72
STOR() #73
INC (S = 1, D = 1, C = 1)#74
JUMP (Z = 1, N = 0, Reg2 = R2, Reg1 = SOR, T = 86)#75
INC (S= 1, C = 1)#76
#MVAI(COUN)
JUMP (Z = 1, N = 0, Reg2 = R2, Reg1 = SOR, T = 86)#77
#JUMP (Z = 1, N = 0, Reg2 = R3, Reg1 = AC, T= 76)
JUMP (Z = 1, N = 0, Reg2 = R3, Reg1 = COUN, T = 80) #78Updated to reflect increaded capabilites of instructions
JUMP (Z=0, N=0, T = 69) #79
ADD (SOR, R3) #80
RSET (C=1) #81
#-------------------Test2---------------------------
SUB(J = 0, K = 1, Reg1 = SOR)#82
JUMP (Z = 1, N = 0, Reg2 = R2, Reg1 = SOR, T = 86)#83
INC(S = 1)#84
#-------------------Test2End-----------------------
#-----------------------------Test1-------------------------
'''
Fail
MVAI(SOR) #83
MVAO(R1) #84
MVAI(R3) #85
JUMP (Z = 1, N = 1, Reg2 = R1, Reg1 = AC, T = 88)#86
'''
#----------------------------EndTest1-----------------------
JUMP (Z = 0, N = 0, T = 70)#85
END() #86
'''
#Testing Algorithm
RSET(1,1,1,1,1) #0
RSET(1,1,1,1,1) #1
INC(M=1) #2
INC(M=1) #3
INC(M=1) #4
MVAI(MAR) #5 AC=3
MVAO(MAR) #6 MAR=3
INC(M=1) #7 MAR=4
MVAI(MAR) #8 AC=4
RSET(M=1) #9 MAR=0
INC(M=1) #10 MAR = 1
STOR() #11Stores 4 in dram[1]
INC(M=1) #12 MAR = 2
STOR() #13 Stores 4 in dram [2]
MVAO(R3) #14 R3 = 4
RSET(M=1) #15 MAR=0
MVAI(MAR) #16 AC=0
INC(M=1) #17 MAR=1
LOAD() #18 AC=4
JUMP(Z=1, Reg1 = AC, Reg2 = R3, T=0)#19
ADD(AC, R3) #20
STOR() #21
JUMP(T = 1) #22
END() #23
'''
f.close()