Files
CPU-Simulation/Models.cs
T

85 lines
1.6 KiB
C#

using System;
namespace CPUSimulation
{
public class Bit
{
bool _value;
public Bit(bool value = false)
{
_value = value;
}
public bool GetValue()
{
return _value;
}
}
public class Byte
{
Bit[] _bits;
public Byte(Bit[] bits)
{
if (bits.Length != 8)
{
throw new ArgumentException("A byte must consist of exactly 8 bits.");
}
_bits = bits;
}
public Bit[] GetBits()
{
return _bits;
}
}
public class MemoryBank
{
Byte[] _bytes;
public MemoryBank(Byte[] bytes)
{
_bytes = bytes;
}
public Byte Read(byte address)
{
return _bytes[address];
}
public void Write(byte address, Byte data)
{
_bytes[address] = data;
}
}
public class Word
{
Byte[] _bytes;
public Word(Byte[] bytes)
{
_bytes = bytes;
}
public int GetByteCount()
{
return _bytes.Length;
}
public Byte[] GetBytes()
{
return _bytes;
}
public override string ToString()
{
string wordString = "";
foreach (Byte b in _bytes)
{
foreach (Bit bit in b.GetBits())
{
wordString += bit.GetValue() ? "1" : "0";
}
}
return wordString;
}
}
}