/// <summary> /// Checks the PPU cycle and state and calls the scanLine() method when appropriate. /// </summary> public override void Tick() { PPU ppu = PPU.Instance; if (ppu.Cycle != 280) { return; } if (ppu.Scanline > 239 && ppu.Scanline < 261) { return; } if (ppu.flagShowbackground == 0 && ppu.flagShowSprite == 0) { return; } scanLine(); }
/// <summary> /// Put value in memory in the specified location /// </summary> /// <param name="location">Index to be written at</param> /// <param name="value">Value to be written at given index</param> public void WriteMemory(ushort address, byte value) { if (address < 0x2000) { memory[address % 0x0800] = value; } else if (address < 0x4000) { PPU ppu = PPU.Instance; ppu.writeRegister((ushort)(0x2000 + address % 8), value); } else if (address < 0x4014) { //set APU register value //throw new NotImplementedException(); } else if (address == 0x4014) { PPU ppu = PPU.Instance; ppu.writeRegister(address, value); } else if (address == 0x4015) { //throw new NotImplementedException(); } else if (address == 0x4016) { input1.Write(value); input2.Write(value); } else if (address == 0x4017) { //throw new NotImplementedException(); } else if (address >= 0x6000) { mapper.write(address, value); } else { throw new Exception("Invalid memory access requested"); } }
/// <summary> /// PPU writes to an address location. Method throws exception if /// PPU is given an address which leads to invalid memory access. /// </summary> /// <param name="addr"></param> /// <param name="value"></param> public void PpuWrite(ushort addr, byte value) { addr = (ushort)(addr % 0x4000); if (addr < 0x2000) { mapper.write(addr, value); } else if (addr < 0x3F00) { PPU ppu = PPU.Instance; byte mode = mapper.cart.Mirroring; ppu.nameTableData[MirroredAddress(mode, addr) % 2048] = value; } else if (addr < 0x4000) { PPU ppu = PPU.Instance; ppu.writePalette((ushort)(addr % 32), value); } else { throw new Exception("Invalid memory access requested"); } }
/// <summary> /// PPU reading from an address specified. Method throws an exception /// if address specifies invalid memory access. /// </summary> /// <param name="addr"></param> /// <returns></returns> public byte PpuRead(ushort addr) { addr = (ushort)(addr % 0x4000); if (addr < 0x2000) { return(mapper.read(addr)); } else if (addr < 0x3F00) { PPU ppu = PPU.Instance; byte mode = mapper.cart.Mirroring; return(ppu.nameTableData[MirroredAddress(mode, addr) % 2048]); } else if (addr < 0x4000) { PPU ppu = PPU.Instance; return(ppu.readPalette((ushort)(addr % 32))); } else { throw new Exception("Invalid memory access requested"); } }
/// <summary> /// Returns value held at the position passed in by the parameter /// Check if memory location is valid /// </summary> /// <param name="location">Memory index to be returned</param> /// <returns>Value held at index location</returns> public byte ReadMemory(ushort address) { if (address < 0x2000) { return(memory[address % 0x0800]); } else if (address < 0x4000) { PPU ppu = PPU.Instance; return(ppu.readRegister((ushort)(0x2000 + address % 8))); } else if (address == 0x4014) { PPU ppu = PPU.Instance; return(ppu.readRegister(address)); } else if (address == 0x4015) { throw new NotImplementedException(); } else if (address == 0x4016) { return(input1.Read()); } else if (address == 0x4017) { return(input2.Read()); } else if (address >= 0x6000) { return(mapper.read(address)); } else { throw new Exception("Invalid memory access requested"); } }