summaryrefslogtreecommitdiff
path: root/emu/6502.c
blob: 53af7a044b835c6ec2281d6371e1d430ab7e5c01 (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
#include "6502.h"
#include "memory.h"

#include <stdio.h>

static const uint16_t reset_vector = 0xFFFC;
static const uint16_t ZP_base = 0x0;
static const uint16_t SP_base = 0x100;

void cpu_6502_init( cpu_6502_t *cpu, bus_t *bus, bool initialize )
{
	cpu->bus = bus;
	cpu->debug_flags = 0;
	cpu->steps = 0;
	cpu->cycles = 0;
	cpu->error_state = ERROR_STATE_OK;
	
	if( initialize ) {
		cpu->PC = 0x0000;
		cpu->SP = 0x00;
		cpu->A = 0x00;
		cpu->X = 0x00;
		cpu->Y = 0x00;
	}
}

uint16_t cpu_6502_read_word( cpu_6502_t *cpu, uint16_t addr )
{
	return cpu_6502_read_byte( cpu, addr ) + ( cpu_6502_read_byte( cpu, addr + 1 ) << 8 );
}
	
void cpu_6502_push_byte( cpu_6502_t *cpu, uint8_t data )
{
	cpu_6502_write_byte( cpu, SP_base + cpu->SP, data );
	cpu->SP--;
}

void cpu_6502_push_word( cpu_6502_t *cpu, uint16_t data )
{
	cpu_6502_push_byte( cpu, ( data >> 8 ) & 0xFF );
	cpu_6502_push_byte( cpu, data & 0xFF );
}

uint8_t cpu_6502_pop_byte( cpu_6502_t *cpu )
{
	uint8_t data;
	
	cpu->SP++;
	data = cpu_6502_read_byte( cpu, SP_base + cpu->SP );
	
	return data;
}

uint16_t cpu_6502_pop_word( cpu_6502_t *cpu )
{
	uint8_t low;
	uint8_t high;
	uint16_t data;

	low = cpu_6502_pop_byte( cpu );
	high = cpu_6502_pop_byte( cpu );
	
	data = ( high << 8 ) + low;
	
	return data;
}

void cpu_6502_reset( cpu_6502_t *cpu )
{
	cpu->PC = cpu_6502_read_word( cpu, reset_vector );
}

uint8_t cpu_6502_read_byte( cpu_6502_t *cpu, uint16_t addr )
{
	return cpu->bus->base.vtable->read( cpu->bus, addr );
}

void cpu_6502_write_byte( cpu_6502_t *cpu, uint16_t addr, uint8_t data )
{
	cpu->bus->base.vtable->write( cpu->bus, addr, data );
}

void cpu_6502_write_word( cpu_6502_t *cpu, uint16_t addr, uint16_t data )
{
	cpu->bus->base.vtable->write( cpu->bus, addr, ( data && 0x00FF ) );
	cpu->bus->base.vtable->write( cpu->bus, addr + 1, ( data && 0x00FF ) );
}

void cpu_6502_run_steps( cpu_6502_t *cpu, int steps )
{
	if( steps != -1 ) {
		for( int i = 1; i <= steps && cpu->error_state == ERROR_STATE_OK; i++ ) {
			cpu_6502_step( cpu );
		}
	} else {
		while( cpu->error_state == ERROR_STATE_OK ) {
			cpu_6502_step( cpu );
		}
	}
}

void cpu_6502_run_cycles( cpu_6502_t *cpu, int cycles )
{
	if( cycles != -1 ) {
		int start_cycles = cpu->cycles;
		while( cpu->cycles < start_cycles + cycles && cpu->error_state == ERROR_STATE_OK ) {
			cpu_6502_step( cpu );
		}
	} else {
		while( cpu->error_state == ERROR_STATE_OK ) {
			cpu_6502_step( cpu );
		}
	}
}

static void handle_zero( cpu_6502_t *cpu, uint8_t x )
{
	if( x == 0 ) {
		cpu->PS |= PS_Z;
	} else {
		cpu->PS &= ~PS_Z;
	}
}

static void handle_sign( cpu_6502_t *cpu, uint8_t x )
{
	if( x & 0x80 ) {
		cpu->PS |= PS_N;
	} else {
		cpu->PS &= ~ PS_N;
	}
}

bool cpu_6502_is_negative( cpu_6502_t *cpu )
{
	return cpu->PS & PS_N;
}

bool cpu_6502_is_overflow( cpu_6502_t *cpu )
{
	return cpu->PS & PS_V;
}

bool cpu_6502_is_brk( cpu_6502_t *cpu )
{
	return cpu->PS & PS_B;
}

bool cpu_6502_is_decimal( cpu_6502_t *cpu )
{
	return cpu->PS & PS_D;
}

bool cpu_6502_is_interrupt( cpu_6502_t *cpu )
{
	return cpu->PS & PS_I;
}

bool cpu_6502_is_zero( cpu_6502_t *cpu )
{
	return cpu->PS & PS_Z;
}

bool cpu_6502_is_carry( cpu_6502_t *cpu )
{
	return cpu->PS & PS_C;
}

static const int cycles[NOF_OPCODES] = {
	/*           0      1      2      3      4      5      6      7      8      9      A      B      C      D      E      F */
	/* 0 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 1 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 2 */      6,     0,     0,     0,     0,     0,     0,     0,     0,     0,     2,     0,     0,     0,     0,     0,
	/* 3 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 4 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     3,     0,     0,     0,
	/* 5 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 6 */      6,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 7 */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* 8 */      0,     0,     0,     0,     0,     0,     3,     0,     2,     0,     0,     0,     0,     0,     4,     0,
	/* 9 */      2,     0,     0,     0,     0,     0,     0,     0,     0,     0,     2,     0,     0,     0,     0,     0,
	/* A */      2,     0,     2,     0,     3,     3,     3,     0,     0,     2,     0,     0,     0,     0,     0,     0,
	/* B */      2,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* C */      0,     0,     0,     0,     0,     0,     0,     0,     2,     0,     2,     0,     0,     0,     0,     0,
	/* D */      2,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	/* E */      3,     0,     0,     0,     0,     0,     5,     0,     0,     2,     2,     0,     0,     0,     0,     0,
	/* F */      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0
};

static const char mnemonic[NOF_OPCODES][MAX_MENMONIC_LENGTH] = {
	/*         0      1      2      3      4      5      6      7      8      9      A      B      C      D      E      F */
	/* 0 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 1 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 2 */  "JSR", "???", "???", "???", "???", "???", "???", "???", "???", "???", "ROL", "???", "???", "???", "???", "???",
	/* 3 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 4 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "JMP", "???", "???", "???",
	/* 5 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 6 */  "RTS", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 7 */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* 8 */  "???", "???", "???", "???", "???", "???", "STX", "???", "DEY", "???", "???", "???", "???", "???", "STX", "???",
	/* 9 */  "BCC", "???", "???", "???", "???", "???", "???", "???", "???", "???", "TXS", "???", "???", "???", "???", "???",
	/* A */  "LDY", "???", "LDX", "???", "LDY", "LDA", "LDX", "???", "???", "LDA", "???", "???", "???", "???", "???", "???",
	/* B */  "BCS", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* C */  "???", "???", "???", "???", "???", "???", "???", "???", "INY", "???", "DEX", "???", "???", "???", "???", "???",
	/* D */  "BNE", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???",
	/* E */  "CPX", "???", "???", "???", "???", "???", "INC", "???", "???", "SBC", "NOP", "???", "???", "???", "???", "???",
	/* F */  "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???"
};

/*
             "BRK", "ORA", ""   , "", "TSB", "ORA", "ASL", "RMB0", "PHP", "ORA", "ASL", ""   , "TSB", "ORA", "ASL", "BBR0",
             "BPL", "ORA", "ORA", "", "TRB", "ORA", "ASL", "RMB1", "CLC", "ORA", "INC", ""   , "TRB", "ORA", "ASL", "BBR1",
             "JSR", "AND", ""   , "", "BIT", "AND", "ROL", "RMB2", "PLP", "AND", "ROL", ""   , "BIT", "AND", "ROL", "BBR2",
             "BMI", "AND", "AND", "", "BIT", "AND", "ROL", "RMB3", "SEC", "AND", "DEC", ""   , "BIT", "AND", "ROL", "BBR3",
             "RTI", "EOR", ""   , "", ""   , "EOR", "LSR", "RMB4", "PHA", "EOR", "LSR", ""   , "JMP", "EOR", "LSR", "BBR4",
             "BVC", "EOR", "EOR", "", ""   , "EOR", "LSR", "RMB5", "CLI", "EOR", "PHY", ""   , ""   , "EOR", "LSR", "BBR5",
             "RTS", "ADC", ""   , "", "STZ", "ADC", "ROR", "RMB6", "PLA", "ADC", "ROR", ""   , "JMP", "ADC", "ROR", "BBR6",
             "BVS", "ADC", "ADC", "", "STZ", "ADC", "ROR", "RMB7", "SEI", "ADC", "PLY", ""   , "JMP", "ADC", "ROR", "BBR7",
             "BRA", "STA", ""   , "", "STY", "STA", "STX", "SMB0", "DEY", "BIT", "TXA", ""   , "STY", "STA", "STX", "BBS0",
             "BCC", "STA", "STA", "", "STY", "STA", "STX", "SMB1", "TYA", "STA", "TXS", ""   , "STZ", "STA", "STZ", "BBS1",
             "LDY", "LDA", "LDX", "", "LDY", "LDA", "LDX", "SMB2", "TAY", "LDA", "TAX", ""   , "LDY", "LDA", "LDX", "BBS2",
             "BCS", "LDA", "LDA", "", "LDY", "LDA", "LDX", "SMB3", "CLV", "LDA", "TSX", ""   , "LDY", "LDA", "LDX", "BBS3",
             "CPY", "CMP", ""   , "", "CPY", "CMP", "DEC", "SMB4", "INY", "CMP", "DEX", "WAI", "CPY", "CMP", "DEC", "BBS4",
             "BNE", "CMP", "CMP", "", ""   , "CMP", "DEC", "SMB5", "CLD", "CMP", "PHX", "STP", ""   , "CMP", "DEC", "BBS5",
             "CPX", "SBC", ""   , "", "CPX", "SBC", "INC", "SMB6", "INX", "SBC", "NOP", ""   , "CPX", "SBC", "INC", "BBS6",
             "BEQ", "SBC", "SBC", "", ""   , "SBC", "INC", "SMB7", "SED", "SBC", "PLX", ""   , ""   , "SBC", "INC", "BBS7"	
*/

void cpu_6502_get_opcode_mnemonic( cpu_6502_t *cpu, char *buf, int buflen, uint16_t addr )
{
	uint8_t opcode;
	
	opcode = cpu_6502_read_byte( cpu, addr );
	
	snprintf( buf, buflen, "%s (%02X)", mnemonic[opcode], opcode );
}

void cpu_6502_print_debug( cpu_6502_t *cpu )
{
	if( cpu->debug_flags & DEBUG_STATUS ) {
		cpu_6502_print_state( cpu, cpu->PC-1 );
	}
	if( cpu->debug_flags & DEBUG_STACK ) {
		cpu_6502_print_stack( cpu );
	}
	if( cpu->debug_flags & DEBUG_ZERO_PAGE ) {
		cpu_6502_print_zerop_page( cpu );
	}
}
	
void cpu_6502_print_state( cpu_6502_t *cpu, uint16_t addr )
{
	char opcode_str[MAX_OPCODE_MNEMONIC_STRING_LENGTH];
	
	cpu_6502_get_opcode_mnemonic( cpu, opcode_str, MAX_OPCODE_MNEMONIC_STRING_LENGTH, addr );
	
	fprintf( stderr, "PC: %04X SP: 01%02X PS: %02X %c%c-%c%c%c%c%c A: %02X X: %02X Y: %02X steps: %d cycles: %d OP: %s\n", 
		cpu->PC, cpu->SP, cpu->PS,
		cpu_6502_is_negative( cpu ) ? 'N' : 'n',
		cpu_6502_is_overflow( cpu ) ? 'V' : 'v',
		cpu_6502_is_brk( cpu ) ? 'B' : 'b',
		cpu_6502_is_decimal( cpu ) ? 'D' : 'd',
		cpu_6502_is_interrupt( cpu ) ? 'I' : 'i',
		cpu_6502_is_zero( cpu ) ? 'Z' : 'z',
		cpu_6502_is_carry( cpu ) ? 'C' : 'c',
		cpu->A, cpu->X, cpu->Y, cpu->steps,
		cpu->cycles, opcode_str );
}

static void update_negative_and_sign( cpu_6502_t *cpu, uint8_t x )
{
	handle_zero( cpu, x );
	handle_sign( cpu, x );
}

void cpu_6502_print_memory( cpu_6502_t *cpu, uint16_t base, uint8_t size )
{
	for( uint16_t addr = base; addr <= base + size; addr++ ) {
		fprintf( stderr, " %02X", cpu_6502_read_byte( cpu, addr ) );
	}
	fputs( "\n", stderr );
}

void cpu_6502_print_stack( cpu_6502_t *cpu )
{
	cpu_6502_print_memory( cpu, SP_base, 255 );
}

void cpu_6502_print_zerop_page( cpu_6502_t *cpu )
{
	cpu_6502_print_memory( cpu, ZP_base, 255 );
}

void cpu_6502_step( cpu_6502_t *cpu )
{
	uint8_t opcode;
	uint8_t operand8;
	uint16_t operand16;
	uint16_t tmp;
	
	opcode = cpu_6502_read_byte( cpu, cpu->PC );
	cpu->PC++;
	
	cpu_6502_print_debug( cpu );
	
	switch( opcode ) {
		case LDX_IMM:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->X = operand8;
			update_negative_and_sign( cpu, cpu->X );
			break;

		case LDX_ZERO:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->X = cpu_6502_read_byte( cpu, operand8 );
			update_negative_and_sign( cpu, cpu->X );
			break;

		case LDY_IMM:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->Y = operand8;
			update_negative_and_sign( cpu, cpu->Y );
			break;

		case LDY_ZERO:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->Y = cpu_6502_read_byte( cpu, operand8 );
			update_negative_and_sign( cpu, cpu->Y );
			break;
			
		case LDA_IMM:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->A = operand8;
			update_negative_and_sign( cpu, cpu->A );
			break;
			
		case LDA_ZERO:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu->A = cpu_6502_read_byte( cpu, operand8 );
			update_negative_and_sign( cpu, cpu->A );
			break;
		
		case STX_ZERO:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			cpu_6502_write_byte( cpu, operand8, cpu->X );
			break;
		
		case STX_ABS:
			operand16 = cpu_6502_read_word( cpu, cpu->PC );
			cpu->PC += 2;
			cpu_6502_write_byte( cpu, operand16, cpu->X );
			break;

		case DEX_IMPL:
			cpu->X--;
			update_negative_and_sign( cpu, cpu->X );
			break;
			
		case DEY_IMPL:
			cpu->Y--;
			update_negative_and_sign( cpu, cpu->Y );
			break;
		
		case INY_IMPL:
			cpu->Y++;
			update_negative_and_sign( cpu, cpu->Y );
			break;
		
		case INC_ZERO:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			tmp = cpu_6502_read_byte( cpu, operand8 );
			tmp++;
			cpu_6502_write_byte( cpu, operand8, tmp );
			update_negative_and_sign( cpu, tmp );
			break;
			
		case ROL_ACC:
			tmp = ( cpu->A << 1 ) | cpu_6502_is_carry( cpu );
			if( tmp & 0xFF00 ) {
				cpu->PS |= PS_C;
			} else {
				cpu->PS &= ~PS_C;
			}
			cpu->A = tmp & 0xFF;
			update_negative_and_sign( cpu, cpu->A );
			break;
		
		case CPX_IMM:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			tmp = ( cpu->X - operand8 ) & 0xFF;
			if( cpu->X >= operand8 ) {
				cpu->PS |= PS_C;
			} else {
				cpu->PS &= ~PS_C;
			}
			update_negative_and_sign( cpu, tmp );
			break;
		
		case SBC_IMM:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			tmp = cpu->A - operand8 - ( cpu_6502_is_carry( cpu ) ? 0 : 1 );
			update_negative_and_sign( cpu, tmp );
			if( tmp & 0xFF00 ) {
				cpu->PS &= ~PS_C;
			} else {
				cpu->PS |= PS_C;
			}
			if( ( ( cpu->A ^ tmp ) & 0x80 ) && ( ( cpu->A ^ operand8 ) & 0x80 ) ) {
				cpu->PS |= PS_V;
			} else {
				cpu->PS &= ~PS_V;
			}
			cpu->A = tmp & 0xFF;
			break;
		
		case BNE_REL:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			if( !cpu_6502_is_zero( cpu ) ) {
				cpu->PC += (int8_t)operand8;
			}
			break;
			
		case BCC_REL:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			if( !cpu_6502_is_carry( cpu ) ) {
				cpu->PC += (int8_t)operand8;
			}
			break;

		case BCS_REL:
			operand8 = cpu_6502_read_byte( cpu, cpu->PC );
			cpu->PC++;
			if( cpu_6502_is_carry( cpu ) ) {
				cpu->PC += (int8_t)operand8;
			}
			break;
			
		case JMP_ABS:
			cpu->PC = cpu_6502_read_word( cpu, cpu->PC );
			break;
		
		case JSR_ABS:
			cpu_6502_push_word( cpu, cpu->PC + 1 );
			cpu->PC = cpu_6502_read_word( cpu, cpu->PC );
			break;
		
		case RTS_IMPL:
			cpu->PC = cpu_6502_pop_word( cpu );
			cpu->PC++;
			break;

		case TXS_IMPL:
			cpu->SP = cpu->X;
			break;
			
		case NOP_IMPL:
			break;
			
		default:
			fprintf( stderr, "ERROR: Illegal opcode %02X at PC %04X\n", opcode, cpu->PC );
			cpu->error_state |= ERROR_STATE_ILLEGAL_OPCODE;
	}
	
	cpu->steps++;
	cpu->cycles += cycles[opcode];
}