-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuhelper.py
766 lines (605 loc) · 19.5 KB
/
cpuhelper.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
class flags():
def __init__(self):
self.flags = 0
self.Z_POS = 7
self.N_POS = 6
self.H_POS = 5
self.C_POS = 4
def flagbytes(self):
return self.flags
def isZ(self):
return ((self.flags & (1 << self.Z_POS)) != 0)
def isN(self):
return ((self.flags & (1 << self.N_POS)) != 0)
def isH(self):
return ((self.flags & (1 << self.H_POS)) != 0)
def isC(self):
return ((self.flags & (1 << self.C_POS)) != 0)
def setZ(self, state):
if not ((0 <= self.flags <= 0xff)):
raise Exception("Bad bytes in register")
if(state):
self.flags = (self.flags | (1 << self.Z_POS)) & 0xff
else:
self.flags = ~(1 << self.Z_POS) & self.flags & 0xff
def setN(self, state):
if not ((0 <= self.flags <= 0xff)):
raise Exception("Bad bytes in register")
if(state):
self.flags = (self.flags | (1 << self.N_POS)) & 0xff
else:
self.flags = ~(1 << self.N_POS) & self.flags & 0xff
def setH(self, state):
if not ((0 <= self.flags <= 0xff)):
raise Exception("Bad bytes in register")
if(state):
self.flags = (self.flags | (1 << self.H_POS)) & 0xff
else:
self.flags = ~(1 << self.H_POS) & self.flags & 0xff
def setC(self, state):
if not ((0 <= self.flags <= 0xff)):
raise Exception("Bad bytes in register")
if(state):
self.flags = (self.flags | (1 << self.C_POS)) & 0xff
else:
self.flags = ~(1 << self.C_POS) & self.flags & 0xff
def setFlagByte(self, flagsval):
if not ((0 <= flagsval <= 0xff)):
raise Exception("Bad bytes in register")
self.flags = (flagsval & 0xf0)# >> 4) << 4
#& 0xf0 # BLAGGERS
class register():
# For opcodes
# http://www.geocities.co.jp/Bookend/5829/soft/emugb/gb_opcode.htm
def __init__(self):
# Internal 8-Bit resigeters
self.A = 0
self.B = 0
self.C = 0
self.D = 0
self.E = 0
self.F = 0
self.H = 0
self.L = 0
# 16 bit rgsters
self.PC = 0
self.SP = 0
self.localflags = flags()
self.ime = False
self.debug = False
'''
self.PC = 0#0x01B0
self.SP = 0xFFFE
self.setAF(0x1180)
self.setBC(0x0000)
self.setDE(0xFF56)
self.setHL(0x000D)
'''
'''
// Init CPU and its registers to the initial values.
func (cpu *CPU) Init(cgb bool) {
cpu.PC = 0x100
if cgb {
cpu.AF.Set(0x1180)
} else {
cpu.AF.Set(0x01B0)
}
cpu.BC.Set(0x0000)
cpu.DE.Set(0xFF56)
cpu.HL.Set(0x000D)
cpu.SP.Set(0xFFFE)
cpu.AF.Mask = 0xFFF0
}
'''
def setRegister(self, registerName,addressspace,args, value, call=False):
if not call:
return lambda : self.setRegister(registerName,addressspace, args, value, call=True)
if(len(registerName) == 1):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
if(len(registerName) == 2):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
if(registerName == "A"):
self.A = value
elif(registerName == "B"):
self.B = value
elif(registerName == "C"):
self.C = value
elif(registerName == "D"):
self.D = value
elif(registerName == "E"):
self.E = value
elif(registerName == "H"):
self.H = value
elif(registerName == "L"):
self.L = value
elif(registerName == "AF"):
self.setAF(value)
elif(registerName == "BC"):
self.setBC(value)
elif(registerName == "DE"):
self.setDE(value)
elif(registerName == "HL"):
self.setHL(value)
elif(registerName == "SP"):
self.setSP(value)
elif(registerName == "PC"):
self.setPC(value)
elif(registerName == "d8"):
raise Exception("bad code called in setRegister")
elif(registerName == "d16"):
raise Exception("bad code called in setRegister")
elif(registerName == "r8"):
raise Exception("bad code called in setRegister")
elif(registerName == "a16"):
raise Exception("bad code called in setRegister")
elif(registerName == "(BC)"):
addressspace.setByte(self.getBC(), value)
elif(registerName == "(DE)"):
# print(self.getDE())
# print(self.getDE())
# print(addressspace)
addressspace.setByte(self.getDE(), value)
elif(registerName == "(HL)"):
addressspace.setByte(self.getHL(), value)
elif(registerName == "(a8)"):
addressspace.setByte(0xff00 | args[0], value)
elif(registerName == "(a16)"):
addressspace.setByte(toword(args), value)
elif(registerName == "(C)"):
addressspace.setByte(0xff00 | self.getC(), value)
'''
saveopcode(0, 0xe0, 12, 1, "LDH (n), A", lambda registers, addressspace, funksjonsargumenter : addressspace.setByte(0xff00 | funksjonsargumenter[0], registers.getA() )) # (r, m, a) -> m.setByte(0xff00 + a[0], r.getA()));
# "LDH A, (n)",
saveopcode(0, 0xf0, 12, 1, "LDH A, (n)", lambda registers, addressspace, funksjonsargumenter : registers.setA(addressspace.getByte(0xff00 | funksjonsargumenter[0]) ) ) # (r, m, a) -> r.setA(m.getByte(0xff00 + a[0])));
'''
def getRegister(self, registerName,addressspace,args, call=False):
if not call:
return lambda : self.getRegister(registerName, call=True)
if(registerName == "A"):
return self.A
elif(registerName == "B"):
return self.B
elif(registerName == "C"):
return self.C
elif(registerName == "D"):
return self.D
elif(registerName == "E"):
return self.E
elif(registerName == "H"):
return self.H
elif(registerName == "L"):
return self.L
elif(registerName == "AF"):
return self.getAF()
elif(registerName == "BC"):
return self.getBC()
elif(registerName == "DE"):
return self.getDE()
elif(registerName == "HL"):
return self.getHL()
elif(registerName == "SP"):
return self.getSP()
elif(registerName == "PC"):
return self.getPC()
elif(registerName == "d8"):
return args[0]
elif(registerName == "d16"):
return toword(args)
elif(registerName == "r8"):
return args[0]
elif(registerName == "a16"):
return toword(args)
elif(registerName == "(BC)"):
return addressspace.getByte(self.getBC())
elif(registerName == "(DE)"):
return addressspace.getByte(self.getDE())
elif(registerName == "(HL)"):
return addressspace.getByte(self.getHL())
elif(registerName == "(a8)"):
return addressspace.getByte(0xff00 | args[0])
elif(registerName == "(a16)"):
return addressspace.getByte(toword(args))
elif(registerName == "(C)"):
return addressspace.getByte(0xff00 | self.getC())
# Writing to registers
def setA(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.A = value
def setB(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.B = value
def setC(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.C = value
def setD(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.D = value
def setE(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.E = value
def setH(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.H = value
def setL(self, value):
if not ((0 <= value <= 0xff)):
raise Exception("Bad bytes in register")
self.L = value
def setAF(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.A = (value >> 8)
#self.F = (value & 0xff) & 0xf0
self.localflags.setFlagByte(getLSB(value))
#getLSB(value & 0xf0)
# print( (getLSB(value) & 0xf0) == ((value & 0xf0) & 0xff) )
# self.localflags.setFlagByte(getLSB(value))
def setBC(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.B = (value >> 8)
self.C = (value & 0xff)
def setDE(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.D = (value >> 8)
self.E = (value & 0xff)
def setHL(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.H = (value >> 8)
self.L = (value & 0xff)
def setSP(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.SP = value
def setPC(self, value):
if not ((0 <= value <= 0xffff)):
raise Exception("Bad word in register")
self.PC = value
def setIME(self, value):
if not type(value) == bool:
raise Exception("bad arguemtn for setIME")
self.ime = value
def decrementHL(self):
old = self.getHL()
self.setHL((old - 1) % 0xffff)
# print(old)
return old
def incrementHL(self):
old = self.getHL()
self.setHL((old + 1) % 0xffff)
return old
def decrementSP(self):
self.SP = (self.SP - 1) % 0xffff
def incrementSP(self):
self.SP = (self.SP + 1) % 0xffff
def incrementPC(self):
self.PC = (self.PC + 1) % 0xffff
def flagsstatus(self):
newFlags = ""
for flagsarr in [
[self.localflags.isZ(), "Z"],
[self.localflags.isN(), "N"],
[self.localflags.isH(), "H"],
[self.localflags.isC(), "C"]
]:
if(flagsarr[0]):
newFlags += flagsarr[1]# + "-"
else:
newFlags += "-"
return newFlags
def getStatus(self):
def setZeros(var):
return (4-len(var)) * "0" + var
AF = setZeros(hex(self.getAF()).replace("0x", ""))
BC = setZeros(hex(self.getBC()).replace("0x", ""))
DE = setZeros(hex(self.getDE()).replace("0x", ""))
HL = setZeros(hex(self.getHL()).replace("0x", ""))
SP = setZeros(hex(self.getSP()).replace("0x", ""))
PC = setZeros(hex(self.getPC()).replace("0x", ""))
string = ("AF={}, BC={}, DE={}, HL={}, SP={}, PC={}".format(AF, BC, DE, HL, SP, PC))
# string = string.replace("0x","")
newFlags = self.flagsstatus()
string += (", {}{}".format(newFlags ,((8-len(newFlags)) * "-")))
return string
# Reading the registers
def getA(self):
return self.A
def getB(self):
return self.B
def getC(self):
return self.C
def getD(self):
return self.D
def getE(self):
return self.E
def getF(self):
return self.F
def getH(self):
return self.H
def getL(self):
return self.L
def getAF(self):
#return (self.getA() << 8 | self.getF())
#self.localflags.flagbytes())#self.getF() )
return (self.getA() << 8 | self.localflags.flagbytes() )# self.localflags.flagbytes())
def getBC(self):
return (self.getB() << 8 | self.getC())
def getDE(self):
return (self.getD() << 8 | self.getE())
def getHL(self):
return (self.getH() << 8 | self.getL())
def getSP(self):
return (self.SP)
def getPC(self):
return (self.PC)
def getFlags(self):
return self.localflags.flags
def isIME(self):
return self.ime
'''
Operations
'''
def addbytes(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
self.localflags.setZ(((byte1 + byte2 ) & 0xff == 0))
self.localflags.setN(False)
self.localflags.setH(((byte1 & 0x0f) + (byte2 & 0x0f) > 0x0f))
self.localflags.setC(((byte1 + byte2 > 0xff)))
return ((byte1 + byte2) & 0xff )
def addBytesAndCarry(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
cary = 1 if(self.localflags.isC()) else 0
self.localflags.setZ((byte1 + byte2 + cary ) & 0xff == 0)
self.localflags.setN(False)
self.localflags.setH(((byte1 & 0x0f) + (byte2 & 0x0f) + cary > 0x0f))
self.localflags.setC(((byte1 + byte2 + cary > 0xff)))
return ((byte1 + byte2 + cary) & 0xff )
def subytes(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
self.localflags.setZ(((byte1 - byte2 ) & 0xff == 0))
self.localflags.setN(True)
self.localflags.setH((0x0f & byte2) > (0x0f & byte1))
self.localflags.setC(((byte2 > byte1)))
return ((byte1 - byte2) % 0xff )
def subBytesWithCarry(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
carry = 1 if(self.localflags.isC()) else 0
self.localflags.setZ(((byte1 - byte2 - carry) & 0xff) == 0)
self.localflags.setN(True)
self.localflags.setH(((0x0f & (byte2 + carry)) > (0x0f & byte1)))
self.localflags.setC((byte2 + carry > byte1))
return ((byte1 - byte2 - carry) % 0xff)
def bitAnd(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
result = byte1 & byte2;
self.localflags.setZ((result == 0))
self.localflags.setN((False))
self.localflags.setH((True))
self.localflags.setC((False))
return result
def bitOr(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
result = byte1 | byte2;
self.localflags.setZ((result == 0))
self.localflags.setN((False))
self.localflags.setH((False))
self.localflags.setC((False))
return result
def bitXor(self, byte1, byte2):
if not ((0 <= byte1 <= 0xff) or (0 <= byte2 <= 0xff)):
raise Exception("Bad bytes in addbytes")
result = byte1 ^ byte2;
self.localflags.setZ((result == 0))
self.localflags.setN((False))
self.localflags.setH((False))
self.localflags.setC((False))
return result
def INC(self,flags, byte):
if not ((0 <= byte <= 0xff)):
raise Exception("Bad bytes in addbytes")
result = (byte + 1) & 0xff;
self.localflags.setZ((result == 0))
self.localflags.setN((False))
self.localflags.setH(((0x0f & result) < (0x0f & byte)))
return result
def DEC(self, flags, byte):
if not ((0 <= byte <= 0xff)):
raise Exception("Bad bytes in addbytes")
result = (byte - 1) & 0xff;
self.localflags.setZ((result == 0))
self.localflags.setN((True))
self.localflags.setH((byte & 0x0f ) == 0)
return result
def addSignedByteToWord(self, word, signedbyte):
if not ((0 <= signedbyte <= 0xff)):
raise Exception("Bad bytes in addSignedByteToWord")
if not ((0 <= word <= 0xffff)):
raise Exception("Bad word in addSignedByteToWord")
self.localflags.setZ(False)
self.localflags.setN(False)
def abs(byte):
if((byte & (1 << 7)) != 0):
return 0x100 - byte
else:
return byte
b = abs(byte)
if((signedbyte & (1 << 7)) != 0):
self.localflags.setH( (word & 0x0f) < (b & 0x0f) )
self.localflags.setC( (word & 0xff) < (b) )
return (word-b) % 0xffff
else:
self.localflags.setC((word & 0xff) + b > 0xff)
self.localflags.setH((word & 0x0f) + (b & 0x0f) > 0x0f)
return (word+b) & 0xffff
def addWords(self, word1, word2):
if not ((0 <= word1 <= 0xffff) or (0 <= word2 <= 0xffff)):
raise Exception("Bad word in addSignedByteToWord")
self.localflags.setN(False)
self.localflags.setH((word1 & 0x0fff) + (word2 & 0x0fff) > 0x0fff)
self.localflags.setC(word1 + word2 > 0xffff)
return (word1 + word2) & 0xffff;
def swap(self, flags, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
upper = byteValue & 0xf0;
lower = byteValue & 0x0f;
result = (lower << 4) | (upper >> 4)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
self.localflags.setC(False)
return result
def rotateLeft(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue << 1) & 0xff;
if ((byteValue & (1<<7)) != 0):
result |= 1;
self.localflags.setC(True)
else:
self.localflags.setC(False)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def rotateRight(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue >> 1);
if ((byteValue & (1<<7)) != 0):
result |= 1;
self.localflags.setC(True)
else:
self.localflags.setC(False)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def rotateLeftThroughCarry(self, flags, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue << 1) & 0xff
result |= (1 if(self.localflags.isC()) else 0)
#print((byteValue & (1<<7)) != 0)
self.localflags.setC((byteValue & (1<<7)) != 0)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
# https://www.reddit.com/r/EmuDev/comments/6mobks/question_gameboy_f_register_bug/
# ^ her er sannheten?
def rotateRightThroughCarry(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = byteValue >> 1
result |= (1 << 7) if(flags.isC()) else 0
self.localflags.setC((byteValue & 1) != 0)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def shiftLeft(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue << 1) & 0xff
self.localflags.setC((byteValue & (1<<7)) != 0)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def shiftRightArtithmetic(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue >> 1) | (byteValue & (1 << 7))
self.localflags.setC((byteValue & 1) != 0)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def shiftRightLogical(self, byteValue):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
result = (byteValue >> 1)
self.localflags.setC((byteValue & 1) != 0)
self.localflags.setZ(result == 0)
self.localflags.setN(False)
self.localflags.setH(False)
return result
def bit(self,flags, byteValue, bit):
if not ((0 <= byteValue <= 0xff) or (0 <= bit <= 0xff)):
raise Exception("Bad bytes in swap")
if(bit < 8):
self.localflags.setZ((byteValue & (1 << bit)) != 0)
self.localflags.setN(False)
self.localflags.setH(True)
def setBit(self, byteValue, position):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
return (byteValue | (1 << position)) & 0xff
def clearBit(self, byteValue, position):
if not ((0 <= byteValue <= 0xff)):
raise Exception("Bad bytes in swap")
return ~(1 << position) & byteValue & 0xff
def push(self, registers, addressspace, word):
if not ((0 <= word <= 0xffff)):
raise Exception("Bad word in push")
# print(word)
self.decrementSP()
# addressspace.setByte(self.getSP(), getMSB(word))
addressspace.setByte(self.getSP(), (word & 0xff00) >> 8)
self.decrementSP()
# addressspace.setByte(self.getSP(), getLSB(word))
addressspace.setByte(self.getSP(), (word & 0x00ff))
def pop(self, addressspace):
# print(self.getSP())
# print(addressspace.getByte(self.getSP(), True))
lsb = addressspace.getByte(self.getSP())
self.incrementSP()
msb = addressspace.getByte(self.getSP())
self.incrementSP()
#print("ALL ZERO?")
#print(lsb | (msb << 8))
return lsb | (msb << 8);
def call(self, registers, addressspace, address):
if not ((0 <= address <= 0xff)):
raise Exception("Bad bytes in rest")
# def load -> pc
self.push(registers, addressspace, (registers.getPC()) & 0xffff);
self.setPC(address);
def reset(self, registers, addressspace, address):
if not ((0 <= address <= 0xff)):
raise Exception("Bad bytes in rest")
self.push(registers, addressspace, address)
self.setPC(address)
def ret(self, registers, addressspace):
#print(self.pop(addressspace))
self.setPC(self.pop(addressspace))
def getLSB(word):
if not ((0 <= word <= 0xffff)):
raise Exception("Bad word in addSignedByteToWord")
return (word & 0xff)
def getMSB(word):
if not ((0 <= word <= 0xffff)):
raise Exception("Bad word in addSignedByteToWord")
return (word >> 8)
def towordint(msb, lsb):
if not ((0 <= msb <= 0xff) or (0 <= lsb <= 0xff)):
raise Exception("Bad bytes in toword")
return (msb << 8) | lsb
def toword(arr):
return towordint(arr[1], arr[0])