diff --git a/NES Emulator/bus.c b/NES Emulator/bus.c index 9530767..5ad9e53 100644 --- a/NES Emulator/bus.c +++ b/NES Emulator/bus.c @@ -28,6 +28,8 @@ struct Bus* createBus() // Create and insert cartridge bus->cartridge = createCartridge(bus, "roms/nestest.nes"); + printf("Reset vector: $%x\n", ((Word)readCartridge(bus->cartridge, 0xFFFD) << 8) | (readCartridge(bus->cartridge, 0xFFFC))); + return bus; } @@ -40,7 +42,7 @@ void destroyBus(struct Bus* bus) free(bus); } -Byte Read(struct Bus* bus, Word addr) +Byte readBus(struct Bus* bus, Word addr) { Byte val = 0; @@ -62,9 +64,9 @@ Byte Read(struct Bus* bus, Word addr) return val; } -void Write(struct Bus* bus, Word addr, Byte val) +void writeBus(struct Bus* bus, Word addr, Byte val) { - // Write to the appropriate memory or device + // writeCartridge to the appropriate memory or device if (addr <= 0x1FFF) // RAM (or one of the mirrored addresses) { bus->ram[addr & 0x7FF] = val; diff --git a/NES Emulator/bus.h b/NES Emulator/bus.h index 92b29d4..df9370a 100644 --- a/NES Emulator/bus.h +++ b/NES Emulator/bus.h @@ -22,7 +22,7 @@ struct Bus* createBus(); void destroyBus(struct Bus* bus); // Read/Write to and from the bus -Byte Read(struct Bus* bus, Word addr); -void Write(struct Bus* bus, Word addr, Byte val); +Byte readBus(struct Bus* bus, Word addr); +void writeBus(struct Bus* bus, Word addr, Byte val); #endif // _BUS_H_ \ No newline at end of file diff --git a/NES Emulator/cartridge.c b/NES Emulator/cartridge.c index 3b8b105..733326f 100644 --- a/NES Emulator/cartridge.c +++ b/NES Emulator/cartridge.c @@ -43,7 +43,7 @@ struct Cartridge* createCartridge(struct Bus* parent, const char* filepath) // Open ROM FILE* fp = fopen(filepath, "rb"); - // Read header + // readCartridge header struct INES_Header header; fread(&header, sizeof(header), 1, fp); @@ -55,12 +55,15 @@ struct Cartridge* createCartridge(struct Bus* parent, const char* filepath) } cartridge->chr_rom = (Byte*)malloc(0x2000 * (size_t)header.chr_rom_size); - if (cartridge->prg_rom == NULL) + if (cartridge->chr_rom == NULL) { fprintf(stderr, "Failed to allocate CHR memory for cartridge, aborting.\n"); exit(1); } + fread(cartridge->prg_rom, 0x4000, header.prg_rom_size, fp); + fread(cartridge->chr_rom, 0x2000, header.chr_rom_size, fp); + cartridge->bus = parent; return cartridge; } @@ -72,3 +75,35 @@ void destroyCartridge(struct Cartridge* cartridge) free(cartridge); } + +Byte readCartridge(struct Cartridge* cartridge, Word addr) +{ + // TODO: Force M000 mapper for now + Byte val = 0; + + if (0x6000 <= addr && addr <= 0x7FFF) // PRG RAM + { + // do nothing for now + } + else if (0x8000 <= addr && addr <= 0xFFFF) // PRG ROM + { + val = cartridge->prg_rom[addr & 0x1FFF]; + } + + return val; +} + +void writeCartridge(struct Cartridge* cartridge, Word addr, Byte val) +{ + // TODO: Force M000 mapper for now + + if (0x6000 <= addr && addr <= 0x7FFF) // PRG RAM + { + // do nothing for now + } + else if (0x8000 <= addr && addr <= 0xFFFF) // PRG ROM + { + cartridge->prg_rom[addr & 0x1FFF] = val; + } + +} diff --git a/NES Emulator/cartridge.h b/NES Emulator/cartridge.h index 6bdcf37..2ab2c19 100644 --- a/NES Emulator/cartridge.h +++ b/NES Emulator/cartridge.h @@ -16,4 +16,8 @@ struct Cartridge struct Cartridge* createCartridge(struct Bus* parent, const char* filepath); void destroyCartridge(struct Cartridge* cartridge); +// readCartridge/writeCartridge to and from the cartridge +Byte readCartridge(struct Cartridge* cartridge, Word addr); +void writeCartridge(struct Cartridge* cartridge, Word addr, Byte val); + #endif //_CARTRIDGE_H_ \ No newline at end of file