BizHawk/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs

128 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using System.Runtime.InteropServices;
namespace BizHawk.Emulation.Cores.Nintendo.GBA
{
[CoreAttributes("mGBA", "endrift", true, false, "NOT DONE", "NOT DONE", false)]
2015-06-04 22:47:51 +00:00
public class MGBAHawk : IEmulator, IVideoProvider, ISyncSoundProvider
{
IntPtr core;
[CoreConstructor("GBA")]
public MGBAHawk(byte[] file, CoreComm comm)
{
var ser = new BasicServiceProvider(this);
ser.Register<IDisassemblable>(new ArmV4Disassembler());
ServiceProvider = ser;
CoreComm = comm;
2015-06-05 00:12:12 +00:00
byte[] bios = null;
if (true) // TODO: config me
{
bios = comm.CoreFileProvider.GetFirmware("GBA", "Bios", true);
}
if (bios != null && bios.Length != 16384)
{
throw new InvalidOperationException("BIOS must be exactly 16384 bytes!");
}
core = LibmGBA.BizCreate(bios);
if (core == IntPtr.Zero)
2015-06-05 00:12:12 +00:00
{
throw new InvalidOperationException("BizCreate() returned NULL! Bad BIOS?");
}
try
{
if (!LibmGBA.BizLoad(core, file, file.Length))
{
2015-06-05 00:12:12 +00:00
throw new InvalidOperationException("BizLoad() returned FALSE! Bad ROM?");
}
}
catch
{
LibmGBA.BizDestroy(core);
throw;
}
}
public IEmulatorServiceProvider ServiceProvider { get; private set; }
public ControllerDefinition ControllerDefinition { get { return GBA.GBAController; } }
public IController Controller { get; set; }
public void FrameAdvance(bool render, bool rendersound = true)
{
2015-06-04 22:47:51 +00:00
Frame++;
2015-06-04 23:30:24 +00:00
if (Controller["Power"])
LibmGBA.BizReset(core);
LibmGBA.BizAdvance(core, VBANext.GetButtons(Controller), videobuff, ref nsamp, soundbuff);
}
public int Frame { get; private set; }
public string SystemId { get { return "GBA"; } }
public bool DeterministicEmulation { get { return true; } }
public string BoardName { get { return null; } }
public void ResetCounters()
{
2015-06-04 22:47:51 +00:00
Frame = 0;
}
public CoreComm CoreComm { get; private set; }
public void Dispose()
{
if (core != IntPtr.Zero)
{
LibmGBA.BizDestroy(core);
core = IntPtr.Zero;
}
}
2015-06-04 22:47:51 +00:00
#region IVideoProvider
public int VirtualWidth { get { return 240; } }
public int VirtualHeight { get { return 160; } }
public int BufferWidth { get { return 240; } }
public int BufferHeight { get { return 160; } }
public int BackgroundColor
{
get { return unchecked((int)0xff000000); }
}
public int[] GetVideoBuffer()
{
return videobuff;
}
2015-06-04 22:47:51 +00:00
private readonly int[] videobuff = new int[240 * 160];
#endregion
2015-06-04 22:47:51 +00:00
#region ISoundProvider
private readonly short[] soundbuff = new short[2048];
private int nsamp;
public void GetSamples(out short[] samples, out int nsamp)
{
nsamp = this.nsamp;
samples = soundbuff;
2015-06-04 23:30:24 +00:00
Console.WriteLine(nsamp);
2015-06-04 22:47:51 +00:00
DiscardSamples();
}
public void DiscardSamples()
{
nsamp = 0;
}
public ISoundProvider SoundProvider { get { throw new InvalidOperationException(); } }
public ISyncSoundProvider SyncSoundProvider { get { return this; } }
public bool StartAsyncSound() { return false; }
public void EndAsyncSound() { }
#endregion
}
}