From 6fc4ff01c55f4bf386e9e93d450f2370e7c95db2 Mon Sep 17 00:00:00 2001 From: zeromus Date: Fri, 30 Mar 2012 00:35:15 +0000 Subject: [PATCH] a2600-add mapper system --- BizHawk.Emulation/BizHawk.Emulation.csproj | 3 ++ .../Consoles/Atari/2600/Atari2600.Core.cs | 37 +++++++++++++++-- .../Consoles/Atari/2600/Atari2600.cs | 8 +++- .../Consoles/Atari/2600/Mappers/m2K.cs | 20 ++++++++++ .../Consoles/Atari/2600/Mappers/m4K.cs | 20 ++++++++++ .../Consoles/Atari/2600/Mappers/mF8.cs | 40 +++++++++++++++++++ 6 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 BizHawk.Emulation/Consoles/Atari/2600/Mappers/m2K.cs create mode 100644 BizHawk.Emulation/Consoles/Atari/2600/Mappers/m4K.cs create mode 100644 BizHawk.Emulation/Consoles/Atari/2600/Mappers/mF8.cs diff --git a/BizHawk.Emulation/BizHawk.Emulation.csproj b/BizHawk.Emulation/BizHawk.Emulation.csproj index 509ed439fd..870f8f3736 100644 --- a/BizHawk.Emulation/BizHawk.Emulation.csproj +++ b/BizHawk.Emulation/BizHawk.Emulation.csproj @@ -78,6 +78,9 @@ + + + diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs index 50220fa1e1..577e580aee 100644 --- a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs +++ b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs @@ -12,6 +12,8 @@ namespace BizHawk public MOS6502X cpu; public M6532 m6532; public TIA tia; + public byte[] ram = new byte[128]; + public MapperBase mapper; bool resetSignal; @@ -56,7 +58,7 @@ namespace BizHawk // registers // else // ROM - public byte ReadMemory(ushort addr) + public byte BaseReadMemory(ushort addr) { addr = (ushort)(addr & 0x1FFF); if ((addr & 0x1080) == 0) @@ -73,7 +75,7 @@ namespace BizHawk } } - public void WriteMemory(ushort addr, byte value) + public void BaseWriteMemory(ushort addr, byte value) { addr = (ushort)(addr & 0x1FFF); if ((addr & 0x1080) == 0) @@ -90,8 +92,28 @@ namespace BizHawk } } + public byte ReadMemory(ushort addr) + { + return mapper.ReadMemory((ushort)(addr&0x1FFF)); + } + + public void WriteMemory(ushort addr, byte value) + { + mapper.WriteMemory((ushort)(addr & 0x1FFF), value); + } + public void HardReset() { + //regenerate mapper here to make sure its state is entirely clean + switch (game.GetOptionsDict()["m"]) + { + case "4K": mapper = new m4K(); break; + case "2K": mapper = new m2K(); break; + case "F8": mapper = new mF8(); break; + default: throw new InvalidOperationException("mapper not supported: " + game.GetOptionsDict()["m"]); + } + mapper.core = this; + _lagcount = 0; cpu = new MOS6502X(); //cpu.debug = true; @@ -107,8 +129,9 @@ namespace BizHawk //setup the system state here. for instance.. // Read from the reset vector for where to start - cpu.PC = (ushort)(ReadMemory(0xFFFC) + (ReadMemory(0xFFFD) << 8)); //set the initial PC + cpu.PC = (ushort)(ReadMemory(0x1FFC) + (ReadMemory(0x1FFD) << 8)); //set the initial PC //cpu.PC = 0x0000; //set the initial PC + } public void FrameAdvance(bool render) @@ -184,7 +207,13 @@ namespace BizHawk return value; } + } - + public class MapperBase + { + public Atari2600 core; + public virtual byte ReadMemory(ushort addr) { return core.BaseReadMemory(addr); } + public virtual void WriteMemory(ushort addr, byte value) { core.BaseWriteMemory(addr, value); } + public virtual void SyncState(Serializer ser) { } } } \ No newline at end of file diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.cs b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.cs index 60857020a9..67ce3e302c 100644 --- a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.cs +++ b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.cs @@ -7,22 +7,24 @@ namespace BizHawk public partial class Atari2600 : IEmulator, IVideoProvider, ISoundProvider { public string SystemId { get { return "A26"; } } + public GameInfo game; public int[] frameBuffer = new int[320 * 262]; public CoreInputComm CoreInputComm { get; set; } public CoreOutputComm CoreOutputComm { get; private set; } public IVideoProvider VideoProvider { get { return this; } } public ISoundProvider SoundProvider { get { return this; } } - public byte[] ram = new byte[128]; + public Atari2600(GameInfo game, byte[] rom) { var domains = new List(1); - Console.WriteLine("Game uses mapper " + game.GetOptionsDict()["m"]); domains.Add(new MemoryDomain("Main RAM", 128, Endian.Little, addr => ram[addr & 127], (addr, value) => ram[addr & 127] = value)); memoryDomains = domains.AsReadOnly(); CoreOutputComm = new CoreOutputComm(); CoreInputComm = new CoreInputComm(); this.rom = rom; + this.game = game; + Console.WriteLine("Game uses mapper " + game.GetOptionsDict()["m"]); HardReset(); } public void ResetFrameCounter() @@ -47,6 +49,8 @@ namespace BizHawk ser.Sync("ram", ref ram, false); ser.Sync("Lag", ref _lagcount); ser.Sync("Frame", ref _frame); + //TODO - you need to sync your m6532 and tia + mapper.SyncState(ser); } public ControllerDefinition ControllerDefinition { get { return Atari2600ControllerDefinition; } } diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m2K.cs b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m2K.cs new file mode 100644 index 0000000000..1ab0901aff --- /dev/null +++ b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m2K.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.IO; +using BizHawk.Emulation.CPUs.M6502; +using BizHawk.Emulation.Consoles.Atari; + +namespace BizHawk +{ + partial class Atari2600 + { + class m2K : MapperBase + { + public override byte ReadMemory(ushort addr) + { + if (addr < 0x1000) return base.ReadMemory(addr); + return core.rom[addr & 0x7FF]; + } + } + } +} \ No newline at end of file diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m4K.cs b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m4K.cs new file mode 100644 index 0000000000..aa604e90de --- /dev/null +++ b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/m4K.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.IO; +using BizHawk.Emulation.CPUs.M6502; +using BizHawk.Emulation.Consoles.Atari; + +namespace BizHawk +{ + partial class Atari2600 + { + class m4K : MapperBase + { + public override byte ReadMemory(ushort addr) + { + if (addr < 0x1000) return base.ReadMemory(addr); + return core.rom[addr & 0xFFF]; + } + } + } +} \ No newline at end of file diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Mappers/mF8.cs b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/mF8.cs new file mode 100644 index 0000000000..ffa3817978 --- /dev/null +++ b/BizHawk.Emulation/Consoles/Atari/2600/Mappers/mF8.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.IO; +using BizHawk.Emulation.CPUs.M6502; +using BizHawk.Emulation.Consoles.Atari; + +namespace BizHawk +{ + partial class Atari2600 + { + class mF8 : MapperBase + { + int toggle = 0; + + public override byte ReadMemory(ushort addr) + { + Address(addr); + if (addr < 0x1000) return base.ReadMemory(addr); + return core.rom[toggle*4*1024 + (addr&0xFFF)]; + } + public override void WriteMemory(ushort addr, byte value) + { + Address(addr); + if (addr < 0x1000) base.WriteMemory(addr, value); + } + + public override void SyncState(Serializer ser) + { + base.SyncState(ser); + ser.Sync("toggle", ref toggle); + } + + void Address(ushort addr) + { + if (addr == 0x1FF8) toggle = 0; + if (addr == 0x1FF9) toggle = 1; + } + } + } +} \ No newline at end of file