forked from JonathanDotCel/NOTPSXSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NOTPSXSERIAL.CS
1550 lines (1172 loc) · 52 KB
/
NOTPSXSERIAL.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
// 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/.
//
// Additionally, the ELFSharp binary is licensed under the terms
// of the LLVM Release license. See ELFSharp_license.txt
//
// NoPS - NotPsxSerial
// Feb 2020 - github.com/JonathanDotCel
//
//
// This is the PC-side companion to Unirom8 with backwards compat for Unirom7/PSXSerial format.
//
// 03_05_2020 - Release 2
// - Added the continous hex dump mode
// - Removed deprecated response enums
//
// 10_05_2020 - Release 3
// - Added the /fast option for 512k baud
// - Ability to use smaller chunks for CP210x UARTs, etc
// - Hidden /verify switch for checking faster connections
// - Linux support thanks to r0r0
//
// 26_05_2020 - Release 4
// - Now supports binary upload/download during gameplay through Unirom 8.0.B4
// /debug to enable this via Unirom's debug mode
// - /reset to reset the machine
// - /fast improvements on the other end
// - "unsp" response for unsupported command (unirom in debug mode)
// - Remembers your last-used com port
//
// 18_06_2020 - Release 5
// - .exe file size is now calculated as ( total - 0x800 )
// - 2x stop bits by default for all modes now
// - fast sio will now use odd parity
//
// 15_08_2020 - Release 9
// - restructuring for GDB implementation
// - removed the /verify command
// - ping/pong
// - faster startup
// - halt / cont commands
// - warnings for no dbg or dbg only
//
// ??_09_2020 - Release 10 - (~Unirom 8.0.C)
// - poke8, poke16, poke32 commands
// - hookread, hookwrite, hookex
// 20_09_2020 - Release 11 - (~8.0.D)
// - poke8, poke16, poke32 commands
// - hookread, hookwrite, hookex
// - /dump now has a V2 variant
// Note: Some seriously incomplete GDB stuff in the works!
// 21_10_2020 - Spooky Release 12 - (~8.0.E)
// - clearer errors
// - read32 function
// - tidied up some misused vars
// - memory card control
// - condensed the custom attributes
// - some new custom attributes
// - a wee bit more code documentation
// 05_01_2021 - Near near new nops
// - Made things more mac/unix friendly
// - Proper macos detection + scripts
// - lowercased NoPS.EXE for everyone's sanity
// - moved some things to the utils class
//
// 00_03_2020 - Release 14 - (~8.0.E)
// - Distinction between /gdb and /bridge
//
// 05_01_2021 - Release 15 - (~8.0.J)
// - Proper TCP/Serial abstraction, thanks Skitchin!
// - /Bridge mode now takes an IP so you can bind e.g. 192.168.0.4
//
// 28_08_2022 - Release 16 - (~8.0.K)
// - Skitchin's GDB stuff
// See github for further release info.
#define DebugArgs
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Linq;
using static Utils;
public enum CommandMode {
NOT_SET,
// Upload
[Command( "/bin" )]
[NeedsInputFile]
[NeedsAddress]
[Challenge( "SBIN" )]
[Response( "OKAY" )]
SEND_BIN,
[Command( "/rom" )]
[NeedsInputFile]
[Challenge( "SROM" )]
[Response( "OKAY" )]
SEND_ROM,
// Flow control
[Command( "/exe" )]
[NeedsInputFile]
[Challenge( "SEXE" )]
[Response( "OKAY" )]
SEND_EXE,
[Command( "/jmp" )]
[Challenge( "JUMP" )]
[Response( "OKAY" )]
[NeedsAddress]
JUMP_JMP,
[Command( "/jal" )]
[Challenge( "CALL" )]
[Response( "OKAY" )]
[NeedsAddress]
JUMP_CALL,
[Command( "/dump" )]
[Challenge( "DUMP" )]
[Response( "OKAY" )]
[NeedsAddress]
[NeedsSize]
[NeedsOutputFile]
DUMP,
// Poke commands
[Command( "/poke8" )]
[Challenge( "SBIN" )]
[Response( "OKAY" )]
[NeedsAddress]
[NeedsValue]
POKE8,
[Command( "/poke16" )]
[Challenge( "SBIN" )]
[Response( "OKAY" )]
[NeedsAddress]
[NeedsValue]
POKE16,
[Command( "/poke32" )]
[Challenge( "SBIN" )]
[Response( "OKAY" )]
[NeedsAddress]
[NeedsValue]
POKE32,
// Peek
[Command( "/watch" )]
[Challenge( "HEXD" )]
[Response( "OKAY" )]
[NeedsAddress]
[NeedsSize]
WATCH,
// Various
[Command( "/reset" )]
[Challenge( "REST" )]
[Response( "OKAY" )]
RESET,
[Command( "/ping" )]
[Challenge( "PING" )]
[Response( "PONG" )]
PING,
// Debug mode functions
// install Unirom kernel-resident debug SIO
[Command( "/debug" )]
[Challenge( "DEBG" )]
[Response( "OKAY" )]
DEBUG,
// UART-TCP bridge
[Command( "/bridge" )]
[NeedsIP]
[NeedsPort]
[Challenge( "NONE" )]
[Response( "OKAY" )]
BRIDGE,
// combines /debug and /bridge
[Command( "/gdb" )]
[NeedsIP]
[NeedsPort]
[Challenge( "DEBG" )]
[Response( "OKAY" )]
GDB,
// don't send manifest, mainly for IDA
[Command("/nomanifest")]
NOMANIFEST,
// Verbose logging
[Command("/v")]
VERBOSE,
[Command( "/halt" )]
[Challenge( "HALT" )]
[Response( "HLTD" )]
HALT,
[Command( "/cont" )]
[Challenge( "CONT" )]
[Response( "OKAY" )]
CONT,
[Command( "/regs" )]
REGS,
[Command( "/setreg" )]
[NeedsRegister]
[NeedsValue] // abused as a 32bit hexa value
SETREG,
// Debug hooks
[Command( "/hookread" )]
[NeedsAddress]
[Challenge( "HKRD" )]
[Response( "OKAY" )]
HOOKREAD,
[Command( "/hookwrite" )]
[NeedsAddress]
[Challenge( "HKWR" )]
[Response( "OKAY" )]
HOOKWRITE,
[Command( "/hookex" )]
[NeedsAddress]
[Challenge( "HKEX" )]
[Response( "OKAY" )]
HOOKEXEC,
[Command( "/unhook" )]
[Challenge( "UNHK" )]
[Response( "OKAY" )]
UNHOOK,
// Memcard
[Command( "/mcdown" )]
[NeedsCardNumber]
[NeedsOutputFile]
[Challenge( "MCDN" )]
[Response( "OKAY" )]
MCDOWN,
[Command( "/mcup" )]
[NeedsCardNumber]
[NeedsInputFile]
[Challenge( "MCUP" )]
[Response( "OKAY" )]
MCUP,
[Command( "/wipemem" )]
[NeedsValue]
[NeedsAddress]
WIPEMEM,
[Command( "/stdin" )]
[NeedsString]
[Challenge( "STDI" )]
[Response( "STOK" )]
STDIN,
[Command( "/type" )]
[NeedsString]
TYPESTRING,
/*
// Deprecated as of 8.0.C's self-correcting algo
[Command( "/verify" )]
[Challenge("SBIN")]
[Response("OKAY")]
[NeedsInputFile]
[NeedsAddress]
VERIFY,
*/
COUNT
};
public enum SIOSPEED {
SLOW, // default 115200 baud
FAST, // about half a meg
FTDI // using the FTDI mod for the Power Replay carts
}
internal partial class Program {
const string VERSION = "v15 (8.0.L)";
const int TIMEOUT = 500;
public static void PrintUsage( bool justTheTip = false ) {
#if !DebugArgs
if( !justTheTip ){
Console.Clear();
}
#endif
SetDefaultColour();
// assuming 80 columns
Log.Write( "\n" );
Log.Write( "================================================================================\n" );
Log.Write( " Totally NOtPsxSerial " + VERSION + "\n" );
Log.Write( " Thanks: Jihad of HITMEN, Shendo, Type79, Dax, \n" );
Log.Write( " r0r0, Skitchin, danhans42, Schnappy, \n" );
Log.Write( " Nicolas Noble, T0fuZ & Arthur!\n" );
Log.Write( "\n" );
Log.Write( " Instructions : http://unirom.github.io\n" );
Log.Write( " Discord : http://psx.dev\n" );
Log.Write( "================================================================================\n" );
Log.Write( "\n" );
Log.Write( " Note: You may have to install mono and launch via 'mono nops.exe /args' if...\n" );
Log.Write( " - Windows cant put your serial device (FTDI, etc) into 115200baud\n" );
Log.Write( " - You are using a Sharklink/Net Yaroze cable\n" );
Log.Write( " - Any flavour of *nix/OSX\n" );
Log.Write( "\n\n" );
if ( justTheTip ) return;
// COM8, /dev/tty*, /dev/cu.usbserial*, etc
suggestedComPort = SampleCOMPortForEnvironment;
Log.Write( " Usage : NOPS.EXE [/args] [FILENAME] [COMPORT]\n" );
Log.Write( " Usage : NOPS.EXE [/args] [FILENAME] [IPADDRESS:PORT]\n\n" );
Log.Write( " Bridge: NOPS.EXE /bridge [LOCALIP:PORT] [COMPORT]\n" );
Log.Write( " bridges e.g. a local serial device and port, allowing remote connections\n" );
Log.Write( "\n\n" );
Log.Write( $" Send an .EXE : NOPS.EXE /exe <FILE.EXE> {suggestedComPort}\n\n" );
Log.Write( $" Flash a .ROM : NOPS.EXE /rom <FILE.ROM> {suggestedComPort}\n\n" );
Log.Write( $" Send a .BIN : NOPS.EXE /bin 0xADDRESS0 <FILE.BIN> {suggestedComPort}\n\n" );
Log.Write( $" Jump addr (jr) : NOPS.EXE /jmp 0xADDRESS0 {suggestedComPort}\n" );
Log.Write( $" Call addr (jal): NOPS.EXE /jal 0xADDRESS0 {suggestedComPort}\n\n" );
Log.Write($" Dumpy Stuff:\n");
Log.Write($" RAM ( 2m) : NOPS.EXE /dump 0x80000000 0x200000 <DUMP.BIN> {suggestedComPort}\n");
Log.Write($" ROM (128k) : NOPS.EXE /dump 0x1F000000 0x20000 <DUMP.BIN> {suggestedComPort}\n");
Log.Write($" ROM (384k) : NOPS.EXE /dump 0x1F000000 0x60000 <DUMP.BIN> {suggestedComPort}\n");
Log.Write($" ROM (512k) : NOPS.EXE /dump 0x1F000000 0x80000 <DUMP.BIN> {suggestedComPort}\n");
Log.Write($" BIOS (512k) : NOPS.EXE /dump 0xBFC00000 0x80000 <DUMP.BIN> {suggestedComPort}\n");
Log.Write($" SPAD ( 1k) : NOPS.EXE /dump 0x1F800000 0x400 <DUMP.BIN> {suggestedComPort}\n\n");
Log.Write($" WARNING! If named dump file already exist it will be overwritten!\n");
Log.Write($" Also note that you can use * for <DUMP.BIN> to have automagically generated filename\n\n");
Log.Write($" Memory Cards:\n");
Log.Write($" MC0->File : NOPS.EXE /mcdown 0 filename.mcr {suggestedComPort}\n");
Log.Write($" MC1->File : NOPS.EXE /mcdown 1 filename.mcr {suggestedComPort}\n");
Log.Write($" File->MC0 : NOPS.EXE /mcup 0 filename.mcr {suggestedComPort}\n\n");
Log.Write($" Pokey Poke:\n");
Log.Write($" 8 bits : NOPS.exe /poke8 0x80100001 0x01 {suggestedComPort}\n");
Log.Write($" 16 bits : NOPS.exe /poke16 0x80100002 0x0202 {suggestedComPort}\n");
Log.Write($" 32 bits : NOPS.exe /poke32 0x80100004 0x04040404 {suggestedComPort}\n\n");
Log.Write($" Continuous Hex Dump (to screen):\n");
Log.Write($" CD REGS: NOPS.EXE /watch 0x1F801800 0x4 {suggestedComPort}\n\n");
Log.Write($" Extra switches:\n");
Log.Write($" /m to open the Serial IO monitor (can be used /m {suggestedComPort} alone)\n");
Log.Write( " /fast to enable or continue using 500k baud\n" );
Log.Write( " /debug to enable experimental /bin & /dump during gameplay\n" );
Log.Write( " /v Verbose logging\n" );
Log.Write( "\n" );
Log.Write( " Examples:\n" );
Log.Write($" nops /fast /rom unirom_b.rom {suggestedComPort}\n" );
Log.Write($" nops /exe mything.exe /m {suggestedComPort}\n" );
Log.Write($" nops /fast /poke8 0x80100000 0x04 {suggestedComPort}\n" );
Log.Write( "\n" );
Log.Write( " Debug Functions: (Must be in debug mode via L1+Square or nops /debug)\n" );
Log.Write( " Halt the PSX : nops /halt\n" );
Log.Write( " Continue from halt/exeception/hook : nops /cont\n" );
Log.Write( " Show registers from last interrupt : nops /regs\n" );
Log.Write( " Set a register while halted : nops /setreg 0xADDR 0xVALUE\n" );
Log.Write( " Hook (halt) on memory read * : nops /hookread 0xADDR\n" );
Log.Write( " Hook (halt) on memory write * : nops /hookwrite 0xADDR\n" );
Log.Write( " Hook (halt) on memory exec * : nops /hookex 0xADDR\n" );
Log.Write( " Unhook (read/write/exec) : nops /unhook\n" );
Log.Write( " * = Resume with nops /cont\n" );
Log.Write( "\n" );
Log.Write( " GDB Server:\n" );
Log.Write( " /gdb Starts a GDB listen server. Requires IP:port and com port\n" );
Log.Write( " It's highly recommended to start the server with /fast\n" );
Log.Write($" i.e. nops /fast /gdb 127.0.0.1:3333 {suggestedComPort}\n" );
Log.Write( " You can then connect to the server with a GDB client\n" );
Log.Write( " such as GDB or Visual Studio Code\n" );
Log.Write( " /nomanifest Don't send manifest to GDB client, may be required for IDA\n" );
Log.Write( " STDIN Functions:\n" );
Log.Write( " Types a string into stdin : nops /type \"something\"\n" );
Log.Write( " at about 1 char per frame : nops /type 'something'\n" );
Log.Write( "\n" );
}
//
// public stuff
//
public static TargetDataPort activeSerial;
public static UInt32 protocolVersion = 1;
//
// less public stuff
//
// Shared args
static string argTargetDevice = ""; // E.g. COM8, /dev/tty.SLAB_USBtoUART, 127.0.0.1, etc
static string suggestedComPort = "COM8";
static UInt32 argAddr;
static UInt32 argSize;
static UInt32 argValue;
static UInt32 argCard;
static string argIP; // argIP and argPort used for both local and remote params
static UInt32 argtargetPort = 0; // the port we'll connect to the device/emu on
static UInt32 argListenPort = 0; // the listen port we'll use for bridge mode
static CommandMode argCommand = CommandMode.NOT_SET;
static string argFileName;
static string argRegister = "";
static string argString = "";
// standalone/modifiers such as /fast, /slow, /m, etc
static bool allArgsAreSecondary = false;
// Input validation
static bool satisfiedAddressRequirements = false;
static bool satisfiedSizeRequirements = false;
static bool satisfiedRegisterRequirements = false;
static bool satisfiedValueRequirements = false;
static bool satisfiedCardRequirement = false;
static bool satisfiedInputFileRequirements = false;
static bool satisfiedOutputFileRequirements = false;
static bool satisfiedIPRequirements = false;
static bool satisfiedPortRequirements = false;
static bool satisfiedStringRequirements = false;
// Misc
static byte[] inFile;
static bool enterFastMode = false;
static bool enterSlowMode = false;
static bool enterFTDIMode = false;
// Force gapped datel V2 model via SIO
// (or force it off in the case of homebrew carts triggering auto detect)
static bool forceFlatMode = false;
static bool forceGappedMode = false;
static bool monitorComms = false;
static bool usingCachedPortOrAddress = false;
/// <summary>
/// Ensure the input args are vaguely sane
/// - Make a list of args
/// - Cross them off one by one as they're valid
/// - Determine COM port
/// - Extra args such as /monitor comms
/// - Grab address values, filename etc
/// </summary>
/// <param name="inArgs">Command line args</param>
/// <returns>Are there any unprocessed args?</returns>
static bool VerifyArgs( string[] inArgs ) {
if ( inArgs.Length == 0 ) {
PrintUsage( false );
return false;
}
// Thank you, linq <3
// We'll remove args as they're processed so they're not processed twice
// and extra args will be left over.
// ( we don't want extra args left over )
List<string> remainingArgs = inArgs.ToList();
// Specified a com port?
#if DebugArgs
Log.WriteLine( "__DEBUG__Argsleft: " + remainingArgs.Count, LogType.Debug );
#endif
// Remove the arg for the com port: COM* or /dev/tty*
for ( int i = remainingArgs.Count - 1; i >= 0; i-- ) {
string s = remainingArgs[ i ];
if ( ValidIPAddress( s, ref argTargetDevice, ref argtargetPort ) ) {
// 127.0.0.1:1234 etc
remainingArgs.RemoveAt( i );
break;
} else if ( ValidTargetDevice( s, ref argTargetDevice ) ) {
// COM5 or /dev/uart_thing/
remainingArgs.RemoveAt( i );
break;
}
}
// Cache the com port, or load the cached value
if ( string.IsNullOrEmpty( argTargetDevice ) ) {
string readVal = "";
try {
if ( System.IO.File.Exists( "comport.txt" ) ) {
readVal = File.ReadAllText( "comport.txt" );
}
} catch ( System.Exception e ) {
Log.WriteLine( "Error checking for cached com port...\n" + e, LogType.Error );
}
if ( ValidIPAddress( readVal, ref argTargetDevice, ref argtargetPort ) ) {
Log.WriteLine( $"Using address and port: {argTargetDevice}:{argtargetPort}" );
} else if ( ValidTargetDevice( readVal, ref argTargetDevice ) ) {
Log.WriteLine( $"Using device {argTargetDevice}" );
} else {
// I think the GC can manage the hit on this one :)
return Error( $"\nERROR! Please specify a device or COM port in the format: {SampleCOMPortForEnvironment} or 127.0.0.1:1234, etc\ngot: {argCommand}" );
}
usingCachedPortOrAddress = true;
} else {
// port was specified, cache it for later
try {
string savedTarget = argtargetPort != 0 ? (argTargetDevice + ":" + argtargetPort) : argTargetDevice;
File.WriteAllText( "comport.txt", savedTarget );
} catch ( System.Exception e ) {
Log.WriteLine( "Error writing cached com port!\n" + e, LogType.Error );
}
}
// Are we using secondary args only? e.g. /fast /m
// in this case, there's no point processing the rest of the input
allArgsAreSecondary = true;
for ( int i = remainingArgs.Count - 1; i >= 0; i-- ) {
string lower = remainingArgs[ i ].ToLowerInvariant();
if ( lower == "/m" ) {
monitorComms = true;
remainingArgs.RemoveAt( i );
} else if ( lower == "/fast" ) {
enterFastMode = true;
remainingArgs.RemoveAt( i );
} else if ( lower == "/slow" ) {
enterSlowMode = true;
remainingArgs.RemoveAt( i );
} else if ( lower == "/ftdi" ){
enterFTDIMode = true;
remainingArgs.RemoveAt( i );
} else if ( lower == "/flat" ){
forceFlatMode = true;
remainingArgs.RemoveAt(i);
} else if ( lower == "/gaps" ){
forceGappedMode = true;
remainingArgs.RemoveAt(i);
}
else if (lower == "/v")
{
Log.SetLevel(LogType.Info | LogType.Warning | LogType.Error | LogType.Debug);
remainingArgs.RemoveAt(i);
}
else if (lower == "/nomanifest")
{
GDBServer.DisableManifest();
remainingArgs.RemoveAt(i);
} else
{
allArgsAreSecondary = false;
}
}
#if DebugArgs
Log.WriteLine( "__DEBUG__Argsleft: " + remainingArgs.Count, LogType.Debug );
Log.WriteLine( "__DEBUG__TARGDEVICE: " + argTargetDevice, LogType.Debug);
Log.WriteLine( "__DEBUG__TARGPORT: " + argtargetPort, LogType.Debug);
Log.WriteLine( "__DEBUG__IP:TARGETPORT: " + argIP + ":" + argtargetPort, LogType.Debug);
Log.WriteLine( "__DEBUG__IP:LISTENPORT: " + argIP + ":" + argListenPort, LogType.Debug);
Log.WriteLine( "__DEBUG__FAST: " + enterFastMode, LogType.Debug);
Log.WriteLine( "__DEBUG__SLOW: " + enterSlowMode, LogType.Debug);
Log.WriteLine( "__DEBUG__FTDI: " + enterFTDIMode, LogType.Debug );
#endif
// Not really transferring anything, let's go
if ( allArgsAreSecondary ) return true;
// Outer loop: Does anything resemble an command? /dump, /poke, etc?
// Specified a command arg (or several for future-proofing)?
for ( int paramIndex = remainingArgs.Count - 1; paramIndex >= 0; paramIndex-- ) {
string param = remainingArgs[ paramIndex ].ToLowerInvariant();
// Inner loop: does it match any known commands?
// If so, check for sub params
// E.g. /dump <addr> <size> <filename>
// first one is uninitialised... ignore it
for ( int argIndex = 1; argIndex < (int)CommandMode.COUNT; argIndex++ ) {
CommandMode c = (CommandMode)argIndex;
if ( param.ToLowerInvariant() == c.command() ) {
// Set the current value and remove
// it from the list of available args
argCommand = c;
remainingArgs.RemoveAt( paramIndex );
// Now we've removed the /command, if there's
// an address after it, it will be remaningArgs[ arg ]
// Does the command require an address value?
if ( argCommand.hasAttribute<NeedsAddressAttribute>() ) {
// end of the array!
if ( paramIndex >= remainingArgs.Count ) {
return Error( "Specify an address in the format 0x01234567\n" );
}
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires an address ", LogType.Debug );
#endif
// reassign it
param = remainingArgs[ paramIndex ].ToLowerInvariant();
// try and get the address argument
try {
argAddr = ParseHexa( argCommand, param );
} catch ( System.Exception ) {
return Error( "EXCEPTION: Specify an address in the format 0x01234567" );
}
remainingArgs.RemoveAt( paramIndex );
satisfiedAddressRequirements = true;
}
// Do we need a register?
if ( argCommand.hasAttribute<NeedsRegisterAttribute>() ) {
Log.WriteLine( "__DEBUG__Command " + c + " requires a register ", LogType.Debug );
// reassign it
param = remainingArgs[ paramIndex ].ToLowerInvariant();
// TODO: verify register validity?
// TODO: settle on a format: R4 vs V0 vs $V0, etc
argRegister = param;
remainingArgs.RemoveAt( paramIndex );
satisfiedRegisterRequirements = true;
}
// On top of that... do we need a size? E.g. for dumping bios.
if ( argCommand.hasAttribute<NeedsSizeAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires a size parameter ", LogType.Debug );
#endif
// reassign it
param = remainingArgs[ paramIndex ].ToLowerInvariant();
// try to get the size argument
try {
argSize = ParseHexa( argCommand, param );
} catch ( System.Exception ) {
return Error( "EXCEPTION: Specify a size in the format 0x01234567" );
}
remainingArgs.RemoveAt( paramIndex );
satisfiedSizeRequirements = true;
}
// Do we need a value? 1 - 4 bytes
if ( argCommand.hasAttribute<NeedsValueAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires a value parameter ", LogType.Debug );
#endif
// reassign it
param = remainingArgs[ paramIndex ].ToLowerInvariant();
// try to get the size argument
try {
argValue = ParseHexa( argCommand, param );
} catch ( System.Exception ) {
return Error( "EXCEPTION: Specify a 1, 2 or 4 byte hex value" );
}
remainingArgs.RemoveAt( paramIndex );
satisfiedValueRequirements = true;
}
// if it uses both, then let's parse them as an IP:PORT combo
if ( argCommand.hasAttribute<NeedsIPAttribute>()
&& argCommand.hasAttribute<NeedsPortAttribute>()
) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires an IP:PORT combo", LogType.Debug );
#endif
if ( remainingArgs.Count == 0 ) {
return Error( "Need an IP:PORT e.g. 192.168.0.7:1234\n" );
}
param = remainingArgs[ paramIndex ].ToLowerInvariant();
if ( ValidIPAddress( remainingArgs[ paramIndex ], ref argIP, ref argListenPort ) ) {
remainingArgs.RemoveAt( paramIndex );
satisfiedAddressRequirements = true;
}
}
// We might've already done a check for ip:port combos
if ( !satisfiedPortRequirements && !satisfiedIPRequirements ) {
if ( argCommand.hasAttribute<NeedsIPAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires an IP/host parameter!", LogType.Debug );
#endif
argIP = param;
remainingArgs.RemoveAt( paramIndex );
satisfiedIPRequirements = true;
} // needs ip
if ( argCommand.hasAttribute<NeedsPortAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires a local/remote port!", LogType.Debug);
#endif
if ( remainingArgs.Count == 0 ) {
return Error( "Need to specify a local TCP/IP port!" );
}
param = remainingArgs[ paramIndex ].ToLowerInvariant();
if ( !UInt32.TryParse( param, out argListenPort ) ) {
return Error( "EXCEPTION: Couldn't parse a valid port number from \"" + param + "\"" );
}
remainingArgs.RemoveAt( paramIndex );
satisfiedPortRequirements = true;
} // port
} // port and ip already satisfied
if ( argCommand.hasAttribute<NeedsStringAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires a memcard parameter ", LogType.Debug);
#endif
argString = remainingArgs[ paramIndex ];
remainingArgs.RemoveAt( paramIndex );
satisfiedStringRequirements = true;
}
// Do we need a memcard number
if ( argCommand.hasAttribute<NeedsCardNumberAttribute>() ) {
#if DebugArgs
Log.WriteLine( "__DEBUG__Command " + c + " requires a memcard parameter ", LogType.Debug);
#endif
// reassign it
param = remainingArgs[ paramIndex ].ToLowerInvariant();
argCard = UInt32.Parse( param );
/*
// Working on something interesting here... hold tight.
if ( _arg != "0" && _arg != "1" ){
return Error( "EXCEPTION: memory card should be 0 or 1!" );
} else {
argCard = UInt32.Parse( _arg );
}
*/
remainingArgs.RemoveAt( paramIndex );
satisfiedCardRequirement = true;
}
break; // outer loop to check new args
}
} // inner loop
} // outer loop
Log.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count, LogType.Debug);
Log.WriteLine( "__DEBUG__COM: " + argTargetDevice, LogType.Debug );
Log.WriteLine("__DEBUG__COMMAND: " + argCommand, LogType.Debug );
Log.WriteLine("__DEBUG__ADDR: " + argAddr.ToString( "X8" ), LogType.Debug);
Log.WriteLine("__DEBUG__VALUE: " + argValue.ToString( "X8" ), LogType.Debug);
if ( argCommand == CommandMode.NOT_SET ) {
return Error( "Please specify a command - e.g. /r, /e /b, etc!\n\n" );
}
// Do we need to load an .exe, .rom, data etc?
if ( argCommand.hasAttribute<NeedsInputFileAttribute>() ) {
// One of the args specifies a file?
for ( int i = remainingArgs.Count - 1; i >= 0; i-- ) {
string fName = remainingArgs[ i ];
argFileName = fName;
try {
inFile = File.ReadAllBytes( fName );
} catch ( System.Exception e ) {
return Error( "Couldn't open input file " + fName + " Exception: " + e );
}
satisfiedInputFileRequirements = true;
remainingArgs.RemoveAt( i );
}
}
// Do we need to save somewhere?
if ( argCommand.hasAttribute<NeedsOutputFileAttribute>() ) {
// One of the args specifies a file?
for ( int i = remainingArgs.Count - 1; i >= 0; i-- ) {
argFileName = remainingArgs[ i ];
remainingArgs.RemoveAt( i );
satisfiedOutputFileRequirements = true;
}
}
// The /poke command for example doesn't require a filename
// so that requirement is considered satisfied.
if ( !argCommand.hasAttribute<NeedsRegisterAttribute>() ) satisfiedRegisterRequirements = true;
if ( !argCommand.hasAttribute<NeedsAddressAttribute>() ) satisfiedAddressRequirements = true;
if ( !argCommand.hasAttribute<NeedsSizeAttribute>() ) satisfiedSizeRequirements = true;
if ( !argCommand.hasAttribute<NeedsValueAttribute>() ) satisfiedValueRequirements = true;
if ( !argCommand.hasAttribute<NeedsCardNumberAttribute>() ) satisfiedCardRequirement = true;
if ( !argCommand.hasAttribute<NeedsInputFileAttribute>() ) satisfiedInputFileRequirements = true;
if ( !argCommand.hasAttribute<NeedsOutputFileAttribute>() ) satisfiedOutputFileRequirements = true;
if ( !argCommand.hasAttribute<NeedsIPAttribute>() ) satisfiedIPRequirements = true;
if ( !argCommand.hasAttribute<NeedsPortAttribute>() ) satisfiedPortRequirements = true;
if ( !argCommand.hasAttribute<NeedsStringAttribute>() ) satisfiedStringRequirements = true;
// Missed something...
if ( !satisfiedAddressRequirements ) return Error( "Did you specify an address or hex value? E.g. 0x23456788\n" );
if ( !satisfiedSizeRequirements ) return Error( "Did you specify a size? E.g. 0x23456788\n" );
if ( !satisfiedRegisterRequirements ) return Error( "Did you specify a register? E.g. a0" );
if ( !satisfiedValueRequirements ) return Error( "Specify a 1-4 byte value in the format 0x01" );
if ( !satisfiedCardRequirement ) return Error( "Specify memory card 0 or 1!" );
if ( !satisfiedInputFileRequirements ) return Error( "Specify an input file!" );
if ( !satisfiedOutputFileRequirements ) return Error( "Specify a filename to write to!" );
if ( !satisfiedIPRequirements ) return Error( "Specify a remote host/ip!" );
if ( !satisfiedPortRequirements ) return Error( "Specify a local or remote port!" );
if ( !satisfiedStringRequirements ) return Error( "Need a string with either \"\" or ''!" );
#if DebugArgs
Log.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count, LogType.Debug);
Log.WriteLine("__DEBUG__FILENAME: " + argFileName, LogType.Debug);
Log.WriteLine("__DEBUG__INPUTFILE: " + inFile, LogType.Debug);
Log.WriteLine("__DEBUG__stringArg: " + argString, LogType.Debug);
#endif
// there shouldn't be any arguments left!
// (noise on the console, etc)
if ( remainingArgs.Count > 0 ) {
for ( int i = remainingArgs.Count - 1; i >= 0; i-- ) {
Error( "Unknown arg! " + remainingArgs[ i ] );
}
return false;
}
// All done
return true;
} //VerifyArgs
// Quite satisfyingly small
private static void Main( string[] args ) {
Log.SetLevel(LogType.Info | LogType.Warning | LogType.Error);
ApplyConsoleMode();
// Verify all the required args are present and parsed properly
if ( !VerifyArgs( args ) ) {
return;
}
// Do stuff about it.
DoStuff();
}
/// <summary>
/// Set up a new serial connection on the port we parsed earlier
/// </summary>
/// <param name="inSpeed">Go fast or not go fast</param>
private static bool NewSIO( SIOSPEED inSpeed ) {
if ( activeSerial != null ) activeSerial.Close();
// We need to find a suitable overlap in frequencies which can be divided
// from the Playstation's clock and from the FTDI/clone, with some wiggle room.
//
// PSX/libs uses whole integer divisions of 2073600 for anything over 115200
// giving us 518400 close to the half-megabyte mark.
//
// Most FTDI/clones seem to operate in 2xinteger divisions of 48mhz
// giving us 510000 or 521000 close to the half-megabyte mark. e.g. (48m/47/2) or (48m/46/2)
//
// 5210000 (and even 518400) is a little fast, but the lower end
// of things (510000 to 518300) seems pretty stable.
//
// note: psx @ 518400, pc @ 510000
//
int baud;
switch( inSpeed )
{
default: baud = 115200; break;
case SIOSPEED.FAST: baud = 510000; break;