Files
dbowerman eae5314e3b added a new ToString method to Word, DWord, and QWord classes.
The ToString method will concatenate the string representations of the high and low words (or dwords) to create a single string representation of the entire word.
This can be used to more easily write and read the value from the MC and CU classes.
2026-06-03 09:54:36 -04:00

218 lines
5.5 KiB
C#

using System;
namespace CPUSimulation
{
public class Bit
{
bool _value;
public Bit(bool value = false)
{
_value = value;
}
public Bit(string bitString)
{
if (bitString.Length != 1 || (bitString != "0" && bitString != "1"))
{
throw new ArgumentException("Bit string must be '0' or '1'.");
}
_value = bitString == "1";
}
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 Byte(string bitString)
{
if (bitString.Length != 8)
{
throw new ArgumentException("Bit string must be exactly 8 characters long.");
}
_bits = new Bit[8];
for (int i = 0; i < 8; i++)
{
_bits[i] = new Bit(bitString[i] == '1');
}
}
public Bit[] GetBits()
{
return _bits;
}
}
public class MemoryBank
{
Dictionary<string, Byte> _bytes;
public MemoryBank(Byte[] bytes)
{
_bytes = new Dictionary<string, Byte>();
for (int i = 0; i < bytes.Length; i++)
{
_bytes[new Address((Int16)i).ToString()] = bytes[i];
}
}
public Byte Read(string address)
{
return _bytes[address];
}
public void Write(string address, Byte data)
{
_bytes[address] = data;
}
}
public class Address
{
Byte _highByte;
Byte _lowByte;
public Address(Byte highByte, Byte lowByte)
{
_highByte = highByte;
_lowByte = lowByte;
}
public Address (Int16 address)
{
byte highByteValue = (byte)(address / 256);
byte lowByteValue = (byte)(address % 256);
_highByte = new Byte(GetBitsFromByte(highByteValue));
_lowByte = new Byte(GetBitsFromByte(lowByteValue));
}
public Address(string address)
{
if (address.Length != 16)
{
throw new ArgumentException("Address string must be exactly 16 characters long.");
}
Bit[] highByteBits = new Bit[8];
Bit[] lowByteBits = new Bit[8];
for (int i = 0; i < 8; i++)
{
highByteBits[i] = new Bit(address[i] == '1');
lowByteBits[i] = new Bit(address[i + 8] == '1');
}
_highByte = new Byte(highByteBits);
_lowByte = new Byte(lowByteBits);
}
private Bit[] GetBitsFromByte(byte value)
{
Bit[] bits = new Bit[8];
for (int i = 0; i < 8; i++)
{
bits[7 - i] = new Bit((value & (1 << i)) != 0);
}
return bits;
}
public override string ToString()
{
string addressString = "";
foreach (Bit bit in _highByte.GetBits())
{
addressString += bit.GetValue() ? "1" : "0";
}
foreach (Bit bit in _lowByte.GetBits())
{
addressString += bit.GetValue() ? "1" : "0";
}
return addressString;
}
}
/// <summary>
/// A word is a collection of 2 Bytes
/// </summary>
public class Word
{
Byte[] _bytes;
public Word(Byte[] bytes)
{
if (bytes.Length == 0 || bytes.Length > 2)
{
throw new ArgumentException("A word must consist of 1 or 2 bytes.");
}
else if (bytes.Length == 1)
{
_bytes = new Byte[2] { new Byte("00000000"), bytes[0] };
}
_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;
}
}
/// <summary>
/// A double word is a collection of 2 Words, or 4 Bytes
/// </summary>
public class DWord
{
Word _highWord;
Word _lowWord;
public DWord(Word highWord, Word lowWord)
{
_highWord = highWord;
_lowWord = lowWord;
}
public override string ToString()
{
return _highWord.ToString() + _lowWord.ToString();
}
}
/// <summary>
/// A quad word is a collection of 2 Double Words, or 8 Bytes
/// </summary>
public class QWord
{
DWord _highDWord;
DWord _lowDWord;
public QWord(DWord highDWord, DWord lowDWord)
{
_highDWord = highDWord;
_lowDWord = lowDWord;
}
override public string ToString()
{
return _highDWord.ToString() + _lowDWord.ToString();
}
}
}