some code cleanup

This commit is contained in:
adelikat 2014-02-22 23:59:52 +00:00
parent a907d3ce7b
commit 02ea7d360f
5 changed files with 138 additions and 95 deletions

View File

@ -1,15 +1,30 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class NESGameGenieDecoder public class NESGameGenieDecoder
{ {
public int Address { get; private set; } private readonly string _code = string.Empty;
public int Value { get; private set; }
public int? Compare { get; private set; }
private readonly string _code = String.Empty; private readonly Dictionary<char, int> _gameGenieTable = new Dictionary<char, int>
{
{ 'A', 0 }, // 0000
{ 'P', 1 }, // 0001
{ 'Z', 2 }, // 0010
{ 'L', 3 }, // 0011
{ 'G', 4 }, // 0100
{ 'I', 5 }, // 0101
{ 'T', 6 }, // 0110
{ 'Y', 7 }, // 0111
{ 'E', 8 }, // 1000
{ 'O', 9 }, // 1001
{ 'X', 10 }, // 1010
{ 'U', 11 }, // 1011
{ 'K', 12 }, // 1100
{ 'S', 13 }, // 1101
{ 'V', 14 }, // 1110
{ 'N', 15 }, // 1111
};
public NESGameGenieDecoder(string code) public NESGameGenieDecoder(string code)
{ {
@ -17,115 +32,97 @@ namespace BizHawk.Client.Common
Decode(); Decode();
} }
private readonly Dictionary<char, int> GameGenieTable = new Dictionary<char, int> public int Address { get; private set; }
{ public int Value { get; private set; }
{ 'A', 0 }, //0000 public int? Compare { get; private set; }
{ 'P', 1 }, //0001
{ 'Z', 2 }, //0010
{ 'L', 3 }, //0011
{ 'G', 4 }, //0100
{ 'I', 5 }, //0101
{ 'T', 6 }, //0110
{ 'Y', 7 }, //0111
{ 'E', 8 }, //1000
{ 'O', 9 }, //1001
{ 'X', 10}, //1010
{ 'U', 11}, //1011
{ 'K', 12}, //1100
{ 'S', 13}, //1101
{ 'V', 14}, //1110
{ 'N', 15}, //1111
};
public void Decode() public void Decode()
{ {
//char 3 bit 3 denotes the code length. // char 3 bit 3 denotes the code length.
if (_code.Length == 6) if (_code.Length == 6)
{ {
//Char # | 1 | 2 | 3 | 4 | 5 | 6 | // Char # | 1 | 2 | 3 | 4 | 5 | 6 |
//Bit # |3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0| // Bit # |3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|
//maps to|1|6|7|8|H|2|3|4|-|I|J|K|L|A|B|C|D|M|N|O|5|E|F|G| // maps to|1|6|7|8|H|2|3|4|-|I|J|K|L|A|B|C|D|M|N|O|5|E|F|G|
Value = 0; Value = 0;
Address = 0x8000; Address = 0x8000;
int x; int x;
GameGenieTable.TryGetValue(_code[0], out x); _gameGenieTable.TryGetValue(_code[0], out x);
Value |= (x & 0x07); Value |= x & 0x07;
Value |= (x & 0x08) << 4; Value |= (x & 0x08) << 4;
GameGenieTable.TryGetValue(_code[1], out x); _gameGenieTable.TryGetValue(_code[1], out x);
Value |= (x & 0x07) << 4; Value |= (x & 0x07) << 4;
Address |= (x & 0x08) << 4; Address |= (x & 0x08) << 4;
GameGenieTable.TryGetValue(_code[2], out x); _gameGenieTable.TryGetValue(_code[2], out x);
Address |= (x & 0x07) << 4; Address |= (x & 0x07) << 4;
GameGenieTable.TryGetValue(_code[3], out x); _gameGenieTable.TryGetValue(_code[3], out x);
Address |= (x & 0x07) << 12; Address |= (x & 0x07) << 12;
Address |= (x & 0x08); Address |= x & 0x08;
GameGenieTable.TryGetValue(_code[4], out x); _gameGenieTable.TryGetValue(_code[4], out x);
Address |= (x & 0x07); Address |= x & 0x07;
Address |= (x & 0x08) << 8; Address |= (x & 0x08) << 8;
GameGenieTable.TryGetValue(_code[5], out x); _gameGenieTable.TryGetValue(_code[5], out x);
Address |= (x & 0x07) << 8; Address |= (x & 0x07) << 8;
Value |= (x & 0x08); Value |= x & 0x08;
} }
else if (_code.Length == 8) else if (_code.Length == 8)
{ {
//Char # | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | // Char # | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
//Bit # |3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0| // Bit # |3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|
//maps to|1|6|7|8|H|2|3|4|-|I|J|K|L|A|B|C|D|M|N|O|%|E|F|G|!|^|&|*|5|@|#|$| // maps to|1|6|7|8|H|2|3|4|-|I|J|K|L|A|B|C|D|M|N|O|%|E|F|G|!|^|&|*|5|@|#|$|
Value = 0; Value = 0;
Address = 0x8000; Address = 0x8000;
Compare = 0; Compare = 0;
int x; int x;
GameGenieTable.TryGetValue(_code[0], out x); _gameGenieTable.TryGetValue(_code[0], out x);
Value |= (x & 0x07); Value |= x & 0x07;
Value |= (x & 0x08) << 4; Value |= (x & 0x08) << 4;
GameGenieTable.TryGetValue(_code[1], out x); _gameGenieTable.TryGetValue(_code[1], out x);
Value |= (x & 0x07) << 4; Value |= (x & 0x07) << 4;
Address |= (x & 0x08) << 4; Address |= (x & 0x08) << 4;
GameGenieTable.TryGetValue(_code[2], out x); _gameGenieTable.TryGetValue(_code[2], out x);
Address |= (x & 0x07) << 4; Address |= (x & 0x07) << 4;
GameGenieTable.TryGetValue(_code[3], out x); _gameGenieTable.TryGetValue(_code[3], out x);
Address |= (x & 0x07) << 12; Address |= (x & 0x07) << 12;
Address |= (x & 0x08); Address |= x & 0x08;
GameGenieTable.TryGetValue(_code[4], out x); _gameGenieTable.TryGetValue(_code[4], out x);
Address |= (x & 0x07); Address |= x & 0x07;
Address |= (x & 0x08) << 8; Address |= (x & 0x08) << 8;
GameGenieTable.TryGetValue(_code[5], out x); _gameGenieTable.TryGetValue(_code[5], out x);
Address |= (x & 0x07) << 8; Address |= (x & 0x07) << 8;
Compare |= (x & 0x08); Compare |= x & 0x08;
GameGenieTable.TryGetValue(_code[6], out x); _gameGenieTable.TryGetValue(_code[6], out x);
Compare |= (x & 0x07); Compare |= x & 0x07;
Compare |= (x & 0x08) << 4; Compare |= (x & 0x08) << 4;
GameGenieTable.TryGetValue(_code[7], out x); _gameGenieTable.TryGetValue(_code[7], out x);
Compare |= (x & 0x07) << 4; Compare |= (x & 0x07) << 4;
Value |= (x & 0x08); Value |= x & 0x08;
} }
} }
} }
public class NESGameGenieEncoder public class NESGameGenieEncoder
{ {
private readonly char[] letters = { 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y', 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N' }; private readonly char[] _letters = { 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y', 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N' };
private int _address; private readonly int _address;
private int _value; private readonly int _value;
private int? _compare; private int? _compare;
public string GameGenieCode { get; private set; }
public NESGameGenieEncoder(int address, int value, int? compare) public NESGameGenieEncoder(int address, int value, int? compare)
{ {
_address = address; _address = address;
@ -133,21 +130,24 @@ namespace BizHawk.Client.Common
{ {
_address -= 0x8000; _address -= 0x8000;
} }
_value = value; _value = value;
_compare = compare; _compare = compare;
GameGenieCode = String.Empty; GameGenieCode = string.Empty;
} }
public string GameGenieCode { get; private set; }
public string Encode() public string Encode()
{ {
byte[] num = { 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] num = { 0, 0, 0, 0, 0, 0, 0, 0 };
num[0] = (byte)((_value & 7) + ((_value >> 4) & 8)); num[0] = (byte)((_value & 7) + ((_value >> 4) & 8));
num[1] = (byte)(((_value >> 4) & 7) + ((_address >> 4) & 8)); num[1] = (byte)(((_value >> 4) & 7) + ((_address >> 4) & 8));
num[2] = (byte)(((_address >> 4) & 7)); num[2] = (byte)((_address >> 4) & 7);
num[3] = (byte)((_address >> 12) + (_address & 8)); num[3] = (byte)((_address >> 12) + (_address & 8));
num[4] = (byte)((_address & 7) + ((_address >> 8) & 8)); num[4] = (byte)((_address & 7) + ((_address >> 8) & 8));
num[5] = (byte)(((_address >> 8) & 7)); num[5] = (byte)((_address >> 8) & 7);
if (_compare.HasValue) if (_compare.HasValue)
{ {
@ -155,17 +155,17 @@ namespace BizHawk.Client.Common
num[5] += (byte)(_compare & 8); num[5] += (byte)(_compare & 8);
num[6] = (byte)((_compare & 7) + ((_compare >> 4) & 8)); num[6] = (byte)((_compare & 7) + ((_compare >> 4) & 8));
num[7] = (byte)(((_compare >> 4) & 7) + (_value & 8)); num[7] = (byte)(((_compare >> 4) & 7) + (_value & 8));
for (int i = 0; i < 8; i++) for (var i = 0; i < 8; i++)
{ {
GameGenieCode += letters[num[i]]; GameGenieCode += _letters[num[i]];
} }
} }
else else
{ {
num[5] += (byte)(_value & 8); num[5] += (byte)(_value & 8);
for (int i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
{ {
GameGenieCode += letters[num[i]]; GameGenieCode += _letters[num[i]];
} }
} }

View File

@ -115,7 +115,7 @@ namespace BizHawk.Client.Common
break; break;
} }
if (str.Trim() == String.Empty) if (str.Trim() == string.Empty)
{ {
continue; continue;
} }

View File

@ -1,5 +1,4 @@
using System; using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
@ -196,11 +195,14 @@ namespace BizHawk.Client.Common
} }
} }
string GetStringForPulse(int val) private string GetStringForPulse(int val)
{ {
if (_watch.Type == Watch.DisplayType.Hex) if (_watch.Type == Watch.DisplayType.Hex)
{
return val.ToString("X8"); return val.ToString("X8");
else return val.ToString(); }
return val.ToString();
} }
public void Pulse() public void Pulse()
@ -274,6 +276,5 @@ namespace BizHawk.Client.Common
Changed(this); Changed(this);
} }
} }
} }
} }

