summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/m6502/opsc02.h
blob: 0dce82cc5ab24a822601ca7e9a6bb28eb2c7062e (plain) (blame)
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
/*****************************************************************************
 *
 *   m6502ops.h
 *   Addressing mode and opcode macros for 6502,65c02,65sc02,6510,n2a03 CPUs
 *
 *   Copyright Juergen Buchmueller, all rights reserved.
 *   65sc02 core Copyright Peter Trauner, all rights reserved.
 *
 *   - This source code is released as freeware for non-commercial purposes.
 *   - You are free to use and redistribute this code in modified or
 *     unmodified form, provided you list me in the credits.
 *   - If you modify this source code, you must add a notice to each modified
 *     source file that it has been changed.  If you're a nice person, you
 *     will clearly mark each change too.  :)
 *   - If you wish to use this for commercial purposes, please contact me at
 *     pullmoll@t-online.de
 *   - The author of this copywritten work reserves the right to change the
 *     terms of its usage and license at any time, including retroactively
 *   - This entire notice must remain in the source code.
 *
 *****************************************************************************/

/***************************************************************
 ***************************************************************
 *          Macros to emulate the 65C02 opcodes
 ***************************************************************
 ***************************************************************/

/* 65C02 *******************************************************
 *  EA = absolute address + X
 * one additional read if page boundary is crossed
 ***************************************************************/
#define EA_ABX_C02_P											\
	EA_ABS;														\
	if ( EAL + X > 0xff ) {										\
		RDMEM( PCW - 1 );										\
	}															\
	EAW += X;

/* 65C02 *******************************************************
 *  EA = absolute address + X
 ***************************************************************/
#define EA_ABX_C02_NP											\
	EA_ABS;														\
	RDMEM( PCW - 1 );											\
	EAW += X;

/***************************************************************
 *  EA = absolute address + Y
 * one additional read if page boundary is crossed
 ***************************************************************/
#define EA_ABY_C02_P											\
	EA_ABS;														\
	if ( EAL + Y > 0xff ) {										\
		RDMEM( PCW - 1 );										\
	}															\
	EAW += Y;

/* 65C02 *******************************************************
 *  EA = absolute address + Y
 ***************************************************************/
#define EA_ABY_C02_NP											\
	EA_ABS;														\
	RDMEM( PCW - 1 );											\
	EAW += Y

/* 65C02 *******************************************************
 *  EA = zero page indirect + Y (post indexed)
 *  subtract 1 cycle if page boundary is crossed
 ***************************************************************/
#define EA_IDY_C02_P											\
	ZPL = RDOPARG();											\
	EAL = RDMEM(ZPD);											\
	ZPL++;														\
	EAH = RDMEM(ZPD);											\
	if (EAL + Y > 0xff) {										\
		RDMEM( PCW - 1 );										\
	}															\
	EAW += Y;

/* 65C02 *******************************************************
 *  EA = zero page indirect + Y
 ***************************************************************/
#define EA_IDY_C02_NP											\
	ZPL = RDOPARG();											\
	EAL = RDMEM(ZPD);											\
	ZPL++;														\
	EAH = RDMEM(ZPD);											\
	RDMEM( PCW - 1 );											\
	EAW += Y

/* 65C02 *******************************************************
 *  EA = indirect (only used by JMP)
 * correct overflow handling
 ***************************************************************/
#define EA_IND_C02												\
	EA_ABS;														\
	tmp = RDMEM(EAD);											\
	RDMEM(PCW-1);												\
	EAD++;														\
	EAH = RDMEM(EAD);											\
	EAL = tmp

/* 65C02 *******************************************************
 *  EA = indirect plus x (only used by 65c02 JMP)
 ***************************************************************/
#define EA_IAX													\
	EA_ABS;														\
	RDMEM( PCW - 1 );											\
	if (EAL + X > 0xff) {										\
		RDMEM( PCW - 1 );										\
	}															\
	EAW += X;													\
	tmp = RDMEM(EAD);											\
	EAD++;														\
	EAH = RDMEM(EAD);											\
	EAL = tmp

