Skip to content

Commit

Permalink
Temporary breakpoints disabled after continue.
Browse files Browse the repository at this point in the history
  • Loading branch information
maziac committed Apr 14, 2020
1 parent 7a1c17f commit 3dc800e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 40 deletions.
117 changes: 82 additions & 35 deletions Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
* A client may connect at anytime.
* A connection is terminated only by the client.
* If a connection has been terminated a new connection can be established.
*
* CSpect:
* Set/ClearBreakpoint: This increments/decrements a breakpoint counter.
* One has to check with GetBreakpoint == 0 if the breakpoint is not active.
* Normal breakpoints are "orange"m
* Physical breakpoints are "red" in the CSpect UI.
*/


namespace DeZogPlugin
{
/**
* Handles the sokcet commands, responses and notifications.
* Handles the socket commands, responses and notifications.
*/
public class Commands
{
Expand All @@ -48,9 +54,9 @@ protected enum BreakReason
// The data array for preparing data to send.
protected static byte[] Data;

// Temporary breakpoint (IDs) for Continue. 0 = unused.
protected static ushort TmpBreakpoint1;
protected static ushort TmpBreakpoint2;
// Temporary breakpoint (addresses) for Continue. -1 = unused.
protected static int TmpBreakpoint1;
protected static int TmpBreakpoint2;


// The breakpoint map to keep the IDs and addresses.
Expand All @@ -60,7 +66,7 @@ protected enum BreakReason
protected static ushort LastBreakpointId;

// Action queue.
protected static List<Action> ActionQueue = new List<Action>();
//protected static List<Action> ActionQueue = new List<Action>();

// Stores the previous debugger state.
protected static bool CpuRunning = false;
Expand All @@ -70,24 +76,25 @@ protected enum BreakReason
*/
public static void Init()
{
ActionQueue = new List<Action>();
// ActionQueue = new List<Action>();
BreakpointMap = new Dictionary<ushort, ushort>();
LastBreakpointId = 0;
TmpBreakpoint1 = 0;
TmpBreakpoint2 = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
CpuRunning = false;
StartCpu(false);

// Clear all breakpoints etc.
var cspect = Main.CSpect;
/*
for (int addr = 0; addr < 0x10000; addr++)
{
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, addr);
cspect.Debugger(Plugin.eDebugCommand.ClearReadBreakpoint, addr);
cspect.Debugger(Plugin.eDebugCommand.ClearWriteBreakpoint, addr);
while (cspect.Debugger(Plugin.eDebugCommand.GetBreakpoint, addr) != 0)
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, addr);
while (cspect.Debugger(Plugin.eDebugCommand.GetReadBreakpoint, addr) != 0)
cspect.Debugger(Plugin.eDebugCommand.ClearReadBreakpoint, addr);
while (cspect.Debugger(Plugin.eDebugCommand.GetBreakpoint, addr) != 0)
cspect.Debugger(Plugin.eDebugCommand.GetWriteBreakpoint, addr);
}
*/
// Disable CSpect debugger screen
//cspect.Debugger(Plugin.eDebugCommand.SetRemote, 1);
// cspect.Debugger(Plugin.eDebugCommand.Run); // TODO
Expand Down Expand Up @@ -137,19 +144,12 @@ public static void Tick()
{
Console.WriteLine("RUN");
var csp = Main.CSpect;
for (int addr = 0x80F8; addr < 0x80F9; addr++)
{
csp.Debugger(Plugin.eDebugCommand.SetBreakpoint, addr);
csp.Debugger(Plugin.eDebugCommand.SetBreakpoint, addr);
Console.WriteLine(csp.Debugger(Plugin.eDebugCommand.GetBreakpoint, addr));
csp.Debugger(Plugin.eDebugCommand.ClearReadBreakpoint, addr);
csp.Debugger(Plugin.eDebugCommand.ClearWriteBreakpoint, addr);
}
Main.CSpect.Debugger(Plugin.eDebugCommand.Run);
Console.WriteLine("RUNdone " );
}

// Check if something to send
/*
int count = ActionQueue.Count;
if (count > 0)
{
Expand All @@ -160,6 +160,7 @@ public static void Tick()
ActionQueue.Clear();
}
}
*/

// Check if debugger state changed
var cspect = Main.CSpect;
Expand All @@ -171,13 +172,30 @@ public static void Tick()
CpuRunning = running;
if(CpuRunning==false)
{
// Send break notification
SendPauseNotification(BreakReason.MANUAL_BREAK, 0);
DebuggerStopped();
}
}
}


/**
* Called when the debugger stopped.
* E.g. because a breakpoint was hit.
*/
protected static void DebuggerStopped()
{
// Disable temporary breakpoints
var cspect = Main.CSpect;
if (TmpBreakpoint1 >= 0)
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, TmpBreakpoint1);
if (TmpBreakpoint2 >= 0)
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, TmpBreakpoint2);

// Send break notification
SendPauseNotification(BreakReason.MANUAL_BREAK, 0);
}


