bitworks/hwo.bitworks/io/BitFormat.cs

73 lines
1.5 KiB
C#

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