-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#testEmulator.py#
329 lines (281 loc) · 9.72 KB
/
#testEmulator.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
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
self.pc = 52
self.memory = ['001','001','002','003','000','000','000','000','000','000',
'003','002','002','003','013','000','000','013','004','100',
'001','001','001','003','000','000','000','000','000','000',
'001','001','002','002','000','000','000','000','000','000',
'000','000','000','000','000','000','000','000','000','000',
'000','000']
self.memory.extend(['015','114','715','614','617','514','718','362','617',
'858','115','410','217','219','667','100','616',
'516','114','716','614','514','952'])
self.memory.extend(['000' for i in range(100-len(self.memory))])
>>>>>>> c705fc4b0d32f0bc14b3fe4a438433ecfe07ccd6
self.istream = 0
self.output_stream = []
self.input_stream = []
self.memory[0]='001'
self.inst = '000'
self.acc = 0
self.memory=['000' for i in range(100)]
self.memory[0]='001'
def dump(self):
if len(self.input_stream)==0:
istr="None"
else:
istr=self.input_stream[self.istream % len(self.input_stream)]
print(self.pc,'\t',self.acc,'\t',self.inst,'\t',self.meaning[(int(self.inst[0]))],'\t',
istr,'\t',self.output_stream,'\n')
for i in range(10):
if i==0:
line="00: "
else:
line=str(i*10)+": "
for j in range(10):
line=line+ ' '+self.memory[i*10+j]
print(line)
def read(self):
if self.istream < len(self.input_stream):
inst = self.input_stream[istream]
self.istream = self.istream + 1
return true
else:
return false
def copy_cell_to_output(self,adr):
# copies contents of memory cell 'adr' to output
self.output_stream.append(self.memory[adr])
def copy_input_to_cell(self,cell):
# copies input to memory cell
if cell > len(self.memory):
raise ValueError("Memory length is ", len(self.memory)," and index is ",cell)
self.memory[cell]=self.input_stream[self.istream]
self.istream = self.istream + 1
def clear_and_add(self,cell):
# clears accumulator and adds contents of cell to accumulator
self.acc = int(self.memory[cell])
def add(self,cell):
# adds contents of cell to accumulator
self.acc += int(self.memory[cell])
def move(self,cell):
# moves bug to cell (program counter)
self.pc = int(cell)
def shift(self,left,right):
# shifts left then right
self.acc = self.acc * pow(10,left)
self.acc = int(self.acc % 1000)
self.acc = self.acc / pow(10,right)
self.acc = int(self.acc % 1000)
def copy_to_output(self,cell):
self.output_stream.append(self.memory[cell])
def copy_acc_into_cell(self,cell):
self.memory[cell] = str(self.acc).zfill(3)
def subtract_contents_of_cell(self,cell):
self.acc = self.acc - int(self.memory[cell])
def call(self,cell):
self.memory[99]=str(800+self.pc)
self.pc = cell
def fetch(self):
self.inst = self.memory[self.pc]
self.pc = self.pc + 1
def decode(self):
op = int(self.inst[0])
adr = int(self.inst[1])*10 + int(self.inst[2])
if op == 0:
# copy input card
self.copy_input_to_cell(adr)
elif op == 1:
# clear and add
<<<<<<< HEAD
=======
# print(adr)
>>>>>>> c705fc4b0d32f0bc14b3fe4a438433ecfe07ccd6
self.clear_and_add(adr)
elif op == 2:
# add
self.add(adr)
elif op == 3:
# test accumulator (jump)
<<<<<<< HEAD
=======
# print(op,adr)
>>>>>>> c705fc4b0d32f0bc14b3fe4a438433ecfe07ccd6
if self.acc < 0:
self.pc=adr
print("set pc to ",adr)
elif op == 4:
# shift
self.shift(int(adr/10),adr % 10)
elif op == 5:
# output
self.copy_to_output(adr)
elif op == 6:
# store acc into cell
self.copy_acc_into_cell(adr)
elif op == 7:
# subtract
self.subtract_contents_of_cell(adr)
elif op == 8:
# jump (subroutine)
self.call(adr)
elif op == 9:
# stop
self.pc=adr
# print("done")
else:
# error
print("error")
def step1(self,first = False):
self.fetch()
self.decode()
if self.istream >= len(self.input_stream):
self.istream = len(self.input_stream) - 1
if first:
print(self.pc-1,'\t',self.acc,'\t',self.inst,'\t',self.meaning[(int(self.inst[0]))],'\t',
self.input_stream[0],'\t',self.output_stream)
else:
print(self.pc-1,'\t',self.acc,'\t',self.inst,'\t',self.meaning[(int(self.inst[0]))],'\t',
self.input_stream[self.istream],'\t',self.output_stream)
def step(self,n):
print("\npc\tacc\tinst\tmeaning \tinput\toutput")
for i in range(n):
self.step1(i==0)
def loadProgram(self,fileName):
self.memory=['000' for i in range(100)]
self.memory[0]='001'
f=open(fileName,'r')
s=f.readline()
while len(s)>0:
a=s.split(";")
b=a[0]
c=b.split(":")
adr=c[0]
dta=c[1]
self.memory[int(adr)]=dta.strip()
s=f.readline()
f.close()
self.dump()
def loadInput(self,fileName):
self.input_stream = []
f=open(fileName,'r')
s=f.readline()
while len(s)>0:
self.input_stream.append(s.strip())
s=f.readline()
f.close()
print(self.input_stream)
class TestEmulator(unittest.TestCase):
def test_PC(self):
em = Emulator()
# self.assertEqual(em.pc,19,"pc incorrect") ## for simple addition program
self.assertEqual(em.pc,0,"pc incorrect") ## for improved Nim program
def test_read(self):
# reads the next input value
em = Emulator()
self.assertTrue(em.read)
self.assertTrue('034',em.inst)
def test_copy_cell_to_output(self):
# write memory cell to output
em = Emulator()
self.assertEqual(0,len(em.output_stream))
em.copy_cell_to_output(0)
self.assertEqual(1,len(em.output_stream))
self.assertEqual('001',em.output_stream[0])
def test_copy_input_to_cell(self):
# write input to cell
em = Emulator()
em.input_stream = ['002']
em.copy_input_to_cell(19) # input has '002'
self.assertEqual('002',em.memory[19])
def test_clear_and_add(self):
em = Emulator()
em.memory[19]=1
em.clear_and_add(19)
self.assertEqual(1,em.acc)
def test_add(self):
em = Emulator()
em.add(0)
self.assertEqual(1,em.acc)
def test_move(self):
em = Emulator()
em.move(19)
self.assertEqual(19,em.pc)
def test_shift(self):
em = Emulator()
em.acc = 1
em.shift(1,0)
self.assertEqual(10,em.acc)
em.shift(0,1)
self.assertEqual(1,em.acc)
em.shift(4,0)
self.assertEqual(0,em.acc)
def test_copy_to_output(self):
em = Emulator()
em.copy_to_output(0)
self.assertEqual('001',em.output_stream[0])
def test_copy_acc_into_cell(self):
em = Emulator()
em.acc = 2
em.copy_acc_into_cell(2)
self.assertEqual('002',em.memory[2])
def test_subtract_contents_of_cell(self):
em = Emulator()
em.acc = 2
em.subtract_contents_of_cell(0)
self.assertEqual(1,em.acc)
def test_accumulator(self):
print("test accumulator")
em = Emulator()
em.inst='361'
em.acc=-1
em.decode()
self.assertEqual(61,em.pc)
def test_call(self):
em = Emulator()
em.pc = 1
em.call(12)
self.assertEqual(em.pc,12)
self.assertEqual(em.memory[99],'801')
def test_fetch(self):
em = Emulator()
em.input_stream=['002']
em.fetch()
<<<<<<< HEAD
=======
# self.assertEqual(em.inst,'034') ## for simple addition program
self.assertEqual(em.inst,'015') ## for improved Nim program
def test_length_of_memory(self):
em = Emulator()
self.assertEqual(100,len(em.memory))
def test_input(self):
em = Emulator()
# self.assertEqual(em.input_stream[em.istream],'004')
self.assertEqual(em.input_stream[em.istream],'002')
>>>>>>> c705fc4b0d32f0bc14b3fe4a438433ecfe07ccd6
def test_step(self):
em = Emulator()
self.assertEqual(em.pc,0)
em.input_stream=['002']
em.step1()
self.assertEqual(em.inst,'001')
self.assertEqual(em.memory[0],'001')
<<<<<<< HEAD
def test_length_of_memory(self):
em = Emulator()
self.assertEqual(100,len(em.memory))
def test_input(self):
em = Emulator()
em.input_stream=['002']
self.assertEqual(em.input_stream[em.istream],'002')
def test_load(self):
em = Emulator()
em.loadProgram("pgm1.asm")
self.assertEqual("034",em.memory[17])
=======
em.reset()
self.assertEqual(em.pc,52) ## for improved Nim program
em.dump()
print("\n")
em.step(75)
em.dump()
>>>>>>> c705fc4b0d32f0bc14b3fe4a438433ecfe07ccd6
if __name__ == '__main__':
unittest.main()