/**
* Sets a byte in the Data array.
*/
Expand Down Expand Up @@ -331,9 +349,8 @@ public static void WriteBank()
*/
protected static ushort SetBreakpoint(ushort address)
{
// Set in CSpect (if not already existing)
if (!BreakpointMap.ContainsValue(address))
Main.CSpect.Debugger(Plugin.eDebugCommand.SetBreakpoint, address);
// Set in CSpect (increment counter)
Main.CSpect.Debugger(Plugin.eDebugCommand.SetBreakpoint, address);
// Add to array (ID = element position + 1)
BreakpointMap.Add(++LastBreakpointId, address);
return LastBreakpointId;
Expand All @@ -350,10 +367,8 @@ protected static void DeleteBreakpoint(ushort bpId)
if (BreakpointMap.TryGetValue(bpId, out address))
{
BreakpointMap.Remove(bpId);

// Clear in CSpect (if no other breakpoint exists)
if (!BreakpointMap.ContainsValue(address))
Main.CSpect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, address);
// Clear in CSpect (decrement counter)
Main.CSpect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, address);
}
}

Expand All @@ -371,11 +386,23 @@ public static void Continue()
ushort bp2Address = CSpectSocket.GetDataWord();

// Set temporary breakpoints
TmpBreakpoint1 = (bp1Enable) ? SetBreakpoint(bp1Address) : (ushort)0;
TmpBreakpoint2 = (bp2Enable) ? SetBreakpoint(bp2Address) : (ushort)0;
var cspect = Main.CSpect;
TmpBreakpoint1 = -1;
if (bp1Enable)
{
TmpBreakpoint1 = bp1Address;
cspect.Debugger(Plugin.eDebugCommand.SetBreakpoint, TmpBreakpoint1);
}
TmpBreakpoint2 = -1;
if (bp2Enable)
{
TmpBreakpoint2 = bp2Address;
cspect.Debugger(Plugin.eDebugCommand.SetBreakpoint, TmpBreakpoint2);
}

// Run
Console.WriteLine("Continue: Run debugger.");
var regs = cspect.GetRegs();
Console.WriteLine("Continue: Run debugger. pc=0x{0:X4}/{0}, bp1=0x{1:X4}/{1}, bp2=0x{2:X4}/{2}", regs.PC, TmpBreakpoint1, TmpBreakpoint2);
Main.CSpect.Debugger(Plugin.eDebugCommand.Run);

/*
Expand Down Expand Up @@ -568,10 +595,30 @@ public static void WriteState()
*/
protected static void SendPauseNotification(BreakReason reason, int bpId)
{

// Prepare data
int length = 7;
byte[] data =
{
// Length
(byte)(length & 0xFF),
(byte)((length >> 8) & 0xFF),
(byte)((length >> 16) & 0xFF),
(byte)(length >> 24),
// SeqNo = 0
0,
// PAUSE
(byte)DZRP_NTF.NTF_PAUSE,
// Reason
(byte)reason,
// Breakpoint ID
(byte)(bpId & 0xFF),
(byte)((bpId >> 8) & 0xFF),
// No string
0
};

// Respond
CSpectSocket.SendResponse();
CSpectSocket.Send(data);
}
}
}
19 changes: 14 additions & 5 deletions Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace DeZogPlugin
{

// The comand enums.
// The command enums.
public enum DZRP {
CMD_GET_CONFIG=1,
CMD_GET_REGISTERS=2,
Expand All @@ -37,6 +37,13 @@ public enum DZRP {
}


// The notifications
public enum DZRP_NTF
{
NTF_PAUSE = 1
}


// State object for reading client data asynchronously
public class StateObject
{
Expand Down Expand Up @@ -423,8 +430,6 @@ public static void SendResponse(byte[] byteData=null)
if(byteData!=null)
byteData.CopyTo(wrapBuffer, HEADER_LEN_LENGTH + 1);
receveivedSeqno = 0; // Ready for next message.
if (LogEnabled)
WriteResp(wrapBuffer);
// Begin sending the data to the remote device.
Send(wrapBuffer);
}
Expand All @@ -438,6 +443,9 @@ public static void Send(byte[] byteData)
if (CSpectSocket.socket == null)
return;

// Log
if (LogEnabled)
WriteResp(byteData);
// Begin sending the data to the remote device.
Socket handler = CSpectSocket.socket.workSocket;
handler.BeginSend(byteData, 0, byteData.Length, 0,
Expand Down Expand Up @@ -503,6 +511,7 @@ protected static void WriteCmd(byte[] data)
int seqno = data[4];
int cmd = data[5];
string cmdString = ((DZRP)cmd).ToString();
Console.WriteLine();
Console.WriteLine("<-- Command {0}:", cmdString);
Console.WriteLine(" Length: {0} ", length);
Console.WriteLine(" SeqNo: {0}", seqno);
Expand All @@ -521,7 +530,6 @@ protected static void WriteCmd(byte[] data)
*/
protected static void WriteResp(byte[] data)
{
Console.WriteLine("--> WriteResp");
int count = data.Length;
int index = 0;
if (count >= 5)
Expand All @@ -533,7 +541,8 @@ protected static void WriteResp(byte[] data)
text = "Notification:";
else
text = "Response:";
Console.WriteLine(text);
Console.WriteLine();
Console.WriteLine("--> "+text);
Console.WriteLine(" Length: {0} ", length);
Console.WriteLine(" SeqNo: {0}", seqno);
index = 5;
Expand Down

0 comments on commit 3dc800e

Please sign in to comment.