forked from hkzlab/catflap4linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatFlap.cpp
2493 lines (1925 loc) · 64.4 KB
/
CatFlap.cpp
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
#define SPECIAL_VERSION 0
////////////////////////////////////////////////////////////////////////////////
//
// CatFlap
//
////////////////////////////////////////////////////////////////////////////////
//
// CAETLA Communications Tool ported to Linux by Hkz
//
// (C) 1998 Ash Hogg, Intar Technologies Limited
//
////////////////////////////////////////////////////////////////////////////////
//
// Uses C++ classes for extra sweetness :)
//
////////////////////////////////////////////////////////////////////////////////
//
// Rev Date Comment
// ======================
// 1.01 14/10/98 First version
// 1.02 12/11/98 Added 'run exe' mode
// 1.03 16/11/98 Finally sorted keyboard re-direction! Server mode also sorted.
// 1.04 17/11/98 Added switches, many bugs fixed in CCaetla class!
// 1.05 18/11/98 A few tidy-ups, that's all :)
// 1.06 19/11/98 Fixed file-write bug in server mode
// 1.07 20/11/98 Improved the PC keyboard redirection / server mode
// 1.08 21/11/98 Added preliminary CPE support too!
// 1.09 25/11/98 Added PC executable support thru CMD: pseudo-device!
// 1.10 02/12/98 Organised command flow much better, non-filtering switches after EXE name
// 1.12 02/01/99 First revision of NT support finally completed
// 1.13 04/01/99 Relocated driver & install/uninstall code to the Caetla class
// 1.14 07/01/99 Added fix for weird EXEs - EZ-packed?
// 1.15 08/01/99 Added VRAM support
// 1.16 09/01/99 Added MemCard support
// 1.17 12/01/99 Much faster MemCard delete
// 1.18 12/01/99 Tidied up some error-trapping, MemCard status checks
// 1.20 26/01/99 Minor Revision update
// 1.21 28/01/99 Changed so that Address can be 0
// 1.22 20/03/99 Added EEPROM Flash function
// 1.23 27/03/99 Fixed EEPROM Flash under NT
// 1.24 04/05/99 Adding back the TIMEOUT break in Send8 seemed to sort out the
// problem with sometimes being unable to get control of the PSX,
// forcing a manual reset/retry. Nice, but not *too* sure why! :)
// 1.25 06/05/99 Added RAW console mode for just dumping text
// 1.26 28/07/99 Added CMD:QUIT as a special CatFlap command to exit console
// 1.27 29/07/99 Modified help screens to return to main help menu each time
// 1.28 30/07/99 Added log file support
// 1.29 04/08/99 Fixed log flush
// 1.30 05/08/99 VRAM fetch to BMP (detect "*.bmp")
// 1.31 09/08/99 First revision of key-binding/scripting system
// 1.32 03/09/99 Fixed F10 error in DOS Console of C++ class, added SysCall Method
// 1.33 18/10/99 Sensibly shows when a file can't be found :) Oops.
// 1.34 27/10/99 A little messy, but XPlorer support is now in there
// 1.35 28/10/99 Stupid port-setup bug (only 0x378) fixed
// 1.36 28/10/99 Added progress bar to SendData, currently used on SendEXE, not NT
// * MAJOR REVISION *
// 2.00 29/10/99 Major Revision change, all NT & XPlorer stuff works, all features OK!
// 2.01 03/11/99 Sorted PsyQ PCwrite(-1,...) bug
// 2.02 08/12/99 Decent progress indication on CPE upload
// 2.03 18/02/00 Altered Timeouts (with Sleeps), Option to run EXE in Console
// 2.04 21/02/00 New Port/Cart-Type settings, new ENV settings
// 2.05 22/02/00 Refined Timeout/Sleep a little
// 2.06 22/02/00 New Timeout system with GetTickCount()
// 2.07 22/02/00 Callback Hook for Keyboard Input in Console mode
// 2.08 22/02/00 Callback Hook for Print Output in Console mode, DateStamp moved to CCaetla
// 2.09 23/02/00 New Help System
// 2.10 09/03/00 Better timeouts
// 2.11 09/03/00 Mega Timeout sorting, plus Console Mode optimisation
// 2.12 10/03/00 Fixed really stupid bug on -k- which disabled Args rather than KB.
// Some comms sorted out to fix some XPlorer problems.
// 2.13 16/03/00 Kickass new Kernel-Mode driver for NT4/Win2000
// 2.14 17/03/00 Some optimisations
// 2.15 17/03/00 Some minor tweaks and fixes
// 2.16 17/03/00 Added Caetla hook control on EXE execution
// 2.17 18/03/00 Change to RAWCON mode
// 2.18 20/03/00 Added some Debug-mode functions
// 2.19 21/03/00 Added some Debug-mode functions
// 2.20 22/03/00 Added some Debug-mode functions, minor revisions to console
// 2.21 22/03/00 Debug Functions, Added some Debug-mode functions, minor revisions to console
// 2.22 22/03/00 Further minor improvements
// 2.23 23/03/00 Streamlining uni/bi-directional low-level comms functions
// 2.24 23/03/00 Minor changes (help info)
// 2.25 27/03/00 Rewrote CPE loading to 'depack' into virtual buffer first
// 2.26 01/04/00 Added 'resume' command
// 2.27 01/04/00 Added 'resume' command to Help function :)
// 2.28 12/04/00 Fixed dual-mode up/down code for XP
// 2.29 12/04/00 Changed EXE execution as it was using m_FileSize always
// 2.30 13/04/00 Minor fixes, self-terminate detection won't work on XP so removed
// 2.31 13/04/00 New method of self-termination for XP
// 2.32 15/08/00 Cursor/Control keys working under DOS box? Fine on my system...
// 2.33 27/09/00 Found TIMEOUT errors on PCwrite mode, temp fixed but must finalise
// 2.34 27/09/00 Fixed stupid 'count' variable not incrementing and causing default timeouts
// 2.35 29/09/00 Added -as switch to disable commlink autosensing in the console mode
// 2.36 29/09/00 Modified CArgs to always build and utilise a local arguments buffer
// 2.36 10/12/10 Ported to Linux by Hkz
//
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/io.h>
#include <strings.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "CArgs.h"
#include "CCaetla.h"
#include "common_defs.h"
// Internal
#define VER 2
#define REV 36
// Linux version
#define LVER 0
#define LREV 1
#define PROGNAME "CatFlap"
////////////////////////////////////////////////////////////////////////////////
// Function prototypes
////////////////////////////////////////////////////////////////////////////////
int main(int, char *[]);
void usage(void);
void cleanup(void);
//short ConKeyHook(void);
void ConPrintHook(char *);
int BindIni(char *);
void DumpIni(void);
void ParseLine(char *, char *);
int ConHook(uint8, FILE *);
void ConOut(FILE *file, const char *fmt, ...);
int CallFunction(FILE *, char *, char *);
char *NextLine(char *);
int FileExists(char *);
////////////////////////////////////////////////////////////////////////////////
CArgs m_Args; // our Args-handler instance
CCaetla m_Caetla; // our Caetla-handler instance!
int m_Verbose = 0;
int ARPort = 0;
int ARArgs = 0;
int m_DoConsole = 1;
bool m_ExeHooksOn = 1;
int m_RawCon = 0;
int m_CartType = CART_XP;
char *m_Log = NULL;
char *m_Ini = NULL;
char PPdevice[MAX_PPDEV_SIZE];
////////////////////////////////////////////////////////////////////////////////
// Multi-purpose Number parser :)
uint64 ParseNumber(char *num) {
uint64 ret = 0;
// Hex?
if (strncasecmp(num, "0x", 2) == 0) {
if (sscanf(num + 2, "%lx", &ret) == 1) {
return(ret);
} else {
printf("Error in parameter: %s\n", num);
return(ret);
}
}
// Dec?
else {
if (sscanf(num, "%ld", &ret) == 1) {
return(ret);
} else {
printf("Error in parameter: %s\n", num);
return(ret);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Callback functions for Switch processing
void SwitchCallback_Null(char *arg) {
}
void SwitchCallback_PAR(char *arg) {
m_CartType = CART_PAR;
}
void SwitchCallback_XP(char *arg) {
m_CartType = CART_XP;
}
void SwitchCallback_K(char *arg) {
if (*arg == '-') {
m_Caetla.m_KeyIn = 0;
if (m_Verbose) {
printf("Keyboard Redirection disabled\n");
}
} else {
int addr;
addr = ParseNumber(arg);
m_Caetla.m_KeyIn = addr;
if (m_Verbose) {
printf("Keyboard Buffer override - Set to 0x%x\n", m_Caetla.m_KeyIn);
}
}
}
void SwitchCallback_A(char *arg) {
if (*arg == '-') {
ARArgs = 0;
if (m_Verbose) {
printf("PSX Arguments disabled\n");
}
} else {
int addr;
addr = ParseNumber(arg);
ARArgs = addr;
if (m_Verbose) {
printf("PSX Args Buffer override - Set to 0x%x\n", ARArgs);
}
}
}
void SwitchCallback_V(char *arg) {
m_Verbose = 1;
}
void SwitchCallback_C(char *arg) {
if (*arg == '-') {
m_DoConsole = 0;
if (m_Verbose) {
printf("Console Mode disabled after program execution\n");
}
}
}
void SwitchCallback_P(char *arg) {
int addr;
addr = ParseNumber(arg);
ARPort = addr;
if (m_Verbose) {
printf("Comms Port set to 0x%x\n", ARPort);
}
}
void SwitchCallback_L(char *arg) {
if (*arg == '=')
m_Log = arg + 1;
else
m_Log = arg;
if (m_Verbose) {
printf("Logging Console output to file '%s'\n", m_Log);
}
}
void SwitchCallback_RAWCON(char *arg) {
m_RawCon = 1;
if (m_Verbose) {
printf("RAW Console Mode enabled\n");
}
}
void SwitchCallback_H(char *arg) {
m_ExeHooksOn = false;
m_DoConsole = 0;
if (m_Verbose) {
printf("Caetla Hook disabled on program execution\n (Console Mode automatically disabled)\n");
}
}
#define TIMEOUT_MINDEF 0.0f
#define TIMEOUT_MAXDEF 30.0f
void SwitchCallback_AS(char *arg) {
if (m_Verbose) {
printf("CommLink Status AutoSense disabled in Console Mode\n");
}
m_Caetla.m_AutoSense = false;
}
////////////////////////////////////////////////////////////////////////////////
// Switch Callback list
struct Arg_SwitchAssign Switches[] = {
{ "v", SwitchCallback_V, ARG_CASE_INSENSITIVE },
{ "PAR", SwitchCallback_PAR, ARG_CASE_INSENSITIVE },
{ "XP", SwitchCallback_XP, ARG_CASE_INSENSITIVE },
{ "as", SwitchCallback_AS, ARG_CASE_INSENSITIVE },
{ "k", SwitchCallback_K, ARG_CASE_INSENSITIVE },
{ "a", SwitchCallback_A, ARG_CASE_INSENSITIVE },
{ "c", SwitchCallback_C, ARG_CASE_INSENSITIVE },
{ "p", SwitchCallback_P, ARG_CASE_INSENSITIVE },
{ "l", SwitchCallback_L, ARG_CASE_INSENSITIVE },
{ "h", SwitchCallback_H, ARG_CASE_INSENSITIVE },
{ "rawcon", SwitchCallback_RAWCON, ARG_CASE_INSENSITIVE },
{ NULL, NULL, 0 },
};
////////////////////////////////////////////////////////////////////////////////
// Commands parsing
enum {
CMD_RUN,
CMD_SEND,
CMD_RECEIVE,
CMD_RESET,
CMD_RESUME,
CMD_CONSOLE,
CMD_HELP,
CMD_INSTALLNT,
CMD_UNINSTALLNT,
CMD_VSEND,
CMD_VGET,
CMD_LISTCARDS,
CMD_UPCARDF,
CMD_DOWNCARDF,
CMD_BACKUPCARD,
CMD_RESTORECARD,
CMD_FORMATCARD,
CMD_DELETECARD,
CMD_FLASHEPROM,
// CMD_,
};
typedef struct Cmd_Definition {
int id;
const char *name;
int (*func)(int argc, char *argv[]);
} CMD_DEFINITION;
////////////////////////////////////////////////////////////////////////////////
// Run an EXE or CPE
int CmdCallback_Run(int argc, char *argv[]) {
char *exename;
exename = m_Args.GetNormalArg(2);
if (!exename) {
printf("Error - no EXE file specified.\n");
return(EXIT_FAILURE);
}
if (FileExists(exename) == 0) {
printf("Error - file '%s' not found.\n", exename);
return(EXIT_FAILURE);
}
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Reset();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
// Prepare argc,argv[] first! (if required)
if (ARArgs) {
char argbuff[4+(16*4)+256]; // ==324
char *argp;
uint32 *lp;
int ac;
int ai = m_Args.GetNormalArgIndex(2);
int argc2 = m_Args.GetNumArgs() - ai;
if (argc2 < 0) argc2 = 0;
if (argc2 > 16) argc2 = 16;
if (argc2) {
lp = (uint32 *)&argbuff;
lp[0] = argc2;
for (ac = 0; ac < 16; ac++)
lp[1+ac] = 0;
argp = &argbuff[4+(16*4)];
*argp = 0;
for (ac = 0; ac < argc2; ac++) {
lp[1+ac] = (uint32)((uint8 *)argp - (uint8 *)&argbuff[0] + ARArgs);
strcpy((char *)argp, argv[ac+ai]);
*(argp + strlen(argv[ac+ai])) = 0;
argp += strlen((const char *)argp) + 1;
}
printf("Preparing PSX Runtime Arguments...\n");
m_Caetla.Upload(&argbuff[0], ARArgs, sizeof(argbuff), false, false);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
}
}
printf("Executing [%s]...\n", exename);
if (m_DoConsole)
m_Caetla.RunExe(exename, false, m_ExeHooksOn);
else
m_Caetla.RunExe(exename, true, m_ExeHooksOn);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
if (m_DoConsole) {
if (m_RawCon)
m_Caetla.RawConsole();
else
m_Caetla.DosConsole();
}
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Send a file
int CmdCallback_Send(int argc, char *argv[]) {
int Addr;
char *fname;
char *add;
fname = m_Args.GetNormalArg(2);
if (!fname) {
printf("Error - no filename specified.\n");
return(EXIT_FAILURE);
}
add = m_Args.GetNormalArg(3);
if (!add) {
printf("Error - no address specified.\n");
return(EXIT_FAILURE);
}
if (FileExists(fname) == 0) {
printf("Error - file '%s' not found.\n", fname);
return(EXIT_FAILURE);
}
#if 0
if (Addr = ParseNumber(add))
#else
Addr = ParseNumber(add);
if (1)
#endif
{
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
printf("Sending File [%s] to 0x%x...\n", fname, Addr);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Listen(CAETLA_MODE_MAIN);
// m_Caetla.Listen(CAETLA_MODE_DEBUG);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.SendFile(fname, Addr);
// m_Caetla.Resume();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
} else {
usage();
return(EXIT_FAILURE);
}
}
////////////////////////////////////////////////////////////////////////////////
// Receive a file
int CmdCallback_Receive(int argc, char *argv[]) {
int Addr, Len;
char *fname;
char *add;
fname = m_Args.GetNormalArg(2);
if (!fname) {
printf("Error - no filename specified.\n");
return(EXIT_FAILURE);
}
add = m_Args.GetNormalArg(3);
if (!add) {
printf("Error - no address specified.\n");
return(EXIT_FAILURE);
}
#if 0
if (!(Addr = ParseNumber(add))) {
usage();
return(EXIT_FAILURE);
}
#else
Addr = ParseNumber(add);
#endif
add = m_Args.GetNormalArg(4);
if (!add) {
printf("Error - no length specified.\n");
return(EXIT_FAILURE);
}
if (Len = ParseNumber(add)) {
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
printf("Receiving 0x%x (%d) bytes from 0x%x to File [%s]...\n", Len, Len, Addr, fname);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Listen(CAETLA_MODE_MAIN);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
// m_Caetla.Listen(CAETLA_MODE_DEBUG);
m_Caetla.ReceiveFile(fname, Addr, Len);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
// m_Caetla.Resume();
return(EXIT_SUCCESS);
} else {
usage();
return(EXIT_FAILURE);
}
}
////////////////////////////////////////////////////////////////////////////////
// Reset the PSX
int CmdCallback_Reset(int argc, char *argv[]) {
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
printf("Resetting PSX...\n");
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Reset();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Resume control on the PSX
int CmdCallback_Resume(int argc, char *argv[]) {
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
// printf("Resetting PSX...\n");
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Resume();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Console Mode
int CmdCallback_Console(int argc, char *argv[]) {
// Initialise our comms port
//testing XP cart...
if (m_RawCon) {
m_Caetla.RawConsole();
return(0);
}
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.DosConsole();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Help screen
int CmdCallback_Help(int argc, char *argv[]) {
fprintf(stdout, "CatFlap usage:\n"
" * GENERAL COMMANDS:\n"
" run [filename] [runtime args, ...]\t\tRun a PSX EXE or CPE file\n"
" reset\t\t\t\t\tReset the PSX\n"
" resume\t\t\t\t\tContinue the execution on PSX if halted\n"
" help\t\t\t\t\t\tView Help information\n"
" console\t\t\t\t\tEnter Console mode\n"
"\n"
" * MEMORY TRANSFER COMMANDS:\n"
" up [filename] [address]\t\t\tUpload file to PSX RAM\n"
" down [filename] [address] [size]\t\tDownload file from PSX RAM\n"
"\n"
" * VRAM TRANSFER COMMANDS:\n"
" vup [filename] [x] [y] [w] [h] [depth]\tUpload file to PSX VRAM\n"
" vdown [filename] [x] [y] [w] [h] [depth]\tDownload file from PSX VRAM\n"
"\n"
" VDOWN with a filename of \"*.BMP\" to save VRAM in BMP format.\n"
"\n"
" * MEMORY CARD COMMANDS:\n"
" mclist [slot]\t\t\t\tList Contents of Memory Card\n"
" mcup [filename] [slot] <optional name>\tUpload a Savegame file to Memory Card\n"
" mcdown [filename] [slot] [file]\t\tDownload a Savegame file from Memory Card\n"
" mcbackup [filename] [slot]\t\t\tBackup a whole Memory Card\n"
" mcrestore [filename] [slot]\t\t\tRestore a whole Memory Card\n"
" mcdelete [slot] [file]\t\t\tDelete a File from Memory Card\n"
" mcformat [slot]\t\t\t\tFormat a Memory Card\n"
"\n");
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Install Kernel-Mode I/O Driver for NT * USELESS IN LINUX *
int CmdCallback_InstallNT(int argc, char *argv[]) {
m_Caetla.InstallNT();
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Remove Kernel-Mode I/O Driver for NT * USELESS IN LINUX *
int CmdCallback_UninstallNT(int argc, char *argv[]) {
m_Caetla.UninstallNT();
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Send file to VRAM
int CmdCallback_SendVRAM(int argc, char *argv[]) {
int x, y, w, h, depth;
char *fname;
char *param;
fname = m_Args.GetNormalArg(2);
if (!fname) {
printf("Error - no filename specified.\n");
return(EXIT_FAILURE);
}
if (FileExists(fname) == 0) {
printf("Error - file '%s' not found.\n", fname);
return(EXIT_FAILURE);
}
param = m_Args.GetNormalArg(3);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
x = ParseNumber(param);
param = m_Args.GetNormalArg(4);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
y = ParseNumber(param);
param = m_Args.GetNormalArg(5);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
w = ParseNumber(param);
param = m_Args.GetNormalArg(6);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
h = ParseNumber(param);
param = m_Args.GetNormalArg(7);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
depth = ParseNumber(param);
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
printf("Uploading File [%s] to VRAM...\n", fname);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.SendVRAM(fname, x, y, w, h, depth);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Resume();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Download VRAM to file
int CmdCallback_GetVRAM(int argc, char *argv[]) {
int x, y, w, h, depth;
char *fname;
char *param;
fname = m_Args.GetNormalArg(2);
if (!fname) {
printf("Error - no filename specified.\n");
return(EXIT_FAILURE);
}
param = m_Args.GetNormalArg(3);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
x = ParseNumber(param);
param = m_Args.GetNormalArg(4);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
y = ParseNumber(param);
param = m_Args.GetNormalArg(5);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
w = ParseNumber(param);
param = m_Args.GetNormalArg(6);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
h = ParseNumber(param);
param = m_Args.GetNormalArg(7);
if (!param) {
printf("Error - missing parameter.\n");
return(EXIT_FAILURE);
}
depth = ParseNumber(param);
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
printf("Receiving VRAM to File [%s] ...\n", fname);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.ReceiveVRAM(fname, x, y, w, h, depth);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Resume();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// List Memory Cards
char JIS_129[] = {
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 0
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 20
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 40
' ', ' ', ' ', ' ', ' ', ' ', ' ', ',', '.', ' ', ':', ';', '?', '!', ' ', ' ', ' ', ' ', ' ', '^', // 60
'~', '_', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '/', ' ', ' ', ' ', '|', ' ', // 80
'`', ' ', '\'', ' ', '"', '(', ')', ' ', ' ', '[', ']', '{', '}', '<', '>', ' ', ' ', ' ', ' ', ' ', // 100
' ', ' ', ' ', '+', '-', ' ', ' ', ' ', ' ', '=', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 120
' ', ' ', ' ', '\\', '$', ' ', ' ', '%', '#', '&', '*', '@', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 140
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 160
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 180
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 200
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 220
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 240
};
char JIS_130[] = {
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 0
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 20
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 40
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', // 60
'1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', 'B', 'C', 'D', // 80
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 100
'Y', 'Z', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', // 120
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', ' ', ' ', ' ', ' ', // 140
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 160
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 180
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 200
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 220
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 240
};
int CmdCallback_ListCards(int argc, char *argv[]) {
int port;
char *param;
param = m_Args.GetNormalArg(2);
if (!param) {
printf("Error - Memory Card Port not specified.\n");
return(EXIT_FAILURE);
}
port = (ParseNumber(param) - 1) & 1;
// Initialise our comms port
printf("Initializing Caetla...\n");
m_Caetla.Init(ARPort, m_CartType);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Detect();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.Reset();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
m_Caetla.ScanMemCards(port);
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
if (m_Caetla.m_MemCards[port].numfiles > 0) {
int i, len, j, k;
uint8 *comment;
printf("\n");
printf("Contents of Memory Card Slot %d\n", port + 1);
printf("------+------+----------------------------------------------\n");
printf(" File | Blks | Name\n");
printf("------+------+----------------------------------------------\n");
for (i = 0; i < m_Caetla.m_MemCards[port].numfiles; i++) {
printf(" %0d | %02d | %s\n", i, m_Caetla.m_MemCards[port].files[i].size / 8192, (char *)&m_Caetla.m_MemCards[port].files[i].name[0]);
comment = (uint8 *)&m_Caetla.m_MemCards[port].files[i].comment[0];
len = strlen((char *)comment);
for (j = 0, k = 0; j < len; j += 2, k++) {
switch (comment[j]) {
case 0x81:
comment[k] = JIS_129[comment[j+1]];
break;
case 0x82:
comment[k] = JIS_130[comment[j+1]];
break;
default:
comment[k] = '#';
break;
}
}
comment[k] = 0;
printf(" | | %s\n", (char *)&m_Caetla.m_MemCards[port].files[i].comment[0]);
if (i == m_Caetla.m_MemCards[port].numfiles - 1)
printf("------+------+----------------------------------------------\n");
}
} else {
printf("\nMemory Card in Slot %d is empty.\n", port + 1);
}
m_Caetla.Resume();
if (m_Caetla.ShowError()) return(EXIT_FAILURE);
return(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
// Download a Memory Card File
int CmdCallback_DownCardFile(int argc, char *argv[]) {
int port, mfile;
char *param, *fname;
fname = m_Args.GetNormalArg(2);
if (!fname) {
printf("Error - no filename specified.\n");
return(EXIT_FAILURE);
}
param = m_Args.GetNormalArg(3);
if (!param) {
printf("Error - Memory Card Port not specified.\n");
return(EXIT_FAILURE);
}
port = (ParseNumber(param) - 1) & 1;
param = m_Args.GetNormalArg(4);
if (!param) {
printf("Error - Memory Card File not specified.\n");
return(EXIT_FAILURE);
}
mfile = ParseNumber(param);