-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
510 lines (444 loc) · 13.1 KB
/
cpu.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
#include <stdio.h>
#include <stdlib.h>
#include "cpu.h"
#include "memory.h"
#include "logging.h"
static byte CPU_fetch_byte(struct CPU *cpu, struct memory *mem, unsigned long long *cycles); /*Fetch instruction(8bits) from program counter address*/
static word CPU_fetch_word(struct CPU *cpu, struct memory *mem, unsigned long long *cycles); /*Fetch 16bits from program counter address*/
static byte CPU_read_byte(struct CPU *cpu, struct memory *mem, word addr, unsigned long long *cycles); /*Read 8bits from memory address*/
static void CPU_split_word(word value, byte *low, byte *high, unsigned long long *cycles); /*Splits a word into two bytes*/
static void CPU_combine_bytes(word *value, byte low, byte high, unsigned long long *cycles); /*Combines two bytes into a word*/
static void CPU_set_flag_z(struct CPU *cpu, byte value);
static void CPU_set_flag_n(struct CPU *cpu, byte value);
static void CPU_set_flag_c_carry(struct CPU *cpu, byte value);
static void CPU_set_flag_c_borrow(struct CPU *cpu, byte value);
static void CPU_set_flags(struct CPU *cpu, byte inst, byte value);
static void CPU_operate_reg(struct CPU *cpu, byte *reg, enum operations oper, unsigned long long *cycles);
static void CPU_inc_program_counter(struct CPU* cpu, struct memory *mem, unsigned long long *cycles);
static byte zp_addr;
static word ab_addr;
static byte low;
static byte high;
static sbyte offset;
struct CPU* CPU_power_on(void){
struct CPU *cpu = malloc(sizeof(*cpu));
if (cpu != NULL){
return cpu;
}
else{
/*Handle error if needed*/
return NULL;
}
}
void CPU_power_off(struct CPU **cpu){
if (cpu != NULL){
free((*cpu));
(*cpu) = NULL;
return;
}
}
void CPU_reset(struct CPU *cpu){
if (cpu != NULL){
cpu->PC = 0x00;
cpu->SP = 0xFF;
cpu->C = 0;
cpu->Z = 0;
cpu->I = 0;
cpu->D = 0;
cpu->B = 0;
cpu->V = 0;
cpu->N = 0;
cpu->A = 0;
cpu->X = 0;
cpu->Y = 0;
return;
}
}
void CPU_state(struct CPU* cpu){
if (cpu != NULL){
printf("\t\t---CPU STATUS---\n");
printf("\t\tProgram counter: %d\n", cpu->PC);
printf("\t\tStack pointer: %d\n", cpu->SP);
printf("\t\tRegister A: %d\n", cpu->A);
printf("\t\tReguister X: %d\n", cpu->X);
printf("\t\tReguister Y: %d\n", cpu->Y);
printf("\t\tFlag C: %d\n", cpu->C);
printf("\t\tFlag Z: %d\n", cpu->Z);
printf("\t\tFlag I: %d\n", cpu->I);
printf("\t\tFlag D: %d\n", cpu->D);
printf("\t\tFlag B: %d\n", cpu->B);
printf("\t\tFlag V: %d\n", cpu->V);
printf("\t\tFlag N: %d\n", cpu->N);
}
}
void CPU_exec(struct CPU * cpu, struct memory* mem, unsigned long long cycles){
if (cpu != NULL && mem != NULL){
while(cycles){
/*One machine cycle is always used to fetch the next instruction(byte) from memory*/
byte inst = CPU_fetch_byte(cpu, mem, &cycles);
switch (inst)
{
case BVS:
offset = CPU_fetch_byte(cpu, mem, &cycles);
if (cpu->V == 1){
high = cpu->PC >> 8;
cpu->PC = cpu->PC + offset;
if (high == (cpu->PC >> 8)){ /*If their high bytes are equal they are in the same page*/
CPU_dec_cycles(&cycles, 1);
}
else{
CPU_dec_cycles(&cycles, 2);
}
}
break;
case CLC:
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, low);
break;
case CLD:
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, low);
break;
case CLI:
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, low);
break;
case CLV:
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, low);
break;
case CMP_IM:
low = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CMP_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CMP_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, ab_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPX_IM:
low = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPX_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPX_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, ab_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPY_IM:
low = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPY_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case CPY_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
low = CPU_read_byte(cpu, mem, ab_addr, &cycles);
CPU_set_flags(cpu, inst, low);
break;
case DEC_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
CPU_dec_cycles(&cycles, 1);
operate_addr(cpu, mem, zp_addr, DEC, &cycles);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, zp_addr, &cycles));
break;
case DEC_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
CPU_dec_cycles(&cycles, 1);
operate_addr(cpu, mem, ab_addr, DEC, &cycles);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, ab_addr, &cycles));
break;
case DEX:
CPU_operate_reg(cpu, &cpu->X, DEC, &cycles);
CPU_set_flags(cpu, inst, cpu->X);
break;
case DEY:
CPU_operate_reg(cpu, &cpu->Y, DEC, &cycles);
CPU_set_flags(cpu, inst, cpu->Y);
break;
case EOR_IM:
cpu->A ^= CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, cpu->A);
break;
case EOR_ZP:
cpu->A ^= CPU_read_byte(cpu, mem, CPU_fetch_byte(cpu, mem, &cycles), &cycles);
CPU_set_flags(cpu, inst, cpu->A);
break;
case EOR_AB:
cpu->A ^= CPU_read_byte(cpu, mem, CPU_fetch_word(cpu, mem, &cycles), &cycles);
CPU_set_flags(cpu, inst, cpu->A);
break;
case INC_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
operate_addr(cpu, mem, zp_addr, INC, &cycles);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, zp_addr, &cycles));
break;
case INC_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
operate_addr(cpu, mem, ab_addr, INC, &cycles);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, ab_addr, &cycles));
break;
case INX:
CPU_operate_reg(cpu, &cpu->X, INC, &cycles);
CPU_set_flags(cpu, inst, cpu->X);
break;
case INY:
CPU_operate_reg(cpu, &cpu->Y, INC, &cycles);
CPU_set_flags(cpu, inst, cpu->Y);
break;
case JMP:
cpu->PC = CPU_fetch_word(cpu, mem, &cycles);
break;
case JSR:
CPU_split_word(cpu->PC + 1, &low, &high, &cycles);
stack_push(cpu, mem, low, &cycles);
stack_push(cpu, mem, high, &cycles);
cpu->PC = CPU_fetch_word(cpu, mem, &cycles);
break;
case LDA_IM:
cpu->A = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, cpu->A);
break;
case LDA_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
cpu->A = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, cpu->A);
break;
case LDX_IM:
cpu->X = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, cpu->X);
break;
case LDX_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
cpu->X = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, cpu->X);
break;
case LDY_IM:
cpu->Y = CPU_fetch_byte(cpu, mem, &cycles);
CPU_set_flags(cpu, inst, cpu->Y);
break;
case LDY_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
cpu->Y = CPU_read_byte(cpu, mem, zp_addr, &cycles);
CPU_set_flags(cpu, inst, cpu->Y);
break;
/*LSR always sets the N flag to 0 so we dont need to use a machine cycle to check*/
case LSR_A:
CPU_set_flags(cpu, inst, cpu->A);
CPU_operate_reg(cpu, &cpu->A, RSH, &cycles);
break;
case LSR_ZP:
zp_addr = CPU_fetch_byte(cpu, mem, &cycles);
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, zp_addr, &cycles));
operate_addr(cpu, mem, zp_addr, RSH, &cycles);
break;
case LSR_AB:
ab_addr = CPU_fetch_word(cpu, mem, &cycles);
CPU_dec_cycles(&cycles, 1);
CPU_set_flags(cpu, inst, CPU_read_byte(cpu, mem, ab_addr, &cycles));
operate_addr(cpu, mem, ab_addr, RSH, &cycles);
break;
case NOP:
CPU_dec_cycles(&cycles, 1);
break;
case RTS:
high = stack_pop(cpu, mem, &cycles);
low = stack_pop(cpu, mem, &cycles);
CPU_combine_bytes(&cpu->PC, low, high, &cycles);
CPU_inc_program_counter(cpu, mem, &cycles);
break;
default:
break;
}
}
}
}
/*The following bound checking is redundant since the program counter
cannot be negative or higher than 65535 since its a word(unsigned short)
but will be kept as is since it doesn't cause any problems to check*/
static byte CPU_fetch_byte(struct CPU * cpu, struct memory* mem, unsigned long long *cycles){
if (cpu != NULL){
if (cpu->PC >= 0 && cpu->PC < sizeof(mem->cell)){
byte inst = mem->cell[cpu->PC];
CPU_inc_program_counter(cpu, mem, cycles);
return inst;
}
else{
exit_and_save_status(cpu, OUTOFBOUNDSMEM);
}
}
}
/*Two machine cycles are used to fetch a word(16bits) from memory*/
static word CPU_fetch_word(struct CPU * cpu, struct memory* mem, unsigned long long *cycles){
if (cpu != NULL){
if (cpu->PC >= 0 && cpu->PC < sizeof(mem->cell)){
word data = mem->cell[cpu->PC]; /*6502 is little endian so this is the least significant byte*/
CPU_inc_program_counter(cpu, mem, cycles);
data |= (mem->cell[cpu->PC] << 8); /*6502 is little endian so this is the most significant byte*/
CPU_inc_program_counter(cpu, mem, cycles);
return data;
}
else{
exit_and_save_status(cpu, OUTOFBOUNDSMEM);
}
}
}
static byte CPU_read_byte(struct CPU * cpu, struct memory* mem, word addr, unsigned long long *cycles){
if (cpu != NULL){
if (addr >= 0 && addr < sizeof(mem->cell)){
CPU_dec_cycles(cycles, 1);
byte data = mem->cell[addr];
return data;
}
else{
exit_and_save_status(cpu, OUTOFBOUNDSMEM);
}
}
}
static void CPU_split_word(word value, byte *low, byte *high, unsigned long long *cycles){
if (low != NULL && high != NULL){
CPU_dec_cycles(cycles, 1);
(*low) = ((value) & 0xFF);
(*high) = ((value) >> 8);
}
}
static void CPU_combine_bytes(word *value, byte low, byte high, unsigned long long *cycles){
if (value != NULL){
CPU_dec_cycles(cycles, 2);
(*value) = (high << 8);
(*value) |= low;
}
}
static void CPU_set_flag_z(struct CPU *cpu, byte value){
if (cpu != NULL) cpu->Z = (value == 0);
}
static void CPU_set_flag_n(struct CPU *cpu, byte value){
if (cpu != NULL) cpu->N = (value & 0b10000000 > 0);
}
static void CPU_set_flag_c_carry(struct CPU *cpu, byte value){
if (cpu != NULL) cpu->C = (value & 0b00000001);
}
static void CPU_set_flag_c_borrow(struct CPU *cpu, byte value){
if (cpu != NULL){
if (cpu->Y >= value){
cpu->C = 1;
}
else{
cpu->C = 0;
}
}
}
static void CPU_set_flags(struct CPU *cpu, byte inst, byte value){
if (cpu != NULL){
if (inst == CPY_IM || inst == CPY_ZP || inst == CPY_AB){
byte result = cpu->Y - value;
CPU_set_flag_c_borrow(cpu, value);
CPU_set_flag_z(cpu, result);
CPU_set_flag_n(cpu, result);
}
else if (inst == CPX_IM || inst == CPX_ZP || inst == CPX_AB){
byte result = cpu->X - value;
CPU_set_flag_c_borrow(cpu, value);
CPU_set_flag_z(cpu, result);
CPU_set_flag_n(cpu, result);
}
else if (inst == CMP_IM || inst == CMP_ZP || inst == CMP_AB){
byte result = cpu->A - value;
CPU_set_flag_c_borrow(cpu, value);
CPU_set_flag_z(cpu, result);
CPU_set_flag_n(cpu, result);
}
else if (inst == CLC){
cpu->C ^= cpu->C;
}
else if (inst == CLD){
cpu->D ^= cpu->D;
}
else if (inst == CLI){
cpu->I ^= cpu->I;
}
else if (inst == CLV){
cpu->V ^= cpu->V;
}
else if (inst == DEC_ZP || inst == DEC_AB ||
inst == DEX || inst == DEY ||
inst == EOR_IM || inst == EOR_ZP ||
inst == EOR_AB || inst == INC_ZP ||
inst == INC_AB || inst == INX ||
inst == INY || inst == LDA_IM ||
inst == LDA_ZP || inst == LDX_IM ||
inst == LDX_ZP || inst == LDY_IM ||
inst == LDY_ZP)
{
CPU_set_flag_z(cpu, value);
CPU_set_flag_n(cpu, value);
}
else if (inst == LSR_A || inst == LSR_ZP || inst == LSR_AB ){
CPU_set_flag_c_carry(cpu, value);
cpu->N = 0;
value >>= 8;
CPU_set_flag_z(cpu, value);
}
}
}
static void CPU_operate_reg(struct CPU* cpu, byte *reg, enum operations oper, unsigned long long *cycles){
if (cpu != NULL){
if (reg != NULL){
CPU_dec_cycles(cycles, 1);
switch(oper){
case DEC:
(*reg)--;
break;
case INC:
(*reg)++;
break;
case LSH:
(*reg) <<= 1;
break;
case RSH:
(*reg) >>= 1;
break;
}
}
else{
exit_and_save_status(cpu, OUTOFBOUNDSMEM);
}
}
}
static void CPU_inc_program_counter(struct CPU* cpu, struct memory *mem, unsigned long long *cycles){
if (cpu != NULL){
if (cpu->PC < sizeof(mem->cell)){
CPU_dec_cycles(cycles, 1);
cpu->PC++;
}
else{
exit_and_save_status(cpu, OUTOFBOUNDSMEM);
}
}
}
/*
Checking whether or not there are enough cycles left to
finish an instruction before starting its execution
*/
void CPU_dec_cycles(unsigned long long *cycles, unsigned short dec){
if ((*cycles) > 0 && (*cycles) >= dec){
(*cycles)-= dec;
}
else {
exit(0);
}
}