First Tree

master
Harald Wolff 2017-06-14 15:06:37 +02:00
parent 664c4d4a90
commit c6efb35d68
26 changed files with 506 additions and 27 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<sources.Length;n++){
digits[n] = sources[sources.Length - 1 - n].value() ? '1' : '0';
}
return new string(digits);
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using hwo.bitworks.identity;
namespace hwo.bitworks
{
public static class Constants {
@ -10,9 +12,16 @@ namespace hwo.bitworks
class ZERO : LogicBase
{
private BitIdentity biZERO = new BitIdentity("0");
public ZERO():base(new LogicBase[0]){
}
public override BitIdentity identity()
{
return biZERO;
}
public override bool value()
{
return false;
@ -21,9 +30,16 @@ namespace hwo.bitworks
class ONE : LogicBase
{
private BitIdentity biONE = new BitIdentity("1");
public ONE():base(new LogicBase[0]){
}
public override BitIdentity identity()
{
return biONE;
}
public override bool value()
{
return true;

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
namespace hwo.bitworks
{
public class Identities
{
static Identities identities = new Identities();
public static Identities Instance {
get { return identities; }
}
List<object> idObjects;
protected Identities()
{
idObjects = new List<object>();
}
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];
}
}
}

View File

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

View File

@ -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<sources.Length;n++){
bids[n] = sources[n].identity();
}
return bids;
}
}
}

View File

@ -1,12 +1,26 @@
using System;
using System.Collections.Generic;
using hwo.bitworks.identity;
namespace hwo.bitworks
{
public class MultipleBitsSource
{
static Dictionary<Type,List<MultipleBitsSource>> knownSources = new Dictionary<Type, List<MultipleBitsSource>>();
static protected void register(MultipleBitsSource s){
Type t = s.GetType();
if (!knownSources.ContainsKey(t)){
knownSources.Add(t,new List<MultipleBitsSource>());
}
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);
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -45,9 +45,22 @@
<Compile Include="XOR.cs" />
<Compile Include="ADDX.cs" />
<Compile Include="Constants.cs" />
<Compile Include="BitFormat.cs" />
<Compile Include="ShiftLeft.cs" />
<Compile Include="ShiftRight.cs" />
<Compile Include="ShiftArithmeticLeft.cs" />
<Compile Include="NOT.cs" />
<Compile Include="Invert.cs" />
<Compile Include="BComplement.cs" />
<Compile Include="RotateLeft.cs" />
<Compile Include="RotateRight.cs" />
<Compile Include="Identities.cs" />
<Compile Include="identity\BitIdentity.cs" />
<Compile Include="identity\BitOperation.cs" />
<Compile Include="io\BitFormat.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="identity\" />
<Folder Include="io\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

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

View File

@ -0,0 +1,8 @@
using System;
namespace hwo.bitworks.identity
{
public enum BitOperation
{
NONE,NOT,AND,OR,SHL,SHR,SHAR,NAND,NOR,XOR
}
}

View File

@ -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<<n);
}
} else {
if (neg){
i64 |= (1L<<n);
}
}
}
return i64;
}
public static string toString(LogicBase[] sources){
char[] digits = new char[sources.Length + ((sources.Length-1) / 8)];
int n2 = 0;
for (int n=0;n<sources.Length;n++){
int p = sources.Length - 1 - n;
digits[n2++] = sources[p].value() ? '1' : '0';
if ((p>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<n;i++){
ws[i] = ' ';
}
return new string(ws);
}
public static void dumpBitIdentity(BitIdentity id){
dumpBitIdentity(id,0);
}
public static void dumpBitIdentity(BitIdentity id,int intend){
if (id.Sources == null){
Console.WriteLine("{0} {1}",whitespace(intend*2),id.Name);
} else {
Console.WriteLine("{0} {1} {{",whitespace(intend*2),id.BitOperation.ToString());
foreach (BitIdentity sub in id.Sources){
dumpBitIdentity(sub,intend + 2);
}
Console.WriteLine("{0} }} // {1}",whitespace(intend*2),id.BitOperation.ToString());
}
}
}
}