summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/codecs/libgme/nes_cpu_run.h
blob: fd1fea965970bf0b63ca864ff23286025ab9aaa3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
// NES 6502 cpu emulator run function

#if 0
/* Define these macros in the source file before #including this file.
- Parameters might be expressions, so they are best evaluated only once,
though they NEVER have side-effects, so multiple evaluation is OK.
- Output parameters might be a multiple-assignment expression like "a=x",
so they must NOT be parenthesized.
- Except where noted, time() and related functions will NOT work
correctly inside a macro. TIME() is always correct, and FLUSH_TIME() and
CACHE_TIME() allow the time changing functions to work.
- Macros "returning" void may use a {} statement block. */

	// 0 <= addr <= 0xFFFF + page_size
	// time functions can be used
	int  READ_MEM(  addr_t );
	void WRITE_MEM( addr_t, int data );
	// 0 <= READ_MEM() <= 0xFF
	
	// 0 <= addr <= 0x1FF
	int  READ_LOW(  addr_t );
	void WRITE_LOW( addr_t, int data );
	// 0 <= READ_LOW() <= 0xFF

	// Often-used instructions attempt these before using a normal memory access.
	// Optional; defaults to READ_MEM() and WRITE_MEM()
	bool CAN_READ_FAST( addr_t ); // if true, uses result of READ_FAST
	void READ_FAST( addr_t, int& out ); // ALWAYS called BEFORE CAN_READ_FAST
	bool CAN_WRITE_FAST( addr_t ); // if true, uses WRITE_FAST instead of WRITE_MEM
	void WRITE_FAST( addr_t, int data );

	// Used by instructions most often used to access the NES PPU (LDA abs and BIT abs).
	// Optional; defaults to READ_MEM.
	void READ_PPU(  addr_t, int& out );
	// 0 <= out <= 0xFF

// The following can be used within macros:
	
	// Current time
	time_t TIME();
	
	// Allows use of time functions
	void FLUSH_TIME();
	
	// Must be used before end of macro if FLUSH_TIME() was used earlier
	void CACHE_TIME();

// Configuration (optional; commented behavior if defined)
	
	// Emulates dummy reads for indexed instructions
	#define NES_CPU_DUMMY_READS 1
	
	// Optimizes as if map_code( 0, 0x10000 + cpu_padding, FLAT_MEM ) is always in effect
	#define FLAT_MEM my_mem_array
	
	// Expanded just before beginning of code, to help debugger
	#define CPU_BEGIN void my_run_cpu() {
	
#endif

/* Copyright (C) 2003-2008 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

// Allows MWCW debugger to step through code properly
#ifdef CPU_BEGIN
	CPU_BEGIN
#endif

// Time
#define TIME()          (s_time + s.base)
#define FLUSH_TIME()    {s.time = s_time - time_offset;}
#define CACHE_TIME()    {s_time = s.time + time_offset;}

// Defaults
#ifndef CAN_WRITE_FAST
	#define CAN_WRITE_FAST( addr )      0
	#define WRITE_FAST( addr, data )
#endif

#ifndef CAN_READ_FAST
	#define CAN_READ_FAST( addr )       0
	#define READ_FAST( addr, out )
#endif

#ifndef READ_PPU
	#define READ_PPU( addr, out )\
	{\
		FLUSH_TIME();\
		out = READ_MEM( addr );\
		CACHE_TIME();\
	}
#endif

#define READ_STACK  READ_LOW
#define WRITE_STACK WRITE_LOW

// Dummy reads
#ifdef NES_CPU_DUMMY_READS
	// TODO: optimize time handling
	#define DUMMY_READ( addr, idx ) \
		if ( (addr & 0xFF) < idx )\
		{\
			int const time_offset = 1;\
			FLUSH_TIME();\
			READ_MEM( (addr - 0x100) );\
			CACHE_TIME();\
		}
#else
	#define DUMMY_READ( addr, idx )
#endif

// Code
#ifdef FLAT_MEM
	#define CODE_PAGE(   addr ) (FLAT_MEM)
	#define CODE_OFFSET( addr ) (addr)
#else
	#define CODE_PAGE( addr )   (s.code_map [NES_CPU_PAGE( addr )])
	#define CODE_OFFSET( addr ) NES_CPU_OFFSET( addr )
#endif
#define READ_CODE( addr )   (CODE_PAGE( addr ) [CODE_OFFSET( addr )])

// Stack
#define SET_SP( v ) (sp = ((v) + 1) | 0x100)
#define GET_SP()    ((sp - 1) & 0xFF)
#define SP( o )     ((sp + (o - (o>0)*0x100)) | 0x100)

// Truncation
#define BYTE(  n ) ((uint8_t ) (n)) /* (unsigned) n & 0xFF */
#define SBYTE( n ) ((int8_t  ) (n)) /* (BYTE( n ) ^ 0x80) - 0x80 */
#define WORD(  n ) ((uint16_t) (n)) /* (unsigned) n & 0xFFFF */

