diff --git a/EightBit/Chip.cs b/EightBit/Chip.cs index 9872621..56c11d0 100644 --- a/EightBit/Chip.cs +++ b/EightBit/Chip.cs @@ -12,6 +12,10 @@ namespace EightBit { } + public static byte Bit(int which) => (byte)(1 << which); + + public static byte Bit(byte which) => Bit((int)which); + public static byte SetBit(byte input, byte which) => (byte)(input | which); public static byte SetBit(byte input, byte which, int condition) => SetBit(input, which, condition != 0); @@ -72,22 +76,22 @@ namespace EightBit public static bool EvenParity(byte value) => EvenParity((int)value); - public static int FindFirstSet(int x) + public static int FindFirstSet(int value) { - if (x == 0) + if (value == 0) { return 0; } - var t = 1; - var r = 1; - while ((x & t) == 0) + var test = 1; + var result = 1; + while ((value & test) == 0) { - t <<= 1; - ++r; + test <<= 1; + ++result; } - return r; + return result; } } } diff --git a/LR35902/LR35902.cs b/LR35902/LR35902.cs index 9d4060a..6da6fe1 100644 --- a/LR35902/LR35902.cs +++ b/LR35902/LR35902.cs @@ -154,9 +154,9 @@ namespace EightBit.GameBoy private static byte AdjustHalfCarrySub(byte input, byte before, byte value, int calculation) => SetBit(input, StatusBits.HC, CalculateHalfCarrySub(before, value, calculation)); - private static byte Res(int n, byte operand) => (byte)(operand & ~(1 << n)); + private static byte Res(int n, byte operand) => ClearBit(operand, Bit(n)); - private static byte Set(int n, byte operand) => (byte)(operand | (1 << n)); + private static byte Set(int n, byte operand) => SetBit(operand, Bit(n)); private void DI() => this.IME = false; @@ -1068,7 +1068,7 @@ namespace EightBit.GameBoy private void Bit(int n, byte operand) { var carry = this.F & (byte)StatusBits.CF; - this.AndR(operand, (byte)(1 << n)); + this.AndR(operand, Bit(n)); this.F = SetBit(this.F, StatusBits.CF, carry); } diff --git a/Z80/Z80.cs b/Z80/Z80.cs index 777886d..c7c3a56 100644 --- a/Z80/Z80.cs +++ b/Z80/Z80.cs @@ -347,9 +347,9 @@ namespace EightBit 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 RES(int n, byte operand) => (byte)(operand & ~(1 << n)); + private static byte RES(int n, byte operand) => ClearBit(operand, Bit(n)); - private static byte SET(int n, byte operand) => (byte)(operand | (1 << n)); + private static byte SET(int n, byte operand) => SetBit(operand, Bit(n)); private void DisableInterrupts() => this.IFF1 = this.IFF2 = false; @@ -1724,7 +1724,7 @@ namespace EightBit { this.F = SetBit(this.F, StatusBits.HC); this.F = ClearBit(this.F, StatusBits.NF); - var discarded = (byte)(operand & (1 << n)); + var discarded = (byte)(operand & Bit(n)); this.F = AdjustSZ(this.F, discarded); this.F = ClearBit(this.F, StatusBits.PF, discarded); }