-
Notifications
You must be signed in to change notification settings - Fork 1
/
display.py
86 lines (73 loc) · 3.26 KB
/
display.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
"""
The display module is used for displaying the machine state and descriptions of instructions
Copyright (c) Ryan Showalter and Cole Nutter under the terms of the MIT License
"""
def describe(a, b):
"""Displays an explanation of the instruction
Args:
a (str): The string of the first half of the description
b (str): The string of the second half of the description
Returns:
str: The complete description
"""
instruction = a + b
opcode = instruction[0]
if opcode == '1':
result = "LOAD register {} with the value in register {}"\
.format(instruction[1], instruction[2:])
elif opcode == '2':
result = "LOAD register {} with the value {}"\
.format(instruction[1], instruction[2:])
elif opcode == '3':
result = "STORE the the value in register {} in memory cell {}"\
.format(instruction[1], instruction[2:])
elif opcode == '4':
result = "MOVE the value in register {} to register {}"\
.format(instruction[2], instruction[3])
elif opcode == '5' or opcode == '6':
result = "ADD the values in registers {} and {} in twos complement "\
"notation and place the result in register {}"\
.format(instruction[2], instruction[3], instruction[1])
elif opcode == '7':
result = "OR the values in registers {} and {} and place the result "\
"in register {}".format(instruction[2], instruction[3],
instruction[1])
elif opcode == '8':
result = "AND the values in registers {} and {} and place the result "\
"in register {}".format(instruction[2], instruction[3],
instruction[1])
elif opcode == '9':
result = "XOR the values in registers {} and {} and place the result "\
"in register {}".format(instruction[2], instruction[3],
instruction[1])
elif opcode == 'a':
result = "ROTATE the bit pattern in register {} {} times"\
.format(instruction[1], instruction[3])
elif opcode == 'b':
result = "JUMP to the instruction at memory cell {} if the bit pattern "\
" in register {} is equal to the bit pattern in register 0"\
.format(instruction[2:], instruction[1])
elif opcode == 'c':
result = "HALT execution"
else:
result = ""
return result
def display(cells, registers, icounter):
""" Displays the memory cells, registers, instruction counter, and their contents
Args:
cells (list of Cell): the complete list of memory cells
registers (list of Cell): the complete list of register cells
icounter (int): the instruction counter for the program
"""
print("Memory Cells")
i = 0
for cell in cells:
print(cell.getid(), " ", cell.tostr(), end=' ')
if i % 2 == 0 and len(cells) > i + 1:
print(describe(cells[i].tostr(), cells[i + 1].tostr()), end='')
i += 1
print("")
print("Registers")
for register in registers:
print(register.getid(), " ", register.tostr())
print("Instruction counter: ", str(hex(icounter))[2:])