// Flags with hex value for clarity when used as mask.
// Stored in indicated variable during emulation.
int const n80 = 0x80; // nz
int const v40 = 0x40; // flags
int const r20 = 0x20;
int const b10 = 0x10;
int const d08 = 0x08; // flags
int const i04 = 0x04; // flags
int const z02 = 0x02; // nz
int const c01 = 0x01; // c

#define IS_NEG (nz & 0x8080)

#define GET_FLAGS( out ) \
{\
	out = flags & (v40 | d08 | i04);\
	out += ((nz >> 8) | nz) & n80;\
	out += c >> 8 & c01;\
	if ( !BYTE( nz ) )\
		out += z02;\
}

#define SET_FLAGS( in ) \
{\
	flags = in & (v40 | d08 | i04);\
	c = nz = in << 8;\
	nz += ~in & z02;\
}

{
	int const time_offset = 0;
	
	// Local state
	struct cpu_state_t s;
	#ifdef FLAT_MEM
		s.base = cpu->cpu_state_.base;
	#else
		s = cpu->cpu_state_;
	#endif
	cpu->cpu_state = &s;
	int s_time = cpu->cpu_state_.time; // helps even on x86
	
	// Registers
	int pc = cpu->r.pc;
	int a  = cpu->r.a;
	int x  = cpu->r.x;
	int y  = cpu->r.y;
	int sp;
	SET_SP( cpu->r.sp );
	
	// Flags
	int flags;
	int c;  // carry set if (c & 0x100) != 0
	int nz; // Z set if (nz & 0xFF) == 0, N set if (nz & 0x8080) != 0
	{
		int temp = cpu->r.flags;
		SET_FLAGS( temp );
	}
	
loop:
	
	// Check all values
	check( (unsigned) sp - 0x100 < 0x100 );
	check( (unsigned) pc < 0x10000 );
	check( (unsigned) a < 0x100 );
	check( (unsigned) x < 0x100 );
	check( (unsigned) y < 0x100 );
	
	// Read instruction
	byte const* instr = CODE_PAGE( pc );
	int opcode;
	
	if ( CODE_OFFSET(~0) == ~0 )
	{
		opcode = instr [pc];
		pc++;
		instr += pc;
	}
	else
	{
		instr += CODE_OFFSET( pc );
		opcode = *instr++;
		pc++;
	}
	
	// local to function in case it helps optimizer
	static byte const clock_table [256] =
	{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
		0,6,2,8,3,3,5,5,3,2,2,2,4,4,6,6,// 0
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,// 1
		6,6,0,8,3,3,5,5,4,2,2,2,4,4,6,6,// 2
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,// 3
		6,6,2,8,3,3,5,5,3,2,2,2,3,4,6,6,// 4
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,// 5
		6,6,2,8,3,3,5,5,4,2,2,2,5,4,6,6,// 6
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,// 7
		2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,// 8
		2,6,2,6,4,4,4,4,2,5,2,5,5,5,5,5,// 9
		2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,// A
		2,5,2,5,4,4,4,4,2,4,2,4,4,4,4,4,// B
		2,6,2,8,3,3,5,5,2,2,2,2,4,4,6,6,// C
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,// D
		2,6,2,8,3,3,5,5,2,2,2,2,4,4,6,6,// E
		2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7 // F
	}; // 0x00 was 7 and 0x22 was 2
	
	// Update time
	if ( s_time >= 0 )
		goto out_of_time;
	
	#ifdef CPU_INSTR_HOOK
	{ CPU_INSTR_HOOK( (pc-1), (&instr [-1]), a, x, y, GET_SP(), TIME() ); }
	#endif
	
	s_time += clock_table [opcode];
	
	int data;
	data = *instr;
	
	switch ( opcode )
	{

// Macros

#define GET_MSB()       (instr [1])
#define ADD_PAGE( out ) (pc++, out = data + 0x100 * GET_MSB())
#define GET_ADDR()      GET_LE16( instr )

#define PAGE_PENALTY( lsb ) s_time += (lsb) >> 8;

#define INC_DEC( reg, n ) reg = BYTE( nz = reg + n ); goto loop;

#define IND_Y( cross, out ) {\
		int temp = READ_LOW( data ) + y;\
		out = temp + 0x100 * READ_LOW( BYTE( data + 1 ) );\
		cross( temp );\
	}
	
#define IND_X( out ) {\
		int temp = data + x;\
		out = 0x100 * READ_LOW( BYTE( temp + 1 ) ) + READ_LOW( BYTE( temp ) );\
	}
	
