From 24273cb3ba1246891ddfae5c685aaf5365752894 Mon Sep 17 00:00:00 2001 From: mjbohn Date: Sun, 29 Jun 2025 11:54:15 +0200 Subject: [PATCH 1/4] add /docs --- docs/index.html | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 docs/index.html diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..c5f2fa1 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,8 @@ + + + sim6502 + + + sim6502 + + From 94c628a2758f9c2284699b6a4916c452d7270b4d Mon Sep 17 00:00:00 2001 From: mjbohn Date: Sun, 13 Jul 2025 23:25:30 +0200 Subject: [PATCH 2/4] refactor class member 'cycles' --- CPU_emu/CPU_emu.CMD_Methods.cs | 20 +++++++++--------- CPU_emu/Class_CPU.cs | 37 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/CPU_emu/CPU_emu.CMD_Methods.cs b/CPU_emu/CPU_emu.CMD_Methods.cs index c9844d2..73e7b6b 100644 --- a/CPU_emu/CPU_emu.CMD_Methods.cs +++ b/CPU_emu/CPU_emu.CMD_Methods.cs @@ -18,21 +18,21 @@ public void Cmd_00() // Load Accumulator immidiate A9 public void Cmd_A9() { - SetRegister("A", FetchByte(ref cycles)); + SetRegister("A", FetchByte(ref _CpuCycle)); SetZeroAndNegativeFlags(A); } // Load Accumulator zeropage A5 public void Cmd_A5() { - SetRegister("A", ReadByteFromMemory(FetchByte(ref cycles))); + SetRegister("A", ReadByteFromMemory(FetchByte(ref _CpuCycle))); SetZeroAndNegativeFlags(A); } // Load Accumulator zeropage X B5 public void Cmd_B5() { - byte b_tmp = FetchByte(ref cycles); + byte b_tmp = FetchByte(ref _CpuCycle); b_tmp += X; // add regX to address SetRegister("A", ReadByteFromMemory(b_tmp)); SetZeroAndNegativeFlags(A); @@ -43,7 +43,7 @@ public void Cmd_B5() // Load X immidiate A2 public void Cmd_A2() { - byte b_tmp = FetchByte(ref cycles); + byte b_tmp = FetchByte(ref _CpuCycle); SetRegister("X", b_tmp); SetZeroAndNegativeFlags(X); } @@ -51,7 +51,7 @@ public void Cmd_A2() // Load X zeropage A6 public void Cmd_A6() { - byte b_tmp = FetchByte(ref cycles); + byte b_tmp = FetchByte(ref _CpuCycle); SetRegister("X", ReadByteFromMemory(b_tmp)); SetZeroAndNegativeFlags(X); } @@ -59,7 +59,7 @@ public void Cmd_A6() // Load X zeropage Y B6 public void Cmd_B6() { - byte b_tmp = FetchByte(ref cycles); // Get ZP address + byte b_tmp = FetchByte(ref _CpuCycle); // Get ZP address b_tmp += Y; // add regY to address SetRegister("X", ReadByteFromMemory(b_tmp)); SetZeroAndNegativeFlags(X); @@ -71,7 +71,7 @@ public void Cmd_B6() // Load Y immidiate A0 public void Cmd_A0() { - byte b_tmp = FetchByte(ref cycles); + byte b_tmp = FetchByte(ref _CpuCycle); SetRegister("Y", b_tmp); SetZeroAndNegativeFlags(Y); } @@ -79,7 +79,7 @@ public void Cmd_A0() // Load Y zeropage A4 public void Cmd_A4() { - byte b_tmp = FetchByte(ref cycles); + byte b_tmp = FetchByte(ref _CpuCycle); SetRegister("Y", ReadByteFromMemory(b_tmp)); SetZeroAndNegativeFlags(Y); } @@ -87,7 +87,7 @@ public void Cmd_A4() // Load Y zeropage X B4 public void Cmd_B4() { - byte b_tmp = FetchByte(ref cycles); // Get ZP address + byte b_tmp = FetchByte(ref _CpuCycle); // Get ZP address b_tmp += X; // add regX to address SetRegister("Y", ReadByteFromMemory(b_tmp)); SetZeroAndNegativeFlags(Y); @@ -100,7 +100,7 @@ public void Cmd_B4() // Push Accumulator on Stack public void Cmd_48() { - PushByteToStack(A,ref cycles); + PushByteToStack(A,ref _CpuCycle); } #endregion diff --git a/CPU_emu/Class_CPU.cs b/CPU_emu/Class_CPU.cs index 691662a..068f153 100644 --- a/CPU_emu/Class_CPU.cs +++ b/CPU_emu/Class_CPU.cs @@ -27,7 +27,7 @@ public partial class CPU private ulong _InterruptPeriod; private bool _ExitRequested; private bool _SteppingMode; - private ulong cycles; + private ulong _CpuCycle; private const uint MAX_MEM = 1024 * 64; private byte[] Data = new byte[MAX_MEM]; @@ -161,14 +161,14 @@ public void Start() private void CpuRunner_DoWork(object sender, DoWorkEventArgs e) { - cycles = InterruptPeriod; + //_CpuCycle = InterruptPeriod; Type thisType = this.GetType(); while (CpuIsRunning) { - byte instruction = FetchByte(ref cycles); + byte instruction = FetchByte(ref _CpuCycle); // Build method name from 'Cmd' + opcode string cmd = "Cmd_" + instruction.ToString("X2").ToUpper(); @@ -198,10 +198,11 @@ private void CpuRunner_DoWork(object sender, DoWorkEventArgs e) break; } - if (cycles <= 0) - { - cycles = InterruptPeriod; - } + //ToDo remove + //if (_CpuCycle <= 0) + //{ + // _CpuCycle = InterruptPeriod; + //} if (SlowDown && !_SteppingMode) { @@ -214,24 +215,24 @@ private void CpuRunner_DoWork(object sender, DoWorkEventArgs e) private uint AddrAbsolute() { - return FetchWord(ref cycles); + return FetchWord(ref _CpuCycle); } - private byte PullByteFromStack(ref ulong cycles) + private byte PullByteFromStack(ref ulong CpuCycle) { IncrementSP(); - cycles--; + CpuCycle++; byte b = ReadByteFromMemory(SP); - cycles--; + CpuCycle++; return b; } - private void PushByteToStack(byte b,ref ulong cycles) + private void PushByteToStack(byte b,ref ulong CpuCycle) { WriteByteToMemory(b, SP); - cycles--; + CpuCycle++; DecrementSP(); - cycles--; + CpuCycle++; } @@ -248,21 +249,21 @@ private void SetZeroAndNegativeFlags(byte register) OnFlagsUpdate?.Invoke(this, new CPUEventArgs(this)); } - private byte FetchByte(ref ulong cycles) + private byte FetchByte(ref ulong CpuCycle) { byte data = Data[PC]; IncrementPC(); - cycles--; + CpuCycle++; return data; } - private ushort FetchWord(ref ulong cycles) + private ushort FetchWord(ref ulong CpuCycle) { ushort LoByte = ReadByteFromMemory((ushort)PC); PC++; ushort HiByte = (ushort)(ReadByteFromMemory((ushort)PC) << 8); - cycles -= 2; + CpuCycle += 2; return LoByte |= HiByte; } From 5d1176a865a6b3db56bb8ac7df5ab5a38974bde1 Mon Sep 17 00:00:00 2001 From: mjbohn Date: Sun, 13 Jul 2025 23:39:07 +0200 Subject: [PATCH 3/4] introduce IncrementCpuCycle(ulong count) --- CPU_emu/Class_CPU.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/CPU_emu/Class_CPU.cs b/CPU_emu/Class_CPU.cs index 068f153..d949fe6 100644 --- a/CPU_emu/Class_CPU.cs +++ b/CPU_emu/Class_CPU.cs @@ -62,6 +62,7 @@ public byte[] Memory public event EventHandler OnFlagsUpdate; public event EventHandler OnMemoryUpdate; public event EventHandler OnRegisterUpdate; + public event EventHandler OnCpuCycleIncrement; public event EventHandler OnProgramCounterUpdate; public event EventHandler OnStackPointerUpdate; public event EventHandler OnPCoverflow; @@ -221,18 +222,18 @@ private uint AddrAbsolute() private byte PullByteFromStack(ref ulong CpuCycle) { IncrementSP(); - CpuCycle++; + IncrementCpuCycle(1); byte b = ReadByteFromMemory(SP); - CpuCycle++; + IncrementCpuCycle(1); return b; } private void PushByteToStack(byte b,ref ulong CpuCycle) { WriteByteToMemory(b, SP); - CpuCycle++; + IncrementCpuCycle(1); DecrementSP(); - CpuCycle++; + IncrementCpuCycle(1); } @@ -253,7 +254,7 @@ private byte FetchByte(ref ulong CpuCycle) { byte data = Data[PC]; IncrementPC(); - CpuCycle++; + IncrementCpuCycle(1); return data; } @@ -263,7 +264,7 @@ private ushort FetchWord(ref ulong CpuCycle) PC++; ushort HiByte = (ushort)(ReadByteFromMemory((ushort)PC) << 8); - CpuCycle += 2; + IncrementCpuCycle(2); return LoByte |= HiByte; } @@ -311,6 +312,12 @@ private void DecrementSP() OnStackPointerUpdate?.Invoke(this, new CPUEventArgs(this)); } + private void IncrementCpuCycle(ulong count) + { + _CpuCycle += count; + OnCpuCycleIncrement?.Invoke(this, new CPUEventArgs(this)); + } + // Programcounter PC //UT public void SetPC(uint value) From 9f61fb7e74f89cecac559fb042b9b8a8de5017d4 Mon Sep 17 00:00:00 2001 From: mjbohn Date: Mon, 14 Jul 2025 00:05:37 +0200 Subject: [PATCH 4/4] visualize cycles in GUI staus panel --- CPU_emu/Class_CPU.cs | 11 ++++++++++- CPU_emu/Forms/CPU_emu.Designer.cs | 26 +++++++++++++++++--------- CPU_emu/Forms/CPU_emu.cs | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/CPU_emu/Class_CPU.cs b/CPU_emu/Class_CPU.cs index d949fe6..47162a3 100644 --- a/CPU_emu/Class_CPU.cs +++ b/CPU_emu/Class_CPU.cs @@ -27,7 +27,7 @@ public partial class CPU private ulong _InterruptPeriod; private bool _ExitRequested; private bool _SteppingMode; - private ulong _CpuCycle; + internal ulong _CpuCycle; private const uint MAX_MEM = 1024 * 64; private byte[] Data = new byte[MAX_MEM]; @@ -101,6 +101,7 @@ public void Reset() OnFlagsUpdate?.Invoke(this, new CPUEventArgs(this)); OnProgramCounterUpdate?.Invoke(this, new CPUEventArgs(this)); + ResetCpuCycle(); } @@ -318,6 +319,12 @@ private void IncrementCpuCycle(ulong count) OnCpuCycleIncrement?.Invoke(this, new CPUEventArgs(this)); } + private void ResetCpuCycle() + { + _CpuCycle =0; + OnCpuCycleIncrement?.Invoke(this, new CPUEventArgs(this)); + } + // Programcounter PC //UT public void SetPC(uint value) @@ -394,6 +401,7 @@ public class CPUEventArgs : EventArgs public byte A { get; set; } public byte X { get; set; } public byte Y { get; set; } + public ulong Cycles { get; set; } public IDictionary Flags { get; set; } public CPUEventArgs(CPU cpu) @@ -412,6 +420,7 @@ private void SetProperties() X = cpu.X; Y = cpu.Y; Flags = cpu.flags; + Cycles = cpu._CpuCycle; } diff --git a/CPU_emu/Forms/CPU_emu.Designer.cs b/CPU_emu/Forms/CPU_emu.Designer.cs index 8f6eaaa..7bc2c42 100644 --- a/CPU_emu/Forms/CPU_emu.Designer.cs +++ b/CPU_emu/Forms/CPU_emu.Designer.cs @@ -92,6 +92,7 @@ private void InitializeComponent() toolStripStatusLabelKernal = new System.Windows.Forms.ToolStripStatusLabel(); toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel(); toolStripStatusLabelBasic = new System.Windows.Forms.ToolStripStatusLabel(); + toolStripStatusLabel3 = new System.Windows.Forms.ToolStripStatusLabel(); checkBoxSlowDown = new System.Windows.Forms.CheckBox(); groupBoxStartStop = new System.Windows.Forms.GroupBox(); panelMain = new System.Windows.Forms.Panel(); @@ -689,23 +690,23 @@ private void InitializeComponent() // // statusStrip1 // - statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripStatusLabelBRK, toolStripStatusElapsedTime, toolStripProgressBar1, toolStripStatusLabel1, toolStripStatusLabelKernal, toolStripStatusLabel2, toolStripStatusLabelBasic }); - statusStrip1.Location = new System.Drawing.Point(0, 599); + statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripStatusLabelBRK, toolStripStatusElapsedTime, toolStripProgressBar1, toolStripStatusLabel1, toolStripStatusLabelKernal, toolStripStatusLabel2, toolStripStatusLabelBasic, toolStripStatusLabel3 }); + statusStrip1.Location = new System.Drawing.Point(0, 597); statusStrip1.Name = "statusStrip1"; statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0); - statusStrip1.Size = new System.Drawing.Size(1306, 22); + statusStrip1.Size = new System.Drawing.Size(1306, 24); statusStrip1.TabIndex = 11; // // toolStripStatusLabelBRK // toolStripStatusLabelBRK.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0); toolStripStatusLabelBRK.Name = "toolStripStatusLabelBRK"; - toolStripStatusLabelBRK.Size = new System.Drawing.Size(0, 17); + toolStripStatusLabelBRK.Size = new System.Drawing.Size(0, 19); // // toolStripStatusElapsedTime // toolStripStatusElapsedTime.Name = "toolStripStatusElapsedTime"; - toolStripStatusElapsedTime.Size = new System.Drawing.Size(0, 17); + toolStripStatusElapsedTime.Size = new System.Drawing.Size(0, 19); // // toolStripProgressBar1 // @@ -718,27 +719,33 @@ private void InitializeComponent() // toolStripStatusLabel1 // toolStripStatusLabel1.Name = "toolStripStatusLabel1"; - toolStripStatusLabel1.Size = new System.Drawing.Size(56, 17); + toolStripStatusLabel1.Size = new System.Drawing.Size(56, 19); toolStripStatusLabel1.Text = "KERNAL: "; // // toolStripStatusLabelKernal // toolStripStatusLabelKernal.Name = "toolStripStatusLabelKernal"; - toolStripStatusLabelKernal.Size = new System.Drawing.Size(24, 17); + toolStripStatusLabelKernal.Size = new System.Drawing.Size(24, 19); toolStripStatusLabelKernal.Text = "0x0"; // // toolStripStatusLabel2 // toolStripStatusLabel2.Name = "toolStripStatusLabel2"; - toolStripStatusLabel2.Size = new System.Drawing.Size(45, 17); + toolStripStatusLabel2.Size = new System.Drawing.Size(45, 19); toolStripStatusLabel2.Text = "BASIC: "; // // toolStripStatusLabelBasic // toolStripStatusLabelBasic.Name = "toolStripStatusLabelBasic"; - toolStripStatusLabelBasic.Size = new System.Drawing.Size(24, 17); + toolStripStatusLabelBasic.Size = new System.Drawing.Size(24, 19); toolStripStatusLabelBasic.Text = "0x0"; // + // toolStripStatusLabel3 + // + toolStripStatusLabel3.Name = "toolStripStatusLabel3"; + toolStripStatusLabel3.Size = new System.Drawing.Size(44, 19); + toolStripStatusLabel3.Text = "Cycles:"; + // // checkBoxSlowDown // checkBoxSlowDown.AutoSize = true; @@ -1349,6 +1356,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem loadCharRomToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator6; private System.Windows.Forms.ToolStripMenuItem loadBasicRomToolStripMenuItem; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3; } } diff --git a/CPU_emu/Forms/CPU_emu.cs b/CPU_emu/Forms/CPU_emu.cs index 2962e31..c5885d8 100644 --- a/CPU_emu/Forms/CPU_emu.cs +++ b/CPU_emu/Forms/CPU_emu.cs @@ -40,6 +40,7 @@ public CPU_emu() Cpu.OnProgramCounterUpdate += Cpu_OnProgramCounterUpdate; Cpu.OnPCoverflow += Cpu_OnPCgtThenMaxMem; Cpu.OnBreak += Cpu_OnBreak; + Cpu.OnCpuCycleIncrement += Cpu_OnCpuCycleIncrement; Cpu.Reset(); @@ -47,6 +48,8 @@ public CPU_emu() } + + private void Config_OnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) @@ -190,6 +193,19 @@ private void Cpu_onFlagsUpdate(object sender, CPUEventArgs e) } + private void Cpu_OnCpuCycleIncrement(object? sender, CPUEventArgs e) + { + if (InvokeRequired) + { + CpuEventCallback cb = new CpuEventCallback(Cpu_OnCpuCycleIncrement); + this.Invoke(cb, new object[] { sender, e }); + } + else + { + toolStripStatusLabel3.Text = $"Cycles: {e.Cycles}"; + } + } + #endregion