2019-02-04 23:52:21 +00:00
|
|
|
|
// <copyright file="Chip.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace EightBit
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
public class Chip : Device
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
protected Chip()
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-08-07 12:27:03 +00:00
|
|
|
|
public static byte Bit(int which) => (byte)(1 << which);
|
|
|
|
|
|
|
|
|
|
public static byte Bit(byte which) => Bit((int)which);
|
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte SetBit(byte input, byte which) => (byte)(input | which);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte SetBit(byte input, byte which, int condition) => SetBit(input, which, condition != 0);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte SetBit(byte input, byte which, bool condition) => condition ? SetBit(input, which) : ClearBit(input, which);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte ClearBit(byte input, byte which) => (byte)(input & (byte)~which);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte ClearBit(byte input, byte which, int condition) => ClearBit(input, which, condition != 0);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
public static byte ClearBit(byte input, byte which, bool condition) => SetBit(input, which, !condition);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static byte HighByte(int value) => (byte)(value >> 8);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static byte HighByte(ushort value) => HighByte((int)value);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2020-06-21 23:00:15 +00:00
|
|
|
|
public static byte LowByte(int value) => (byte)(value & (int)Mask.Eight);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static byte LowByte(ushort value) => LowByte((int)value);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static ushort PromoteByte(byte value) => (ushort)(value << 8);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static byte DemoteByte(ushort value) => HighByte(value);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static ushort HigherPart(ushort value) => (ushort)(value & 0xff00);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static byte LowerPart(ushort value) => LowByte(value);
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public static ushort MakeWord(byte low, byte high) => (ushort)(PromoteByte(high) | low);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public static int HighNibble(byte value) => value >> 4;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public static int LowNibble(byte value) => value & 0xf;
|
|
|
|
|
|
|
|
|
|
public static int HigherNibble(byte value) => value & 0xf0;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public static int LowerNibble(byte value) => LowNibble(value);
|
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public static int PromoteNibble(byte value) => LowByte(value << 4);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public static int DemoteNibble(byte value) => HighNibble(value);
|
2019-02-16 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
public static int CountBits(int value)
|
|
|
|
|
{
|
2019-02-22 22:33:51 +00:00
|
|
|
|
var count = 0;
|
2019-02-16 21:32:34 +00:00
|
|
|
|
while (value != 0)
|
|
|
|
|
{
|
|
|
|
|
++count;
|
|
|
|
|
value &= value - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int CountBits(byte value) => CountBits((int)value);
|
|
|
|
|
|
|
|
|
|
public static bool EvenParity(int value) => CountBits(value) % 2 == 0;
|
|
|
|
|
|
|
|
|
|
public static bool EvenParity(byte value) => EvenParity((int)value);
|
2019-07-14 16:34:23 +00:00
|
|
|
|
|
2019-08-07 12:27:03 +00:00
|
|
|
|
public static int FindFirstSet(int value)
|
2019-07-14 16:34:23 +00:00
|
|
|
|
{
|
2019-08-07 12:27:03 +00:00
|
|
|
|
if (value == 0)
|
2019-07-14 16:34:23 +00:00
|
|
|
|
{
|
2019-07-30 22:55:20 +00:00
|
|
|
|
return 0;
|
2019-07-14 16:34:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 12:27:03 +00:00
|
|
|
|
var test = 1;
|
|
|
|
|
var result = 1;
|
|
|
|
|
while ((value & test) == 0)
|
2019-07-30 22:55:20 +00:00
|
|
|
|
{
|
2019-08-07 12:27:03 +00:00
|
|
|
|
test <<= 1;
|
|
|
|
|
++result;
|
2019-07-30 22:55:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 12:27:03 +00:00
|
|
|
|
return result;
|
2019-07-14 16:34:23 +00:00
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|