2014-09-15 20:12:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
using BizHawk.Emulation.Common;
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.Emulation.Cores.Atari.Lynx
|
|
|
|
|
{
|
2014-11-17 21:58:13 +00:00
|
|
|
|
[CoreAttributes("Handy", "K. Wilkins", true, true, "mednafen 0-9-34-1", "http://mednafen.sourceforge.net/")]
|
2015-08-06 00:12:09 +00:00
|
|
|
|
[ServiceNotApplicable(typeof(ISettable<,>), typeof(IDriveLight), typeof(IRegionable))]
|
2016-12-11 17:14:42 +00:00
|
|
|
|
public partial class Lynx : IEmulator, IVideoProvider, ISoundProvider, ISaveRam, IStatable, IInputPollable
|
2014-09-15 20:12:11 +00:00
|
|
|
|
{
|
2017-04-24 16:51:59 +00:00
|
|
|
|
private IntPtr Core;
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
2014-11-21 01:32:15 +00:00
|
|
|
|
[CoreConstructor("Lynx")]
|
2014-09-15 20:12:11 +00:00
|
|
|
|
public Lynx(byte[] file, GameInfo game, CoreComm comm)
|
|
|
|
|
{
|
2014-12-04 03:38:30 +00:00
|
|
|
|
ServiceProvider = new BasicServiceProvider(this);
|
2014-09-15 20:12:11 +00:00
|
|
|
|
CoreComm = comm;
|
|
|
|
|
|
2014-11-21 01:32:15 +00:00
|
|
|
|
byte[] bios = CoreComm.CoreFileProvider.GetFirmware("Lynx", "Boot", true, "Boot rom is required");
|
2014-09-15 20:12:11 +00:00
|
|
|
|
if (bios.Length != 512)
|
2017-04-24 16:51:59 +00:00
|
|
|
|
{
|
2014-09-15 20:12:11 +00:00
|
|
|
|
throw new MissingFirmwareException("Lynx Bootrom must be 512 bytes!");
|
2017-04-24 16:51:59 +00:00
|
|
|
|
}
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
|
|
|
|
int pagesize0 = 0;
|
|
|
|
|
int pagesize1 = 0;
|
|
|
|
|
byte[] realfile = null;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var ms = new MemoryStream(file, false);
|
|
|
|
|
var br = new BinaryReader(ms);
|
|
|
|
|
string header = Encoding.ASCII.GetString(br.ReadBytes(4));
|
|
|
|
|
int p0 = br.ReadUInt16();
|
|
|
|
|
int p1 = br.ReadUInt16();
|
|
|
|
|
int ver = br.ReadUInt16();
|
|
|
|
|
string cname = Encoding.ASCII.GetString(br.ReadBytes(32)).Trim();
|
|
|
|
|
string mname = Encoding.ASCII.GetString(br.ReadBytes(16)).Trim();
|
|
|
|
|
int rot = br.ReadByte();
|
|
|
|
|
|
|
|
|
|
ms.Position = 6;
|
|
|
|
|
string bs93 = Encoding.ASCII.GetString(br.ReadBytes(6));
|
|
|
|
|
if (bs93 == "BS93")
|
2017-04-24 16:51:59 +00:00
|
|
|
|
{
|
2014-09-15 20:12:11 +00:00
|
|
|
|
throw new InvalidOperationException("Unsupported BS93 Lynx ram image");
|
2017-04-24 16:51:59 +00:00
|
|
|
|
}
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
2014-11-21 01:35:54 +00:00
|
|
|
|
if (header == "LYNX" && (ver & 255) == 1)
|
2014-09-15 20:12:11 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Processing Handy-Lynx header");
|
|
|
|
|
pagesize0 = p0;
|
|
|
|
|
pagesize1 = p1;
|
|
|
|
|
Console.WriteLine("TODO: Rotate {0}", rot);
|
|
|
|
|
Console.WriteLine("Cart: {0} Manufacturer: {1}", cname, mname);
|
|
|
|
|
realfile = new byte[file.Length - 64];
|
|
|
|
|
Buffer.BlockCopy(file, 64, realfile, 0, realfile.Length);
|
|
|
|
|
Console.WriteLine("Header Listed banking: {0} {1}", p0, p1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("No Handy-Lynx header found! Assuming raw rom image.");
|
|
|
|
|
realfile = file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (game.OptionPresent("pagesize0"))
|
|
|
|
|
{
|
|
|
|
|
pagesize0 = int.Parse(game.OptionValue("pagesize0"));
|
|
|
|
|
pagesize1 = int.Parse(game.OptionValue("pagesize1"));
|
|
|
|
|
Console.WriteLine("Loading banking options {0} {1} from gamedb", pagesize0, pagesize1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pagesize0 == 0 && pagesize1 == 0)
|
|
|
|
|
{
|
|
|
|
|
switch (realfile.Length)
|
|
|
|
|
{
|
|
|
|
|
case 0x10000: pagesize0 = 0x100; break;
|
2014-10-14 00:53:13 +00:00
|
|
|
|
case 0x20000: pagesize0 = 0x200; break; //
|
|
|
|
|
case 0x40000: pagesize0 = 0x400; break; // all known good dumps fall in one of these three categories
|
|
|
|
|
case 0x80000: pagesize0 = 0x800; break; //
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
|
|
|
|
case 0x30000: pagesize0 = 0x200; pagesize1 = 0x100; break;
|
2014-09-16 00:40:15 +00:00
|
|
|
|
case 0x50000: pagesize0 = 0x400; pagesize1 = 0x100; break;
|
2014-09-15 20:12:11 +00:00
|
|
|
|
case 0x60000: pagesize0 = 0x400; pagesize1 = 0x200; break;
|
2014-09-16 00:40:15 +00:00
|
|
|
|
case 0x90000: pagesize0 = 0x800; pagesize1 = 0x100; break;
|
|
|
|
|
case 0xa0000: pagesize0 = 0x800; pagesize1 = 0x200; break;
|
2014-09-15 20:12:11 +00:00
|
|
|
|
case 0xc0000: pagesize0 = 0x800; pagesize1 = 0x400; break;
|
|
|
|
|
case 0x100000: pagesize0 = 0x800; pagesize1 = 0x800; break;
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Auto-guessed banking options {0} {1}", pagesize0, pagesize1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core = LibLynx.Create(realfile, realfile.Length, bios, bios.Length, pagesize0, pagesize1, false);
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-09-16 00:40:15 +00:00
|
|
|
|
CoreComm.VsyncNum = 16000000; // 16.00 mhz refclock
|
|
|
|
|
CoreComm.VsyncDen = 16 * 105 * 159;
|
2014-09-29 20:13:26 +00:00
|
|
|
|
|
|
|
|
|
savebuff = new byte[LibLynx.BinStateSize(Core)];
|
|
|
|
|
savebuff2 = new byte[savebuff.Length + 13];
|
2014-10-15 04:58:27 +00:00
|
|
|
|
|
|
|
|
|
int rot = game.OptionPresent("rotate") ? int.Parse(game.OptionValue("rotate")) : 0;
|
|
|
|
|
LibLynx.SetRotation(Core, rot);
|
|
|
|
|
if ((rot & 1) != 0)
|
|
|
|
|
{
|
|
|
|
|
BufferWidth = HEIGHT;
|
|
|
|
|
BufferHeight = WIDTH;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BufferWidth = WIDTH;
|
|
|
|
|
BufferHeight = HEIGHT;
|
|
|
|
|
}
|
2014-11-02 19:11:19 +00:00
|
|
|
|
SetupMemoryDomains();
|
2014-09-15 20:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
public IEmulatorServiceProvider ServiceProvider { get; }
|
2014-12-04 03:38:30 +00:00
|
|
|
|
|
2014-09-15 20:12:11 +00:00
|
|
|
|
public void FrameAdvance(bool render, bool rendersound = true)
|
|
|
|
|
{
|
|
|
|
|
Frame++;
|
2016-12-14 18:42:15 +00:00
|
|
|
|
if (Controller.IsPressed("Power"))
|
|
|
|
|
{
|
2014-09-15 20:12:11 +00:00
|
|
|
|
LibLynx.Reset(Core);
|
2016-12-14 18:42:15 +00:00
|
|
|
|
}
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
|
|
|
|
int samples = soundbuff.Length;
|
2014-09-29 18:53:53 +00:00
|
|
|
|
IsLagFrame = LibLynx.Advance(Core, GetButtons(), videobuff, soundbuff, ref samples);
|
2014-09-16 00:40:15 +00:00
|
|
|
|
numsamp = samples / 2; // sound provider wants number of sample pairs
|
2014-09-29 18:53:53 +00:00
|
|
|
|
if (IsLagFrame)
|
2017-04-24 16:51:59 +00:00
|
|
|
|
{
|
2014-09-29 18:53:53 +00:00
|
|
|
|
LagCount++;
|
2017-04-24 16:51:59 +00:00
|
|
|
|
}
|
2014-09-15 20:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Frame { get; private set; }
|
2014-12-04 00:43:12 +00:00
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
public string SystemId => "Lynx";
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
public bool DeterministicEmulation => true;
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
|
|
|
|
public void ResetCounters()
|
|
|
|
|
{
|
|
|
|
|
Frame = 0;
|
|
|
|
|
LagCount = 0;
|
|
|
|
|
IsLagFrame = false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
public string BoardName => null;
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
public CoreComm CoreComm { get; }
|
2014-09-15 20:12:11 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (Core != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
LibLynx.Destroy(Core);
|
|
|
|
|
Core = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Controller
|
|
|
|
|
|
2014-09-16 00:40:15 +00:00
|
|
|
|
private static readonly ControllerDefinition LynxTroller = new ControllerDefinition
|
|
|
|
|
{
|
|
|
|
|
Name = "Lynx Controller",
|
|
|
|
|
BoolButtons = { "Up", "Down", "Left", "Right", "A", "B", "Option 1", "Option 2", "Pause", "Power" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public ControllerDefinition ControllerDefinition { get { return LynxTroller; } }
|
2014-09-15 20:12:11 +00:00
|
|
|
|
public IController Controller { get; set; }
|
|
|
|
|
|
2017-04-24 16:51:59 +00:00
|
|
|
|
private LibLynx.Buttons GetButtons()
|
2014-09-16 00:40:15 +00:00
|
|
|
|
{
|
|
|
|
|
LibLynx.Buttons ret = 0;
|
2016-12-14 18:42:15 +00:00
|
|
|
|
if (Controller.IsPressed("A")) ret |= LibLynx.Buttons.A;
|
|
|
|
|
if (Controller.IsPressed("B")) ret |= LibLynx.Buttons.B;
|
|
|
|
|
if (Controller.IsPressed("Up")) ret |= LibLynx.Buttons.Up;
|
|
|
|
|
if (Controller.IsPressed("Down")) ret |= LibLynx.Buttons.Down;
|
|
|
|
|
if (Controller.IsPressed("Left")) ret |= LibLynx.Buttons.Left;
|
|
|
|
|
if (Controller.IsPressed("Right")) ret |= LibLynx.Buttons.Right;
|
|
|
|
|
if (Controller.IsPressed("Pause")) ret |= LibLynx.Buttons.Pause;
|
|
|
|
|
if (Controller.IsPressed("Option 1")) ret |= LibLynx.Buttons.Option_1;
|
|
|
|
|
if (Controller.IsPressed("Option 2")) ret |= LibLynx.Buttons.Option_2;
|
2014-09-16 00:40:15 +00:00
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-15 20:12:11 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|