Broken code which converting state representation

This commit is contained in:
Lucas Scharenbroich 2016-12-05 23:35:22 -06:00
parent 2e75b8d354
commit e430006da2
2 changed files with 23 additions and 30 deletions

View File

@ -46,17 +46,15 @@
// An even-length run can be done, at best in 4 cycles/word // An even-length run can be done, at best in 4 cycles/word
// An odd-length run is even + 3 cycles/byte // An odd-length run is even + 3 cycles/byte
var count = state.Bytes.Count; if (state.IsEmpty) return 0;
if (count == 0) return 0; var offsets = state.RemainingBytes();
var offsets = state.Bytes.Select(x => x.Offset).OrderBy(x => x).ToList();
var start = offsets[0]; var start = offsets[0];
var stack = state.S.Value; var stack = state.S.Value;
var curr = start; var curr = start;
var cost = 0; var cost = 0;
for (int i = 1; i < count; i++) for (int i = 1; i < offsets.Count; i++)
{ {
var prev = curr; var prev = curr;
curr = offsets[i]; curr = offsets[i];

View File

@ -6,19 +6,13 @@
public class SpriteGeneratorState : IEquatable<SpriteGeneratorState> public class SpriteGeneratorState : IEquatable<SpriteGeneratorState>
{ {
// Single static reference to the original data set
public static List<SpriteByte> DATASET = null;
public SpriteGeneratorState() public SpriteGeneratorState()
: this(new SpriteByte[0])
{ {
} // The closed list contains all of the bytes that have been written
Closed = new HashSet<ushort>();
public SpriteGeneratorState(byte[] data)
: this(data.Select((x, i) => new SpriteByte(x, (ushort)i)))
{
}
public SpriteGeneratorState(IEnumerable<SpriteByte> bytes)
{
Bytes = bytes.ToList();
// Initialize the CPU state // Initialize the CPU state
A = Register.INITIAL_OFFSET; // the address to draw the sprite is passed in, this is a run-time value A = Register.INITIAL_OFFSET; // the address to draw the sprite is passed in, this is a run-time value
@ -33,7 +27,7 @@
private SpriteGeneratorState(SpriteGeneratorState other) private SpriteGeneratorState(SpriteGeneratorState other)
{ {
Bytes = new List<SpriteByte>(other.Bytes); Closed = new HashSet<ushort>(other.Closed);
A = other.A; A = other.A;
X = other.X; X = other.X;
Y = other.Y; Y = other.Y;
@ -49,20 +43,21 @@
public void RemoveWord(ushort offset) public void RemoveWord(ushort offset)
{ {
var total = Bytes.RemoveAll(x => x.Offset == offset || x.Offset == (offset + 1)); Closed.Add(offset);
if (total != 2) Closed.Add((ushort)(offset + 1));
{
throw new ArgumentException(string.Format("Cannot remove word at {0}", offset));
}
} }
public void RemoveByte(ushort offset) public void RemoveByte(ushort offset)
{ {
var total = Bytes.RemoveAll(x => x.Offset == offset); Closed.Add(offset);
if (total != 1) }
{
throw new ArgumentException(string.Format("Cannot remove byte at {0}", offset)); public List<ushort> RemainingBytes()
} {
return DATASET
.Select(x => x.Offset)
.Where(x => !Closed.Contains(x))
.ToList();
} }
public SpriteGeneratorState Clone(Action<SpriteGeneratorState> f = null) public SpriteGeneratorState Clone(Action<SpriteGeneratorState> f = null)
@ -81,8 +76,8 @@
// data and mask array. Then the state is just the locations and registers, rather // data and mask array. Then the state is just the locations and registers, rather
// than a full copy of the data // than a full copy of the data
public List<SpriteByte> Bytes { get; private set; } public ISet<ushort> Closed { get; private set; }
public bool IsEmpty { get { return Bytes.Count == 0; } } public bool IsEmpty { get { return Closed.Count == DATASET.Count; } }
public bool LongA { get { return (P & 0x10) == 0x10; } } public bool LongA { get { return (P & 0x10) == 0x10; } }
public bool LongI { get { return (P & 0x20) == 0x20; } } public bool LongI { get { return (P & 0x20) == 0x20; } }
@ -107,7 +102,7 @@
public bool Equals(SpriteGeneratorState other) public bool Equals(SpriteGeneratorState other)
{ {
// Two states are equal if the bytes are the same and all registers are the same // Two states are equal if the bytes are the same and all registers are the same
return Bytes.SequenceEqual(other.Bytes) && return Closed.SetEquals(other.Closed) &&
A.Equals(other.A) && A.Equals(other.A) &&
X.Equals(other.X) && X.Equals(other.X) &&
Y.Equals(other.Y) && Y.Equals(other.Y) &&