View File

@ -273,7 +273,7 @@ namespace BizHawk.Client.Common
.Append(cheat.AddressStr).Append('\t') .Append(cheat.AddressStr).Append('\t')
.Append(cheat.ValueStr).Append('\t') .Append(cheat.ValueStr).Append('\t')
.Append(cheat.Compare.HasValue ? cheat.Compare.Value.ToString() : "N").Append('\t') .Append(cheat.Compare.HasValue ? cheat.Compare.Value.ToString() : "N").Append('\t')
.Append(cheat.Domain != null ? cheat.Domain.Name : String.Empty).Append('\t') .Append(cheat.Domain != null ? cheat.Domain.Name : string.Empty).Append('\t')
.Append(cheat.Enabled ? '1' : '0').Append('\t') .Append(cheat.Enabled ? '1' : '0').Append('\t')
.Append(cheat.Name).Append('\t') .Append(cheat.Name).Append('\t')
.Append(cheat.SizeAsChar).Append('\t') .Append(cheat.SizeAsChar).Append('\t')

View File

@ -372,26 +372,28 @@ namespace BizHawk.Client.EmuHawk
{ {
return null; return null;
} }
else if (SpecificValueRadio.Checked)
if (SpecificValueRadio.Checked)
{ {
return SpecificValueBox.ToRawInt(); return SpecificValueBox.ToRawInt();
} }
else if (SpecificAddressRadio.Checked)
if (SpecificAddressRadio.Checked)
{ {
return SpecificAddressBox.ToRawInt(); return SpecificAddressBox.ToRawInt();
} }
else if (NumberOfChangesRadio.Checked)
if (NumberOfChangesRadio.Checked)
{ {
return NumberOfChangesBox.ToRawInt(); return NumberOfChangesBox.ToRawInt();
} }
else if (DifferenceRadio.Checked)
if (DifferenceRadio.Checked)
{ {
return DifferenceBox.ToRawInt(); return DifferenceBox.ToRawInt();
} }
else
{ return null;
return null;
}
} }
} }
@ -407,13 +409,37 @@ namespace BizHawk.Client.EmuHawk
{ {
get get
{ {
if (NotEqualToRadio.Checked) return RamSearchEngine.ComparisonOperator.NotEqual; if (NotEqualToRadio.Checked)
else if (LessThanRadio.Checked) return RamSearchEngine.ComparisonOperator.LessThan; {
else if (GreaterThanRadio.Checked) return RamSearchEngine.ComparisonOperator.GreaterThan; return RamSearchEngine.ComparisonOperator.NotEqual;
else if (LessThanOrEqualToRadio.Checked) return RamSearchEngine.ComparisonOperator.LessThanEqual; }
else if (GreaterThanOrEqualToRadio.Checked) return RamSearchEngine.ComparisonOperator.GreaterThanEqual;
else if (DifferentByRadio.Checked) return RamSearchEngine.ComparisonOperator.DifferentBy; if (LessThanRadio.Checked)
else return RamSearchEngine.ComparisonOperator.Equal; {
return RamSearchEngine.ComparisonOperator.LessThan;
}
if (GreaterThanRadio.Checked)
{
return RamSearchEngine.ComparisonOperator.GreaterThan;
}
if (LessThanOrEqualToRadio.Checked)
{
return RamSearchEngine.ComparisonOperator.LessThanEqual;
}
if (GreaterThanOrEqualToRadio.Checked)
{
return RamSearchEngine.ComparisonOperator.GreaterThanEqual;
}
if (DifferentByRadio.Checked)
{
return RamSearchEngine.ComparisonOperator.DifferentBy;
}
return RamSearchEngine.ComparisonOperator.Equal;
} }
} }
@ -421,11 +447,27 @@ namespace BizHawk.Client.EmuHawk
{ {
get get
{ {
if (SpecificValueRadio.Checked) return RamSearchEngine.Compare.SpecificValue; if (SpecificValueRadio.Checked)
else if (SpecificAddressRadio.Checked) return RamSearchEngine.Compare.SpecificAddress; {
else if (NumberOfChangesRadio.Checked) return RamSearchEngine.Compare.Changes; return RamSearchEngine.Compare.SpecificValue;
else if (DifferenceRadio.Checked) return RamSearchEngine.Compare.Difference; }
else return RamSearchEngine.Compare.Previous;
if (SpecificAddressRadio.Checked)
{
return RamSearchEngine.Compare.SpecificAddress;
}
if (NumberOfChangesRadio.Checked)
{
return RamSearchEngine.Compare.Changes;
}
if (DifferenceRadio.Checked)
{
return RamSearchEngine.Compare.Difference;
}
return RamSearchEngine.Compare.Previous;
} }
} }