-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instructions.py
369 lines (305 loc) · 8.28 KB
/
Instructions.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from Register import Register
from collections import OrderedDict
class I(object):
def __init__(self,opcodes,args=('w','r'),size=1):
for opcode in opcodes:
if not (all([d in 'sdax01' for d in opcode]) and len(opcode)==8):
raise RuntimeError('ivalid opcode: {}'.format(opcode))
self.opcodes=opcodes
self.args=args
self.size=size
def __call__(self,function):
def func(cpu,*args):
cpu=args[0]
args=list(args)[1:]
do_write=lambda *largs:None
for n,arg in enumerate(zip(args,self.args)):
arg,mode=arg
if isinstance(arg,Register):
if mode=='w':
do_write=args[n].set
args[n]=args[n].get()
elif mode=='i':
args[n]=args[n].n
elif mode=='r':
args[n]=args[n].get()
continue
if isinstance(arg,int):
if mode=='r':
continue
if mode=='w':
raise TypeError('Integer not supported as destination')
rv=function(cpu,*args)
do_write(rv)
func.opcodes=self.opcodes
func.__name__=function.__name__
func.args=self.args
func.size=self.size
return func
def __len__(self):
return self.size
def get_input(cpu,port):
if cpu.verbose:
print("I:PORT:",port)
if port in cpu.hw:
return cpu.hw[port].input(cpu)
return None
def set_output(cpu,port,value):
if cpu.verbose:
print("O:PORT:",port,"VALUE:",value)
if port in cpu.hw:
cpu.hw[port].output(cpu,value)
class Instructions:
#opcode bits: s: src, d: dst ,a: addr, x: don't care
def __init__(self,cpu):
self.insts=list(filter(lambda x: not (x.startswith('_')), dir(self)))
self.insts.remove('hlt')
self.insts.insert(0,'hlt')
cpu.opcodes=OrderedDict()
cpu.opcode_names=OrderedDict()
for inst in self.insts:
inst=self.__getattribute__(inst)
cpu.opcodes[inst.opcodes]=inst
cpu.opcode_names[inst.__name__]=inst
if len(set(cpu.opcodes))!=len(set(self.insts)):
raise RuntimeError('Duplicate Opcode detected')
self.cpu=cpu
@I(('0000000x','11111111'),())
def hlt(cpu):
cpu.halted=True
@I(('0100sss1',),('r',))
def inp(cpu,r1):
port=r1
cpu.registers['A'][0]=get_input(cpu,port)
@I(('0111sss1',),('r',))
def out(cpu,r1):
value=cpu.registers['A'][0]
port=r1
set_output(cpu,port,value)
@I(('01xxx100',),(),3)
def jmp(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
cpu.set_ip(addr+2)
@I(('01000000',),(),3)
def jnc(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if not cpu.flags['C']:
cpu.set_ip(addr)
@I(('01001000',),(),3)
def jnz(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if not cpu.flags['Z']:
cpu.set_ip(addr)
@I(('01010000',),(),3)
def jp(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if not cpu.flags['S']:
cpu.set_ip(addr)
@I(('01011000',),(),3)
def jpo(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if cpu.flags['P']:
cpu.set_ip(addr)
@I(('01100000',),(),3)
def jc(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if cpu.flags['C']:
cpu.set_ip(addr)
@I(('01101000',),(),3)
def jz(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if cpu.flags['Z']:
cpu.set_ip(addr)
@I(('01110000',),(),3)
def jm(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if cpu.flags['S']:
cpu.set_ip(addr)
@I(('01111000',),(),3)
def jpe(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-5
if not cpu.flags['P']:
cpu.set_ip(addr)
@I(('01xxx110',),(),3)
def call(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01000010',),(),3)
def cnc(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if not cpu.flags['C']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01001010',),(),3)
def cnz(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if not cpu.flags['Z']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01010010',),(),3)
def cp(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if not cpu.flags['S']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01011010',),(),3)
def cpo(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if cpu.flags['P']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01100010',),(),3)
def cc(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if cpu.flags['C']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01101010',),(),3)
def cz(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if cpu.flags['Z']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01110010',),(),3)
def cm(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if cpu.flags['S']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('01111010',),(),3)
def cpe(cpu,*data):
addr=(int.from_bytes(data,'little')&((1<<14)-1))-3
if not cpu.flags['P']:
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('00xxx111',),(),1)
def ret(cpu):
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00000011',),(),1)
def rnc(cpu):
if not cpu.flags['C']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00001011',),(),1)
def rnz(cpu):
if not cpu.flags['Z']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00010011',),(),1)
def rp(cpu):
if not cpu.flags['S']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('01011011',),(),1)
def rpo(cpu):
if cpu.flags['P']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00100011',),(),1)
def rc(cpu):
if not cpu.flags['C']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00101011',),(),1)
def rz(cpu):
if cpu.flags['Z']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00110011',),(),1)
def rm(cpu):
if cpu.flags['S']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00111011',),(),1)
def rpe(cpu):
if not cpu.flags['P']:
addr=cpu.stack.pop()+2
cpu.set_ip(addr)
@I(('00aaa101',),('i',))
def rst(cpu,r1):
addr=(r1&((1<<3)-1))<<3
cpu.stack.push(cpu.get_ip())
cpu.set_ip(addr)
@I(('00ddd110',),('w',),2)
def mvi(cpu,r1,*data):
return data[0]
@I(('11dddsss',))
def mov(cpu,r1,r2):
return r2
@I(('00ddd000',),('w',))
def inc(cpu,r1):
return r1+1
@I(('00ddd001',),('w',))
def dec(cpu,r1):
return r1-1
@I(('10000sss',),('r',))
def add(cpu,r1):
cpu.registers['A'][0]+=r1
@I(('00000100',),(),2)
def adi(cpu,*data):
cpu.registers['A'][0]+=data[0]
@I(('10001sss',),('r',))
def adc(cpu,r1):
cpu.registers['A'][0]+=r1[0]+cpu.flags['C']
@I(('00001100',),(),2)
def aci(cpu,*data):
cpu.registers['A'][0]+=data[0]+cpu.flags['C']
@I(('10010sss',),('r',))
def sub(cpu,r1):
cpu.registers['A'][0]-=r1
@I(('00010100',),(),2)
def sui(cpu,*data):
cpu.registers['A'][0]-=data[0]
@I(('10011sss',),('r',))
def sbb(cpu,r1):
cpu.registers['A'][0]-=r1[0]+cpu.flags['C']
@I(('00011100',),('r',),2)
def sbi(cpu,*data):
cpu.registers['A'][0]-=data[0]+cpu.flags['C']
@I(('10100sss',),('r',))
def ana(cpu,r1):
cpu.registers['A'][0]&=r1
@I(('00100100',),('r',),2)
def ani(cpu,*data):
cpu.registers['A'][0]&=data[0]
@I(('10101sss',),('r',))
def xra(cpu,r1):
cpu.registers['A'][0]^=r1
@I(('00101100',),('r',),2)
def xri(cpu,*data):
cpu.registers['A'][0]^=data[0]
@I(('10110sss',),('r',))
def ora(cpu,r1):
cpu.registers['A'][0]|=r1
@I(('00110100',),('r',),2)
def ori(cpu,*data):
cpu.registers['A'][0]|=data[0]
@I(('10111sss',),('r',))
def cmp(cpu,r1):
cpu.update_flags(cpu.registers['A'][0],r1)
@I(('00111100',),('r',),2)
def cpi(cpu,*data):
cpu.update_flags(cpu.registers['A'][0],data[0])
@I(('00000010',),())
def rlc(cpu):
tip=cpu.registers['A'][0]&(1<<cpu.bits-1)
cpu.registers['A'][0]=(cpu.registers['A'][0]<<1)+int(bool(tip))
@I(('00001010',),())
def rrc(cpu):
tail=cpu.registers['A'][0]&1
cpu.registers['A'][0]=(cpu.registers['A'][0]>>1)+(int(bool(tail))<<(cpu.bits-1))
@I(('00010010',),())
def ral(cpu):
new_carry=int(bool(cpu.registers['A'][0]&(1<<cpu.bits-1)))
old_carry=cpu.flags['C']
cpu.registers['A'][0]=(cpu.registers['A'][0]<<1)+int(bool(old_carry))
cpu.flags['C']=new_carry
@I(('00011010',),())
def rar(cpu):
new_carry=cpu.registers['A'][0]&1
old_carry=cpu.flags['C']
cpu.registers['A'][0]=(cpu.registers['A'][0]>>1)+(int(bool(old_carry))<<(cpu.bits-1))
cpu.flags['C']=new_carry
#TODO Recheck Opcodes