From c6efb35d68d5ef91fa93d50a108fb99630b6e4f4 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Wed, 14 Jun 2017 15:06:37 +0200 Subject: [PATCH] First Tree --- hwo.bitworks/ADD.cs | 12 ++++- hwo.bitworks/ADDX.cs | 8 +++ hwo.bitworks/AND.cs | 8 +++ hwo.bitworks/BComplement.cs | 26 ++++++++++ hwo.bitworks/BitBuffer.cs | 11 ++++ hwo.bitworks/BitFormat.cs | 17 ------- hwo.bitworks/Constants.cs | 16 ++++++ hwo.bitworks/Identities.cs | 34 +++++++++++++ hwo.bitworks/Invert.cs | 22 ++++++++ hwo.bitworks/LogicBase.cs | 14 +++++- hwo.bitworks/MultipleBitsSource.cs | 30 ++++++++++- hwo.bitworks/NAND.cs | 7 +++ hwo.bitworks/NOR.cs | 7 +++ hwo.bitworks/NOT.cs | 24 +++++++++ hwo.bitworks/OR.cs | 7 +++ hwo.bitworks/Program.cs | 31 +++++++++--- hwo.bitworks/RotateLeft.cs | 32 ++++++++++++ hwo.bitworks/RotateRight.cs | 32 ++++++++++++ hwo.bitworks/ShiftArithmeticLeft.cs | 36 ++++++++++++++ hwo.bitworks/ShiftLeft.cs | 11 ++++ hwo.bitworks/ShiftRight.cs | 10 ++++ hwo.bitworks/XOR.cs | 6 +++ hwo.bitworks/hwo.bitworks.csproj | 15 +++++- hwo.bitworks/identity/BitIdentity.cs | 37 ++++++++++++++ hwo.bitworks/identity/BitOperation.cs | 8 +++ hwo.bitworks/io/BitFormat.cs | 72 +++++++++++++++++++++++++++ 26 files changed, 506 insertions(+), 27 deletions(-) create mode 100644 hwo.bitworks/BComplement.cs delete mode 100644 hwo.bitworks/BitFormat.cs create mode 100644 hwo.bitworks/Identities.cs create mode 100644 hwo.bitworks/Invert.cs create mode 100644 hwo.bitworks/NOT.cs create mode 100644 hwo.bitworks/RotateLeft.cs create mode 100644 hwo.bitworks/RotateRight.cs create mode 100644 hwo.bitworks/ShiftArithmeticLeft.cs create mode 100644 hwo.bitworks/identity/BitIdentity.cs create mode 100644 hwo.bitworks/identity/BitOperation.cs create mode 100644 hwo.bitworks/io/BitFormat.cs diff --git a/hwo.bitworks/ADD.cs b/hwo.bitworks/ADD.cs index c0d3c56..411c62f 100644 --- a/hwo.bitworks/ADD.cs +++ b/hwo.bitworks/ADD.cs @@ -30,7 +30,17 @@ namespace hwo.bitworks } } - + public override identity.BitIdentity getBitIdentity(int bit) + { + switch (bit){ + case 1: + return or.identity(); + case 0: + return xor3.identity(); + default: + throw new IndexOutOfRangeException(String.Format("ADD supplies 2 bits, but bit #{0} requested",bit)); + } + } } } diff --git a/hwo.bitworks/ADDX.cs b/hwo.bitworks/ADDX.cs index 374593e..c490257 100644 --- a/hwo.bitworks/ADDX.cs +++ b/hwo.bitworks/ADDX.cs @@ -48,5 +48,13 @@ namespace hwo.bitworks } } + public override identity.BitIdentity getBitIdentity(int bit){ + if (bit == add.Length){ + return add[bit-1].getBitIdentity(1); + } else { + return add[bit].getBitIdentity(0); + } + } + } } diff --git a/hwo.bitworks/AND.cs b/hwo.bitworks/AND.cs index 4fd0510..1974cb1 100644 --- a/hwo.bitworks/AND.cs +++ b/hwo.bitworks/AND.cs @@ -1,4 +1,6 @@ using System; +using hwo.bitworks.identity; + namespace hwo.bitworks { public class AND : LogicBase @@ -18,5 +20,11 @@ namespace hwo.bitworks return true; } + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.AND,identities(this.sources)); + } + + } } diff --git a/hwo.bitworks/BComplement.cs b/hwo.bitworks/BComplement.cs new file mode 100644 index 0000000..afa2bd5 --- /dev/null +++ b/hwo.bitworks/BComplement.cs @@ -0,0 +1,26 @@ +using System; +namespace hwo.bitworks +{ + public class BComplement : MultipleBitsSource + { + Invert invert; + ADDX addx; + + public BComplement(LogicBase[] sources) + :base(sources) + { + invert = new Invert(sources); + addx = new ADDX(invert.getLogicalBases(),new LogicBase[]{Constants.ONE}); + } + + public override bool bitValue(int bit) + { + return addx.bitValue(bit); + } + + public override identity.BitIdentity getBitIdentity(int bit){ + return addx.getBitIdentity(bit); + } + + } +} diff --git a/hwo.bitworks/BitBuffer.cs b/hwo.bitworks/BitBuffer.cs index f989aae..23e5e0e 100644 --- a/hwo.bitworks/BitBuffer.cs +++ b/hwo.bitworks/BitBuffer.cs @@ -4,10 +4,17 @@ namespace hwo.bitworks public class BitBuffer : MultipleBitsSource { bool[] bits; + string name; public BitBuffer(int len) { this.bits = new bool[len]; + this.name = SourceName; + } + public BitBuffer(int len,string name) + { + this.bits = new bool[len]; + this.name = name; } public override int getBits() @@ -35,6 +42,10 @@ namespace hwo.bitworks return this.bits[ bit ]; } + public override identity.BitIdentity getBitIdentity(int bit) + { + return new identity.BitIdentity(String.Format("{0}({1})",this.name,bit)); + } } diff --git a/hwo.bitworks/BitFormat.cs b/hwo.bitworks/BitFormat.cs deleted file mode 100644 index c36bfee..0000000 --- a/hwo.bitworks/BitFormat.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -namespace hwo.bitworks -{ - public static class BitFormat - { - - public static string toString(LogicBase[] sources){ - char[] digits = new char[sources.Length]; - for (int n=0;n idObjects; + + protected Identities() + { + idObjects = new List(); + } + + public int getObjectIndex(object ido){ + if (!idObjects.Contains(ido)){ + idObjects.Add(ido); + } + return idObjects.IndexOf(ido); + } + + public object getObject(int index){ + return this.idObjects[index]; + } + + + } + +} diff --git a/hwo.bitworks/Invert.cs b/hwo.bitworks/Invert.cs new file mode 100644 index 0000000..2484179 --- /dev/null +++ b/hwo.bitworks/Invert.cs @@ -0,0 +1,22 @@ +using System; +namespace hwo.bitworks +{ + public class Invert : MultipleBitsSource + { + public Invert(LogicBase[] sources) + :base(sources) + { + } + + public override bool bitValue(int bit) + { + return !sources[bit].value(); + } + + public override identity.BitIdentity getBitIdentity(int bit) + { + return sources[bit].identity(); + } + + } +} diff --git a/hwo.bitworks/LogicBase.cs b/hwo.bitworks/LogicBase.cs index e2948a6..908b223 100644 --- a/hwo.bitworks/LogicBase.cs +++ b/hwo.bitworks/LogicBase.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using hwo.bitworks.identity; + namespace hwo.bitworks { - public class LogicBase + public abstract class LogicBase { protected LogicBase[] sources; @@ -17,6 +19,16 @@ namespace hwo.bitworks throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'bool value()'",this.GetType().FullName)); } + public abstract BitIdentity identity(); + + public BitIdentity[] identities(LogicBase[] sources){ + BitIdentity[] bids = new BitIdentity[sources.Length]; + for (int n=0;n> knownSources = new Dictionary>(); + + static protected void register(MultipleBitsSource s){ + Type t = s.GetType(); + if (!knownSources.ContainsKey(t)){ + knownSources.Add(t,new List()); + } + knownSources[t].Add(s); + } + protected LogicBase[] sources; public MultipleBitsSource() { + register(this); this.sources = null; } @@ -41,6 +55,10 @@ namespace hwo.bitworks return lb; } + public virtual BitIdentity getBitIdentity(int bit){ + throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'BitIdentity getBitIdentity(int bit)'",this.GetType().FullName)); + } + public class BIT : LogicBase { MultipleBitsSource source; int bit; @@ -56,8 +74,18 @@ namespace hwo.bitworks { return source.bitValue(this.bit); } + + public override identity.BitIdentity identity() + { + return source.getBitIdentity(this.bit); + } } - + public string SourceName { + get { + int no = knownSources[GetType()].IndexOf(this); + return string.Format("{0}[{1}]",GetType().Name,no); + } + } } } diff --git a/hwo.bitworks/NAND.cs b/hwo.bitworks/NAND.cs index f3b3d18..e966d2d 100644 --- a/hwo.bitworks/NAND.cs +++ b/hwo.bitworks/NAND.cs @@ -1,4 +1,6 @@ using System; +using hwo.bitworks.identity; + namespace hwo.bitworks { public class NAND : LogicBase @@ -18,5 +20,10 @@ namespace hwo.bitworks return false; } + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.NAND,identities(this.sources)); + } + } } diff --git a/hwo.bitworks/NOR.cs b/hwo.bitworks/NOR.cs index a5c0a4d..ae0e36e 100644 --- a/hwo.bitworks/NOR.cs +++ b/hwo.bitworks/NOR.cs @@ -1,4 +1,6 @@ using System; +using hwo.bitworks.identity; + namespace hwo.bitworks { public class NOR : LogicBase @@ -17,5 +19,10 @@ namespace hwo.bitworks } return true; } + + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.NOR,identities(this.sources)); + } } } diff --git a/hwo.bitworks/NOT.cs b/hwo.bitworks/NOT.cs new file mode 100644 index 0000000..007e842 --- /dev/null +++ b/hwo.bitworks/NOT.cs @@ -0,0 +1,24 @@ +using System; +using hwo.bitworks.identity; + +namespace hwo.bitworks +{ + public class NOT : LogicBase + { + public NOT(LogicBase source) + :base(new LogicBase[]{source}) + { + } + + public override bool value() + { + return !this.sources[0].value(); + } + + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.NOT,identities(this.sources)); + } + + } +} diff --git a/hwo.bitworks/OR.cs b/hwo.bitworks/OR.cs index 4b008d2..f3b2b9c 100644 --- a/hwo.bitworks/OR.cs +++ b/hwo.bitworks/OR.cs @@ -1,4 +1,6 @@ using System; +using hwo.bitworks.identity; + namespace hwo.bitworks { public class OR : LogicBase @@ -17,5 +19,10 @@ namespace hwo.bitworks } return false; } + + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.OR,identities(this.sources)); + } } } diff --git a/hwo.bitworks/Program.cs b/hwo.bitworks/Program.cs index 68817fe..067f93f 100644 --- a/hwo.bitworks/Program.cs +++ b/hwo.bitworks/Program.cs @@ -1,4 +1,5 @@ using System; +using hwo.bitworks.io; namespace hwo.bitworks { @@ -28,14 +29,14 @@ namespace hwo.bitworks } public static void test8bit(){ - Console.WriteLine("Test: 8bit"); + Console.WriteLine("Test: 32bit patterns"); BitBuffer bb1,bb2; - bb1 = new BitBuffer(8); - bb2 = new BitBuffer(8); + bb1 = new BitBuffer(32,"A"); + bb2 = new BitBuffer(32,"B"); - bb1.set(0x05); + bb1.set(0x0505); bb2.set(0x14); Console.WriteLine("bb1: {0}",BitFormat.toString(bb1.getLogicalBases())); @@ -43,14 +44,32 @@ namespace hwo.bitworks ADDX addx = new ADDX(bb1.getLogicalBases(),bb2.getLogicalBases()); - Console.WriteLine("addx: {0}",BitFormat.toString(addx.getLogicalBases())); + Console.WriteLine("addx: {0}",BitFormat.toString(addx.getLogicalBases())); ShiftLeft shl = new ShiftLeft(addx.getLogicalBases(0,addx.getBits()-1),4); Console.WriteLine("shl: {0}",BitFormat.toString(shl.getLogicalBases())); - ShiftRight shr = new ShiftRight(shl.getLogicalBases(),4); + ShiftArithmeticRight shar = new ShiftArithmeticRight(shl.getLogicalBases(),2); + Console.WriteLine("shar: {0}",BitFormat.toString(shar.getLogicalBases())); + + ShiftRight shr = new ShiftRight(shar.getLogicalBases(),2); Console.WriteLine("shr: {0}",BitFormat.toString(shr.getLogicalBases())); + Invert inv = new Invert(shr.getLogicalBases()); + Console.WriteLine("inv: {0}",BitFormat.toString(inv.getLogicalBases())); + + BComplement bc = new BComplement(inv.getLogicalBases()); + Console.WriteLine("bcomp: {0}",BitFormat.toString(bc.getLogicalBases())); + + RotateLeft rol = new RotateLeft(bc.getLogicalBases(),8); + Console.WriteLine("rol: {0}",BitFormat.toString(rol.getLogicalBases())); + + RotateRight ror = new RotateRight(rol.getLogicalBases(),19); + Console.WriteLine("ror: {0}",BitFormat.toString(ror.getLogicalBases())); + + + BitFormat.dumpBitIdentity(ror.getLogicalBase(0).identity()); + Console.WriteLine("test ended. ---------------------"); } diff --git a/hwo.bitworks/RotateLeft.cs b/hwo.bitworks/RotateLeft.cs new file mode 100644 index 0000000..3726697 --- /dev/null +++ b/hwo.bitworks/RotateLeft.cs @@ -0,0 +1,32 @@ +using System; +namespace hwo.bitworks +{ + public class RotateLeft : MultipleBitsSource + { + int n; + + public RotateLeft(LogicBase[] sources,int n) + :base(sources) + { + this.n = n; + } + + public override bool bitValue(int bit) + { + int m = (bit - n) % getBits(); + if (m < 0) + m += getBits(); + + return sources[m].value(); + } + + public override identity.BitIdentity getBitIdentity(int bit) + { + int m = (bit - n) % getBits(); + if (m < 0) + m += getBits(); + + return sources[m].identity(); + } + } +} diff --git a/hwo.bitworks/RotateRight.cs b/hwo.bitworks/RotateRight.cs new file mode 100644 index 0000000..17e9438 --- /dev/null +++ b/hwo.bitworks/RotateRight.cs @@ -0,0 +1,32 @@ +using System; +namespace hwo.bitworks +{ + public class RotateRight : MultipleBitsSource + { + int n; + + public RotateRight(LogicBase[] sources,int n) + :base(sources) + { + this.n = n; + } + + public override bool bitValue(int bit) + { + int m = (bit + n) % getBits(); + if (m < 0) + m += getBits(); + + return sources[m].value(); + } + + public override identity.BitIdentity getBitIdentity(int bit) + { + int m = (bit + n) % getBits(); + if (m < 0) + m += getBits(); + + return sources[m].identity(); + } + } +} diff --git a/hwo.bitworks/ShiftArithmeticLeft.cs b/hwo.bitworks/ShiftArithmeticLeft.cs new file mode 100644 index 0000000..d70ca3d --- /dev/null +++ b/hwo.bitworks/ShiftArithmeticLeft.cs @@ -0,0 +1,36 @@ +using System; +namespace hwo.bitworks +{ + public class ShiftArithmeticRight : MultipleBitsSource + { + int n; + + public ShiftArithmeticRight(LogicBase[] sources,int n) + :base(sources) + { + this.n = n; + } + + public override bool bitValue(int bit) + { + if (bit < (getBits()-this.n)){ + return sources[bit + this.n].value(); + } else if (bit < getBits()){ + return sources[getBits()-1].value(); + }; + + throw new IndexOutOfRangeException(String.Format("ShiftRight supplies {0} bits, but bit #{1} requested",getBits(),bit)); + } + + public override identity.BitIdentity getBitIdentity(int bit) + { + if (bit < (getBits()-this.n)){ + return sources[bit + this.n].identity(); + } else if (bit < getBits()){ + return sources[getBits()-1].identity(); + }; + + throw new IndexOutOfRangeException(String.Format("ShiftRight supplies {0} bits, but bit #{1} requested",getBits(),bit)); + } + } +} diff --git a/hwo.bitworks/ShiftLeft.cs b/hwo.bitworks/ShiftLeft.cs index 18cd8aa..2fdc06d 100644 --- a/hwo.bitworks/ShiftLeft.cs +++ b/hwo.bitworks/ShiftLeft.cs @@ -20,5 +20,16 @@ namespace hwo.bitworks } throw new IndexOutOfRangeException(String.Format("ShiftLeft supplies {0} bits, but bit #{1} requested",getBits(),bit)); } + + public override identity.BitIdentity getBitIdentity(int bit) + { + if (bit < this.n){ + return Constants.ZERO.identity(); + } else if (bit < getBits()){ + return sources[bit - this.n].identity(); + } + throw new IndexOutOfRangeException(String.Format("ShiftLeft supplies {0} bits, but bit #{1} requested",getBits(),bit)); + } + } } diff --git a/hwo.bitworks/ShiftRight.cs b/hwo.bitworks/ShiftRight.cs index 05783eb..bd6749d 100644 --- a/hwo.bitworks/ShiftRight.cs +++ b/hwo.bitworks/ShiftRight.cs @@ -21,5 +21,15 @@ namespace hwo.bitworks throw new IndexOutOfRangeException(String.Format("ShiftRight supplies {0} bits, but bit #{1} requested",getBits(),bit)); } + + public override identity.BitIdentity getBitIdentity(int bit) + { + if (bit < (getBits()-this.n)){ + return sources[bit + this.n].identity(); + } else if (bit < getBits()){ + return Constants.ZERO.identity(); + }; + throw new IndexOutOfRangeException(String.Format("ShiftLeft supplies {0} bits, but bit #{1} requested",getBits(),bit)); + } } } diff --git a/hwo.bitworks/XOR.cs b/hwo.bitworks/XOR.cs index b46ed79..71bd9a5 100644 --- a/hwo.bitworks/XOR.cs +++ b/hwo.bitworks/XOR.cs @@ -1,4 +1,6 @@ using System; +using hwo.bitworks.identity; + namespace hwo.bitworks { public class XOR : LogicBase @@ -21,5 +23,9 @@ namespace hwo.bitworks return r; } + public override BitIdentity identity() + { + return new BitIdentity(BitOperation.XOR,identities(this.sources)); + } } } diff --git a/hwo.bitworks/hwo.bitworks.csproj b/hwo.bitworks/hwo.bitworks.csproj index 9db6ae4..4b2eb01 100644 --- a/hwo.bitworks/hwo.bitworks.csproj +++ b/hwo.bitworks/hwo.bitworks.csproj @@ -45,9 +45,22 @@ - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hwo.bitworks/identity/BitIdentity.cs b/hwo.bitworks/identity/BitIdentity.cs new file mode 100644 index 0000000..6c7c572 --- /dev/null +++ b/hwo.bitworks/identity/BitIdentity.cs @@ -0,0 +1,37 @@ +using System; +namespace hwo.bitworks.identity +{ + public class BitIdentity + { + BitOperation operation; + BitIdentity[] sources; + string name; + + public BitIdentity(BitOperation oper,BitIdentity[] sources) + { + this.operation = oper; + this.sources = sources; + this.name = null; + } + + public BitIdentity(string name){ + operation = BitOperation.NONE; + sources = null; + this.name = name; + } + + public BitOperation BitOperation { + get { return this.operation; } + } + public BitIdentity[] Sources { + get { return this.sources; } + } + + public string Name { + get { + return this.name; + } + } + + } +} diff --git a/hwo.bitworks/identity/BitOperation.cs b/hwo.bitworks/identity/BitOperation.cs new file mode 100644 index 0000000..dbbd7b7 --- /dev/null +++ b/hwo.bitworks/identity/BitOperation.cs @@ -0,0 +1,8 @@ +using System; +namespace hwo.bitworks.identity +{ + public enum BitOperation + { + NONE,NOT,AND,OR,SHL,SHR,SHAR,NAND,NOR,XOR + } +} diff --git a/hwo.bitworks/io/BitFormat.cs b/hwo.bitworks/io/BitFormat.cs new file mode 100644 index 0000000..93da9c3 --- /dev/null +++ b/hwo.bitworks/io/BitFormat.cs @@ -0,0 +1,72 @@ +using System; + +using hwo.bitworks.identity; + +namespace hwo.bitworks.io +{ + public static class BitFormat + { + + public static Int64 toInt64(LogicBase[] sources){ + Int64 i64 = 0; + bool neg = sources[sources.Length-1].value(); + + for (int n=0;n<64;n++){ + if (n < sources.Length){ + if (sources[n].value()){ + i64 |= (1L<0) && ((p%8) == 0)){ + digits[n2++] = ' '; + } + } + return String.Format("{0} {1,12:D} {1:x16}",new string(digits),toInt64(sources)); + } + + + public static string whitespace(int n){ + char[] ws = new char[n]; + + for (int i=0;i