-
Notifications
You must be signed in to change notification settings - Fork 0
/
f4.py
106 lines (92 loc) · 2.71 KB
/
f4.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
import sys
class machine:
def __init__(self):
self.PC = 0
self.A = 0
self.V = 0
self.program_memory = []*65535
self.data_memory = {}
self.command_register_parameter = [0]
self.command_register = {
"ADDi": self.ADDi,
"ADDm": self.ADDm,
"ADDpc": self.ADDpc,
"BVS": self.BVS,
"LDAi": self.LDAi,
"LDAm": self.LDAm,
"LDApc": self.LDApc,
"STAm": self.STAm,
"STApc": self.STApc,
"HLT": self.HLT
}
def execute(self):
command, parameter = self.program_memory[self.PC]
self.command_register_parameter = parameter
return self.command_register[command]()
def ADDi(self):
if((self.A + self.command_register_parameter[0]) > 65535):
self.V = 1
else:
self.V = 0
self.A += self.command_register_parameter[0]
self.PC+=1
def ADDm(self):
if((self.A + self.data_memory[self.command_register_parameter[0]]) > 65535):
self.V = 1
else:
self.V = 0
self.A += self.data_memory[self.command_register_parameter[0]]
self.PC+=1
def ADDpc(self):
if((self.A + self.PC) > 65535):
self.V = 1
else:
self.V = 0
self.A+=self.PC
self.PC+=1
def BVS(self):
if (self.V == 1):
self.PC = self.command_register_parameter[0]
else:
self.PC+=1
def LDAi(self):
self.A = self.command_register_parameter[0]
self.PC+=1
def LDAm(self):
self.A = self.data_memory[self.command_register_parameter[0]]
self.PC+=1
def LDApc(self):
self.A = self.PC
self.PC+=1
def STAm(self):
self.data_memory[self.command_register_parameter[0]] = self.A
self.PC+=1
def STApc(self):
self.PC = self.A
def HLT(self):
return 0
rm = machine()
rm.program_memory.append(["LDAi", [2]])
rm.program_memory.append(["ADDi",[2]])
rm.program_memory.append(["STAm",[1]])
rm.program_memory.append(["ADDm",[1]])
rm.program_memory.append(["ADDpc",None])
rm.program_memory.append(["ADDi",[70000]])
rm.program_memory.append(["STAm",[100]])
rm.program_memory.append(["BVS", [9]])
rm.program_memory.append([None, None])
rm.program_memory.append(["HLT", None])
while True:
code = rm.execute()
if code is None:
pass
elif code == 0:
break
print("data memory:", rm.data_memory)
print(" ")
print("accumulator:",rm.A)
print(" ")
print("program memory:",rm.program_memory)
print(" ")
print("program counter:",rm.PC)
print("-------------")