Correct some straightforward analysis issues.

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2019-07-01 00:15:25 +01:00
parent 21472154e0
commit 853b6e2b08
10 changed files with 243 additions and 228 deletions

View File

@ -10,17 +10,17 @@ namespace EightBit
{ {
} }
public static byte SetFlag(byte input, byte flag) => (byte)(input | flag); public static byte SetBit(byte input, byte which) => (byte)(input | which);
public static byte SetFlag(byte input, byte flag, int condition) => SetFlag(input, flag, condition != 0); public static byte SetBit(byte input, byte which, int condition) => SetBit(input, which, condition != 0);
public static byte SetFlag(byte input, byte flag, bool condition) => condition ? SetFlag(input, flag) : ClearFlag(input, flag); public static byte SetBit(byte input, byte which, bool condition) => condition ? SetBit(input, which) : ClearBit(input, which);
public static byte ClearFlag(byte input, byte flag) => (byte)(input & (byte)~flag); public static byte ClearBit(byte input, byte which) => (byte)(input & (byte)~which);
public static byte ClearFlag(byte input, byte flag, int condition) => ClearFlag(input, flag, condition != 0); public static byte ClearBit(byte input, byte which, int condition) => ClearBit(input, which, condition != 0);
public static byte ClearFlag(byte input, byte flag, bool condition) => SetFlag(input, flag, !condition); public static byte ClearBit(byte input, byte which, bool condition) => SetBit(input, which, !condition);
public static byte HighByte(int value) => (byte)(value >> 8); public static byte HighByte(int value) => (byte)(value >> 8);

View File

@ -26,82 +26,82 @@ namespace EightBit
} }
[TestMethod] [TestMethod]
public void TestClearFlag() public void TestClearBit()
{ {
byte flags = 0xff; byte flags = 0xff;
flags = Chip.ClearFlag(flags, 0x80); flags = Chip.ClearBit(flags, 0x80);
Assert.AreEqual(0x7f, flags); Assert.AreEqual(0x7f, flags);
} }
[TestMethod] [TestMethod]
public void TestClearFlagNonZero() public void TestClearBitNonZero()
{ {
byte flags = 0xff; byte flags = 0xff;
flags = Chip.ClearFlag(flags, 0x80, 1); flags = Chip.ClearBit(flags, 0x80, 1);
Assert.AreEqual(0x7f, flags); Assert.AreEqual(0x7f, flags);
} }
[TestMethod] [TestMethod]
public void TestClearFlagZero() public void TestClearBitZero()
{ {
byte flags = 0xff; byte flags = 0xff;
flags = Chip.ClearFlag(flags, 0x80, 0); flags = Chip.ClearBit(flags, 0x80, 0);
Assert.AreEqual(0xff, flags); Assert.AreEqual(0xff, flags);
} }
[TestMethod] [TestMethod]
public void TestClearFlagFalse() public void TestClearBitFalse()
{ {
byte flags = 0xff; byte flags = 0xff;
flags = Chip.ClearFlag(flags, 0x80, false); flags = Chip.ClearBit(flags, 0x80, false);
Assert.AreEqual(0xff, flags); Assert.AreEqual(0xff, flags);
} }
[TestMethod] [TestMethod]
public void TestClearFlagTrue() public void TestClearBitTrue()
{ {
byte flags = 0xff; byte flags = 0xff;
flags = Chip.ClearFlag(flags, 0x80, true); flags = Chip.ClearBit(flags, 0x80, true);
Assert.AreEqual(0x7f, flags); Assert.AreEqual(0x7f, flags);
} }
[TestMethod] [TestMethod]
public void TestSetFlag() public void TestSetBit()
{ {
byte flags = 0x7f; byte flags = 0x7f;
flags = Chip.SetFlag(flags, 0x80); flags = Chip.SetBit(flags, 0x80);
Assert.AreEqual(0xff, flags); Assert.AreEqual(0xff, flags);
} }
[TestMethod] [TestMethod]
public void TestSetFlagNonZero() public void TestSetBitNonZero()
{ {
byte flags = 0x7f; byte flags = 0x7f;
flags = Chip.SetFlag(flags, 0x80, 1); flags = Chip.SetBit(flags, 0x80, 1);
Assert.AreEqual(0xff, flags); Assert.AreEqual(0xff, flags);
} }
[TestMethod] [TestMethod]
public void TestSetFlagZero() public void TestSetBitZero()
{ {
byte flags = 0x7f; byte flags = 0x7f;
flags = Chip.SetFlag(flags, 0x80, 0); flags = Chip.SetBit(flags, 0x80, 0);
Assert.AreEqual(0x7f, flags); Assert.AreEqual(0x7f, flags);
} }
[TestMethod] [TestMethod]
public void TestSetFlagFalse() public void TestSetBitFalse()
{ {
byte flags = 0x7f; byte flags = 0x7f;
flags = Chip.SetFlag(flags, 0x80, false); flags = Chip.SetBit(flags, 0x80, false);
Assert.AreEqual(0x7f, flags); Assert.AreEqual(0x7f, flags);
} }
[TestMethod] [TestMethod]
public void TestSetFlagTrue() public void TestSetBitTrue()
{ {
byte flags = 0x7f; byte flags = 0x7f;
flags = Chip.SetFlag(flags, 0x80, true); flags = Chip.SetBit(flags, 0x80, true);
Assert.AreEqual(0xff, flags); Assert.AreEqual(0xff, flags);
} }

View File

