Couple of small refactorings, based on repeated bit patterns

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2019-08-07 13:27:03 +01:00
parent dedc340bf5
commit f54ef07057
3 changed files with 18 additions and 14 deletions

View File

@ -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;
}
}
}

View File

@ -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);
}

View File

@ -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);
}