Repository navigation
Expand file tree
/
Copy pathsimulator_plot.py
More file actions
executable file
·493 lines (355 loc) · 12.4 KB
/
Copy pathsimulator_plot.py
File metadata and controls
executable file
·493 lines (355 loc) · 12.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#
# Q4.py
# By - Shivoy Arora
# Suhani Mathur
# Shobhit Pandey
# Date - 30/06/2022
#
from matplotlib import pyplot as plt
import sys
MAX_REG = 65535
xPC = []
yPC = []
x = []
y = []
showPlt = False
memAddress = dict()
class registers:
""" Call registers to handle the value of registers and printing them """
def __init__(self):
self.regs = {"000": 0, "001": 0, "010": 0, "011": 0,
"100": 0, "101": 0, "110": 0, "111": "0"*16, "PC": 0}
def clearFlag(self):
""" Clear FLAGS """
self.regs["111"] = "0"*16
def setOverflow(self):
""" Set Overflow in FLAGS """
self.regs["111"] = "0"*12 + "1" + "000"
def setLess(self):
""" Set less than in FLAGS """
self.regs["111"] = "0"*13 + "1" + "00"
def setGreater(self):
""" Set greater than in FLAGS """
self.regs["111"] = "0"*14 + "1" + "0"
def setEqual(self):
""" Set equal to in FLAGS """
self.regs["111"] = "0"*15 + "1"
def convBin8(self, num):
""" Convert the num to 8 bit binary number
Args:
int num: number to be converted
Returns: 8 bit binary representation of num
"""
binNum = bin(num)[2:]
return "0"*(8-len(binNum)) + binNum
def convBin16(self, num):
""" Convert the num to 16 bit binary number
Args:
int num: number to be converted
Returns: 16 bit binary representation of num
"""
binNum = bin(num)[2:]
return "0"*(16-len(binNum)) + binNum
def __repr__(self) -> str:
return "{} {} {} {} {} {} {} {} {}".format(self.convBin8(self.regs["PC"]), self.convBin16(self.regs["000"]), self.convBin16(self.regs["001"]), self.convBin16(self.regs["010"]),
self.convBin16(self.regs["011"]), self.convBin16(self.regs["100"]), self.convBin16(self.regs["101"]), self.convBin16(self.regs["110"]), self.regs["111"])
class operation:
""" Class operation to handle the operations to be done """
def __init__(self, regs: registers) -> None:
# operation of each opcode
self.opcodes = {"10000": self.add, "10001": self.sub, "10010": self.mov1, "10011": self.mov2, "10100": self.ld, "10101": self.st, "10110": self.mul, "10111": self.div, "11000": self.rs, "11001": self.ls,
"11010": self.xor, "11011": self.orOps, "11100": self.andOps, "11101": self.notOps, "11110": self.cmp, "11111": self.jmp, "01100": self.jlt, "01101": self.jgt, "01111": self.je, "01010": self.hlt}
self.regsObj = regs
self.regs = regs.regs
##### Functions of the operations that has to be done #####
def add(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] + \
self.regs[command[3:6]]
# checking for overflow
if self.regs[reg3] > MAX_REG:
self.regsObj.setOverflow()
self.regs[reg3] %= (MAX_REG+1)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def sub(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation who is eight
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] - \
self.regs[command[3:6]]
# checking for overflow
if self.regs[reg3] < 0:
self.regs[reg3] = 0
self.regsObj.setOverflow()
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def mov1(self, command):
# Clearing the flag
self.regsObj.clearFlag()
self.regs[command[:3]] = int(command[3:], 2)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def mov2(self, command):
# removing filler bits
command = command[5:]
if command[:3] != "111":
self.regs[command[3:]] = self.regs[command[:3]]
else:
self.regs[command[3:]] = int(self.regs[command[:3]], 2)
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def ld(self, command):
# Clearing the flag
self.regsObj.clearFlag()
# adding to the graph
y.append(int(command[3:], 2))
x.append(xPC[-1])
if int(command[3:], 2) in memAddress:
self.regs[command[:3]] = memAddress[int(command[3:], 2)]
else:
self.regs[command[:3]] = 0
memAddress[int(command[3:], 2)] = 0
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def st(self, command):
# Clearing the flag
self.regsObj.clearFlag()
# adding to the graph
y.append(int(command[3:], 2))
x.append(xPC[-1])
memAddress[int(command[3:], 2)] = self.regs[command[:3]]
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def mul(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] * \
self.regs[command[3:6]]
# checking for overflow
if self.regs[reg3] > MAX_REG:
self.regsObj.setOverflow()
self.regs[reg3] %= (MAX_REG+1)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def div(self, command):
# removing filler bits
command = command[5:]
# Clearing the flag
self.regsObj.clearFlag()
# quotient
self.regs["000"] = self.regs[command[:3]] // self.regs[command[3:]]
# remainder
self.regs["001"] = self.regs[command[:3]] % self.regs[command[3:]]
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def rs(self, command):
# Clearing the flag
self.regsObj.clearFlag()
self.regs[command[:3]] = self.regs[command[:3]] >> int(command[3:], 2)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def ls(self, command):
# Clearing the flag
self.regsObj.clearFlag()
self.regs[command[:3]] = self.regs[command[:3]] << int(command[3:], 2)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def xor(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] ^ \
self.regs[command[3:6]]
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def orOps(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] | \
self.regs[command[3:6]]
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def andOps(self, command):
# removing filler bits
command = command[2:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
reg3 = command[6:]
self.regs[reg3] = self.regs[command[:3]] & \
self.regs[command[3:6]]
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def notOps(self, command):
# removing filler bits
command = command[5:]
# Clearing the flag
self.regsObj.clearFlag()
# Doing the operation
num = self.regsObj.convBin16(self.regs[command[:3]])
invert = ["1" if i == "0" else "0" for i in num]
self.regs[command[3:]] = int("".join(invert), 2)
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def cmp(self, command):
# removing filler bits
command = command[5:]
# Setting FLAGS
if self.regs[command[:3]] == self.regs[command[3:]]:
self.regsObj.setEqual()
elif self.regs[command[:3]] > self.regs[command[3:]]:
self.regsObj.setGreater()
elif self.regs[command[:3]] < self.regs[command[3:]]:
self.regsObj.setLess()
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] += 1
def jmp(self, command):
# removing filler bits
command = command[3:]
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
# Setting the program counter
self.regs["PC"] = int(command, 2)
def jlt(self, command):
# removing filler bits
command = command[3:]
FLAGS = self.regs["111"]
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
# Checking flag
# Setting the program counter
if FLAGS[-3] == "1":
self.regs["PC"] = int(command, 2)
else:
self.regs["PC"] += 1
def jgt(self, command):
# removing filler bits
command = command[3:]
FLAGS = self.regs["111"]
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
# Checking flag
# Setting the program counter
if FLAGS[-2] == "1":
self.regs["PC"] = int(command, 2)
else:
self.regs["PC"] += 1
def je(self, command):
# removing filler bits
command = command[3:]
FLAGS = self.regs["111"]
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
# Checking flag
# Setting the program counter
if FLAGS[-1] == "1":
self.regs["PC"] = int(command, 2)
else:
self.regs["PC"] += 1
def hlt(self, command):
global showPlt
global lines
# Clearing the flag
self.regsObj.clearFlag()
# printing the object
print(self.regsObj)
for i in range(256):
if i < len(lines):
print(lines[i])
elif i in memAddress:
binNum = bin(memAddress[i])[2:]
print("0" * (16 - len(binNum)) + binNum)
else:
print("0" * 16)
showPlt = True
############# Main Function #############
if __name__ == "__main__":
######## getting input ########
lines = [i.strip() for i in sys.stdin.readlines()]
while lines[-1] == "":
lines.pop()
######## Creating Objects ########
# registers
regs = registers()
# operations
ops = operation(regs)
while not showPlt:
xPC.append((xPC[-1] + 1) if (len(xPC) > 0) else 1)
yPC.append(regs.regs["PC"])
ops.opcodes[lines[regs.regs["PC"]][:5]](lines[regs.regs["PC"]][5:])
#### PLotting the graph ####
# Setting the graph size
plt.figure(figsize=(10, 7))
plt.scatter(xPC, yPC, label="PC")
plt.scatter(x, y, label="Variables")
plt.xticks(xPC)
# plt.yticks([i for i in range(257)])
if len(y) != 0:
plt.yticks([i for i in range(max(max(y), max(yPC)) + 1)])
else:
plt.yticks([i for i in range(max(yPC) + 1)])
plt.yticks([i for i in range(max(max(y), max(yPC)) + 1)])
plt.xlabel("Cycle Number")
plt.ylabel("Memory Address")
plt.legend()
plt.show()