-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
219 lines (184 loc) · 4.74 KB
/
simulate.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
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
import sys
byte=0
filename = sys.argv[1]
offset = int(sys.argv[2])
f=open("Output/"+filename,'r')
data=f.read()
f.close()
opcodes = {}
code=data.splitlines()
lines = {}
reg={'A':0, 'B':0, 'C':0, 'D':0, 'E':0, 'H':0, 'L':0}
mem=[0]*1024
sp=1023
flag=[0]*5
dispflag=0
f=open('opcode_len.txt','r')
data=f.read()
f.close()
lin=data.splitlines()
for line in lin:
line=line.split(' ')
opcodes[line[0]]=int(line[1])
j=offset
for line in code:
lines[str(j)]=line
if line.startswith('DC') or line.startswith('DM'):
mem[j]=int(line.split(' ')[1])
j += opcodes[line.split(' ')[0]]
i=j
byte=offset
def set_flag(acc):
if acc == 0:
flag[0]=1
flag[1]=0
flag[2]=0
elif acc > 0:
flag[1]=1
flag[0]=0
flag[2]=0
else:
flag[2]=1
flag[1]=0
flag[0]=0
dispflag=0
def display(prevs, nexts):
global dispflag
if dispflag is 0:
print '\nRegisters:'
for key in reg:
print key+' : '+str(reg[key])
print 'Memory:'
print mem[:100]
print 'Flags : '+str(flag)
print 'Prev Statement:'+lines[str(prevs)]
print 'Next Statement:'+lines[str(nexts)]
print 'Contiue(c) or Next(n) :',
res=raw_input()
if res is 'c':
dispflag=1
while byte < i:
line=lines[str(byte)]
line_split = line.split(' ')
if len(line_split) > 1:
line_split[1] = line_split[1].strip(',')
if line.startswith('MOV'):
reg[line_split[1]]=reg[line_split[2]]
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('MVI'):
reg[line_split[1]]=int(line_split[2])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('LDA'):
reg['A']=mem[int(line_split[1])]
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('ADI'):
reg['A']=reg['A'] + int(line_split[1])
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('SUI'):
reg['A']=reg['A'] - int(line_split[1])
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('SUB'):
reg['A']=reg['A'] - reg[line_split[1]]
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('ADD'):
reg['A']=reg['A'] + reg[line_split[1]]
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('STA'):
mem[int(line_split[1])]=reg['A']
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('PUSH'):
if line_split[1] is 'D':
mem[sp]=reg['D']
mem[sp-1]=reg['E']
elif line_split[1] is 'B':
mem[sp]=reg['B']
mem[sp-1]=reg['C']
else:
mem[sp]=reg['H']
mem[sp-1]=reg['L']
sp=sp-2
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('POP'):
if line_split[1] is 'D':
reg['D']=mem[sp+1]
reg['E']=mem[sp]
elif line_split[1] is 'B':
reg['B']=mem[sp+1]
reg['C']=mem[sp]
else:
reg['H']=mem[sp+1]
reg['L']=mem[sp]
sp=sp+2
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('ORI'):
reg['A']=reg['A'] | int(line_split[1])
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('ORA'):
reg['A']=reg['A'] | reg[line_split[1]]
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('JNZ'):
byte_old=byte
if flag[0] == 0:
byte=int(line_split[1])
else:
byte += opcodes[line_split[0]]
display(byte_old, byte)
elif line.startswith('JZ'):
byte_old=byte
if flag[0] == 1:
byte=int(line_split[1])
else:
byte += opcodes[line_split[0]]
display(byte_old, byte)
elif line.startswith('JP'):
byte_old=byte
if flag[1] == 1:
byte=int(line_split[1])
else:
byte += opcodes[line_split[0]]
display(byte_old, byte)
elif line.startswith('JMP'):
byte_old=byte
byte=int(line_split[1])
display(byte_old, byte)
# elif line.startswith('DB'):
# mem[byte]=int(line_split[1])
# byte += opcodes[line_split[0]]
# display(byte-opcodes[line_split[0]], byte)
elif line.startswith('JM'):
byte_old=byte
byte=int(line_split[1]) -1
byte += opcodes[line_split[0]]
display(byte_old, byte)
elif line.startswith('ANI'):
reg['A']=reg['A'] & int(line_split[1])
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('ANA'):
reg['A']=reg['A'] & reg[line_split[1]]
set_flag(reg['A'])
byte += opcodes[line_split[0]]
display(byte-opcodes[line_split[0]], byte)
elif line.startswith('HLT'):
byte += opcodes[line_split[0]]
for key in reg:
print key+' : '+str(reg[key])