-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcu.py
2377 lines (1898 loc) · 82.3 KB
/
mcu.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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Tuple, Union
import disassembler
class Microcontroller:
def __init__(self):
self.rom = [0] * 65536 # 64 KiB
self.mem = InternalDataMemory()
self.xmem = ExternalDataMemory()
self._pc = DoubleByte()
self.interrupt_stack = Stack()
self.interrupt_stack.push(0)
self.timer0 = Timer0(self)
self.timer1 = Timer1(self)
@property
def pc(self):
return self._pc
@pc.setter
def pc(self, value):
self._pc.value = value
def load_hex_file(self, file_content):
for record in disassembler.IntelHexFile(file_content):
for addr, byte in enumerate(record, record.first_byte_addr):
self.rom[addr] = byte
def reset_rom(self):
self.reset_ram()
self.rom = [0] * 65536
def reset_ram(self):
self.mem = InternalDataMemory()
self.xmem = ExternalDataMemory()
self._pc = DoubleByte()
self.interrupt_stack = Stack()
self.interrupt_stack.push(0)
def next_cycle(self):
# Set/clear the parity flag
self.mem.p = 0 if self.mem.a.bits.count('1') % 2 else 1
# Check for an external interrupt request from INT0/INT1
# that can be either negative edge-triggered or negative level-activated
if (self.mem.it0 and self.mem.int0_previous_state and not self.mem.int0
or not self.mem.it0 and not self.mem.int0):
self.mem.ie0 = 1
if (self.mem.it1 and self.mem.int1_previous_state and not self.mem.int1
or not self.mem.it1 and not self.mem.int1):
self.mem.ie1 = 1
# Handle an interrupt request
if self.mem.ea:
# INT0 (high priority)
if ((int0_awaiting := self.mem.ie0 and self.mem.ex0)
and self.mem.px0 and self.interrupt_stack.top() < 10):
if self.mem.it0:
self.mem.ie0 = 0
self.interrupt_stack.push(10)
self._exec_18(0, 3)
# T0 (high priority)
elif ((t0_awaiting := self.mem.tf0 and self.mem.et0)
and self.mem.pt0 and self.interrupt_stack.top() < 9):
self.mem.tf0 = 0
self.interrupt_stack.push(9)
self._exec_18(0, 11)
# INT1 (high priority)
elif ((int1_awaiting := self.mem.ie1 and self.mem.ex1)
and self.mem.px1 and self.interrupt_stack.top() < 8):
if self.mem.it1:
self.mem.ie1 = 0
self.interrupt_stack.push(8)
self._exec_18(0, 19)
# T1 (high priority)
elif ((t1_awaiting := self.mem.tf1 and self.mem.et1)
and self.mem.pt1 and self.interrupt_stack.top() < 7):
self.mem.tf1 = 0
self.interrupt_stack.push(7)
self._exec_18(0, 27)
# INT0 (low priority)
elif int0_awaiting and self.interrupt_stack.top() < 5:
if self.mem.it0:
self.mem.ie0 = 0
self.interrupt_stack.push(5)
self._exec_18(0, 3)
# T0 (low priority)
elif t0_awaiting and self.interrupt_stack.top() < 4:
self.mem.tf0 = 0
self.interrupt_stack.push(4)
self._exec_18(0, 11)
# INT1 (low priority)
elif int1_awaiting and self.interrupt_stack.top() < 3:
if self.mem.it1:
self.mem.ie1 = 0
self.interrupt_stack.push(3)
self._exec_18(0, 19)
# T1 (low priority)
elif t1_awaiting and self.interrupt_stack.top() < 2:
self.mem.tf1 = 0
self.interrupt_stack.push(2)
self._exec_18(0, 27)
# Execute an operation
op = Operation(self.rom[int(self.pc)])
op.args = self.rom[int(self.pc + 1):int(self.pc + len(op))]
self.pc += len(op)
# Jump operations may override the PC
exec(f'self._exec_{op.opcode}(*op.args)')
# Increment Timer 0
if self.mem.tr0 and (self.mem.int0 or not self.mem.t0_gate):
# Increment in response to a negative edge at T0
if self.mem.t0_ct:
if self.mem.t0_previous_state and not self.mem.t0:
self.timer0.increment()
# Increment every machine cycle
else:
for _ in range(op.cycles):
self.timer0.increment()
if self.mem.tr1 and self.mem.t0_mode == 3:
for _ in range(op.cycles):
self.timer0.increment(mode3_th0_only=True)
# Increment Timer 1
if self.mem.tr1 and (self.mem.int1 or not self.mem.t1_gate):
# Increment in response to a negative edge at T1
if self.mem.t1_ct:
if self.mem.t1_previous_state and not self.mem.t1:
self.timer1.increment()
# Increment every machine cycle
else:
for _ in range(op.cycles):
self.timer1.increment()
# Save the current state of T0, T1, INT0, INT1 for use in the next cycle
self.mem.t0_previous_state = self.mem.t0
self.mem.t1_previous_state = self.mem.t1
self.mem.int0_previous_state = self.mem.int0
self.mem.int1_previous_state = self.mem.int1
def _exec_0(self):
return
def _exec_1(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}000{addr11:08b}', 2)
def _exec_2(self, high_order_byte, low_order_byte):
# Turn two one-byte arguments (as stored in a .hex file) into one two-byte argument
# e.g. 0xAB = 171, 0xCD = 205; 171 * 16 ** 2 + 205 = 43981 = 0xABCD
self.pc = high_order_byte * 16 ** 2 + low_order_byte
def _exec_3(self):
self.mem.a.rotate_right()
def _exec_4(self):
self.mem.a += 1
def _exec_5(self, direct):
self.mem[direct] += 1
def _exec_6(self):
self.mem[self.mem.r0] += 1
def _exec_7(self):
self.mem[self.mem.r1] += 1
def _exec_8(self):
self.mem.r0 += 1
def _exec_9(self):
self.mem.r1 += 1
def _exec_10(self):
self.mem.r2 += 1
def _exec_11(self):
self.mem.r3 += 1
def _exec_12(self):
self.mem.r4 += 1
def _exec_13(self):
self.mem.r5 += 1
def _exec_14(self):
self.mem.r6 += 1
def _exec_15(self):
self.mem.r7 += 1
def _exec_16(self, bit, offset):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
if self.mem[byte_no][7 - bit % 8]:
self.mem[byte_no][7 - bit % 8] = 0
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_17(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}000{addr11:08b}', 2)
def _exec_18(self, high_order_byte, low_order_byte):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
# Turn two one-byte arguments (as stored in a .hex file) into one two-byte argument
# e.g. 0xAB = 171, 0xCD = 205; 171 * 16 ** 2 + 205 = 43981 = 0xABCD
self.pc = high_order_byte * 16 ** 2 + low_order_byte
def _exec_19(self):
least_significant_bit = self.mem.a.bits[-1]
self.mem.a = int(str(self.mem.c) + self.mem.a.bits[:-1], 2)
self.mem.c = least_significant_bit
def _exec_20(self):
self.mem.a -= 1
def _exec_21(self, direct):
self.mem[direct] -= 1
def _exec_22(self):
self.mem[self.mem.r0] -= 1
def _exec_23(self):
self.mem[self.mem.r1] -= 1
def _exec_24(self):
self.mem.r0 -= 1
def _exec_25(self):
self.mem.r1 -= 1
def _exec_26(self):
self.mem.r2 -= 1
def _exec_27(self):
self.mem.r3 -= 1
def _exec_28(self):
self.mem.r4 -= 1
def _exec_29(self):
self.mem.r5 -= 1
def _exec_30(self):
self.mem.r6 -= 1
def _exec_31(self):
self.mem.r7 -= 1
def _exec_32(self, bit, offset):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
if self.mem[byte_no][7 - bit % 8]:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_33(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}001{addr11:08b}', 2)
def _exec_34(self):
high_order_byte = self.mem[self.mem.sp]
self.mem.sp -= 1
low_order_byte = self.mem[self.mem.sp]
self.mem.sp -= 1
self.pc = int(f'{high_order_byte:b}{low_order_byte:08b}', 2)
def _exec_35(self):
self.mem.a.rotate_left()
def _exec_36(self, immed):
self.mem.c = 1 if int(self.mem.a) + immed > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(f'{immed:08b}'[4:], 2) > 15 else 0
# Convert A and immed as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
immed_signed = immed - 256 if immed > 127 else immed
self.mem.ov = 1 if not -129 < a_signed + immed_signed < 128 else 0
self.mem.a += immed
def _exec_37(self, direct):
self.mem.c = 1 if int(self.mem.a) + int(self.mem[direct]) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[direct].bits[4:], 2) > 15 else 0
# Convert A and mem[direct] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_direct_signed = int(self.mem[direct]) - 256 if self.mem[direct] > 127 else int(self.mem[direct])
self.mem.ov = 1 if not -129 < a_signed + mem_direct_signed < 128 else 0
self.mem.a += self.mem[direct]
def _exec_38(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem[self.mem.r0]) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[self.mem.r0].bits[4:], 2) > 15 else 0
# Convert A and mem[r0] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r0_signed = int(self.mem[self.mem.r0]) - 256 if self.mem[self.mem.r0] > 127 else int(self.mem[self.mem.r0])
self.mem.ov = 1 if not -129 < a_signed + mem_r0_signed < 128 else 0
self.mem.a += self.mem[self.mem.r0]
def _exec_39(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem[self.mem.r1]) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[self.mem.r1].bits[4:], 2) > 15 else 0
# Convert A and mem[r1] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r1_signed = int(self.mem[self.mem.r1]) - 256 if self.mem[self.mem.r1] > 127 else int(self.mem[self.mem.r1])
self.mem.ov = 1 if not -129 < a_signed + mem_r1_signed < 128 else 0
self.mem.a += self.mem[self.mem.r1]
def _exec_40(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r0) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r0.bits[4:], 2) > 15 else 0
# Convert A and R0 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r0_signed = int(self.mem.r0) - 256 if self.mem.r0 > 127 else int(self.mem.r0)
self.mem.ov = 1 if not -129 < a_signed + r0_signed < 128 else 0
self.mem.a += self.mem.r0
def _exec_41(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r1) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r1.bits[4:], 2) > 15 else 0
# Convert A and R1 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r1_signed = int(self.mem.r1) - 256 if self.mem.r1 > 127 else int(self.mem.r1)
self.mem.ov = 1 if not -129 < a_signed + r1_signed < 128 else 0
self.mem.a += self.mem.r1
def _exec_42(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r2) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r2.bits[4:], 2) > 15 else 0
# Convert A and R2 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r2_signed = int(self.mem.r2) - 256 if self.mem.r2 > 127 else int(self.mem.r2)
self.mem.ov = 1 if not -129 < a_signed + r2_signed < 128 else 0
self.mem.a += self.mem.r2
def _exec_43(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r3) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r3.bits[4:], 2) > 15 else 0
# Convert A and R3 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r3_signed = int(self.mem.r3) - 256 if self.mem.r3 > 127 else int(self.mem.r3)
self.mem.ov = 1 if not -129 < a_signed + r3_signed < 128 else 0
self.mem.a += self.mem.r3
def _exec_44(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r4) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r4.bits[4:], 2) > 15 else 0
# Convert A and R4 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r4_signed = int(self.mem.r4) - 256 if self.mem.r4 > 127 else int(self.mem.r4)
self.mem.ov = 1 if not -129 < a_signed + r4_signed < 128 else 0
self.mem.a += self.mem.r4
def _exec_45(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r5) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r5.bits[4:], 2) > 15 else 0
# Convert A and R5 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r5_signed = int(self.mem.r5) - 256 if self.mem.r5 > 127 else int(self.mem.r5)
self.mem.ov = 1 if not -129 < a_signed + r5_signed < 128 else 0
self.mem.a += self.mem.r5
def _exec_46(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r6) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r6.bits[4:], 2) > 15 else 0
# Convert A and R6 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r6_signed = int(self.mem.r6) - 256 if self.mem.r6 > 127 else int(self.mem.r6)
self.mem.ov = 1 if not -129 < a_signed + r6_signed < 128 else 0
self.mem.a += self.mem.r6
def _exec_47(self):
self.mem.c = 1 if int(self.mem.a) + int(self.mem.r7) > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r7.bits[4:], 2) > 15 else 0
# Convert A and R7 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r7_signed = int(self.mem.r7) - 256 if self.mem.r7 > 127 else int(self.mem.r7)
self.mem.ov = 1 if not -129 < a_signed + r7_signed < 128 else 0
self.mem.a += self.mem.r7
def _exec_48(self, bit, offset):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
if not self.mem[byte_no][7 - bit % 8]:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_49(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}001{addr11:08b}', 2)
def _exec_50(self):
high_order_byte = self.mem[self.mem.sp]
self.mem.sp -= 1
low_order_byte = self.mem[self.mem.sp]
self.mem.sp -= 1
self.pc = int(f'{high_order_byte:b}{low_order_byte:08b}', 2)
self.interrupt_stack.pop()
def _exec_51(self):
most_significant_bit = self.mem.a.bits[0]
self.mem.a = int(self.mem.a.bits[1:] + str(self.mem.c), 2)
self.mem.c = most_significant_bit
def _exec_52(self, immed):
c = 1 if int(self.mem.a) + immed + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(f'{immed:08b}'[4:], 2) + self.mem.c > 15 else 0
# Convert A and immed as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
immed_signed = immed - 256 if immed > 127 else immed
self.mem.ov = 1 if not -129 < a_signed + immed_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + immed
self.mem.c = c
def _exec_53(self, direct):
c = 1 if int(self.mem.a) + int(self.mem[direct]) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[direct].bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and mem[direct] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_direct_signed = int(self.mem[direct]) - 256 if self.mem[direct] > 127 else int(self.mem[direct])
self.mem.ov = 1 if not -129 < a_signed + mem_direct_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem[direct]
self.mem.c = c
def _exec_54(self):
c = 1 if int(self.mem.a) + int(self.mem[self.mem.r0]) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[self.mem.r0].bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and mem[r0] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r0_signed = int(self.mem[self.mem.r0]) - 256 if self.mem[self.mem.r0] > 127 else int(self.mem[self.mem.r0])
self.mem.ov = 1 if not -129 < a_signed + mem_r0_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem[self.mem.r0]
self.mem.c = c
def _exec_55(self):
c = 1 if int(self.mem.a) + int(self.mem[self.mem.r1]) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem[self.mem.r1].bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and mem[r1] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r1_signed = int(self.mem[self.mem.r1]) - 256 if self.mem[self.mem.r1] > 127 else int(self.mem[self.mem.r1])
self.mem.ov = 1 if not -129 < a_signed + mem_r1_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem[self.mem.r1]
self.mem.c = c
def _exec_56(self):
c = 1 if int(self.mem.a) + int(self.mem.r0) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r0.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R0 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r0_signed = int(self.mem.r0) - 256 if self.mem.r0 > 127 else int(self.mem.r0)
self.mem.ov = 1 if not -129 < a_signed + r0_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r0
self.mem.c = c
def _exec_57(self):
c = 1 if int(self.mem.a) + int(self.mem.r1) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r1.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R1 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r1_signed = int(self.mem.r1) - 256 if self.mem.r1 > 127 else int(self.mem.r1)
self.mem.ov = 1 if not -129 < a_signed + r1_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r1
self.mem.c = c
def _exec_58(self):
c = 1 if int(self.mem.a) + int(self.mem.r2) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r2.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R2 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r2_signed = int(self.mem.r2) - 256 if self.mem.r2 > 127 else int(self.mem.r2)
self.mem.ov = 1 if not -129 < a_signed + r2_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r2
self.mem.c = c
def _exec_59(self):
c = 1 if int(self.mem.a) + int(self.mem.r3) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r3.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R3 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r3_signed = int(self.mem.r3) - 256 if self.mem.r3 > 127 else int(self.mem.r3)
self.mem.ov = 1 if not -129 < a_signed + r3_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r3
self.mem.c = c
def _exec_60(self):
c = 1 if int(self.mem.a) + int(self.mem.r4) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r4.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R4 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r4_signed = int(self.mem.r4) - 256 if self.mem.r4 > 127 else int(self.mem.r4)
self.mem.ov = 1 if not -129 < a_signed + r4_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r4
self.mem.c = c
def _exec_61(self):
c = 1 if int(self.mem.a) + int(self.mem.r5) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r5.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R5 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r5_signed = int(self.mem.r5) - 256 if self.mem.r5 > 127 else int(self.mem.r5)
self.mem.ov = 1 if not -129 < a_signed + r5_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r5
self.mem.c = c
def _exec_62(self):
c = 1 if int(self.mem.a) + int(self.mem.r6) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r6.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R6 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r6_signed = int(self.mem.r6) - 256 if self.mem.r6 > 127 else int(self.mem.r6)
self.mem.ov = 1 if not -129 < a_signed + r6_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r6
self.mem.c = c
def _exec_63(self):
c = 1 if int(self.mem.a) + int(self.mem.r7) + self.mem.c > 255 else 0
self.mem.ac = 1 if int(self.mem.a.bits[4:], 2) + int(self.mem.r7.bits[4:], 2) + self.mem.c > 15 else 0
# Convert A and R7 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r7_signed = int(self.mem.r7) - 256 if self.mem.r7 > 127 else int(self.mem.r7)
self.mem.ov = 1 if not -129 < a_signed + r7_signed + self.mem.c < 128 else 0
self.mem.a += self.mem.c + self.mem.r7
self.mem.c = c
def _exec_64(self, offset):
if self.mem.c:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_65(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}010{addr11:08b}', 2)
def _exec_66(self, direct):
self.mem[direct] |= self.mem.a
def _exec_67(self, direct, immed):
self.mem[direct] |= immed
def _exec_68(self, immed):
self.mem.a |= immed
def _exec_69(self, direct):
self.mem.a |= self.mem[direct]
def _exec_70(self):
self.mem.a |= self.mem[self.mem.r0]
def _exec_71(self):
self.mem.a |= self.mem[self.mem.r1]
def _exec_72(self):
self.mem.a |= self.mem.r0
def _exec_73(self):
self.mem.a |= self.mem.r1
def _exec_74(self):
self.mem.a |= self.mem.r2
def _exec_75(self):
self.mem.a |= self.mem.r3
def _exec_76(self):
self.mem.a |= self.mem.r4
def _exec_77(self):
self.mem.a |= self.mem.r5
def _exec_78(self):
self.mem.a |= self.mem.r6
def _exec_79(self):
self.mem.a |= self.mem.r7
def _exec_80(self, offset):
if not self.mem.c:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_81(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}010{addr11:08b}', 2)
def _exec_82(self, direct):
self.mem[direct] &= self.mem.a
def _exec_83(self, direct, immed):
self.mem[direct] &= immed
def _exec_84(self, immed):
self.mem.a &= immed
def _exec_85(self, direct):
self.mem.a &= self.mem[direct]
def _exec_86(self):
self.mem.a &= self.mem[self.mem.r0]
def _exec_87(self):
self.mem.a &= self.mem[self.mem.r1]
def _exec_88(self):
self.mem.a &= self.mem.r0
def _exec_89(self):
self.mem.a &= self.mem.r1
def _exec_90(self):
self.mem.a &= self.mem.r2
def _exec_91(self):
self.mem.a &= self.mem.r3
def _exec_92(self):
self.mem.a &= self.mem.r4
def _exec_93(self):
self.mem.a &= self.mem.r5
def _exec_94(self):
self.mem.a &= self.mem.r6
def _exec_95(self):
self.mem.a &= self.mem.r7
def _exec_96(self, offset):
if self.mem.a == 0:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_97(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}011{addr11:08b}', 2)
def _exec_98(self, direct):
self.mem[direct] ^= self.mem.a
def _exec_99(self, direct, immed):
self.mem[direct] ^= immed
def _exec_100(self, immed):
self.mem.a ^= immed
def _exec_101(self, direct):
self.mem.a ^= self.mem[direct]
def _exec_102(self):
self.mem.a ^= self.mem[self.mem.r0]
def _exec_103(self):
self.mem.a ^= self.mem[self.mem.r1]
def _exec_104(self):
self.mem.a ^= self.mem.r0
def _exec_105(self):
self.mem.a ^= self.mem.r1
def _exec_106(self):
self.mem.a ^= self.mem.r2
def _exec_107(self):
self.mem.a ^= self.mem.r3
def _exec_108(self):
self.mem.a ^= self.mem.r4
def _exec_109(self):
self.mem.a ^= self.mem.r5
def _exec_110(self):
self.mem.a ^= self.mem.r6
def _exec_111(self):
self.mem.a ^= self.mem.r7
def _exec_112(self, offset):
if self.mem.a != 0:
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_113(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}011{addr11:08b}', 2)
def _exec_114(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem.c |= self.mem[byte_no][7 - bit % 8]
def _exec_115(self):
self.pc = self.mem.dptr + self.mem.a
def _exec_116(self, immed):
self.mem.a = immed
def _exec_117(self, direct, immed):
self.mem[direct] = immed
def _exec_118(self, immed):
self.mem[self.mem.r0] = immed
def _exec_119(self, immed):
self.mem[self.mem.r1] = immed
def _exec_120(self, immed):
self.mem.r0 = immed
def _exec_121(self, immed):
self.mem.r1 = immed
def _exec_122(self, immed):
self.mem.r2 = immed
def _exec_123(self, immed):
self.mem.r3 = immed
def _exec_124(self, immed):
self.mem.r4 = immed
def _exec_125(self, immed):
self.mem.r5 = immed
def _exec_126(self, immed):
self.mem.r6 = immed
def _exec_127(self, immed):
self.mem.r7 = immed
def _exec_128(self, offset):
# Convert from two's complement representation
self.pc += offset - 256 if offset > 127 else offset
def _exec_129(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}100{addr11:08b}', 2)
def _exec_130(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem.c &= self.mem[byte_no][7 - bit % 8]
def _exec_131(self):
self.mem.a = self.rom[int(self.pc + self.mem.a)]
def _exec_132(self):
self.mem.c = 0
self.mem.ov = 0 if self.mem.b else 1
self.mem.a, self.mem.b = divmod(self.mem.a, self.mem.b)
def _exec_133(self, src_direct, dest_direct):
self.mem[dest_direct] = self.mem[src_direct]
def _exec_134(self, direct):
self.mem[direct] = self.mem[self.mem.r0]
def _exec_135(self, direct):
self.mem[direct] = self.mem[self.mem.r1]
def _exec_136(self, direct):
self.mem[direct] = self.mem.r0
def _exec_137(self, direct):
self.mem[direct] = self.mem.r1
def _exec_138(self, direct):
self.mem[direct] = self.mem.r2
def _exec_139(self, direct):
self.mem[direct] = self.mem.r3
def _exec_140(self, direct):
self.mem[direct] = self.mem.r4
def _exec_141(self, direct):
self.mem[direct] = self.mem.r5
def _exec_142(self, direct):
self.mem[direct] = self.mem.r6
def _exec_143(self, direct):
self.mem[direct] = self.mem.r7
def _exec_144(self, immed):
self.mem.dptr = immed
def _exec_145(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}100{addr11:08b}', 2)
def _exec_146(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem[byte_no][7 - bit % 8] = self.mem.c
def _exec_147(self):
self.mem.a = self.rom[int(self.mem.dptr + self.mem.a)]
def _exec_148(self, immed):
c = 1 if immed > self.mem.a else 0
self.mem.ac = 1 if int(f'{immed:08b}'[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and immed as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
immed_signed = immed - 256 if immed > 127 else immed
self.mem.ov = 1 if not -129 < a_signed - immed_signed < 128 else 0
self.mem.a -= self.mem.c + immed
self.mem.c = c
def _exec_149(self, direct):
c = 1 if self.mem[direct] > self.mem.a else 0
self.mem.ac = 1 if int(self.mem[direct].bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and mem[direct] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_direct_signed = int(self.mem[direct]) - 256 if self.mem[direct] > 127 else int(self.mem[direct])
self.mem.ov = 1 if not -129 < a_signed - mem_direct_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem[direct]
self.mem.c = c
def _exec_150(self):
c = 1 if self.mem[self.mem.r0] > self.mem.a else 0
self.mem.ac = 1 if int(self.mem[self.mem.r0].bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and mem[r0] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r0_signed = int(self.mem[self.mem.r0]) - 256 if self.mem[self.mem.r0] > 127 else int(self.mem[self.mem.r0])
self.mem.ov = 1 if not -129 < a_signed - mem_r0_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem[self.mem.r0]
self.mem.c = c
def _exec_151(self):
c = 1 if self.mem[self.mem.r1] > self.mem.a else 0
self.mem.ac = 1 if int(self.mem[self.mem.r1].bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and mem[r1] as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
mem_r1_signed = int(self.mem[self.mem.r1]) - 256 if self.mem[self.mem.r1] > 127 else int(self.mem[self.mem.r1])
self.mem.ov = 1 if not -129 < a_signed - mem_r1_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem[self.mem.r1]
self.mem.c = c
def _exec_152(self):
c = 1 if self.mem.r0 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r0.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R0 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r0_signed = int(self.mem.r0) - 256 if self.mem.r0 > 127 else int(self.mem.r0)
self.mem.ov = 1 if not -129 < a_signed - r0_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r0
self.mem.c = c
def _exec_153(self):
c = 1 if self.mem.r1 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r1.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R1 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r1_signed = int(self.mem.r1) - 256 if self.mem.r1 > 127 else int(self.mem.r1)
self.mem.ov = 1 if not -129 < a_signed - r1_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r1
self.mem.c = c
def _exec_154(self):
c = 1 if self.mem.r2 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r2.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R2 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r2_signed = int(self.mem.r2) - 256 if self.mem.r2 > 127 else int(self.mem.r2)
self.mem.ov = 1 if not -129 < a_signed - r2_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r2
self.mem.c = c
def _exec_155(self):
c = 1 if self.mem.r3 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r3.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R3 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r3_signed = int(self.mem.r3) - 256 if self.mem.r3 > 127 else int(self.mem.r3)
self.mem.ov = 1 if not -129 < a_signed - r3_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r3
self.mem.c = c
def _exec_156(self):
c = 1 if self.mem.r4 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r4.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R4 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r4_signed = int(self.mem.r4) - 256 if self.mem.r4 > 127 else int(self.mem.r4)
self.mem.ov = 1 if not -129 < a_signed - r4_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r4
self.mem.c = c
def _exec_157(self):
c = 1 if self.mem.r5 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r5.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R5 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r5_signed = int(self.mem.r5) - 256 if self.mem.r5 > 127 else int(self.mem.r5)
self.mem.ov = 1 if not -129 < a_signed - r5_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r5
self.mem.c = c
def _exec_158(self):
c = 1 if self.mem.r6 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r6.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R6 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r6_signed = int(self.mem.r6) - 256 if self.mem.r6 > 127 else int(self.mem.r6)
self.mem.ov = 1 if not -129 < a_signed - r6_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r6
self.mem.c = c
def _exec_159(self):
c = 1 if self.mem.r7 > self.mem.a else 0
self.mem.ac = 1 if int(self.mem.r7.bits[4:], 2) > int(self.mem.a.bits[4:], 2) else 0
# Convert A and R7 as if they were two's complement numbers
a_signed = int(self.mem.a) - 256 if self.mem.a > 127 else int(self.mem.a)
r7_signed = int(self.mem.r7) - 256 if self.mem.r7 > 127 else int(self.mem.r7)
self.mem.ov = 1 if not -129 < a_signed - r7_signed < 128 else 0
self.mem.a -= self.mem.c + self.mem.r7
self.mem.c = c
def _exec_160(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem.c |= not self.mem[byte_no][7 - bit % 8]
def _exec_161(self, addr11):
self.pc = int(f'{self.pc.bits[:5]}101{addr11:08b}', 2)
def _exec_162(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem.c = self.mem[byte_no][7 - bit % 8]
def _exec_163(self):
self.mem.dptr += 1
def _exec_164(self):
self.mem.b, self.mem.a = self.mem.a * self.mem.b
self.mem.ov = 1 if self.mem.b else 0
self.mem.c = 0
def _exec_165(self):
return
def _exec_166(self, direct):
self.mem[self.mem.r0] = self.mem[direct]
def _exec_167(self, direct):
self.mem[self.mem.r1] = self.mem[direct]
def _exec_168(self, direct):
self.mem.r0 = self.mem[direct]
def _exec_169(self, direct):
self.mem.r1 = self.mem[direct]
def _exec_170(self, direct):
self.mem.r2 = self.mem[direct]
def _exec_171(self, direct):
self.mem.r3 = self.mem[direct]
def _exec_172(self, direct):
self.mem.r4 = self.mem[direct]
def _exec_173(self, direct):
self.mem.r5 = self.mem[direct]
def _exec_174(self, direct):
self.mem.r6 = self.mem[direct]
def _exec_175(self, direct):
self.mem.r7 = self.mem[direct]
def _exec_176(self, bit):
byte_no = bit // 8 + 32 if bit < 128 else bit // 8 * 8
self.mem.c &= not self.mem[byte_no][7 - bit % 8]
def _exec_177(self, addr11):
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[8:], 2)
self.mem.sp += 1
self.mem[self.mem.sp] = int(self.pc.bits[:8], 2)
self.pc = int(f'{self.pc.bits[:5]}101{addr11:08b}', 2)
def _exec_178(self, bit):