added rom/bus interaction
This commit is contained in:
parent
abb9b2e54a
commit
4b22287ad0
|
@ -28,6 +28,8 @@ struct Bus* createBus()
|
||||||
// Create and insert cartridge
|
// Create and insert cartridge
|
||||||
bus->cartridge = createCartridge(bus, "roms/nestest.nes");
|
bus->cartridge = createCartridge(bus, "roms/nestest.nes");
|
||||||
|
|
||||||
|
printf("Reset vector: $%x\n", ((Word)readCartridge(bus->cartridge, 0xFFFD) << 8) | (readCartridge(bus->cartridge, 0xFFFC)));
|
||||||
|
|
||||||
return bus;
|
return bus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ void destroyBus(struct Bus* bus)
|
||||||
free(bus);
|
free(bus);
|
||||||
}
|
}
|
||||||
|
|
||||||
Byte Read(struct Bus* bus, Word addr)
|
Byte readBus(struct Bus* bus, Word addr)
|
||||||
{
|
{
|
||||||
Byte val = 0;
|
Byte val = 0;
|
||||||
|
|
||||||
|
@ -62,9 +64,9 @@ Byte Read(struct Bus* bus, Word addr)
|
||||||
return val;
|
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)
|
if (addr <= 0x1FFF) // RAM (or one of the mirrored addresses)
|
||||||
{
|
{
|
||||||
bus->ram[addr & 0x7FF] = val;
|
bus->ram[addr & 0x7FF] = val;
|
||||||
|
|
|
@ -22,7 +22,7 @@ struct Bus* createBus();
|
||||||
void destroyBus(struct Bus* bus);
|
void destroyBus(struct Bus* bus);
|
||||||
|
|
||||||
// Read/Write to and from the bus
|
// Read/Write to and from the bus
|
||||||
Byte Read(struct Bus* bus, Word addr);
|
Byte readBus(struct Bus* bus, Word addr);
|
||||||
void Write(struct Bus* bus, Word addr, Byte val);
|
void writeBus(struct Bus* bus, Word addr, Byte val);
|
||||||
|
|
||||||
#endif // _BUS_H_
|
#endif // _BUS_H_
|
|
@ -43,7 +43,7 @@ struct Cartridge* createCartridge(struct Bus* parent, const char* filepath)
|
||||||
// Open ROM
|
// Open ROM
|
||||||
FILE* fp = fopen(filepath, "rb");
|
FILE* fp = fopen(filepath, "rb");
|
||||||
|
|
||||||
// Read header
|
// readCartridge header
|
||||||
struct INES_Header header;
|
struct INES_Header header;
|
||||||
fread(&header, sizeof(header), 1, fp);
|
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);
|
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");
|
fprintf(stderr, "Failed to allocate CHR memory for cartridge, aborting.\n");
|
||||||
exit(1);
|
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;
|
cartridge->bus = parent;
|
||||||
return cartridge;
|
return cartridge;
|
||||||
}
|
}
|
||||||
|
@ -72,3 +75,35 @@ void destroyCartridge(struct Cartridge* cartridge)
|
||||||
|
|
||||||
free(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -16,4 +16,8 @@ struct Cartridge
|
||||||
struct Cartridge* createCartridge(struct Bus* parent, const char* filepath);
|
struct Cartridge* createCartridge(struct Bus* parent, const char* filepath);
|
||||||
void destroyCartridge(struct Cartridge* cartridge);
|
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_
|
#endif //_CARTRIDGE_H_
|
Loading…
Reference in a new issue