emu_nes/cpu.c

317 lines
5.7 KiB
C
Raw Normal View History

2024-05-20 10:09:19 -04:00
#include <stdint.h>
#include <stdio.h>
2024-06-08 08:11:06 -04:00
#include <stdlib.h>
#include <string.h>
2024-06-08 08:11:06 -04:00
#define MAX(a, b) ((a > b) ? a : b)
#define STATUS_UPDATE_ZERO(r) \
(regs.status.zero = r == 0)
#define STATUS_UPDATE_NEGATIVE(r) \
(regs.status.negative = ((r & (1 << 7)) > 0))
2024-05-20 10:09:19 -04:00
struct cpu_flags {
uint8_t carry : 1;
uint8_t zero : 1;
uint8_t interrupt_disable : 1;
uint8_t decimal_mode : 1;
uint8_t brk : 1;
uint8_t unused : 1;
uint8_t overflow : 1;
uint8_t negative : 1;
};
struct registers {
uint8_t a, x, y, sp;
2024-05-20 10:09:19 -04:00
struct cpu_flags status;
uint16_t pc;
};
struct registers regs;
enum addressing_mode {
2024-06-04 06:46:11 -04:00
AM_ACC,
AM_IMM,
AM_ZP,
AM_ZP_X,
AM_ZP_Y,
AM_REL,
AM_ABS,
AM_ABS_X,
AM_ABS_Y,
AM_IND,
AM_IND_X,
AM_IND_Y,
2024-05-20 10:09:19 -04:00
};
2024-06-04 06:24:09 -04:00
/* 64K address space, 16bit words */
uint8_t memory[0x16000];
2024-05-20 10:09:19 -04:00
/* example program taken from https://bugzmanov.github.io/nes_ebook/chapter_3_1.html */
uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 };
2024-06-04 06:24:09 -04:00
uint8_t
2024-06-08 08:11:06 -04:00
peek(uint16_t addr)
2024-06-04 06:24:09 -04:00
{
return memory[addr];
}
2024-06-08 08:11:06 -04:00
uint16_t
2024-06-04 06:24:09 -04:00
peek16(uint16_t addr)
{
/* bytes are stored in little-endian (low then high) */
return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8);
}
2024-06-08 08:11:06 -04:00
void
adc(enum addressing_mode mode)
{
uint8_t arg, val;
uint16_t sum; // 16-bit sum makes it easier to determine carry flag
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
arg = peek(regs.pc++);
else
arg = peek16(regs.pc++);
switch (mode) {
case AM_IMM: /* $69 */
val = arg;
break;
case AM_ZP: /* $65 */
val = peek(arg % 256);
break;
case AM_ABS: /* $6D */
val = peek16(arg);
break;
case AM_ZP_X: /* $75 */
val = peek((arg + regs.x) % 256);
break;
case AM_ABS_X: /* $7D */
val = peek16(arg + regs.x);
break;
case AM_ABS_Y: /* $79 */
val = peek16(arg + regs.y);
break;
case AM_IND_X: /* $61 */
val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256);
break;
case AM_IND_Y: /* $71 */
val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y);
break;
default:
fprintf(stderr, "INVALID ADC ADDRESSING MODE\n");
abort();
return;
}
sum = regs.a + val + regs.status.carry;
regs.a = sum & 0xFF;
regs.status.carry = sum > 0xFF;
/* overflow flag formula: https://stackoverflow.com/a/29224684 */
regs.status.overflow = ~(regs.a ^ arg) & (regs.a ^ sum) & 0x80;
STATUS_UPDATE_ZERO(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
}
void
brk(void)
2024-05-24 02:20:08 -04:00
{
/* $00 */
2024-05-24 02:20:08 -04:00
/* TODO: push regs.pc and regs.status to stack and load IRQ vector */
regs.status.brk = 1;
return;
}
void
tax(void)
{
/* $AA */
regs.x = regs.a;
STATUS_UPDATE_ZERO(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
}
void
inx(void)
{
/* $E8 */
regs.x++;
STATUS_UPDATE_ZERO(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
}
void
lda(enum addressing_mode mode)
2024-05-24 02:20:08 -04:00
{
2024-06-04 06:24:09 -04:00
uint8_t arg, val;
2024-06-04 08:18:34 -04:00
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
arg = peek(regs.pc++);
else
arg = peek16(regs.pc++);
2024-05-24 02:20:08 -04:00
switch (mode) {
2024-06-04 06:46:11 -04:00
case AM_IMM: /* $A9 */
2024-06-04 06:24:09 -04:00
val = arg;
2024-05-24 02:20:08 -04:00
break;
2024-06-04 06:46:11 -04:00
case AM_ZP: /* $A5 */
2024-06-04 06:24:09 -04:00
val = peek(arg % 256);
break;
2024-06-04 06:46:11 -04:00
case AM_ABS: /* $AD */
2024-06-04 08:18:34 -04:00
val = peek16(arg);
2024-06-04 06:24:09 -04:00
break;
2024-06-04 06:46:11 -04:00
case AM_ZP_X: /* $B5 */
2024-06-04 06:24:09 -04:00
val = peek((arg + regs.x) % 256);
break;
2024-06-04 06:46:11 -04:00
case AM_ABS_X: /* $BD */
2024-06-04 08:18:34 -04:00
val = peek16(arg + regs.x);
break;
2024-06-04 06:46:11 -04:00
case AM_ABS_Y: /* $B9 */
2024-06-04 08:18:34 -04:00
val = peek16(arg + regs.y);
break;
2024-06-04 06:46:11 -04:00
case AM_IND_X: /* $A1 */
2024-06-04 06:24:09 -04:00
val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256);
break;
2024-06-04 06:46:11 -04:00
case AM_IND_Y: /* $B1 */
2024-06-04 06:24:09 -04:00
val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y);
2024-05-24 02:20:08 -04:00
break;
default:
2024-06-04 08:18:34 -04:00
fprintf(stderr, "INVALID LDA ADDRESSING MODE\n");
abort();
2024-05-24 02:20:08 -04:00
return;
}
2024-06-04 06:24:09 -04:00
printf("arg1 $%02X\n", arg);
regs.a = val;
STATUS_UPDATE_ZERO(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
2024-05-24 02:20:08 -04:00
}
void
interpret(void)
2024-05-20 10:09:19 -04:00
{
uint8_t opcode;
for (;;) {
2024-06-04 06:24:09 -04:00
opcode = peek(regs.pc++);
2024-05-20 10:09:19 -04:00
printf("opcode: $%02X\n", opcode);
2024-05-20 10:09:19 -04:00
switch (opcode) {
2024-05-24 02:20:08 -04:00
case 0x00:
brk();
return;
2024-06-08 08:11:06 -04:00
case 0x61:
adc(AM_IND_X);
break;
case 0x65:
adc(AM_ZP);
break;
case 0x69:
adc(AM_IMM);
break;
case 0x6D:
adc(AM_ABS);
break;
case 0x71:
adc(AM_IND_Y);
break;
case 0x75:
adc(AM_ZP_X);
break;
case 0x79:
adc(AM_ABS_Y);
break;
case 0x7D:
adc(AM_ABS_X);
break;
case 0xa1:
2024-06-04 06:46:11 -04:00
lda(AM_IND_X);
2024-05-24 02:20:08 -04:00
break;
case 0xa5:
2024-06-04 06:46:11 -04:00
lda(AM_ZP);
2024-05-24 02:20:08 -04:00
break;
case 0xa9:
2024-06-04 06:46:11 -04:00
lda(AM_IMM);
break;
case 0xaa:
tax();
2024-05-24 02:20:08 -04:00
break;
case 0xad:
2024-06-04 06:46:11 -04:00
lda(AM_ABS);
2024-05-24 02:20:08 -04:00
break;
case 0xb1:
2024-06-04 06:46:11 -04:00
lda(AM_IND_Y);
break;
case 0xb5:
2024-06-04 06:46:11 -04:00
lda(AM_ZP_X);
2024-05-24 02:20:08 -04:00
break;
case 0xb9:
2024-06-04 06:46:11 -04:00
lda(AM_ABS_Y);
2024-05-24 02:20:08 -04:00
break;
case 0xbd:
2024-06-04 06:46:11 -04:00
lda(AM_ABS_X);
2024-05-24 02:20:08 -04:00
break;
case 0xe8:
inx();
2024-05-24 02:20:08 -04:00
break;
2024-05-20 10:09:19 -04:00
default:
printf("opcode $%02X not implemented\n", opcode);
2024-05-20 10:09:19 -04:00
break;
}
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
regs.status.zero, regs.status.interrupt_disable,
regs.status.decimal_mode, regs.status.brk,
regs.status.unused, regs.status.overflow,
regs.status.negative);
printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n",
regs.a, regs.x, regs.y, regs.sp, regs.pc);
2024-05-20 10:09:19 -04:00
}
}
2024-06-04 06:24:09 -04:00
/* https://www.nesdev.org/wiki/CPU_power_up_state */
void
cpu_init(void)
{
regs.a = regs.x = regs.y = 0;
regs.pc = 0xFFFC;
regs.sp = 0xFD;
memset(&regs.status, 0, sizeof(regs.status));
regs.status.unused = 1;
}
int
main(void)
{
2024-06-04 06:24:09 -04:00
cpu_init();
if (sizeof(program) > (0x10000 - 0x8000)) {
fprintf(stderr, "program is too big for memory\n");
return 1;
}
2024-06-08 08:11:06 -04:00
memcpy(memory + 0x8000, program, sizeof(program));
regs.pc = 0x8000;
printf("Initial State:\n");
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
regs.status.zero, regs.status.interrupt_disable,
regs.status.decimal_mode, regs.status.brk,
regs.status.unused, regs.status.overflow,
regs.status.negative);
printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n",
regs.a, regs.x, regs.y, regs.sp, regs.pc);
putchar('\n');
interpret();
return 0;
}