Converted address to 16-bit binary string rather than byte.

This commit is contained in:
2026-05-31 15:29:49 -04:00
parent 8cf9e2c5f5
commit 9efadc3b4a
13 changed files with 81 additions and 17 deletions
+6 -6
View File
@@ -8,13 +8,13 @@ namespace CPUSimulation
static MC _memoryController; static MC _memoryController;
public static void Main(string[] args) public static void Main(string[] args)
{ {
_memoryController = new MC(8, 2048); _memoryController = new MC(4, 32768);
for (int i = 0; i < 2049; i++) for (int i = 0; i < 32768; i++)
{ {
Word dataToWrite = GenerateRandomWord(_random.Next(1, 9)); Word dataToWrite = GenerateRandomWord(_random.Next(1, 5));
_memoryController.Write(i, dataToWrite); _memoryController.Write((Int16)i, dataToWrite);
Word dataRead = _memoryController.Read(i); Word dataRead = _memoryController.Read((Int16)i);
Console.WriteLine($"Data read from address {i}: {dataRead.ToString()}"); Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
} }
} }
+5 -5
View File
@@ -27,18 +27,18 @@ namespace CPUSimulation
} }
} }
public Word Read(int address) public Word Read(Int16 address)
{ {
AddressOkay(address); AddressOkay(address);
Byte[] byteData = new Byte[_numberOfMemoryBanks]; Byte[] byteData = new Byte[_numberOfMemoryBanks];
for (int i = 0; i < _numberOfMemoryBanks; i++) for (int i = 0; i < _numberOfMemoryBanks; i++)
{ {
byteData[i] = _memoryBanks[i].Read((byte)address); byteData[i] = _memoryBanks[i].Read(new Address(address).ToString());
} }
return new Word(byteData); return new Word(byteData);
} }
public void Write(int address, Word data) public void Write(Int16 address, Word data)
{ {
AddressOkay(address); AddressOkay(address);
Byte[] byteData = data.GetBytes(); Byte[] byteData = data.GetBytes();
@@ -72,11 +72,11 @@ namespace CPUSimulation
for (int i = 0; i < _numberOfMemoryBanks; i++) for (int i = 0; i < _numberOfMemoryBanks; i++)
{ {
_memoryBanks[i].Write((byte)address, bytesToWrite[i]); _memoryBanks[i].Write(new Address(address).ToString(), bytesToWrite[i]);
} }
} }
private void AddressOkay(int address) private void AddressOkay(Int16 address)
{ {
if (address < 0 || address >= _memoryBankSize) if (address < 0 || address >= _memoryBankSize)
{ {
+68 -4
View File
@@ -34,23 +34,87 @@ namespace CPUSimulation
public class MemoryBank public class MemoryBank
{ {
Byte[] _bytes; Dictionary<string, Byte> _bytes;
public MemoryBank(Byte[] bytes) public MemoryBank(Byte[] bytes)
{ {
_bytes = 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(byte address) public Byte Read(string address)
{ {
return _bytes[address]; return _bytes[address];
} }
public void Write(byte address, Byte data) public void Write(string address, Byte data)
{ {
_bytes[address] = 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;
}
}
public class Word public class Word
{ {
Byte[] _bytes; Byte[] _bytes;
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0be1f139ef3c991f2a1945aef3e923154744c8a7")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8cf9e2c5f5ac52e9ad7b40d2f046e9d08850a297")]
[assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
06ad3fa32c2a2b0302e0bbe519d5f080612e42cca4243f7546cf9621bf5b5d3c f5b5f51936326916e2fb5d1d37c6294b5a3a803b598c90e6e30bc1bd1c0e6dc7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.