-
Notifications
You must be signed in to change notification settings - Fork 26
/
cpu.ino
1399 lines (1368 loc) · 41.8 KB
/
cpu.ino
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
#include <EEPROM.h>
#include "settings.h"
#define FIFO_MAX_SIZE 32
int16_t reg[16] __attribute__ ((aligned));
int16_t shadow_reg[16] __attribute__ ((aligned));
uint16_t pc = 0;
uint16_t interrupt = 0;
uint16_t dataName = 0;
uint32_t accum, saccum;
uint8_t carry = 0;
uint8_t zero = 0;
uint8_t negative = 0;
uint8_t redraw = 0;
int8_t color = 1;
int8_t bgcolor = 0;
int8_t keyPosition;
char s_buffer[7];
String loadedFileName;
char strBuf[16];
uint8_t strBufLength = 0;
uint8_t strBufPosition = 0;
struct Fifo_t {
uint16_t el[FIFO_MAX_SIZE];
uint8_t position_read;
uint8_t position_write;
uint8_t size;
};
struct Fifo_t interruptFifo;
#pragma GCC optimize ("-O2")
#pragma GCC push_options
void fifoClear(){
interruptFifo.position_read = 0;
interruptFifo.position_write = 0;
interruptFifo.size = 0;
for(int16_t i = 0; i < FIFO_MAX_SIZE; i++)
interruptFifo.el[i] = 0;
}
void pushInFifo(int16_t n){
if(interruptFifo.size < FIFO_MAX_SIZE){
interruptFifo.el[interruptFifo.position_write] = n;
interruptFifo.position_write++;
if(interruptFifo.position_write >= FIFO_MAX_SIZE)
interruptFifo.position_write = 0;
interruptFifo.size++;
}
}
uint16_t popOutFifo(){
uint16_t out = 0;
if(interruptFifo.size > 0){
interruptFifo.size--;
out = interruptFifo.el[interruptFifo.position_read];
interruptFifo.position_read++;
if(interruptFifo.position_read >= FIFO_MAX_SIZE)
interruptFifo.position_read = 0;
}
return out;
}
inline int16_t flagsTouint8_t(){
return (carry & 0x1) + ((zero & 0x1) << 1) + ((negative & 0x1) << 2);
}
inline void uint8_tToFlags(int16_t b){
carry = b & 0x1;
zero = (b & 0x2) >> 1;
negative = (b & 0x4) >> 2;
}
void setinterrupt(uint16_t adr, int16_t param){
if(interrupt == 0 && adr != 0){
saccum = accum;
shadow_reg[0] = flagsTouint8_t();
for(int8_t j = 1; j <= 15; j++){
shadow_reg[j] = reg[j];
}
reg[0] -= 2;
writeInt(reg[0], param);
reg[0] -= 2;
writeInt(reg[0], pc);
interrupt = pc;
pc = adr;
}
else{
pushInFifo(adr);
pushInFifo(param);
}
}
void setLoadedFileName(String s){
loadedFileName = s;
}
void cpuInit(){
for(uint8_t i = 1; i < 16; i++){
reg[i] = 0;
}
strBufPosition = 0;
interrupt = 0;
fifoClear();
display_init();
reg[0] = RAM_SIZE - 1;//stack pointer
clearScr(0);
color = 1;
bgcolor = 0;
setCharX(0);
setCharY(0);
pc = 0;
carry = 0;
zero = 0;
negative = 0;
accum = 0;
saccum = 0;
dataName = 0;
setRtttlPlay(0);
}
void debug(){
for(uint8_t i = 0; i < 16; i++){
Serial.print(" R");
Serial.print(i);
Serial.print(':');
Serial.print(reg[i]);
}
Serial.print(F(" OP:"));
Serial.print(readMem(pc),HEX);
Serial.print(F(" PC:"));
Serial.println(pc);
Serial.print(F("carry: "));
Serial.print(carry);
Serial.print(F(" zero: "));
Serial.print(zero);
Serial.print(F(" negative: "));
Serial.print(negative);
Serial.print(F(" interrupt: "));
Serial.print(interrupt);
Serial.print('/');
Serial.println(interruptFifo.size);
spriteDebug();
Serial.print(F("send to screen ms:"));
Serial.print(timeGpu);
Serial.print(F(" draw sprite ms:"));
Serial.print(timeSpr);
Serial.print(' ');
}
inline void setRedraw(){
redraw = 1;
}
inline void setFlags(int32_t n){
carry = (n > 0xffff) ? 1 : 0;
negative = n >> 31;
zero = (n == 0);
}
int16_t isqrt(int16_t n) {
int g = 0x8000;
int c = 0x8000;
for (;;) {
if (g*g > n) {
g ^= c;
}
c >>= 1;
if (c == 0) {
return g;
}
g |= c;
}
}
int16_t distancepp(int16_t x1, int16_t y1, int16_t x2, int16_t y2){
return isqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
inline void setDataName(uint16_t address){
dataName = address;
}
boolean testDataName(uint16_t pos){
uint8_t c;
if(dataName){
for(uint16_t i = 0; i < 12; i++){
c = EEPROM.read(pos + i);
if(c != readMem(dataName + i))
return false;
if(c == 0)
return true;
}
}
else{
for(uint16_t i = 0; i < 12; i++){
c = EEPROM.read(pos + i);
if(c != loadedFileName[i])
return false;
if(c == 0)
return true;
}
}
return true;
}
uint16_t findData(){
uint16_t pos;
uint8_t c;
pos = 0;
while(pos < EEPROM_SIZE){
c = EEPROM.read(pos);
if(c == 0 || c == 0xff)
return EEPROM_SIZE;
if(testDataName(pos + 1))
return pos;
pos += c;
}
return EEPROM_SIZE;
}
uint16_t findEndData(){
uint16_t pos;
uint8_t c;
pos = 0;
while(pos < EEPROM_SIZE){
c = EEPROM.read(pos);
if(c == 0 || c == 0xff)
return pos;
pos += c;
}
return EEPROM_SIZE;
}
uint16_t saveData(uint16_t arrayAddress, uint16_t count){
uint16_t i,pos;
uint8_t c;
if(count > 242)
count = 242;
pos = findData();
if(pos == EEPROM_SIZE)
pos = findEndData();
if((EEPROM_SIZE - pos) - 12 < count)
return (EEPROM_SIZE - pos) - 12;
c = EEPROM.read(pos);
if(c == 0 || c == 0xff){
EEPROM.write(pos, 12 + count);
for(i = 0; i < 12; i++)
EEPROM.write(pos + i + 1, readMem(dataName + i));
}
else if(c < count)
return c;
pos += 12;
for(i = 0; i < count; i++)
EEPROM.write(pos + i, readMem(arrayAddress + i));
EEPROM.commit();
return count;
}
uint16_t loadData(uint16_t arrayAddress){
uint16_t i,pos;
uint8_t c;
pos = findData();
if(pos == EEPROM_SIZE)
return 0;
c = EEPROM.read(pos) - 12;
pos += 12;
for(i = 0; i < c; i++)
writeMem(arrayAddress + i, EEPROM.read(pos + i));
return c;
}
int16_t fixed_sin(int x) {
//Bhaskara I's sine approximation sin(x°) = 4·x·(180−x)/(40500−x·(180−x))
char pos = 1; // positive - keeps an eye on the sign.
if (x < 0){
x = -x;
pos = !pos;
}
if (x >= 360)
x %= 360;
if (x > 180){
x -= 180;
pos = !pos;
}
int16_t nv = x * (180 - x);
int32_t s = (nv * 4 * (1 << fixed_res_bit))/(40500 - nv);
if (pos)
return (int16_t)s;
return (int16_t)-s;
}
inline int16_t fixed_cos(int16_t g){
return fixed_sin(g + 90);
}
void copyMem(uint16_t to_adr, uint16_t from_adr, uint16_t num_bytes) {
for (uint16_t i = 0; i < num_bytes; i++) {
writeMem(to_adr++, readMem(from_adr++));
}
}
void unpackingRLE(uint16_t to_adr, uint16_t a, uint16_t num_bytes) {
uint16_t i = 0;
uint16_t repeat = readMem(a);
a++;
uint16_t color = readMem(a);
while (i < num_bytes) {
if (repeat > 0x81) {
writeMem(to_adr++, color);
i++;
a++;
repeat--;
color = readMem(a);
} else if (repeat == 0x81) {
repeat = readMem(a);
a++;
color = readMem(a);
} else if (repeat > 0) {
writeMem(to_adr++, color);
i++;
repeat--;
} else if (repeat == 0) {
a++;
repeat = readMem(a);
a++;
color = readMem(a);
}
}
}
void unpackingLZ(uint16_t to_adr, uint16_t a, uint16_t num_bytes) {
uint16_t i = 0;
uint16_t j;
uint16_t length;
uint16_t position;
uint16_t point;
while (i < num_bytes) {
if ((readMem(a) & 128) == 0) {
length = ((readMem(a++) & 127) << 8) + readMem(a++);
for (j = 0; j < length; j++) {
writeMem(to_adr++, readMem(a++));
i++;
}
} else {
length = (readMem(a) & 127) >> 1;
position = (((readMem(a++) & 1) << 8) + readMem(a++));
point = to_adr - position;
for (j = 0; j < length; j++) {
writeMem(to_adr++, readMem(point + j));
i++;
}
}
}
}
#ifdef ESPBOY
void setLedColor(uint16_t r5g6b5){
uint8_t r,g,b;
r = ((((r5g6b5 >> 11) & 0x1F) * 527) + 23) >> 6;
g = ((((r5g6b5 >> 5) & 0x3F) * 259) + 33) >> 6;
b = (((r5g6b5 & 0x1F) * 527) + 23) >> 6;
myled.setRGB(r, g, b);
}
#endif
void cpuRun(uint16_t n){
uint8_t op1, op2;
uint16_t reg1, reg2, reg3;
uint16_t adr;
uint16_t j, z;
for(z = 0; z < n; z++){
op1 = readMem(pc++);
op2 = readMem(pc++);
switch(op1 >> 4){
case 0x0:
switch(op1){
case 0x01:
//LDI R,int 01 0R XXXX
reg1 = op2 & 0xf;
accum = readInt(pc);
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x02:
//LDI R,(R) 02 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = readInt(reg[reg2]);
reg[reg1] = (int16_t)accum;
break;
case 0x03:
//LDI R,(adr) 03 0R XXXX
reg1 = (op2 & 0xf);
accum = readInt(readInt(pc));
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x04:
//LDI R,(int+R) 04 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = readInt(reg[reg2] + readInt(pc));
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x05:
//STI (R),R 05 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
writeInt(reg[reg1],reg[reg2]);
break;
case 0x06:
if((op2 & 0x0f) == 0){
//STI (adr),R 06 R0 XXXX
reg1 = op2 >> 4;
writeInt(readInt(pc),reg[reg1]);
pc += 2;
}
else{
//STI (adr+R),R 06 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
writeInt(readInt(pc) + reg[reg1],reg[reg2]);
pc += 2;
}
break;
case 0x07:
//MOV R,R 07 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
reg[reg1] = reg[reg2];
break;
case 0x08:
//LDIAL R,(int+R*2) 08 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = readInt(reg[reg2] * 2 + readInt(pc));
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x09:
//STIAL (adr+R*2),R 09 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
writeInt(readInt(pc) + reg[reg1] * 2,reg[reg2]);
pc += 2;
break;
default:
pc++;
}
break;
case 0x1:
// LDC R,char 1R XX
reg1 = (op1 & 0xf);
accum = op2;
reg[reg1] = (int16_t)accum;
break;
case 0x2:
if(op1 == 0x20){
// LDC R,(R) 20 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = readMem(reg[reg2]);
reg[reg1] = (int16_t)accum;
}
else{
// LDC R,(R+R) 2R RR
reg1 = op1 & 0xf;
reg2 = op2 >> 4;
reg3 = op2 & 0xf;
accum = readMem(reg[reg2] + reg[reg3]);
reg[reg1] = (int16_t)accum;
}
break;
case 0x3:
switch(op1){
case 0x30:
// LDC R,(int+R)30 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = readMem(reg[reg2] + readInt(pc));
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x31:
// LDC R,(adr) 31 0R XXXX
reg1 = op2 & 0xf;
accum = readMem(readInt(pc));
reg[reg1] = (int16_t)accum;
pc += 2;
break;
case 0x32:
// STC (adr),R 32 0R XXXX
reg1 = op2 >> 4;
writeMem(readInt(pc),reg[reg1]);
pc += 2;
break;
case 0x33:
// STC (int+R),R33 RR XXXX
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
writeMem(readInt(pc) + reg[reg1],reg[reg2]);
pc += 2;
break;
}
break;
case 0x4:
if(op1 == 0x40){
// STC (R),R 40 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
writeMem(reg[reg1], reg[reg2]);
}
else{
// STC (R+R),R 4R RR
reg1 = op1 & 0xf;
reg2 = op2 >> 4;
reg3 = op2 & 0xf;
writeMem(reg[reg1] + reg[reg2], reg[reg3]);
}
break;
case 0x5:
switch(op1){
case 0x50:
//HLT 5000
clearSpriteScr();
noTone(SOUNDPIN);
fileList("/");
break;
case 0x51:
// STIMER R,R 51RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
timers[reg[reg1] & 0x7] = reg[reg2];
break;
case 0x52:
// GTIMER R 520R
reg1 = op2 & 0xf;
accum = timers[reg[reg1] & 0x7];
reg[reg1] = (int16_t)accum;
break;
case 0x53:
// SETLED R 530R
#ifdef ESPBOY
reg1 = op2 & 0xf;
setLedColor(reg[reg1]);
#endif
break;
case 0x54:
// LOADRT 540R
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
setRtttlAddress((uint16_t)reg[reg1]);
setRtttlLoop(reg[reg2]);
break;
case 0x55:
switch(op2){
// PLAYRT 5500
case 0x00:
setRtttlPlay(1);
break;
// PAUSERT 5501
case 0x01:
setRtttlPlay(0);
break;
// STOPRT 5502
case 0x02:
setRtttlPlay(2);
break;
}
break;
case 0x56:
// LOADRT 540R
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
addTone(reg[reg1], reg[reg2]);
break;
case 0x57:
if (op2 < 0x10){
// LDATA R 57 0R
reg2 = op2 & 0xf;
reg[reg2] = loadData(reg[reg2]);
}
else if (op2 < 0x20){
// NDATA R 57 1R
reg2 = op2 & 0xf;
setDataName(reg[reg2]);
}
break;
case 0x58:
// SDATA R,R 58 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
reg[reg1] = saveData(reg[reg1], reg[reg2]);
break;
case 0x59:
if (op2 < 0x10) {
// SERBEGIN R 59 0R
reg2 = op2 & 0xf;
reg[reg2] = serialBegin();
} else if (op2 < 0x20) {
// SERAVAIL R 59 1R
reg2 = op2 & 0xf;
reg[reg2] = serialAvailable();
} else if (op2 < 0x30) {
// SERREAD R 59 2R
reg2 = op2 & 0xf;
reg[reg2] = serialRead();
} else if (op2 < 0x40) {
// SERWRITE R 59 3R
reg2 = op2 & 0xf;
serialWrite(reg[reg2]);
}
break;
}
break;
case 0x6:
// LDI R,(R+R) 6R RR
reg1 = op1 & 0xf;
reg2 = op2 >> 4;
reg3 = op2 & 0xf;
accum = readInt(reg[reg2] + reg[reg3]);
reg[reg1] = (int16_t)accum;
break;
case 0x7:
// STI (R+R),R 7R RR
reg1 = op1 & 0xf;
reg2 = op2 >> 4;
reg3 = op2 & 0xf;
writeInt(reg[reg1] + reg[reg2], reg[reg3]);
break;
case 0x8:
switch(op1){
case 0x80:
// POP R 80 0R
reg1 = op2 & 0xf;
reg[reg1] = readInt(reg[0]);
reg[0] += 2;
break;
case 0x81:
// POPN R 81 0R
reg1 = op2 & 0xf;
for(j = reg1; j >= 1; j--){
reg[j] = readInt(reg[0]);
reg[0] += 2;
}
break;
case 0x82:
// PUSH R 82 0R
reg1 = op2 & 0xf;
reg[0] -= 2;
writeInt(reg[0], reg[reg1]);
break;
case 0x83:
// PUSHN R 83 0R
reg1 = op2 & 0xf;
for(j = 1; j <= reg1; j++){
reg[0] -= 2;
writeInt(reg[0], reg[j]);
}
break;
}
break;
case 0x9:
switch(op1){
case 0x90:
// JMP adr 90 00 XXXX
pc = readInt(pc);
break;
case 0x91:
// JNZ adr 91 00 XXXX
setFlags(accum);
if(zero == 0)
pc = readInt(pc);
else
pc += 2;
break;
case 0x92:
// JZ adr 92 00 XXXX
setFlags(accum);
if(zero != 0)
pc = readInt(pc);
else
pc += 2;
break;
case 0x93:
// JNP adr 93 00 XXXX
setFlags(accum);
if(negative == 1)
pc = readInt(pc);
else
pc += 2;
break;
case 0x94:
// JP adr 94 00 XXXX
setFlags(accum);
if(negative != 1)
pc = readInt(pc);
else
pc += 2;
break;
case 0x95:
// JNC adr 95 00 XXXX
setFlags(accum);
if(carry != 1)
pc = readInt(pc);
else
pc += 2;
break;
case 0x96:
// JC adr 96 00 XXXX
setFlags(accum);
if(carry == 1)
pc = readInt(pc);
else
pc += 2;
break;
case 0x97:
// JZR R,adr 97 0R XXXX
setFlags(accum);
reg1 = op2 & 0xf;
if(reg[reg1] == 0)
pc = readInt(pc);
else
pc += 2;
break;
case 0x98:
// JNZR R,adr 98 0R XXXX
setFlags(accum);
reg1 = op2 & 0xf;
if(reg[reg1] != 0)
pc = readInt(pc);
else
pc += 2;
break;
case 0x99:
// CALL adr 99 00 XXXX
reg[0] -= 2;
writeInt(reg[0], pc + 2);
pc = readInt(pc);
break;
case 0x9A:
// RET 9A 00
if(!interrupt){
pc = readInt(reg[0]);
reg[0] += 2;
}
else{
pc = readInt(reg[0]);
if(pc == interrupt){
reg[0] += 4;
accum = saccum;
for(int8_t j = 15; j >= 1; j--){
reg[j] = shadow_reg[j];
}
uint8_tToFlags(shadow_reg[0]);
interrupt = 0;
if(interruptFifo.size > 0)
setinterrupt(popOutFifo(), popOutFifo());
}
else
reg[0] += 2;
}
break;
}
break;
case 0xA:
switch(op1){
case 0xA0:
// ADD R,R A0 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] + reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xA1:
// ADC R,R A1 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] + reg[reg2] + carry;
reg[reg1] = (int16_t)accum;
break;
case 0xA2:
// SUB R,R A2 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] - reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xA3:
// SBC R,R A3 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] - reg[reg2] - carry;
reg[reg1] = (int16_t)accum;
break;
case 0xA4:
// MUL R,R A4 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] * reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xA5:
// DIV R,R A5 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
if(reg[reg2] != 0){
accum = reg[reg1] / reg[reg2];
reg[reg2] = reg[reg1] % reg[reg2];
}
else{
accum = 0;//error
reg[reg2] = 0;
}
reg[reg1] = (int16_t)accum;
break;
case 0xA6:
// AND R,R A6 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] & reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xA7:
// OR R,R A7 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] | reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xA8:
if(op2 == 0x10){
// INC adr A8 10 XXXX
reg1 = op2 & 0xf;
accum = readInt(readInt(pc)) + 1;
writeInt(readInt(pc), (int16_t)accum);
pc += 2;
}
else if(op2 > 0x10){
// INC R,n A8 nR
reg1 = op2 & 0xf;
accum = reg[reg1] + (op2 >> 4);
reg[reg1] = (int16_t)accum;
}
else{
// INC R A8 0R
reg1 = op2 & 0xf;
accum = reg[reg1] + 1;
reg[reg1] = (int16_t)accum;
}
break;
case 0xA9:
if(op2 == 0x10){
// DEC adr A9 10 XXXX
reg1 = op2 & 0xf;
accum = readInt(readInt(pc)) - 1;
writeInt(readInt(pc), (int16_t)accum);
pc += 2;
}
else if(op2 > 0x10){
// DEC R,n A9 nR
reg1 = op2 & 0xf;
accum = reg[reg1] - (op2 >> 4);
reg[reg1] = (int16_t)accum;
}
else{
// DEC R A9 0R
reg1 = op2 & 0xf;
accum = reg[reg1] - 1;
reg[reg1] = (int16_t)accum;
}
break;
case 0xAA:
// XOR R,R AA RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] ^ reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xAB:
// SHL R,R AB RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] << reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xAC:
// SHR R,R AC RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] >> reg[reg2];
reg[reg1] = (int16_t)accum;
break;
case 0xAD:
reg1 = op2 & 0xf;
reg2 = op2 & 0xf0;
switch(reg2){
// RAND R AD 0R
case 0x00:
accum = random(0, reg[reg1] + 1);
reg[reg1] = (int16_t)accum;
break;
// SQRT R AD 1R
case 0x10:
accum = isqrt(reg[reg1]);
reg[reg1] = (int16_t)accum;
break;
// NOT R AD 2R
case 0x20:
accum = (~reg[reg1]);
reg[reg1] = (int16_t)accum;
break;
}
break;
case 0xAE:
// ANDL R,R AE RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = (reg[reg1] != 0 && reg[reg2] != 0) ? 1 : 0;
reg[reg1] = (int16_t)accum;
break;
case 0xAF:
// ORL R,R AF RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = (reg[reg1] != 0 || reg[reg2] != 0) ? 1 : 0;
reg[reg1] = (int16_t)accum;
break;
}
break;
case 0xB:
//CMP R,CHR BR XX
reg1 = (op1 & 0x0f);
accum = reg[reg1] - op2;
break;
case 0xC:
switch(op1){
case 0xC0:
//CMP R,INT C0 R0 XXXX
reg1 = op2 >> 4;
accum = reg[reg1] - readInt(pc);
pc += 2;
break;
case 0xC1:
//CMP R,R C1 RR
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
accum = reg[reg1] - reg[reg2];
break;
case 0xC2:
//LDF R,F C2 RF
reg1 = op2 >> 4;
reg2 = op2 & 0xf;
setFlags(accum);
switch(reg2){
case 0:
reg[reg1] = carry;
break;
case 1:
reg[reg1] = zero;
break;
case 2:
reg[reg1] = negative;
break;
case 3: //pozitive
if(negative == 0 && zero == 0)
reg[reg1] = 1;
else
reg[reg1] = 0;
break;
case 4: //not pozitive
if(negative == 0 && zero == 0)
reg[reg1] = 0;
else
reg[reg1] = 1;
break;
case 5:
reg[reg1] = 1 - zero;
break;
case 6:
reg[reg1] = redraw;
redraw = 0;
break;
default:
reg[reg1] = 0;
}
break;
case 0xc3:
reg1 = op2 & 0x0f;
reg2 = op2 & 0xf0;
switch(reg2){
// ITOF R C3 0R
case 0x00:
reg[reg1] = reg[reg1] * (1 << fixed_res_bit);
break;