-
Notifications
You must be signed in to change notification settings - Fork 2
/
tape.c
1636 lines (1343 loc) · 50.8 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
/* tape.c: Routines for handling tape files
Copyright (c) 2001-2018 Philip Kendall, Darren Salt, Fredrick Meunier
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; either version 2 of the License, or
(at your option) any later version.
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.
Author contact information:
E-mail: [email protected]
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "internals.h"
#include "tape_block.h"
/* The tape type itself */
struct libspectrum_tape {
/* All the blocks */
GSList* blocks;
/* The last block */
GSList* last_block;
/* The state of the current block */
libspectrum_tape_block_state state;
};
/*** Constants ***/
/* The timings for the standard ROM loader */
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_PILOT = 2168; /*Pilot*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_SYNC1 = 667; /*Sync 1*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_SYNC2 = 735; /*Sync 2*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_DATA0 = 855; /*Reset*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_DATA1 = 1710; /*Set*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_TAIL = 945; /*Tail*/
/*** Local function prototypes ***/
/* Free the memory used by a block */
static void
block_free( gpointer data, gpointer user_data );
/* Functions to get the next edge */
static libspectrum_error
rom_edge( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
static libspectrum_error
rom_next_bit( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state );
static libspectrum_error
turbo_edge( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state,
libspectrum_dword *tstates,
int *end_of_block, int *flags );
static libspectrum_error
turbo_next_bit( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state );
static libspectrum_error
tone_edge( libspectrum_tape_pure_tone_block *block,
libspectrum_tape_pure_tone_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pulses_edge( libspectrum_tape_pulses_block *block,
libspectrum_tape_pulses_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pure_data_edge( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
static libspectrum_error
raw_data_edge( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags );
static libspectrum_error
jump_blocks( libspectrum_tape *tape, int offset );
static libspectrum_error
rle_pulse_edge( libspectrum_tape_rle_pulse_block *block,
libspectrum_tape_rle_pulse_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pulse_sequence_edge( libspectrum_tape_pulse_sequence_block *block,
libspectrum_tape_pulse_sequence_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags );
libspectrum_error
libspectrum_tape_data_block_next_bit( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state );
static libspectrum_error
data_block_edge( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
/*** Function definitions ****/
/* Allocate a list of blocks */
libspectrum_tape*
libspectrum_tape_alloc( void )
{
libspectrum_tape *tape = libspectrum_new( libspectrum_tape, 1 );
tape->blocks = NULL;
tape->last_block = NULL;
libspectrum_tape_iterator_init( &(tape->state.current_block), tape );
tape->state.loop_block = NULL;
return tape;
}
/* Free the memory used by a list of blocks, but not the object itself */
libspectrum_error
libspectrum_tape_clear( libspectrum_tape *tape )
{
g_slist_foreach( tape->blocks, block_free, NULL );
g_slist_free( tape->blocks );
tape->blocks = NULL;
libspectrum_tape_iterator_init( &(tape->state.current_block), tape );
return LIBSPECTRUM_ERROR_NONE;
}
/* Free up a list of blocks */
libspectrum_error
libspectrum_tape_free( libspectrum_tape *tape )
{
libspectrum_error error;
error = libspectrum_tape_clear( tape );
if( error ) return error;
libspectrum_free( tape );
return LIBSPECTRUM_ERROR_NONE;
}
/* Free the memory used by one block */
static void
block_free( gpointer data, gpointer user_data GCC_UNUSED )
{
libspectrum_tape_block_free( data );
}
/* Read in a tape file, optionally guessing what sort of file it is */
libspectrum_error
libspectrum_tape_read( libspectrum_tape *tape, const libspectrum_byte *buffer,
size_t length, libspectrum_id_t type,
const char *filename )
{
libspectrum_id_t raw_type;
libspectrum_class_t class;
libspectrum_byte *new_buffer;
libspectrum_error error;
/* If we don't know what sort of file this is, make a best guess */
if( type == LIBSPECTRUM_ID_UNKNOWN ) {
error = libspectrum_identify_file( &type, filename, buffer, length );
if( error ) return error;
/* If we still can't identify it, give up */
if( type == LIBSPECTRUM_ID_UNKNOWN ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_tape_read: couldn't identify file"
);
return LIBSPECTRUM_ERROR_UNKNOWN;
}
}
/* Find out if this file needs decompression */
new_buffer = NULL;
error = libspectrum_identify_file_raw( &raw_type, filename, buffer, length );
if( error ) return error;
error = libspectrum_identify_class( &class, raw_type );
if( error ) return error;
if( class == LIBSPECTRUM_CLASS_COMPRESSED ) {
size_t new_length;
error = libspectrum_uncompress_file( &new_buffer, &new_length, NULL,
raw_type, buffer, length, NULL );
if( error ) return error;
buffer = new_buffer; length = new_length;
}
switch( type ) {
case LIBSPECTRUM_ID_TAPE_TAP:
case LIBSPECTRUM_ID_TAPE_SPC:
case LIBSPECTRUM_ID_TAPE_STA:
case LIBSPECTRUM_ID_TAPE_LTP:
error = internal_tap_read( tape, buffer, length, type ); break;
case LIBSPECTRUM_ID_TAPE_TZX:
error = internal_tzx_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_WARAJEVO:
error = internal_warajevo_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_Z80EM:
error = libspectrum_z80em_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_CSW:
error = libspectrum_csw_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_WAV:
#ifdef HAVE_LIB_AUDIOFILE
error = libspectrum_wav_read( tape, filename ); break;
#else /* #ifdef HAVE_LIB_AUDIOFILE */
error = LIBSPECTRUM_ERROR_LOGIC;
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_read: format not supported without libaudiofile"
);
break;
#endif /* #ifdef HAVE_LIB_AUDIOFILE */
case LIBSPECTRUM_ID_TAPE_PZX:
error = internal_pzx_read( tape, buffer, length ); break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_tape_read: not a tape file" );
libspectrum_free( new_buffer );
return LIBSPECTRUM_ERROR_CORRUPT;
}
libspectrum_free( new_buffer );
return error;
}
libspectrum_error
libspectrum_tape_write( libspectrum_byte **buffer, size_t *length,
libspectrum_tape *tape, libspectrum_id_t type )
{
libspectrum_byte *ptr = *buffer;
libspectrum_buffer *new_buffer;
libspectrum_class_t class;
libspectrum_error error;
error = libspectrum_identify_class( &class, type );
if( error ) return error;
if( class != LIBSPECTRUM_CLASS_TAPE ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_INVALID,
"libspectrum_tape_write: not a tape format" );
return LIBSPECTRUM_ERROR_INVALID;
}
/* Allow for uninitialised buffer on entry */
if( !*length ) *buffer = NULL;
new_buffer = libspectrum_buffer_alloc();
switch( type ) {
case LIBSPECTRUM_ID_TAPE_TAP:
case LIBSPECTRUM_ID_TAPE_SPC:
case LIBSPECTRUM_ID_TAPE_STA:
case LIBSPECTRUM_ID_TAPE_LTP:
error = internal_tap_write( new_buffer, tape, type );
break;
case LIBSPECTRUM_ID_TAPE_TZX:
error = internal_tzx_write( new_buffer, tape );
break;
case LIBSPECTRUM_ID_TAPE_CSW:
error = libspectrum_csw_write( new_buffer, tape );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_tape_write: format not supported" );
error = LIBSPECTRUM_ERROR_UNKNOWN;
break;
}
libspectrum_buffer_append( buffer, length, &ptr, new_buffer );
libspectrum_buffer_free( new_buffer );
return error;
}
/* Does this tape structure actually contain a tape? */
int
libspectrum_tape_present( const libspectrum_tape *tape )
{
return tape->blocks != NULL;
}
/* Some flags which may be set after calling libspectrum_tape_get_next_edge */
const int LIBSPECTRUM_TAPE_FLAGS_BLOCK = 1 << 0; /* End of block */
const int LIBSPECTRUM_TAPE_FLAGS_STOP = 1 << 1; /* Stop tape */
const int LIBSPECTRUM_TAPE_FLAGS_STOP48 = 1 << 2; /* Stop tape if in
48K mode */
const int LIBSPECTRUM_TAPE_FLAGS_NO_EDGE = 1 << 3; /* Not an edge really */
const int LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW = 1 << 4; /* Set level low */
const int LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH = 1 << 5; /* Set level high */
const int LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT = 1 << 6;/* Short edge; used for
loader acceleration */
const int LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG = 1 << 7; /* Long edge; used for
loader acceleration */
const int LIBSPECTRUM_TAPE_FLAGS_TAPE = 1 << 8; /* End of tape */
libspectrum_error
libspectrum_tape_get_next_edge_internal( libspectrum_dword *tstates,
int *flags,
libspectrum_tape *tape,
libspectrum_tape_block_state *it )
{
int error;
libspectrum_tape_block *block =
libspectrum_tape_iterator_current( it->current_block );
/* Has this edge ended the block? */
int end_of_block = 0;
/* After getting a new block, do we want to advance to the next one? */
int no_advance = 0;
/* Assume no special flags by default */
*flags = 0;
if( block ) {
switch( block->type ) {
case LIBSPECTRUM_TAPE_BLOCK_ROM:
error = rom_edge( &(block->types.rom), &(it->block_state.rom), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_TURBO:
error = turbo_edge( &(block->types.turbo), &(it->block_state.turbo), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_TONE:
error = tone_edge( &(block->types.pure_tone), &(it->block_state.pure_tone),
tstates, &end_of_block );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSES:
error = pulses_edge( &(block->types.pulses), &(it->block_state.pulses),
tstates, &end_of_block );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA:
error = pure_data_edge( &(block->types.pure_data),
&(it->block_state.pure_data), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA:
error = raw_data_edge( &(block->types.raw_data), &(it->block_state.raw_data),
tstates, &end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA:
error = generalised_data_edge( &(block->types.generalised_data),
&(it->block_state.generalised_data),
tstates, &end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PAUSE:
*tstates = block->types.pause.length_tstates; end_of_block = 1;
/* If the pause isn't a "don't care" level then set the appropriate pulse
level */
if( block->types.pause.level != -1 &&
block->types.pause.length_tstates ) {
*flags |= block->types.pause.level ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
}
/* 0 ms pause => stop tape */
if( *tstates == 0 ) { *flags |= LIBSPECTRUM_TAPE_FLAGS_STOP; }
break;
case LIBSPECTRUM_TAPE_BLOCK_JUMP:
error = jump_blocks( tape, block->types.jump.offset );
if( error ) return error;
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
no_advance = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_START:
if( it->current_block->next && block->types.loop_start.count ) {
it->loop_block = it->current_block->next;
it->loop_count = block->types.loop_start.count;
}
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_END:
if( it->loop_block ) {
if( --(it->loop_count) ) {
it->current_block = it->loop_block;
no_advance = 1;
} else {
it->loop_block = NULL;
}
}
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_STOP48:
*tstates = 0;
*flags |= LIBSPECTRUM_TAPE_FLAGS_STOP48;
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_SET_SIGNAL_LEVEL:
*tstates = 0; end_of_block = 1;
/* Inverted as the following block will flip the level before recording
the edge */
*flags |= block->types.set_signal_level.level ?
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW : LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
/* For blocks which contain no Spectrum-readable data, return zero
tstates and set end of block set so we instantly get the next block */
case LIBSPECTRUM_TAPE_BLOCK_GROUP_START:
case LIBSPECTRUM_TAPE_BLOCK_GROUP_END:
case LIBSPECTRUM_TAPE_BLOCK_SELECT:
case LIBSPECTRUM_TAPE_BLOCK_COMMENT:
case LIBSPECTRUM_TAPE_BLOCK_MESSAGE:
case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO:
case LIBSPECTRUM_TAPE_BLOCK_HARDWARE:
case LIBSPECTRUM_TAPE_BLOCK_CUSTOM:
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE:
error = rle_pulse_edge( &(block->types.rle_pulse),
&(it->block_state.rle_pulse), tstates, &end_of_block);
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSE_SEQUENCE:
error = pulse_sequence_edge( &(block->types.pulse_sequence),
&(it->block_state.pulse_sequence), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_DATA_BLOCK:
error = data_block_edge( &(block->types.data_block),
&(it->block_state.data_block), tstates,
&end_of_block, flags );
if( error ) return error;
break;
default:
*tstates = 0;
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_get_next_edge: unknown block type 0x%02x",
block->type
);
return LIBSPECTRUM_ERROR_LOGIC;
}
} else {
*tstates = 0;
end_of_block = 1;
}
/* If that ended the block, move onto the next block */
if( end_of_block ) {
*flags |= LIBSPECTRUM_TAPE_FLAGS_BLOCK;
/* Advance to the next block, unless we've been told not to */
if( !no_advance ) {
libspectrum_tape_iterator_next( &(it->current_block) );
/* If we've just hit the end of the tape, stop the tape (and
then `rewind' to the start) */
if( libspectrum_tape_iterator_current( it->current_block ) == NULL ) {
*flags |= LIBSPECTRUM_TAPE_FLAGS_STOP;
*flags |= LIBSPECTRUM_TAPE_FLAGS_TAPE;
/* Need to have an edge at the end of the tape to terminate the last
pulse so clear the NO_EDGE flag if it has been set */
*flags &= ~LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
libspectrum_tape_iterator_init( &(it->current_block), tape );
}
}
/* Initialise the new block */
error = libspectrum_tape_block_init(
libspectrum_tape_iterator_current( it->current_block ),
it );
if( error ) return error;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* The main function: called with a tape object and returns the number of
t-states until the next edge, and a marker if this was the last edge
on the tape */
libspectrum_error
libspectrum_tape_get_next_edge( libspectrum_dword *tstates, int *flags,
libspectrum_tape *tape )
{
return libspectrum_tape_get_next_edge_internal( tstates, flags, tape,
&(tape->state) );
}
/* TZX pauses should have no edge if there is no duration, from the spec:
A 'Pause' block of zero duration is completely ignored, so the 'current pulse
level' will NOT change in this case. This also applies to 'Data' blocks that
have some pause duration included in them. */
static void
do_tail_pause( libspectrum_dword *tstates,
int *end_of_block, int *flags )
{
*end_of_block = 1;
if( *tstates == 0 ) {
/* The tail pause is optional - if there is no tail, there is no edge */
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
}
}
static libspectrum_error
rom_edge( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state,
libspectrum_dword *tstates,
int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT:
/* The next edge occurs in one pilot edge timing */
*tstates = LIBSPECTRUM_TAPE_TIMING_PILOT;
/* If that was the last pilot edge, change state */
if( --(state->edge_count) == 0 )
state->state = LIBSPECTRUM_TAPE_STATE_SYNC1;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC1:
/* The first short sync pulse */
*tstates = LIBSPECTRUM_TAPE_TIMING_SYNC1;
/* Followed by the second sync pulse */
state->state = LIBSPECTRUM_TAPE_STATE_SYNC2;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC2:
/* The second short sync pulse */
*tstates = LIBSPECTRUM_TAPE_TIMING_SYNC2;
/* Followed by the first bit of data */
error = rom_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == LIBSPECTRUM_TAPE_TIMING_DATA0 ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == LIBSPECTRUM_TAPE_TIMING_DATA0 ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = rom_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"rom_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rom_next_bit( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
state->bits_through_byte = 0;
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? LIBSPECTRUM_TAPE_TIMING_DATA1
: LIBSPECTRUM_TAPE_TIMING_DATA0 );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
turbo_edge( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT:
/* Check we actually have some edges */
if( state->edge_count-- != 0 ) {
*tstates = block->pilot_length;
break;
}
/* Fall through */
case LIBSPECTRUM_TAPE_STATE_SYNC1:
/* The first short sync pulse */
*tstates = block->sync1_length;
/* Followed by the second sync pulse */
state->state = LIBSPECTRUM_TAPE_STATE_SYNC2;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC2:
/* The second short sync pulse */
*tstates = block->sync2_length;
/* Followed by the first bit of data */
error = turbo_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = turbo_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"turbo_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
turbo_next_bit( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
/* If we're looking at the last byte, take account of the fact it
may have less than 8 bits in it */
if( state->bytes_through_block == block->length-1 ) {
state->bits_through_byte = 8 - block->bits_in_last_byte;
} else {
state->bits_through_byte = 0;
}
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? block->bit1_length : block->bit0_length );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
tone_edge( libspectrum_tape_pure_tone_block *block,
libspectrum_tape_pure_tone_block_state *state,
libspectrum_dword *tstates, int *end_of_block )
{
/* The next edge occurs in one pilot edge timing */
*tstates = block->length;
/* If that was the last edge, the block is finished */
if( --(state->edge_count) == 0 ) (*end_of_block) = 1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
pulses_edge( libspectrum_tape_pulses_block *block,
libspectrum_tape_pulses_block_state *state,
libspectrum_dword *tstates, int *end_of_block )
{
/* Get the length of this edge */
*tstates = block->lengths[ state->edge_count ];
/* Was that the last edge? */
if( ++(state->edge_count) == block->count ) (*end_of_block) = 1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
pure_data_edge( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = libspectrum_tape_pure_data_next_bit( block, state );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"pure_data_edge: unknown state %d",
state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_tape_pure_data_next_bit( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
/* If we're looking at the last byte, take account of the fact it
may have less than 8 bits in it */
if( state->bytes_through_block == block->length-1 ) {
state->bits_through_byte = 8 - block->bits_in_last_byte;
} else {
state->bits_through_byte = 0;
}
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? block->bit1_length : block->bit0_length );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
raw_data_edge( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags )
{
switch (state->state) {
case LIBSPECTRUM_TAPE_STATE_DATA1:
*tstates = state->bit_tstates;
libspectrum_tape_raw_data_next_bit( block, state );
/* Backwards as last bit is the state of the next bit as it has already been
updated*/
*flags |= state->last_bit ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"raw_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
void
libspectrum_tape_raw_data_next_bit( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state )
{
int length = 0;
if( state->bytes_through_block == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
state->last_bit ^= 0x80;
return;
}
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
/* Step through the data until we find an edge */
do {
size_t bits_in_byte = (state->bytes_through_block == block->length - 1) ?
block->bits_in_last_byte : 8;
length++;
if( ++(state->bits_through_byte) == bits_in_byte ) {
state->bits_through_byte = 0;
if( ++(state->bytes_through_block) == block->length )
break;
}
} while( ( block->data[state->bytes_through_block] <<
state->bits_through_byte & 0x80 ) != state->last_bit) ;
state->bit_tstates = length * block->bit_length;
state->last_bit ^= 0x80;
}
static libspectrum_byte
get_generalised_data_bit( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state )
{
libspectrum_byte r = state->current_byte & 0x80 ? 1 : 0;
state->current_byte <<= 1;
if( ++state->bits_through_byte == 8 ) {
state->bits_through_byte = 0;
state->bytes_through_stream++;
state->current_byte = block->data[ state->bytes_through_stream ];
}
return r;
}
libspectrum_byte
get_generalised_data_symbol( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state )
{
libspectrum_byte symbol;
size_t i;
for( i = 0, symbol = 0;
i < block->bits_per_data_symbol;
i++ ) {
symbol <<= 1;
symbol |= get_generalised_data_bit( block, state );
}
return symbol;
}
static void
set_tstates_and_flags( libspectrum_tape_generalised_data_symbol *symbol,
libspectrum_byte edge, libspectrum_dword *tstates,
int *flags )
{
*tstates = symbol->lengths[ edge ];
if( !edge ) {
switch( symbol->edge_type ) {
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_EDGE:
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_NO_EDGE:
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_LOW:
*flags |= LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_HIGH:
*flags |= LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
}
}
}
libspectrum_error
generalised_data_edge( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags )
{
libspectrum_tape_generalised_data_symbol_table *table;
libspectrum_tape_generalised_data_symbol *symbol;
size_t current_symbol;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT: