Implemented a load more Z80 OpCodes. Added a breakpoint to the debugger
This commit is contained in:
40
Desktop/DebuggerForm.Designer.cs
generated
40
Desktop/DebuggerForm.Designer.cs
generated
@@ -44,6 +44,10 @@
|
||||
lstDisassembly = new ListBox();
|
||||
lstStack = new ListBox();
|
||||
btnExit = new Button();
|
||||
saveFileDialog1 = new SaveFileDialog();
|
||||
label1 = new Label();
|
||||
txtBreakpoint = new TextBox();
|
||||
label2 = new Label();
|
||||
SuspendLayout();
|
||||
//
|
||||
// lblAF
|
||||
@@ -128,7 +132,7 @@
|
||||
//
|
||||
// txtMemoryStart
|
||||
//
|
||||
txtMemoryStart.Location = new Point(238, 10);
|
||||
txtMemoryStart.Location = new Point(300, 21);
|
||||
txtMemoryStart.Margin = new Padding(2);
|
||||
txtMemoryStart.Name = "txtMemoryStart";
|
||||
txtMemoryStart.Size = new Size(121, 27);
|
||||
@@ -161,7 +165,7 @@
|
||||
//
|
||||
// btnRefreshMemory
|
||||
//
|
||||
btnRefreshMemory.Location = new Point(376, 7);
|
||||
btnRefreshMemory.Location = new Point(425, 21);
|
||||
btnRefreshMemory.Margin = new Padding(2);
|
||||
btnRefreshMemory.Name = "btnRefreshMemory";
|
||||
btnRefreshMemory.Size = new Size(90, 27);
|
||||
@@ -208,11 +212,39 @@
|
||||
btnExit.UseVisualStyleBackColor = true;
|
||||
btnExit.Click += btnExit_Click;
|
||||
//
|
||||
// label1
|
||||
//
|
||||
label1.AutoSize = true;
|
||||
label1.Location = new Point(289, 288);
|
||||
label1.Name = "label1";
|
||||
label1.Size = new Size(81, 20);
|
||||
label1.TabIndex = 19;
|
||||
label1.Text = "Breakpoint";
|
||||
//
|
||||
// txtBreakpoint
|
||||
//
|
||||
txtBreakpoint.Location = new Point(376, 281);
|
||||
txtBreakpoint.Name = "txtBreakpoint";
|
||||
txtBreakpoint.Size = new Size(125, 27);
|
||||
txtBreakpoint.TabIndex = 20;
|
||||
//
|
||||
// label2
|
||||
//
|
||||
label2.AutoSize = true;
|
||||
label2.Location = new Point(233, 21);
|
||||
label2.Name = "label2";
|
||||
label2.Size = new Size(62, 20);
|
||||
label2.TabIndex = 21;
|
||||
label2.Text = "Address";
|
||||
//
|
||||
// DebuggerForm
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(928, 350);
|
||||
Controls.Add(label2);
|
||||
Controls.Add(txtBreakpoint);
|
||||
Controls.Add(label1);
|
||||
Controls.Add(btnExit);
|
||||
Controls.Add(lstStack);
|
||||
Controls.Add(lstDisassembly);
|
||||
@@ -254,6 +286,10 @@
|
||||
private ListBox lstStack;
|
||||
private Button btnExit;
|
||||
public ListBox lstDisassembly;
|
||||
private SaveFileDialog saveFileDialog1;
|
||||
private Label label1;
|
||||
private TextBox txtBreakpoint;
|
||||
private Label label2;
|
||||
//private TextBox textBox4;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace Desktop
|
||||
private readonly Z80 _cpu;
|
||||
private readonly MemoryBus _memoryBus;
|
||||
private bool _isRunning = false;
|
||||
private ushort? _breakpoint = null;
|
||||
|
||||
public DebuggerForm(Z80 cpu, MemoryBus memoryBus)
|
||||
{
|
||||
@@ -57,18 +58,45 @@ namespace Desktop
|
||||
return;
|
||||
}
|
||||
|
||||
// --- NEW: Parse the Breakpoint ---
|
||||
if (!string.IsNullOrWhiteSpace(txtBreakpoint.Text))
|
||||
{
|
||||
if (ushort.TryParse(txtBreakpoint.Text, System.Globalization.NumberStyles.HexNumber, null, out ushort parsedBp))
|
||||
{
|
||||
_breakpoint = parsedBp;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Invalid hex address in breakpoint!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_breakpoint = null; // No text means no breakpoint
|
||||
}
|
||||
// ---------------------------------
|
||||
|
||||
// Start the run state
|
||||
_isRunning = true;
|
||||
btnRun.Text = "Stop";
|
||||
|
||||
// Fire up a background thread so the Windows UI doesn't freeze
|
||||
// Fire up a background thread
|
||||
await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Free-run the CPU until the flag is flipped or it crashes!
|
||||
while (_isRunning)
|
||||
{
|
||||
// --- NEW: Breakpoint Check ---
|
||||
// We check BEFORE stepping so it stops exactly on the instruction
|
||||
if (_breakpoint.HasValue && _cpu.PC == _breakpoint.Value)
|
||||
{
|
||||
_isRunning = false;
|
||||
break; // Cleanly exit the while loop
|
||||
}
|
||||
// -----------------------------
|
||||
|
||||
_cpu.Step();
|
||||
}
|
||||
}
|
||||
@@ -76,22 +104,17 @@ namespace Desktop
|
||||
{
|
||||
_isRunning = false;
|
||||
|
||||
// We are on a background thread. We MUST use Invoke to tell the
|
||||
// main UI thread to update the labels and show the message box!
|
||||
this.Invoke((MethodInvoker)delegate
|
||||
{
|
||||
btnRun.Text = "Run";
|
||||
UpdateDisplay();
|
||||
MessageBox.Show(ex.Message, "CPU Break", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// If the user clicked Stop manually (no exception thrown), we still want to update the UI
|
||||
if (!_isRunning)
|
||||
{
|
||||
UpdateDisplay();
|
||||
}
|
||||
// Whether it stopped because of a breakpoint, a crash, or the user clicking "Stop",
|
||||
// we MUST update the UI when the background thread finishes!
|
||||
btnRun.Text = "Run";
|
||||
UpdateDisplay();
|
||||
}
|
||||
|
||||
private void btnExit_Click(object sender, EventArgs e)
|
||||
@@ -186,8 +209,6 @@ namespace Desktop
|
||||
{
|
||||
lstDisassembly.Items.Clear();
|
||||
|
||||
// THIS is the critical link! It forces the top of the list
|
||||
// to always be exactly where the CPU currently is.
|
||||
ushort currentPc = _cpu.PC;
|
||||
|
||||
int instructionsToShow = 8;
|
||||
@@ -220,6 +241,12 @@ namespace Desktop
|
||||
case 0x23:
|
||||
mnemonic = "INC HL";
|
||||
break;
|
||||
case 0x28:
|
||||
sbyte jrZOffset = (sbyte)_memoryBus.Read((ushort)(currentPc + 1));
|
||||
ushort jrZDest = (ushort)(currentPc + 2 + jrZOffset);
|
||||
mnemonic = $"JR Z, 0x{jrZDest:X4}";
|
||||
instructionLength = 2;
|
||||
break;
|
||||
case 0x2B:
|
||||
mnemonic = "DEC HL";
|
||||
break;
|
||||
@@ -229,6 +256,9 @@ namespace Desktop
|
||||
mnemonic = $"JR NC, 0x{dest:X4}";
|
||||
instructionLength = 2;
|
||||
break;
|
||||
case 0x35:
|
||||
mnemonic = "DEC (HL)";
|
||||
break;
|
||||
case 0x36:
|
||||
byte memValue = _memoryBus.Read((ushort)(currentPc + 1));
|
||||
mnemonic = $"LD (HL), 0x{memValue:X2}";
|
||||
@@ -268,6 +298,9 @@ namespace Desktop
|
||||
mnemonic = $"OUT (0x{outPort:X2}), A";
|
||||
instructionLength = 2;
|
||||
break;
|
||||
case 0xD9:
|
||||
mnemonic = "EXX";
|
||||
break;
|
||||
case 0xDE:
|
||||
byte sbcValue = _memoryBus.Read((ushort)(currentPc + 1));
|
||||
mnemonic = $"SBC A, 0x{sbcValue:X2}";
|
||||
|
||||
@@ -117,4 +117,7 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
</root>
|
||||
Reference in New Issue
Block a user