/* read a value into tmp */
/* Base number of cycles taken for each mode (including reading of opcode):
   RD_ABX_C02_P         4/5
   RD_ABX_C02_NP/WR_ABX_C02_NP  5
   RD_ABY_C02_P         4/5
   RD_IDY_C02_P         5/6
   WR_IDY_C02_NP        6
 */
#define RD_ABX_C02_P	EA_ABX_C02_P; tmp = RDMEM(EAD)
#define RD_ABX_C02_NP	EA_ABX_C02_NP; tmp = RDMEM(EAD)
#define RD_ABY_C02_P	EA_ABY_C02_P; tmp = RDMEM(EAD)
#define RD_IDY_C02_P	EA_IDY_C02_P; tmp = RDMEM_ID(EAD); m6502_ICount -= 1

#define WR_ABX_C02_NP	EA_ABX_C02_NP; WRMEM(EAD, tmp)
#define WR_ABY_C02_NP	EA_ABY_C02_NP; WRMEM(EAD, tmp)
#define WR_IDY_C02_NP	EA_IDY_C02_NP; WRMEM_ID(EAD, tmp); m6502_ICount -= 1


/* 65C02********************************************************
 *  BRA  branch relative
 *  extra cycle if page boundary is crossed
 ***************************************************************/
#define BRA_C02(cond)											\
	tmp = RDOPARG();											\
	if (cond)													\
	{															\
		RDMEM(PCW);												\
		EAW = PCW + (signed char)tmp;							\
		if ( EAH != PCH ) {										\
			RDMEM( PCW - 1 );									\
		}														\
		PCD = EAD;												\
		CHANGE_PC;												\
	}

/* 65C02 ********************************************************
 *  ADC Add with carry
 * different setting of flags in decimal mode
 ***************************************************************/
#define ADC_C02 												\
	if (P & F_D)												\
	{															\
		int c = (P & F_C);										\
		int lo = (A & 0x0f) + (tmp & 0x0f) + c; 				\
		int hi = (A & 0xf0) + (tmp & 0xf0); 					\
		P &= ~(F_V | F_C);										\
		if( lo > 0x09 ) 										\
		{														\
			hi += 0x10; 										\
			lo += 0x06; 										\
		}														\
		if( ~(A^tmp) & (A^hi) & F_N )							\
			P |= F_V;											\
		if( hi > 0x90 ) 										\
			hi += 0x60; 										\
		if( hi & 0xff00 )										\
			P |= F_C;											\
		A = (lo & 0x0f) + (hi & 0xf0);							\
		RDMEM( PCW - 1 );										\
	}															\
	else														\
	{															\
		int c = (P & F_C);										\
		int sum = A + tmp + c;									\
		P &= ~(F_V | F_C);										\
		if( ~(A^tmp) & (A^sum) & F_N )							\
			P |= F_V;											\
		if( sum & 0xff00 )										\
			P |= F_C;											\
		A = (UINT8) sum;										\
	}															\
	SET_NZ(A)

/* 65C02 ********************************************************
 *  SBC Subtract with carry
 * different setting of flags in decimal mode
 ***************************************************************/
#define SBC_C02													\
	if (P & F_D)												\
	{															\
		int c = (P & F_C) ^ F_C;								\
		int sum = A - tmp - c;									\
		int lo = (A & 0x0f) - (tmp & 0x0f) - c; 				\
		int hi = (A & 0xf0) - (tmp & 0xf0); 					\
		P &= ~(F_V | F_C);										\
		if( (A^tmp) & (A^sum) & F_N )							\
			P |= F_V;											\
		if( lo & 0xf0 ) 										\
			lo -= 6;											\
		if( lo & 0x80 ) 										\
			hi -= 0x10; 										\
		if( hi & 0x0f00 )										\
			hi -= 0x60; 										\
		if( (sum & 0xff00) == 0 )								\
			P |= F_C;											\
		A = (lo & 0x0f) + (hi & 0xf0);							\
		RDMEM( PCW - 1 );										\
	}															\
	else														\
	{															\
		int c = (P & F_C) ^ F_C;								\
		int sum = A - tmp - c;									\
		P &= ~(F_V | F_C);										\
		if( (A^tmp) & (A^sum) & F_N )							\
			P |= F_V;											\
		if( (sum & 0xff00) == 0 )								\
			P |= F_C;											\
		A = (UINT8) sum;										\
	}															\
	SET_NZ(A)

