-
Notifications
You must be signed in to change notification settings - Fork 1
/
playtzx.c
executable file
·2620 lines (2175 loc) · 86.5 KB
/
playtzx.c
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
/*
* PlayTZX for Linux
*
* version 0.1beta
*
* Based very heavily on original PlayTZX v 0.56b source by Tomaz Kac
* His original comment below...
*
* PLAY TZX , TZX to VOC Converter & TZX INFO
* v0.59b
* (c) 1997,98 Tomaz Kac
* Watcom C 10.0+ specific code... set TABs to 4 characters
*
* This is still beta and stuff...
*
*/
#include "playtzx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAJREV 1 /* Major revision of the format this program supports */
#define MINREV 13 /* Minor revision -||- */
/* C64 Loader defines ...*/
#define ROM_S_HALF 616 /* ROM Loader SHORT Half Wave */
#define ROM_M_HALF 896 /* ROM Loader MEDIUM Half Wave */
#define ROM_L_HALF 1176 /* ROM Loader LONG Half Wave */
#define STT_0_HALF 426 /* Standard Turbo Tape BIT 0 Half Wave */
#define STT_1_HALF 596 /* Standard Turbo Tape BIT 1 Half Wave */
typedef struct
{
int magic; /* magic number SND_MAGIC */
int dataLocation; /* offset or pointer to the data */
int dataSize; /* number of bytes of data */
int dataFormat; /* the data format code */
int samplingRate; /* the sampling rate */
int channelCount; /* the number of channels */
char info[4]; /* optional text information */
} SNDSoundStruct;
#define SND_MAGIC 0x2e736e64
#define SND_FORMAT_LINEAR_8 2
#define HTYPES 0x10
#define HIDS 0x1E
char htype[HTYPES][256]= {
"Computer","External Storage","ROM/RAM Type Add-On","Sound Device",
"Joystick","Mouse","Other Controller","Serial Port","Parallel Port",
"Printer","Modem","Digitiser","Network Adapter","Keyboard or Keypad",
"AD/DA Converter","EPROM Programmer"
};
char hid[HTYPES][HIDS][256]={
{
"ZX Spectrum 16k", "ZX Spectrum 48k, plus","ZX Spectrum 48k Issue 1",
"ZX Spectrum 128k (Sinclair)","ZX Spectrum 128k +2 (Grey case)",
"ZX Spectrum 128k +2A, +3","Timex Sinclair TC-2048",
"Timex Sinclair TS-2068","Pentagon 128","Sam Coupe","Didaktik M",
"Didaktik Gama","ZX-81 with 1k RAM","ZX-81 with 16k RAM or more",
"ZX Spectrum 128k, Spanish version","ZX Spectrum, Arabic version",
"TK 90-X","TK 95","Byte","Elwro","ZS Scorpion",
"Amstrad CPC 464","Amstrad CPC 664","Amstrad CPC 6128",
"Amstrad CPC 464+","Amstrad CPC 6128+","Jupiter ACE",
"Enterprise", "Commodore 64", "Commodore 128"
},
{
"Microdrive","Opus Discovery","Disciple","Plus-D",
"Rotronics Wafadrive","TR-DOS (BetaDisk)","Byte Drive","Watsford",
"FIZ","Radofin","Didaktik disk drives","BS-DOS (MB-02)",
"ZX Spectrum +3 disk drive","JLO (Oliger) disk interface",
"FDD3000","Zebra disk drive","Ramex Millenia","Larken"
},
{
"Sam Ram","Multiface","Multiface 128k","Multiface +3","MultiPrint",
"MB-02 ROM/RAM expansion"
},
{
"Classic AY hardware (compatible with 128k ZXs)",
"Fuller Box AY sound hardware","Currah microSpeech","SpecDrum",
"AY ACB stereo; Melodik","AY ABC stereo"
},
{
"Kempston","Cursor, Protek, AGF","Sinclair 2 Left",
"Sinclair 1 Right","Fuller"
},
{ "AMX Mouse","Kempston mouse" },
{ "Trickstick","ZX Light Gun","Zebra Graphics Tablet" },
{ "ZX Interface 1","ZX Spectrum 128k" },
{
"Kempston S","Kempston E","ZX Spectrum +3","Tasman","DK'Tronics",
"Hilderbay","INES Printerface","ZX LPrint Interface 3",
"MultiPrint","Opus Discovery","Standard 8255 chip"
},
{
"ZX Printer, Alphacom 32 & Compatibles","Generic Printer",
"EPSON Compatible"
},
{ "VTX 5000","T/S 2050 or Westridge 2050" },
{ "RD Digital Tracer","DK'Tronics Light Pen","British MicroGraph Pad" },
{ "ZX interface 1" },
{ "Keypad for ZX Spectrum 128k" },
{ "Harley Systems ADC 8.2","Blackboard Electronics" },
{ "Orme Electronics" }
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
void Error(char *errstr)
{
/* exits with an error message *errstr */
printf("\n-- Error: %s\n", errstr);
}
int n, m;
int num;
unsigned char *d;
int line = 3;
int fh; /* Input File Handle */
int ofh; /* Output File Handle */
size_t flen; /* File Length */
unsigned char *mem; /* File in Memory */
int pos; /* Position in File */
int curr; /* Current block that is playing */
int numblocks; /* Total Num. of blocks */
int block[204800]; /* Array of Block starts */
double cycle; /* Freq/3500000 */
char *sbbuf[2]; /* SB Buffers */
int sbbuflen = 1024; /* Length of buffers */
int freq = 44100; /* Sample Freq. (SHOULD THIS BE 45454 ????) NOT !!! */
int sbcurrpage = 1; /* Buffer that is currently available for writing */
int sbpos = 0; /* Position in the buffer */
int amp; /* Amplitude of the current signal */
int prvi;
int cpc = 0; /* Amstrad CPC tape ? */
int sam = 0; /* SAM Coupe tape ? */
int id; /* Current Block ID */
int pilot; /* Len of Pilot signal (in hp's) */
int sb_pilot; /* Pilot pulse */
int sb_sync1; /* Sync first half-period (hp) */
int sb_sync2; /* Sync second */
int sb_bit0; /* Bit-0 */
int sb_bit1; /* Bit-1 */
int sb_pulse; /* Pulse in Sequence of pulses and direct recording block */
int lastbyte; /* How many bits are in last byte of data ? */
int tzx_pause; /* Pause after current block (in 1s/10) */
unsigned char *data; /* Data to be played */
int datalen; /* Len of ^^^ */
int datapos; /* Position in ^^^ */
int bitcount; /* How many bits to play in current byte ? */
int sb_bit; /* should we play bit 0 or 1 ? */
char databyte; /* Current Byte to be replayed of the data */
signed short jump; /* Relative Jump */
int not_rec; /* Some blocks were not recognised ?? */
int files = 0; /* Number of Files on the command line */
char errstr[255]; /* Error String */
int starting = 1; /* starting block */
int ending = 0; /* ending block */
int au = 0; /* Are we making a .au file? */
int info = 0; /* if info=1 then show EXTENSIVE information */
/* info=2 then show ONE LINE of Info per block */
int pages = 0; /* Waiting after each page of the info ? */
int expand = 0; /* Expand Groups ? */
int draw = 1; /* Local flag for outputing a line when in a
group */
int mode128 = 0; /* Are we working in 128k mode ? (for Stop in
48k block) */
char vochead[0x20] = {'C', 'r', 'e', 'a', 't', 'i', 'v', 'e', ' ', 'V', 'o', 'i', 'c', 'e', ' ', 'F', 'i', 'l', 'e',
0x1A, 0x1A, 0x00, 0x0A, 0x01, 0x29, 0x11};
char *vocbuf; /* Buffer for .VOC block */
int vocbuflen = 0xFFFF; /* Length of .VOC block (and buffer) */
char vocstart[4] = {0x02, 0xFF, 0xFF, 0x00};
int vocpos; /* Length of current .VOC block */
char k;
int speed;
size_t x, last, lastlen;
int loop_start = 0; /* Position of the last Loop Start block */
int loop_count = 0; /* Counter of the Loop */
int call_pos = 0; /* Position of the last Call Sequence block */
int call_num = 0; /* Number of Calls in the last Call Sequence block */
int call_cur = 0; /* Current Call to be made */
int num_sel; /* Number of Selections in the Select block */
int jumparray[256]; /* Array of all possible jumps in Select block */
int sb_bit0_f, sb_bit0_s, sb_bit1_f, sb_bit1_s, xortype, sb_finishbyte_f, sb_finishbyte_s,
sb_finishdata_f, sb_finishdata_s, num_lead_in, xorvalue;
int trailing, sb_trailing;
char lead_in_byte;
int endian;
char add_bit;
#define LOAMP 0x10 /* Low Level Amplitude */
#define HIAMP 0xF0 /* High Level Amplitude */
char tstr[255];
char tstr2[255];
char tstr3[255];
char tstr4[255];
char spdstr[255];
char pstr[255];
SNDSoundStruct auhead; /* .au-file header */
int numt, nump, t2;
/* Conversion functions to get 2,3 and 4 byte words ...*/
int
Get2(unsigned char *mem)
{
return (mem[0] + (mem[1] * 256));
}
int
Get3(unsigned char *mem)
{
return (mem[0] + (mem[1] * 256) + (mem[2] * 256 * 256));
}
int
Get4(unsigned char *mem)
{
return (mem[0] + (mem[1] * 256) + (mem[2] * 256 * 256) + (mem[3] * 256 * 256 * 256));
}
/* This will convert a sampling value in Z80 T-States to samples for SoundBlaster/VOC output */
int
ConvSB(int n)
{
return ((int) (0.5 + (cycle * (double) n)));
}
#define getch() getchar()
#define GetCh() getchar()
#define AUDIO_BUFFER_SIZE 4096
unsigned char audio_buffer[AUDIO_BUFFER_SIZE];
int buf_index = 0;
int audiofd;
size_t
FileLength(int fh)
{
off_t curpos, size;
curpos = lseek(fh, 0, SEEK_CUR);
size = lseek(fh, 0, SEEK_END);
lseek(fh, curpos, SEEK_SET);
return (size);
}
size_t
lsb_write(int fh, int value)
{
char buffer[4];
buffer[3] = value & 0xff;
value >>= 8;
buffer[2] = value & 0xff;
value >>= 8;
buffer[1] = value & 0xff;
value >>= 8;
buffer[0] = value & 0xff;
return (write(fh, buffer, 4));
}
void PlaySB(char amp, int len)
{
/* Puts amplitude amp to .VOC file buffer for len time ... This .VOC file
buffer is also used for .au files */
auhead.dataSize += len;
while (len) {
vocbuf[vocpos] = amp;
len--;
vocpos++;
if (vocpos == vocbuflen) { /* Do we need to write the buffer to the .VOC file ? */
/* write(ofh, vocstart, 4); */
write(ofh, vocbuf, vocbuflen);
vocpos = 0;
}
}
}
void
PauseSB(char amp, int tzx_pause)
{
/* Waits for tzx_pause milliseconds*/
int p;
p = (int) ((((float) tzx_pause) * freq) / 1000.0);
PlaySB(amp, p);
}
void
InitAU(char *fout)
{
/* Prepares AU file to be written to ... */
auhead.magic = SND_MAGIC;
auhead.dataLocation = 32;
auhead.dataSize = 0; /* will be filled when the file is written */
auhead.dataFormat = SND_FORMAT_LINEAR_8;
auhead.samplingRate = freq ;
auhead.channelCount = 1;
/* head.info = ' '; */
vocbuf = (char *) malloc(vocbuflen + 256);
if (vocbuf == NULL) {
free(mem);
Error("Not enough memory to set up .VOC file buffer!");
return;
}
ofh = open(fout, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
write(ofh, &auhead, 0x1A); /* this is propably rubbish now... :) */
/* then go to position 32 from the beginning of the file */
lseek(ofh, 32, SEEK_SET);
vocpos = 0;
}
void
StopAU()
{
/* Finishes the au recording (writes the last buffer and puts the header to
the beginning of the file */
if (vocpos) {
write(ofh, vocbuf, vocpos);
}
lseek(ofh, 0, SEEK_SET);
/* now write the header info in LSB-format */
lsb_write(ofh, auhead.magic);
lsb_write(ofh, auhead.dataLocation);
lsb_write(ofh, auhead.dataSize);
lsb_write(ofh, auhead.dataFormat);
lsb_write(ofh, auhead.samplingRate);
lsb_write(ofh, auhead.channelCount);
free(vocbuf);
close(ofh);
}
void
ToggleAmp()
{
/* Toggles the amplitude of the output*/
if (amp == LOAMP)
amp = HIAMP;
else
amp = LOAMP;
}
void
PlayC64SB(int len)
{
PlaySB(amp, len);
ToggleAmp();
PlaySB(amp, len);
ToggleAmp();
}
void
GetC64ROMName(char *name, unsigned char *data)
{
char d;
for (n = 0; n < 16; n++) {
d = data[14 + n];
if (d < 32 || d > 125)
name[n] = ' ';
else
name[n] = d;
}
name[n] = 0;
}
char
MirrorByte(char s)
{
return ((s << 7) + ((s << 5) & 64) + ((s << 3) & 32) + ((s << 1) & 16) + ((s >> 1) & 8) + ((s >> 3) & 4) + ((s >> 5) & 2) + (s >> 7));
}
void
GetC64StandardTurboTapeName(char *name, char *data)
{
char d;
for (n = 0; n < 16; n++) {
d = data[15 + n];
if (d < 32 || d > 125)
name[n] = ' ';
else
name[n] = d;
}
name[n] = 0;
}
void
IdentifyC64ROM(int pos, unsigned char *data, int type)
{
char name[255];
/* Determine Loader type */
if (sb_pilot == ROM_S_HALF && sb_sync1 == ROM_L_HALF && sb_sync2 == ROM_M_HALF &&
sb_bit0_f == ROM_S_HALF && sb_bit0_s == ROM_M_HALF && sb_bit1_f == ROM_M_HALF &&
sb_bit1_s == ROM_S_HALF && xortype == 0x01) {
/* ROM Loader */
if (data[0] == 0x89 && data[1] == 0x88 && data[2] == 0x87 && data[3] == 0x86 &&
data[4] == 0x85 && data[5] == 0x84 && data[6] == 0x83 && data[7] == 0x82 &&
data[8] == 0x81) {
if (pos == 202) {
if (!type) {
strcpy(name, "Header: ");
GetC64ROMName(name + 8, data);
} else {
strcpy(name, "ROM Header: ");
GetC64ROMName(name + 12, data);
}
} else {
if (!type) {
strcpy(name, "Data Block ");
} else {
strcpy(name, "ROM: Data Block");
}
}
} else {
if (!type)
strcpy(name, "------------------------");
else
strcpy(name, "ROM: Last Block Repeated");
}
strcpy(tstr, name);
strcpy(spdstr, "C64 ROM Data ");
return;
}
if (!type)
strcpy(tstr, "------------------------");
else
strcpy(tstr, "Unknown");
strcpy(spdstr, "C64 Data ");
}
void
IdentifyC64Turbo(int pos, char *data, int type)
{
char name[255];
/* Determine Loader type */
if (sb_bit0 == STT_0_HALF && sb_bit1 == STT_1_HALF && lead_in_byte == 0x02) {
/* Standard Turbo Tape Loader */
if (data[0] == 0x09 && data[1] == 0x08 && data[2] == 0x07 && data[3] == 0x06 &&
data[4] == 0x05 && data[5] == 0x04 && data[6] == 0x03 && data[7] == 0x02 &&
data[8] == 0x01) {
if (pos == 32 && data[9] != 0x00) {
if (!type) {
strcpy(name, "Header: ");
GetC64StandardTurboTapeName(name + 8, data);
} else {
strcpy(name, "TurboTape Header: ");
GetC64StandardTurboTapeName(name + 18, data);
}
} else {
if (!type) {
strcpy(name, "------------------------");
} else {
strcpy(name, "TurboTape Data Block");
}
}
} else {
if (!type)
strcpy(name, "------------------------");
else
strcpy(name, "TurboTape Unknown");
}
strcpy(tstr, name);
strcpy(spdstr, "C64 Turbo ");
return;
}
if (!type)
strcpy(tstr, "------------------------");
else
strcpy(tstr, "Unknown");
strcpy(spdstr, "C64 Data ");
}
void
Identify(int len, char *temp, int type)
{
int n, s;
if (cpc) {
if (temp[0] == 44) {
if (!type)
s = 4;
else
s = 0;
strcpy(tstr, " ");
for (n = 0; n < 16; n++) {
if (temp[n + 1])
tstr[n + s] = temp[n + 1];
else
tstr[n + s] = ' ';
}
for (n = 0; n < 4; n++)
tstr[n + s + 16] = ' ';
tstr[n + s + 16] = 0;
} else {
if (!type)
strcpy(tstr, " ------------------ ");
else
strcpy(tstr, "Headerless");
}
return;
}
if (sam) {
if (temp[0] == 1 && (len > 80 && len < 84) && (temp[1] >= 0x10 && temp[1] <= 0x13)) {
if (!type) {
s = 14;
switch (temp[1]) {
case 0x10:
strcpy(tstr, " Program : ");
break;
case 0x11:
strcpy(tstr, " Num. Array : ");
break;
case 0x12:
strcpy(tstr, "Char. Array : ");
break;
case 0x13:
strcpy(tstr, " Bytes : ");
break;
}
} else {
switch (temp[1]) {
case 0x10:
strcpy(tstr, "Program : ");
s = 10;
break;
case 0x11:
strcpy(tstr, "Num. Array : ");
s = 13;
break;
case 0x12:
strcpy(tstr, "Char. Array : ");
s = 14;
break;
case 0x13:
strcpy(tstr, "Bytes : ");
s = 8;
break;
}
}
for (n = 0; n < 10; n++) {
if (temp[n + 2] > 31 && temp[n + 2] < 127)
tstr[n + s] = temp[n + 2];
else
tstr[n + s] = 32;
}
tstr[n + s] = 0;
} else {
if (!type)
strcpy(tstr, " --------------------"); /* Not Header */
else
strcpy(tstr, "Headerless");
}
return;
}
if (temp[0] == 0 && (len == 19 || len == 20) && temp[1] < 4) {
if (!type) {
s = 14;
switch (temp[1]) {
case 0x00:
strcpy(tstr, " Program : ");
break;
case 0x01:
strcpy(tstr, " Num. Array : ");
break;
case 0x02:
strcpy(tstr, "Char. Array : ");
break;
case 0x03:
strcpy(tstr, " Bytes : ");
break;
}
} else {
switch (temp[1]) {
case 0x00:
strcpy(tstr, "Program : ");
s = 10;
break;
case 0x01:
strcpy(tstr, "Num. Array : ");
s = 13;
break;
case 0x02:
strcpy(tstr, "Char. Array : ");
s = 14;
break;
case 0x03:
strcpy(tstr, "Bytes : ");
s = 8;
break;
}
}
for (n = 0; n < 10; n++) {
if (temp[n + 2] > 31 && temp[n + 2] < 127)
tstr[n + s] = temp[n + 2];
else
tstr[n + s] = 32;
}
tstr[n + s] = 0;
} else {
if (!type)
strcpy(tstr, " --------------------"); /* Not Header */
else
strcpy(tstr, "Headerless");
}
}
int
getnumber(char *s)
{
/* Returns the INT number contained in string *s */
int i;
sscanf(s, "%d", &i);
return (i);
}
void
ChangeFileExtension(char *str, char *ext)
{
/* Changes the File Extension of String *str to *ext */
size_t n;
n = strlen(str);
while (str[n] != '.')
n--;
n++;
str[n] = 0;
strcat(str, ext);
}
void
invalidoption(char *s)
{
/* Prints the Invalid Option error */
sprintf(errstr, "Invalid Option %s !", s);
Error(errstr);
return;
}
char *
GetCheckSum(unsigned char *data, int len)
{
/* Calculates a XOR checksum for a block and returns a STRING containing the result*/
unsigned char c = 0;
int n;
for (n = 0; n < len - 1; n++)
c ^= data[n];
if (c == data[len - 1])
return ("OK");
else {
sprintf(pstr, "Wrong, should be %3d ($%02X)", c, c);
return (pstr);
}
}
void
CopyString(char *dest, unsigned char *sour, int len)
{
/* Could just use strpy ... */
int n;
for (n = 0; n < len; n++)
dest[n] = sour[n];
dest[n] = 0;
}
void
writeout(char *s)
{
/* Simple and not too accurate method of waiting after pages ...*/
char k;
if (pages) {
line++;
if (line > 21) {
printf("scroll?\n");
k = getch();
if (k == 27) {
free(mem);
close(fh);
Error("ESCAPE key pressed!");
}
if (!k)
getch();
printf("\n");
line = 0;
}
}
printf("%s",s);
}
int
MultiLine(char *s, int spaces, char *d)
{
/* This will convert a text which has lines separated by a single LF (13 dec)*/
/* character to the text that can be outputed by MSDOS (LF NL) ... it will also*/
/* Add a number of spaces to the beginning of each line (EXCEPT the first one -*/
/* so you can use the Description: Text stuff ;) )*/
/* NOTE: Some UNIX system like LINUX can cope with just LF char (13), some*/
/* other systems will need just CR char (10) ... experiment :)*/
int n = 0;
int m = 0;
int i;
int l = 0;
while (s[n]) {
if (s[n] == 13) {
d[m] = 13;
d[m + 1] = 10; /* Here is the MS-DOS output for line-end */
m += 2;
for (i = 0; i < spaces; i++) {
d[m] = ' ';
m++;
}
l++;
} else {
d[m] = s[n];
m++;
}
n++;
}
d[m] = 0;
return (l);
}
void
MakeFixedString(char *s, int i)
{
/* This will create a fixed length string from null-terminated one...*/
int n = 0;
int k = 0;
while (i) {
if (!s[n])
k = 1;
if (k)
s[n] = ' ';
n++;
i--;
}
s[n] = 0;
}
void
PlayC64ROMByte(char byte, int finish)
{
xorvalue = xortype;
while (bitcount) {
if (!endian)
sb_bit = byte & 0x01;
else
sb_bit = byte & 0x80;
if (sb_bit) {
if (sb_bit1_f)
PlayC64SB(sb_bit1_f);
if (sb_bit1_s)
PlayC64SB(sb_bit1_s);
xorvalue ^= sb_bit;
} else {
if (sb_bit0_f)
PlayC64SB(sb_bit0_f);
if (sb_bit0_s)
PlayC64SB(sb_bit0_s);
xorvalue ^= sb_bit;
}
if (!endian)
byte >>= 1;
else
byte <<= 1;
bitcount--;
}
if (xortype != 0xFF) {
if (xorvalue) {
if (sb_bit1_f)
PlayC64SB(sb_bit1_f);
if (sb_bit1_s)
PlayC64SB(sb_bit1_s);
} else {
if (sb_bit0_f)
PlayC64SB(sb_bit0_f);
if (sb_bit0_s)
PlayC64SB(sb_bit0_s);
}
}
if (!finish) {
if (sb_finishbyte_f)
PlayC64SB(sb_finishbyte_f);
if (sb_finishbyte_s)
PlayC64SB(sb_finishbyte_s);
} else {
if (sb_finishdata_f)
PlayC64SB(sb_finishdata_f);
if (sb_finishdata_s)
PlayC64SB(sb_finishdata_s);
}
}
void
PlayC64TurboByte(char byte)
{
int add_num;
add_num = add_bit & 3;
if (add_num && !(add_bit & 4)) {
while (add_num) {
if (add_bit & 8)
PlayC64SB(sb_bit1);
else
PlayC64SB(sb_bit0);
add_num--;
}
}
while (bitcount) {
if (!endian)
sb_bit = byte & 0x01;
else
sb_bit = byte & 0x80;
if (sb_bit)
PlayC64SB(sb_bit1);
else
PlayC64SB(sb_bit0);
if (!endian)
byte >>= 1;
else
byte <<= 1;
bitcount--;
}
if (add_num && (add_bit & 4)) {
while (add_num) {
if (add_bit & 8)
PlayC64SB(sb_bit1);
else
PlayC64SB(sb_bit0);
add_num--;
}
}
}
int playtzx_main(const char *finp, char *fout)
{
/*
printf("\nZXTape Utilities - Play TZX , TZX to VOC Converter and TZX Info v0.12b for Linux\n");
if (argc < 2) {
printf("\nUsage: playtzx [switches] file.tzx [output.voc|output.au]\n\n");
printf(" Switches: -voc Create a .VOC file instead of audio output\n");
printf(" -au Create a .au file instead of audio output\n");
printf(" -freq n Set sampling frequency to n Hz\n");
printf(" -info Show extensive Info on TZX file\n");
printf(" -one Show One line of Info per block (condensed form)\n");
printf(" -x eXpand the Groups in one line mode\n");
printf(" -b n Start replay/conversion at block n\n");
printf(" -e n End replay/conversion after block n\n");
printf(" -p Wait after each page of Info\n");
printf(" -128 Work in 128k mode\n");
exit(0);
}
for (n = 1; n < argc; n++) {
if (argv[n][0] == '-') {
switch (argv[n][1]) {
case 'a':
if (strcmp(argv[n], "-au"))
invalidoption(argv[n]);
au = 1;
break;
case 'v':
if (strcmp(argv[n], "-voc"))
invalidoption(argv[n]);
voc = 1;
freq = 30303;
break;
case 'i':
if (strcmp(argv[n], "-info"))
invalidoption(argv[n]);
info = 1;
break;
case 'o':