forked from sadken/TZXDuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TZXProcessing.ino
1689 lines (1521 loc) · 51.1 KB
/
TZXProcessing.ino
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
void clearBuffer()
{
for(int i=0;i<=buffsize;i++)
{
wbuffer[i][0]=0;
wbuffer[i][1]=0;
}
}
word TickToUs(word ticks) {
return (word) ((((float) ticks)/3.5)+0.5);
}
void checkForEXT () {
if(checkForTap()) { //Check for Tap File. As these have no header we can skip straight to playing data
currentTask=PROCESSID;
if((ReadByte(0))==1 && outByte == 0x16) {
currentID=ORIC;
}
else
{
currentID=TAP;
}
//printtextF(PSTR("TAP Playing"),0);
}
else if(checkForP()) { //Check for P File. As these have no header we can skip straight to playing data
currentTask=PROCESSID;
currentID=ZXP;
//printtextF(PSTR("ZX81 P Playing"),0);
}
else if(checkForO()) { //Check for O File. As these have no header we can skip straight to playing data
currentTask=PROCESSID;
currentID=ZXO;
//printtextF(PSTR("ZX80 O Playing"),0);
}
else if(checkForAY()) { //Check for AY File. As these have no TAP header we must create it and send AY DATA Block after
currentTask=GETAYHEADER;
currentID=AYO;
AYPASS = 0; // Reset AY PASS flags
hdrptr = HDRSTART; // Start reading from position 1 -> 0x13 [0x00]
//printtextF(PSTR("AY Playing"),0);
}
else if(checkForUEF()) { //Check for UEF File. As these have no TAP header we must create it and send AY DATA Block after
currentTask=GETUEFHEADER;
currentID=UEF;
//Serial.println(F("UEF playing"));
//printtext("AY Playing",0);
}
}
void TZXPlay() {
Timer.stop(); //Stop timer interrupt
// on entry, fileIndex is already pointing to the file entry you want to play
// and fileName has already been set accordingly
entry.close();
entry.open(&dir, fileIndex, O_RDONLY);
clearBuffer();
isStopped=false;
pinState=LOW; //Always Start on a LOW output for simplicity
LowWrite();
currpct=-1;
newpct=0;
lcdsegs=0;
count=255; //End of file buffer flush
EndOfFile=false;
currentTask = GETFILEHEADER; //First task: search for header
bytesRead=0; //start of file
checkForEXT(); // this might change the first task, based on the type of file
bytesRead=0; //reset to start of file (because checkForExt can use ReadByte/etc)
currentBlockTask = READPARAM; //First block task is to read in parameters
Timer.setPeriod(1000); //set 1ms wait at start of a file.
}
static bool checkExt(const char * const PROGMEM ext) {
return (strstr_P(strlwr(fileName + (fileNameLen-4)), ext));
}
static inline bool checkForTap() {
//Check for TAP file extensions as these have no header
return checkExt(PSTR(".tap"));
}
static inline bool checkForP() {
//Check for .P file extensions as these have no header
return checkExt(PSTR(".p"));
}
static inline bool checkForO() {
//Check for .O file extensions as these have no header
return checkExt(PSTR(".o"));
}
static inline bool checkForAY() {
//Check for AY file extensions as these have no header
return checkExt(PSTR(".ay"));
}
static inline bool checkForUEF() {
//Serial.println(F("checkForUEF"));
return checkExt(PSTR(".uef"));
}
void TZXStop() {
Timer.stop(); //Stop timer
isStopped=true;
entry.close(); //Close file
// DEBUGGING Stuff
//lcd.setCursor(0,1);
//lcd.print(blkchksum,HEX); lcd.print("ck "); lcd.print(bytesRead); lcd.print(" "); lcd.print(ayblklen);
bytesRead=0; // reset read bytes PlayBytes
blkchksum = 0; // reset block chksum byte for AY loading routine
AYPASS = 0; // reset AY flag
ID15switch = false; // ID15switch
}
void TZXPause() {
isStopped=pauseOn;
}
void TZXLoop() {
noInterrupts(); //Pause interrupts to prevent var reads and copy values out
copybuff = morebuff;
morebuff = false;
isStopped = pauseOn;
interrupts();
if(copybuff) {
btemppos=0; //Buffer has swapped, start from the beginning of the new page
copybuff=false;
}
if(btemppos<=buffsize) // Keep filling until full
{
TZXProcess(); //generate the next period to add to the buffer
if(currentPeriod>0) {
noInterrupts(); //Pause interrupts while we add a period to the buffer
wbuffer[btemppos][workingBuffer ^ 1] = currentPeriod; //add period to the buffer
interrupts();
btemppos+=1;
}
} else {
if ((!pauseOn)&& (currpct<100)) lcdTime();
newpct=(100 * bytesRead)/filesize;
if (newpct>currpct) {
Counter2();
currpct = newpct;
}
}
}
void TZXSetup() {
pinMode(outputPin, OUTPUT); //Set output pin
LowWrite(); //Start output LOW
isStopped=true;
pinState=LOW;
Timer.initialize();
}
void TZXProcess() {
byte r = 0;
currentPeriod = 0;
if(currentTask == GETFILEHEADER) {
//grab 7 byte string
ReadTZXHeader();
//set current task to GETID
currentTask = GETID;
}
if(currentTask == GETAYHEADER) {
//grab 8 byte string
ReadAYHeader();
//set current task to PROCESSID
currentTask = PROCESSID;
}
if (currentTask == GETUEFHEADER) {
//grab 12 byte string
ReadUEFHeader();
//set current task to GETCHUNKID
currentTask = GETCHUNKID;
}
if(currentTask == GETCHUNKID) {
if(r=ReadWord(bytesRead)==2) {
chunkID = outWord;
if(r=ReadDword(bytesRead)==4) {
bytesToRead = outLong;
parity = 0;
if (chunkID == ID0104) {
//bytesRead+= 3;
bytesToRead+= -3;
bytesRead+= 1;
//grab 1 byte Parity
if(ReadByte(bytesRead)==1) {
if (outByte == 'O') parity = wibble ? 2 : 1;
else if (outByte == 'E') parity = wibble ? 1 : 2;
else parity = 0 ; // 'N'
}
bytesRead+= 1;
}
} else {
chunkID = IDCHUNKEOF;
}
} else {
chunkID = IDCHUNKEOF;
}
if (!uefTurboMode) {
zeroPulse = UEFZEROPULSE;
onePulse = UEFONEPULSE;
} else {
zeroPulse = UEFTURBOZEROPULSE;
onePulse = UEFTURBOONEPULSE;
}
lastByte=0;
//reset data block values
currentBit=0;
pass=0;
//set current task to PROCESSCHUNKID
currentTask = PROCESSCHUNKID;
currentBlockTask = READPARAM;
UEFPASS = 0;
}
if(currentTask == PROCESSCHUNKID) {
//CHUNKID Processing
switch(chunkID) {
case ID0000:
bytesRead+=bytesToRead;
currentTask = GETCHUNKID;
break;
case ID0100:
//bytesRead+=bytesToRead;
writeUEFData();
break;
case ID0104:
//parity = 1; // ParityOdd i.e complete with value to get Odd number of ones
/* stopBits = */ //stopBitPulses = 1;
writeUEFData();
//bytesRead+=bytesToRead;
break;
case ID0110:
if(currentBlockTask==READPARAM){
if(r=ReadWord(bytesRead)==2) {
if (!uefTurboMode) {
pilotPulses = UEFPILOTPULSES;
pilotLength = UEFPILOTLENGTH;
} else {
// turbo mode
pilotPulses = UEFTURBOPILOTPULSES;
pilotLength = UEFTURBOPILOTLENGTH;
}
}
currentBlockTask = PILOT;
} else {
UEFCarrierToneBlock();
}
//bytesRead+=bytesToRead;
//currentTask = GETCHUNKID;
break;
case ID0111:
if(currentBlockTask==READPARAM){
if(r=ReadWord(bytesRead)==2) {
pilotPulses = UEFPILOTPULSES; // for TURBOBAUD1500 is outWord<<2
pilotLength = UEFPILOTLENGTH;
}
currentBlockTask = PILOT;
UEFPASS+=1;
} else if (UEFPASS == 1){
UEFCarrierToneBlock();
if(pilotPulses==0) {
currentTask = PROCESSCHUNKID;
currentByte = 0xAA;
lastByte = 1;
currentBit = 10;
pass=0;
UEFPASS = 2;
}
} else if (UEFPASS == 2){
parity = 0; // NoParity
writeUEFData();
if (currentBit==0) {
currentTask = PROCESSCHUNKID;
currentBlockTask = READPARAM;
}
} else if (UEFPASS == 3){
UEFCarrierToneBlock();
}
break;
case ID0112:
//if(currentBlockTask==READPARAM){
if(r=ReadWord(bytesRead)==2) {
if (outWord>0) {
//Serial.print(F("delay="));
//Serial.println(outWord,DEC);
temppause = outWord;
currentID = IDPAUSE;
currentPeriod = temppause;
bitSet(currentPeriod, 15);
currentTask = GETCHUNKID;
} else {
currentTask = GETCHUNKID;
}
}
//}
break;
case ID0114:
if(r=ReadWord(bytesRead)==2) {
pilotPulses = UEFPILOTPULSES;
//pilotLength = UEFPILOTLENGTH;
bytesRead-=2;
}
UEFCarrierToneBlock();
bytesRead+=bytesToRead;
currentTask = GETCHUNKID;
break;
case ID0116:
//if(currentBlockTask==READPARAM){
if(r=ReadDword(bytesRead)==4) {
byte * FloatB = (byte *) &outLong;
outWord = (((*(FloatB+2)&0x80) >> 7) | (*(FloatB+3)&0x7f) << 1) + 10;
outWord = *FloatB | (*(FloatB+1))<<8 | ((outWord&1)<<7)<<16 | (outWord>>1)<<24 ;
outFloat = *((float *) &outWord);
outWord= (int) outFloat;
if (outWord>0) {
//Serial.print(F("delay="));
//Serial.println(outWord,DEC);
temppause = outWord;
currentID = IDPAUSE;
currentPeriod = temppause;
bitSet(currentPeriod, 15);
currentTask = GETCHUNKID;
} else {
currentTask = GETCHUNKID;
}
}
//}
break;
case ID0117:
if(r=ReadWord(bytesRead)==2) {
if (outWord == 300) {
passforZero = 8;
passforOne = 16;
currentTask = GETCHUNKID;
} else {
passforZero = 2;
passforOne = 4;
currentTask = GETCHUNKID;
}
}
break;
case IDCHUNKEOF:
if(!count==0) {
//currentPeriod = 32767;
currentPeriod = 10;
bitSet(currentPeriod, 15); //bitSet(currentPeriod, 12);
count += -1;
} else {
bytesRead+=bytesToRead;
stopFile();
return;
}
break;
default:
//Serial.print(F("Skip id "));
//Serial.print(chunkID);
bytesRead+=bytesToRead;
currentTask = GETCHUNKID;
break;
}
}
if(currentTask == GETID) {
//grab 1 byte ID
if(ReadByte(bytesRead)==1) {
currentID = outByte;
} else {
currentID = IDEOF;
}
//reset data block values
currentBit=0;
pass=0;
//set current task to PROCESSID
currentTask = PROCESSID;
currentBlockTask = READPARAM;
}
if(currentTask == PROCESSID) {
//ID Processing
switch(currentID) {
case ID10:
//Process ID10 - Standard Block
switch (currentBlockTask) {
case READPARAM:
if(r=ReadWord(bytesRead)==2) {
pauseLength = outWord;
}
if(r=ReadWord(bytesRead)==2) {
bytesToRead = outWord +1;
}
if(r=ReadByte(bytesRead)==1) {
if(outByte == 0) {
pilotPulses = PILOTNUMBERL;
} else {
pilotPulses = PILOTNUMBERH;
}
bytesRead += -1;
}
pilotLength = PILOTLENGTH;
sync1Length = SYNCFIRST;
sync2Length = SYNCSECOND;
zeroPulse = ZEROPULSE;
onePulse = ONEPULSE;
currentBlockTask = PILOT;
usedBitsInLastByte=8;
break;
default:
StandardBlock();
break;
}
break;
case ID11:
//Process ID11 - Turbo Tape Block
switch (currentBlockTask) {
case READPARAM:
if(r=ReadWord(bytesRead)==2) {
pilotLength = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
sync1Length = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
sync2Length = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
zeroPulse = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
onePulse = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
pilotPulses = outWord;
}
if(r=ReadByte(bytesRead)==1) {
usedBitsInLastByte = outByte;
}
if(r=ReadWord(bytesRead)==2) {
pauseLength = outWord;
}
if(r=ReadLong(bytesRead)==3) {
bytesToRead = outLong +1;
}
currentBlockTask = PILOT;
break;
default:
StandardBlock();
break;
}
break;
case ID12:
//Process ID12 - Pure Tone Block
if(currentBlockTask==READPARAM){
if(r=ReadWord(bytesRead)==2) {
pilotLength = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
pilotPulses = outWord;
//DebugBlock("Pilot Pulses", pilotPulses);
}
currentBlockTask = PILOT;
} else {
PureToneBlock();
}
break;
case ID13:
//Process ID13 - Sequence of Pulses
if(currentBlockTask==READPARAM) {
if(r=ReadByte(bytesRead)==1) {
seqPulses = outByte;
}
currentBlockTask = DATA;
} else {
PulseSequenceBlock();
}
break;
case ID14:
//process ID14 - Pure Data Block
if(currentBlockTask==READPARAM) {
if(r=ReadWord(bytesRead)==2) {
zeroPulse = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
onePulse = TickToUs(outWord);
}
if(r=ReadByte(bytesRead)==1) {
usedBitsInLastByte = outByte;
}
if(r=ReadWord(bytesRead)==2) {
pauseLength = outWord;
}
if(r=ReadLong(bytesRead)==3) {
bytesToRead = outLong+1;
}
currentBlockTask=DATA;
} else {
PureDataBlock();
}
break;
case ID15:
//process ID15 - Direct Recording
if(currentBlockTask==READPARAM) {
if(r=ReadWord(bytesRead)==2) {
//Number of T-states per sample (bit of data) 79 or 158 - 22.6757uS for 44.1KHz
TstatesperSample = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) {
//Pause after this block in milliseconds
pauseLength = outWord;
}
if(r=ReadByte(bytesRead)==1) {
//Used bits in last byte (other bits should be 0)
usedBitsInLastByte = outByte;
}
if(r=ReadLong(bytesRead)==3) {
// Length of samples' data
bytesToRead = outLong+1;
}
currentBlockTask=DATA;
} else {
currentPeriod = TstatesperSample;
bitSet(currentPeriod, 14);
DirectRecording();
}
break;
case ID19:
//Process ID19 - Generalized data block
switch (currentBlockTask) {
case READPARAM:
if(r=ReadDword(bytesRead)==4) {
//bytesToRead = outLong;
}
if(r=ReadWord(bytesRead)==2) {
//Pause after this block in milliseconds
pauseLength = outWord;
}
bytesRead += 86 ; // skip until DataStream filename
//bytesToRead += -88 ; // pauseLength + SYMDEFs
currentBlockTask=DATA;
break;
case DATA:
ZX8081DataBlock();
break;
}
break;
case ID20:
//process ID20 - Pause Block
if(r=ReadWord(bytesRead)==2) {
if(outWord>0) {
temppause = outWord;
currentID = IDPAUSE;
} else {
currentTask = GETID;
}
}
break;
case ID21:
//Process ID21 - Group Start
if(r=ReadByte(bytesRead)==1) {
bytesRead += outByte;
}
currentTask = GETID;
break;
case ID22:
//Process ID22 - Group End
currentTask = GETID;
break;
case ID24:
//Process ID24 - Loop Start
if(r=ReadWord(bytesRead)==2) {
loopCount = outWord;
loopStart = bytesRead;
}
currentTask = GETID;
break;
case ID25:
//Process ID25 - Loop End
loopCount += -1;
if(loopCount!=0) {
bytesRead = loopStart;
}
currentTask = GETID;
break;
case ID2A:
//Skip//
bytesRead+=4;
currentTask = GETID;
break;
case ID2B:
//Skip//
bytesRead+=5;
currentTask = GETID;
break;
case ID30:
//Process ID30 - Text Description
if(r=ReadByte(bytesRead)==1) {
//Show info on screen - removed until bigger screen used
//byte j = outByte;
//for(byte i=0; i<j; i++) {
// if(ReadByte(bytesRead)==1) {
// lcd.print(char(outByte));
// }
//}
bytesRead += outByte;
}
currentTask = GETID;
break;
case ID31:
//Process ID31 - Message block
if(r=ReadByte(bytesRead)==1) {
// dispayTime = outByte;
}
if(r=ReadByte(bytesRead)==1) {
bytesRead += outByte;
}
currentTask = GETID;
break;
case ID32:
//Process ID32 - Archive Info
//Block Skipped until larger screen used
if(ReadWord(bytesRead)==2) {
bytesRead += outWord;
}
currentTask = GETID;
break;
case ID33:
//Process ID32 - Archive Info
//Block Skipped until larger screen used
if(ReadByte(bytesRead)==1) {
bytesRead += (long(outByte) * 3);
}
currentTask = GETID;
break;
case ID35:
//Process ID35 - Custom Info Block
//Block Skipped
bytesRead += 0x10;
if(r=ReadDword(bytesRead)==4) {
bytesRead += outLong;
}
currentTask = GETID;
break;
case ID4B:
//Process ID4B - Kansas City Block (MSX specific implementation only)
switch(currentBlockTask) {
case READPARAM:
if(r=ReadDword(bytesRead)==4) { // Data size to read
bytesToRead = outLong - 12;
}
if(r=ReadWord(bytesRead)==2) { // Pause after block in ms
pauseLength = outWord;
}
if (TSXspeedup == 0){
if(r=ReadWord(bytesRead)==2) { // T-states each pilot pulse
pilotLength = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) { // Number of pilot pulses
pilotPulses = outWord;
}
if(r=ReadWord(bytesRead)==2) { // T-states 0 bit pulse
zeroPulse = TickToUs(outWord);
}
if(r=ReadWord(bytesRead)==2) { // T-states 1 bit pulse
onePulse = TickToUs(outWord);
}
ReadWord(bytesRead);
} else {
//Fixed speedup baudrate, reduced pilot duration
pilotPulses = 10000;
bytesRead += 10;
switch(BAUDRATE){
case 1200:
pilotLength = onePulse = TickToUs(729);
zeroPulse = TickToUs(1458);
break;
case 2400:
pilotLength = onePulse = TickToUs(365);
zeroPulse = TickToUs(730);
break;
case 3600:
pilotLength = onePulse = TickToUs(243);
zeroPulse = TickToUs(486);
break;
case 3760:
pilotLength = onePulse = TickToUs(233);
zeroPulse = TickToUs(466);
break;
}
} //TSX_SPEEDUP
currentBlockTask = PILOT;
break;
case PILOT:
//Start with Pilot Pulses
if (!pilotPulses--) {
currentBlockTask = DATA;
} else {
currentPeriod = pilotLength;
}
break;
case DATA:
//Data playback
writeData4B();
break;
case PAUSE:
//Close block with a pause
temppause = pauseLength;
currentID = IDPAUSE;
break;
}
break;
case TAP:
//Pure Tap file block
switch(currentBlockTask) {
case READPARAM:
pauseLength = PAUSELENGTH;
if(r=ReadWord(bytesRead)==2) {
bytesToRead = outWord+1;
}
if(r=ReadByte(bytesRead)==1) {
if(outByte == 0) {
pilotPulses = PILOTNUMBERL + 1;
} else {
pilotPulses = PILOTNUMBERH + 1;
}
bytesRead += -1;
}
pilotLength = PILOTLENGTH;
sync1Length = SYNCFIRST;
sync2Length = SYNCSECOND;
zeroPulse = ZEROPULSE;
onePulse = ONEPULSE;
currentBlockTask = PILOT;
usedBitsInLastByte=8;
break;
default:
StandardBlock();
break;
}
break;
case ZXP:
switch(currentBlockTask) {
case READPARAM:
pauseLength = PAUSELENGTH*5;
currentChar=0;
currentBlockTask=PILOT;
break;
case PILOT:
ZX81FilenameBlock();
break;
case DATA:
ZX8081DataBlock();
break;
}
break;
case ZXO:
switch(currentBlockTask) {
case READPARAM:
pauseLength = PAUSELENGTH*5;
currentBlockTask=DATA;
break;
case DATA:
ZX8081DataBlock();
break;
}
break;
case AYO: //AY File - Pure AY file block - no header, must emulate it
switch(currentBlockTask) {
case READPARAM:
pauseLength = PAUSELENGTH; // Standard 1 sec pause
// here we must generate the TAP header which in pure AY files is missing.
// This was done with a DOS utility called FILE2TAP which does not work under recent 32bit OSs (only using DOSBOX).
// TAPed AY files begin with a standard 0x13 0x00 header (0x13 bytes to follow) and contain the
// name of the AY file (max 10 bytes) which we will display as "ZXAYFile " followed by the
// length of the block (word), checksum plus 0xFF to indicate next block is DATA.
// 13 00[00 03(5A 58 41 59 46 49 4C 45 2E 49)1A 0B 00 C0 00 80]21<->[1C 0B FF<AYFILE>CHK]
//if(hdrptr==1) {
//bytesToRead = 0x13-2; // 0x13 0x0 - TAP Header minus 2 (FLAG and CHKSUM bytes) 17 bytes total
//}
if(hdrptr==HDRSTART) {
//if (!AYPASS) {
pilotPulses = PILOTNUMBERL + 1;
}
else {
pilotPulses = PILOTNUMBERH + 1;
}
pilotLength = PILOTLENGTH;
sync1Length = SYNCFIRST;
sync2Length = SYNCSECOND;
zeroPulse = ZEROPULSE;
onePulse = ONEPULSE;
currentBlockTask = PILOT; // now send pilot, SYNC1, SYNC2 and DATA (writeheader() from String Vector on 1st pass then writeData() on second)
if (hdrptr==HDRSTART) AYPASS = 1; // Set AY TAP data read flag only if first run
if (AYPASS == 2) { // If we have already sent TAP header
blkchksum = 0;
bytesRead = 0;
bytesToRead = ayblklen+2; // set length of file to be read plus data byte and CHKSUM (and 2 block LEN bytes)
AYPASS = 5; // reset flag to read from file and output header 0xFF byte and end chksum
}
usedBitsInLastByte=8;
break;
default:
StandardBlock();
break;
}
break;
case ORIC:
//ReadByte(bytesRead);
//OricByteWrite();
switch(currentBlockTask) {
case READPARAM: // currentBit = 0 y count = 255
case SYNC1:
if (currentBit >0) OricBitWrite();
else {
//if (count >0) {
ReadByte(bytesRead);currentByte=outByte;currentBit = 11; bitChecksum = 0;lastByte=0;
if (currentByte==0x16) count--;
else {currentBit = 0; currentBlockTask=SYNC2;} //0x24
//}
//else currentBlockTask=SYNC2;
}
break;
case SYNC2:
if(currentBit >0) OricBitWrite();
else {
if (count >0) {currentByte=0x16; currentBit = 11; bitChecksum = 0;lastByte=0; count--;}
else {count=1; currentBlockTask=SYNCLAST;} //0x24
}
break;
case SYNCLAST:
if(currentBit >0) OricBitWrite();
else {
if (count >0) {currentByte=0x24; currentBit = 11; bitChecksum = 0;lastByte=0; count--;}
else {count=9;lastByte=0;currentBlockTask=HEADER;}
}
break;
case HEADER:
if(currentBit >0) OricBitWrite();
else {
if (count >0) {
ReadByte(bytesRead);currentByte=outByte;currentBit = 11; bitChecksum = 0;lastByte=0;
if (count == 5) bytesToRead = 256*outByte;
else if (count == 4) bytesToRead += (outByte +1) ;
else if (count == 3) bytesToRead -= (256 * outByte) ;
else if (count == 2) bytesToRead -= outByte;
count--;
}
else currentBlockTask=NAME;
}
break;
case NAME:
if (currentBit >0) OricBitWrite();
else {
ReadByte(bytesRead);currentByte=outByte;currentBit = 11; bitChecksum = 0;lastByte=0;
if (currentByte==0x00) {count=1;currentBit = 0; currentBlockTask=NAMELAST;}
}
break;
case NAMELAST:
if(currentBit >0) OricBitWrite();
else {
if (count >0) {currentByte=0x00; currentBit = 11; bitChecksum = 0;lastByte=0; count--;}
else {count=100;lastByte=0;currentBlockTask=GAP;}
}
break;
case GAP:
if(count>0) {
currentPeriod = ORICONEPULSE;
count--;
} else {
currentBlockTask=DATA;
}
break;
case DATA:
OricDataBlock();
break;
case PAUSE:
//currentPeriod = 100; // 100ms pause
//bitSet(currentPeriod, 15);
if(!count==0) {
currentPeriod = 32769;
count += -1;
} else {
count= 255;
currentBlockTask=SYNC1;
}
break;
}
break;
case IDPAUSE:
if(temppause>0) {
if(temppause > 1000) {
//Serial.println(temppause, DEC);
currentPeriod = 1000;
temppause -= 1000;
} else {
currentPeriod = temppause;
temppause = 0;
}
bitSet(currentPeriod, 15);
} else {
currentTask = GETID;
if(EndOfFile) currentID=IDEOF;
}
break;
case IDEOF:
//Handle end of file