Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions CPU_emu/CPU_emu.CMD_Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -43,23 +43,23 @@ 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);
}

// 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);
}

// 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);
Expand All @@ -71,23 +71,23 @@ 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);
}

// 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);
}

// 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);
Expand All @@ -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
Expand Down
53 changes: 35 additions & 18 deletions CPU_emu/Class_CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -62,6 +62,7 @@ public byte[] Memory
public event EventHandler<CPUEventArgs> OnFlagsUpdate;
public event EventHandler<CPUEventArgs> OnMemoryUpdate;
public event EventHandler<CPUEventArgs> OnRegisterUpdate;
public event EventHandler<CPUEventArgs> OnCpuCycleIncrement;
public event EventHandler<CPUEventArgs> OnProgramCounterUpdate;
public event EventHandler<CPUEventArgs> OnStackPointerUpdate;
public event EventHandler<CPUEventArgs> OnPCoverflow;
Expand Down Expand Up @@ -100,6 +101,7 @@ public void Reset()

OnFlagsUpdate?.Invoke(this, new CPUEventArgs(this));
OnProgramCounterUpdate?.Invoke(this, new CPUEventArgs(this));
ResetCpuCycle();

}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);

}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<string, bool> Flags { get; set; }

public CPUEventArgs(CPU cpu)
Expand All @@ -404,6 +420,7 @@ private void SetProperties()
X = cpu.X;
Y = cpu.Y;
Flags = cpu.flags;
Cycles = cpu._CpuCycle;
}


Expand Down
26 changes: 17 additions & 9 deletions CPU_emu/Forms/CPU_emu.Designer.cs

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

16 changes: 16 additions & 0 deletions CPU_emu/Forms/CPU_emu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public CPU_emu()
Cpu.OnProgramCounterUpdate += Cpu_OnProgramCounterUpdate;
Cpu.OnPCoverflow += Cpu_OnPCgtThenMaxMem;
Cpu.OnBreak += Cpu_OnBreak;
Cpu.OnCpuCycleIncrement += Cpu_OnCpuCycleIncrement;

Cpu.Reset();

// config.OnPropertyChanged gets initialized @ CPU_emu_Load() due to race conditions

}



private void Config_OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (InvokeRequired)
Expand Down Expand Up @@ -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


Expand Down
8 changes: 8 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>sim6502</title>
</head>
<body>
<strong>sim6502</strong>
</body>
</html>