forked from EtchedPixels/EmulatorKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e6809.c
2651 lines (2368 loc) · 46.8 KB
/
e6809.c
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 VecX : Valavan Manohararajah */
#include <stdio.h>
#include "e6809.h"
/* code assumptions:
* - it is assumed that an 'int' is at least 16 bits long.
* - a 16-bit register has valid bits only in the lower 16 bits and an
* 8-bit register has valid bits only in the lower 8 bits. the upper
* may contain garbage!
* - all reading functions are assumed to return the requested data in
* the lower bits with the unused upper bits all set to zero.
*/
#define einline __inline
enum {
FLAG_E = 0x80,
FLAG_F = 0x40,
FLAG_H = 0x20,
FLAG_I = 0x10,
FLAG_N = 0x08,
FLAG_Z = 0x04,
FLAG_V = 0x02,
FLAG_C = 0x01,
IRQ_NORMAL = 0,
IRQ_SYNC = 1,
IRQ_CWAI = 2
};
/* index registers */
static unsigned reg_x;
static unsigned reg_y;
/* user stack pointer */
static unsigned reg_u;
/* hardware stack pointer */
static unsigned reg_s;
/* program counter */
static unsigned reg_pc;
/* accumulators */
static unsigned reg_a;
static unsigned reg_b;
/* direct page register */
static unsigned reg_dp;
/* condition codes */
static unsigned reg_cc;
/* flag to see if interrupts should be handled (sync/cwai). */
static unsigned irq_status;
static unsigned *rptr_xyus[4] = {
®_x,
®_y,
®_u,
®_s
};
static unsigned trace_cpu;
/* user defined read and write functions */
unsigned char e6809_read8(unsigned address);
void e6809_write8(unsigned address, unsigned char data);
/* obtain a particular condition code. returns 0 or 1. */
static einline unsigned get_cc (unsigned flag)
{
return (reg_cc / flag) & 1;
}
/* set a particular condition code to either 0 or 1.
* value parameter must be either 0 or 1.
*/
static einline void set_cc (unsigned flag, unsigned value)
{
reg_cc &= ~flag;
reg_cc |= value * flag;
}
/* test carry */
static einline unsigned test_c (unsigned i0, unsigned i1,
unsigned r, unsigned sub)
{
unsigned flag;
flag = (i0 | i1) & ~r; /* one of the inputs is 1 and output is 0 */
flag |= (i0 & i1); /* both inputs are 1 */
flag = (flag >> 7) & 1;
flag ^= sub; /* on a sub, carry is opposite the carry of an add */
return flag;
}
/* test negative */
static einline unsigned test_n (unsigned r)
{
return (r >> 7) & 1;
}
/* test for zero in lower 8 bits */
static einline unsigned test_z8 (unsigned r)
{
unsigned flag;
flag = ~r;
flag = (flag >> 4) & (flag & 0xf);
flag = (flag >> 2) & (flag & 0x3);
flag = (flag >> 1) & (flag & 0x1);
return flag;
}
/* test for zero in lower 16 bits */
static einline unsigned test_z16 (unsigned r)
{
unsigned flag;
flag = ~r;
flag = (flag >> 8) & (flag & 0xff);
flag = (flag >> 4) & (flag & 0xf);
flag = (flag >> 2) & (flag & 0x3);
flag = (flag >> 1) & (flag & 0x1);
return flag;
}
/* overflow is set whenever the sign bits of the inputs are the same
* but the sign bit of the result is not same as the sign bits of the
* inputs.
*/
static einline unsigned test_v (unsigned i0, unsigned i1, unsigned r)
{
unsigned flag;
flag = ~(i0 ^ i1); /* input sign bits are the same */
flag &= (i0 ^ r); /* input sign and output sign not same */
flag = (flag >> 7) & 1;
return flag;
}
static einline unsigned get_reg_d (void)
{
return (reg_a << 8) | (reg_b & 0xff);
}
static einline void set_reg_d (unsigned value)
{
reg_a = value >> 8;
reg_b = value;
}
/* read a byte ... the returned value has the lower 8-bits set to the byte
* while the upper bits are all zero.
*/
static einline unsigned read8 (unsigned address)
{
return e6809_read8(address & 0xffff);
}
/* write a byte ... only the lower 8-bits of the unsigned data
* is written. the upper bits are ignored.
*/
static einline void write8 (unsigned address, unsigned data)
{
e6809_write8(address & 0xffff, (unsigned char) data);
}
static einline unsigned read16 (unsigned address)
{
unsigned datahi, datalo;
datahi = read8 (address);
datalo = read8 (address + 1);
return (datahi << 8) | datalo;
}
static einline void write16 (unsigned address, unsigned data)
{
write8 (address, data >> 8);
write8 (address + 1, data);
}
static einline void push8 (unsigned *sp, unsigned data)
{
(*sp)--;
write8 (*sp, data);
}
static einline unsigned pull8 (unsigned *sp)
{
unsigned data;
data = read8 (*sp);
(*sp)++;
return data;
}
static einline void push16 (unsigned *sp, unsigned data)
{
push8 (sp, data);
push8 (sp, data >> 8);
}
static einline unsigned pull16 (unsigned *sp)
{
unsigned datahi, datalo;
datahi = pull8 (sp);
datalo = pull8 (sp);
return (datahi << 8) | datalo;
}
/* read a byte from the address pointed to by the pc */
static einline unsigned pc_read8 (void)
{
unsigned data;
data = read8 (reg_pc);
reg_pc++;
return data;
}
/* read a word from the address pointed to by the pc */
static einline unsigned pc_read16 (void)
{
unsigned data;
data = read16 (reg_pc);
reg_pc += 2;
return data;
}
/* sign extend an 8-bit quantity into a 16-bit quantity */
static einline unsigned sign_extend (unsigned data)
{
return (~(data & 0x80) + 1) | (data & 0xff);
}
/* direct addressing, upper byte of the address comes from
* the direct page register, and the lower byte comes from the
* instruction itself.
*/
static einline unsigned ea_direct (void)
{
return (reg_dp << 8) | pc_read8 ();
}
/* extended addressing, address is obtained from 2 bytes following
* the instruction.
*/
static einline unsigned ea_extended (void)
{
return pc_read16 ();
}
/* indexed addressing */
static einline unsigned ea_indexed (unsigned *cycles)
{
unsigned r, op, ea;
/* post byte */
op = pc_read8 ();
r = (op >> 5) & 3;
switch (op) {
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f:
case 0x20: case 0x21: case 0x22: case 0x23:
case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2a: case 0x2b:
case 0x2c: case 0x2d: case 0x2e: case 0x2f:
case 0x40: case 0x41: case 0x42: case 0x43:
case 0x44: case 0x45: case 0x46: case 0x47:
case 0x48: case 0x49: case 0x4a: case 0x4b:
case 0x4c: case 0x4d: case 0x4e: case 0x4f:
case 0x60: case 0x61: case 0x62: case 0x63:
case 0x64: case 0x65: case 0x66: case 0x67:
case 0x68: case 0x69: case 0x6a: case 0x6b:
case 0x6c: case 0x6d: case 0x6e: case 0x6f:
/* R, +[0, 15] */
ea = *rptr_xyus[r] + (op & 0xf);
(*cycles)++;
break;
case 0x10: case 0x11: case 0x12: case 0x13:
case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1a: case 0x1b:
case 0x1c: case 0x1d: case 0x1e: case 0x1f:
case 0x30: case 0x31: case 0x32: case 0x33:
case 0x34: case 0x35: case 0x36: case 0x37:
case 0x38: case 0x39: case 0x3a: case 0x3b:
case 0x3c: case 0x3d: case 0x3e: case 0x3f:
case 0x50: case 0x51: case 0x52: case 0x53:
case 0x54: case 0x55: case 0x56: case 0x57:
case 0x58: case 0x59: case 0x5a: case 0x5b:
case 0x5c: case 0x5d: case 0x5e: case 0x5f:
case 0x70: case 0x71: case 0x72: case 0x73:
case 0x74: case 0x75: case 0x76: case 0x77:
case 0x78: case 0x79: case 0x7a: case 0x7b:
case 0x7c: case 0x7d: case 0x7e: case 0x7f:
/* R, +[-16, -1] */
ea = *rptr_xyus[r] + (op & 0xf) - 0x10;
(*cycles)++;
break;
case 0x80: case 0x81:
case 0xa0: case 0xa1:
case 0xc0: case 0xc1:
case 0xe0: case 0xe1:
/* ,R+ / ,R++ */
ea = *rptr_xyus[r];
*rptr_xyus[r] += 1 + (op & 1);
*cycles += 2 + (op & 1);
break;
case 0x90: case 0x91:
case 0xb0: case 0xb1:
case 0xd0: case 0xd1:
case 0xf0: case 0xf1:
/* [,R+] ??? / [,R++] */
ea = read16 (*rptr_xyus[r]);
*rptr_xyus[r] += 1 + (op & 1);
*cycles += 5 + (op & 1);
break;
case 0x82: case 0x83:
case 0xa2: case 0xa3:
case 0xc2: case 0xc3:
case 0xe2: case 0xe3:
/* ,-R / ,--R */
*rptr_xyus[r] -= 1 + (op & 1);
ea = *rptr_xyus[r];
*cycles += 2 + (op & 1);
break;
case 0x92: case 0x93:
case 0xb2: case 0xb3:
case 0xd2: case 0xd3:
case 0xf2: case 0xf3:
/* [,-R] ??? / [,--R] */
*rptr_xyus[r] -= 1 + (op & 1);
ea = read16 (*rptr_xyus[r]);
*cycles += 5 + (op & 1);
break;
case 0x84: case 0xa4:
case 0xc4: case 0xe4:
/* ,R */
ea = *rptr_xyus[r];
break;
case 0x94: case 0xb4:
case 0xd4: case 0xf4:
/* [,R] */
ea = read16 (*rptr_xyus[r]);
*cycles += 3;
break;
case 0x85: case 0xa5:
case 0xc5: case 0xe5:
/* B,R */
ea = *rptr_xyus[r] + sign_extend (reg_b);
*cycles += 1;
break;
case 0x95: case 0xb5:
case 0xd5: case 0xf5:
/* [B,R] */
ea = read16 (*rptr_xyus[r] + sign_extend (reg_b));
*cycles += 4;
break;
case 0x86: case 0xa6:
case 0xc6: case 0xe6:
/* A,R */
ea = *rptr_xyus[r] + sign_extend (reg_a);
*cycles += 1;
break;
case 0x96: case 0xb6:
case 0xd6: case 0xf6:
/* [A,R] */
ea = read16 (*rptr_xyus[r] + sign_extend (reg_a));
*cycles += 4;
break;
case 0x88: case 0xa8:
case 0xc8: case 0xe8:
/* byte,R */
ea = *rptr_xyus[r] + sign_extend (pc_read8 ());
*cycles += 1;
break;
case 0x98: case 0xb8:
case 0xd8: case 0xf8:
/* [byte,R] */
ea = read16 (*rptr_xyus[r] + sign_extend (pc_read8 ()));
*cycles += 4;
break;
case 0x89: case 0xa9:
case 0xc9: case 0xe9:
/* word,R */
ea = *rptr_xyus[r] + pc_read16 ();
*cycles += 4;
break;
case 0x99: case 0xb9:
case 0xd9: case 0xf9:
/* [word,R] */
ea = read16 (*rptr_xyus[r] + pc_read16 ());
*cycles += 7;
break;
case 0x8b: case 0xab:
case 0xcb: case 0xeb:
/* D,R */
ea = *rptr_xyus[r] + get_reg_d ();
*cycles += 4;
break;
case 0x9b: case 0xbb:
case 0xdb: case 0xfb:
/* [D,R] */
ea = read16 (*rptr_xyus[r] + get_reg_d ());
*cycles += 7;
break;
case 0x8c: case 0xac:
case 0xcc: case 0xec:
/* byte, PC */
r = sign_extend (pc_read8 ());
ea = reg_pc + r;
*cycles += 1;
break;
case 0x9c: case 0xbc:
case 0xdc: case 0xfc:
/* [byte, PC] */
r = sign_extend (pc_read8 ());
ea = read16 (reg_pc + r);
*cycles += 4;
break;
case 0x8d: case 0xad:
case 0xcd: case 0xed:
/* word, PC */
r = pc_read16 ();
ea = reg_pc + r;
*cycles += 5;
break;
case 0x9d: case 0xbd:
case 0xdd: case 0xfd:
/* [word, PC] */
r = pc_read16 ();
ea = read16 (reg_pc + r);
*cycles += 8;
break;
case 0x9f:
/* [address] */
ea = read16 (pc_read16 ());
*cycles += 5;
break;
default:
printf ("undefined post-byte\n");
break;
}
return ea;
}
/* instruction: neg
* essentially (0 - data).
*/
static einline unsigned inst_neg (unsigned data)
{
unsigned i0, i1, r;
i0 = 0;
i1 = ~data;
r = i0 + i1 + 1;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 1));
return r;
}
/* instruction: com */
static einline unsigned inst_com (unsigned data)
{
unsigned r;
r = ~data;
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, 0);
set_cc (FLAG_C, 1);
return r;
}
/* instruction: lsr
* cannot be faked as an add or substract.
*/
static einline unsigned inst_lsr (unsigned data)
{
unsigned r;
r = (data >> 1) & 0x7f;
set_cc (FLAG_N, 0);
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_C, data & 1);
return r;
}
/* instruction: ror
* cannot be faked as an add or substract.
*/
static einline unsigned inst_ror (unsigned data)
{
unsigned r, c;
c = get_cc (FLAG_C);
r = ((data >> 1) & 0x7f) | (c << 7);
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_C, data & 1);
return r;
}
/* instruction: asr
* cannot be faked as an add or substract.
*/
static einline unsigned inst_asr (unsigned data)
{
unsigned r;
r = ((data >> 1) & 0x7f) | (data & 0x80);
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_C, data & 1);
return r;
}
/* instruction: asl
* essentially (data + data). simple addition.
*/
static einline unsigned inst_asl (unsigned data)
{
unsigned i0, i1, r;
i0 = data;
i1 = data;
r = i0 + i1;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 0));
return r;
}
/* instruction: rol
* essentially (data + data + carry). addition with carry.
*/
static einline unsigned inst_rol (unsigned data)
{
unsigned i0, i1, c, r;
i0 = data;
i1 = data;
c = get_cc (FLAG_C);
r = i0 + i1 + c;
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 0));
return r;
}
/* instruction: dec
* essentially (data - 1).
*/
static einline unsigned inst_dec (unsigned data)
{
unsigned i0, i1, r;
i0 = data;
i1 = 0xff;
r = i0 + i1;
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
return r;
}
/* instruction: inc
* essentially (data + 1).
*/
static einline unsigned inst_inc (unsigned data)
{
unsigned i0, i1, r;
i0 = data;
i1 = 1;
r = i0 + i1;
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
return r;
}
/* instruction: tst */
static einline void inst_tst8 (unsigned data)
{
set_cc (FLAG_N, test_n (data));
set_cc (FLAG_Z, test_z8 (data));
set_cc (FLAG_V, 0);
}
static einline void inst_tst16 (unsigned data)
{
set_cc (FLAG_N, test_n (data >> 8));
set_cc (FLAG_Z, test_z16 (data));
set_cc (FLAG_V, 0);
}
/* instruction: clr */
static einline void inst_clr (void)
{
set_cc (FLAG_N, 0);
set_cc (FLAG_Z, 1);
set_cc (FLAG_V, 0);
set_cc (FLAG_C, 0);
}
/* instruction: suba/subb */
static einline unsigned inst_sub8 (unsigned data0, unsigned data1)
{
unsigned i0, i1, r;
i0 = data0;
i1 = ~data1;
r = i0 + i1 + 1;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 1));
return r;
}
/* instruction: sbca/sbcb/cmpa/cmpb.
* only 8-bit version, 16-bit version not needed.
*/
static einline unsigned inst_sbc (unsigned data0, unsigned data1)
{
unsigned i0, i1, c, r;
i0 = data0;
i1 = ~data1;
c = 1 - get_cc (FLAG_C);
r = i0 + i1 + c;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 1));
return r;
}
/* instruction: anda/andb/bita/bitb.
* only 8-bit version, 16-bit version not needed.
*/
static einline unsigned inst_and (unsigned data0, unsigned data1)
{
unsigned r;
r = data0 & data1;
inst_tst8 (r);
return r;
}
/* instruction: eora/eorb.
* only 8-bit version, 16-bit version not needed.
*/
static einline unsigned inst_eor (unsigned data0, unsigned data1)
{
unsigned r;
r = data0 ^ data1;
inst_tst8 (r);
return r;
}
/* instruction: adca/adcb
* only 8-bit version, 16-bit version not needed.
*/
static einline unsigned inst_adc (unsigned data0, unsigned data1)
{
unsigned i0, i1, c, r;
i0 = data0;
i1 = data1;
c = get_cc (FLAG_C);
r = i0 + i1 + c;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 0));
return r;
}
/* instruction: ora/orb.
* only 8-bit version, 16-bit version not needed.
*/
static einline unsigned inst_or (unsigned data0, unsigned data1)
{
unsigned r;
r = data0 | data1;
inst_tst8 (r);
return r;
}
/* instruction: adda/addb */
static einline unsigned inst_add8 (unsigned data0, unsigned data1)
{
unsigned i0, i1, r;
i0 = data0;
i1 = data1;
r = i0 + i1;
set_cc (FLAG_H, test_c (i0 << 4, i1 << 4, r << 4, 0));
set_cc (FLAG_N, test_n (r));
set_cc (FLAG_Z, test_z8 (r));
set_cc (FLAG_V, test_v (i0, i1, r));
set_cc (FLAG_C, test_c (i0, i1, r, 0));
return r;
}
/* instruction: addd */
static einline unsigned inst_add16 (unsigned data0, unsigned data1)
{
unsigned i0, i1, r;
i0 = data0;
i1 = data1;
r = i0 + i1;
set_cc (FLAG_N, test_n (r >> 8));
set_cc (FLAG_Z, test_z16 (r));
set_cc (FLAG_V, test_v (i0 >> 8, i1 >> 8, r >> 8));
set_cc (FLAG_C, test_c (i0 >> 8, i1 >> 8, r >> 8, 0));
return r;
}
/* instruction: subd */
static einline unsigned inst_sub16 (unsigned data0, unsigned data1)
{
unsigned i0, i1, r;
i0 = data0;
i1 = ~data1;
r = i0 + i1 + 1;
set_cc (FLAG_N, test_n (r >> 8));
set_cc (FLAG_Z, test_z16 (r));
set_cc (FLAG_V, test_v (i0 >> 8, i1 >> 8, r >> 8));
set_cc (FLAG_C, test_c (i0 >> 8, i1 >> 8, r >> 8, 1));
return r;
}
/* instruction: 8-bit offset branch */
static einline void inst_bra8 (unsigned test, unsigned op, unsigned *cycles)
{
unsigned offset, mask;
offset = pc_read8 ();
/* trying to avoid an if statement */
mask = (test ^ (op & 1)) - 1; /* 0xffff when taken, 0 when not taken */
reg_pc += sign_extend (offset) & mask;
*cycles += 3;
}
/* instruction: 16-bit offset branch */
static einline void inst_bra16 (unsigned test, unsigned op, unsigned *cycles)
{
unsigned offset, mask;
offset = pc_read16 ();
/* trying to avoid an if statement */
mask = (test ^ (op & 1)) - 1; /* 0xffff when taken, 0 when not taken */
reg_pc += offset & mask;
*cycles += 5 - mask;
}
/* instruction: pshs/pshu */
static einline void inst_psh (unsigned op, unsigned *sp,
unsigned data, unsigned *cycles)
{
if (op & 0x80) {
push16 (sp, reg_pc);
*cycles += 2;
}
if (op & 0x40) {
/* either s or u */
push16 (sp, data);
*cycles += 2;
}
if (op & 0x20) {
push16 (sp, reg_y);
*cycles += 2;
}
if (op & 0x10) {
push16 (sp, reg_x);
*cycles += 2;
}
if (op & 0x08) {
push8 (sp, reg_dp);
*cycles += 1;
}
if (op & 0x04) {
push8 (sp, reg_b);
*cycles += 1;
}
if (op & 0x02) {
push8 (sp, reg_a);
*cycles += 1;
}
if (op & 0x01) {
push8 (sp, reg_cc);
*cycles += 1;
}
}
/* instruction: puls/pulu */
static einline void inst_pul (unsigned op, unsigned *sp, unsigned *osp,
unsigned *cycles)
{
if (op & 0x01) {
reg_cc = pull8 (sp);
*cycles += 1;
}
if (op & 0x02) {
reg_a = pull8 (sp);
*cycles += 1;
}
if (op & 0x04) {
reg_b = pull8 (sp);
*cycles += 1;
}
if (op & 0x08) {
reg_dp = pull8 (sp);
*cycles += 1;
}
if (op & 0x10) {
reg_x = pull16 (sp);
*cycles += 2;
}
if (op & 0x20) {
reg_y = pull16 (sp);
*cycles += 2;
}
if (op & 0x40) {
/* either s or u */
*osp = pull16 (sp);
*cycles += 2;
}
if (op & 0x80) {
reg_pc = pull16 (sp);
*cycles += 2;
}
}
static einline unsigned exgtfr_read (unsigned reg)
{