add core scanning system

This commit is contained in:
zeromus 2011-07-31 19:46:42 +00:00
parent 64e38e83c8
commit f356d5b354
3 changed files with 295 additions and 248 deletions

View File

@ -8,6 +8,7 @@ using BizHawk.Emulation.CPUs.M6502;
namespace BizHawk.Emulation.Consoles.Nintendo
{
[CoreVersion("0.0.0.1",FriendlyName="NESHawk")]
public partial class NES : IEmulator
{
//hardware/state

View File

@ -1,4 +1,5 @@
using System;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
@ -8,41 +9,80 @@ using System.Text;
namespace BizHawk
{
public struct Tuple<T1,T2> : IEquatable<Tuple<T1, T2>>
{
private readonly T1 first;
private readonly T2 second;
public T1 First { get { return first; } }
public T2 Second { get { return second; } }
public struct Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
{
private readonly T1 first;
private readonly T2 second;
public T1 First { get { return first; } }
public T2 Second { get { return second; } }
public Tuple(T1 o1, T2 o2)
{
first = o1;
second = o2;
}
public Tuple(T1 o1, T2 o2)
{
first = o1;
second = o2;
}
public bool Equals(Tuple<T1, T2> other)
{
return first.Equals(other.first) &&
second.Equals(other.second);
}
public bool Equals(Tuple<T1, T2> other)
{
return first.Equals(other.first) &&
second.Equals(other.second);
}
public override bool Equals(object obj)
{
if (obj is Tuple<T1, T2>)
return this.Equals((Tuple<T1, T2>)obj);
else
return false;
}
public override bool Equals(object obj)
{
if (obj is Tuple<T1, T2>)
return this.Equals((Tuple<T1, T2>)obj);
else
return false;
}
public override int GetHashCode()
{
return first.GetHashCode() ^ second.GetHashCode();
}
}
public override int GetHashCode()
{
return first.GetHashCode() ^ second.GetHashCode();
}
}
public static class Extensions
{
[AttributeUsage(AttributeTargets.Class)]
public class CoreVersion : Attribute
{
public CoreVersion(string version)
{
this.Version = version;
}
public string Version { get; set; }
public string FriendlyName { get; set; }
}
public static class Introspection
{
public class CoreInfo
{
public string ClassName, Version, FriendlyName;
}
public static List<CoreInfo> GetCoreInfo()
{
var ret = new List<CoreInfo>();
//scan types in this assembly to find ones that implement boards to add them to the list
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
var attrs = type.GetCustomAttributes(typeof(CoreVersion), true);
if (attrs.Length == 0) continue;
var cv = (CoreVersion)attrs[0];
var ci = new CoreInfo();
ci.ClassName = type.Name;
ci.FriendlyName = cv.FriendlyName;
if (string.IsNullOrEmpty(ci.FriendlyName)) ci.FriendlyName = ci.ClassName;
ci.Version = cv.Version;
ret.Add(ci);
}
return ret;
}
}
public static class Extensions
{
public static void CopyTo(this Stream src, Stream dest)
{
int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
@ -76,17 +116,17 @@ namespace BizHawk
}
public static bool IsBinary(this string str)
{
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == '0' || c == '1')
continue;
return false;
}
return true;
}
public static bool IsBinary(this string str)
{
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == '0' || c == '1')
continue;
return false;
}
return true;
}
public static bool Bit(this byte b, int index)
{
@ -99,161 +139,161 @@ namespace BizHawk
}
public static string GetPrecedingString(this string str, string value)
{
int index = str.IndexOf(value);
if (index < 0)
return null;
if (index == 0)
return "";
return str.Substring(0, index);
}
public static string GetPrecedingString(this string str, string value)
{
int index = str.IndexOf(value);
if (index < 0)
return null;
if (index == 0)
return "";
return str.Substring(0, index);
}
public static bool In(this string str, params string[] options)
{
foreach (string opt in options)
{
if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
}
public static bool In(this string str, params string[] options)
{
foreach (string opt in options)
{
if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
}
public static bool In(this string str, IEnumerable<string> options)
{
foreach (string opt in options)
{
if (opt.Equals(str,StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
}
public static bool In(this string str, IEnumerable<string> options)
{
foreach (string opt in options)
{
if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
}
public static bool In<T>(this string str, IEnumerable<T> options, Func<T, string, bool> eval)
{
foreach (T opt in options)
{
if (eval(opt, str) == true)
return true;
}
return false;
}
public static bool In<T>(this string str, IEnumerable<T> options, Func<T, string, bool> eval)
{
foreach (T opt in options)
{
if (eval(opt, str) == true)
return true;
}
return false;
}
public static bool NotIn(this string str, params string[] options)
{
foreach (string opt in options)
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
}
public static bool NotIn(this string str, params string[] options)
{
foreach (string opt in options)
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
}
public static bool NotIn(this string str, IEnumerable<string> options)
{
foreach (string opt in options)
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
}
public static bool In(this int i, params int[] options)
{
foreach (int j in options)
{
if (i == j) return true;
}
return false;
}
public static bool NotIn(this string str, IEnumerable<string> options)
{
foreach (string opt in options)
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
}
public static bool In(this int i, IEnumerable<int> options)
{
foreach (int j in options)
{
if (i == j) return true;
}
return false;
}
public static bool In(this int i, params int[] options)
{
foreach (int j in options)
{
if (i == j) return true;
}
return false;
}
public static bool ContainsStartsWith(this IEnumerable<string> options, string str)
{
foreach (string opt in options)
{
if (opt.StartsWith(str)) return true;
}
return false;
}
public static bool In(this int i, IEnumerable<int> options)
{
foreach (int j in options)
{
if (i == j) return true;
}
return false;
}
public static string GetOptionValue(this IEnumerable<string> options, string str)
{
try
{
foreach (string opt in options)
{
if (opt.StartsWith(str))
{
return opt.Split('=')[1];
}
}
}
catch (Exception) { }
return null;
}
public static bool ContainsStartsWith(this IEnumerable<string> options, string str)
{
foreach (string opt in options)
{
if (opt.StartsWith(str)) return true;
}
return false;
}
public static bool IsValidRomExtentsion(this string str, params string[] romExtensions)
{
string strUpper = str.ToUpper();
foreach (string ext in romExtensions)
{
if (strUpper.EndsWith(ext.ToUpper())) return true;
}
return false;
}
public static string GetOptionValue(this IEnumerable<string> options, string str)
{
try
{
foreach (string opt in options)
{
if (opt.StartsWith(str))
{
return opt.Split('=')[1];
}
}
}
catch (Exception) { }
return null;
}
public static string ToCommaSeparated(this List<string> list)
{
var sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i > 0) sb.Append(",");
sb.Append(list[i]);
}
return sb.ToString();
}
public static bool IsValidRomExtentsion(this string str, params string[] romExtensions)
{
string strUpper = str.ToUpper();
foreach (string ext in romExtensions)
{
if (strUpper.EndsWith(ext.ToUpper())) return true;
}
return false;
}
public static void SaveAsHex(this byte[] buffer, TextWriter writer)
{
for (int i=0; i<buffer.Length; i++)
{
writer.Write("{0:X2}", buffer[i]);
}
writer.WriteLine();
}
public static string ToCommaSeparated(this List<string> list)
{
var sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i > 0) sb.Append(",");
sb.Append(list[i]);
}
return sb.ToString();
}
public static void SaveAsHex(this byte[] buffer, TextWriter writer, int length)
{
for (int i = 0; i < length; i++)
{
writer.Write("{0:X2}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this byte[] buffer, TextWriter writer)
{
for (int i = 0; i < buffer.Length; i++)
{
writer.Write("{0:X2}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this short[] buffer, TextWriter writer)
{
for (int i = 0; i < buffer.Length; i++)
{
writer.Write("{0:X4}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this byte[] buffer, TextWriter writer, int length)
{
for (int i = 0; i < length; i++)
{
writer.Write("{0:X2}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this ushort[] buffer, TextWriter writer)
{
for (int i = 0; i < buffer.Length; i++)
{
writer.Write("{0:X4}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this short[] buffer, TextWriter writer)
{
for (int i = 0; i < buffer.Length; i++)
{
writer.Write("{0:X4}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this ushort[] buffer, TextWriter writer)
{
for (int i = 0; i < buffer.Length; i++)
{
writer.Write("{0:X4}", buffer[i]);
}
writer.WriteLine();
}
public static void SaveAsHex(this int[] buffer, TextWriter writer)
{
@ -314,38 +354,38 @@ namespace BizHawk
}
public static void ReadFromHex(this byte[] buffer, string hex)
{
if (hex.Length % 2 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i=0; i<buffer.Length && i*2<hex.Length; i++)
{
string bytehex = "" + hex[i*2] + hex[i*2 + 1];
buffer[i] = byte.Parse(bytehex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this byte[] buffer, string hex)
{
if (hex.Length % 2 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++)
{
string bytehex = "" + hex[i * 2] + hex[i * 2 + 1];
buffer[i] = byte.Parse(bytehex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this short[] buffer, string hex)
{
if (hex.Length % 4 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++)
{
string shorthex = "" + hex[i * 4] + hex[(i * 4) + 1] + hex[(i * 4) + 2] + hex[(i * 4) + 3];
buffer[i] = short.Parse(shorthex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this short[] buffer, string hex)
{
if (hex.Length % 4 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++)
{
string shorthex = "" + hex[i * 4] + hex[(i * 4) + 1] + hex[(i * 4) + 2] + hex[(i * 4) + 3];
buffer[i] = short.Parse(shorthex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this ushort[] buffer, string hex)
{
if (hex.Length % 4 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++)
{
string ushorthex = "" + hex[i*4] + hex[(i*4)+1] + hex[(i*4)+2] + hex[(i*4)+3];
buffer[i] = ushort.Parse(ushorthex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this ushort[] buffer, string hex)
{
if (hex.Length % 4 != 0)
throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++)
{
string ushorthex = "" + hex[i * 4] + hex[(i * 4) + 1] + hex[(i * 4) + 2] + hex[(i * 4) + 3];
buffer[i] = ushort.Parse(ushorthex, NumberStyles.HexNumber);
}
}
public static void ReadFromHex(this int[] buffer, BinaryWriter bw)
{
@ -362,25 +402,25 @@ namespace BizHawk
//these don't work??? they dont get chosen by compiler
public static void WriteBit(this BinaryWriter bw, Bit bit) { bw.Write((bool)bit); }
public static Bit ReadBit(this BinaryReader br) { return br.ReadBoolean(); }
}
}
public static class Colors
{
public static int ARGB(byte red, byte green, byte blue)
{
return (int) ((uint)((red << 0x10) | (green << 8) | blue | (0xFF << 0x18)));
}
public static class Colors
{
public static int ARGB(byte red, byte green, byte blue)
{
return (int)((uint)((red << 0x10) | (green << 8) | blue | (0xFF << 0x18)));
}
public static int ARGB(byte red, byte green, byte blue, byte alpha)
{
return (int) ((uint)((red << 0x10) | (green << 8) | blue | (alpha << 0x18)));
}
public static int ARGB(byte red, byte green, byte blue, byte alpha)
{
return (int)((uint)((red << 0x10) | (green << 8) | blue | (alpha << 0x18)));
}
public static int Luminosity(byte lum)
{
return (int)((uint)((lum << 0x10) | (lum << 8) | lum | (0xFF << 0x18)));
}
}
public static int Luminosity(byte lum)
{
return (int)((uint)((lum << 0x10) | (lum << 8) | lum | (0xFF << 0x18)));
}
}
@ -410,8 +450,8 @@ namespace BizHawk
public static class Util
{
public static class Util
{
public static string Hash_MD5(byte[] data, int offset, int len)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
@ -437,13 +477,13 @@ namespace BizHawk
return (x & (x - 1)) == 0;
}
public static int SaveRamBytesUsed(byte[] SaveRAM)
{
for (int j = SaveRAM.Length - 1; j >= 0; j--)
if (SaveRAM[j] != 0)
return j + 1;
return 0;
}
public static int SaveRamBytesUsed(byte[] SaveRAM)
{
for (int j = SaveRAM.Length - 1; j >= 0; j--)
if (SaveRAM[j] != 0)
return j + 1;
return 0;
}
/// <summary>
/// conerts bytes to an uppercase string of hex numbers in upper case without any spacing or anything
@ -462,7 +502,7 @@ namespace BizHawk
{
MemoryStream ms = new MemoryStream();
if (str.Length % 2 != 0) throw new ArgumentException();
int len = str.Length/2;
int len = str.Length / 2;
for (int i = 0; i < len; i++)
{
int d = 0;
@ -494,11 +534,11 @@ namespace BizHawk
public static short[] ByteBufferToShortBuffer(byte[] buf)
{
int num = buf.Length/2;
int num = buf.Length / 2;
short[] ret = new short[num];
for (int i = 0; i < num; i++)
{
ret[i] =(short)(buf[i * 2] | (buf[i * 2 + 1] << 8));
ret[i] = (short)(buf[i * 2] | (buf[i * 2 + 1] << 8));
}
return ret;
}
@ -510,7 +550,7 @@ namespace BizHawk
for (int i = 0; i < num; i++)
{
ret[i * 2 + 0] = (byte)(buf[i] & 0xFF);
ret[i * 2 + 1] = (byte)((buf[i]>>8) & 0xFF);
ret[i * 2 + 1] = (byte)((buf[i] >> 8) & 0xFF);
}
return ret;
}
@ -583,14 +623,14 @@ namespace BizHawk
Decimal OneGigaByte = OneMegaByte * 1024M;
string suffix;
if (size > 1024*1024*1024)
if (size > 1024 * 1024 * 1024)
{
size /= 1024*1024*1024;
size /= 1024 * 1024 * 1024;
suffix = "GB";
}
else if (size > 1024*1024)
else if (size > 1024 * 1024)
{
size /= 1024*1024;
size /= 1024 * 1024;
suffix = "MB";
}
else if (size > 1024)
@ -643,7 +683,7 @@ namespace BizHawk
{
sections.Push(name);
if (IsText)
if (IsWriter) { tw.WriteLine("[{0}]", name);}
if (IsWriter) { tw.WriteLine("[{0}]", name); }
else { tr.ReadLine(); }
}
@ -659,7 +699,7 @@ namespace BizHawk
{
if (typeof(T).BaseType != typeof(System.Enum))
throw new InvalidOperationException();
if(isText) SyncEnumText<T>(name, ref val);
if (isText) SyncEnumText<T>(name, ref val);
else if (IsReader) val = (T)Enum.ToObject(typeof(T), br.ReadInt32());
else bw.Write(Convert.ToInt32(val));
}
@ -907,7 +947,7 @@ namespace BizHawk
void Read(ref int val) { val = br.ReadInt32(); }
void Write(ref int val) { bw.Write(val); }
void ReadText(string name, ref int val) { val = int.Parse(tr.ReadLine().Split(' ')[1].Replace("0x",""), NumberStyles.HexNumber); }
void ReadText(string name, ref int val) { val = int.Parse(tr.ReadLine().Split(' ')[1].Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); }
void Read(ref bool val) { val = br.ReadBoolean(); }

View File

@ -87,6 +87,12 @@ namespace BizHawk.MultiClient
Global.CoreInputComm = new CoreInputComm();
SyncCoreInputComm();
Console.WriteLine("Scanning cores:");
foreach (var ci in Introspection.GetCoreInfo())
{
Console.WriteLine("{0} - {1} ({2})", ci.FriendlyName, ci.Version, ci.ClassName);
}
Database.LoadDatabase(PathManager.GetExeDirectoryAbsolute() + "\\gamedb.txt");
SyncPresentationMode();