-
Notifications
You must be signed in to change notification settings - Fork 0
/
C6502mak.h
854 lines (768 loc) · 14.4 KB
/
C6502mak.h
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
//
// Copyright (c) 2004 K. Wilkins
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//////////////////////////////////////////////////////////////////////////////
// Handy - An Atari Lynx Emulator //
// Copyright (c) 1996,1997 //
// K. Wilkins //
//////////////////////////////////////////////////////////////////////////////
// 65C02 Macro definitions //
//////////////////////////////////////////////////////////////////////////////
// //
// This file contains all of the required address mode and operand //
// macro definitions for the 65C02 emulation //
// //
// K. Wilkins //
// August 1997 //
// //
//////////////////////////////////////////////////////////////////////////////
// Revision History: //
// ----------------- //
// //
// 01Aug1997 KW Document header added & class documented. //
// //
//////////////////////////////////////////////////////////////////////////////
//
// Addressing mode decoding
//
#define xIMMEDIATE() {mOperand=mPC;mPC++;}
#define xABSOLUTE() {mOperand=CPU_PEEKW(mPC);mPC+=2;}
#define xZEROPAGE() {mOperand=CPU_PEEK(mPC);mPC++;}
#define xZEROPAGE_X() {mOperand=CPU_PEEK(mPC)+mX;mPC++;mOperand&=0xff;}
#define xZEROPAGE_Y() {mOperand=CPU_PEEK(mPC)+mY;mPC++;mOperand&=0xff;}
#define xABSOLUTE_X() {mOperand=CPU_PEEKW(mPC);mPC+=2;mOperand+=mX;mOperand&=0xffff;}
#define xABSOLUTE_Y() {mOperand=CPU_PEEKW(mPC);mPC+=2;mOperand+=mY;mOperand&=0xffff;}
#define xINDIRECT_ABSOLUTE_X() {mOperand=CPU_PEEKW(mPC);mPC+=2;mOperand+=mX;mOperand&=0xffff;mOperand=CPU_PEEKW(mOperand);}
#define xRELATIVE() {mOperand=CPU_PEEK(mPC);mPC++;mOperand=(mPC+mOperand)&0xffff;}
#define xINDIRECT_X() {mOperand=CPU_PEEK(mPC);mPC++;mOperand=mOperand+mX;mOperand&=0x00ff;mOperand=CPU_PEEKW(mOperand);}
#define xINDIRECT_Y() {mOperand=CPU_PEEK(mPC);mPC++;mOperand=CPU_PEEKW(mOperand);mOperand=mOperand+mY;mOperand&=0xffff;}
#define xINDIRECT_ABSOLUTE() {mOperand=CPU_PEEKW(mPC);mPC+=2;mOperand=CPU_PEEKW(mOperand);}
#define xINDIRECT() {mOperand=CPU_PEEK(mPC);mPC++;mOperand=CPU_PEEKW(mOperand);}
//
// Helper Macros
//
//#define SET_Z(m) { mZ=(m)?false:true; }
//#define SET_N(m) { mN=(m&0x80)?true:false; }
//#define SET_NZ(m) SET_Z(m) SET_N(m)
#define SET_Z(m) { mZ=!(m); }
#define SET_N(m) { mN=(m)&0x80; }
#define SET_NZ(m) { mZ=!(m); mN=(m)&0x80; }
#define PULL(m) { mSP++; mSP&=0xff; m=CPU_PEEK(mSP+0x0100); }
#define PUSH(m) { CPU_POKE(0x0100+mSP,m); mSP--; mSP&=0xff; }
//
// Opcode execution
//
/*
#define xADC()\
{\
UBYTE value=CPU_PEEK(mOperand);\
UBYTE oldA=mA;\
if(!mD)\
{\
SWORD sum=(SWORD)((SBYTE)mA)+(SWORD)((SBYTE)value)+(mC?1:0);\
mV=((sum > 127) || (sum < -128));\
sum=(SWORD)mA + (SWORD)value + (mC?1:0);\
mA=(UBYTE)sum;\
mC=(sum>0xff);\
SET_NZ(mA);\
}\
else\
{\
SWORD sum=mBCDTable[0][mA]+mBCDTable[0][value]+(mC?1:0);\
mC=(sum > 99);\
mA=mBCDTable[1][sum & 0xff];\
SET_NZ(mA);\
mV=((oldA^mA)&0x80) && ((mA^value)&0x80);\
}\
}
*/
#define xADC()\
{\
int value=CPU_PEEK(mOperand);\
if(mD)\
{\
int c = mC?1:0;\
int lo = (mA & 0x0f) + (value & 0x0f) + c;\
int hi = (mA & 0xf0) + (value & 0xf0);\
mV=0;\
mC=0;\
if (lo > 0x09)\
{\
hi += 0x10;\
lo += 0x06;\
}\
if (~(mA^value) & (mA^hi) & 0x80) mV=1;\
if (hi > 0x90) hi += 0x60;\
if (hi & 0xff00) mC=1;\
mA = (lo & 0x0f) + (hi & 0xf0);\
}\
else\
{\
int c = mC?1:0;\
int sum = mA + value + c;\
mV=0;\
mC=0;\
if (~(mA^value) & (mA^sum) & 0x80) mV=1;\
if (sum & 0xff00) mC=1;\
mA = (UBYTE) sum;\
}\
SET_NZ(mA)\
}
#define xAND()\
{\
mA&=CPU_PEEK(mOperand);\
SET_NZ(mA);\
}
#define xASL()\
{\
int value=CPU_PEEK(mOperand);\
mC=value&0x80;\
value<<=1;\
value&=0xff;\
SET_NZ(value);\
CPU_POKE(mOperand,value);\
}
#define xASLA()\
{\
mC=mA&0x80;\
mA<<=1;\
mA&=0xff;\
SET_NZ(mA);\
}
#define xBCC()\
{\
if(!mC)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBCS()\
{\
if(mC)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBEQ()\
{\
if(mZ)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBIT()\
{\
int value=CPU_PEEK(mOperand);\
SET_Z(mA&value);\
\
if(mOpcode!=0x89)\
{\
mN=value&0x80;\
mV=value&0x40;\
}\
}
/*
//
// DONT USE THIS VERSION OF BIT, IT BREAKS CALGAMES TITLE SCREEN !!!!
//
#define xBIT()\
{\
int value=CPU_PEEK(mOperand);\
SET_Z(mA&value);\
\
mN=value&0x80;\
mV=value&0x40;\
}
*/
#define xBMI()\
{\
if(mN)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBNE()\
{\
if(!mZ)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBPL()\
{\
if(!mN)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBRA()\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}
/*
#define xBRK()\
{\
mPC++;\
PUSH(mPC>>8);\
PUSH(mPC&0xff);\
PUSH(PS()|0x10);\
\
mD=FALSE;\
mI=TRUE;\
\
mPC=CPU_PEEKW(IRQ_VECTOR);\
}
*/
#define xBRK()\
{\
mPC++;\
PUSH(mPC>>8);\
PUSH(mPC&0xff);\
PUSH(PS()|0x10);\
\
mD=FALSE;\
mI=TRUE;\
\
mPC=CPU_PEEKW(IRQ_VECTOR);\
}
// KW 4/11/98 B flag needed to be set IN the stack status word = 0x10.
#define xBVC()\
{\
if(!mV)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xBVS()\
{\
if(mV)\
{\
int offset=(signed char)CPU_PEEK(mPC);\
mPC++;\
mPC+=offset;\
mPC&=0xffff;\
}\
else\
{\
mPC++;\
mPC&=0xffff;\
}\
}
#define xCLC()\
{\
mC=FALSE;\
}
#define xCLD()\
{\
mD=FALSE;\
}
#define xCLI()\
{\
mI=FALSE;\
}
#define xCLV()\
{\
mV=FALSE;\
}
//
// Alternate CMP code
//
/*
#define xCMP()\
{\
UBYTE value=CPU_PEEK(mOperand);\
if(mA+0x100-value>0xff) mC=TRUE; else mC=FALSE;\
value=mA+0x100-value;\
mZ=!value;\
mN=value&0x0080;\
}
#define xCPX()\
{\
UBYTE value=CPU_PEEK(mOperand);\
if(mX+0x100-value>0xff) mC=TRUE; else mC=FALSE;\
value=mX+0x100-value;\
mZ=!value;\
mN=value&0x0080;\
}
#define xCPY()\
{\
UBYTE value=CPU_PEEK(mOperand);\
if(mY+0x100-value>0xff) mC=TRUE; else mC=FALSE;\
value=mY+0x100-value;\
mZ=!value;\
mN=value&0x0080;\
}
#define xCMP()\
{\
UWORD value=(UWORD)mA-CPU_PEEK(mOperand);\
SET_NZ(value);\
mC=!(value&0x0100);\
}
*/
#define xCMP()\
{\
int value=CPU_PEEK(mOperand);\
mC=0;\
if (mA >= value) mC=1;\
SET_NZ((UBYTE)(mA - value))\
}
/*
#define xCPX()\
{\
UWORD value=(UWORD)mX-CPU_PEEK(mOperand);\
SET_NZ(value);\
mC=!(value&0x0100);\
}
*/
#define xCPX()\
{\
int value=CPU_PEEK(mOperand);\
mC=0;\
if (mX >= value) mC=1;\
SET_NZ((UBYTE)(mX - value))\
}
/*
#define xCPY()\
{\
UWORD value=(UWORD)mY-CPU_PEEK(mOperand);\
SET_NZ(value);\
mC=!(value&0x0100);\
}
*/
#define xCPY()\
{\
int value=CPU_PEEK(mOperand);\
mC=0;\
if (mY >= value) mC=1;\
SET_NZ((UBYTE)(mY - value))\
}
#define xDEC()\
{\
int value=CPU_PEEK(mOperand)-1;\
value&=0xff;\
CPU_POKE(mOperand,value);\
SET_NZ(value);\
}
#define xDECA()\
{\
mA--;\
mA&=0xff;\
SET_NZ(mA);\
}
#define xDEX()\
{\
mX--;\
mX&=0xff;\
SET_NZ(mX);\
}
#define xDEY()\
{\
mY--;\
mY&=0xff;\
SET_NZ(mY);\
}
#define xEOR()\
{\
mA^=CPU_PEEK(mOperand);\
SET_NZ(mA);\
}
#define xINC()\
{\
int value=CPU_PEEK(mOperand)+1;\
value&=0xff;\
CPU_POKE(mOperand,value);\
SET_NZ(value);\
}
#define xINCA()\
{\
mA++;\
mA&=0xff;\
SET_NZ(mA);\
}
#define xINX()\
{\
mX++;\
mX&=0xff;\
SET_NZ(mX);\
}
#define xINY()\
{\
mY++;\
mY&=0xff;\
SET_NZ(mY);\
}
#define xJMP()\
{\
mPC=mOperand;\
}
#define xJSR()\
{\
PUSH((mPC-1)>>8);\
PUSH((mPC-1)&0xff);\
mPC=mOperand;\
}
#define xLDA()\
{\
mA=CPU_PEEK(mOperand);\
SET_NZ(mA);\
}
#define xLDX()\
{\
mX=CPU_PEEK(mOperand);\
SET_NZ(mX);\
}
#define xLDY()\
{\
mY=CPU_PEEK(mOperand);\
SET_NZ(mY);\
}
#define xLSR()\
{\
int value=CPU_PEEK(mOperand);\
mC=value&0x01;\
value=(value>>1)&0x7f;\
CPU_POKE(mOperand,value);\
SET_NZ(value);\
}
#define xLSRA()\
{\
mC=mA&0x01;\
mA=(mA>>1)&0x7f;\
SET_NZ(mA);\
}
#define xNOP()\
{\
}
#define xORA()\
{\
mA|=CPU_PEEK(mOperand);\
SET_NZ(mA);\
}
#define xPHA()\
{\
PUSH(mA);\
}
#define xPHP()\
{\
PUSH(PS());\
}
#define xPHX()\
{\
PUSH(mX);\
}
#define xPHY()\
{\
PUSH(mY);\
}
#define xPLA()\
{\
PULL(mA);\
SET_NZ(mA);\
}
#define xPLP()\
{\
int P;\
PULL(P);\
PS(P);\
}
#define xPLX()\
{\
PULL(mX);\
SET_NZ(mX);\
}
#define xPLY()\
{\
PULL(mY);\
SET_NZ(mY);\
}
#define xROL()\
{\
int value=CPU_PEEK(mOperand);\
int oldC=mC;\
mC=value&0x80;\
value=(value<<1)|(oldC?1:0);\
value&=0xff;\
CPU_POKE(mOperand,value);\
SET_NZ(value);\
}
#define xROLA()\
{\
int oldC=mC;\
mC=mA&0x80;\
mA=(mA<<1)|(oldC?1:0);\
mA&=0xff;\
SET_NZ(mA);\
}
#define xROR()\
{\
int value=CPU_PEEK(mOperand);\
int oldC=mC;\
mC=value&0x01;\
value=((value>>1)&0x7f)|(oldC?0x80:0x00);\
value&=0xff;\
CPU_POKE(mOperand,value);\
SET_NZ(value);\
}
#define xRORA()\
{\
int oldC=mC;\
mC=mA&0x01;\
mA=((mA>>1)&0x7f)|(oldC?0x80:0x00);\
mA&=0xff;\
SET_NZ(mA);\
}
#define xRTI()\
{\
int tmp;\
PULL(tmp);\
PS(tmp);\
PULL(mPC);\
PULL(tmp);\
mPC|=tmp<<8;\
}
#define xRTS()\
{\
int tmp;\
PULL(mPC);\
PULL(tmp);\
mPC|=tmp<<8;\
mPC++;\
}
/*
#define xSBC()\
{\
UBYTE oldA=mA;\
if(!mD)\
{\
UBYTE value=~(CPU_PEEK(mOperand));\
SWORD difference=(SWORD)((SBYTE)mA)+(SWORD)((SBYTE)value)+(mC?1:0);\
mV=((difference>127)||(difference<-128));\
difference=((SWORD)mA)+((SWORD)value)+ (mC?1:0);\
mA=(UBYTE)difference;\
mC=(difference>0xff);\
SET_NZ(mA);\
}\
else\
{\
UBYTE value=CPU_PEEK(mOperand);\
SWORD difference=mBCDTable[0][mA]-mBCDTable[0][value]-(mC?0:1);\
if(difference<0) difference+=100;\
mA=mBCDTable[1][difference];\
mC=(oldA>=(value+(mC?0:1)));\
mV=((oldA^mA)&0x80)&&((mA^value)&0x80);\
SET_NZ(mA);\
}\
}
*/
#define xSBC()\
{\
int value=CPU_PEEK(mOperand);\
if (mD)\
{\
int c = mC?0:1;\
int sum = mA - value - c;\
int lo = (mA & 0x0f) - (value & 0x0f) - c;\
int hi = (mA & 0xf0) - (value & 0xf0);\
mV=0;\
mC=0;\
if ((mA^value) & (mA^sum) & 0x80) mV=1;\
if (lo & 0xf0) lo -= 6;\
if (lo & 0x80) hi -= 0x10;\
if (hi & 0x0f00) hi -= 0x60;\
if ((sum & 0xff00) == 0) mC=1;\
mA = (lo & 0x0f) + (hi & 0xf0);\
}\
else\
{\
int c = mC?0:1;\
int sum = mA - value - c;\
mV=0;\
mC=0;\
if ((mA^value) & (mA^sum) & 0x80) mV=1;\
if ((sum & 0xff00) == 0) mC=1;\
mA = (UBYTE) sum;\
}\
SET_NZ(mA)\
}
#define xSEC()\
{\
mC=true;\
}
#define xSED()\
{\
mD=true;\
}
#define xSEI()\
{\
mI=true;\
}
#define xSTA()\
{\
CPU_POKE(mOperand,mA);\
}
#define xSTP()\
{\
gSystemCPUSleep=TRUE;\
}
#define xSTX()\
{\
CPU_POKE(mOperand,mX);\
}
#define xSTY()\
{\
CPU_POKE(mOperand,mY);\
}
#define xSTZ()\
{\
CPU_POKE(mOperand,0);\
}
#define xTAX()\
{\
mX=mA;\
SET_NZ(mX);\
}
#define xTAY()\
{\
mY=mA;\
SET_NZ(mY);\
}
#define xTRB()\
{\
int value=CPU_PEEK(mOperand);\
SET_Z(mA&value);\
value=value&(mA^0xff);\
CPU_POKE(mOperand,value);\
}
//
// THE COMMENTED OUT CODE IS DERIVED FROM THE MAME 65C02 MODEL AND
// LOOKS TO BE INCORRECT i.e When plugged into Handy things stop working
//
/*
#define xTRB()\
{\
int value=CPU_PEEK(mOperand);\
value &= ~mA;\
SET_NZ(value);\
CPU_POKE(mOperand,value);\
}
*/
#define xTSB()\
{\
int value=CPU_PEEK(mOperand);\
SET_Z(mA&value);\
value=value|mA;\
CPU_POKE(mOperand,value);\
}
//
// THE COMMENTED OUT CODE IS DERIVED FROM THE MAME 65C02 MODEL AND
// LOOKS TO BE INCORRECT i.e When plugged into Handy things stop working
//
/*
#define xTSB()\
{\
int value=CPU_PEEK(mOperand);\
value |= mA;\
SET_NZ(value);\
CPU_POKE(mOperand,value);\
}
*/
#define xTSX()\
{\
mX=mSP;\
SET_NZ(mX);\
}
#define xTXA()\
{\
mA=mX;\
SET_NZ(mA);\
}
#define xTXS()\
{\
mSP=mX;\
}
#define xTYA()\
{\
mA=mY;\
SET_NZ(mA);\
}
#define xWAI()\
{\
gSystemCPUSleep=TRUE;\
}