Initial Commit

master
Harald Wolff 2017-06-14 11:25:16 +02:00
commit 664c4d4a90
19 changed files with 625 additions and 0 deletions

40
.gitignore vendored 100644
View File

@ -0,0 +1,40 @@
# Autosave files
*~
# build
[Oo]bj/
[Bb]in/
packages/
TestResults/
# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
# Mac bundle stuff
*.dmg
*.app
# resharper
*_Resharper.*
*.Resharper
# dotCover
*.dotCover

23
bitworks.sln 100644
View File

@ -0,0 +1,23 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hwo.bitworks", "hwo.bitworks\hwo.bitworks.csproj", "{07EE5432-46BD-4FFD-85D4-04917894FD12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{07EE5432-46BD-4FFD-85D4-04917894FD12}.Debug|x86.ActiveCfg = Debug|x86
{07EE5432-46BD-4FFD-85D4-04917894FD12}.Debug|x86.Build.0 = Debug|x86
{07EE5432-46BD-4FFD-85D4-04917894FD12}.Release|x86.ActiveCfg = Release|x86
{07EE5432-46BD-4FFD-85D4-04917894FD12}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = PrefixedHierarchical
$1.ResourceNamePolicy = FileName
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,36 @@
using System;
namespace hwo.bitworks
{
public class ADD : MultipleBitsSource
{
XOR xor2,xor3;
AND and12,andC;
OR or;
public ADD(LogicBase source1,LogicBase source2,LogicBase carry)
{
and12 = new AND(source1,source2);
xor2 = new XOR(source1,source2);
andC = new AND(carry,xor2);
or = new OR(andC,and12);
xor3 = new XOR(source1,source2,carry);
}
public override bool bitValue(int bit)
{
switch (bit){
case 1:
return or.value();
case 0:
return xor3.value();
default:
throw new IndexOutOfRangeException(String.Format("ADD supplies 2 bits, but bit #{0} requested",bit));
}
}
}
}

View File

@ -0,0 +1,52 @@
using System;
namespace hwo.bitworks
{
public class ADDX : MultipleBitsSource
{
LogicBase[] srcA,srcB;
ADD[] add;
public ADDX(LogicBase[] sourcesA,LogicBase[] sourcesB)
{
int l = sourcesA.Length > sourcesB.Length ? sourcesA.Length : sourcesB.Length;
srcA = new LogicBase[l];
srcB = new LogicBase[l];
add = new ADD[l];
Array.Copy(sourcesA,srcA,sourcesA.Length);
Array.Copy(sourcesB,srcB,sourcesB.Length);
for (int n=sourcesA.Length; n < l; n++){
srcA[n] = Constants.ZERO;
}
for (int n=sourcesB.Length; n < l; n++){
srcB[n] = Constants.ZERO;
}
LogicBase lb = Constants.ZERO;
for (int n=0;n<add.Length;n++){
add[n] = new ADD(srcA[n],srcB[n],lb);
lb = add[n].getLogicalBase(1);
}
}
public override int getBits()
{
return add.Length + 1;
}
public override bool bitValue(int bit)
{
if (bit > add.Length){
throw new IndexOutOfRangeException(String.Format("ADDX supplies {0} bits, but bit #{1} requested",add.Length+1,bit));
} else if (bit == add.Length){
return add[bit-1].bitValue(1);
} else {
return add[bit].bitValue(0);
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace hwo.bitworks
{
public class AND : LogicBase
{
public AND(params LogicBase[] sources)
:base(sources){
}
public override bool value()
{
foreach (LogicBase source in this.sources){
if (!source.value()){
return false;
}
}
return true;
}
}
}

View File

@ -0,0 +1,41 @@
using System;
namespace hwo.bitworks
{
public class BitBuffer : MultipleBitsSource
{
bool[] bits;
public BitBuffer(int len)
{
this.bits = new bool[len];
}
public override int getBits()
{
return bits.Length;
}
public void zero(){
for (int n=0;n<bits.Length;n++)
bits[n] = false;
}
public void set(Int32 i){
zero();
int w = 32 < getBits() ? 32 : getBits();
for (int n=0;n<w;n++){
bits[n] = (i & (1<<n)) == 0 ? false : true;
}
}
public override bool bitValue(int bit)
{
return this.bits[ bit ];
}
}
}

View File

@ -0,0 +1,17 @@
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

@ -0,0 +1,32 @@
using System;
namespace hwo.bitworks
{
public static class Constants {
public static readonly LogicBase ZERO = new ZERO();
public static readonly LogicBase ONE = new ONE();
}
class ZERO : LogicBase
{
public ZERO():base(new LogicBase[0]){
}
public override bool value()
{
return false;
}
}
class ONE : LogicBase
{
public ONE():base(new LogicBase[0]){
}
public override bool value()
{
return true;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
namespace hwo.bitworks
{
public class LogicBase
{
protected LogicBase[] sources;
public LogicBase(LogicBase[] sources)
{
this.sources = new LogicBase[sources.Length];
Array.Copy(sources,this.sources,sources.Length);
}
public virtual bool value(){
throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'bool value()'",this.GetType().FullName));
}
}
}

View File

@ -0,0 +1,63 @@
using System;
namespace hwo.bitworks
{
public class MultipleBitsSource
{
protected LogicBase[] sources;
public MultipleBitsSource()
{
this.sources = null;
}
public MultipleBitsSource(LogicBase[] sources)
{
this.sources = sources;
}
public virtual int getBits(){
return sources.Length;
}
public virtual bool bitValue(int bit){
throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'bool bitValue(int bit)'",this.GetType().FullName));
}
public LogicBase getLogicalBase(int bit){
return new BIT(this,bit);
}
public LogicBase[] getLogicalBases(){
return getLogicalBases(0,getBits());
}
public LogicBase[] getLogicalBases(int start,int len){
LogicBase[] lb = new LogicBase[len];
for (int n=0;n<len;n++){
lb[n] = getLogicalBase(n + start);
}
return lb;
}
public class BIT : LogicBase {
MultipleBitsSource source;
int bit;
public BIT(MultipleBitsSource source,int bit)
:base(new LogicBase[0])
{
this.source = source;
this.bit = bit;
}
public override bool value()
{
return source.bitValue(this.bit);
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace hwo.bitworks
{
public class NAND : LogicBase
{
public NAND(LogicBase[] sources)
:base(sources){
}
public override bool value()
{
foreach (LogicBase source in this.sources){
if (!source.value()){
return true;
}
}
return false;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
namespace hwo.bitworks
{
public class NOR : LogicBase
{
public NOR(LogicBase[] sources)
:base(sources)
{
}
public override bool value()
{
foreach (LogicBase source in this.sources){
if (source.value()){
return false;
}
}
return true;
}
}
}

21
hwo.bitworks/OR.cs 100644
View File

@ -0,0 +1,21 @@
using System;
namespace hwo.bitworks
{
public class OR : LogicBase
{
public OR(params LogicBase[] sources)
:base(sources)
{
}
public override bool value()
{
foreach (LogicBase source in this.sources){
if (source.value()){
return true;
}
}
return false;
}
}
}

View File

@ -0,0 +1,60 @@
using System;
namespace hwo.bitworks
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Give me a little time...");
test8bit();
BitBuffer bb1,bb2;
bb1 = new BitBuffer(32);
bb2 = new BitBuffer(32);
bb1.set(0x00aa00aa);
bb2.set(0x55005500);
Console.WriteLine("bb1: {0}",BitFormat.toString(bb1.getLogicalBases()));
Console.WriteLine("bb2: {0}",BitFormat.toString(bb2.getLogicalBases()));
ADDX addx = new ADDX(bb1.getLogicalBases(),bb2.getLogicalBases());
Console.WriteLine("addx: {0}",BitFormat.toString(addx.getLogicalBases()));
}
public static void test8bit(){
Console.WriteLine("Test: 8bit");
BitBuffer bb1,bb2;
bb1 = new BitBuffer(8);
bb2 = new BitBuffer(8);
bb1.set(0x05);
bb2.set(0x14);
Console.WriteLine("bb1: {0}",BitFormat.toString(bb1.getLogicalBases()));
Console.WriteLine("bb2: {0}",BitFormat.toString(bb2.getLogicalBases()));
ADDX addx = new ADDX(bb1.getLogicalBases(),bb2.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);
Console.WriteLine("shr: {0}",BitFormat.toString(shr.getLogicalBases()));
Console.WriteLine("test ended. ---------------------");
}
}
}

View File

@ -0,0 +1,26 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("hwo.bitworks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View File

@ -0,0 +1,24 @@
using System;
namespace hwo.bitworks
{
public class ShiftLeft : MultipleBitsSource
{
int n;
public ShiftLeft(LogicBase[] sources,int n)
:base(sources)
{
this.n = n;
}
public override bool bitValue(int bit)
{
if (bit < this.n){
return false;
} else if (bit < getBits()){
return sources[bit - this.n].value();
}
throw new IndexOutOfRangeException(String.Format("ShiftLeft supplies {0} bits, but bit #{1} requested",getBits(),bit));
}
}
}

View File

@ -0,0 +1,25 @@
using System;
namespace hwo.bitworks
{
public class ShiftRight : MultipleBitsSource
{
int n;
public ShiftRight(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 false;
};
throw new IndexOutOfRangeException(String.Format("ShiftRight supplies {0} bits, but bit #{1} requested",getBits(),bit));
}
}
}

View File

@ -0,0 +1,25 @@
using System;
namespace hwo.bitworks
{
public class XOR : LogicBase
{
public XOR(params LogicBase[] sources)
:base(sources)
{
}
public override bool value()
{
bool r = false;
foreach (LogicBase source in sources){
if (source.value()){
r = !r;
}
}
return r;
}
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{07EE5432-46BD-4FFD-85D4-04917894FD12}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>hwo.bitworks</RootNamespace>
<AssemblyName>hwo.bitworks</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LogicBase.cs" />
<Compile Include="OR.cs" />
<Compile Include="NOR.cs" />
<Compile Include="AND.cs" />
<Compile Include="NAND.cs" />
<Compile Include="BitBuffer.cs" />
<Compile Include="MultipleBitsSource.cs" />
<Compile Include="ADD.cs" />
<Compile Include="XOR.cs" />
<Compile Include="ADDX.cs" />
<Compile Include="Constants.cs" />
<Compile Include="BitFormat.cs" />
<Compile Include="ShiftLeft.cs" />
<Compile Include="ShiftRight.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>