load program rom into proper offset in memory

This commit is contained in:
Vineet K 2024-05-24 12:55:44 +05:30
parent 8493837ef9
commit 10567e0be8

40
cpu.c
View File

@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define STATUS_UPDATE_ZERO(r) \
(regs.status.zero = r == 0)
@ -41,7 +42,7 @@ enum addressing_mode {
AM_INDIRECT_INDEXED,
};
uint8_t addrbus[0x10000];
uint8_t memory[0x10000];
/* example program taken from https://bugzmanov.github.io/nes_ebook/chapter_3_1.html */
uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 };
@ -82,7 +83,7 @@ lda(enum addressing_mode mode)
switch (mode) {
case AM_IMMEDIATE: /* $A9 */
val = program[regs.pc++];
val = memory[regs.pc++];
printf("arg1 $%02X\n", val);
regs.a = val;
break;
@ -119,22 +120,9 @@ void
interpret(void)
{
uint8_t opcode;
regs.pc = 0;
regs.status.unused = 1;
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');
for (;;) {
opcode = program[regs.pc++];
opcode = memory[regs.pc++];
printf("opcode: $%02X\n", opcode);
@ -190,6 +178,26 @@ interpret(void)
int
main(void)
{
if (sizeof(program) > (0x10000 - 0x8000)) {
fprintf(stderr, "program is too big for memory\n");
return 1;
}
memcpy(memory + 0x8000, program, sizeof(program));
regs.pc = 0x8000;
regs.status.unused = 1;
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;