-
Notifications
You must be signed in to change notification settings - Fork 1
/
tzx2tap.c
510 lines (447 loc) · 14.4 KB
/
tzx2tap.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
#pragma output CLIB_MALLOC_HEAP_SIZE = -1
///////////////////////////////////////////////////////////////////////////////
// TZX2TAP for ZX Spectrum Next (NextZXOS) by Chris Young 2020
// Based on TZX to TAP converter v0.13b (c) 1997 Tomaz Kac
//
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arch/zxn.h>
#include <arch/zxn/esxdos.h>
#include <arch/zxn/sysvar.h>
#include <z80.h>
#define PROGVER "1.3.1"
#define MAX_HEADER_SIZE 0x14 // Size of largest block header
#define MAJREV 1 // Major revision of the format this program supports
#define MINREV 20 // Minor revision -||-
#define RTM_28MHZ 3 // from manual
static unsigned char old_cpu_speed;
static bool browser = false;
static uint32_t Get2(char *mem) { return(mem[0]+(mem[1]*256UL)); }
static uint32_t Get3(char *mem) { return(mem[0]+(mem[1]*256UL)+(mem[2]*256UL*256UL)); }
static uint32_t Get4(char *mem) { return(mem[0]+(mem[1]*256UL)+(mem[2]*256UL*256UL)+(mem[3]*256UL*256UL*256UL)); }
// exits with an error message *errstr
static int Err(char *errstr, bool browser)
{
if(browser) {
printf("\x16\x15\x08\x14\x01\x13\x01%s", errstr);
} else {
printf("\nError: %s", errstr);
}
return 0;
}
// Changes the File Extension of String *str to *ext
static void ChangeFileExtension(char *str,char *ext)
{
int n;
n=strlen(str);
while(str[n]!='.')
n--;
n++;
str[n]=0;
strcat(str,ext);
}
// Determine length of file
static uint32_t FileLength(unsigned char fh)
{
struct esx_stat es;
errno = 0;
if(esx_f_fstat(fh, (struct esx_stat *)&es)) {
return 0;
}
return(es.size);
}
// read next header from input and return the position within the file
static uint32_t read_file(unsigned char fh, char *mem, uint32_t posn)
{
esx_f_seek(fh, posn, ESX_SEEK_SET);
esx_f_read(fh, mem, MAX_HEADER_SIZE);
return posn;
}
// read chunks of 1K from input and write to output
static int convert_data(unsigned char fhi, unsigned char fho, uint32_t posn, uint32_t len)
{
char *buf;
uint32_t bytes_read = 0;
uint32_t bytes_to_read;
buf=(char *) malloc(1024);
if(buf==NULL) {
Err("Out of memory", browser); //ERRB_4_OUT_OF_MEMORY
return -1;
}
esx_f_seek(fhi, posn, ESX_SEEK_SET);
while(bytes_read < len) {
if((len - bytes_read) <= 1024) {
bytes_to_read = (len - bytes_read);
} else {
bytes_to_read = 1024;
}
esx_f_read(fhi, buf, bytes_to_read);
esx_f_write(fho, buf, bytes_to_read);
bytes_read += bytes_to_read;
}
free(buf);
return 0;
}
int main(int argc, char *argv[])
{
unsigned char fhi,fho = 0;
uint32_t flen;
char *mem = NULL;
char buf[256];
uint32_t pos = 10, p, oldpos = 0;
uint32_t len;
long block = 0, blocks = 0;
bool longer,custom,only,dataonly,direct,not_rec,snap,call_seq,deprecated;
bool verbose = false;
bool help = false;
bool list = false;
char tzxbuf[10]={ 'Z','X','T','a','p','e','!', 0x1A, 1, 00 };
uint32_t start;
long loop_start = 0;
uint32_t loop_count = 0;
int err = 0;
int i;
int converted = 0;
char conv[4]={0x20, 0x2b, 0x2a, 0x3e};
const char *type[4]={"Program", "Num array", "Char array", "Bytes"};
char *src = NULL;
char *dst = NULL;
if(argc>1) {
for(i=1;i<argc;i++) {
if(strcmp(argv[i], "-v") == 0) {
verbose = true;
} else if(strcmp(argv[i], "-l") == 0) {
list = true;
verbose = true; /* list implies verbose */
} else if(strcmp(argv[i], "-b") == 0) {
browser = true;
} else if(strcmp(argv[i], "-h") == 0) {
help = true;
} else {
if(src==NULL) {
src = argv[i];
} else if(dst==NULL) {
dst = argv[i];
} else {
help = true;
}
}
}
} else {
help = true;
}
if(browser && src) {
if(verbose || list) {
help = true;
} else {
printf("\x16\x15\x01\x08\x14\x01\x13\x01TZX2TAP ");
}
} else {
printf("TZX2TAP v%s by Chris Young\ngithub.com/chris-y/tzx2tap\n", PROGVER);
// printf("Based on ZXTape Utilities\nTZX to TAP Converter v0.13b\n");
}
if((help==true) || (src==NULL) || ((list==true) && (dst!=NULL))) {
printf("\nUsage:\n.TZX2TAP [OPTS] IN.TZX [OUT.TAP]\n");
printf("\nWhere OPTS is one of:\n");
printf(" -h Show this help\n");
printf(" -l List\n");
printf(" -v Verbose\n");
printf(" -b Browser mode\n");
return 0;
}
if(dst==NULL) {
strcpy(buf,src);
ChangeFileExtension(buf,"tap");
} else {
strcpy(buf,dst);
}
old_cpu_speed = ZXN_READ_REG(REG_TURBO_MODE);
ZXN_NEXTREG(REG_TURBO_MODE, RTM_28MHZ);
errno = 0;
fhi = esx_f_open(src, ESX_MODE_READ);
if(errno) {
err = errno;
goto end;
}
if(!list) {
errno = 0;
fho = esx_f_open(buf, ESX_MODE_WRITE | ESX_MODE_OPEN_CREAT_NOEXIST);
if(errno) {
if(errno == ESX_EEXIST) {
if(!browser) printf("\nFile already converted?");
err = Err("File exists", browser); //ERRB_4_OUT_OF_MEMORY
goto end;
} else {
err = errno;
goto end;
}
}
}
flen=FileLength(fhi);
if(flen==0) {
err=errno;
goto end;
}
mem=(char *) malloc(MAX_HEADER_SIZE);
if(mem==NULL) {
err = Err("Out of memory", browser); //ERRB_4_OUT_OF_MEMORY
goto end;
}
esx_f_read(fhi,mem,10); mem[7]=0;
if(strcmp(mem,"ZXTape!")) {
err = ESX_EWRTYPE;
goto end;
}
if(!browser) printf("\nZXTape file revision %d.%02d\n",mem[8],mem[9]);
if(!mem[8]) {
if(!browser) printf("Error: TZX dev ver not supported\n");
err = ESX_EWRTYPE;
goto end;
}
if(mem[8]>MAJREV)
if(!browser) printf("\nWarning: Some blocks may not be recognised and used\n");
if(mem[8]==MAJREV && mem[9]>MINREV)
if(!browser) printf("\nWarning: Some of the data might not be properly recognised\n");
longer=custom=only=dataonly=direct=not_rec=snap=call_seq=deprecated=false;
if((!list) && (!browser)) printf("\nConverting...");
if(verbose) {
printf("\nID Len ");
}
while(pos<flen) {
start = read_file(fhi, mem, pos);
pos++;
p = pos - start;
if(verbose) {
printf("\n%02x", mem[p-1]);
oldpos = pos;
strcpy(buf, "");
converted = 0;
if(list==false) {
z80_bpoke(23692, 50);
}
}
switch(mem[p-1])
{
case 0x10: len=Get2(&mem[p+0x02]);
if(!list) {
err = convert_data(fhi, fho, pos+0x02, 2);
if(err) {
err=0;
goto end;
}
err = convert_data(fhi, fho, pos+0x04, len);
if(err) {
err=0;
goto end;
}
}
if(verbose) {
converted = 1;
if(mem[p+0x04]==0) { //header
sprintf(buf, "%s: %.10s", type[mem[p+0x05]], &mem[p+0x06]);
} else {
sprintf(buf, "Data: %u bytes", len);
}
}
pos+=len+0x04;
block++;
break;
case 0x11: len=Get3(&mem[p+0x0F]);
if(len<65536) {
if(!list) {
err = convert_data(fhi, fho, pos+0x0F, 2);
if(err) {
err=0;
goto end;
}
err = convert_data(fhi, fho, pos+0x12, len);
if(err) {
err=0;
goto end;
}
}
if(verbose) converted = 2;
block++;
}
else {
longer=true;
if(verbose) converted = 3;
}
custom=true;
pos+=len+0x12;
if(verbose) strcpy(buf, "Turbo data");
break;
case 0x12: only=true;
pos+=0x04;
if(verbose) strcpy(buf, "Pure tone");
break;
case 0x13: only=true;
pos+=(mem[p+0x00]*0x02)+0x01;
if(verbose) strcpy(buf, "Pulse sequence");
break;
case 0x14: len=Get3(&mem[p+0x07]);
if(len<65536) {
if(!list) {
err = convert_data(fhi, fho, pos+0x07, 2);
if(err) {
err=0;
goto end;
}
err = convert_data(fhi, fho, pos+0x0A, len);
if(err) {
err=0;
goto end;
}
}
if(verbose) converted = 2;
block++;
}
else {
longer=true;
if(verbose) converted = 3;
}
dataonly=true;
pos+=len+0x0A;
if(verbose) strcpy(buf, "Custom data");
break;
case 0x15: direct=true;
pos+=Get3(&mem[p+0x05])+0x08;
if(verbose) strcpy(buf, "Direct recording");
break;
case 0x16:
case 0x17: deprecated = true;
pos+=Get4(&mem[p+0x00]+0x04);
if(verbose) sprintf(buf, "C64 %s", mem[p-1]==0x16 ? "standard" : "turbo");
break;
case 0x18:
case 0x19: only = true;
pos+=Get4(&mem[p+0x00]+0x04);
if(verbose) sprintf(buf, "%s", mem[p-1]==0x18 ? "CSW recording" : "General data");
break;
case 0x20: pos+=0x02;
if(verbose) sprintf(buf, "Pause for %ums", Get2(&mem[p+0x00]));
break;
case 0x21: pos+=mem[p+0x00]+0x01;
if(verbose) sprintf(buf, "Group: %.*s", mem[p+0x00]>17 ? 17 : mem[p+0x00], &mem[p+0x01]);
break;
case 0x22: if(verbose) strcpy(buf, "Group end");
break;
case 0x23: pos+=0x02;
if(verbose) sprintf(buf, "Jump %u blocks", Get2(&mem[p+0x00]));
break;
case 0x24: pos+=0x02;
if(list==false) {
loop_start=pos;
loop_count=Get2(&mem[p+0x00]);
}
if(verbose) {
converted = 1;
sprintf(buf, "Loop start, count=%u", loop_count);
}
break;
case 0x25: if(list==false) {
if(loop_count > 0) {
pos = loop_start;
loop_count--;
}
}
if(verbose) {
converted = 1;
sprintf(buf, "Loop end, count=%u", loop_count);
}
break;
case 0x26: pos += (Get2(&mem[p+0x00])*2)+0x02;
call_seq = true;
if(verbose) strcpy(buf, "Call sequence");
break;
case 0x27: call_seq = true;
if(verbose) strcpy(buf, "Call sequence return");
break;
case 0x28: pos += Get2(&mem[p+0x00])+0x02;
if(verbose) sprintf(buf, "Select, items=%d", mem[p+0x02]);
break;
case 0x2A: pos+=Get4(&mem[p+0x00]+0x04);
if(verbose) strcpy(buf, "Stop tape in 48K mode");
case 0x2B: pos+=Get4(&mem[p+0x00]+0x04);
if(verbose) sprintf(buf, "Set signal level %s", mem[p+0x04] == 0 ? "low" : "high");
break;
case 0x30: pos+=mem[p+0x00]+0x01;
if(verbose) sprintf(buf, "Desc: %.*s", mem[p+0x00]>18 ? 18 : mem[p+0x00], &mem[p+0x01]);
break;
case 0x31: pos+=mem[p+0x01]+0x02;
if(verbose) sprintf(buf, "Message: %.*s", mem[p+0x01]>15 ? 15 : mem[p+0x01], &mem[p+0x02]);
break;
case 0x32: pos+=Get2(&mem[p+0x00])+0x02;
if(verbose) sprintf(buf, "Arc info %02x=%.*s", mem[p+0x03], mem[p+0x04]>12 ? 12 : mem[p+0x04], &mem[p+0x05]);
break;
case 0x33: pos+=(mem[p+0x00]*0x03)+0x01;
if(verbose) strcpy(buf, "Hardware type");
break;
case 0x34: pos+=0x08;
deprecated = true;
if(verbose) strcpy(buf, "Emulation info");
break;
case 0x35: pos+=Get4(&mem[p+0x10])+0x14;
if(verbose) sprintf(buf, "Custom info: %.*s", 10, &mem[p+0x00]);
break;
case 0x40: pos+=Get3(&mem[p+0x01])+0x04;
snap = true;
deprecated = true;
if(verbose) sprintf(buf, "Snapshot: %s", mem[p+0x00] == 0 ? "Z80" : "SNA");
break;
case 0x5A: pos+=0x09;
if(verbose) strcpy(buf, "Glue block");
break;
default: pos+=Get4(&mem[p+0x00]+0x04);
not_rec=true;
if(verbose) strcpy(buf, "UNKNOWN");
}
blocks++;
if(verbose) {
printf("%c", conv[converted]);
if(pos > oldpos) {
printf("%04x %s", pos - oldpos, buf);
} else {
printf("0000 %s", buf);
}
} else if(browser) {
printf("\x16\x15\x08\x14\x01\x13\x01%c[%02lx/%02lx]", (custom|longer|dataonly) ? conv[2] : conv[0], block, blocks);
} else {
printf(".");
}
}
if(!browser) {
printf("\n\n");
if(!list) {
if(custom)
printf("Warning: Custom Loading blocks were converted\n\n");
if(longer)
printf("Warning: Over 64k long Custom Loading blocks not converted\n\n");
if(only)
printf("Warning: Sequence of Pulses and/or Pure Tone blocks encountered\n\n");
if(dataonly)
printf("Warning: Data Only blocks were converted\n\n");
if(direct)
printf("Warning: Direct Recording blockswere encountered\n\n");
if(call_seq)
printf("Warning: Call sequence blocks were encountered\n\n");
if(deprecated)
printf("Warning: Deprecated blocks were encountered\n\n");
if(snap)
printf("Note: Embedded snapshot(s) not extracted\n\n");
if(not_rec)
printf("Warning: Some blocks were NOT recognised\n\n");
printf("Converted %ld blocks of %ld\n",block,blocks);
}
}
end:
if(fhi) esx_f_close(fhi);
if(fho) esx_f_close(fho);
if(mem) free(mem);
ZXN_NEXTREGA(REG_TURBO_MODE, old_cpu_speed);
return err;
}