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)
{
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)
{
ServiceProvider = new BasicServiceProvider(this);
(ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(avProvider);
(ServiceProvider as BasicServiceProvider).Register<ISoundProvider>(avProvider);
(ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(_avProvider);
(ServiceProvider as BasicServiceProvider).Register<ISoundProvider>(_avProvider);
CoreComm = comm;
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);
theMachine.ComputeNextFrame(avProvider.framebuffer);
theMachine.ComputeNextFrame(_avProvider.Framebuffer);
_islag = theMachine.InputState.Lagged;
@ -122,7 +122,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
_lagcount++;
}
avProvider.FillFrameBuffer();
_avProvider.FillFrameBuffer();
}
public CoreComm CoreComm { get; }
@ -134,10 +134,10 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
public void Dispose()
{
if (avProvider != null)
if (_avProvider != null)
{
avProvider.Dispose();
avProvider = null;
_avProvider.Dispose();
_avProvider = null;
}
}
@ -148,7 +148,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
_islag = false;
}
public Atari7800Control ControlAdapter;
public Atari7800Control ControlAdapter { get; private set; }
public ControllerDefinition ControllerDefinition { get; private 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;
private void HardReset()
@ -207,7 +207,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
ControlAdapter = new Atari7800Control(theMachine);
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
CoreComm.VsyncNum = theMachine.FrameHZ;
@ -218,16 +218,16 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
#region audio\video
private MyAVProvider avProvider = new MyAVProvider();
private MyAVProvider _avProvider = new MyAVProvider();
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)
{
framebuffer = m.CreateFrameBuffer();
BufferWidth = framebuffer.VisiblePitch;
BufferHeight = framebuffer.Scanlines;
Framebuffer = m.CreateFrameBuffer();
BufferWidth = Framebuffer.VisiblePitch;
BufferHeight = Framebuffer.Scanlines;
vidbuffer = new int[BufferWidth * BufferHeight];
uint newsamplerate = (uint)m.SoundSampleFrequency;
@ -261,7 +261,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
{
unsafe
{
fixed (byte* src_ = framebuffer.VideoBuffer)
fixed (byte* src_ = Framebuffer.VideoBuffer)
fixed (int* dst_ = vidbuffer)
fixed (int* pal = palette)
{
@ -292,10 +292,10 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
public void GetSamplesSync(out short[] samples, out int nsamp)
{
int nsampin = framebuffer.SoundBufferByteLength;
int nsampin = Framebuffer.SoundBufferByteLength;
unsafe
{
fixed (byte* src = framebuffer.SoundBuffer)
fixed (byte* src = Framebuffer.SoundBuffer)
{
for (int i = 0; i < nsampin; i++)
{

View File

@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using BizHawk.Emulation.Common;
using System.Runtime.InteropServices;
namespace BizHawk.Emulation.Cores.Atari.Lynx
{
public static class LibLynx
{
const string dllname = "bizlynx.dll";
const CallingConvention cc = CallingConvention.Cdecl;
private const string dllname = "bizlynx.dll";
private const CallingConvention cc = CallingConvention.Cdecl;
[DllImport(dllname, CallingConvention = cc)]
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.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using Newtonsoft.Json;
namespace BizHawk.Emulation.Cores.Atari.Lynx
{
@ -15,7 +10,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
[ServiceNotApplicable(typeof(ISettable<,>), typeof(IDriveLight), typeof(IRegionable))]
public partial class Lynx : IEmulator, IVideoProvider, ISoundProvider, ISaveRam, IStatable, IInputPollable
{
IntPtr Core;
private IntPtr Core;
[CoreConstructor("Lynx")]
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");
if (bios.Length != 512)
{
throw new MissingFirmwareException("Lynx Bootrom must be 512 bytes!");
}
int pagesize0 = 0;
int pagesize1 = 0;
@ -45,7 +42,9 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
ms.Position = 6;
string bs93 = Encoding.ASCII.GetString(br.ReadBytes(6));
if (bs93 == "BS93")
{
throw new InvalidOperationException("Unsupported BS93 Lynx ram image");
}
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.");
realfile = file;
}
}
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)
{
@ -137,14 +135,16 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
IsLagFrame = LibLynx.Advance(Core, GetButtons(), videobuff, soundbuff, ref samples);
numsamp = samples / 2; // sound provider wants number of sample pairs
if (IsLagFrame)
{
LagCount++;
}
}
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()
{
@ -153,9 +153,9 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
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()
{
@ -177,7 +177,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
public ControllerDefinition ControllerDefinition { get { return LynxTroller; } }
public IController Controller { get; set; }
LibLynx.Buttons GetButtons()
private LibLynx.Buttons GetButtons()
{
LibLynx.Buttons ret = 0;
if (Controller.IsPressed("A")) ret |= LibLynx.Buttons.A;