#define ARITH_ADDR_MODES( op )\
case op - 0x04: /* (ind,x) */\
	IND_X( data )\
	goto ptr##op;\
case op + 0x0C: /* (ind),y */\
	IND_Y( PAGE_PENALTY, data )\
	goto ptr##op;\
case op + 0x10: /* zp,X */\
	data = BYTE( data + x );\
case op + 0x00: /* zp */\
	data = READ_LOW( data );\
	goto imm##op;\
case op + 0x14: /* abs,Y */\
	data += y;\
	goto ind##op;\
case op + 0x18: /* abs,X */\
	data += x;\
ind##op:\
	PAGE_PENALTY( data );\
case op + 0x08: /* abs */\
	ADD_PAGE( data );\
ptr##op:\
	FLUSH_TIME();\
	data = READ_MEM( data );\
	CACHE_TIME();\
case op + 0x04: /* imm */\
imm##op:

// TODO: more efficient way to handle negative branch that wraps PC around
#define BRANCH( cond )\
{\
	++pc;\
	if ( !(cond) ) goto loop;\
	s_time++;\
	int offset = SBYTE( data );\
	s_time += (BYTE(pc) + offset) >> 8 & 1;\
	pc = WORD( pc + offset );\
	goto loop;\
}

// Often-Used

	case 0xB5: // LDA zp,x
		a = nz = READ_LOW( BYTE( data + x ) );
		pc++;
		goto loop;
	
	case 0xA5: // LDA zp
		a = nz = READ_LOW( data );
		pc++;
		goto loop;
	
	case 0xD0: // BNE
		BRANCH( BYTE( nz ) );
	
	case 0x20: { // JSR
		int temp = pc + 1;
		pc = GET_ADDR();
		WRITE_STACK( SP( -1 ), temp >> 8 );
		sp = SP( -2 );
		WRITE_STACK( sp, temp );
		goto loop;
	}
	
	case 0x4C: // JMP abs
		pc = GET_ADDR();
		goto loop;
	
	case 0xE8: // INX
		INC_DEC( x, 1 )
	
	case 0x10: // BPL
		BRANCH( !IS_NEG )
	
	ARITH_ADDR_MODES( 0xC5 ) // CMP
		nz = a - data;
		pc++;
		c = ~nz;
		nz &= 0xFF;
		goto loop;
	
	case 0x30: // BMI
		BRANCH( IS_NEG )
	
	case 0xF0: // BEQ
		BRANCH( !BYTE( nz ) );
	
	case 0x95: // STA zp,x
		data = BYTE( data + x );
	case 0x85: // STA zp
		pc++;
		WRITE_LOW( data, a );
		goto loop;
	
	case 0xC8: // INY
		INC_DEC( y, 1 )

	case 0xA8: // TAY
		y  = a;
		nz = a;
		goto loop;
	
	case 0x98: // TYA
		a  = y;
		nz = y;
		goto loop;
	
	case 0xAD:{// LDA abs
		int addr = GET_ADDR();
		pc += 2;
		READ_PPU( addr, a = nz );
		goto loop;
	}
	
	case 0x60: // RTS
		pc = 1 + READ_STACK( sp );
		pc += 0x100 * READ_STACK( SP( 1 ) );
		sp = SP( 2 );
		goto loop;
	
	{
		int addr;
		
	case 0x8D: // STA abs
		addr = GET_ADDR();
		pc += 2;
		if ( CAN_WRITE_FAST( addr ) )
		{
			WRITE_FAST( addr, a );
			goto loop;
		}
	sta_ptr:
		FLUSH_TIME();
		WRITE_MEM( addr, a );
		CACHE_TIME();
		goto loop;
	
	case 0x99: // STA abs,Y
		addr = y + GET_ADDR();
		pc += 2;
		if ( CAN_WRITE_FAST( addr ) )
		{
			WRITE_FAST( addr, a );
			goto loop;
		}
		goto sta_abs_x;
	
	case 0x9D: // STA abs,X (slightly more common than STA abs)
		addr = x + GET_ADDR();
		pc += 2;
		if ( CAN_WRITE_FAST( addr ) )
		{
			WRITE_FAST( addr, a );
			goto loop;
		}
		DUMMY_READ( addr, x );
	sta_abs_x:
		FLUSH_TIME();
		WRITE_MEM( addr, a );
		CACHE_TIME();
		goto loop;
	
	case 0x91: // STA (ind),Y
		#define NO_PAGE_PENALTY( lsb )
		IND_Y( NO_PAGE_PENALTY, addr )
		pc++;
		DUMMY_READ( addr, y );
		goto sta_ptr;
	
	case 0x81: // STA (ind,X)
		IND_X( addr )
		pc++;
		goto sta_ptr;
	
	}
	
	case 0xA9: // LDA #imm
		pc++;
		a  = data;
		nz = data;
		goto loop;

	// common read instructions
	{
		int addr;
		
	case 0xA1: // LDA (ind,X)
		IND_X( addr )
		pc++;
		goto a_nz_read_addr;
	
	case 0xB1:// LDA (ind),Y
		addr = READ_LOW( data ) + y;
		PAGE_PENALTY( addr );
		addr += 0x100 * READ_LOW( BYTE( data + 1 ) );
		pc++;
		READ_FAST( addr, a = nz );
		if ( CAN_READ_FAST( addr ) )
			goto loop;
		DUMMY_READ( addr, y );
		goto a_nz_read_addr;
	
	case 0xB9: // LDA abs,Y
		PAGE_PENALTY( data + y );
		addr = GET_ADDR() + y;
		pc += 2;
		READ_FAST( addr, a = nz );
		if ( CAN_READ_FAST( addr ) )
			goto loop;
		goto a_nz_read_addr;
	
	case 0xBD: // LDA abs,X
		PAGE_PENALTY( data + x );
		addr = GET_ADDR() + x;
		pc += 2;
		READ_FAST( addr, a = nz );
		if ( CAN_READ_FAST( addr ) )
			goto loop;
		DUMMY_READ( addr, x );
	a_nz_read_addr:
		FLUSH_TIME();
		a = nz = READ_MEM( addr );
		CACHE_TIME();
		goto loop;
	
	}

