-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommands.cs
1302 lines (1147 loc) · 44 KB
/
Commands.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
/*
* The used socket protocol is simple. It consists of header and payload.
*
* Message:
* int Length: The length of the following bytes containing Command. Little endian. Size=4.
* byte SeqNo: Sequence number (must be returned in response)
* byte Command: UART_DATA.
* Payload bytes: The data
*
* 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.
*
*/
namespace DeZogPlugin
{
/**
* Handles the socket commands, responses and notifications.
*/
public class Commands
{
protected static byte[] DZRP_VERSION = { 2, 0, 0 };
/**
* The break reason.
*/
protected enum BreakReason
{
NO_REASON = 0,
MANUAL_BREAK = 1,
BREAKPOINT_HIT = 2,
WATCHPOINT_READ = 3,
WATCHPOINT_WRITE = 4,
OTHER = 255,
}
/**
* The alternate command for CMD_CONTINUE.
*/
protected enum AlternateCommand
{
CONTINUE = 0, // I.e. no alternate command
STEP_OVER = 1,
STEP_OUT = 2
}
// Used for locking
protected static object lockObj = new Object();
// Index for setting a byte in the array.
protected static int Index;
// The data array for preparing data to send.
protected static byte[] Data;
// Temporary breakpoint (addresses) for Continue. -1 = unused.
protected static int TmpBreakpoint1;
protected static int TmpBreakpoint2;
// The breakpoint map to keep the IDs and addresses.
// If it is null then the connection is not active.
protected static Dictionary<ushort,int> BreakpointMap = null;
// The last breakpoint ID used.
protected static ushort LastBreakpointId;
// Action queue.
//protected static List<Action> ActionQueue = new List<Action>();
// Stores the previous debugger state.
protected static bool CpuRunning = false;
// Stores if a PAUSE command has been sent.
protected static bool ManualBreak = false;
/**
* General initalization function.
*/
public static void Init()
{
lock (lockObj)
{
var cspect = Main.CSpect;
BreakpointMap = new Dictionary<ushort, int>();
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
// Stop
CpuRunning = false;
cspect.Debugger(Plugin.eDebugCommand.Enter);
// Clear any pending interrupt
var regs = cspect.GetRegs();
var prevRegs = regs;
regs.IFF1 = false;
regs.IFF2 = false;
cspect.SetRegs(regs); // This seems to clear a pending interrupt
// Restore original value
cspect.SetRegs(prevRegs);
}
}
/**
* Resets the breakpoint map.
* Is called so that 'tick' does not process ticks anymore.
*/
public static void Reset()
{
lock (lockObj)
{
BreakpointMap = null;
LastBreakpointId = 0;
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
CpuRunning = false;
// Clear breakpoints
ClearAllBreakAndWatchpoints();
}
}
/**
* Initializes the data buffer.
*/
protected static void InitData(int size)
{
Index = 0;
Data = new byte[size];
}
/**
* Returns the DZRP version as a string.
*/
public static string GetDzrpVersion()
{
string version = "" + DZRP_VERSION[0] + '.' + DZRP_VERSION[1] + '.' + DZRP_VERSION[2];
return version;
}
/**
* Debug function to print all Breakpoints and watchpoints.
*/
protected static void PrintAllBpWp()
{
Log.WriteLine("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);
if ((bp | wpr | wpw) != 0)
{
Log.Write(" Address 0x{0:X4}:", addr);
if (bp != 0)
Log.Write(" [Breakpoint]");
if (wpr != 0)
Log.Write(" [Watchpoint read]");
if (wpw != 0)
Log.Write(" [Watchpoint write]");
Log.WriteLine();
}
}
}
/**
* Start/stop debugger.
* @param start Starts the CPU if true (currently this is the only operation mode)
*/
protected static void StartCpu(bool start)
{
//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)
{
// 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;
// Get previous debug state
var cspect = Main.CSpect;
var prevDbgState = cspect.Debugger(Plugin.eDebugCommand.GetState); // 0 = runnning
bool prevRunning = (prevDbgState == 0);
// Loop and wait until executed
if (start != prevRunning)
{
var dbgCommand = (start) ? Plugin.eDebugCommand.Run : Plugin.eDebugCommand.Enter;
while (true)
{
// Run or stop
cspect.Debugger(dbgCommand);
// Check if done
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (start == running)
break;
// Else wait a little bit
Thread.Sleep(1); // in ms
}
// If changed to 'stop' then check the breakpoints
if (!start)
{
CheckIfBreakpointHit();
}
}
// Start/stop
CpuRunning = start;
}
}
/**
* Returns address as string.
* If it is a long address than together with bank information.
* E.g. "0x35F6" or 0x35F6 @bank9"
*/
protected static string GetAddressString(int address)
{
int addr = address & 0xFFFF;
string addrString = string.Format("0x{0:X4}", addr);
byte bank = (byte)(address >> 16);
if(bank>0)
addrString += " @bank" + (bank - 1);
return addrString;
}
/**
* Checks if registers have changed.
* Not all registers are tested.
* Only R and PC.
*/
protected static bool RegsChanged(Plugin.Z80Regs r1, Plugin.Z80Regs r2)
{
return (r1.PC != r2.PC) || (r1.R != r2.R);
}
/**
* Used to execute an action but make sure that the CPU is stopped before.
* If the CPU was running before it will be restarted.
* If breakpoints are found on stop a notification will be sent.
*/
public static void ExecuteStopped(Action action)
{
// 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)
{
// 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;
// Get previous debug state
var cspect = Main.CSpect;
var prevDbgState = cspect.Debugger(Plugin.eDebugCommand.GetState); // 0 = runnning
bool prevRunning = (prevDbgState == 0);
// Loop and wait until executed
if (prevRunning)
{
var dbgCommand = Plugin.eDebugCommand.Enter;
var prevRegs = cspect.GetRegs();
while (true)
{
// Run or stop
cspect.Debugger(dbgCommand);
// Wait a little bit
Thread.Sleep(1); // in ms // DOES NOT WORK: If I wait too long in this function CSpect behaves oddly. E.g. changes memory, restarts Z80, ...
// Checks registers
var curRegs = cspect.GetRegs();
if (!RegsChanged(prevRegs, curRegs)) {
// Check if done
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (!running)
break;
}
prevRegs = curRegs;
}
}
// Execute the action
action();
// If previously running get back to the old state
if (prevRunning)
{
// If changed to 'stop' then check the breakpoints
CheckIfBreakpointHit(false);
// Restart
var dbgCommand = Plugin.eDebugCommand.Run;
while (true)
{
// Run or stop
cspect.Debugger(dbgCommand);
// Check if done
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (running)
break;
// Else wait a little bit
Thread.Sleep(1); // in ms
}
}
}
}
//static bool run = false;
//static Plugin.Z80Regs prevRegs = Main.CSpect.GetRegs();
//static int prevMem = 0;
//static int counter = 0;
//static System.Diagnostics.Stopwatch stopwatch;
/**
* Called on every tick.
* Every 20ms.
*/
public static void Tick()
{
/*
var cspect = Main.CSpect;
var curRegs = cspect.GetRegs();
if(!run)
{
// Compare
if (prevRegs.PC != curRegs.PC || prevRegs.R != curRegs.R )
{
Log.WriteLine("prevPC={0}/0x{0:X4}, prevR={1}, curPC={2}/0x{2:X4}, curR={3}", prevRegs.PC, prevRegs.R, curRegs.PC, curRegs.R);
Log.WriteLine("prevSP={0}/0x{0:X4}, curSP={1}/0x{1:X4}", prevRegs.SP, curRegs.SP);
byte[] spCont = cspect.Peek((ushort)(curRegs.SP - 2), 2);
int mem = spCont[0] + 256 * spCont[1];
Log.WriteLine("prevMem={0}/0x{0:X4}, (curSP-2)={1}/0x{1:X4}", prevMem, prevMem, mem, mem);
Log.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
prevRegs = curRegs;
byte[] prevCont = cspect.Peek(curRegs.SP, 2);
prevMem = prevCont[0] + 256 * prevCont[1];
run = !run;
var dbgCommand = (run) ? Plugin.eDebugCommand.Run : Plugin.eDebugCommand.Enter;
cspect.Debugger(dbgCommand);
*/
/*
counter++;
if (counter == 1)
{
stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
}
if (counter == 1000)
{
stopwatch.Stop();
Console.WriteLine("RunTime: " + stopwatch.ElapsedMilliseconds);
counter = 0;
}
*/
// Return if not initialized
if (BreakpointMap == null)
return;
// Check if debugger state changed
lock (lockObj)
{
var cspect = Main.CSpect;
var debugState = cspect.Debugger(Plugin.eDebugCommand.GetState);
bool running = (debugState == 0);
if (CpuRunning != running)
{
// State changed
if (Log.Enabled)
Log.WriteLine("Debugger state changed to {0}, 0=running", debugState);
CpuRunning = running;
if (!running)
{
CheckIfBreakpointHit();
}
}
}
}
/**
* Called when the debugger stopped.
* E.g. because a breakpoint was hit.
* @param sendNtfIfNoReason If no reason is found a notification (with "manual break") is sent
* if true.
* The notification is not sent if false. In that case a notification is only sent if a reason
* is found.
*/
protected static void CheckIfBreakpointHit(bool sendNtfIfNoReason=true)
{
// Get PC
var cspect = Main.CSpect;
var regs = cspect.GetRegs();
var pc = regs.PC;
byte slot = (byte)(pc >> 13);
int bank = cspect.GetNextRegister((byte)(0x50 + slot));
//Log.WriteLine("Debugger stopped: bank {0}, slot {1}", bank, slot);
int pcLong = ((bank+1)<<16) + pc;
if (Log.Enabled)
Log.WriteLine("Debugger stopped at 0x{0:X4} (long address=0x{1:X6})", pc, pcLong);
// Disable temporary breakpoints (64k addresses)
if (TmpBreakpoint1 >= 0)
{
if(!BreakpointMap.ContainsValue((ushort)TmpBreakpoint1))
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, TmpBreakpoint1);
}
if (TmpBreakpoint2 >= 0)
{
if (!BreakpointMap.ContainsValue((ushort)TmpBreakpoint2))
cspect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, TmpBreakpoint2);
}
// Guess break reason
BreakReason reason = BreakReason.MANUAL_BREAK;
string reasonString = "";
int bpAddress = 0;
// Check for temporary breakpoints
if (pc == TmpBreakpoint1 || pc == TmpBreakpoint2)
{
reason = BreakReason.NO_REASON;
bpAddress = pcLong;
}
// Check for breakpoints
else if (BreakpointMap.ContainsValue(pcLong) || BreakpointMap.ContainsValue(pc))
{
// Breakpoint hit
reason = BreakReason.BREAKPOINT_HIT;
bpAddress = pcLong;
}
// Note: Watchpoint reasons cannot be safely recognized.
// Use a few heuristics to determine if a watchpoint is hit.
if (reason == BreakReason.MANUAL_BREAK)
{
if (!ManualBreak)
{
// No pause command has been sent.
// Check if there is any watchpoint set. If yes
// assume it was a watchpoint.
// Note: It could also be a user stopping the CSpect by F1.
for (int i = 0; i < 0x10000; i++)
{
if (cspect.Debugger(Plugin.eDebugCommand.GetReadBreakpoint, i) != 0
|| cspect.Debugger(Plugin.eDebugCommand.GetWriteBreakpoint, i) != 0)
{
reason = BreakReason.OTHER;
reasonString = "Watchpoint hit or manual break.";
break;
}
}
}
}
// Send break notification
if(sendNtfIfNoReason || reason!=BreakReason.MANUAL_BREAK)
{
SendPauseNotification(reason, bpAddress, reasonString);
}
// "Undefine" temporary breakpoints
TmpBreakpoint1 = -1;
TmpBreakpoint2 = -1;
// Reset Pause
ManualBreak = false;
}
/**
* Sets a byte in the Data array.
*/
protected static void SetByte(int value)
{
Data[Index++] = (byte) value;
}
/**
* Sets a dword in the Data array.
*/
protected static void SetWord(int value)
{
Data[Index++] = (byte)(value & 0xFF);
Data[Index++] = (byte)(value>>8);
}
/**
* Copies a buffer to the Data array.
*/
protected static void SetBuffer(byte[] buffer)
{
foreach (var value in buffer)
Data[Index++] = value;
}
/**
* Copies a string (including terminating 0) into the Data array.
*/
protected static void SetString(string text)
{
foreach (var value in text)
Data[Index++] = (byte)value;
}
/**
* Clears all break- and watchpoints.
*/
protected static void ClearAllBreakAndWatchpoints()
{
// Clear all breakpoints etc.
var cspect = Main.CSpect;
cspect.Debugger(Plugin.eDebugCommand.ClearAllBreakpoints);
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);
}
}
/**
* Returns the configuration.
*/
public static void CmdInit()
{
// Clear breakpoints
ClearAllBreakAndWatchpoints();
// Return values
int length = 1 + 3 + Main.ProgramName.Length + 1;
InitData(length);
SetByte(0); // No error
SetBuffer(DZRP_VERSION);
SetByte((byte)DzrpMachineType.ZXNEXT); // machine type = ZXNext
SetString(Main.ProgramName);
CSpectSocket.SendResponse(Data);
}
/**
* Just sends a response.
*/
public static void CmdClose()
{
CSpectSocket.SendResponse();
}
/**
* Returns the registers.
*/
public static void GetRegisters()
{
// Get registers
var cspect = Main.CSpect;
var regs = cspect.GetRegs();
InitData(29+8);
// Return registers
SetWord(regs.PC);
SetWord(regs.SP);
SetWord(regs.AF);
SetWord(regs.BC);
SetWord(regs.DE);
SetWord(regs.HL);
SetWord(regs.IX);
SetWord(regs.IY);
SetWord(regs._AF);
SetWord(regs._BC);
SetWord(regs._DE);
SetWord(regs._HL);
SetByte(regs.R);
SetByte(regs.I);
SetByte(regs.IM);
SetByte(0); // reserved
// Return the slots/banks
SetByte(8);
for (int i = 0; i < 8; i++)
{
byte bank = cspect.GetNextRegister((byte)(0x50 + i));
SetByte(bank);
}
// Return
CSpectSocket.SendResponse(Data);
//Log.WriteLine("GetRegs: I={0}, R={1}", regs.I, regs.R);
}
/**
* Sets one double or single register.
*/
public static void SetRegister()
{
// Get register number
byte regNumber = CSpectSocket.GetDataByte();
// Get new value
ushort value = CSpectSocket.GetDataWord();
ushort valueByte = (ushort)(value & 0xFF);
// Get registers
var cspect = Main.CSpect;
var regs = cspect.GetRegs();
// Set a specific register
switch (regNumber)
{
case 0: regs.PC = value; break;
case 1: regs.SP = value; break;
case 2: regs.AF = value; break;
case 3: regs.BC = value; break;
case 4: regs.DE = value; break;
case 5: regs.HL = value; break;
case 6: regs.IX = value; break;
case 7: regs.IY = value; break;
case 8: regs._AF = value; break;
case 9: regs._BC = value; break;
case 10: regs._DE = value; break;
case 11: regs._HL = value; break;
case 13: regs.IM = (byte)value; break;
case 14: regs.AF = (ushort)((regs.AF & 0xFF00) + valueByte); break; // F
case 15: regs.AF = (ushort)((regs.AF & 0xFF) + 256* valueByte); break; // A
case 16: regs.BC = (ushort)((regs.BC & 0xFF00) + valueByte); break; // C
case 17: regs.BC = (ushort)((regs.BC & 0xFF) + 256 * valueByte); break; // B
case 18: regs.DE = (ushort)((regs.DE & 0xFF00) + valueByte); break; // E
case 19: regs.DE = (ushort)((regs.DE & 0xFF) + 256 * valueByte); break; // D
case 20: regs.HL = (ushort)((regs.HL & 0xFF00) + valueByte); break; // L
case 21: regs.HL = (ushort)((regs.HL & 0xFF) + 256 * valueByte); break; // H
case 22: regs.IX = (ushort)((regs.IX & 0xFF00) + valueByte); break; // IXL
case 23: regs.IX = (ushort)((regs.IX & 0xFF) + 256 * valueByte); break; // IXH
case 24: regs.IY = (ushort)((regs.IY & 0xFF00) + valueByte); break; // IYL
case 25: regs.IY = (ushort)((regs.IY & 0xFF) + 256 * valueByte); break; // IYH
case 26: regs._AF = (ushort)((regs._AF & 0xFF00) + valueByte); break; // F'
case 27: regs._AF = (ushort)((regs._AF & 0xFF) + 256 * valueByte); break; // A'
case 28: regs._BC = (ushort)((regs._BC & 0xFF00) + valueByte); break; // C'
case 29: regs._BC = (ushort)((regs._BC & 0xFF) + 256 * valueByte); break; // B'
case 30: regs._DE = (ushort)((regs._DE & 0xFF00) + valueByte); break; // E'
case 31: regs._DE = (ushort)((regs._DE & 0xFF) + 256 * valueByte); break; // D'
case 32: regs._HL = (ushort)((regs._HL & 0xFF00) + valueByte); break; // L'
case 33: regs._HL = (ushort)((regs._HL & 0xFF) + 256 * valueByte); break; // H'
case 34: regs.R = (byte)value; break;
case 35: regs.I = (byte)value; break;
default:
// Error
Log.WriteLine("Error: Wrong register number {0} to set.", regNumber);
break;
}
// Set register(s)
cspect.SetRegs(regs);
//Log.WriteLine("SetRegs: I={0}, R={1}", regs.I, regs.R);
// Respond
CSpectSocket.SendResponse();
}
/**
* Writes one memory bank.
*/
public static void WriteBank()
{
// Get size
int bankSize = CSpectSocket.GetRemainingDataCount()-1;
if(bankSize!=0x2000)
{
// Only supported is 0x2000
string errorString = "Bank size incorrect!";
int length = 1 + errorString.Length + 1;
InitData(length);
SetByte(1); // Error
SetString(errorString);
CSpectSocket.SendResponse(Data);
}
// Get bank number
byte bankNumber = CSpectSocket.GetDataByte();
// Calculate physical address
// Example: phys. address $1f021 = ($1f021&$1fff) for offset and ($1f021>>13) for bank.
Int32 physAddress = bankNumber * bankSize;
// Write memory
var cspect = Main.CSpect;
if (Log.Enabled)
Log.WriteLine("WriteBank: bank={0}", bankNumber);
for (int i = bankSize; i > 0; i--)
{
byte value = CSpectSocket.GetDataByte();
cspect.PokePhysical(physAddress++, new byte[] {value});
}
// Respond
InitData(2);
SetByte(0); // No error
SetByte(0);
CSpectSocket.SendResponse(Data);
}
/**
* Sets a breakpoint and returns an ID (!=0).
* The address is the physical address.
*/
protected static ushort SetBreakpoint(int address)
{
if (Log.Enabled)
Log.WriteLine(" SetBreakpoint: 0x{0:X6}", address);
// Set in CSpect
byte bank = (byte)(address >> 16);
if(bank>0)
{
// Adjust physical address
int physAddress = (address & 0x1FFF) + ((bank - 1) << 13);
Main.CSpect.Debugger(Plugin.eDebugCommand.SetPhysicalBreakpoint, physAddress);
if(Log.Enabled)
Log.WriteLine(" Phys. breakpoint 0x{0:X6}", physAddress);
}
else
{
// Use 64k address
Main.CSpect.Debugger(Plugin.eDebugCommand.SetBreakpoint, address);
if (Log.Enabled)
Log.WriteLine(" Normal breakpoint 0x{0:X4}", address);
}
// Add to array (ID = element position + 1)
BreakpointMap.Add(++LastBreakpointId, address);
return LastBreakpointId;
}
/**
* Removes a breakpoint.
*/
protected static void DeleteBreakpoint(ushort bpId)
{
// Remove
int address;
if (BreakpointMap.TryGetValue(bpId, out address))
{
BreakpointMap.Remove(bpId);
// Clear in CSpect (only if last breakpoint with that address)
if (!BreakpointMap.ContainsValue(address))
{
byte bank = (byte)(address >> 16);
if (bank > 0)
{
// Adjust physical address
address = (address & 0x1FFF) + ((bank - 1) << 13);
Main.CSpect.Debugger(Plugin.eDebugCommand.ClearPhysicalBreakpoint, address);
}
else
{
// Use 64k address
Main.CSpect.Debugger(Plugin.eDebugCommand.ClearBreakpoint, address);
}
}
}
}
/**
* Continues execution.
*/
public static void Continue()
{
var cspect = Main.CSpect;
// Breakpoint 1
bool bp1Enable = (CSpectSocket.GetDataByte() != 0);
ushort bp1Address = CSpectSocket.GetDataWord();
// Breakpoint 2
bool bp2Enable = (CSpectSocket.GetDataByte() != 0);
ushort bp2Address = CSpectSocket.GetDataWord();
// Alternate command?
//AlternateCommand alternateCmd = (AlternateCommand)CSpectSocket.GetDataByte();
AlternateCommand alternateCmd = AlternateCommand.CONTINUE;
switch (alternateCmd)
{
/* Note: Cspect cannot support this because of the strange step-over behavior:
* CSpect step-over does not step-over a conditional jump backwards, e.g. "JP cc, -5"
case AlternateCommand.STEP_OVER: // Step over
ushort address = CSpectSocket.GetDataWord();
ushort endAddress = CSpectSocket.GetDataWord();
// Respond
CSpectSocket.SendResponse();
//ManualBreak = false;
//CpuRunning = true; Need to be locked
//cspect.Debugger(Plugin.eDebugCommand.StepOver);
break;
case AlternateCommand.STEP_OUT: // Step out
// Respond
CSpectSocket.SendResponse();
break;
*/
case AlternateCommand.CONTINUE: // Continue
default:
// Set temporary breakpoints
TmpBreakpoint1 = -1;
if (bp1Enable)
{
TmpBreakpoint1 = bp1Address;
var result = cspect.Debugger(Plugin.eDebugCommand.SetBreakpoint, TmpBreakpoint1);
if (Log.Enabled)
Log.WriteLine(" Set tmp breakpoint 1 at 0x{0:X4}, result={1}", TmpBreakpoint1, result);
}
TmpBreakpoint2 = -1;
if (bp2Enable)
{
TmpBreakpoint2 = bp2Address;
var result = cspect.Debugger(Plugin.eDebugCommand.SetBreakpoint, TmpBreakpoint2);
if (Log.Enabled)
Log.WriteLine(" Set tmp breakpoint 2 at 0x{0:X4}, result={1}", TmpBreakpoint2, result);
}
// Log
if (Log.Enabled)
{
var regs = cspect.GetRegs();
Log.WriteLine("Continue: Run debugger. pc=0x{0:X4}/{0}, bp1=0x{1:X4}/{1}, bp2=0x{2:X4}/{2}", regs.PC, TmpBreakpoint1, TmpBreakpoint2);
}
// Respond
CSpectSocket.SendResponse();
// Run
ManualBreak = false;
StartCpu(true);
break;
}
}
/**
* Does a Step-Over.
* Step-over in a loop until PC is out of the given range.
* The idea is to step over e.g. a macro which consists of several instructions.
*/
public static void StepOver(ushort address, ushort endAddress)
{
var cspect = Main.CSpect;
// Step over
while (true)
{
cspect.Debugger(Plugin.eDebugCommand.StepOver);
var regs = cspect.GetRegs();
if (regs.PC < address || regs.PC >= endAddress)
break;
}
}
/**
* Pauses execution.
*/
public static void Pause()
{
// Pause
if (Log.Enabled)
Log.WriteLine("Pause: Stop debugger.");
ManualBreak = true;
Main.CSpect.Debugger(Plugin.eDebugCommand.Enter);
// Respond
CSpectSocket.SendResponse();
}
/**
* Adds a breakpoint.
*/
public static void AddBreakpoint()
{
// Get breakpoint address
int bpAddr = CSpectSocket.GetLongAddress();
// Set CSpect breakpoint
ushort bpId = SetBreakpoint(bpAddr);
// Respond
InitData(2);
SetWord(bpId);
CSpectSocket.SendResponse(Data);
}
/**
* Removes a breakpoint.
*/
public static void RemoveBreakpoint()
{
// Get breakpoint ID
ushort bpId = CSpectSocket.GetDataWord();
// Remove breakpoint
DeleteBreakpoint(bpId);
// Respond
CSpectSocket.SendResponse();
}
/**
* Adds a watchpoint area.
*/
public static void AddWatchpoint()
{
// Get data
ushort start = CSpectSocket.GetDataWord();
ushort size = CSpectSocket.GetDataWord();
ushort end = (ushort)(start + size);
byte access = CSpectSocket.GetDataByte();
if (Log.Enabled)
Log.WriteLine("AddWatchpoint: address={0:X4}, size={1}", start, size);
// condition is not used
var cspect = Main.CSpect;
// Read
if ((access & 0x01) != 0)
{
for (ushort i = start; i != end; i++)
{
cspect.Debugger(Plugin.eDebugCommand.SetReadBreakpoint, i);
//Log.WriteLine("Read Watchpoint {0}", i);
}
}
// Write
if ((access & 0x02) != 0)
{
for (ushort i = start; i != end; i++)
{
cspect.Debugger(Plugin.eDebugCommand.SetWriteBreakpoint, i);
//Log.WriteLine("Write Watchpoint {0}", i);
}
}
// Respond
CSpectSocket.SendResponse();
}
/**
* Removes a watchpoint area.
*/
public static void RemoveWatchpoint()
{
// Get data
ushort start = CSpectSocket.GetDataWord();
ushort size = CSpectSocket.GetDataWord();
ushort end = (ushort)(start + size);
var cspect = Main.CSpect;
// Remove both read and write
for (ushort i = start; i != end; i++)
{
cspect.Debugger(Plugin.eDebugCommand.ClearReadBreakpoint, i);
cspect.Debugger(Plugin.eDebugCommand.ClearWriteBreakpoint, i);
}
// Respond
CSpectSocket.SendResponse();
}
/**
* Reads a memory area.
*/
public static void ReadMem()
{
// Skip reserved
CSpectSocket.GetDataByte();