misc code cleanups Lynx

This commit is contained in:
adelikat 2017-04-24 11:51:59 -05:00
parent c628557912
commit 4c71a34dfa
10 changed files with 77 additions and 96 deletions

View File

@ -59,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
if (ser.IsReader) if (ser.IsReader)
{ {
theMachine = MachineBase.Deserialize(new BinaryReader(new MemoryStream(core, false))); theMachine = MachineBase.Deserialize(new BinaryReader(new MemoryStream(core, false)));
avProvider.ConnectToMachine(theMachine, GameInfo); _avProvider.ConnectToMachine(theMachine, GameInfo);
} }
} }
} }

View File

@ -45,8 +45,8 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
public Atari7800(CoreComm comm, GameInfo game, byte[] rom, string GameDBfn) public Atari7800(CoreComm comm, GameInfo game, byte[] rom, string GameDBfn)
{ {
ServiceProvider = new BasicServiceProvider(this); ServiceProvider = new BasicServiceProvider(this);
(ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(avProvider); (ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(_avProvider);
(ServiceProvider as BasicServiceProvider).Register<ISoundProvider>(avProvider); (ServiceProvider as BasicServiceProvider).Register<ISoundProvider>(_avProvider);
CoreComm = comm; CoreComm = comm;
byte[] highscoreBIOS = comm.CoreFileProvider.GetFirmware("A78", "Bios_HSC", false, "Some functions may not work without the high score BIOS."); byte[] highscoreBIOS = comm.CoreFileProvider.GetFirmware("A78", "Bios_HSC", false, "Some functions may not work without the high score BIOS.");
@ -113,7 +113,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
} }
ControlAdapter.Convert(Controller, theMachine.InputState); ControlAdapter.Convert(Controller, theMachine.InputState);
theMachine.ComputeNextFrame(avProvider.framebuffer); theMachine.ComputeNextFrame(_avProvider.Framebuffer);
_islag = theMachine.InputState.Lagged; _islag = theMachine.InputState.Lagged;
@ -122,7 +122,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
_lagcount++; _lagcount++;
} }
avProvider.FillFrameBuffer(); _avProvider.FillFrameBuffer();
} }
public CoreComm CoreComm { get; } public CoreComm CoreComm { get; }
@ -134,10 +134,10 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
public void Dispose() public void Dispose()
{ {
if (avProvider != null) if (_avProvider != null)
{ {
avProvider.Dispose(); _avProvider.Dispose();
avProvider = null; _avProvider = null;
} }
} }
@ -148,7 +148,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
_islag = false; _islag = false;
} }
public Atari7800Control ControlAdapter; public Atari7800Control ControlAdapter { get; private set; }
public ControllerDefinition ControllerDefinition { get; private set; } public ControllerDefinition ControllerDefinition { get; private set; }
public IController Controller { get; set; } public IController Controller { get; set; }
@ -177,7 +177,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
} }
} }
private bool _pal; private readonly bool _pal;
public DisplayType Region => _pal ? DisplayType.PAL : DisplayType.NTSC; public DisplayType Region => _pal ? DisplayType.PAL : DisplayType.NTSC;
private void HardReset() private void HardReset()
@ -207,7 +207,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
ControlAdapter = new Atari7800Control(theMachine); ControlAdapter = new Atari7800Control(theMachine);
ControllerDefinition = ControlAdapter.ControlType; ControllerDefinition = ControlAdapter.ControlType;
avProvider.ConnectToMachine(theMachine, GameInfo); _avProvider.ConnectToMachine(theMachine, GameInfo);
// to sync exactly with audio as this emulator creates and times it, the frame rate should be exactly 60:1 or 50:1 // to sync exactly with audio as this emulator creates and times it, the frame rate should be exactly 60:1 or 50:1
CoreComm.VsyncNum = theMachine.FrameHZ; CoreComm.VsyncNum = theMachine.FrameHZ;
@ -218,16 +218,16 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
#region audio\video #region audio\video
private MyAVProvider avProvider = new MyAVProvider(); private MyAVProvider _avProvider = new MyAVProvider();
private class MyAVProvider : IVideoProvider, ISoundProvider, IDisposable private class MyAVProvider : IVideoProvider, ISoundProvider, IDisposable
{ {
public FrameBuffer framebuffer { get; private set; } public FrameBuffer Framebuffer { get; private set; }
public void ConnectToMachine(MachineBase m, EMU7800.Win.GameProgram g) public void ConnectToMachine(MachineBase m, EMU7800.Win.GameProgram g)
{ {
framebuffer = m.CreateFrameBuffer(); Framebuffer = m.CreateFrameBuffer();
BufferWidth = framebuffer.VisiblePitch; BufferWidth = Framebuffer.VisiblePitch;
BufferHeight = framebuffer.Scanlines; BufferHeight = Framebuffer.Scanlines;
vidbuffer = new int[BufferWidth * BufferHeight]; vidbuffer = new int[BufferWidth * BufferHeight];
uint newsamplerate = (uint)m.SoundSampleFrequency; uint newsamplerate = (uint)m.SoundSampleFrequency;
@ -261,7 +261,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
{ {
unsafe unsafe
{ {
fixed (byte* src_ = framebuffer.VideoBuffer) fixed (byte* src_ = Framebuffer.VideoBuffer)
fixed (int* dst_ = vidbuffer) fixed (int* dst_ = vidbuffer)
fixed (int* pal = palette) fixed (int* pal = palette)
{ {
@ -292,10 +292,10 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
public void GetSamplesSync(out short[] samples, out int nsamp) public void GetSamplesSync(out short[] samples, out int nsamp)
{ {
int nsampin = framebuffer.SoundBufferByteLength; int nsampin = Framebuffer.SoundBufferByteLength;
unsafe unsafe
{ {
fixed (byte* src = framebuffer.SoundBuffer) fixed (byte* src = Framebuffer.SoundBuffer)
{ {
for (int i = 0; i < nsampin; i++) for (int i = 0; i < nsampin; i++)
{ {

View File

@ -1,17 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using System.Runtime.InteropServices;
namespace BizHawk.Emulation.Cores.Atari.Lynx namespace BizHawk.Emulation.Cores.Atari.Lynx
{ {
public static class LibLynx public static class LibLynx
{ {
const string dllname = "bizlynx.dll"; private const string dllname = "bizlynx.dll";
const CallingConvention cc = CallingConvention.Cdecl; private const CallingConvention cc = CallingConvention.Cdecl;
[DllImport(dllname, CallingConvention = cc)] [DllImport(dllname, CallingConvention = cc)]
public static extern IntPtr Create(byte[] game, int gamesize, byte[] bios, int biossize, int pagesize0, int pagesize1, bool lowpass); public static extern IntPtr Create(byte[] game, int gamesize, byte[] bios, int biossize, int pagesize0, int pagesize1, bool lowpass);

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;

View File

@ -12,7 +12,10 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
int size; int size;
IntPtr data; IntPtr data;
if (!LibLynx.GetSaveRamPtr(Core, out size, out data)) if (!LibLynx.GetSaveRamPtr(Core, out size, out data))
{
return null; return null;
}
byte[] ret = new byte[size]; byte[] ret = new byte[size];
Marshal.Copy(data, ret, 0, size); Marshal.Copy(data, ret, 0, size);
return ret; return ret;
@ -23,9 +26,15 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
int size; int size;
IntPtr data; IntPtr data;
if (!LibLynx.GetSaveRamPtr(Core, out size, out data)) if (!LibLynx.GetSaveRamPtr(Core, out size, out data))
{
throw new InvalidOperationException(); throw new InvalidOperationException();
}
if (size != srcdata.Length) if (size != srcdata.Length)
{
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
}
Marshal.Copy(srcdata, 0, data, size); Marshal.Copy(srcdata, 0, data, size);
} }

View File

@ -5,18 +5,15 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
{ {
public partial class Lynx : ISoundProvider public partial class Lynx : ISoundProvider
{ {
private short[] soundbuff = new short[2048]; private readonly short[] _soundbuff = new short[2048];
private int numsamp; private int _numsamp;
public bool CanProvideAsync public bool CanProvideAsync => false;
{
get { return false; }
}
public void GetSamplesSync(out short[] samples, out int nsamp) public void GetSamplesSync(out short[] samples, out int nsamp)
{ {
samples = soundbuff; samples = _soundbuff;
nsamp = numsamp; nsamp = _numsamp;
} }
public void DiscardSamples() public void DiscardSamples()
@ -32,10 +29,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
} }
} }
public SyncSoundMode SyncMode public SyncSoundMode SyncMode => SyncSoundMode.Sync;
{
get { return SyncSoundMode.Sync; }
}
public void GetSamplesAsync(short[] samples) public void GetSamplesAsync(short[] samples)
{ {

View File

@ -1,6 +1,5 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
@ -9,10 +8,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
{ {
public partial class Lynx : IStatable public partial class Lynx : IStatable
{ {
public bool BinarySaveStatesPreferred public bool BinarySaveStatesPreferred => true;
{
get { return true; }
}
public void SaveStateText(TextWriter writer) public void SaveStateText(TextWriter writer)
{ {
@ -24,17 +20,18 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
s.ExtraData.LagCount = LagCount; s.ExtraData.LagCount = LagCount;
s.ExtraData.Frame = Frame; s.ExtraData.Frame = Frame;
ser.Serialize(writer, s); _ser.Serialize(writer, s);
// write extra copy of stuff we don't use // write extra copy of stuff we don't use
writer.WriteLine(); writer.WriteLine();
writer.WriteLine("Frame {0}", Frame); writer.WriteLine("Frame {0}", Frame);
//Console.WriteLine(BizHawk.Common.BufferExtensions.BufferExtensions.HashSHA1(SaveStateBinary())); ////Console.WriteLine(BizHawk.Common.BufferExtensions.BufferExtensions.HashSHA1(SaveStateBinary()));
} }
public void LoadStateText(TextReader reader) public void LoadStateText(TextReader reader)
{ {
var s = (TextState<TextStateData>)ser.Deserialize(reader, typeof(TextState<TextStateData>)); var s = (TextState<TextStateData>)_ser.Deserialize(reader, typeof(TextState<TextStateData>));
s.Prepare(); s.Prepare();
var ff = s.GetFunctionPointersLoad(); var ff = s.GetFunctionPointersLoad();
LibLynx.TxtStateLoad(Core, ref ff); LibLynx.TxtStateLoad(Core, ref ff);
@ -45,13 +42,13 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
public void SaveStateBinary(BinaryWriter writer) public void SaveStateBinary(BinaryWriter writer)
{ {
if (!LibLynx.BinStateSave(Core, savebuff, savebuff.Length)) if (!LibLynx.BinStateSave(Core, _savebuff, _savebuff.Length))
{ {
throw new InvalidOperationException("Core's BinStateSave() returned false!"); throw new InvalidOperationException("Core's BinStateSave() returned false!");
} }
writer.Write(savebuff.Length); writer.Write(_savebuff.Length);
writer.Write(savebuff); writer.Write(_savebuff);
// other variables // other variables
writer.Write(IsLagFrame); writer.Write(IsLagFrame);
@ -62,13 +59,13 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
public void LoadStateBinary(BinaryReader reader) public void LoadStateBinary(BinaryReader reader)
{ {
int length = reader.ReadInt32(); int length = reader.ReadInt32();
if (length != savebuff.Length) if (length != _savebuff.Length)
{ {
throw new InvalidOperationException("Save buffer size mismatch!"); throw new InvalidOperationException("Save buffer size mismatch!");
} }
reader.Read(savebuff, 0, length); reader.Read(_savebuff, 0, length);
if (!LibLynx.BinStateLoad(Core, savebuff, savebuff.Length)) if (!LibLynx.BinStateLoad(Core, _savebuff, _savebuff.Length))
{ {
throw new InvalidOperationException("Core's BinStateLoad() returned false!"); throw new InvalidOperationException("Core's BinStateLoad() returned false!");
} }
@ -81,22 +78,22 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
public byte[] SaveStateBinary() public byte[] SaveStateBinary()
{ {
var ms = new MemoryStream(savebuff2, true); var ms = new MemoryStream(_savebuff2, true);
var bw = new BinaryWriter(ms); var bw = new BinaryWriter(ms);
SaveStateBinary(bw); SaveStateBinary(bw);
bw.Flush(); bw.Flush();
if (ms.Position != savebuff2.Length) if (ms.Position != _savebuff2.Length)
{ {
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
ms.Close(); ms.Close();
return savebuff2; return _savebuff2;
} }
private JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; private readonly JsonSerializer _ser = new JsonSerializer { Formatting = Formatting.Indented };
private byte[] savebuff; private readonly byte[] _savebuff;
private byte[] savebuff2; private readonly byte[] _savebuff2;
private class TextStateData private class TextStateData
{ {

View File

@ -1,40 +1,27 @@
using System; using BizHawk.Emulation.Common;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Atari.Lynx namespace BizHawk.Emulation.Cores.Atari.Lynx
{ {
public partial class Lynx : IVideoProvider public partial class Lynx : IVideoProvider
{ {
private const int WIDTH = 160; private const int Width = 160;
private const int HEIGHT = 102; private const int Height = 102;
private int[] videobuff = new int[WIDTH * HEIGHT]; private readonly int[] _videobuff = new int[Width * Height];
public int[] GetVideoBuffer() public int[] GetVideoBuffer()
{ {
return videobuff; return _videobuff;
} }
public int VirtualWidth public int VirtualWidth => BufferWidth;
{
get { return BufferWidth; }
}
public int VirtualHeight public int VirtualHeight => BufferHeight;
{
get { return BufferHeight; }
}
public int BufferWidth { get; private set; } public int BufferWidth { get; }
public int BufferHeight { get; private set; } public int BufferHeight { get; }
public int BackgroundColor public int BackgroundColor => unchecked((int)0xff000000);
{
get { return unchecked((int)0xff000000); }
}
} }
} }

View File

@ -1,13 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using BizHawk.Common;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using Newtonsoft.Json;
namespace BizHawk.Emulation.Cores.Atari.Lynx namespace BizHawk.Emulation.Cores.Atari.Lynx
{ {
@ -15,7 +10,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
[ServiceNotApplicable(typeof(ISettable<,>), typeof(IDriveLight), typeof(IRegionable))] [ServiceNotApplicable(typeof(ISettable<,>), typeof(IDriveLight), typeof(IRegionable))]
public partial class Lynx : IEmulator, IVideoProvider, ISoundProvider, ISaveRam, IStatable, IInputPollable public partial class Lynx : IEmulator, IVideoProvider, ISoundProvider, ISaveRam, IStatable, IInputPollable
{ {
IntPtr Core; private IntPtr Core;
[CoreConstructor("Lynx")] [CoreConstructor("Lynx")]
public Lynx(byte[] file, GameInfo game, CoreComm comm) public Lynx(byte[] file, GameInfo game, CoreComm comm)
@ -25,7 +20,9 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
byte[] bios = CoreComm.CoreFileProvider.GetFirmware("Lynx", "Boot", true, "Boot rom is required"); byte[] bios = CoreComm.CoreFileProvider.GetFirmware("Lynx", "Boot", true, "Boot rom is required");
if (bios.Length != 512) if (bios.Length != 512)
{
throw new MissingFirmwareException("Lynx Bootrom must be 512 bytes!"); throw new MissingFirmwareException("Lynx Bootrom must be 512 bytes!");
}
int pagesize0 = 0; int pagesize0 = 0;
int pagesize1 = 0; int pagesize1 = 0;
@ -45,7 +42,9 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
ms.Position = 6; ms.Position = 6;
string bs93 = Encoding.ASCII.GetString(br.ReadBytes(6)); string bs93 = Encoding.ASCII.GetString(br.ReadBytes(6));
if (bs93 == "BS93") if (bs93 == "BS93")
{
throw new InvalidOperationException("Unsupported BS93 Lynx ram image"); throw new InvalidOperationException("Unsupported BS93 Lynx ram image");
}
if (header == "LYNX" && (ver & 255) == 1) if (header == "LYNX" && (ver & 255) == 1)
{ {
@ -63,7 +62,6 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
Console.WriteLine("No Handy-Lynx header found! Assuming raw rom image."); Console.WriteLine("No Handy-Lynx header found! Assuming raw rom image.");
realfile = file; realfile = file;
} }
} }
if (game.OptionPresent("pagesize0")) if (game.OptionPresent("pagesize0"))
@ -123,7 +121,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
} }
} }
public IEmulatorServiceProvider ServiceProvider { get; private set; } public IEmulatorServiceProvider ServiceProvider { get; }
public void FrameAdvance(bool render, bool rendersound = true) public void FrameAdvance(bool render, bool rendersound = true)
{ {
@ -137,14 +135,16 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
IsLagFrame = LibLynx.Advance(Core, GetButtons(), videobuff, soundbuff, ref samples); IsLagFrame = LibLynx.Advance(Core, GetButtons(), videobuff, soundbuff, ref samples);
numsamp = samples / 2; // sound provider wants number of sample pairs numsamp = samples / 2; // sound provider wants number of sample pairs
if (IsLagFrame) if (IsLagFrame)
{
LagCount++; LagCount++;
}
} }
public int Frame { get; private set; } public int Frame { get; private set; }
public string SystemId { get { return "Lynx"; } } public string SystemId => "Lynx";
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation => true;
public void ResetCounters() public void ResetCounters()
{ {
@ -153,9 +153,9 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
IsLagFrame = false; IsLagFrame = false;
} }
public string BoardName { get { return null; } } public string BoardName => null;
public CoreComm CoreComm { get; private set; } public CoreComm CoreComm { get; }
public void Dispose() public void Dispose()
{ {
@ -177,7 +177,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
public ControllerDefinition ControllerDefinition { get { return LynxTroller; } } public ControllerDefinition ControllerDefinition { get { return LynxTroller; } }
public IController Controller { get; set; } public IController Controller { get; set; }
LibLynx.Buttons GetButtons() private LibLynx.Buttons GetButtons()
{ {
LibLynx.Buttons ret = 0; LibLynx.Buttons ret = 0;
if (Controller.IsPressed("A")) ret |= LibLynx.Buttons.A; if (Controller.IsPressed("A")) ret |= LibLynx.Buttons.A;