// Branch

	case 0x50: // BVC
		BRANCH( !(flags & v40) )
	
	case 0x70: // BVS
		BRANCH( flags & v40 )
	
	case 0xB0: // BCS
		BRANCH( c & 0x100 )
	
	case 0x90: // BCC
		BRANCH( !(c & 0x100) )
	
// Load/store
	
	case 0x94: // STY zp,x
		data = BYTE( data + x );
	case 0x84: // STY zp
		pc++;
		WRITE_LOW( data, y );
		goto loop;
	
	case 0x96: // STX zp,y
		data = BYTE( data + y );
	case 0x86: // STX zp
		pc++;
		WRITE_LOW( data, x );
		goto loop;
	
	case 0xB6: // LDX zp,y
		data = BYTE( data + y );
	case 0xA6: // LDX zp
		data = READ_LOW( data );
	case 0xA2: // LDX #imm
		pc++;
		x = data;
		nz = data;
		goto loop;
	
	case 0xB4: // LDY zp,x
		data = BYTE( data + x );
	case 0xA4: // LDY zp
		data = READ_LOW( data );
	case 0xA0: // LDY #imm
		pc++;
		y = data;
		nz = data;
		goto loop;
	
	case 0xBC: // LDY abs,X
		data += x;
		PAGE_PENALTY( data );
	case 0xAC:{// LDY abs
		int addr = data + 0x100 * GET_MSB();
		pc += 2;
		FLUSH_TIME();
		y = nz = READ_MEM( addr );
		CACHE_TIME();
		goto loop;
	}
	
	case 0xBE: // LDX abs,y
		data += y;
		PAGE_PENALTY( data );
	case 0xAE:{// LDX abs
		int addr = data + 0x100 * GET_MSB();
		pc += 2;
		FLUSH_TIME();
		x = nz = READ_MEM( addr );
		CACHE_TIME();
		goto loop;
	}
	
	{
		int temp;
	case 0x8C: // STY abs
		temp = y;
		goto store_abs;
	
	case 0x8E: // STX abs
		temp = x;
	store_abs:
		{
			int addr = GET_ADDR();
			pc += 2;
			if ( CAN_WRITE_FAST( addr ) )
			{
				WRITE_FAST( addr, temp );
				goto loop;
			}
			FLUSH_TIME();
			WRITE_MEM( addr, temp );
			CACHE_TIME();
			goto loop;
		}
	}

