Skip to content

Commit

Permalink
Lock introduced which seems to solve the "run-away" problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
maziac committed May 8, 2020
1 parent e3c063b commit c732f0e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 64 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.9.0
- DZRP v0.4.0: Changes to CMD_CONTINUE.
- Lock introduced which seems to solve the "run-away" problem.

## 0.8.0
- Fixed a mix of response and notification problem (asynchronous problem)
Expand Down
137 changes: 73 additions & 64 deletions Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ protected enum AlternateCommand
STEP_OUT = 2
}

// Used for locking
protected static object lockObj = new Object();

// Index for setting a byte in the array.
protected static int Index;
Expand Down Expand Up @@ -90,17 +92,21 @@ protected enum AlternateCommand
*/
public static void Init()
{
var cspect = Main.CSpect;
bool dbgVisible = Main.Settings.CSpectDebuggerVisible;
if (Log.Enabled)
Log.WriteLine("CSpectDebuggerVisible={0}", dbgVisible);
cspect.Debugger(Plugin.eDebugCommand.SetRemote, (dbgVisible) ? 0 : 1);
BreakpointMap = new Dictionary<ushort, ushort>();
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
CpuRunning = false;
StartCpu(false);
lock (lockObj)
{
var cspect = Main.CSpect;
bool dbgVisible = Main.Settings.CSpectDebuggerVisible;
if (Log.Enabled)
Log.WriteLine("CSpectDebuggerVisible={0}", dbgVisible);
cspect.Debugger(Plugin.eDebugCommand.SetRemote, (dbgVisible) ? 0 : 1);
BreakpointMap = new Dictionary<ushort, ushort>();
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
// Stop
CpuRunning = false;
cspect.Debugger(Plugin.eDebugCommand.Enter);
}
}


Expand All @@ -110,13 +116,16 @@ public static void Init()
*/
public static void Reset()
{
BreakpointMap = null;
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
CpuRunning = false;
// Clear breakpoints
ClearAllBreakAndWatchpoints();
lock (lockObj)
{
BreakpointMap = null;
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
CpuRunning = false;
// Clear breakpoints
ClearAllBreakAndWatchpoints();
}
}


Expand All @@ -139,9 +148,9 @@ protected static void PrintAllBpWp()
var cspect = Main.CSpect;
for (int addr = 0; addr < 0x10000; addr++)
{
var bp=cspect.Debugger(Plugin.eDebugCommand.GetBreakpoint, addr);
var wpr=cspect.Debugger(Plugin.eDebugCommand.GetReadBreakpoint, addr);
var wpw=cspect.Debugger(Plugin.eDebugCommand.GetWriteBreakpoint, addr);
var bp = cspect.Debugger(Plugin.eDebugCommand.GetBreakpoint, addr);
var wpr = cspect.Debugger(Plugin.eDebugCommand.GetReadBreakpoint, addr);
var wpw = cspect.Debugger(Plugin.eDebugCommand.GetWriteBreakpoint, addr);
if ((bp | wpr | wpw) != 0)
{
Console.Write(" Address 0x{0:X4}:", addr);
Expand All @@ -162,21 +171,31 @@ protected static void PrintAllBpWp()
*/
protected static void StartCpu(bool start)
{
// Is required. Otherwise a stop could be missed because the tick is called only
// every 20ms. If start/stop happens within this timeframe it would not be recognized.
CpuRunning = start;
// Start/stop
var cspect = Main.CSpect;
if (start)
{
//PrintAllBpWp();
// Run
cspect.Debugger(Plugin.eDebugCommand.Run);
}
else

//if (start)
// PrintAllBpWp();


// The lock is required, otherwise CpuRunning can be set here and the Tick() jumps in
// between "CpuRunning = true/false" and "eDebugCommand.Run/Enter"
lock (lockObj)
{
// Stop
cspect.Debugger(Plugin.eDebugCommand.Enter);
// Is required. Otherwise a stop could be missed because the tick is called only
// every 20ms. If start/stop happens within this timeframe it would not be recognized.
//CpuRunning = start;
// Start/stop
var cspect = Main.CSpect;
CpuRunning = start;
if (start)
{
// Run
cspect.Debugger(Plugin.eDebugCommand.Run);
}
else
{
// Stop
cspect.Debugger(Plugin.eDebugCommand.Enter);
}
}
}

Expand All @@ -190,33 +209,22 @@ public static void Tick()
if (BreakpointMap == null)
return;


// TODO: REMOVE
/*
if(Log.Enabled)
{
for (int i = 0; i < 128; i++)
{
var spr = Main.CSpect.GetSprite(i);
if ((spr.visible_name&0x80) != 0)
Log.WriteLine("sprite[{0}]: x={1}, y={2}, a2={3:X2}, a3={4:X2}, a4={5:X2}", i, spr.x, spr.y, spr.paloff_mirror_flip_rotate_xmsb, spr.visible_name, spr.H_N6_0_XX_YY_Y8);
}
}
*/

// Check if debugger state changed
var cspect = Main.CSpect;
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (CpuRunning != running)
lock (lockObj)
{
// State changed
if (Log.Enabled)
Log.WriteLine("Debugger state changed to {0}, 0=running", debugState);
CpuRunning = running;
if (running == false)
var cspect = Main.CSpect;
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (CpuRunning != running)
{
DebuggerStopped();
// State changed
if (Log.Enabled)
Log.WriteLine("Debugger state changed to {0}, 0=running", debugState);
CpuRunning = running;
if (running == false)
{
DebuggerStopped();
}
}
}
}
Expand Down Expand Up @@ -426,7 +434,8 @@ public static void SetRegister()
case 33: regs._HL = (ushort)((regs._HL & 0xFF) + 256 * valueByte); break; // H'

default:
// TODO: Error
// Error
Console.WriteLine("Error: Wrong register number {0} to set.", regNumber);
break;
}

Expand Down Expand Up @@ -521,7 +530,7 @@ public static void Continue()
// Respond
CSpectSocket.SendResponse();
//ManualBreak = false;
//CpuRunning = true;
//CpuRunning = true; Need to be locked
//cspect.Debugger(Plugin.eDebugCommand.StepOver);
break;
Expand Down Expand Up @@ -765,7 +774,7 @@ public static void GetSlots()
*/
public static void ReadState()
{
// TODO: No CSpect interface yet.
// Not implemented: No CSpect interface yet.

// Respond
CSpectSocket.SendResponse();
Expand All @@ -776,10 +785,10 @@ public static void ReadState()
*/
public static void WriteState()
{
// TODO: No CSpect interface yet.
// Not implemented: No CSpect interface yet.

// Respond
//CSpectSocket.SendResponse();
CSpectSocket.SendResponse();
}


Expand Down

0 comments on commit c732f0e

Please sign in to comment.