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..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 cycles; + internal ulong _CpuCycle; private const uint MAX_MEM = 1024 * 64; private byte[] Data = new byte[MAX_MEM]; @@ -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; @@ -100,6 +101,7 @@ public void Reset() OnFlagsUpdate?.Invoke(this, new CPUEventArgs(this)); OnProgramCounterUpdate?.Invoke(this, new CPUEventArgs(this)); + ResetCpuCycle(); } @@ -161,14 +163,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 +200,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 +217,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--; + IncrementCpuCycle(1); byte b = ReadByteFromMemory(SP); - cycles--; + IncrementCpuCycle(1); return b; } - private void PushByteToStack(byte b,ref ulong cycles) + private void PushByteToStack(byte b,ref ulong CpuCycle) { WriteByteToMemory(b, SP); - cycles--; + IncrementCpuCycle(1); DecrementSP(); - cycles--; + IncrementCpuCycle(1); } @@ -248,21 +251,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--; + IncrementCpuCycle(1); 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; + IncrementCpuCycle(2); return LoByte |= HiByte; } @@ -310,6 +313,18 @@ private void DecrementSP() OnStackPointerUpdate?.Invoke(this, new CPUEventArgs(this)); } + private void IncrementCpuCycle(ulong count) + { + _CpuCycle += 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) @@ -386,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) @@ -404,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 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 + +