// Compare

	case 0xEC: {// CPX abs
		int addr = GET_ADDR();
		pc++;
		FLUSH_TIME();
		data = READ_MEM( addr );
		CACHE_TIME();
		goto cpx_data;
	}
	
	case 0xE4: // CPX zp
		data = READ_LOW( data );
	case 0xE0: // CPX #imm
	cpx_data:
		nz = x - data;
		pc++;
		c = ~nz;
		nz &= 0xFF;
		goto loop;
	
	case 0xCC:{// CPY abs
		int addr = GET_ADDR();
		pc++;
		FLUSH_TIME();
		data = READ_MEM( addr );
		CACHE_TIME();
		goto cpy_data;
	}
	
	case 0xC4: // CPY zp
		data = READ_LOW( data );
	case 0xC0: // CPY #imm
	cpy_data:
		nz = y - data;
		pc++;
		c = ~nz;
		nz &= 0xFF;
		goto loop;
	
// Logical

	ARITH_ADDR_MODES( 0x25 ) // AND
		nz = (a &= data);
		pc++;
		goto loop;
	
	ARITH_ADDR_MODES( 0x45 ) // EOR
		nz = (a ^= data);
		pc++;
		goto loop;
	
	ARITH_ADDR_MODES( 0x05 ) // ORA
		nz = (a |= data);
		pc++;
		goto loop;
	
	case 0x2C:{// BIT abs
		int addr = GET_ADDR();
		pc += 2;
		READ_PPU( addr, nz );
		flags = (flags & ~v40) + (nz & v40);
		if ( a & nz )
			goto loop;
		nz <<= 8; // result must be zero, even if N bit is set
		goto loop;
	}
	
	case 0x24: // BIT zp
		nz = READ_LOW( data );
		pc++;
		flags = (flags & ~v40) + (nz & v40);
		if ( a & nz )
			goto loop; // Z should be clear, and nz must be non-zero if nz & a is
		nz <<= 8; // set Z flag without affecting N flag
		goto loop;
		
