forked from pete-gordon/oricutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtape.c
1570 lines (1367 loc) · 39.7 KB
/
tape.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
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "joystick.h"
#include "filereq.h"
#include "tape.h"
#include "msgbox.h"
extern char tapefile[], tapepath[];
extern SDL_bool refreshtape;
char tmptapename[4096];
extern char filetmp[];
// Pop-up the name of the currently inserted tape
// image file (or an eject symbol if no tape image).
void tape_popup( struct machine *oric )
{
char tmp[40];
if( !oric->tapename[0] )
{
// Eject symbol
do_popup( oric, "\x0f\x10\x13" );
return;
}
// Tape image name
sprintf( tmp, "\x0f\x10""%c %-16s", oric->tapemotor ? 18 : 17, oric->tapename );
do_popup( oric, tmp );
}
void toggletapecap( struct machine *oric, struct osdmenuitem *mitem, int dummy )
{
char ortheader[] = { 'O', 'R', 'T', 0 }; // Oric Raw Tape, Version 0
/* If capturing, stop */
if( oric->tapecap )
{
fclose( oric->tapecap );
oric->tapecap = NULL;
mitem->name = "Save tape output...";
refreshtape = SDL_TRUE;
return;
}
/* Otherwise, prompt for the file to capture */
if( !filerequester( oric, "Capture tape output", tapepath, tmptapename, FR_TAPESAVEORT ) )
{
// Never mind
return;
}
if( tmptapename[0] == 0 ) return;
/* If it ends in ".tap", we need to change it to ".ort", because
we're capturing real signals here folks! */
if( (strlen(tmptapename)>3) && (strcasecmp(&tmptapename[strlen(tmptapename)-4], ".tap")==0) )
tmptapename[strlen(tmptapename)-4] = 0;
/* Add .ort extension, if necessary */
if( (strlen(tmptapename)<4) || (strcasecmp(&tmptapename[strlen(tmptapename)-4], ".ort")!=0) )
strncat(tmptapename, ".ort", 4096);
tmptapename[4095] = 0;
joinpath( tapepath, tmptapename );
/* Open the file */
oric->tapecap = fopen( filetmp, "wb" );
if( !oric->tapecap )
{
/* Oh well */
msgbox( oric, MSGBOX_OK, "Unable to create file" );
return;
}
/* Write header */
fwrite( ortheader, 4, 1, oric->tapecap);
/* Counter reset */
oric->tapecapcount = -1;
oric->tapecaplastbit = (oric->via.orb&oric->via.ddrb)>>7;
/* Update menu */
mitem->name = "Stop tape recording";
refreshtape = SDL_TRUE;
}
/* When we're loading a .tap file (or a non-raw section of a .ort file),
** we have to do 2 things... First, we have to elongate the leader so the ROM
** can see it, and secondly we have to add a delay after the header so that the
** tape isn't several bytes into the program when the rom is ready to start
** reading it.
**
** Only call it when you think you're at a tape leader since it doesn't do many
** sanity checks.
*/
void tape_setup_header( struct machine *oric )
{
int i;
oric->tapedupbytes = 0;
oric->tapehdrend = 0;
if( oric->tapebuf[oric->tapeoffs] != 0x16 )
return;
/* Does it look like a header? */
for( i=0; oric->tapebuf[oric->tapeoffs+i]==0x16; i++ )
{
if( (oric->tapeoffs+i) >= oric->tapelen )
return;
}
if( ( i < 3 ) || ( oric->tapebuf[oric->tapeoffs+i] != 0x24 ) )
return;
i++;
if( (oric->tapeoffs+i+9) >= oric->tapelen )
return;
i+=9;
while( oric->tapebuf[oric->tapeoffs+i] != 0 )
{
if( (oric->tapeoffs+i) >= oric->tapelen )
return;
i++;
}
i++;
oric->tapedupbytes = 80;
oric->tapehdrend = oric->tapeoffs+i;
}
void tape_orbchange( struct via *via )
{
struct machine *oric = via->oric;
unsigned char buffer[4], tapebit, bufwrite;
unsigned int count;
/* Capturing tape output? */
if( !oric->tapecap ) return;
/* Saving a tap section? */
if( oric->tapecap == oric->tsavf ) return;
tapebit = (via->orb & via->ddrb) >> 7;
if( tapebit == oric->tapecaplastbit ) return;
oric->tapecaplastbit = tapebit;
/* Were we waiting for the first toggle? */
if( oric->tapecapcount < 0 )
{
/* Well, we found it */
oric->tapecapcount = 0;
fputc( tapebit, oric->tapecap );
return;
}
count = (oric->tapecapcount>>1);
if( count < 0xfc )
{
buffer[0] = count;
bufwrite = 1;
}
else if( count < 0x100 )
{
buffer[0] = 0xfc;
buffer[1] = count;
bufwrite = 2;
}
else if( count < 0x10000 )
{
buffer[0] = 0xfd;
buffer[1] = (count>>8)&0xff;
buffer[2] = count&0xff;
bufwrite = 3;
}
else
{
buffer[0] = 0xfd;
buffer[1] = 0xff;
buffer[2] = 0xff;
bufwrite = 3;
}
fwrite( buffer, bufwrite, 1, oric->tapecap );
oric->tapecapcount = 0;
}
// Set the tape motor on or off
void tape_setmotor( struct machine *oric, SDL_bool motoron )
{
if( motoron == oric->tapemotor )
return;
// Refresh the tape status icon in the status bar
refreshtape = SDL_TRUE;
// "Real" tape emulation?
if( ( !oric->tapeturbo ) || ( !oric->pch_tt_available ) )
{
if( ( !oric->rawtape ) || ( oric->tapeoffs < oric->nonrawend ) )
{
// If we're stopping part way through a byte, just move
// the current position on to the start of the next byte
if( !motoron )
{
if( oric->tapebit > 0 )
oric->tapeoffs++;
oric->tapebit = 0;
}
// If we're starting the tape motor and the tape
// is at a sync header, generate a bunch of dummy
// sync bytes. This makes the Oric Atmos welcome tape
// work without turbo tape enabled, for example.
if( ( motoron ) &&
( oric->tapebuf ) &&
( oric->tapeoffs < oric->tapelen ) &&
( oric->tapebuf[oric->tapeoffs] == 0x16 ) )
{
tape_setup_header(oric);
}
}
}
// Set the new status and do a popup
oric->tapemotor = motoron;
if( oric->tapename[0] ) tape_popup( oric );
}
// Free up the current tape image
void tape_eject( struct machine *oric )
{
if( oric->tapebuf ) free( oric->tapebuf );
oric->tapebuf = NULL;
oric->tapelen = 0;
oric->tapename[0] = 0;
tape_popup( oric );
refreshtape = SDL_TRUE;
}
void tape_next_raw_count( struct machine *oric )
{
unsigned int n;
int nonrawend;
if( oric->tapeoffs >= oric->tapelen )
{
if( oric->nonrawend != oric->tapelen )
oric->tapehitend = 3;
return;
}
n = oric->tapebuf[oric->tapeoffs++];
if (n < 0xfc)
{
oric->tapecount = n<<1;
return;
}
switch (n)
{
case 0xff: // non-raw section
if( oric->tapeoffs >= (oric->tapelen-1) )
{
oric->tapeoffs = oric->tapelen;
oric->tapehitend = 3;
return;
}
nonrawend = ((oric->tapebuf[oric->tapeoffs]<<8)|oric->tapebuf[oric->tapeoffs+1]) + oric->tapeoffs+2;
if( nonrawend > oric->tapelen )
{
oric->tapeoffs = oric->tapelen;
oric->tapehitend = 3;
return;
}
oric->nonrawend = nonrawend;
oric->tapeoffs += 2;
oric->tapebit = 0;
oric->tapecount = 2;
oric->tapeout = 0;
tape_setup_header( oric );
return;
case 0xfc:
if( oric->tapeoffs >= oric->tapelen )
{
oric->tapehitend = 3;
return;
}
oric->tapecount = oric->tapebuf[oric->tapeoffs++] << 1;
return;
case 0xfd:
if( oric->tapeoffs >= (oric->tapelen-1) )
{
oric->tapeoffs = oric->tapelen;
oric->tapehitend = 3;
return;
}
oric->tapecount = (oric->tapebuf[oric->tapeoffs]<<9)|(oric->tapebuf[oric->tapeoffs+1]<<1);
oric->tapeoffs += 2;
return;
}
// Invalid data
oric->tapeoffs = oric->tapelen;
oric->tapehitend = 3;
}
// Rewind to the start of the tape
void tape_rewind( struct machine *oric )
{
oric->nonrawend = 0;
if( oric->rawtape )
{
oric->tapeout = oric->tapebuf[4];
via_write_CB1( &oric->via, oric->tapeout );
oric->tapeoffs = 5;
tape_next_raw_count( oric );
}
else
{
oric->tapeoffs = 0;
oric->tapebit = 0;
oric->tapecount = 2;
oric->tapeout = 0;
oric->tapedupbytes = 0;
if( oric->tapebuf )
tape_setup_header( oric );
}
oric->tapehitend = 0;
oric->tapedelay = 0;
refreshtape = SDL_TRUE;
}
// This is used by the "tapsections" function. It returns the next time
// value from an ort buffer, or -1 for invalid (or if an existing tapsection
// is found)
static int next_ort_value( unsigned char *ortdata, int *offset, int end, SDL_bool *anytapsec )
{
int val=0, skip;
while ((*offset) < end)
{
switch (ortdata[*offset])
{
case 0xfc:
if ((*offset) >= (end-1)) return 0;
val = ortdata[(*offset)+1];
(*offset) += 2;
return val;
case 0xfd:
if ((*offset) >= (end-2)) return 0;
val = (ortdata[(*offset)+1]<<8)|ortdata[(*offset)+2];
(*offset) += 3;
return val;
case 0xfe:
return 0;
case 0xff:
if ((*offset) >= (end-2)) return 0;
skip = (ortdata[(*offset)+1]<<8)|ortdata[(*offset)+2];
(*offset)+=3;
if ((*offset) >= (end-skip)) return 0;
(*offset)+=skip;
(*anytapsec) = SDL_TRUE;
break;
default:
return ortdata[(*offset)++];
}
}
return 0;
}
enum
{
TAPSEC_STATE_SEARCH = 0,
TAPSEC_STATE_LEADER,
TAPSEC_STATE_FINDHEADER,
TAPSEC_STATE_HEADER,
TAPSEC_STATE_FILENAME,
TAPSEC_STATE_FINDDATA,
TAPSEC_STATE_DATA
};
static int validbyte(int accum)
{
int i, parity = 0;
if ((accum&3)!=1) return 0;
for (i=4; i!=0x800; i<<=1)
if (accum&i) parity ^= 1;
return parity;
}
// This converts oric standard encoded waveforms to decoded TAP sections
static int tapsections( unsigned char *ortbuf, int ortbuflen, unsigned char *scratch, SDL_bool slow )
{
int ort_offs, tapebit, ort_val, cyccount;
int accum, state, thisbit, leaderstart=0, leadercount=0;
int last_offsets[20], i, bitcount, tapbytes=0;
int data_bytes=0, slow0s=0, slow1s=0;
SDL_bool anytapsec = SDL_FALSE;
ort_offs = 4;
tapebit = ortbuf[ort_offs++];
cyccount = 0;
state = TAPSEC_STATE_SEARCH;
accum = 0;
bitcount = 0;
for (i=0; i<20; i++)
last_offsets[i] = 5;
// Loop through the ORT data
while (ort_offs < ortbuflen)
{
// Shuffle the offset array
for (i=0; i<19; i++)
last_offsets[i] = last_offsets[i+1];
last_offsets[19] = ort_offs;
ort_val = next_ort_value(ortbuf, &ort_offs, ortbuflen, &anytapsec);
// Invalid ORT data?
if (ort_val < 1) return ortbuflen;
// Count these cycles
cyccount += ort_val;
// Invert the tape input
tapebit ^= 1;
// Rising edge?
if (tapebit)
{
// smaller than 100? filter it out
if (cyccount < 100)
continue;
// Convert to 1/0
thisbit = TIME_TO_BIT(cyccount);
// Seen a value that is outside the bounds of Oric standard encoding?
if (thisbit == -1)
{
// if ((state != TAPSEC_STATE_SEARCH) || (slow0s>0) || (slow1s>0))
// {
// if (cyccount < TAPE_DECODE_1_MIN) printf("%d is too fast\n", cyccount);
// if (cyccount > TAPE_DECODE_0_MAX) printf("%d is too slow\n", cyccount);
// fflush(stdout);
// }
state = TAPSEC_STATE_SEARCH;
cyccount = 0;
accum = 0;
slow1s = 0;
slow0s = 0;
}
else
{
// Slow mode?
if (slow)
{
if (thisbit==1)
{
slow1s++;
slow0s=0;
if (slow1s != 2)
{
if (slow1s >= 8)
slow1s = 0;
cyccount=0;
continue;
}
}
else
{
slow0s++;
slow1s=0;
if (slow0s != 2)
{
if (slow0s >= 4)
slow0s = 0;
cyccount=0;
continue;
}
}
}
// Accumulate this bit
accum = (accum>>1) | (thisbit<<10);
// What are we doing?
switch (state)
{
case TAPSEC_STATE_SEARCH:
// Look for 0x058 (0x16 encoded with start bit and parity)
if ((accum&0x7fe) == 0x58)
{
// printf("Seen 0x16 @ %d\n", ort_offs);
// fflush(stdout);
leaderstart = last_offsets[8];
leadercount = 1;
state = TAPSEC_STATE_LEADER;
bitcount = 0;
}
else if (validbyte(accum))
{
// printf("Saw %02X @ %d\n", (accum>>2)&0xff, ort_offs);
// fflush(stdout);
}
break;
case TAPSEC_STATE_LEADER:
bitcount = (bitcount+1)%14;
if ((bitcount==13)&&validbyte(accum)) bitcount = 0;
if (!bitcount)
{
// printf("Byte @ %d is %03x\n", ort_offs, accum&0x7fe);
// fflush(stdout);
if ((accum&0x7fe) != 0x58)
{
// printf("Which is NOT 0x58 :-(\n");
state = TAPSEC_STATE_SEARCH;
cyccount = 0;
break;
}
leadercount++;
if (leadercount==4)
{
state = TAPSEC_STATE_FINDHEADER;
break;
}
}
break;
case TAPSEC_STATE_FINDHEADER:
bitcount = (bitcount+1)%14;
if ((bitcount==13)&&validbyte(accum)) bitcount = 0;
if (!bitcount)
{
// printf("Byte @ %d is %03x\n", ort_offs, accum&0x7fe);
// fflush(stdout);
if ((accum&0x7fe) == 0x490) // 0x24 fully encoded
{
// printf("At the header...\n");
// fflush(stdout);
scratch[0] = 0x16;
scratch[1] = 0x16;
scratch[2] = 0x16;
scratch[3] = 0x24;
tapbytes = 4;
state = TAPSEC_STATE_HEADER;
}
}
break;
case TAPSEC_STATE_HEADER:
bitcount = (bitcount+1)%14;
if ((bitcount==13)&&validbyte(accum)) bitcount = 0;
if (!bitcount)
{
// printf("Byte @ %d is %03x (%02X)\n", ort_offs, accum&0x7fe, (accum>>2)&0xff);
// fflush(stdout);
scratch[tapbytes++] = (accum>>2)&0xff;
if (tapbytes == 13)
{
int load_start, load_end;
load_start = (scratch[10]<<8)|scratch[11];
load_end = (scratch[ 8]<<8)|scratch[ 9];
// printf("Load start = %04X, Load end = %04X\n", load_start, load_end);
// fflush(stdout);
if (load_end <= load_start)
{
state = TAPSEC_STATE_SEARCH;
cyccount = 0;
break;
}
data_bytes = (load_end-load_start)+1;
state = TAPSEC_STATE_FILENAME;
// printf("Lets try and load %d bytes!\n", data_bytes);
// fflush(stdout);
}
}
break;
case TAPSEC_STATE_FILENAME:
bitcount = (bitcount+1)%14;
if ((bitcount==13)&&validbyte(accum)) bitcount = 0;
if (!bitcount)
{
// printf("Filename byte = %03x (%02x) %c\n", accum&0x7fe, (accum>>2)&0xff, (accum>>2)&0xff);
// fflush(stdout);
scratch[tapbytes++] = (accum>>2)&0xff;
if (scratch[tapbytes-1] == 0)
{
state = TAPSEC_STATE_FINDDATA;
}
}
break;
case TAPSEC_STATE_FINDDATA:
// Look for a valid byte (start bits, valid parity)
if (validbyte(accum))
{
// printf("Data starts @ %d\n", ort_offs);
// fflush(stdout);
scratch[tapbytes++] = (accum>>2)&0xff;
data_bytes--;
bitcount=0;
state = TAPSEC_STATE_DATA;
}
break;
case TAPSEC_STATE_DATA:
bitcount = (bitcount+1)%14;
if ((bitcount==13)&&validbyte(accum)) bitcount = 0;
if (!bitcount)
{
scratch[tapbytes++] = (accum>>2)&0xff;
data_bytes--;
// printf("%d to go..\n", data_bytes);
// fflush(stdout);
// ??
if (data_bytes < 0)
{
state = TAPSEC_STATE_SEARCH;
break;
}
if (data_bytes > 0)
break;
if ((!anytapsec) && (leaderstart < 512))
leaderstart = 5;
// Got a whole standard oric tap section!
ortbuf[leaderstart++] = 0xff;
ortbuf[leaderstart++] = (tapbytes>>8)&0xff;
ortbuf[leaderstart++] = (tapbytes&0xff);
memcpy(&ortbuf[leaderstart], scratch, tapbytes);
leaderstart += tapbytes;
memmove(&ortbuf[leaderstart], &ortbuf[ort_offs], ortbuflen-ort_offs);
ortbuflen -= (ort_offs-leaderstart);
ort_offs = leaderstart;
state = TAPSEC_STATE_SEARCH;
accum = 0;
anytapsec = SDL_TRUE;
// printf("Back to scanning...\n");
// fflush(stdout);
}
break;
}
}
cyccount = 0;
}
}
return ortbuflen;
}
// This is used by the wav loader routine. It gets a sample from a wav
// file (either 8 or 16bit), and returns it as a signed value. No scaling
// is performed, so it returns -128 to 127 for 8bit wavs, and -32768 to
// 32767 for 16bit wavs.
static inline signed short getsmp(unsigned char *buf, SDL_bool is8bit)
{
return is8bit ? ((short)*buf)-128 : (short)((buf[1]<<8)|buf[0]);
}
// This converts a WAV file to Oricutron's ORT format. It should cope
// with any PCM wav file (stereo, mono, 8bit, 16bit...). It also adjusts
// any DC offset in the recording. It also calls "tapsections" to convert
// any standard oric tape format waveforms it finds into non-raw "tap"
// style sections to enable turbo loading of those parts.
SDL_bool wav_convert( struct machine *oric )
{
// Chunk pointers
unsigned char *p=oric->tapebuf, *data=NULL, *ortbuf=NULL;
unsigned int i, j, k, l, chunklen, bps=0, freq=0, smpdelta=0, datalen=0, ortlen;
signed int smaxl, sminl, smaxr, sminr, dcoffs=0, dcoffsav;
signed int *lastsmps = NULL;
signed short smp=0;
// Cycles per sample
double cps, count, pcount;
SDL_bool stereo = SDL_FALSE, fmtseen = SDL_FALSE, useright = SDL_FALSE;
// Basic validation
i = 12;
while ((!fmtseen) || (!data))
{
// Run out of data?
if (i >= (oric->tapelen-8)) return SDL_FALSE;
// Length of this chunk
chunklen = (p[i+7]<<24)|(p[i+6]<<16)|(p[i+5]<<8)|p[i+4];
// Sane length?
if ((i+chunklen+8) > oric->tapelen)
return SDL_FALSE;
// Format chunk?
if (memcmp(&p[i], "fmt ", 4) == 0)
{
// PCM?
if ((chunklen != 16) || (((p[i+9]<<8)|p[i+8])!=1))
return SDL_FALSE;
// Channels
switch ((p[i+11]<<8)|(p[i+10]))
{
case 1: stereo = SDL_FALSE; break;
case 2: stereo = SDL_TRUE; break;
default: return SDL_FALSE; break;
}
// Frequency
freq = (p[i+15]<<24)|(p[i+14]<<16)|(p[i+13]<<8)|p[i+12];
if( !freq ) return SDL_FALSE;
// Sample delta
smpdelta = (p[i+21]<<16)|p[i+20];
// Bits per sample
bps = (p[i+23]<<16)|p[i+22];
if ((bps!=8)&&(bps!=16)) return SDL_FALSE;
// Bytes per sample
bps /= 8;
fmtseen = SDL_TRUE;
}
else if (memcmp(&p[i], "data", 4) == 0)
{
data = &p[i+8];
datalen = chunklen;
}
// Skip chunk
i += chunklen+8;
}
// printf("wavsize = %d\n", datalen);
smaxl = -70000;
sminl = 70000;
smaxr = -70000;
sminr = 70000;
for (i=0; i<datalen; i+=smpdelta)
{
smp = getsmp(&data[i], bps==1);
if (smp < sminl) sminl = smp;
if (smp > smaxl) smaxl = smp;
if (stereo)
{
smp = getsmp(&data[i+bps], bps==1);
if (smp < sminr) sminr = smp;
if (smp > smaxr) smaxr = smp;
}
}
// Use the loudest channel
if ((stereo) && ((smaxr-sminr)>(smaxl-sminl)))
{
useright = SDL_TRUE;
}
else
{
useright = SDL_FALSE;
}
dcoffsav = freq / 700;
if (dcoffsav)
{
lastsmps = malloc(dcoffsav * sizeof(int));
if (lastsmps) memset(lastsmps, 0, dcoffsav * sizeof(int));
}
dcoffsav--;
// Now convert to a 1/0 squarewave
j = 0;
for (i=0; i<datalen; i+=smpdelta)
{
if (useright)
smp = getsmp(&data[i+bps], bps==1);
else
smp = getsmp(&data[i], bps==1);
if (lastsmps)
{
smaxl = smp;
sminl = smp;
for (k=0; k<dcoffsav; k++)
{
lastsmps[k] = lastsmps[k+1];
if (lastsmps[k] < sminl) sminl = lastsmps[k];
if (lastsmps[k] > smaxl) smaxl = lastsmps[k];
}
dcoffs = ((smaxl-sminl)/2)+sminl;
}
if (j >= oric->tapelen)
{
// printf("Oh dear\n");
// fflush(stdout);
break;
}
oric->tapebuf[j++] = (smp>dcoffs) ? 1 : 0;
}
// printf("ortsize = %d\n", j);
// fflush(stdout);
// Calculate cycles per sample
cps = 500000 / ((double)freq); // .ORT is 500khz
// Calculate the length of the .ORT data
ortlen = 5; // header + initial state
i = oric->tapebuf[0];
count = 0.0f;
pcount = 0.0f;
for (k=1; k<j; k++)
{
if (oric->tapebuf[k] != i)
{
i = oric->tapebuf[k];
if (((int)count) < 1)
{
// Just a spike?
if (ortlen>5) ortlen--;
count += pcount;
}
else if (((int)count) < 0xfc)
{
ortlen++;
pcount = count;
count = 0.0f;
}
else if (((int)count) < 0x100)
{
ortlen+=2;
pcount = count;
count = 0.0f;
}
else
{
ortlen+=3;
pcount = count;
count = 0.0f;
}
}
count+=cps;
}
free( lastsmps );
// Allocate a buffer for the converted data
ortbuf = malloc(ortlen);
if (!ortbuf) return SDL_FALSE;
// Write the header
memcpy(ortbuf, "ORT\0", 4);
// Write the ORT data
i = oric->tapebuf[0];
ortbuf[4] = i;
count = 0.0f;
pcount = 0.0f;
l = 5;
for (k=1; k<j; k++)
{
if (oric->tapebuf[k] != i)
{
i = oric->tapebuf[k];
if (((int)count) < 1)
{
// Just a spike. Cancel the last swap.
if (l==5)
ortbuf[4] = i;
else
l--;
count += pcount;
}
else if (((int)count) < 0xfc)
{
ortbuf[l++] = count;
pcount = count;
count = 0.0f;
}
else if (((int)count) < 0x100)
{
ortbuf[l++] = 0xfc;
ortbuf[l++] = count;
pcount = count;
count = 0.0f;
}
else
{
ortbuf[l++] = 0xfd;
ortbuf[l++] = (((int)count)>>8)&0xff;
ortbuf[l++] = ((int)count)&0xff;
pcount = count;
count = 0.0f;
}
}
count+=cps;
}
// Look for any standard oric encoded parts to convert
// to non-raw "tap" sections
ortlen = tapsections(ortbuf, ortlen, oric->tapebuf, SDL_FALSE);
ortlen = tapsections(ortbuf, ortlen, oric->tapebuf, SDL_TRUE);
// Substitute the ORT data for the original WAV data
free(oric->tapebuf);
oric->tapebuf = ortbuf;
oric->tapelen = ortlen;
return SDL_TRUE;
}
// Insert a new tape image
SDL_bool tape_load_tap( struct machine *oric, char *fname )
{
FILE *f;
// First make sure the image file exists
f = fopen( fname, "rb" );
if( !f ) return SDL_FALSE;
// Eject any old image
tape_eject( oric );
// Get the image size
fseek( f, 0, SEEK_END );
oric->tapelen = (int)ftell( f );
fseek( f, 0, SEEK_SET );
if( oric->tapelen <= 4 ) // Even worth loading it?
{
fclose( f );
return SDL_FALSE;
}
// Allocate memory for the tape image and read it in