@ -11,6 +11,7 @@ namespace EightBit
public class IntelHexFile : IDisposable public class IntelHexFile : IDisposable
{ {
private readonly StreamReader reader; private readonly StreamReader reader;
private bool eof;
private bool disposed = false; private bool disposed = false;
public IntelHexFile(string path) => this.reader = File.OpenText(path); public IntelHexFile(string path) => this.reader = File.OpenText(path);
@ -36,25 +37,33 @@ namespace EightBit
public IEnumerable<Tuple<ushort, byte[]>> Parse() public IEnumerable<Tuple<ushort, byte[]>> Parse()
{ {
var eof = false; this.eof = false;
while (!this.reader.EndOfStream && !eof) while (!this.reader.EndOfStream && !this.eof)
{ {
var line = this.reader.ReadLine(); var line = this.reader.ReadLine();
var parsed = this.Parse(line); var parsed = this.Parse(line);
eof = parsed == null; if (parsed != null)
if (!eof)
{ {
yield return parsed; yield return parsed;
} }
} }
if (!this.eof)
{
throw new InvalidOperationException("File is missing an EOF record");
}
} }
private Tuple<ushort, byte[]> Parse(string line) private Tuple<ushort, byte[]> Parse(string line)
{ {
if (string.IsNullOrEmpty(line))
{
throw new ArgumentNullException(nameof(line));
}
var colon = line.Substring(0, 1); var colon = line.Substring(0, 1);
if (colon != ":") if (colon != ":")
{ {
throw new System.InvalidOperationException("Invalid hex file: line does not begin with a colon"); throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line does not begin with a colon");
} }
var countString = line.Substring(1, 2); var countString = line.Substring(1, 2);
@ -72,6 +81,7 @@ namespace EightBit
return ParseDataRecord(line, address, count); return ParseDataRecord(line, address, count);
case 0x01: case 0x01:
this.eof = true;
return null; return null;
default: default:
@ -81,18 +91,23 @@ namespace EightBit
private static Tuple<ushort, byte[]> ParseDataRecord(string line, ushort address, byte count) private static Tuple<ushort, byte[]> ParseDataRecord(string line, ushort address, byte count)
{ {
var data = new byte[count]; if (string.IsNullOrEmpty(line))
{
throw new ArgumentNullException(nameof(line));
}
var requiredLength = 9 + 2 + (count * 2); var requiredLength = 9 + 2 + (count * 2);
if (line.Length != requiredLength) if (line.Length != requiredLength)
{ {
throw new InvalidOperationException("Invalid hex file: line is not the required length"); throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line is not the required length");
} }
var data = new byte[count];
for (var i = 0; i < count; ++i) for (var i = 0; i < count; ++i)
{ {
var position = 9 + (i * 2); var position = 9 + (i * 2);
var datumString = line.Substring(position, 2); var extracted = line.Substring(position, 2);
data[i] = Convert.ToByte(datumString, 16); data[i] = Convert.ToByte(extracted, 16);
} }
return new Tuple<ushort, byte[]>(address, data); return new Tuple<ushort, byte[]>(address, data);

View File

@ -52,7 +52,7 @@ namespace EightBit
public ushort Word public ushort Word
{ {
get => (ushort)(this.Low | Chip.PromoteByte(this.High)); get => Chip.MakeWord(this.Low, this.High);
set set
{ {

View File

@ -104,21 +104,21 @@ namespace EightBit
} }
} }
private static byte SetFlag(byte f, StatusBits flag) => SetFlag(f, (byte)flag); private static byte SetBit(byte f, StatusBits flag) => SetBit(f, (byte)flag);
private static byte SetFlag(byte f, StatusBits flag, int condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, int condition) => SetBit(f, (byte)flag, condition);
private static byte SetFlag(byte f, StatusBits flag, bool condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, bool condition) => SetBit(f, (byte)flag, condition);
private static byte ClearFlag(byte f, StatusBits flag) => ClearFlag(f, (byte)flag); private static byte ClearBit(byte f, StatusBits flag) => ClearBit(f, (byte)flag);
private static byte ClearFlag(byte f, StatusBits flag, int condition) => ClearFlag(f, (byte)flag, condition); private static byte ClearBit(byte f, StatusBits flag, int condition) => ClearBit(f, (byte)flag, condition);
private static byte AdjustSign(byte input, byte value) => SetFlag(input, StatusBits.SF, value & (byte)StatusBits.SF); private static byte AdjustSign(byte input, byte value) => SetBit(input, StatusBits.SF, value & (byte)StatusBits.SF);
private static byte AdjustZero(byte input, byte value) => ClearFlag(input, StatusBits.ZF, value); private static byte AdjustZero(byte input, byte value) => ClearBit(input, StatusBits.ZF, value);
private static byte AdjustParity(byte input, byte value) => SetFlag(input, StatusBits.PF, EvenParity(value)); private static byte AdjustParity(byte input, byte value) => SetBit(input, StatusBits.PF, EvenParity(value));
private static byte AdjustSZ(byte input, byte value) private static byte AdjustSZ(byte input, byte value)
{ {
@ -132,9 +132,9 @@ namespace EightBit
return AdjustParity(input, value); return AdjustParity(input, value);
} }
private static byte AdjustAuxiliaryCarryAdd(byte input, byte before, byte value, int calculation) => SetFlag(input, StatusBits.AC, CalculateHalfCarryAdd(before, value, calculation)); private static byte AdjustAuxiliaryCarryAdd(byte input, byte before, byte value, int calculation) => SetBit(input, StatusBits.AC, CalculateHalfCarryAdd(before, value, calculation));
private static byte AdjustAuxiliaryCarrySub(byte input, byte before, byte value, int calculation) => ClearFlag(input, StatusBits.AC, CalculateHalfCarrySub(before, value, calculation)); private static byte AdjustAuxiliaryCarrySub(byte input, byte before, byte value, int calculation) => ClearBit(input, StatusBits.AC, CalculateHalfCarrySub(before, value, calculation));
private void DisableInterrupts() => this.interruptEnable = false; private void DisableInterrupts() => this.interruptEnable = false;
@ -610,14 +610,14 @@ namespace EightBit
private byte Increment(byte operand) private byte Increment(byte operand)
{ {
this.F = AdjustSZP(this.F, ++operand); this.F = AdjustSZP(this.F, ++operand);
this.F = ClearFlag(this.F, StatusBits.AC, LowNibble(operand)); this.F = ClearBit(this.F, StatusBits.AC, LowNibble(operand));
return operand; return operand;
} }
private byte Decrement(byte operand) private byte Decrement(byte operand)
{ {
this.F = AdjustSZP(this.F, --operand); this.F = AdjustSZP(this.F, --operand);
this.F = SetFlag(this.F, StatusBits.AC, LowNibble(operand) != (byte)Mask.Mask4); this.F = SetBit(this.F, StatusBits.AC, LowNibble(operand) != (byte)Mask.Mask4);
return operand; return operand;
} }
@ -700,7 +700,7 @@ namespace EightBit
{ {
var result = this.HL.Word + value.Word; var result = this.HL.Word + value.Word;
this.HL.Word = (ushort)result; this.HL.Word = (ushort)result;
this.F = SetFlag(this.F, StatusBits.CF, result & (int)Bits.Bit16); this.F = SetBit(this.F, StatusBits.CF, result & (int)Bits.Bit16);
} }
private void Add(byte value, int carry = 0) private void Add(byte value, int carry = 0)
@ -711,7 +711,7 @@ namespace EightBit
this.A = this.intermediate.Low; this.A = this.intermediate.Low;
this.F = SetFlag(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF); this.F = SetBit(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF);
this.F = AdjustSZP(this.F, this.A); this.F = AdjustSZP(this.F, this.A);
} }
@ -725,7 +725,7 @@ namespace EightBit
var result = this.intermediate.Low; var result = this.intermediate.Low;
this.F = SetFlag(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF); this.F = SetBit(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF);
this.F = AdjustSZP(this.F, result); this.F = AdjustSZP(this.F, result);
return result; return result;
@ -737,20 +737,20 @@ namespace EightBit
private void AndR(byte value) private void AndR(byte value)
{ {
this.F = SetFlag(this.F, StatusBits.AC, (this.A | value) & (int)Bits.Bit3); this.F = SetBit(this.F, StatusBits.AC, (this.A | value) & (int)Bits.Bit3);
this.F = ClearFlag(this.F, StatusBits.CF); this.F = ClearBit(this.F, StatusBits.CF);
this.F = AdjustSZP(this.F, this.A &= value); this.F = AdjustSZP(this.F, this.A &= value);
} }
private void XorR(byte value) private void XorR(byte value)
{ {
this.F = ClearFlag(this.F, StatusBits.AC | StatusBits.CF); this.F = ClearBit(this.F, StatusBits.AC | StatusBits.CF);
this.F = AdjustSZP(this.F, this.A ^= value); this.F = AdjustSZP(this.F, this.A ^= value);
} }
private void OrR(byte value) private void OrR(byte value)
{ {
this.F = ClearFlag(this.F, StatusBits.AC | StatusBits.CF); this.F = ClearBit(this.F, StatusBits.AC | StatusBits.CF);
this.F = AdjustSZP(this.F, this.A |= value); this.F = AdjustSZP(this.F, this.A |= value);
} }
@ -759,28 +759,28 @@ namespace EightBit
private byte RLC(byte operand) private byte RLC(byte operand)
{ {
var carry = operand & (byte)Bits.Bit7; var carry = operand & (byte)Bits.Bit7;
this.F = SetFlag(this.F, StatusBits.CF, carry); this.F = SetBit(this.F, StatusBits.CF, carry);
return (byte)((operand << 1) | (carry >> 7)); return (byte)((operand << 1) | (carry >> 7));
} }
private byte RRC(byte operand) private byte RRC(byte operand)
{ {
var carry = operand & (byte)Bits.Bit0; var carry = operand & (byte)Bits.Bit0;
this.F = SetFlag(this.F, StatusBits.CF, carry); this.F = SetBit(this.F, StatusBits.CF, carry);
return (byte)((operand >> 1) | (carry << 7)); return (byte)((operand >> 1) | (carry << 7));
} }
private byte RL(byte operand) private byte RL(byte operand)
{ {
var carry = this.F & (byte)StatusBits.CF; var carry = this.F & (byte)StatusBits.CF;
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit7);
return (byte)((operand << 1) | carry); return (byte)((operand << 1) | carry);
} }
private byte RR(byte operand) private byte RR(byte operand)
{ {
var carry = this.F & (byte)StatusBits.CF; var carry = this.F & (byte)StatusBits.CF;
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit0); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit0);
return (byte)((operand >> 1) | (carry << 7)); return (byte)((operand >> 1) | (carry << 7));
} }
@ -801,14 +801,14 @@ namespace EightBit
} }
this.Add(addition); this.Add(addition);
this.F = SetFlag(this.F, StatusBits.CF, carry); this.F = SetBit(this.F, StatusBits.CF, carry);
} }
private void CMA() => this.A = (byte)~this.A; private void CMA() => this.A = (byte)~this.A;
private void STC() => this.F = SetFlag(this.F, StatusBits.CF); private void STC() => this.F = SetBit(this.F, StatusBits.CF);
private void CMC() => this.F = ClearFlag(this.F, StatusBits.CF, this.F & (byte)StatusBits.CF); private void CMC() => this.F = ClearBit(this.F, StatusBits.CF, this.F & (byte)StatusBits.CF);
private void XHTL() private void XHTL()
{ {

View File

@ -179,7 +179,7 @@ namespace EightBit
case 0x15: this.A = this.OrR(this.A, this.AM_ZeroPageX()); break; // ORA (zero page, X) case 0x15: this.A = this.OrR(this.A, this.AM_ZeroPageX()); break; // ORA (zero page, X)
case 0x16: this.BusReadModifyWrite(this.ASL(this.AM_ZeroPageX())); break; // ASL (zero page, X) case 0x16: this.BusReadModifyWrite(this.ASL(this.AM_ZeroPageX())); break; // ASL (zero page, X)
case 0x17: this.SLO(this.AM_ZeroPageX()); break; // *SLO (zero page, X) case 0x17: this.SLO(this.AM_ZeroPageX()); break; // *SLO (zero page, X)
case 0x18: this.BusRead(); this.P = ClearFlag(this.P, StatusBits.CF); break; // CLC (implied) case 0x18: this.BusRead(); this.P = ClearBit(this.P, StatusBits.CF); break; // CLC (implied)
case 0x19: this.A = this.OrR(this.A, this.AM_AbsoluteY()); break; // ORA (absolute, Y) case 0x19: this.A = this.OrR(this.A, this.AM_AbsoluteY()); break; // ORA (absolute, Y)
case 0x1a: this.BusRead(); break; // *NOP (implied) case 0x1a: this.BusRead(); break; // *NOP (implied)
case 0x1b: this.SLO(this.AM_AbsoluteY()); break; // *SLO (absolute, Y) case 0x1b: this.SLO(this.AM_AbsoluteY()); break; // *SLO (absolute, Y)
@ -213,7 +213,7 @@ namespace EightBit
case 0x35: this.A = this.AndR(this.A, this.AM_ZeroPageX()); break; // AND (zero page, X) case 0x35: this.A = this.AndR(this.A, this.AM_ZeroPageX()); break; // AND (zero page, X)
case 0x36: this.BusReadModifyWrite(this.ROL(this.AM_ZeroPageX())); break; // ROL (zero page, X) case 0x36: this.BusReadModifyWrite(this.ROL(this.AM_ZeroPageX())); break; // ROL (zero page, X)
case 0x37: this.RLA(this.AM_ZeroPageX()); break; // *RLA (zero page, X) case 0x37: this.RLA(this.AM_ZeroPageX()); break; // *RLA (zero page, X)
case 0x38: this.BusRead(); this.P = SetFlag(this.P, StatusBits.CF); break; // SEC (implied) case 0x38: this.BusRead(); this.P = SetBit(this.P, StatusBits.CF); break; // SEC (implied)
case 0x39: this.A = this.AndR(this.A, this.AM_AbsoluteY()); break; // AND (absolute, Y) case 0x39: this.A = this.AndR(this.A, this.AM_AbsoluteY()); break; // AND (absolute, Y)
case 0x3a: this.BusRead(); break; // *NOP (implied) case 0x3a: this.BusRead(); break; // *NOP (implied)
case 0x3b: this.RLA(this.AM_AbsoluteY()); break; // *RLA (absolute, Y) case 0x3b: this.RLA(this.AM_AbsoluteY()); break; // *RLA (absolute, Y)
@ -247,7 +247,7 @@ namespace EightBit
case 0x55: this.A = this.EorR(this.A, this.AM_ZeroPageX()); break; // EOR (zero page, X) case 0x55: this.A = this.EorR(this.A, this.AM_ZeroPageX()); break; // EOR (zero page, X)
case 0x56: this.BusReadModifyWrite(this.LSR(this.AM_ZeroPageX())); break; // LSR (zero page, X) case 0x56: this.BusReadModifyWrite(this.LSR(this.AM_ZeroPageX())); break; // LSR (zero page, X)
case 0x57: this.SRE(this.AM_ZeroPageX()); break; // *SRE (zero page, X) case 0x57: this.SRE(this.AM_ZeroPageX()); break; // *SRE (zero page, X)
case 0x58: this.BusRead(); this.P = ClearFlag(this.P, StatusBits.IF); break; // CLI (implied) case 0x58: this.BusRead(); this.P = ClearBit(this.P, StatusBits.IF); break; // CLI (implied)
case 0x59: this.A = this.EorR(this.A, this.AM_AbsoluteY()); break; // EOR (absolute, Y) case 0x59: this.A = this.EorR(this.A, this.AM_AbsoluteY()); break; // EOR (absolute, Y)
case 0x5a: this.BusRead(); break; // *NOP (implied) case 0x5a: this.BusRead(); break; // *NOP (implied)
case 0x5b: this.SRE(this.AM_AbsoluteY()); break; // *SRE (absolute, Y) case 0x5b: this.SRE(this.AM_AbsoluteY()); break; // *SRE (absolute, Y)
@ -281,7 +281,7 @@ namespace EightBit
case 0x75: this.A = this.ADC(this.A, this.AM_ZeroPageX()); break; // ADC (zero page, X) case 0x75: this.A = this.ADC(this.A, this.AM_ZeroPageX()); break; // ADC (zero page, X)
case 0x76: this.BusReadModifyWrite(this.ROR(this.AM_ZeroPageX())); break; // ROR (zero page, X) case 0x76: this.BusReadModifyWrite(this.ROR(this.AM_ZeroPageX())); break; // ROR (zero page, X)
case 0x77: this.RRA(this.AM_ZeroPageX()); break; // *RRA (zero page, X) case 0x77: this.RRA(this.AM_ZeroPageX()); break; // *RRA (zero page, X)
case 0x78: this.BusRead(); this.P = SetFlag(this.P, StatusBits.IF); break; // SEI (implied) case 0x78: this.BusRead(); this.P = SetBit(this.P, StatusBits.IF); break; // SEI (implied)
case 0x79: this.A = this.ADC(this.A, this.AM_AbsoluteY()); break; // ADC (absolute, Y) case 0x79: this.A = this.ADC(this.A, this.AM_AbsoluteY()); break; // ADC (absolute, Y)
case 0x7a: this.BusRead(); break; // *NOP (implied) case 0x7a: this.BusRead(); break; // *NOP (implied)
case 0x7b: this.RRA(this.AM_AbsoluteY()); break; // *RRA (absolute, Y) case 0x7b: this.RRA(this.AM_AbsoluteY()); break; // *RRA (absolute, Y)
@ -349,7 +349,7 @@ namespace EightBit
case 0xb5: this.A = this.Through(this.AM_ZeroPageX()); break; // LDA (zero page, X) case 0xb5: this.A = this.Through(this.AM_ZeroPageX()); break; // LDA (zero page, X)
case 0xb6: this.X = this.Through(this.AM_ZeroPageY()); break; // LDX (zero page, Y) case 0xb6: this.X = this.Through(this.AM_ZeroPageY()); break; // LDX (zero page, Y)
case 0xb7: this.A = this.X = this.Through(this.AM_ZeroPageY()); break; // *LAX (zero page, Y) case 0xb7: this.A = this.X = this.Through(this.AM_ZeroPageY()); break; // *LAX (zero page, Y)
case 0xb8: this.BusRead(); this.P = ClearFlag(this.P, StatusBits.VF); break; // CLV (implied) case 0xb8: this.BusRead(); this.P = ClearBit(this.P, StatusBits.VF); break; // CLV (implied)
case 0xb9: this.A = this.Through(this.AM_AbsoluteY()); break; // LDA (absolute, Y) case 0xb9: this.A = this.Through(this.AM_AbsoluteY()); break; // LDA (absolute, Y)
case 0xba: this.BusRead(); this.X = this.Through(this.S); break; // TSX (implied) case 0xba: this.BusRead(); this.X = this.Through(this.S); break; // TSX (implied)
case 0xbb: break; case 0xbb: break;
@ -383,7 +383,7 @@ namespace EightBit
case 0xd5: this.CMP(this.A, this.AM_ZeroPageX()); break; // CMP (zero page, X) case 0xd5: this.CMP(this.A, this.AM_ZeroPageX()); break; // CMP (zero page, X)
case 0xd6: this.BusReadModifyWrite(this.DEC(this.AM_ZeroPageX())); break; // DEC (zero page, X) case 0xd6: this.BusReadModifyWrite(this.DEC(this.AM_ZeroPageX())); break; // DEC (zero page, X)
case 0xd7: this.DCP(this.AM_ZeroPageX()); break; // *DCP (zero page, X) case 0xd7: this.DCP(this.AM_ZeroPageX()); break; // *DCP (zero page, X)
case 0xd8: this.BusRead(); this.P = ClearFlag(this.P, StatusBits.DF); break; // CLD (implied) case 0xd8: this.BusRead(); this.P = ClearBit(this.P, StatusBits.DF); break; // CLD (implied)
case 0xd9: this.CMP(this.A, this.AM_AbsoluteY()); break; // CMP (absolute, Y) case 0xd9: this.CMP(this.A, this.AM_AbsoluteY()); break; // CMP (absolute, Y)
case 0xda: this.BusRead(); break; // *NOP (implied) case 0xda: this.BusRead(); break; // *NOP (implied)
case 0xdb: this.DCP(this.AM_AbsoluteY()); break; // *DCP (absolute, Y) case 0xdb: this.DCP(this.AM_AbsoluteY()); break; // *DCP (absolute, Y)
@ -416,7 +416,7 @@ namespace EightBit
case 0xf5: this.A = this.SBC(this.A, this.AM_ZeroPageX()); break; // SBC (zero page, X) case 0xf5: this.A = this.SBC(this.A, this.AM_ZeroPageX()); break; // SBC (zero page, X)
case 0xf6: this.BusReadModifyWrite(this.INC(this.AM_ZeroPageX())); break; // INC (zero page, X) case 0xf6: this.BusReadModifyWrite(this.INC(this.AM_ZeroPageX())); break; // INC (zero page, X)
case 0xf7: this.ISB(this.AM_ZeroPageX()); break; // *ISB (zero page, X) case 0xf7: this.ISB(this.AM_ZeroPageX()); break; // *ISB (zero page, X)
case 0xf8: this.BusRead(); this.P = SetFlag(this.P, StatusBits.DF); break; // SED (implied) case 0xf8: this.BusRead(); this.P = SetBit(this.P, StatusBits.DF); break; // SED (implied)
case 0xf9: this.A = this.SBC(this.A, this.AM_AbsoluteY()); break; // SBC (absolute, Y) case 0xf9: this.A = this.SBC(this.A, this.AM_AbsoluteY()); break; // SBC (absolute, Y)
case 0xfa: this.BusRead(); break; // *NOP (implied) case 0xfa: this.BusRead(); break; // *NOP (implied)
case 0xfb: this.ISB(this.AM_AbsoluteY()); break; // *ISB (absolute, Y) case 0xfb: this.ISB(this.AM_AbsoluteY()); break; // *ISB (absolute, Y)
@ -546,15 +546,15 @@ namespace EightBit
return base.BusRead(); return base.BusRead();
} }
private static byte SetFlag(byte f, StatusBits flag) => SetFlag(f, (byte)flag); private static byte SetBit(byte f, StatusBits flag) => SetBit(f, (byte)flag);
private static byte SetFlag(byte f, StatusBits flag, int condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, int condition) => SetBit(f, (byte)flag, condition);
private static byte SetFlag(byte f, StatusBits flag, bool condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, bool condition) => SetBit(f, (byte)flag, condition);
private static byte ClearFlag(byte f, StatusBits flag) => ClearFlag(f, (byte)flag); private static byte ClearBit(byte f, StatusBits flag) => ClearBit(f, (byte)flag);
private static byte ClearFlag(byte f, StatusBits flag, int condition) => ClearFlag(f, (byte)flag, condition); private static byte ClearBit(byte f, StatusBits flag, int condition) => ClearBit(f, (byte)flag, condition);
private void HandleNMI() private void HandleNMI()
{ {
@ -588,7 +588,7 @@ namespace EightBit
this.Push((byte)(this.P | (int)(software ? StatusBits.BF : 0))); this.Push((byte)(this.P | (int)(software ? StatusBits.BF : 0)));
} }
this.P = SetFlag(this.P, StatusBits.IF); // Disable IRQ this.P = SetBit(this.P, StatusBits.IF); // Disable IRQ
var vector = reset ? RSTvector : (nmi ? NMIvector : IRQvector); var vector = reset ? RSTvector : (nmi ? NMIvector : IRQvector);
this.Jump(this.GetWordPaged(0xff, vector).Word); this.Jump(this.GetWordPaged(0xff, vector).Word);
this.handlingRESET = this.handlingNMI = this.handlingINT = false; this.handlingRESET = this.handlingNMI = this.handlingINT = false;
@ -709,9 +709,9 @@ namespace EightBit
return possible; return possible;
} }
private void AdjustZero(byte datum) => this.P = ClearFlag(this.P, StatusBits.ZF, datum); private void AdjustZero(byte datum) => this.P = ClearBit(this.P, StatusBits.ZF, datum);
private void AdjustNegative(byte datum) => this.P = SetFlag(this.P, StatusBits.NF, datum & (byte)StatusBits.NF); private void AdjustNegative(byte datum) => this.P = SetBit(this.P, StatusBits.NF, datum & (byte)StatusBits.NF);
private void AdjustNZ(byte datum) private void AdjustNZ(byte datum)
{ {
@ -756,8 +756,8 @@ namespace EightBit
var returned = this.SUB(operand, data, ~this.P & (int)StatusBits.CF); var returned = this.SUB(operand, data, ~this.P & (int)StatusBits.CF);
this.AdjustNZ(this.intermediate.Low); this.AdjustNZ(this.intermediate.Low);
this.P = SetFlag(this.P, StatusBits.VF, (operand ^ data) & (operand ^ this.intermediate.Low) & (int)StatusBits.NF); this.P = SetBit(this.P, StatusBits.VF, (operand ^ data) & (operand ^ this.intermediate.Low) & (int)StatusBits.NF);
this.P = ClearFlag(this.P, StatusBits.CF, this.intermediate.High); this.P = ClearBit(this.P, StatusBits.CF, this.intermediate.High);
return returned; return returned;
} }
@ -804,8 +804,8 @@ namespace EightBit
{ {
this.intermediate.Word = (ushort)(operand + data + carry); this.intermediate.Word = (ushort)(operand + data + carry);
this.P = SetFlag(this.P, StatusBits.VF, ~(operand ^ data) & (operand ^ this.intermediate.Low) & (int)StatusBits.NF); this.P = SetBit(this.P, StatusBits.VF, ~(operand ^ data) & (operand ^ this.intermediate.Low) & (int)StatusBits.NF);
this.P = SetFlag(this.P, StatusBits.CF, this.intermediate.High & (int)StatusBits.CF); this.P = SetBit(this.P, StatusBits.CF, this.intermediate.High & (int)StatusBits.CF);
return this.intermediate.Low; return this.intermediate.Low;
} }
@ -821,14 +821,14 @@ namespace EightBit
} }
var high = (byte)(HighNibble(operand) + HighNibble(data) + (low > 0xf ? 1 : 0)); var high = (byte)(HighNibble(operand) + HighNibble(data) + (low > 0xf ? 1 : 0));
this.P = SetFlag(this.P, StatusBits.VF, ~(operand ^ data) & (operand ^ Chip.PromoteNibble(high)) & (int)StatusBits.NF); this.P = SetBit(this.P, StatusBits.VF, ~(operand ^ data) & (operand ^ Chip.PromoteNibble(high)) & (int)StatusBits.NF);
if (high > 9) if (high > 9)
{ {
high += 6; high += 6;
} }
this.P = SetFlag(this.P, StatusBits.CF, high > 0xf); this.P = SetBit(this.P, StatusBits.CF, high > 0xf);
return (byte)(PromoteNibble(high) | LowNibble(low)); return (byte)(PromoteNibble(high) | LowNibble(low));
} }
@ -837,13 +837,13 @@ namespace EightBit
private byte ASL(byte value) private byte ASL(byte value)
{ {
this.P = SetFlag(this.P, StatusBits.CF, value & (byte)Bits.Bit7); this.P = SetBit(this.P, StatusBits.CF, value & (byte)Bits.Bit7);
return this.Through(value << 1); return this.Through(value << 1);
} }
private void BIT(byte operand, byte data) private void BIT(byte operand, byte data)
{ {
this.P = SetFlag(this.P, StatusBits.VF, data & (byte)StatusBits.VF); this.P = SetBit(this.P, StatusBits.VF, data & (byte)StatusBits.VF);
this.AdjustZero((byte)(operand & data)); this.AdjustZero((byte)(operand & data));
this.AdjustNegative(data); this.AdjustNegative(data);
} }
@ -852,7 +852,7 @@ namespace EightBit
{ {
this.intermediate.Word = (ushort)(first - second); this.intermediate.Word = (ushort)(first - second);
this.AdjustNZ(this.intermediate.Low); this.AdjustNZ(this.intermediate.Low);
this.P = ClearFlag(this.P, StatusBits.CF, this.intermediate.High); this.P = ClearBit(this.P, StatusBits.CF, this.intermediate.High);
} }
private byte DEC(byte value) => this.Through(--value); private byte DEC(byte value) => this.Through(--value);
@ -873,7 +873,7 @@ namespace EightBit
private byte LSR(byte value) private byte LSR(byte value)
{ {
this.P = SetFlag(this.P, StatusBits.CF, value & (byte)Bits.Bit0); this.P = SetBit(this.P, StatusBits.CF, value & (byte)Bits.Bit0);
return this.Through(value >> 1); return this.Through(value >> 1);
} }
@ -886,7 +886,7 @@ namespace EightBit
private byte ROL(byte operand) private byte ROL(byte operand)
{ {
var carryIn = this.Carry; var carryIn = this.Carry;
this.P = SetFlag(this.P, StatusBits.CF, operand & (byte)Bits.Bit7); this.P = SetBit(this.P, StatusBits.CF, operand & (byte)Bits.Bit7);
var result = (operand << 1) | carryIn; var result = (operand << 1) | carryIn;
return this.Through(result); return this.Through(result);
} }
@ -894,7 +894,7 @@ namespace EightBit
private byte ROR(byte operand) private byte ROR(byte operand)
{ {
var carryIn = this.Carry; var carryIn = this.Carry;
this.P = SetFlag(this.P, StatusBits.CF, operand & (byte)Bits.Bit0); this.P = SetBit(this.P, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (operand >> 1) | (carryIn << 7); var result = (operand >> 1) | (carryIn << 7);
return this.Through(result); return this.Through(result);
} }
@ -916,15 +916,15 @@ namespace EightBit
private void ANC(byte value) private void ANC(byte value)
{ {
this.A = this.AndR(this.A, value); this.A = this.AndR(this.A, value);
this.P = SetFlag(this.P, StatusBits.CF, this.A & (byte)Bits.Bit7); this.P = SetBit(this.P, StatusBits.CF, this.A & (byte)Bits.Bit7);
} }
private void ARR(byte value) private void ARR(byte value)
{ {
this.A = this.AndR(this.A, value); this.A = this.AndR(this.A, value);
this.A = this.ROR(this.A); this.A = this.ROR(this.A);
this.P = SetFlag(this.P, StatusBits.CF, this.A & (byte)Bits.Bit6); this.P = SetBit(this.P, StatusBits.CF, this.A & (byte)Bits.Bit6);
this.P = SetFlag(this.P, StatusBits.VF, ((this.A & (byte)Bits.Bit6) >> 6) ^ ((this.A & (byte)Bits.Bit5) >> 5)); this.P = SetBit(this.P, StatusBits.VF, ((this.A & (byte)Bits.Bit6) >> 6) ^ ((this.A & (byte)Bits.Bit5) >> 5));
} }
private void ASR(byte value) private void ASR(byte value)
@ -936,7 +936,7 @@ namespace EightBit
private void AXS(byte value) private void AXS(byte value)
{ {
this.X = this.Through(this.SUB((byte)(this.A & this.X), value)); this.X = this.Through(this.SUB((byte)(this.A & this.X), value));
this.P = ClearFlag(this.P, StatusBits.CF, this.intermediate.High); this.P = ClearBit(this.P, StatusBits.CF, this.intermediate.High);
} }
private void DCP(byte value) private void DCP(byte value)

View File

@ -29,7 +29,7 @@ namespace EightBit
{ {
this.board.Poke(0, 0x89); this.board.Poke(0, 0x89);
this.board.Poke(1, 0x7c); this.board.Poke(1, 0x7c);
this.cpu.CC = EightBit.Chip.SetFlag(this.cpu.CC, (byte)StatusBits.CF); this.cpu.CC = EightBit.Chip.SetBit(this.cpu.CC, (byte)StatusBits.CF);
this.cpu.A = 0x3a; this.cpu.A = 0x3a;
this.cpu.Step(); this.cpu.Step();
Assert.AreEqual(0xb7, this.cpu.A); Assert.AreEqual(0xb7, this.cpu.A);

View File

@ -305,8 +305,8 @@
this.LowerBA(); this.LowerBA();
this.RaiseBS(); this.RaiseBS();
this.DP = 0; this.DP = 0;
this.CC = SetFlag(this.CC, StatusBits.IF); // Disable IRQ this.CC = SetBit(this.CC, StatusBits.IF); // Disable IRQ
this.CC = SetFlag(this.CC, StatusBits.FF); // Disable FIRQ this.CC = SetBit(this.CC, StatusBits.FF); // Disable FIRQ
this.Jump(this.GetWordPaged(0xff, RESETvector)); this.Jump(this.GetWordPaged(0xff, RESETvector));
this.Tick(10); this.Tick(10);
} }
@ -317,7 +317,7 @@
this.LowerBA(); this.LowerBA();
this.RaiseBS(); this.RaiseBS();
this.SaveEntireRegisterState(); this.SaveEntireRegisterState();
this.CC = SetFlag(this.CC, StatusBits.IF); // Disable IRQ this.CC = SetBit(this.CC, StatusBits.IF); // Disable IRQ
this.Jump(this.GetWordPaged(0xff, IRQvector)); this.Jump(this.GetWordPaged(0xff, IRQvector));
this.Tick(12); this.Tick(12);
} }
@ -338,8 +338,8 @@
this.LowerBA(); this.LowerBA();
this.RaiseBS(); this.RaiseBS();
this.SaveEntireRegisterState(); this.SaveEntireRegisterState();
this.CC = SetFlag(this.CC, StatusBits.IF); // Disable IRQ this.CC = SetBit(this.CC, StatusBits.IF); // Disable IRQ
this.CC = SetFlag(this.CC, StatusBits.FF); // Disable FIRQ this.CC = SetBit(this.CC, StatusBits.FF); // Disable FIRQ
this.Jump(this.GetWordPaged(0xff, NMIvector)); this.Jump(this.GetWordPaged(0xff, NMIvector));
this.Tick(12); this.Tick(12);
} }
@ -350,8 +350,8 @@
this.LowerBA(); this.LowerBA();
this.RaiseBS(); this.RaiseBS();
this.SavePartialRegisterState(); this.SavePartialRegisterState();
this.CC = SetFlag(this.CC, StatusBits.IF); // Disable IRQ this.CC = SetBit(this.CC, StatusBits.IF); // Disable IRQ
this.CC = SetFlag(this.CC, StatusBits.FF); // Disable FIRQ this.CC = SetBit(this.CC, StatusBits.FF); // Disable FIRQ
this.Jump(this.GetWordPaged(0xff, FIRQvector)); this.Jump(this.GetWordPaged(0xff, FIRQvector));
this.Tick(12); this.Tick(12);
} }
@ -400,15 +400,15 @@
private void OnExecutedInstruction() => this.ExecutedInstruction?.Invoke(this, EventArgs.Empty); private void OnExecutedInstruction() => this.ExecutedInstruction?.Invoke(this, EventArgs.Empty);
private static byte SetFlag(byte f, StatusBits flag) => SetFlag(f, (byte)flag); private static byte SetBit(byte f, StatusBits flag) => SetBit(f, (byte)flag);
private static byte SetFlag(byte f, StatusBits flag, int condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, int condition) => SetBit(f, (byte)flag, condition);
private static byte SetFlag(byte f, StatusBits flag, bool condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, bool condition) => SetBit(f, (byte)flag, condition);
private static byte ClearFlag(byte f, StatusBits flag) => ClearFlag(f, (byte)flag); private static byte ClearBit(byte f, StatusBits flag) => ClearBit(f, (byte)flag);
private static byte ClearFlag(byte f, StatusBits flag, int condition) => ClearFlag(f, (byte)flag, condition); private static byte ClearBit(byte f, StatusBits flag, int condition) => ClearBit(f, (byte)flag, condition);
private void Push(Register16 stack, byte value) => this.BusWrite(--stack.Word, value); private void Push(Register16 stack, byte value) => this.BusWrite(--stack.Word, value);
@ -562,15 +562,15 @@
private Register16 AM_extended_word() => this.GetWord(this.Address_extended()); private Register16 AM_extended_word() => this.GetWord(this.Address_extended());
private byte AdjustZero(byte datum) => ClearFlag(this.CC, StatusBits.ZF, datum); private byte AdjustZero(byte datum) => ClearBit(this.CC, StatusBits.ZF, datum);
private byte AdjustZero(ushort datum) => ClearFlag(this.CC, StatusBits.ZF, datum); private byte AdjustZero(ushort datum) => ClearBit(this.CC, StatusBits.ZF, datum);
private byte AdjustZero(Register16 datum) => this.AdjustZero(datum.Word); private byte AdjustZero(Register16 datum) => this.AdjustZero(datum.Word);
private byte AdjustNegative(byte datum) => SetFlag(this.CC, StatusBits.NF, datum & (byte)Bits.Bit7); private byte AdjustNegative(byte datum) => SetBit(this.CC, StatusBits.NF, datum & (byte)Bits.Bit7);
private byte AdjustNegative(ushort datum) => SetFlag(this.CC, StatusBits.NF, datum & (ushort)Bits.Bit15); private byte AdjustNegative(ushort datum) => SetBit(this.CC, StatusBits.NF, datum & (ushort)Bits.Bit15);
private byte AdjustNZ(byte datum) private byte AdjustNZ(byte datum)
{ {
@ -586,9 +586,9 @@
private byte AdjustNZ(Register16 datum) => this.AdjustNZ(datum.Word); private byte AdjustNZ(Register16 datum) => this.AdjustNZ(datum.Word);
private byte AdjustCarry(ushort datum) => SetFlag(this.CC, StatusBits.CF, datum & (ushort)Bits.Bit8); // 8-bit addition private byte AdjustCarry(ushort datum) => SetBit(this.CC, StatusBits.CF, datum & (ushort)Bits.Bit8); // 8-bit addition
private byte AdjustCarry(uint datum) => SetFlag(this.CC, StatusBits.CF, (int)(datum & (uint)Bits.Bit16)); // 16-bit addition private byte AdjustCarry(uint datum) => SetBit(this.CC, StatusBits.CF, (int)(datum & (uint)Bits.Bit16)); // 16-bit addition
private byte AdjustCarry(Register16 datum) => this.AdjustCarry(datum.Word); private byte AdjustCarry(Register16 datum) => this.AdjustCarry(datum.Word);
@ -596,17 +596,17 @@
{ {
var lowAfter = after.Low; var lowAfter = after.Low;
var highAfter = after.High; var highAfter = after.High;
return SetFlag(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 7)) & (int)Bits.Bit7); return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 7)) & (int)Bits.Bit7);
} }
private byte AdjustOverflow(ushort before, ushort data, uint after) private byte AdjustOverflow(ushort before, ushort data, uint after)
{ {
var lowAfter = (ushort)(after & (uint)Mask.Mask16); var lowAfter = (ushort)(after & (uint)Mask.Mask16);
var highAfter = (ushort)(after >> 16); var highAfter = (ushort)(after >> 16);
return SetFlag(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 15)) & (int)Bits.Bit15); return SetBit(this.CC, StatusBits.VF, (before ^ data ^ lowAfter ^ (highAfter << 15)) & (int)Bits.Bit15);
} }
private byte AdjustHalfCarry(byte before, byte data, byte after) => SetFlag(this.CC, StatusBits.HF, (before ^ data ^ after) & (int)Bits.Bit4); private byte AdjustHalfCarry(byte before, byte data, byte after) => SetBit(this.CC, StatusBits.HF, (before ^ data ^ after) & (int)Bits.Bit4);
private byte AdjustAddition(byte before, byte data, Register16 after) private byte AdjustAddition(byte before, byte data, Register16 after)
{ {
@ -647,14 +647,14 @@
private byte Through(byte data) private byte Through(byte data)
{ {
this.CC = ClearFlag(this.CC, StatusBits.VF); this.CC = ClearBit(this.CC, StatusBits.VF);
this.CC = this.AdjustNZ(data); this.CC = this.AdjustNZ(data);
return data; return data;
} }
private Register16 Through(Register16 data) private Register16 Through(Register16 data)
{ {
this.CC = ClearFlag(this.CC, StatusBits.VF); this.CC = ClearBit(this.CC, StatusBits.VF);
this.CC = this.AdjustNZ(data); this.CC = this.AdjustNZ(data);
return data; return data;
} }
@ -693,13 +693,13 @@
private void SaveEntireRegisterState() private void SaveEntireRegisterState()
{ {
this.CC = SetFlag(this.CC, StatusBits.EF); this.CC = SetBit(this.CC, StatusBits.EF);
this.SaveRegisterState(); this.SaveRegisterState();
} }
private void SavePartialRegisterState() private void SavePartialRegisterState()
{ {
this.CC = ClearFlag(this.CC, StatusBits.EF); this.CC = ClearBit(this.CC, StatusBits.EF);
this.SaveRegisterState(); this.SaveRegisterState();
} }
@ -1225,16 +1225,16 @@
private byte ASL(byte operand) private byte ASL(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7); this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7);
this.CC = this.AdjustNZ(operand <<= 1); this.CC = this.AdjustNZ(operand <<= 1);
var overflow = this.Carry ^ (this.Negative >> 3); var overflow = this.Carry ^ (this.Negative >> 3);
this.CC = SetFlag(this.CC, StatusBits.VF, overflow); this.CC = SetBit(this.CC, StatusBits.VF, overflow);
return operand; return operand;
} }
private byte ASR(byte operand) private byte ASR(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0); this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (byte)((operand >> 1) | (int)Bits.Bit7); var result = (byte)((operand >> 1) | (int)Bits.Bit7);
this.CC = this.AdjustNZ(result); this.CC = this.AdjustNZ(result);
return result; return result;
@ -1244,7 +1244,7 @@
private byte CLR() private byte CLR()
{ {
this.CC = ClearFlag(this.CC, StatusBits.CF); this.CC = ClearBit(this.CC, StatusBits.CF);
return this.Through((byte)0U); return this.Through((byte)0U);
} }
@ -1254,7 +1254,7 @@
private byte COM(byte operand) private byte COM(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.CF); this.CC = SetBit(this.CC, StatusBits.CF);
return this.Through((byte)~operand); return this.Through((byte)~operand);
} }
@ -1267,7 +1267,7 @@
private byte DA(byte operand) private byte DA(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.CF, operand > 0x99); this.CC = SetBit(this.CC, StatusBits.CF, operand > 0x99);
var lowAdjust = (this.HalfCarry != 0) || (Chip.LowNibble(operand) > 9); var lowAdjust = (this.HalfCarry != 0) || (Chip.LowNibble(operand) > 9);
var highAdjust = (this.Carry != 0) || (operand > 0x99); var highAdjust = (this.Carry != 0) || (operand > 0x99);
@ -1352,7 +1352,7 @@
private byte LSR(byte operand) private byte LSR(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0); this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0);
this.CC = this.AdjustNZ(operand >>= 1); this.CC = this.AdjustNZ(operand >>= 1);
return operand; return operand;
} }
@ -1361,13 +1361,13 @@
{ {
var result = new Register16(first * second); var result = new Register16(first * second);
this.CC = this.AdjustZero(result); this.CC = this.AdjustZero(result);
this.CC = SetFlag(this.CC, StatusBits.CF, result.Low & (byte)Bits.Bit7); this.CC = SetBit(this.CC, StatusBits.CF, result.Low & (byte)Bits.Bit7);
return result; return result;
} }
private byte NEG(byte operand) private byte NEG(byte operand)
{ {
this.CC = SetFlag(this.CC, StatusBits.VF, operand == (byte)Bits.Bit7); this.CC = SetBit(this.CC, StatusBits.VF, operand == (byte)Bits.Bit7);
var result = new Register16(0 - operand); var result = new Register16(0 - operand);
operand = result.Low; operand = result.Low;
this.CC = this.AdjustNZ(operand); this.CC = this.AdjustNZ(operand);
@ -1484,8 +1484,8 @@
private byte ROL(byte operand) private byte ROL(byte operand)
{ {
var carryIn = this.Carry; var carryIn = this.Carry;
this.CC = SetFlag(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7); this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit7);
this.CC = SetFlag(this.CC, StatusBits.VF, ((operand & (byte)Bits.Bit7) >> 7) ^ ((operand & (byte)Bits.Bit6) >> 6)); this.CC = SetBit(this.CC, StatusBits.VF, ((operand & (byte)Bits.Bit7) >> 7) ^ ((operand & (byte)Bits.Bit6) >> 6));
var result = (byte)((operand << 1) | carryIn); var result = (byte)((operand << 1) | carryIn);
this.CC = this.AdjustNZ(result); this.CC = this.AdjustNZ(result);
return result; return result;
@ -1494,7 +1494,7 @@
private byte ROR(byte operand) private byte ROR(byte operand)
{ {
var carryIn = this.Carry; var carryIn = this.Carry;
this.CC = SetFlag(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0); this.CC = SetBit(this.CC, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (byte)((operand >> 1) | (carryIn << 7)); var result = (byte)((operand >> 1) | (carryIn << 7));
this.CC = this.AdjustNZ(result); this.CC = this.AdjustNZ(result);
return result; return result;
@ -1529,8 +1529,8 @@
private void SWI() private void SWI()
{ {
this.SaveEntireRegisterState(); this.SaveEntireRegisterState();
this.CC = SetFlag(this.CC, StatusBits.IF); // Disable IRQ this.CC = SetBit(this.CC, StatusBits.IF); // Disable IRQ
this.CC = SetFlag(this.CC, StatusBits.FF); // Disable FIRQ this.CC = SetBit(this.CC, StatusBits.FF); // Disable FIRQ
this.Jump(this.GetWordPaged(0xff, SWIvector)); this.Jump(this.GetWordPaged(0xff, SWIvector));
} }

View File

@ -326,14 +326,14 @@ namespace EightBit
get get
{ {
byte status = 0; byte status = 0;
status = SetFlag(status, StatusRegister.STATUS_RDRF, this.statusRDRF); status = SetBit(status, StatusRegister.STATUS_RDRF, this.statusRDRF);
status = SetFlag(status, StatusRegister.STATUS_TDRE, this.statusTDRE); status = SetBit(status, StatusRegister.STATUS_TDRE, this.statusTDRE);
status = SetFlag(status, StatusRegister.STATUS_DCD, this.DCD.Raised()); status = SetBit(status, StatusRegister.STATUS_DCD, this.DCD.Raised());
status = SetFlag(status, StatusRegister.STATUS_CTS, this.CTS.Raised()); status = SetBit(status, StatusRegister.STATUS_CTS, this.CTS.Raised());
status = ClearFlag(status, StatusRegister.STATUS_FE); status = ClearBit(status, StatusRegister.STATUS_FE);
status = SetFlag(status, StatusRegister.STATUS_OVRN, this.statusOVRN); status = SetBit(status, StatusRegister.STATUS_OVRN, this.statusOVRN);
status = ClearFlag(status, StatusRegister.STATUS_PE); status = ClearBit(status, StatusRegister.STATUS_PE);
return SetFlag(status, StatusRegister.STATUS_IRQ, this.IRQ.Lowered()); return SetBit(status, StatusRegister.STATUS_IRQ, this.IRQ.Lowered());
} }
} }
@ -389,9 +389,9 @@ namespace EightBit
this.Step(); this.Step();
} }
private static byte SetFlag(byte f, StatusRegister flag, bool condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusRegister flag, bool condition) => SetBit(f, (byte)flag, condition);
private static byte ClearFlag(byte f, StatusRegister flag) => ClearFlag(f, (byte)flag); private static byte ClearBit(byte f, StatusRegister flag) => ClearBit(f, (byte)flag);
private void Step() private void Step()
{ {

View File

@ -281,21 +281,21 @@ namespace EightBit
} }
} }
private static byte SetFlag(byte f, StatusBits flag) => SetFlag(f, (byte)flag); private static byte SetBit(byte f, StatusBits flag) => SetBit(f, (byte)flag);
private static byte SetFlag(byte f, StatusBits flag, int condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, int condition) => SetBit(f, (byte)flag, condition);
private static byte SetFlag(byte f, StatusBits flag, bool condition) => SetFlag(f, (byte)flag, condition); private static byte SetBit(byte f, StatusBits flag, bool condition) => SetBit(f, (byte)flag, condition);
private static byte ClearFlag(byte f, StatusBits flag) => ClearFlag(f, (byte)flag); private static byte ClearBit(byte f, StatusBits flag) => ClearBit(f, (byte)flag);
private static byte ClearFlag(byte f, StatusBits flag, int condition) => ClearFlag(f, (byte)flag, condition); private static byte ClearBit(byte f, StatusBits flag, int condition) => ClearBit(f, (byte)flag, condition);
private static byte AdjustSign(byte input, byte value) => SetFlag(input, StatusBits.SF, value & (byte)StatusBits.SF); private static byte AdjustSign(byte input, byte value) => SetBit(input, StatusBits.SF, value & (byte)StatusBits.SF);
private static byte AdjustZero(byte input, byte value) => ClearFlag(input, StatusBits.ZF, value); private static byte AdjustZero(byte input, byte value) => ClearBit(input, StatusBits.ZF, value);
private static byte AdjustParity(byte input, byte value) => SetFlag(input, StatusBits.PF, EvenParity(value)); private static byte AdjustParity(byte input, byte value) => SetBit(input, StatusBits.PF, EvenParity(value));
private static byte AdjustSZ(byte input, byte value) private static byte AdjustSZ(byte input, byte value)
{ {
@ -311,8 +311,8 @@ namespace EightBit
private static byte AdjustXY(byte input, byte value) private static byte AdjustXY(byte input, byte value)
{ {
input = SetFlag(input, StatusBits.XF, value & (byte)StatusBits.XF); input = SetBit(input, StatusBits.XF, value & (byte)StatusBits.XF);
return SetFlag(input, StatusBits.YF, value & (byte)StatusBits.YF); return SetBit(input, StatusBits.YF, value & (byte)StatusBits.YF);
} }
private static byte AdjustSZPXY(byte input, byte value) private static byte AdjustSZPXY(byte input, byte value)
@ -327,14 +327,14 @@ namespace EightBit
return AdjustXY(input, value); return AdjustXY(input, value);
} }
private static byte AdjustHalfCarryAdd(byte input, byte before, byte value, int calculation) => SetFlag(input, StatusBits.HC, CalculateHalfCarryAdd(before, value, calculation)); private static byte AdjustHalfCarryAdd(byte input, byte before, byte value, int calculation) => SetBit(input, StatusBits.HC, CalculateHalfCarryAdd(before, value, calculation));
private static byte AdjustHalfCarrySub(byte input, byte before, byte value, int calculation) => SetFlag(input, StatusBits.HC, CalculateHalfCarrySub(before, value, calculation)); private static byte AdjustHalfCarrySub(byte input, byte before, byte value, int calculation) => SetBit(input, StatusBits.HC, CalculateHalfCarrySub(before, value, calculation));
private static byte AdjustOverflowAdd(byte input, int beforeNegative, int valueNegative, int afterNegative) private static byte AdjustOverflowAdd(byte input, int beforeNegative, int valueNegative, int afterNegative)
{ {
var overflow = (beforeNegative == valueNegative) && (beforeNegative != afterNegative); var overflow = (beforeNegative == valueNegative) && (beforeNegative != afterNegative);
return SetFlag(input, StatusBits.VF, overflow); return SetBit(input, StatusBits.VF, overflow);
} }
private static byte AdjustOverflowAdd(byte input, byte before, byte value, byte calculation) => AdjustOverflowAdd(input, before & (byte)StatusBits.SF, value & (byte)StatusBits.SF, calculation & (byte)StatusBits.SF); private static byte AdjustOverflowAdd(byte input, byte before, byte value, byte calculation) => AdjustOverflowAdd(input, before & (byte)StatusBits.SF, value & (byte)StatusBits.SF, calculation & (byte)StatusBits.SF);
@ -342,7 +342,7 @@ namespace EightBit
private static byte AdjustOverflowSub(byte input, int beforeNegative, int valueNegative, int afterNegative) private static byte AdjustOverflowSub(byte input, int beforeNegative, int valueNegative, int afterNegative)
{ {
var overflow = (beforeNegative != valueNegative) && (beforeNegative != afterNegative); var overflow = (beforeNegative != valueNegative) && (beforeNegative != afterNegative);
return SetFlag(input, StatusBits.VF, overflow); return SetBit(input, StatusBits.VF, overflow);
} }
private static byte AdjustOverflowSub(byte input, byte before, byte value, byte calculation) => AdjustOverflowSub(input, before & (byte)StatusBits.SF, value & (byte)StatusBits.SF, calculation & (byte)StatusBits.SF); private static byte AdjustOverflowSub(byte input, byte before, byte value, byte calculation) => AdjustOverflowSub(input, before & (byte)StatusBits.SF, value & (byte)StatusBits.SF, calculation & (byte)StatusBits.SF);
@ -605,7 +605,7 @@ namespace EightBit
} }
this.F = AdjustSZPXY(this.F, this.Bus.Data); this.F = AdjustSZPXY(this.F, this.Bus.Data);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.Tick(12); this.Tick(12);
break; break;
case 1: // Output to port with 16-bit address case 1: // Output to port with 16-bit address
@ -700,14 +700,14 @@ namespace EightBit
break; break;
case 2: // LD A,I case 2: // LD A,I
this.F = AdjustSZXY(this.F, this.A = this.IV); this.F = AdjustSZXY(this.F, this.A = this.IV);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.PF, this.IFF2); this.F = SetBit(this.F, StatusBits.PF, this.IFF2);
this.Tick(9); this.Tick(9);
break; break;
case 3: // LD A,R case 3: // LD A,R
this.F = AdjustSZXY(this.F, this.A = this.REFRESH); this.F = AdjustSZXY(this.F, this.A = this.REFRESH);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.PF, this.IFF2); this.F = SetBit(this.F, StatusBits.PF, this.IFF2);
this.Tick(9); this.Tick(9);
break; break;
case 4: // RRD case 4: // RRD
@ -1404,8 +1404,8 @@ namespace EightBit
this.F = AdjustHalfCarrySub(this.F, operand, value, result); this.F = AdjustHalfCarrySub(this.F, operand, value, result);
this.F = AdjustOverflowSub(this.F, operand, value, result); this.F = AdjustOverflowSub(this.F, operand, value, result);
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
this.F = SetFlag(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF); this.F = SetBit(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF);
this.F = AdjustSZ(this.F, result); this.F = AdjustSZ(this.F, result);
return result; return result;
@ -1413,21 +1413,21 @@ namespace EightBit
private byte Increment(byte operand) private byte Increment(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
var result = ++operand; var result = ++operand;
this.F = AdjustSZXY(this.F, result); this.F = AdjustSZXY(this.F, result);
this.F = SetFlag(this.F, StatusBits.VF, result == (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.VF, result == (byte)Bits.Bit7);
this.F = ClearFlag(this.F, StatusBits.HC, LowNibble(result)); this.F = ClearBit(this.F, StatusBits.HC, LowNibble(result));
return result; return result;
} }
private byte Decrement(byte operand) private byte Decrement(byte operand)
{ {
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
this.F = ClearFlag(this.F, StatusBits.HC, LowNibble(operand)); this.F = ClearBit(this.F, StatusBits.HC, LowNibble(operand));
var result = --operand; var result = --operand;
this.F = AdjustSZXY(this.F, result); this.F = AdjustSZXY(this.F, result);
this.F = SetFlag(this.F, StatusBits.VF, result == (byte)Mask.Mask7); this.F = SetBit(this.F, StatusBits.VF, result == (byte)Mask.Mask7);
return result; return result;
} }
@ -1544,12 +1544,12 @@ namespace EightBit
var afterNegative = hl2.High & (byte)StatusBits.SF; var afterNegative = hl2.High & (byte)StatusBits.SF;
this.F = SetFlag(this.F, StatusBits.SF, afterNegative); this.F = SetBit(this.F, StatusBits.SF, afterNegative);
this.F = ClearFlag(this.F, StatusBits.ZF, hl2.Word); this.F = ClearBit(this.F, StatusBits.ZF, hl2.Word);
this.F = AdjustHalfCarrySub(this.F, this.MEMPTR.High, value.High, hl2.High); this.F = AdjustHalfCarrySub(this.F, this.MEMPTR.High, value.High, hl2.High);
this.F = AdjustOverflowSub(this.F, beforeNegative, valueNegative, afterNegative); this.F = AdjustOverflowSub(this.F, beforeNegative, valueNegative, afterNegative);
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
this.F = SetFlag(this.F, StatusBits.CF, result & (int)Bits.Bit16); this.F = SetBit(this.F, StatusBits.CF, result & (int)Bits.Bit16);
this.F = AdjustXY(this.F, hl2.High); this.F = AdjustXY(this.F, hl2.High);
++this.MEMPTR.Word; ++this.MEMPTR.Word;
@ -1568,12 +1568,12 @@ namespace EightBit
var afterNegative = hl2.High & (byte)StatusBits.SF; var afterNegative = hl2.High & (byte)StatusBits.SF;
this.F = SetFlag(this.F, StatusBits.SF, afterNegative); this.F = SetBit(this.F, StatusBits.SF, afterNegative);
this.F = ClearFlag(this.F, StatusBits.ZF, hl2.Word); this.F = ClearBit(this.F, StatusBits.ZF, hl2.Word);
this.F = AdjustHalfCarryAdd(this.F, this.MEMPTR.High, value.High, hl2.High); this.F = AdjustHalfCarryAdd(this.F, this.MEMPTR.High, value.High, hl2.High);
this.F = AdjustOverflowAdd(this.F, beforeNegative, valueNegative, afterNegative); this.F = AdjustOverflowAdd(this.F, beforeNegative, valueNegative, afterNegative);
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
this.F = SetFlag(this.F, StatusBits.CF, result & (int)Bits.Bit16); this.F = SetBit(this.F, StatusBits.CF, result & (int)Bits.Bit16);
this.F = AdjustXY(this.F, hl2.High); this.F = AdjustXY(this.F, hl2.High);
++this.MEMPTR.Word; ++this.MEMPTR.Word;
@ -1588,8 +1588,8 @@ namespace EightBit
hl2.Word = (ushort)result; hl2.Word = (ushort)result;
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
this.F = SetFlag(this.F, StatusBits.CF, result & (int)Bits.Bit16); this.F = SetBit(this.F, StatusBits.CF, result & (int)Bits.Bit16);
this.F = AdjustHalfCarryAdd(this.F, this.MEMPTR.High, value.High, hl2.High); this.F = AdjustHalfCarryAdd(this.F, this.MEMPTR.High, value.High, hl2.High);
this.F = AdjustXY(this.F, hl2.High); this.F = AdjustXY(this.F, hl2.High);
@ -1603,8 +1603,8 @@ namespace EightBit
this.F = AdjustHalfCarryAdd(this.F, this.A, value, this.intermediate.Low); this.F = AdjustHalfCarryAdd(this.F, this.A, value, this.intermediate.Low);
this.F = AdjustOverflowAdd(this.F, this.A, value, this.intermediate.Low); this.F = AdjustOverflowAdd(this.F, this.A, value, this.intermediate.Low);
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
this.F = SetFlag(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF); this.F = SetBit(this.F, StatusBits.CF, this.intermediate.High & (byte)StatusBits.CF);
this.F = AdjustSZXY(this.F, this.A = this.intermediate.Low); this.F = AdjustSZXY(this.F, this.A = this.intermediate.Low);
} }
@ -1620,20 +1620,20 @@ namespace EightBit
private void AndR(byte value) private void AndR(byte value)
{ {
this.F = SetFlag(this.F, StatusBits.HC); this.F = SetBit(this.F, StatusBits.HC);
this.F = ClearFlag(this.F, StatusBits.CF | StatusBits.NF); this.F = ClearBit(this.F, StatusBits.CF | StatusBits.NF);
this.F = AdjustSZPXY(this.F, this.A &= value); this.F = AdjustSZPXY(this.F, this.A &= value);
} }
private void XorR(byte value) private void XorR(byte value)
{ {
this.F = ClearFlag(this.F, StatusBits.HC | StatusBits.CF | StatusBits.NF); this.F = ClearBit(this.F, StatusBits.HC | StatusBits.CF | StatusBits.NF);
this.F = AdjustSZPXY(this.F, this.A ^= value); this.F = AdjustSZPXY(this.F, this.A ^= value);
} }
private void OrR(byte value) private void OrR(byte value)
{ {
this.F = ClearFlag(this.F, StatusBits.HC | StatusBits.CF | StatusBits.NF); this.F = ClearBit(this.F, StatusBits.HC | StatusBits.CF | StatusBits.NF);
this.F = AdjustSZPXY(this.F, this.A |= value); this.F = AdjustSZPXY(this.F, this.A |= value);
} }
@ -1645,9 +1645,9 @@ namespace EightBit
private byte RLC(byte operand) private byte RLC(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
var carry = operand & (byte)Bits.Bit7; var carry = operand & (byte)Bits.Bit7;
this.F = SetFlag(this.F, StatusBits.CF, carry); this.F = SetBit(this.F, StatusBits.CF, carry);
var result = (byte)((operand << 1) | (carry >> 7)); var result = (byte)((operand << 1) | (carry >> 7));
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1655,9 +1655,9 @@ namespace EightBit
private byte RRC(byte operand) private byte RRC(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
var carry = operand & (byte)Bits.Bit0; var carry = operand & (byte)Bits.Bit0;
this.F = SetFlag(this.F, StatusBits.CF, carry); this.F = SetBit(this.F, StatusBits.CF, carry);
var result = (byte)((operand >> 1) | (carry << 7)); var result = (byte)((operand >> 1) | (carry << 7));
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1665,9 +1665,9 @@ namespace EightBit
private byte RL(byte operand) private byte RL(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
var carry = this.F & (byte)StatusBits.CF; var carry = this.F & (byte)StatusBits.CF;
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit7);
var result = (byte)((operand << 1) | carry); var result = (byte)((operand << 1) | carry);
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1675,9 +1675,9 @@ namespace EightBit
private byte RR(byte operand) private byte RR(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
var carry = this.F & (byte)StatusBits.CF; var carry = this.F & (byte)StatusBits.CF;
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit0); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (byte)((operand >> 1) | (carry << 7)); var result = (byte)((operand >> 1) | (carry << 7));
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1685,8 +1685,8 @@ namespace EightBit
private byte SLA(byte operand) private byte SLA(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit7);
var result = (byte)(operand << 1); var result = (byte)(operand << 1);
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1694,8 +1694,8 @@ namespace EightBit
private byte SRA(byte operand) private byte SRA(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit0); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (byte)((operand >> 1) | (operand & (byte)Bits.Bit7)); var result = (byte)((operand >> 1) | (operand & (byte)Bits.Bit7));
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1703,8 +1703,8 @@ namespace EightBit
private byte SLL(byte operand) private byte SLL(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit7);
var result = (byte)((operand << 1) | (byte)Bits.Bit0); var result = (byte)((operand << 1) | (byte)Bits.Bit0);
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
return result; return result;
@ -1712,21 +1712,21 @@ namespace EightBit
private byte SRL(byte operand) private byte SRL(byte operand)
{ {
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.CF, operand & (byte)Bits.Bit0); this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit0);
var result = (byte)((operand >> 1) & ~(byte)Bits.Bit7); var result = (byte)((operand >> 1) & ~(byte)Bits.Bit7);
this.F = AdjustXY(this.F, result); this.F = AdjustXY(this.F, result);
this.F = SetFlag(this.F, StatusBits.ZF, result); this.F = SetBit(this.F, StatusBits.ZF, result);
return result; return result;
} }
private void BIT(int n, byte operand) private void BIT(int n, byte operand)
{ {
this.F = SetFlag(this.F, StatusBits.HC); this.F = SetBit(this.F, StatusBits.HC);
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
var discarded = (byte)(operand & (1 << n)); var discarded = (byte)(operand & (1 << n));
this.F = AdjustSZ(this.F, discarded); this.F = AdjustSZ(this.F, discarded);
this.F = ClearFlag(this.F, StatusBits.PF, discarded); this.F = ClearBit(this.F, StatusBits.PF, discarded);
} }
private void DAA() private void DAA()
@ -1768,23 +1768,23 @@ namespace EightBit
private void SCF() private void SCF()
{ {
this.F = SetFlag(this.F, StatusBits.CF); this.F = SetBit(this.F, StatusBits.CF);
this.F = ClearFlag(this.F, StatusBits.HC | StatusBits.NF); this.F = ClearBit(this.F, StatusBits.HC | StatusBits.NF);
this.F = AdjustXY(this.F, this.A); this.F = AdjustXY(this.F, this.A);
} }
private void CCF() private void CCF()
{ {
this.F = ClearFlag(this.F, StatusBits.NF); this.F = ClearBit(this.F, StatusBits.NF);
var carry = this.F & (byte)StatusBits.CF; var carry = this.F & (byte)StatusBits.CF;
this.F = SetFlag(this.F, StatusBits.HC, carry); this.F = SetBit(this.F, StatusBits.HC, carry);
this.F = ClearFlag(this.F, StatusBits.CF, carry); this.F = ClearBit(this.F, StatusBits.CF, carry);
this.F = AdjustXY(this.F, this.A); this.F = AdjustXY(this.F, this.A);
} }
private void CPL() private void CPL()
{ {
this.F = SetFlag(this.F, StatusBits.HC | StatusBits.NF); this.F = SetBit(this.F, StatusBits.HC | StatusBits.NF);
this.F = AdjustXY(this.F, this.A = (byte)~this.A); this.F = AdjustXY(this.F, this.A = (byte)~this.A);
} }
@ -1805,16 +1805,16 @@ namespace EightBit
var value = this.BusRead(source); var value = this.BusRead(source);
var result = (byte)(this.A - value); var result = (byte)(this.A - value);
this.F = SetFlag(this.F, StatusBits.PF, counter); this.F = SetBit(this.F, StatusBits.PF, counter);
this.F = AdjustSZ(this.F, result); this.F = AdjustSZ(this.F, result);
this.F = AdjustHalfCarrySub(this.F, this.A, value, result); this.F = AdjustHalfCarrySub(this.F, this.A, value, result);
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
result -= (byte)((this.F & (byte)StatusBits.HC) >> 4); result -= (byte)((this.F & (byte)StatusBits.HC) >> 4);
this.F = SetFlag(this.F, StatusBits.YF, result & (byte)Bits.Bit1); this.F = SetBit(this.F, StatusBits.YF, result & (byte)Bits.Bit1);
this.F = SetFlag(this.F, StatusBits.XF, result & (byte)Bits.Bit3); this.F = SetBit(this.F, StatusBits.XF, result & (byte)Bits.Bit3);
} }
private void CPI() private void CPI()
@ -1846,10 +1846,10 @@ namespace EightBit
var value = this.BusRead(source); var value = this.BusRead(source);
this.BusWrite(destination, value); this.BusWrite(destination, value);
var xy = this.A + value; var xy = this.A + value;
this.F = SetFlag(this.F, StatusBits.XF, xy & (int)Bits.Bit3); this.F = SetBit(this.F, StatusBits.XF, xy & (int)Bits.Bit3);
this.F = SetFlag(this.F, StatusBits.YF, xy & (int)Bits.Bit1); this.F = SetBit(this.F, StatusBits.YF, xy & (int)Bits.Bit1);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
this.F = SetFlag(this.F, StatusBits.PF, counter); this.F = SetBit(this.F, StatusBits.PF, counter);
} }
private void LDI() => this.BlockLoad(this.HL.Word++, this.DE.Word++, --this.BC.Word); private void LDI() => this.BlockLoad(this.HL.Word++, this.DE.Word++, --this.BC.Word);
@ -1874,7 +1874,7 @@ namespace EightBit
var value = this.ReadPort(); var value = this.ReadPort();
this.BusWrite(destination, value); this.BusWrite(destination, value);
source.High = this.Decrement(source.High); source.High = this.Decrement(source.High);
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
} }
private void INI() private void INI()
@ -1908,8 +1908,8 @@ namespace EightBit
this.WritePort(); this.WritePort();
destination.High = this.Decrement(destination.High); destination.High = this.Decrement(destination.High);
this.MEMPTR.Word = destination.Word; this.MEMPTR.Word = destination.Word;
this.F = SetFlag(this.F, StatusBits.NF, value & (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.NF, value & (byte)Bits.Bit7);
this.F = SetFlag(this.F, StatusBits.HC | StatusBits.CF, (this.L + value) > 0xff); this.F = SetBit(this.F, StatusBits.HC | StatusBits.CF, (this.L + value) > 0xff);
this.F = AdjustParity(this.F, (byte)(((value + this.L) & (int)Mask.Mask3) ^ this.B)); this.F = AdjustParity(this.F, (byte)(((value + this.L) & (int)Mask.Mask3) ^ this.B));
} }
@ -1939,9 +1939,9 @@ namespace EightBit
private void NEG() private void NEG()
{ {
this.F = SetFlag(this.F, StatusBits.PF, this.A == (byte)Bits.Bit7); this.F = SetBit(this.F, StatusBits.PF, this.A == (byte)Bits.Bit7);
this.F = SetFlag(this.F, StatusBits.CF, this.A); this.F = SetBit(this.F, StatusBits.CF, this.A);
this.F = SetFlag(this.F, StatusBits.NF); this.F = SetBit(this.F, StatusBits.NF);
var original = this.A; var original = this.A;
@ -1961,7 +1961,7 @@ namespace EightBit
this.BusWrite((byte)(PromoteNibble(this.A) | HighNibble(memory))); this.BusWrite((byte)(PromoteNibble(this.A) | HighNibble(memory)));
this.A = (byte)(HigherNibble(this.A) | LowerNibble(memory)); this.A = (byte)(HigherNibble(this.A) | LowerNibble(memory));
this.F = AdjustSZPXY(this.F, this.A); this.F = AdjustSZPXY(this.F, this.A);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
} }
private void RLD() private void RLD()
@ -1972,7 +1972,7 @@ namespace EightBit
this.BusWrite((byte)(PromoteNibble(memory) | LowNibble(this.A))); this.BusWrite((byte)(PromoteNibble(memory) | LowNibble(this.A)));
this.A = (byte)(HigherNibble(this.A) | HighNibble(memory)); this.A = (byte)(HigherNibble(this.A) | HighNibble(memory));
this.F = AdjustSZPXY(this.F, this.A); this.F = AdjustSZPXY(this.F, this.A);
this.F = ClearFlag(this.F, StatusBits.NF | StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC);
} }
private void WritePort(byte port) private void WritePort(byte port)