// Add/subtract

	ARITH_ADDR_MODES( 0xE5 ) // SBC
	case 0xEB: // unofficial equivalent
		data ^= 0xFF;
		goto adc_imm;
	
	ARITH_ADDR_MODES( 0x65 ) // ADC
	adc_imm: {
		int carry = c >> 8 & 1;
		int ov = (a ^ 0x80) + carry + SBYTE( data );
		flags = (flags & ~v40) + (ov >> 2 & v40);
		c = nz = a + data + carry;
		pc++;
		a = BYTE( nz );
		goto loop;
	}
	
// Shift/rotate

	case 0x4A: // LSR A
		c = 0;
	case 0x6A: // ROR A
		nz = c >> 1 & 0x80;
		c = a << 8;
		nz += a >> 1;
		a = nz;
		goto loop;

	case 0x0A: // ASL A
		nz = a << 1;
		c = nz;
		a = BYTE( nz );
		goto loop;

	case 0x2A: { // ROL A
		nz = a << 1;
		int temp = c >> 8 & 1;
		c = nz;
		nz += temp;
		a = BYTE( nz );
		goto loop;
	}
	
	case 0x5E: // LSR abs,X
		data += x;
	case 0x4E: // LSR abs
		c = 0;
	case 0x6E: // ROR abs
	ror_abs: {
		ADD_PAGE( data );
		FLUSH_TIME();
		int temp = READ_MEM( data );
		nz = (c >> 1 & 0x80) + (temp >> 1);
		c = temp << 8;
		goto rotate_common;
	}
	
	case 0x3E: // ROL abs,X
		data += x;
		goto rol_abs;
	
	case 0x1E: // ASL abs,X
		data += x;
	case 0x0E: // ASL abs
		c = 0;
	case 0x2E: // ROL abs
	rol_abs:
		ADD_PAGE( data );
		nz = c >> 8 & 1;
		FLUSH_TIME();
		nz += (c = READ_MEM( data ) << 1);
	rotate_common:
		pc++;
		WRITE_MEM( data, BYTE( nz ) );
		CACHE_TIME();
		goto loop;
	
	case 0x7E: // ROR abs,X
		data += x;
		goto ror_abs;
	
	case 0x76: // ROR zp,x
		data = BYTE( data + x );
		goto ror_zp;
	
	case 0x56: // LSR zp,x
		data = BYTE( data + x );
	case 0x46: // LSR zp
		c = 0;
	case 0x66: // ROR zp
	ror_zp: {
		int temp = READ_LOW( data );
		nz = (c >> 1 & 0x80) + (temp >> 1);
		c = temp << 8;
		goto write_nz_zp;
	}
	
	case 0x36: // ROL zp,x
		data = BYTE( data + x );
		goto rol_zp;
	
	case 0x16: // ASL zp,x
		data = BYTE( data + x );
	case 0x06: // ASL zp
		c = 0;
	case 0x26: // ROL zp
	rol_zp:
		nz = c >> 8 & 1;
		nz += (c = READ_LOW( data ) << 1);
		goto write_nz_zp;
	
// Increment/decrement

	case 0xCA: // DEX
		INC_DEC( x, -1 )
	
	case 0x88: // DEY
		INC_DEC( y, -1 )
	
	case 0xF6: // INC zp,x
		data = BYTE( data + x );
	case 0xE6: // INC zp
		nz = 1;
		goto add_nz_zp;
	
	case 0xD6: // DEC zp,x
		data = BYTE( data + x );
	case 0xC6: // DEC zp
		nz = -1;
	add_nz_zp:
		nz += READ_LOW( data );
	write_nz_zp:
		pc++;
		WRITE_LOW( data, nz );
		goto loop;
	
	case 0xFE: // INC abs,x
		data = x + GET_ADDR();
		goto inc_ptr;
	
	case 0xEE: // INC abs
		data = GET_ADDR();
	inc_ptr:
		nz = 1;
		goto inc_common;
	
	case 0xDE: // DEC abs,x
		data = x + GET_ADDR();
		goto dec_ptr;
	
	case 0xCE: // DEC abs
		data = GET_ADDR();
	dec_ptr:
		nz = -1;
	inc_common:
		FLUSH_TIME();
		pc += 2;
		nz += READ_MEM( data );
		WRITE_MEM( data, BYTE( nz ) );
		CACHE_TIME();
		goto loop;
		
