Skip to content

Commit

Permalink
#8 Work on debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Oct 12, 2019
1 parent 0c34028 commit a272dd2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions ComputerSystems/Commodore64/FormC64Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Timer = System.Threading.Timer;
using Debugger;
using System.Threading.Tasks;
using MicroProcessor.Cpu6502;

namespace ComputerSystem.Commodore64 {
public partial class FormC64Screen : Form {
Expand Down
1 change: 1 addition & 0 deletions Debugger/Debugger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Design" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
26 changes: 26 additions & 0 deletions Debugger/FormDebugger.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 53 additions & 2 deletions Debugger/FormDebugger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Windows.Forms;
using System.Windows.Forms;
using System.Globalization;
using System;
using MicroProcessor.Cpu6502;
using Hardware.Memory;
using Memory;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;

namespace Debugger {
public partial class FormDebugger : Form {
Expand All @@ -14,6 +17,12 @@ public partial class FormDebugger : Form {

private ConcurrentDictionary<int, WatchItem> _watchItems = new ConcurrentDictionary<int, WatchItem>();

private SynchronizedCollection<OpCode> lastOpCodes = new SynchronizedCollection<OpCode>();

private SolidBrush b = new SolidBrush(Color.Black);
private Font f = new Font("Consolas", 10);

private Thread t;

public FormDebugger(Cpu cpu, IMemory<byte> memory) {
InitializeComponent();
Expand All @@ -23,8 +32,11 @@ public FormDebugger(Cpu cpu, IMemory<byte> memory) {

_memory.OnWrite += _memory_OnWrite;
_memory.OnRead += _memory_OnRead;
}

UpdateUi();
private void _cpu_OnStep(object sender, OpCode e) {
lastOpCodes.Add(e);
if (lastOpCodes.Count > 10) lastOpCodes.RemoveAt(0);
}

private void _memory_OnWrite(object sender, MemoryWriteEventArgs<byte> e) {
Expand Down Expand Up @@ -93,5 +105,44 @@ private void DgWatch_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
}
}

private void FormDebugger_Load(object sender, EventArgs e) {
_cpu.OnStep += _cpu_OnStep;

t = new Thread(InvalidateScreen);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}

private void InvalidateScreen() {
while (true) {
if (!Visible || WindowState == FormWindowState.Minimized) {
Thread.Sleep(1000);
continue;
}

try {
pictureBox1.Invalidate();

//Invoke(new Action(() => {
// pictureBox1.Invalidate();
//}));

} catch (Exception) {
return;
}

Thread.Sleep((int)(1000.0f / 60.0f));
}
}

private void PictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(pictureBox1.BackColor);

lock (lastOpCodes.SyncRoot) {
for (int i = 0; i < lastOpCodes.Count; i++) {
e.Graphics.DrawString(lastOpCodes?[i]?.ToString() ?? "", f, b, 0, i * f.Height + 3);
}
}
}
}
}
17 changes: 16 additions & 1 deletion MicroProcessor/Cpu6502/Cpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using Hardware.Memory;
using MicroProcessor.Cpu6502.Attributes;
using MicroProcessor.Cpu6502.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MicroProcessor.Cpu6502 {
public class Cpu {

public event EventHandler<OpCode> OnStep;

public const ushort NMI_VECTOR_ADDRESS = 0xFFFA;
public const ushort RESET_VECTOR_ADDRESS = 0xFFFC;
public const ushort IRQ_VECTOR_ADDRESS = 0xFFFE;
Expand Down Expand Up @@ -205,13 +208,25 @@ private void DoCycle() {
/// <param name="ignoreCycles"></param>
public void Step(bool ignoreCycles = false) {
OpCode = OpCodeCache[Memory[PC]];
OpCodeAddress = PC;
OpCode.OpCodeAddress = PC;

var o = OpCode.FromOpCodeDefinitionAttribute(null, null, OpCode);
o.OpCodeAddress = PC;

o.Operands.Clear();
for (int j = 1; j < o.Length; j++) {
o.Operands.Add(Memory[PC + j]);
}

OnStep?.Invoke(this, o);

PC++;

if (OpCode.AddressingMode != AddressingMode.Implied && OpCode.AddressingMode != AddressingMode.Accumulator) {
OpCode.GetAddress();
}
OpCode.Run();

TotalInstructions += 1;

// Count total cycles
Expand Down

0 comments on commit a272dd2

Please sign in to comment.