-
Notifications
You must be signed in to change notification settings - Fork 8
/
GDBServer.cs
930 lines (757 loc) · 31 KB
/
GDBServer.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// GDB TCP->SIO bridge for Unirom 8
//
// NOTE!
//
// While all of the basic debug functionality is present, the GDB
// bridge is still very much a work in progress!
//
// Many thanks to Nicolas "Pixel" Noble
// This GDB server is heavily based on his GDB server implementation
// in PCSX-Redux. Some bits were borrowed and wisdom called upon
// when things broke.
// -John "Skitchin" Baumann
// TODO: set running/halted state when reconnecting
// TODO: Handle software breakpoints internally?
// If we break/step on a BD, the original branch instruction must be
// the next PC. We could just lie to GDB about our PC but gdb will
// try to shove it's software breakpoint in place.
// TODO: Add 4 to the PC if we're in a branch delay slot. (see above first)
// TODO: Split GDB server code from emulation logic, cache, etc?
// TODO: Continue and Step both take optional address arguments, needs testing.
// TODO: Begin moving non-gdb-specific code to new classes
// i.e. CPUHelper for emulation, calculating branches, etc
using System;
using System.Text;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Globalization;
using System.Collections.Generic;
public class GDBServer {
private static bool _enabled = false;
public static bool IsEnabled => _enabled;
private static bool emulate_steps = false;
private static Dictionary<UInt32, UInt32> original_opcode = new Dictionary<UInt32, UInt32>();
public static TargetDataPort serial => Program.activeSerial;
private static bool ack_enabled = true;
private static bool manifest_enabled = true;
const string memoryMap = @"<?xml version=""1.0""?>
<memory-map>
<!-- Everything here is described as RAM, because we don't really
have any better option. -->
<!-- Main memory bloc: let's go with 8MB straight off the bat. -->
<memory type=""ram"" start=""0x0000000000000000"" length=""0x800000""/>
<memory type=""ram"" start=""0xffffffff80000000"" length=""0x800000""/>
<memory type=""ram"" start=""0xffffffffa0000000"" length=""0x800000""/>
<!-- EXP1 can go up to 8MB too. -->
<memory type=""ram"" start=""0x000000001f000000"" length=""0x800000""/>
<memory type=""ram"" start=""0xffffffff9f000000"" length=""0x800000""/>
<memory type=""ram"" start=""0xffffffffbf000000"" length=""0x800000""/>
<!-- Scratchpad -->
<memory type=""ram"" start=""0x000000001f800000"" length=""0x400""/>
<memory type=""ram"" start=""0xffffffff9f800000"" length=""0x400""/>
<!-- Hardware registers -->
<memory type=""ram"" start=""0x000000001f801000"" length=""0x2000""/>
<memory type=""ram"" start=""0xffffffff9f801000"" length=""0x2000""/>
<memory type=""ram"" start=""0xffffffffbf801000"" length=""0x2000""/>
<!-- DTL BIOS SRAM -->
<memory type=""ram"" start=""0x000000001fa00000"" length=""0x200000""/>
<memory type=""ram"" start=""0xffffffff9fa00000"" length=""0x200000""/>
<memory type=""ram"" start=""0xffffffffbfa00000"" length=""0x200000""/>
<!-- BIOS -->
<memory type=""ram"" start=""0x000000001fc00000"" length=""0x80000""/>
<memory type=""ram"" start=""0xffffffff9fc00000"" length=""0x80000""/>
<memory type=""ram"" start=""0xffffffffbfc00000"" length=""0x80000""/>
<!-- This really is only for 0xfffe0130 -->
<memory type=""ram"" start=""0xfffffffffffe0000"" length=""0x200""/>
</memory-map>
";
const string targetXML = @"<?xml version=""1.0""?>
<!DOCTYPE feature SYSTEM ""gdb-target.dtd"">
<target version=""1.0"">
<!-- Helping GDB -->
<architecture>mips:3000</architecture>
<osabi>none</osabi>
<!-- Mapping ought to be flexible, but there seems to be some
hardcoded parts in gdb, so let's use the same mapping. -->
<feature name=""org.gnu.gdb.mips.cpu"">
<reg name=""r0"" bitsize=""32"" regnum=""0""/>
<reg name=""r1"" bitsize=""32""/>
<reg name=""r2"" bitsize=""32""/>
<reg name=""r3"" bitsize=""32""/>
<reg name=""r4"" bitsize=""32""/>
<reg name=""r5"" bitsize=""32""/>
<reg name=""r6"" bitsize=""32""/>
<reg name=""r7"" bitsize=""32""/>
<reg name=""r8"" bitsize=""32""/>
<reg name=""r9"" bitsize=""32""/>
<reg name=""r10"" bitsize=""32""/>
<reg name=""r11"" bitsize=""32""/>
<reg name=""r12"" bitsize=""32""/>
<reg name=""r13"" bitsize=""32""/>
<reg name=""r14"" bitsize=""32""/>
<reg name=""r15"" bitsize=""32""/>
<reg name=""r16"" bitsize=""32""/>
<reg name=""r17"" bitsize=""32""/>
<reg name=""r18"" bitsize=""32""/>
<reg name=""r19"" bitsize=""32""/>
<reg name=""r20"" bitsize=""32""/>
<reg name=""r21"" bitsize=""32""/>
<reg name=""r22"" bitsize=""32""/>
<reg name=""r23"" bitsize=""32""/>
<reg name=""r24"" bitsize=""32""/>
<reg name=""r25"" bitsize=""32""/>
<reg name=""r26"" bitsize=""32""/>
<reg name=""r27"" bitsize=""32""/>
<reg name=""r28"" bitsize=""32""/>
<reg name=""r29"" bitsize=""32""/>
<reg name=""r30"" bitsize=""32""/>
<reg name=""r31"" bitsize=""32""/>
<reg name=""lo"" bitsize=""32"" regnum=""33""/>
<reg name=""hi"" bitsize=""32"" regnum=""34""/>
<reg name=""pc"" bitsize=""32"" regnum=""37""/>
</feature>
<feature name=""org.gnu.gdb.mips.cp0"">
<reg name=""status"" bitsize=""32"" regnum=""32""/>
<reg name=""badvaddr"" bitsize=""32"" regnum=""35""/>
<reg name=""cause"" bitsize=""32"" regnum=""36""/>
</feature>
<!-- We don't have an FPU, but gdb hardcodes one, and will choke
if this section isn't present. -->
<feature name=""org.gnu.gdb.mips.fpu"">
<reg name=""f0"" bitsize=""32"" type=""ieee_single"" regnum=""38""/>
<reg name=""f1"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f2"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f3"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f4"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f5"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f6"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f7"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f8"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f9"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f10"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f11"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f12"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f13"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f14"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f15"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f16"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f17"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f18"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f19"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f20"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f21"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f22"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f23"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f24"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f25"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f26"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f27"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f28"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f29"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f30"" bitsize=""32"" type=""ieee_single""/>
<reg name=""f31"" bitsize=""32"" type=""ieee_single""/>
<reg name=""fcsr"" bitsize=""32"" group=""float""/>
<reg name=""fir"" bitsize=""32"" group=""float""/>
</feature>
</target>
";
// For joining parts of the TCP stream
// TODO: there's no checks to stop this getting out of hand
private static bool stitchingPacketsTogether = false;
private static string activePacketString = "";
/// <summary>
/// Calculate the checksum for the packet
/// </summary>
/// <param name="packet"></param>
/// <returns></returns>
private static string CalculateChecksum( string packet ) {
byte checksum = 0;
foreach ( char c in packet ) {
checksum += (byte)c;
}
//checksum %= (byte)256;
return checksum.ToString( "X2" );
}
private static void Continue( string data ) {
// TODO: specify an addr?
Log.WriteLine( "Got continue request", LogType.Debug );
if ( data.Length == 9 ) {
// UNTESTED
// To-do: Test it.
// Got memory address to continue to
Log.WriteLine( "Got memory address to continue to", LogType.Debug );
CPU.SetHardwareBreakpoint( UInt32.Parse( data.Substring( 1, 8 ), NumberStyles.HexNumber ) );
}
lock ( SerialTarget.serialLock ) {
if ( TransferLogic.Cont( false ) ) {
SetHaltStateInternal( CPU.HaltState.RUNNING, false );
}
}
SendGDBResponse( "OK" );
}
/// <summary>
/// Respond to GDB Detach 'D' packet
/// </summary>
private static void Detach() {
Log.WriteLine( "Detaching from target...", LogType.Info );
// Do some stuff to detach from the target
// Close & restart server
ResetConnection();
SendGDBResponse( "OK" );
}
public static void DisableManifest() {
manifest_enabled = false;
}
/// <summary>
/// Respond to GDB Extended Mode '!' packet
/// </summary>
private static void EnableExtendedMode() {
SendGDBResponse( "OK" );
}
/*public static void EnableManifest() {
manifest_enabled = true;
}*/
/// <summary>
/// Attempt to locate an instruction from cache,
/// otherwise grab it from ram.
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static UInt32 GetInstructionCached( UInt32 address ) {
byte[] read_buffer = new byte[ 4 ];
UInt32 opcode = 0;
if ( original_opcode.ContainsKey( address ) ) {
opcode = original_opcode[ address ];
} else {
if ( GetMemory( address, 4, read_buffer ) ) {
// To-do: Maybe grab larger chunks and parse
opcode = BitConverter.ToUInt32( read_buffer, 0 );
original_opcode[ address ] = opcode;
}
}
return opcode;
}
/// <summary>
/// Grab data from Unirom
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <param name="data"></param>
/// <returns></returns>
public static bool GetMemory( uint address, uint length, byte[] data ) {
Log.WriteLine( "Getting memory from 0x" + address.ToString( "X8" ) + " for " + length.ToString() + " bytes", LogType.Debug );
lock ( SerialTarget.serialLock ) {
if ( !TransferLogic.ReadBytes( address, length, data ) ) {
Log.WriteLine( "Couldn't read bytes from Unirom!", LogType.Error );
return false;
}
}
return true;
}
/// <summary>
/// Convert a hex nibble char to a byte
/// </summary>
/// <param name="inChar"></param>
/// <returns></returns>
private static byte GimmeNibble( char inChar ) {
// TODO: lol not this
/*switch ( inChar ) {
case '0': return 0x0;
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0x4;
case '5': return 0x5;
case '6': return 0x6;
case '7': return 0x7;
case '8': return 0x8;
case '9': return 0x9;
case 'A': case 'a': return 0xA;
case 'B': case 'b': return 0xB;
case 'C': case 'c': return 0xC;
case 'D': case 'd': return 0xD;
case 'E': case 'e': return 0xE;
case 'F': case 'f': return 0xF;
}
return 0;*/
// Maybe this instead?
if ( inChar >= '0' && inChar <= '9' )
return (byte)(inChar - 48);
else if ( inChar >= 'A' && inChar <= 'F' )
return (byte)(inChar - 55);
else if ( inChar >= 'a' && inChar <= 'f' )
return (byte)(inChar - 87);
// Not a valid hex char
else return 0;
}
/// <summary>
/// User pressed Ctrl+C, do a thing
/// </summary>
private static void HandleCtrlC() {
lock ( SerialTarget.serialLock ) {
if ( TransferLogic.Halt( false ) )
SetHaltStateInternal( CPU.HaltState.HALT, true );
}
}
/// <summary>
/// Double check that the console's there
/// when starting up
/// </summary>
public static void Init() {
Log.WriteLine( "Checking if Unirom is in debug mode...", LogType.Debug );
// if it returns true, we might enter /m (monitor) mode, etc
if (
!TransferLogic.ChallengeResponse( CommandMode.DEBUG )
) {
Log.WriteLine( "Couldn't determine if Unirom is in debug mode.", LogType.Error );
return;
}
// More of a test than a requirement...
Log.WriteLine( "Grabbing initial state...", LogType.Debug );
CPU.DumpRegs();
Log.WriteLine( "GDB server initialised" );
_enabled = true;
}
/// <summary>
/// Respond to GDB Memory Read 'm' packet
/// </summary>
/// <param name="data"></param>
private static void MemoryRead( string data ) {
string[] parts = data.Substring( 1 ).Split( ',' );
uint address = uint.Parse( parts[ 0 ], System.Globalization.NumberStyles.HexNumber );
uint length = uint.Parse( parts[ 1 ], System.Globalization.NumberStyles.HexNumber );
byte[] read_buffer = new byte[ length ];
string response = "";
//ReadCached( address, length, read_buffer );
GetMemory( address, length, read_buffer );
for ( uint i = 0; i < length; i++ ) {
response += read_buffer[ i ].ToString( "X2" );
}
//Console.WriteLine( "MemoryRead @ 0x"+ address.ToString("X8") + ":" + response );
SendGDBResponse( response );
}
/// <summary>
/// Parse and upload an $M packet - e.g. as a result of `load` in GDB
/// </summary>
/// <param name="data"></param>
private static void MemoryWrite( string data ) {
// TODO: validate memory regions
UInt32 address = UInt32.Parse( data.Substring( 1, data.IndexOf(",") - 1 ), NumberStyles.HexNumber );
// Where in the string do we find the addr substring
int sizeStart = data.IndexOf( "," ) + 1;
int sizeEnd = data.IndexOf( ":" );
UInt32 length = UInt32.Parse( data.Substring( sizeStart, (sizeEnd - sizeStart) ), NumberStyles.HexNumber );
byte[] bytes_out = ParseHexBytes( data, sizeEnd + 1, length );
if ( !original_opcode.ContainsKey( address ) ) {
PareseToCache( address, length, bytes_out );
}
lock ( SerialTarget.serialLock ) {
TransferLogic.Command_SendBin( address, bytes_out );
}
SendGDBResponse( "OK" );
}
private static void PareseToCache( UInt32 address, UInt32 length, byte[] read_buffer ) {
UInt32 instruction;
for ( uint i = 0; i < length; i += 4 ) {
if ( length - i < 4 )
break; // derp?
instruction = BitConverter.ToUInt32( read_buffer, (int)i );
if ( !original_opcode.ContainsKey( address + i ) && !CPU.IsBreakInstruction( instruction ) ) {
original_opcode[ address + i ] = instruction;
}
}
}
/// <summary>
/// Parse a string of hex bytes (no preceding 0x)
/// </summary>
/// <param name="inString"></param>
/// <param name="startChar"></param>
/// <param name="numBytesToRead"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public static byte[] ParseHexBytes( string inString, int startChar, UInt32 numBytesToRead ) {
if ( inString.Length < startChar + (numBytesToRead * 2) ) {
throw new IndexOutOfRangeException( "Input string is too short!" );
}
byte[] outBytes = new byte[ numBytesToRead ];
byte activeByte;
int charPos = 0;
for ( int i = startChar; i < startChar + (numBytesToRead * 2); i += 2 ) {
char first = inString[ i ];
char second = inString[ i + 1 ];
activeByte = (byte)((GimmeNibble( first ) << 4) | GimmeNibble( second ));
outBytes[ charPos++ ] = activeByte;
}
return outBytes;
}
/// <summary>
/// Receive a command and do some stuff with it
/// </summary>
/// <param name="data"></param>
private static void ProcessCommand( string data ) {
//Console.WriteLine( "Got command " + data );
switch ( data[ 0 ] ) {
case '!':
EnableExtendedMode();
break;
case '?':
QueryHaltReason();
break;
case 'c': // Continue - c [addr]
Continue( data );
break;
case 's': // Step - s [addr]
lock ( SerialTarget.serialLock ) {
Step( data, emulate_steps );
}
break;
case 'D': // Detach
Detach();
break;
case 'g': // Get registers
ReadRegisters();
break;
case 'G': // Write registers
WriteRegisters( data );
break;
case 'H': // thread stuff
if ( data.StartsWith( "Hc0" ) ) {
SendGDBResponse( "OK" );
} else if ( data.StartsWith( "Hc-1" ) ) {
SendGDBResponse( "OK" );
} else if ( data.StartsWith( "Hg0" ) ) {
SendGDBResponse( "OK" );
} else Unimplemented( data );
break;
case 'm': // Read memory
MemoryRead( data );
break;
case 'M': // Write memory
MemoryWrite( data );
break;
case 'p': // Read single register
ReadRegister( data );
break;
case 'P': // Write single register
WriteRegister( data );
break;
case 'q':
if ( data.StartsWith( "qAttached" ) ) {
SendGDBResponse( "1" );
} else if ( data.StartsWith( "qC" ) ) {
// Get Thread ID, always 00
SendGDBResponse( "QC00" );
} else if ( data.StartsWith( "qSupported" ) ) {
if ( manifest_enabled ) {
SendGDBResponse( "PacketSize=4000;qXfer:features:read+;qXfer:threads:read+;qXfer:memory-map:read+;QStartNoAckMode+" );
} else {
SendGDBResponse( "PacketSize=4000;qXfer:threads:read+;QStartNoAckMode+" );
}
} else if ( manifest_enabled && data.StartsWith( "qXfer:features:read:target.xml:" ) ) {
SendPagedResponse( targetXML );
} else if ( data.StartsWith( "qXfer:memory-map:read::" ) ) {
SendPagedResponse( memoryMap );
} else if ( data.StartsWith( "qXfer:threads:read::" ) ) {
SendPagedResponse( "<?xml version=\"1.0\"?><threads></threads>" );
} else if ( data.StartsWith( "qRcmd" ) ) {
// To-do: Process monitor commands
ProcessMonitorCommand( data );
} else Unimplemented( data );
break;
case 'Q':
if ( data.StartsWith( "QStartNoAckMode" ) ) {
SendGDBResponse( "OK" );
ack_enabled = false;
} else Unimplemented( data );
break;
case 'v':
if ( data.StartsWith( "vAttach" ) ) {
//
Unimplemented( data );
} else if ( data.StartsWith( "vMustReplyEmpty" ) ) {
SendGDBResponse( "" );
} else if ( data.StartsWith( "vKill;" ) ) {
// Kill the process
SendGDBResponse( "OK" );
} else Unimplemented( data );
break;
case 'X':
// Write data to memory
// E.g. to signal the start of mem writes with
// $Xffffffff8000f800,0:#e4
//Console.WriteLine( "Pausing the PSX for uploads..." );
lock ( SerialTarget.serialLock ) {
TransferLogic.ChallengeResponse( CommandMode.HALT );
}
SendGDBResponse( "" );
break;
// Comment out, let GDB manage writing breakpoints
// To-do: Consider tracking/setting breakpoints in our GDB stub
/*case 'Z':
// Set breakpoint
SetBreakpoint( data );
break;
case 'z':
SendGDBResponse( "" );
break;*/
default:
Unimplemented( data );
break;
}
}
/// <summary>
/// Get data and do a thing with it
/// </summary>
/// <param name="Data"></param>
public static void ProcessData( string Data ) {
char[] packet = Data.ToCharArray();
string packetData = "";
string our_checksum = "0";
int offset = 0;
int size = Data.Length;
// This one isn't sent in plain text
if ( Data[ 0 ] == (byte)0x03 ) {
//Console.WriteLine( "Got a ^C" );
HandleCtrlC();
return;
}
// TODO: this could maybe be done nicer?
if ( stitchingPacketsTogether ) {
// rip GC, #yolo
//Console.WriteLine( "Adding partial packet, len= " + Data.Length );
activePacketString += Data;
// did we reach the end?
if ( Data.IndexOf( "#" ) == Data.Length - 2 - 1 ) {
stitchingPacketsTogether = false;
// now re-call this function with the completed packet
ProcessData( activePacketString );
}
return;
}
//Console.WriteLine( "Processing data: " + Data );
while ( size > 0 ) {
char c = packet[ offset++ ];
size--;
if ( c == '+' ) {
SendAck();
}
if ( c == '$' ) {
int end = Data.IndexOf( '#', offset );
if ( end == -1 ) {
//Console.WriteLine( "Partial packet, len=" + Data.Length );
stitchingPacketsTogether = true;
activePacketString = Data;
return;
}
packetData = Data.Substring( offset, end - offset );
//Console.WriteLine( "Packet data: " + packetData );
our_checksum = CalculateChecksum( packetData );
size -= (end - offset);
offset = end;
} else if ( c == '#' ) {
string checksum = Data.Substring( offset, 2 );
//Console.WriteLine( "Checksum: " + checksum );
//Console.WriteLine( "Our checksum: " + our_checksum );
if ( checksum.ToUpper().Equals( our_checksum ) ) {
//Console.WriteLine( "Checksums match!" );
if ( ack_enabled )
SendAck();
ProcessCommand( packetData );
//Bridge.Send( "$" + packetData + "#" + CalculateChecksum( packetData ));
//ProcessPacket( packetData );
} else {
Log.WriteLine( "Checksums don't match!", LogType.Error );
}
offset += 2;
size -= 3;
} else if ( c == '-' ) {
Log.WriteLine( "Negative ACK", LogType.Error );
}
}
}
private static void ProcessMonitorCommand( string data ) {
Log.WriteLine( "Got qRcmd: " + data, LogType.Debug );
SendGDBResponse( "OK" );
}
/// <summary>
/// Respond to GDB Query '?' packet
/// </summary>
private static void QueryHaltReason() {
switch ( CPU.GetHaltState() ) {
case CPU.HaltState.RUNNING: SendGDBResponse( "S00" ); break;
case CPU.HaltState.HALT: SendGDBResponse( "S05" ); break;
}
}
private static void ReadCached( UInt32 address, UInt32 length, byte[] read_buffer ) {
UInt32 instruction;
// Check for data 4 bytes at a time
// If not found, fetch memory and push it to cache + buffer
// Just grab the whole chunk for now if we don't have the start
if ( original_opcode.ContainsKey( address ) ) {
for ( uint i = 0; i < length; i += 4 ) {
instruction = GetInstructionCached( address + i );
Array.Copy( BitConverter.GetBytes( instruction ), 0, read_buffer, i, (length - i < 4) ? length - i : 4 );
}
} else {
GetMemory( address, length, read_buffer );
PareseToCache( address, length, read_buffer );
}
}
/// <summary>
/// Respond to GDB Read Register 'p' packet
/// </summary>
/// <param name="data"></param>
private static void ReadRegister( string data ) {
if ( (data.Length != 12) || (data.Substring( 3, 1 ) != "=") ) {
SendGDBResponse( "E00" );
} else {
uint reg_num = uint.Parse( data.Substring( 1, 2 ), System.Globalization.NumberStyles.HexNumber );
lock ( SerialTarget.serialLock ) {
bool wasRunning = CPU.GetHaltState() == CPU.HaltState.RUNNING;
if ( wasRunning )
TransferLogic.Halt( false );
CPU.GetRegs();
if ( wasRunning )
TransferLogic.Cont( false );
}
CPU.GetOneRegisterBE( reg_num ).ToString( "X8" );
}
}
/// <summary>
/// Respond to GDB Read Register 'g' packet
/// </summary>
private static void ReadRegisters() {
string register_data = "";
CPU.GetRegs();
for ( uint i = 0; i < 72; i++ )
register_data += CPU.GetOneRegisterBE( i ).ToString( "X8" );
SendGDBResponse( register_data );
}
public static void ResetConnection() {
ack_enabled = true;
}
/// <summary>
/// Send GDB a packet acknowledgement(only in ack mode)
/// </summary>
private static void SendAck() {
Bridge.Send( "+" );
}
/// <summary>
/// The main function used for replying to GDB
/// </summary>
/// <param name="response"></param>
private static void SendGDBResponse( string response ) {
Bridge.Send( "$" + response + "#" + CalculateChecksum( response ) );
}
/// <summary>
/// Send GDB a Paged response
/// </summary>
/// <param name="response"></param>
private static void SendPagedResponse( string response ) {
Bridge.Send( "$l" + response + "#" + CalculateChecksum( response ) );
}
/// <summary>
/// Set the console's state
/// </summary>
/// <param name="inState"></param>
/// <param name="notifyGDB"></param>
public static void SetHaltStateInternal( CPU.HaltState inState, bool notifyGDB ) {
CPU.SetHaltState( inState );
if ( notifyGDB ) {
if ( CPU.GetHaltState() == CPU.HaltState.RUNNING ) {
SendGDBResponse( "S00" );
} else {
SendGDBResponse( "S05" );
}
}
}
/// <summary>
/// Respond to a GDB Step 's' packet
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static void Step( string data, bool use_emulation ) {
if ( data.Length > 1 ) {
Log.WriteLine( "Hrm?", LogType.Debug );
}
// This isn't really fleshed out, disabled for now.
// Attempt to emulate instructions internally rather than firing them on console
// If there is something we can't handle, recurse with use_emulation = false
if ( use_emulation ) {
CPU.EmulateStep();
// Notify GDB of "halt"?
} else {
/*if ( data.Length == 9 ) {
// UNTESTED
// To-do: Test it.
// Got memory address to step to
Log.ToScreen( "Got memory address to step to", LogType.Debug );
next_pc = UInt32.Parse( data.Substring( 1, 8 ), NumberStyles.HexNumber );
} else {
}*/
CPU.HardwareStep();
SendGDBResponse( "OK" );
if ( TransferLogic.Cont( false ) ) {
SetHaltStateInternal( CPU.HaltState.RUNNING, false );
}
}
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
private static void Unimplemented( string data ) {
SendGDBResponse( "" );
Log.WriteLine( "Got unimplemented gdb command " + data + ", reply empty", LogType.Debug );
}
/// <summary>
/// Respond to GDB Write Register 'P' packet
/// </summary>
/// <param name="data"></param>
private static void WriteRegister( string data ) {
if ( (data.Length != 12) || (data.Substring( 3, 1 ) != "=") ) {
SendGDBResponse( "E00" );
} else {
uint reg_num = uint.Parse( data.Substring( 1, 2 ), System.Globalization.NumberStyles.HexNumber );
uint reg_value = uint.Parse( data.Substring( 4, 8 ), System.Globalization.NumberStyles.HexNumber );
lock ( SerialTarget.serialLock ) {
bool wasRunning = CPU.GetHaltState() == CPU.HaltState.RUNNING;
if ( wasRunning )
TransferLogic.Halt( false );
//GetRegs(); // Request registers from Unirom
CPU.SetOneRegisterBE( reg_num, reg_value ); // Set the register
CPU.SetRegs(); // Send registers to Unirom
if ( wasRunning )
TransferLogic.Cont( false );
}
SendGDBResponse( "OK" );
}
}
/// <summary>
/// Respond to GDB Write Registers 'G' packet
/// </summary>
/// <param name="data"></param>
private static void WriteRegisters( string data ) {
uint length = (uint)data.Length - 1;
lock ( SerialTarget.serialLock ) {
bool wasRunning = CPU.GetHaltState() == CPU.HaltState.RUNNING;
if ( wasRunning )
TransferLogic.Halt( false );
//GetRegs();
for ( uint i = 0; i < length; i += 8 ) {
uint reg_num = i / 8;
uint reg_value = uint.Parse( data.Substring( (int)i + 1, 8 ), System.Globalization.NumberStyles.HexNumber );
CPU.SetOneRegisterBE( reg_num, reg_value );
}
CPU.SetRegs();
if ( wasRunning )
TransferLogic.Cont( false );
}
}
}