// Transfer

	case 0xAA: // TAX
		x = nz = a;
		goto loop;
		
	case 0x8A: // TXA
		a = nz = x;
		goto loop;

	case 0x9A: // TXS
		SET_SP( x ); // verified (no flag change)
		goto loop;
	
	case 0xBA: // TSX
		x = nz = GET_SP();
		goto loop;
	
// Stack
	
	case 0x48: // PHA
		sp = SP( -1 );
		WRITE_STACK( sp, a );
		goto loop;
		
	case 0x68: // PLA
		a = nz = READ_STACK( sp );
		sp = SP( 1 );
		goto loop;
		
	case 0x40:{// RTI
		pc  = READ_STACK( SP( 1 ) );
		pc += READ_STACK( SP( 2 ) ) * 0x100;
		int temp = READ_STACK( sp );
		sp = SP( 3 );
		data = flags;
		SET_FLAGS( temp );
		cpu->r.flags = flags; // update externally-visible I flag
		int delta = s.base - cpu->irq_time;
		if ( delta <= 0 ) goto loop; // end_time < irq_time
		if ( flags & i04 ) goto loop;
		s_time += delta;
		s.base = cpu->irq_time;
		goto loop;
	}
	
	case 0x28:{// PLP
		int temp = READ_STACK( sp );
		sp = SP( 1 );
		int changed = flags ^ temp;
		SET_FLAGS( temp );
		if ( !(changed & i04) )
			goto loop; // I flag didn't change
		if ( flags & i04 )
			goto handle_sei;
		goto handle_cli;
	}
	
	case 0x08:{// PHP
		int temp;
		GET_FLAGS( temp );
		sp = SP( -1 );
		WRITE_STACK( sp, temp | (b10 | r20) );
		goto loop;
	}
	
	case 0x6C:{// JMP (ind)
		data = GET_ADDR();
		byte const* page = CODE_PAGE( data );
		pc = page [CODE_OFFSET( data )];
		data = (data & 0xFF00) + ((data + 1) & 0xFF);
		pc += page [CODE_OFFSET( data )] * 0x100;
		goto loop;
	}
	
	case 0x00: // BRK
		goto handle_brk;
	
// Flags

	case 0x38: // SEC
		c = 0x100;
		goto loop;
	
	case 0x18: // CLC
		c = 0;
		goto loop;
		
	case 0xB8: // CLV
		flags &= ~v40;
		goto loop;
	
	case 0xD8: // CLD
		flags &= ~d08;
		goto loop;
	
	case 0xF8: // SED
		flags |= d08;
		goto loop;
	
	case 0x58: // CLI
		if ( !(flags & i04) )
			goto loop;
		flags &= ~i04;
	handle_cli: {
		//dprintf( "CLI at %d\n", TIME );
		cpu->r.flags = flags; // update externally-visible I flag
		int delta = s.base - cpu->irq_time;
		if ( delta <= 0 )
		{
			if ( TIME() < cpu->irq_time )
				goto loop;
			goto delayed_cli;
		}
		s.base = cpu->irq_time;
		s_time += delta;
		if ( s_time < 0 )
			goto loop;
		
		if ( delta >= s_time + 1 )
		{
			// delayed irq until after next instruction
			s.base += s_time + 1;
			s_time = -1;
			goto loop;
		}
		
		// TODO: implement
	delayed_cli:
		dprintf( "Delayed CLI not emulated\n" );
		goto loop;
	}
	
	case 0x78: // SEI
		if ( flags & i04 )
			goto loop;
		flags |= i04;
	handle_sei: {
		cpu->r.flags = flags; // update externally-visible I flag
		int delta = s.base - cpu->end_time;
		s.base = cpu->end_time;
		s_time += delta;
		if ( s_time < 0 )
			goto loop;
		
		dprintf( "Delayed SEI not emulated\n" );
		goto loop;
	}
	
