BizHawk/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs

867 lines
24 KiB
C#
Raw Normal View History

2012-09-08 21:36:04 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using BizHawk.Common;
using BizHawk.Emulation.Common;
2012-09-09 02:06:07 +00:00
namespace BizHawk.Emulation.Consoles.GB
2012-09-08 21:36:04 +00:00
{
/// <summary>
/// a gameboy/gameboy color emulator wrapped around native C++ libgambatte
/// </summary>
public class Gameboy : IEmulator, IVideoProvider, ISyncSoundProvider
2012-09-08 21:36:04 +00:00
{
/// <summary>
/// internal gambatte state
/// </summary>
internal IntPtr GambatteState = IntPtr.Zero;
2012-09-08 21:36:04 +00:00
2012-09-09 14:17:57 +00:00
/// <summary>
/// keep a copy of the input callback delegate so it doesn't get GCed
/// </summary>
LibGambatte.InputGetter InputCallback;
/// <summary>
/// whatever keys are currently depressed
/// </summary>
LibGambatte.Buttons CurrentButtons = 0;
2012-09-08 21:36:04 +00:00
/// <summary>
/// RTC time when emulation begins.
/// </summary>
long zerotime = 0;
LibGambatte.RTCCallback TimeCallback;
long GetCurrentTime()
{
long fn = Frame;
fn /= 60; // exactly 60 fps. in case you feel bad about it, remember that we're not exactly tracking cpu cycles either.
fn += zerotime;
return fn;
}
public Gameboy(CoreComm comm, GameInfo game, byte[] romdata)
2012-09-08 21:36:04 +00:00
{
CoreComm = comm;
comm.VsyncNum = 262144;
comm.VsyncDen = 4389;
comm.RomStatusAnnotation = null;
comm.RomStatusDetails = null;
comm.CpuTraceAvailable = true;
comm.NominalWidth = 160;
comm.NominalHeight = 144;
ThrowExceptionForBadRom(romdata);
BoardName = MapperName(romdata);
GambatteState = LibGambatte.gambatte_create();
if (GambatteState == IntPtr.Zero)
throw new Exception("gambatte_create() returned null???");
LibGambatte.LoadFlags flags = 0;
if (game["ForceDMG"])
flags |= LibGambatte.LoadFlags.FORCE_DMG;
if (game["GBACGB"])
flags |= LibGambatte.LoadFlags.GBA_CGB;
if (game["MulitcartCompat"])
flags |= LibGambatte.LoadFlags.MULTICART_COMPAT;
if (LibGambatte.gambatte_load(GambatteState, romdata, (uint)romdata.Length, GetCurrentTime(), flags) != 0)
throw new Exception("gambatte_load() returned non-zero (is this not a gb or gbc rom?)");
2012-09-08 21:36:04 +00:00
// set real default colors (before anyone mucks with them at all)
ChangeDMGColors(new int[] { 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157 });
SetCGBColors(GBColors.ColorType.gambatte);
InitSound();
2012-09-09 14:17:57 +00:00
Frame = 0;
LagCount = 0;
IsLagFrame = false;
2012-09-09 14:17:57 +00:00
InputCallback = new LibGambatte.InputGetter(ControllerCallback);
LibGambatte.gambatte_setinputgetter(GambatteState, InputCallback);
InitMemoryDomains();
CoreComm.RomStatusDetails = string.Format("{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n",
game.Name,
Util.BytesToHexString(System.Security.Cryptography.SHA1.Create().ComputeHash(romdata)),
Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romdata))
);
TimeCallback = new LibGambatte.RTCCallback(GetCurrentTime);
LibGambatte.gambatte_setrtccallback(GambatteState, TimeCallback);
2012-09-08 21:36:04 +00:00
}
2012-09-09 02:06:07 +00:00
public static readonly ControllerDefinition GbController = new ControllerDefinition
2012-09-08 21:36:04 +00:00
{
Name = "Gameboy Controller",
BoolButtons =
{
"Up", "Down", "Left", "Right", "Start", "Select", "B", "A", "Power"
2012-09-08 21:36:04 +00:00
}
};
public ControllerDefinition ControllerDefinition
{
get { return GbController; }
}
public IController Controller { get; set; }
LibGambatte.Buttons ControllerCallback()
{
CoreComm.InputCallback.Call();
IsLagFrame = false;
return CurrentButtons;
}
/// <summary>
/// true if the emulator is currently emulating CGB
/// </summary>
/// <returns></returns>
public bool IsCGBMode()
{
return (LibGambatte.gambatte_iscgb(GambatteState));
}
2012-09-09 14:17:57 +00:00
internal void FrameAdvancePrep()
2012-09-08 21:36:04 +00:00
{
2012-09-09 14:17:57 +00:00
Controller.UpdateControls(Frame++);
// update our local copy of the controller data
CurrentButtons = 0;
if (Controller["Up"])
CurrentButtons |= LibGambatte.Buttons.UP;
if (Controller["Down"])
CurrentButtons |= LibGambatte.Buttons.DOWN;
if (Controller["Left"])
CurrentButtons |= LibGambatte.Buttons.LEFT;
if (Controller["Right"])
CurrentButtons |= LibGambatte.Buttons.RIGHT;
if (Controller["A"])
CurrentButtons |= LibGambatte.Buttons.A;
if (Controller["B"])
CurrentButtons |= LibGambatte.Buttons.B;
if (Controller["Select"])
CurrentButtons |= LibGambatte.Buttons.SELECT;
if (Controller["Start"])
CurrentButtons |= LibGambatte.Buttons.START;
// the controller callback will set this to false if it actually gets called during the frame
IsLagFrame = true;
// download any modified data to the core
foreach (var r in MemoryRefreshers)
r.RefreshWrite();
if (Controller["Power"])
LibGambatte.gambatte_reset(GambatteState, GetCurrentTime());
RefreshMemoryCallbacks();
if (CoreComm.Tracer.Enabled)
2012-11-02 19:44:31 +00:00
tracecb = MakeTrace;
else
tracecb = null;
LibGambatte.gambatte_settracecallback(GambatteState, tracecb);
}
internal void FrameAdvancePost()
{
// upload any modified data to the memory domains
foreach (var r in MemoryRefreshers)
r.RefreshRead();
if (IsLagFrame)
LagCount++;
2012-09-08 21:36:04 +00:00
if (endofframecallback != null)
endofframecallback(LibGambatte.gambatte_cpuread(GambatteState, 0xff40));
2012-09-08 21:36:04 +00:00
}
public void FrameAdvance(bool render, bool rendersound)
{
FrameAdvancePrep();
uint nsamp = 35112; // according to gambatte docs, this is the nominal length of a frame in 2mhz clocks
LibGambatte.gambatte_runfor(GambatteState, VideoBuffer, 160, soundbuff, ref nsamp);
if (rendersound)
2013-10-25 01:00:31 +00:00
{
soundbuffcontains = (int)nsamp;
2013-10-25 01:00:31 +00:00
ProcessSound();
}
else
2013-10-25 01:00:31 +00:00
{
soundbuffcontains = 0;
2013-10-25 01:00:31 +00:00
}
FrameAdvancePost();
}
static string MapperName(byte[] romdata)
{
switch (romdata[0x147])
{
case 0x00: return "Plain ROM"; // = PLAIN; break;
case 0x01: return "MBC1 ROM"; // = MBC1; break;
case 0x02: return "MBC1 ROM+RAM"; // = MBC1; break;
case 0x03: return "MBC1 ROM+RAM+BATTERY"; // = MBC1; break;
case 0x05: return "MBC2 ROM"; // = MBC2; break;
case 0x06: return "MBC2 ROM+BATTERY"; // = MBC2; break;
case 0x08: return "Plain ROM+RAM"; // = PLAIN; break;
case 0x09: return "Plain ROM+RAM+BATTERY"; // = PLAIN; break;
case 0x0F: return "MBC3 ROM+TIMER+BATTERY"; // = MBC3; break;
case 0x10: return "MBC3 ROM+TIMER+RAM+BATTERY"; // = MBC3; break;
case 0x11: return "MBC3 ROM"; // = MBC3; break;
case 0x12: return "MBC3 ROM+RAM"; // = MBC3; break;
case 0x13: return "MBC3 ROM+RAM+BATTERY"; // = MBC3; break;
case 0x19: return "MBC5 ROM"; // = MBC5; break;
case 0x1A: return "MBC5 ROM+RAM"; // = MBC5; break;
case 0x1B: return "MBC5 ROM+RAM+BATTERY"; // = MBC5; break;
case 0x1C: return "MBC5 ROM+RUMBLE"; // = MBC5; break;
case 0x1D: return "MBC5 ROM+RUMBLE+RAM"; // = MBC5; break;
case 0x1E: return "MBC5 ROM+RUMBLE+RAM+BATTERY"; // = MBC5; break;
case 0xFF: return "HuC1 ROM+RAM+BATTERY"; // = HUC1; break;
default: return "UNKNOWN";
}
}
/// <summary>
/// throw exception with intelligible message on some kinds of bad rom
/// </summary>
/// <param name="romdata"></param>
static void ThrowExceptionForBadRom(byte[] romdata)
{
if (romdata.Length < 0x148)
throw new Exception("ROM is far too small to be a valid GB\\GBC rom!");
2012-09-09 14:17:57 +00:00
switch (romdata[0x147])
{
case 0x00: break;
case 0x01: break;
case 0x02: break;
case 0x03: break;
case 0x05: break;
case 0x06: break;
case 0x08: break;
case 0x09: break;
case 0x0b: throw new Exception("\"MM01\" Mapper not supported!");
case 0x0c: throw new Exception("\"MM01\" Mapper not supported!");
case 0x0d: throw new Exception("\"MM01\" Mapper not supported!");
case 0x0f: break;
case 0x10: break;
case 0x11: break;
case 0x12: break;
case 0x13: break;
case 0x15: throw new Exception("\"MBC4\" Mapper not supported!");
case 0x16: throw new Exception("\"MBC4\" Mapper not supported!");
case 0x17: throw new Exception("\"MBC4\" Mapper not supported!");
case 0x19: break;
case 0x1a: break;
case 0x1b: break;
case 0x1c: break; // rumble
case 0x1d: break; // rumble
case 0x1e: break; // rumble
case 0x20: throw new Exception("\"MBC6\" Mapper not supported!");
case 0x22: throw new Exception("\"MBC7\" Mapper not supported!");
case 0xfc: throw new Exception("\"Pocket Camera\" Mapper not supported!");
case 0xfd: throw new Exception("\"Bandai TAMA5\" Mapper not supported!");
case 0xfe: throw new Exception("\"HuC3\" Mapper not supported!");
case 0xff: break;
default: throw new Exception(string.Format("Unknown mapper: {0:x2}", romdata[0x147]));
}
return;
}
2012-09-09 14:17:57 +00:00
public int Frame { get; set; }
2012-09-08 21:36:04 +00:00
public int LagCount { get; set; }
public bool IsLagFrame { get; private set; }
2012-09-08 21:36:04 +00:00
public string SystemId
{
get { return "GB"; }
}
public string BoardName { get; private set; }
public bool DeterministicEmulation { get { return true; } }
2012-09-08 21:36:04 +00:00
#region saveram
public byte[] ReadSaveRam()
2012-09-08 21:36:04 +00:00
{
int length = LibGambatte.gambatte_savesavedatalength(GambatteState);
if (length > 0)
{
byte[] ret = new byte[length];
LibGambatte.gambatte_savesavedata(GambatteState, ret);
return ret;
}
else
return new byte[0];
}
public void StoreSaveRam(byte[] data)
{
if (data.Length != LibGambatte.gambatte_savesavedatalength(GambatteState))
throw new ArgumentException("Size of saveram data does not match expected!");
LibGambatte.gambatte_loadsavedata(GambatteState, data);
2012-09-08 21:36:04 +00:00
}
/// <summary>
/// reset cart save ram, if any, to initial state
/// </summary>
public void ClearSaveRam()
{
int length = LibGambatte.gambatte_savesavedatalength(GambatteState);
if (length == 0)
return;
byte[] clear = new byte[length];
for (int i = 0; i < clear.Length; i++)
clear[i] = 0xff; // this exactly matches what gambatte core does
StoreSaveRam(clear);
}
2012-09-08 21:36:04 +00:00
public bool SaveRamModified
{
get
{
if (LibGambatte.gambatte_savesavedatalength(GambatteState) == 0)
return false;
else
return true; // need to wire more stuff into the core to actually know this
}
set { }
2012-09-08 21:36:04 +00:00
}
#endregion
public void ResetCounters()
2012-09-08 21:36:04 +00:00
{
Frame = 0;
LagCount = 0;
IsLagFrame = false;
2012-09-08 21:36:04 +00:00
}
#region savestates
/// <summary>
/// handles the core-portion of savestating
/// </summary>
/// <returns>private binary data corresponding to a savestate</returns>
byte[] SaveCoreBinary()
{
uint nlen = 0;
IntPtr ndata = IntPtr.Zero;
if (!LibGambatte.gambatte_savestate(GambatteState, VideoBuffer, 160, ref ndata, ref nlen))
throw new Exception("Gambatte failed to save the savestate!");
if (nlen == 0)
throw new Exception("Gambatte returned a 0-length savestate?");
byte[] data = new byte[nlen];
System.Runtime.InteropServices.Marshal.Copy(ndata, data, 0, (int)nlen);
LibGambatte.gambatte_savestate_destroy(ndata);
return data;
}
/// <summary>
/// handles the core portion of loadstating
/// </summary>
/// <param name="data">private binary data previously returned from SaveCoreBinary()</param>
void LoadCoreBinary(byte[] data)
{
if (!LibGambatte.gambatte_loadstate(GambatteState, data, (uint)data.Length))
throw new Exception("Gambatte failed to load the savestate!");
// since a savestate has been loaded, all memory domain data is now dirty
foreach (var r in MemoryRefreshers)
r.RefreshRead();
}
2012-11-19 17:59:57 +00:00
2012-09-08 21:36:04 +00:00
public void SaveStateText(System.IO.TextWriter writer)
{
var temp = SaveStateBinary();
temp.SaveAsHex(writer);
// write extra copy of stuff we don't use
writer.WriteLine("Frame {0}", Frame);
2012-09-08 21:36:04 +00:00
}
public void LoadStateText(System.IO.TextReader reader)
{
string hex = reader.ReadLine();
if (hex.StartsWith("emuVersion")) // movie save
{
do // theoretically, our portion should start right after StartsFromSavestate, maybe...
{
hex = reader.ReadLine();
} while (!hex.StartsWith("StartsFromSavestate"));
hex = reader.ReadLine();
}
byte[] state = new byte[hex.Length / 2];
state.ReadFromHex(hex);
LoadStateBinary(new BinaryReader(new MemoryStream(state)));
2012-09-08 21:36:04 +00:00
}
public void SaveStateBinary(System.IO.BinaryWriter writer)
{
byte[] data = SaveCoreBinary();
writer.Write(data.Length);
writer.Write(data);
// other variables
writer.Write(IsLagFrame);
writer.Write(LagCount);
writer.Write(Frame);
2012-09-08 21:36:04 +00:00
}
public void LoadStateBinary(System.IO.BinaryReader reader)
{
int length = reader.ReadInt32();
byte[] data = reader.ReadBytes(length);
LoadCoreBinary(data);
// other variables
IsLagFrame = reader.ReadBoolean();
LagCount = reader.ReadInt32();
Frame = reader.ReadInt32();
2012-09-08 21:36:04 +00:00
}
public byte[] SaveStateBinary()
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
SaveStateBinary(bw);
bw.Flush();
return ms.ToArray();
2012-09-08 21:36:04 +00:00
}
public bool BinarySaveStatesPreferred { get { return true; } }
#endregion
#region memorycallback
LibGambatte.MemoryCallback readcb;
LibGambatte.MemoryCallback writecb;
void RefreshMemoryCallbacks()
{
var mcs = CoreComm.MemoryCallbackSystem;
// we RefreshMemoryCallbacks() after the triggers in case the trigger turns itself off at that point
if (mcs.HasReads)
readcb = delegate(uint addr) { mcs.CallRead(addr); RefreshMemoryCallbacks(); };
else
readcb = null;
if (mcs.HasWrites)
writecb = delegate(uint addr) { mcs.CallWrite(addr); RefreshMemoryCallbacks(); };
else
writecb = null;
LibGambatte.gambatte_setreadcallback(GambatteState, readcb);
LibGambatte.gambatte_setwritecallback(GambatteState, writecb);
}
2012-11-19 17:59:57 +00:00
#endregion
2012-09-08 21:36:04 +00:00
public CoreComm CoreComm { get; set; }
2012-09-08 21:36:04 +00:00
2012-11-02 19:44:31 +00:00
LibGambatte.TraceCallback tracecb;
void MakeTrace(IntPtr _s)
{
int[] s = new int[13];
System.Runtime.InteropServices.Marshal.Copy(_s, s, 0, 13);
ushort unused;
2012-11-02 19:44:31 +00:00
CoreComm.Tracer.Put(string.Format(
"{13} SP:{2:x2} A:{3:x2} B:{4:x2} C:{5:x2} D:{6:x2} E:{7:x2} F:{8:x2} H:{9:x2} L:{10:x2} {11} Cy:{0}",
2012-11-02 19:44:31 +00:00
s[0],
s[1] & 0xffff,
s[2] & 0xffff,
s[3] & 0xff,
s[4] & 0xff,
s[5] & 0xff,
s[6] & 0xff,
s[7] & 0xff,
s[8] & 0xff,
s[9] & 0xff,
s[10] & 0xff,
s[11] != 0 ? "skip" : "",
s[12] & 0xff,
2012-11-03 18:17:55 +00:00
CPUs.Z80GB.NewDisassembler.Disassemble((ushort)s[1], (addr) => LibGambatte.gambatte_cpuread(GambatteState, addr), out unused).PadRight(30)
2012-11-02 19:44:31 +00:00
));
}
#region MemoryDomains
class MemoryRefresher
{
IntPtr data;
int length;
byte[] CachedMemory;
public MemoryRefresher(IntPtr data, int length)
{
this.data = data;
this.length = length;
CachedMemory = new byte[length];
writeneeded = false;
// needs to be true in case a read is attempted before the first frame advance
readneeded = true;
}
2012-11-19 17:59:57 +00:00
bool readneeded;
bool writeneeded;
/// <summary>
/// reads data from native core to managed buffer
/// </summary>
public void RefreshRead()
{
readneeded = true;
}
/// <summary>
/// writes data from managed buffer back to core
/// </summary>
public void RefreshWrite()
{
if (writeneeded)
{
System.Runtime.InteropServices.Marshal.Copy(CachedMemory, 0, data, length);
writeneeded = false;
}
}
public byte Peek(int addr)
{
if (readneeded)
{
System.Runtime.InteropServices.Marshal.Copy(data, CachedMemory, 0, length);
readneeded = false;
}
return CachedMemory[addr % CachedMemory.Length];
}
public void Poke(int addr, byte val)
{
// a poke without any peek is certainly legal. we need to update read, because writeneeded = true means that
// all of this data will be downloaded before the next frame. so everything but that which was poked needs to
// be up to date.
if (readneeded)
{
System.Runtime.InteropServices.Marshal.Copy(data, CachedMemory, 0, length);
readneeded = false;
}
CachedMemory[addr % CachedMemory.Length] = val;
writeneeded = true;
}
}
void CreateMemoryDomain(LibGambatte.MemoryAreas which, string name)
2012-09-08 21:36:04 +00:00
{
IntPtr data = IntPtr.Zero;
int length = 0;
if (!LibGambatte.gambatte_getmemoryarea(GambatteState, which, ref data, ref length))
throw new Exception("gambatte_getmemoryarea() failed!");
// if length == 0, it's an empty block; (usually rambank on some carts); that's ok
// TODO: when length == 0, should we simply not add the memory domain at all?
if (data == IntPtr.Zero && length > 0)
throw new Exception("bad return from gambatte_getmemoryarea()");
var refresher = new MemoryRefresher(data, length);
MemoryRefreshers.Add(refresher);
_MemoryDomains.Add(new MemoryDomain(name, length, MemoryDomain.Endian.Little, refresher.Peek, refresher.Poke));
2012-09-08 21:36:04 +00:00
}
void InitMemoryDomains()
2012-09-08 21:36:04 +00:00
{
MemoryRefreshers = new List<MemoryRefresher>();
CreateMemoryDomain(LibGambatte.MemoryAreas.wram, "WRAM");
CreateMemoryDomain(LibGambatte.MemoryAreas.rom, "ROM");
CreateMemoryDomain(LibGambatte.MemoryAreas.vram, "VRAM");
CreateMemoryDomain(LibGambatte.MemoryAreas.cartram, "Cart RAM");
CreateMemoryDomain(LibGambatte.MemoryAreas.oam, "OAM");
CreateMemoryDomain(LibGambatte.MemoryAreas.hram, "HRAM");
// also add a special memory domain for the system bus, where calls get sent directly to the core each time
_MemoryDomains.Add(new MemoryDomain("System Bus", 65536, MemoryDomain.Endian.Little,
delegate(int addr)
{
return LibGambatte.gambatte_cpuread(GambatteState, (ushort)addr);
},
delegate(int addr, byte val)
{
LibGambatte.gambatte_cpuwrite(GambatteState, (ushort)addr, val);
}));
MemoryDomains = new MemoryDomainList(_MemoryDomains);
2012-09-08 21:36:04 +00:00
}
private List<MemoryDomain> _MemoryDomains = new List<MemoryDomain>();
public MemoryDomainList MemoryDomains { get; private set; }
2012-11-19 17:59:57 +00:00
List<MemoryRefresher> MemoryRefreshers;
#endregion
#region ppudebug
public bool GetGPUMemoryAreas(out IntPtr vram, out IntPtr bgpal, out IntPtr sppal, out IntPtr oam)
{
IntPtr _vram = IntPtr.Zero;
IntPtr _bgpal = IntPtr.Zero;
IntPtr _sppal = IntPtr.Zero;
IntPtr _oam = IntPtr.Zero;
int unused = 0;
if (!LibGambatte.gambatte_getmemoryarea(GambatteState, LibGambatte.MemoryAreas.vram, ref _vram, ref unused)
|| !LibGambatte.gambatte_getmemoryarea(GambatteState, LibGambatte.MemoryAreas.bgpal, ref _bgpal, ref unused)
|| !LibGambatte.gambatte_getmemoryarea(GambatteState, LibGambatte.MemoryAreas.sppal, ref _sppal, ref unused)
|| !LibGambatte.gambatte_getmemoryarea(GambatteState, LibGambatte.MemoryAreas.oam, ref _oam, ref unused))
{
vram = IntPtr.Zero;
bgpal = IntPtr.Zero;
sppal = IntPtr.Zero;
oam = IntPtr.Zero;
return false;
}
vram = _vram;
bgpal = _bgpal;
sppal = _sppal;
oam = _oam;
return true;
}
/// <summary>
///
/// </summary>
/// <param name="lcdc">current value of register $ff40 (LCDC)</param>
public delegate void ScanlineCallback(int lcdc);
/// <summary>
/// set up callback
/// </summary>
/// <param name="callback"></param>
/// <param name="line">scanline. -1 = end of frame, -2 = RIGHT NOW</param>
public void SetScanlineCallback(ScanlineCallback callback, int line)
{
if (GambatteState == IntPtr.Zero)
// not sure how this is being reached. tried the debugger...
return;
endofframecallback = null;
if (callback == null || line == -1 || line == -2)
{
scanlinecb = null;
LibGambatte.gambatte_setscanlinecallback(GambatteState, null, 0);
if (line == -1)
endofframecallback = callback;
else if (line == -2)
callback(LibGambatte.gambatte_cpuread(GambatteState, 0xff40));
}
else if (line >= 0 && line <= 153)
{
scanlinecb = delegate()
{
callback(LibGambatte.gambatte_cpuread(GambatteState, 0xff40));
};
LibGambatte.gambatte_setscanlinecallback(GambatteState, scanlinecb, line);
}
else
throw new ArgumentOutOfRangeException("line must be in [0, 153]");
}
LibGambatte.ScanlineCallback scanlinecb;
ScanlineCallback endofframecallback;
#endregion
2012-09-08 21:36:04 +00:00
public void Dispose()
{
LibGambatte.gambatte_destroy(GambatteState);
GambatteState = IntPtr.Zero;
DisposeSound();
2012-09-08 21:36:04 +00:00
}
#region IVideoProvider
public IVideoProvider VideoProvider
{
get { return this; }
}
/// <summary>
/// stored image of most recent frame
/// </summary>
2012-09-08 21:36:04 +00:00
int[] VideoBuffer = new int[160 * 144];
public int[] GetVideoBuffer()
{
return VideoBuffer;
}
public int VirtualWidth
{
// only sgb changes this, which we don't emulate here
2012-09-08 21:36:04 +00:00
get { return 160; }
}
public int BufferWidth
{
get { return 160; }
}
public int BufferHeight
{
get { return 144; }
}
public int BackgroundColor
{
get { return 0; }
}
#endregion
#region palette
2012-09-12 22:18:51 +00:00
/// <summary>
/// update gambatte core's internal colors
/// </summary>
public void ChangeDMGColors(int[] colors)
2012-09-12 22:18:51 +00:00
{
for (int i = 0; i < 12; i++)
LibGambatte.gambatte_setdmgpalettecolor(GambatteState, (LibGambatte.PalType)(i / 4), (uint)i % 4, (uint)colors[i]);
2012-09-12 22:18:51 +00:00
}
public void SetCGBColors(GBColors.ColorType type)
{
int[] lut = GBColors.GetLut(type);
2012-11-19 17:59:57 +00:00
LibGambatte.gambatte_setcgbpalette(GambatteState, lut);
}
2012-09-08 21:36:04 +00:00
#endregion
#region ISoundProvider
2012-11-19 17:59:57 +00:00
public ISoundProvider SoundProvider { get { return null; } }
2013-10-25 01:00:31 +00:00
public ISyncSoundProvider SyncSoundProvider { get { return this; } }
public bool StartAsyncSound() { return false; }
public void EndAsyncSound() { }
/// <summary>
/// sample pairs before resampling
/// </summary>
2012-12-29 17:11:19 +00:00
short[] soundbuff = new short[(35112 + 2064) * 2];
/// <summary>
/// how many sample pairs are in soundbuff
/// </summary>
2012-12-29 17:11:19 +00:00
int soundbuffcontains = 0;
2013-10-25 01:00:31 +00:00
int soundoutbuffcontains = 0;
short[] soundoutbuff = new short[2048];
int latchaudio = 0;
//Sound.Utilities.SpeexResampler resampler;
//Sound.Utilities.DCFilter dcfilter;
Sound.Utilities.BlipBuffer blip;
void ProcessSound()
{
for (uint i = 0; i < soundbuffcontains; i++)
{
int curr = soundbuff[i * 2];
if (curr != latchaudio)
{
int diff = latchaudio - curr;
latchaudio = curr;
blip.AddDelta(i, diff);
}
}
blip.EndFrame((uint)soundbuffcontains);
soundoutbuffcontains = blip.SamplesAvailable();
blip.ReadSamples(soundoutbuff, soundoutbuffcontains, true);
for (int i = 0; i < soundoutbuffcontains * 2; i += 2)
soundoutbuff[i + 1] = soundoutbuff[i];
soundbuffcontains = 0;
}
void InitSound()
{
2013-10-25 01:00:31 +00:00
//resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this);
//dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 65536);
// lowpass filtering on an actual GB was probably pretty aggressive?
2013-10-25 01:00:31 +00:00
//dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 2048);
blip = new Sound.Utilities.BlipBuffer(1024);
blip.SetRates(2097152, 44100);
}
void DisposeSound()
{
2013-10-25 01:00:31 +00:00
blip.Dispose();
blip = null;
//resampler.Dispose();
//resampler = null;
}
public void DiscardSamples()
{
soundbuffcontains = 0;
}
public void GetSamples(out short[] samples, out int nsamp)
{
2013-10-25 01:00:31 +00:00
samples = soundoutbuff;
nsamp = soundoutbuffcontains;
}
#endregion
2012-09-08 21:36:04 +00:00
}
}