/* 65C02 *******************************************************
 *  BBR Branch if bit is reset
 ***************************************************************/
#define BBR(bit)												\
	BRA(!(tmp & (1<<bit)))

/* 65C02 *******************************************************
 *  BBS Branch if bit is set
 ***************************************************************/
#define BBS(bit)												\
	BRA(tmp & (1<<bit))

/* 65c02 ********************************************************
 *  BRK Break
 *  increment PC, push PC hi, PC lo, flags (with B bit set),
 *  set I flag, reset D flag and jump via IRQ vector
 ***************************************************************/
#define BRK_C02 												\
	RDOPARG();													\
	PUSH(PCH);													\
	PUSH(PCL);													\
	PUSH(P | F_B);												\
	P = (P | F_I) & ~F_D;										\
	PCL = RDMEM(M6502_IRQ_VEC); 								\
	PCH = RDMEM(M6502_IRQ_VEC+1);								\
	CHANGE_PC


/* 65C02 *******************************************************
 *  DEA Decrement accumulator
 ***************************************************************/
#define DEA 													\
	A = (UINT8)--A; 											\
	SET_NZ(A)

/* 65C02 *******************************************************
 *  INA Increment accumulator
 ***************************************************************/
#define INA 													\
	A = (UINT8)++A; 											\
	SET_NZ(A)

/* 65C02 *******************************************************
 *  PHX Push index X
 ***************************************************************/
#define PHX 													\
	PUSH(X)

/* 65C02 *******************************************************
 *  PHY Push index Y
 ***************************************************************/
#define PHY 													\
	PUSH(Y)

/* 65C02 *******************************************************
 *  PLX Pull index X
 ***************************************************************/
#define PLX 													\
	RDMEM(SPD);													\
	PULL(X);													\
	SET_NZ(X)

/* 65C02 *******************************************************
 *  PLY Pull index Y
 ***************************************************************/
#define PLY 													\
	RDMEM(SPD);													\
	PULL(Y);													\
	SET_NZ(Y)

/* 65C02 *******************************************************
 *  RMB Reset memory bit
 ***************************************************************/
#define RMB(bit)												\
	tmp &= ~(1<<bit)

/* 65C02 *******************************************************
 *  SMB Set memory bit
 ***************************************************************/
#define SMB(bit)												\
	tmp |= (1<<bit)

/* 65C02 *******************************************************
 * STZ  Store zero
 ***************************************************************/
#define STZ 													\
	tmp = 0

/* 65C02 *******************************************************
 * TRB  Test and reset bits
 ***************************************************************/
#define TRB 													\
	SET_Z(tmp&A);												\
	tmp &= ~A

/* 65C02 *******************************************************
 * TSB  Test and set bits
 ***************************************************************/
#define TSB 													\
	SET_Z(tmp&A);												\
	tmp |= A


/***************************************************************
 ***************************************************************
 *          Macros to emulate the 65sc02 opcodes
 ***************************************************************
 ***************************************************************/


/* 65sc02 ********************************************************
 *  BSR Branch to subroutine
 ***************************************************************/
#define BSR 													\
	EAL = RDOPARG();											\
	RDMEM(SPD);													\
	PUSH(PCH);													\
	PUSH(PCL);													\
	EAH = RDOPARG();											\
	EAW = PCW + (INT16)(EAW-1); 								\
	PCD = EAD;													\
	CHANGE_PC