// Unofficial
	
	// SKW - skip word
	case 0x1C: case 0x3C: case 0x5C: case 0x7C: case 0xDC: case 0xFC:
		PAGE_PENALTY( data + x );
	case 0x0C:
		pc++;
	// SKB - skip byte
	case 0x74: case 0x04: case 0x14: case 0x34: case 0x44: case 0x54: case 0x64:
	case 0x80: case 0x82: case 0x89: case 0xC2: case 0xD4: case 0xE2: case 0xF4:
		pc++;
		goto loop;
	
	// NOP
	case 0xEA: case 0x1A: case 0x3A: case 0x5A: case 0x7A: case 0xDA: case 0xFA:
		goto loop;
	
	case halt_opcode: // HLT - halt processor
		if ( pc-- > 0x10000 )
		{
			// handle wrap-around (assumes caller has put page of HLT at 0x10000)
			pc = WORD( pc );
			goto loop;
		}
	case 0x02: case 0x12:            case 0x32: case 0x42: case 0x52:
	case 0x62: case 0x72: case 0x92: case 0xB2: case 0xD2: case 0xF2:
		goto stop;
	
// Unimplemented
	
	case 0xFF:  // force 256-entry jump table for optimization purposes
		c |= 1; // compiler doesn't know that this won't affect anything
	default:
		check( (unsigned) opcode < 0x100 );
		
		#ifdef UNIMPL_INSTR
			UNIMPL_INSTR();
		#endif
		
		// At least skip over proper number of bytes instruction uses
		static unsigned char const illop_lens [8] = {
			0x40, 0x40, 0x40, 0x80, 0x40, 0x40, 0x80, 0xA0
		};
		int opcode = instr [-1];
		int len = illop_lens [opcode >> 2 & 7] >> (opcode << 1 & 6) & 3;
		if ( opcode == 0x9C )
			len = 2;
		pc += len;
		
		// Account for extra clock
		if ( (opcode >> 4) == 0x0B )
		{
			if ( opcode == 0xB3 )
				data = READ_LOW( data );
			if ( opcode != 0xB7 )
				PAGE_PENALTY( data + y );
		}
		goto loop;
	}
	assert( false ); // catch missing 'goto loop' or accidental 'break'
	
	int result_;
handle_brk:
	pc++;
	result_ = b10 | 4;
	
#ifdef CPU_DONE
interrupt:
#endif
	{
		s_time += 7;
		
		// Save PC and read vector
		WRITE_STACK( SP( -1 ), pc >> 8 );
		WRITE_STACK( SP( -2 ), pc );
		pc = GET_LE16( &READ_CODE( 0xFFFA ) + (result_ & 4) );
		
		// Save flags
		int temp;
		GET_FLAGS( temp );
		temp |= r20 + (result_ & b10); // B flag set for BRK
		sp = SP( -3 );
		WRITE_STACK( sp, temp );
		
		// Update I flag in externally-visible flags
		cpu->r.flags = (flags |= i04);
		
		// Update time
		int delta = s.base - cpu->end_time;
		if ( delta >= 0 )
			goto loop;
		s_time += delta;
		s.base = cpu->end_time;
		goto loop;
	}
	
out_of_time:
	pc--;
	
	// Optional action that triggers interrupt or changes irq/end time
	#ifdef CPU_DONE
	{
		CPU_DONE( result_ );
		if ( result_ >= 0 )
			goto interrupt;
		if ( s_time < 0 )
			goto loop;
	}
	#endif
stop:
	
	// Flush cached state
	cpu->r.pc = pc;
	cpu->r.sp = GET_SP();
	cpu->r.a  = a;
	cpu->r.x  = x;
	cpu->r.y  = y;

	int temp;
	GET_FLAGS( temp );
	cpu->r.flags = temp;
	
	cpu->cpu_state_.base = s.base;
	cpu->cpu_state_.time = s_time;
	cpu->cpu_state = &cpu->cpu_state_;
}