Skip to content

Commit

Permalink
Again read write implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
somelun committed Sep 10, 2024
1 parent 9ecaa74 commit 41ac332
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/bus.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub const Bus = struct {

pub fn init() Bus {
var bus: Bus = Bus{};
bus.ppu = PPU.init(bus.rom.chr_rom, bus.rom.screen_mirroring);
bus.ppu = PPU.init();

return bus;
}
Expand All @@ -79,6 +79,7 @@ pub const Bus = struct {
self.rom = Rom.init(rom_path) catch {
return false;
};
self.ppu.updateRomData(self.rom.chr_rom, self.rom.screen_mirroring);

return true;
}
Expand All @@ -100,9 +101,14 @@ pub const Bus = struct {
data = self.wram[address & 0x07FF];
},

// read from PPU Data register
0x2007 => {
data = self.ppu.readData(address);
},

// PPU memory range also with mirroring
PPU_REG_BEGIN...PPU_REG_END => {
data = self.read8(address & 0x2007);
data = self.ppu.readData(address & 0x2007);
},

// ROM memory range
Expand All @@ -123,7 +129,13 @@ pub const Bus = struct {
self.wram[address & 0x07FF] = data;
},

// Controll, Address, Data PPu registers
0x2000, 0x2006, 0x2007 => {
self.ppu.writeData(data);
},

PPU_REG_BEGIN...PPU_REG_END => {
// for now calling self with mirrored address
self.write8(address & 0x2007, data);
},

Expand Down
15 changes: 9 additions & 6 deletions src/ppu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ pub const PPU = struct {

internal_buffer: u8 = 0,

pub fn init(chr_rom: []u8, mirroring: Mirroring) PPU {
pub fn init() PPU {
var ppu = PPU{};

ppu.chr_rom = chr_rom;
ppu.mirroring = mirroring;
ppu.addressRegister = AddressRegister.init();
ppu.controllerRegister = ControllerRegister.init();

return ppu;
}

pub fn updateRomData(self: *PPU, chr_rom: []u8, mirroring: Mirroring) void {
self.chr_rom = chr_rom;
self.mirroring = mirroring;
}

// instead of implementing PPU Data Register (0x2007) we just have this function
pub fn readData(self: *PPU) u8 {
const address: u16 = self.addressRegister.get();
Expand Down Expand Up @@ -128,9 +131,9 @@ pub const PPU = struct {
self.addressRegister.increment(self.controllerRegister.VRAMAddressIncrement());
},

// 0x2000...0x2FFF => {
// self.vram[self.mirrorVRAMAddress(address)] = value;
// },
0x2000...0x2FFF => {
self.vram[self.mirrorVRAMAddress(address)] = value;
},

0x3F00...0x3FFF => {
// Addresses 0x3F10/0x3F14/0x3F18/0x3F1C are mirrors of $3F00/$3F04/$3F08/$3F0C.
Expand Down

0 comments on commit 41ac332

Please sign in to comment.