Added Read/Write function to MC
This commit is contained in:
@@ -4,9 +4,38 @@ namespace CPUSimulation
|
||||
{
|
||||
public class CPU
|
||||
{
|
||||
public void Main(string[] args)
|
||||
static Random _random = new Random();
|
||||
static MC _memoryController;
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, CPU Simulation!");
|
||||
_memoryController = new MC(8, 2048);
|
||||
for (int i = 0; i < 2049; i++)
|
||||
{
|
||||
Word dataToWrite = GenerateRandomWord(_random.Next(1, 9));
|
||||
_memoryController.Write(i, dataToWrite);
|
||||
Word dataRead = _memoryController.Read(i);
|
||||
Console.WriteLine($"Data read from address {i}: {dataRead.ToString()}");
|
||||
}
|
||||
}
|
||||
|
||||
private static Word GenerateRandomWord(int byteCount)
|
||||
{
|
||||
//Console.WriteLine($"Generating random word with {byteCount} bytes.");
|
||||
Byte[] bytes = new Byte[byteCount];
|
||||
for (int i = 0; i < byteCount; i++)
|
||||
{
|
||||
bytes[i] = GenerateRandomByte();
|
||||
}
|
||||
return new Word(bytes);
|
||||
}
|
||||
private static Byte GenerateRandomByte()
|
||||
{
|
||||
Bit[] bits = new Bit[8];
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
bits[i] = new Bit(_random.Next(2) == 1);
|
||||
}
|
||||
return new Byte(bits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,86 @@ namespace CPUSimulation
|
||||
{
|
||||
public class MC
|
||||
{
|
||||
string _name;
|
||||
public MC(string name)
|
||||
|
||||
MemoryBank[] _memoryBanks;
|
||||
int _numberOfMemoryBanks;
|
||||
int _memoryBankSize;
|
||||
|
||||
public MC(int numberOfMemoryBanks, int memoryBankSize = 1024)
|
||||
{
|
||||
_name = name;
|
||||
_numberOfMemoryBanks = numberOfMemoryBanks;
|
||||
_memoryBankSize = memoryBankSize;
|
||||
Bit bit = new Bit();
|
||||
Byte thisByte = new Byte(new Bit[8] { bit, bit, bit, bit, bit, bit, bit, bit });
|
||||
|
||||
_memoryBanks = new MemoryBank[numberOfMemoryBanks];
|
||||
|
||||
for(int i = 0; i < numberOfMemoryBanks; i++)
|
||||
{
|
||||
Byte[] newMemoryBank = new Byte[memoryBankSize];
|
||||
for(int j = 0; j < memoryBankSize; j++)
|
||||
{
|
||||
newMemoryBank[j] = thisByte;
|
||||
}
|
||||
_memoryBanks[i] = new MemoryBank(newMemoryBank);
|
||||
}
|
||||
}
|
||||
|
||||
public Word Read(int address)
|
||||
{
|
||||
AddressOkay(address);
|
||||
Byte[] byteData = new Byte[_numberOfMemoryBanks];
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
byteData[i] = _memoryBanks[i].Read((byte)address);
|
||||
}
|
||||
return new Word(byteData);
|
||||
}
|
||||
|
||||
public void Write(int address, Word data)
|
||||
{
|
||||
AddressOkay(address);
|
||||
Byte[] byteData = data.GetBytes();
|
||||
int byteCount = data.GetByteCount();
|
||||
Byte[] bytesToWrite;
|
||||
if (byteCount < _numberOfMemoryBanks)
|
||||
{
|
||||
int numberOfPaddingBytes = _numberOfMemoryBanks - byteCount;
|
||||
bytesToWrite = new Byte[_numberOfMemoryBanks];
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
if (i < numberOfPaddingBytes)
|
||||
{
|
||||
bytesToWrite[i] = new Byte(new Bit[8]{ new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false) });
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite[i] = byteData[i - numberOfPaddingBytes];
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (data.GetByteCount() > _numberOfMemoryBanks)
|
||||
{
|
||||
throw new ArgumentException($"Data must consist of {_numberOfMemoryBanks} bytes or less.");
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite = byteData;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
_memoryBanks[i].Write((byte)address, bytesToWrite[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddressOkay(int address)
|
||||
{
|
||||
if (address < 0 || address >= _memoryBankSize)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(address), $"Address must be between 0 and {_memoryBankSize - 1}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,85 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"runtimeTarget": {
|
||||
"name": ".NETCoreApp,Version=v10.0",
|
||||
"signature": ""
|
||||
},
|
||||
"compilationOptions": {},
|
||||
"targets": {
|
||||
".NETCoreApp,Version=v10.0": {
|
||||
"CPUSimulation/1.0.0": {
|
||||
"runtime": {
|
||||
"CPUSimulation.dll": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"libraries": {
|
||||
"CPUSimulation/1.0.0": {
|
||||
"type": "project",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"runtimeOptions": {
|
||||
"tfm": "net10.0",
|
||||
"framework": {
|
||||
"name": "Microsoft.NETCore.App",
|
||||
"version": "10.0.0"
|
||||
},
|
||||
"configProperties": {
|
||||
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0be1f139ef3c991f2a1945aef3e923154744c8a7")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
8cc2d01c0c88fb895e41bdcc39b7cca3750604405cfd8368af957ff013a48636
|
||||
06ad3fa32c2a2b0302e0bbe519d5f080612e42cca4243f7546cf9621bf5b5d3c
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ee6baa5db2772c0e2c5d383021abedaeebee16b3198cd6e6a57be209e6f61557
|
||||
@@ -0,0 +1,14 @@
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.GeneratedMSBuildEditorConfig.editorconfig
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.AssemblyInfoInputs.cache
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.AssemblyInfo.cs
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.csproj.CoreCompileInputs.cache
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.exe
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.deps.json
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.runtimeconfig.json
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.dll
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.pdb
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.dll
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\refint\CPUSimulation.dll
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.pdb
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.genruntimeconfig.cache
|
||||
C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\ref\CPUSimulation.dll
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
c421d8a3d43a99866e74ff534ad991e6de3bb29db540eb835e6bc3b54b358471
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user