-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
140 lines (126 loc) · 4.49 KB
/
run.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
import os
import sys
add2 = os.getcwd() + '/Hardware'
add1 = os.getcwd() + '/Assembler'
sys.path.append(add1)
sys.path.append(add2)
import all_memory as memory
import mainprogram as assembler
import CPU as cpu
def load(micro_code, main_code):
micro, main = assembler.assembler(micro_code, main_code)
mem = memory.AllMemory()
for i in micro:
bin, address = i.split('-')
mem.control_memory.write(int(address), '0b'+bin, 'b')
for j in main:
bin, address = j.split('-')
mem.main_memory.write(int(address), '0b'+bin, 'b')
mem.CAR.write(64, 'd')
mem.PC.write(0, 'd')
return cpu.CPU(mem)
# command line to control simulator
# >clock num done
# >clock next command done
# >run done
# >memory address done
# >registers done
# >write address value done
# >load mircro file_address done
# >load main file_address done
# >assemble done
# >quit done
# >help
# >codes done
help = """
clock <num:decimal> : run <num> clock cycles
clock next command : run until next command in main memory
run : run until halt
memory <address:decimal> : print the value in the address
registers : print the value of all registers
write <address:decimal> <value:decimal> : write the value in the address
load micro <file_address> : load micro code from file
load main <file_address> : load main code from file
assemble : assemble micro and main code
quit : quit the simulator
codes : print the assembled micro and main code
"""
def show_code(micro_bin, main_bin):
print("micro code")
for i in micro_bin:
i = i.split('-')
print(i[1], i[0])
print('--------------------------------------------------')
print("main code")
for i in main_bin:
i = i.split('-')
print(i[1], i[0])
print('--------------------------------------------------')
def main():
micro = None
main = None
micro_bin = None
main_bin = None
computer = None
while True:
print("****************************************************")
inp = input(">>> ").split()
if inp[0] == 'quit':
break
elif inp[0] == "write":
try:
computer.memory.main_memory.write(int(inp[1]), int(inp[2]), 'd')
except:
print("syntax error")
elif inp[0] == "load":
if inp[1] == 'main':
try:
with open(inp[2], 'r') as f:
main = str(f.read())
print(main)
except:
print("file not found")
elif inp[1] == 'micro':
try:
with open(inp[2], 'r') as f:
micro = str(f.read())
print(micro)
except:
print("file not found")
else:
print("syntax error")
elif inp[0] == 'assemble':
micro_bin, main_bin = assembler.assembler(micro, main)
computer = load(micro, main)
show_code(micro_bin, main_bin)
elif inp[0] == 'clock':
computer.clock()
if inp[1] == 'next':
while computer.memory.CAR.read_dec() != 64:
computer.clock()
else:
try:
for _ in range(int(inp[1])):
computer.clock()
except:
print("syntax error")
elif inp[0] == 'register':
computer.print_reg()
elif inp[0] == 'memory':
print(computer.memory.main_memory.read(int(inp[1]), 'd'))
try:
pass
except:
print("syntax error")
elif inp[0] == 'codes':
show_code(micro_bin, main_bin)
elif inp[0] == 'run':
try:
while True:
computer.clock()
except:
pass
else:
print(help)
if __name__ == "__main__":
main()