I'm refactoring my 8080 Space Invaders emulator. I've moved some methods into a CPU class and others into a _8080 class. I'm using Visual Studio so there's a MainForm class to. When I run the program now I'm getting a Stack Overflow error on the CPU class declaration in the _8080 class. The thing is, when I remove that to test, I get it on the MainForm declaration and then in the CPU class I get it for the same declarations in that class.
This is the whole of the _8080 class
namespace SpaceInvaders
{
class _8080
{
private CPU cpu = new CPU(); // ERROR IS THROWN HERE **
private MainForm MainForm = new MainForm();
public void StartEmulator()
{
cpu.initialiseEmulator(); // Reset
LoadProgram("invaders.rom"); // Load ROM
cpu.emulateCPU();
}
private bool LoadProgram(string filename)
{
Console.WriteLine("\nLoading: " filename "\n");
//Initialize(); //reset
//load file
byte[] loadedProgramBytes = null;
try
{
loadedProgramBytes = File.ReadAllBytes(filename);
}
catch (Exception Ex)
{
Debug.WriteLine(Ex.ToString());
}
//verify file is not empty
if (loadedProgramBytes == null)
{
Debug.WriteLine("Error loading program. The byte array loaded is null.");
return false;
}
int lSize = loadedProgramBytes.Count();
for (int i = 0; i < lSize; i)
{
cpu.memory[i] = loadedProgramBytes[i]; // Store file into memory.
}
Debug.WriteLine("ROM loaded in successfully.\n");
return true;
}
private void updateScreen(int addr, int val)
{
int x = addr >> 5;
int y = 255 - ((addr & 0x1f) << 3);
for (int bit = 1; bit <= 128; bit <<= 1)
{
//screenBuffer.SetPixel(x, y--, (bit & val) != 0 ? Color.White : Color.Black);
}
//Bitmap clone = (Bitmap)screenBuffer.Clone();
//MainForm.pictureBox1.BackgroundImage = clone;
}
}
}
The exception thrown is
System.StackOverflowException HResult=0x800703E9 Message=Exception of type 'System.StackOverflowException' was thrown.
Anyone know how to fix this? I'm new to the site so not sure how things work, posting long class text etc. I don't want to overload people with code but at the same time I understand that without enough information it's hard to help. If it helps I've added three Pastebin's of each of the classes in the program below.
https://pastebin.com/gYhhQd5D << MainForm Class
https://pastebin.com/MhdGwanm << _8080 Class
https://pastebin.com/T9t6dNjL << CPU Class
Thanks.
CodePudding user response:
The CPU Constructor is calling the MainForm constructor, which is calling the CPU constructor which is...
It's most likely due to the infinite loop of constructor calls.
For example
class _8080 {
private CPU cpu;
private MainForm MainForm;
public _8080 (CPU cpu, MainForm, mainForm) {
this.cpu = cpu;
this.MainForm = mainform;
}
}
apply this to the CPU and MainForm class as well
CodePudding user response:
Remove :
private _8080 _8080 = new _8080();
private MainForm MainForm = new MainForm();
from the CPU class (for the paste bin code you aren't using them there)
In 8080 your code uses the cpu and the mainform
In Mainform your code uses (comented) the 8080
So, in Mainform create the global cpu and pass it to the other constructors
private CPU cpu = new CPU(); //Remember you removed from cpu class the 2 lines with the new ....
private _8080 _8080 = new _8080(this, cpu);
public MainForm()
{
InitializeComponent();
}
[...]
And the 8080 class remove the inizialitations and add a constructor:
private CPU cpu;
private MainForm MainForm;
public 8080( MainForm _MainForm, CPU _cpu)
{
cpu = _cpu;
MainForm = _MainForm;
}
// Note that for minimizing the code changes I'm using incorrectly the naming conventions., don't do it at home guys