-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMdvDecode.cpp
More file actions
1094 lines (1004 loc) · 40.8 KB
/
Copy pathMdvDecode.cpp
File metadata and controls
1094 lines (1004 loc) · 40.8 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "MdvDecode.h"
#include "Track.h"
__int64 Timestamp(int time, int traceFreq)
{
return (__int64)time * 1000000 / traceFreq;
}
// Read file into vector
inline bool Load(const string& path, vector<BYTE>& result)
{
result.clear();
char buffer[512];
int fileIndex = 1;
while (true)
{
sprintf(buffer, "%s\\logic-1-%d", path.c_str(), fileIndex);
FILE* f = fopen(buffer, "rb");
if (f == NULL)
break;
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
size_t base = result.size();
if (base == 0)
result.reserve((size_t)len * 100);
result.resize(base + len);
fread(&result[base], 1, len, f);
fclose(f);
fileIndex++;
}
return result.size() != 0;
}
// Per-chunk instrumentation: how often does ByteSync see the two tracks drift
// far enough apart that it silently declines to resync (delta >= 40% of the
// bit period)? Set BYTE_SYNC_TOLERANCE, reset once per DecodeBlock, printed
// in verbose mode after each block.
static const float BYTE_SYNC_TOLERANCE = 0.4f;
struct ByteSyncStats
{
int missCount = 0;
float maxRatio = 0.0f; // max observed |delta|/period among misses
int firstMissByteIdx = -1;
int lastMissByteIdx = -1;
};
static ByteSyncStats g_bsStats;
// Byte-window flux picture params (mid-block region-of-interest dump). Set
// once from main() before the decode loop. When both are non-negative and
// this chunk's debugTag is active, DecodeBlock saves a flux+alignment JPG
// per track around block-data byte offset g_fluxByteOffset.
static int g_fluxByteOffset = -1;
static int g_fluxByteWindow = 16;
// If chunkIdx is the -flux-jpg target, format a debug tag into `buf` and
// return it; otherwise return nullptr. Keeps the per-chunk decode loop
// visually uncluttered by the debug wiring.
static const char* MakeFluxJpgTag(int chunkIdx, int fluxJpgChunk, int time,
int traceFreq, char* buf, size_t bufLen)
{
if (chunkIdx != fluxJpgChunk) return nullptr;
snprintf(buf, bufLen, "chunk%d", chunkIdx);
printf("Dumping flux picture for chunk #%d (timestamp %lld uSec)\n",
chunkIdx, (long long)Timestamp(time, traceFreq));
return buf;
}
// Slice the per-track alignmentBuffer around `byteOffset` (block-level, 0-based)
// with `halfWidth` bytes of context on each side, and hand it to DrawErrorNamed
// together with the track's full flux. DrawErrorImpl auto-clips the picture to
// the flux region matching alignment[0], so windowing the alignment is enough.
static void SaveByteFluxPictures(Track& track1, Track& track2, const char* debugTag,
int byteOffset, int halfWidth)
{
// block.data is interleaved (t1, t2, t1, t2, ...); byte P → track P%2, index P/2
int perTrackByte = byteOffset / 2;
int firstByte = perTrackByte - halfWidth;
if (firstByte < 0) firstByte = 0;
int lastByte = perTrackByte + halfWidth + 1;
int firstBit = firstByte * 8;
int lastBit = lastByte * 8;
auto dumpOne = [&](Track& t, int trackNum) {
const vector<int>& a = t.GetAlignmentBuffer();
const vector<int>& v = t.GetBitValueBuffer();
if (a.empty()) return;
int lo = min(firstBit, (int)a.size());
int hi = min(lastBit, (int)a.size());
if (hi - lo < 8) return; // fewer than one byte of ticks → not useful
vector<int> subA(a.begin() + lo, a.begin() + hi);
vector<int> subV;
if ((int)v.size() >= hi)
subV.assign(v.begin() + lo, v.begin() + hi);
char path[256];
snprintf(path, sizeof(path), "flux_%s_byte%d_track%d.jpg",
debugTag, byteOffset, trackNum);
DrawErrorNamedBits(t.GetFlux(), subA, subV, path);
printf("Saved %s (alignment %d bits, ticks %d..%d)\n", path, hi - lo, lo, hi);
// Text dump for cross-checking: labeled bits + tick times + raw flux
// intervals overlapping the window's time range. Lets us compare what
// the decoder concluded vs what the flux numerically contains.
printf(" track%d labels (bit lo..hi, LSB-first): ", trackNum);
for (size_t i = 0; i < subV.size(); i++)
printf("%d%s", subV[i], ((i + 1) % 8 == 0) ? " " : "");
printf("\n");
printf(" track%d ticks (sample times):", trackNum);
for (size_t i = 0; i < subA.size(); i++)
{
if (i < 8 || i >= subA.size() - 8)
printf(" %d", subA[i]);
else if (i == 8)
printf(" ...");
}
printf("\n");
int tStart = subA.front();
int tEnd = subA.back();
const vector<int>& flux = t.GetFlux();
printf(" track%d flux intervals in [%d..%d]:", trackNum, tStart, tEnd);
int cum = 0;
int printed = 0;
for (size_t i = 0; i < flux.size() && cum <= tEnd; i++)
{
cum += flux[i];
if (cum >= tStart && cum <= tEnd)
{
printf(" %d", flux[i]);
if (++printed >= 80) { printf(" ..."); break; }
}
}
printf("\n");
};
dumpOne(track1, 1);
dumpOne(track2, 2);
}
// Returns true if ByteSync applied the correction; false if delta was too big
// (miss, tracked in g_bsStats) or if we had no midTime yet.
bool ByteSync(Track& track1, Track& track2)
{
float period = (track1.GetPeriod() + track2.GetPeriod()) * 0.5;
float midTime = track2.GetMidTime();
if (midTime == 0)
return false;
double delta = track1.GetExpectedTime() - midTime;
double absDelta = fabs(delta);
if (absDelta < period * BYTE_SYNC_TOLERANCE)
{
track1.SetPeriod(period);
track2.SetPeriod(period);
delta *= 0.5;
track1.AdjustExpectedTime(-(float)delta);
track2.AdjustExpectedTime(+(float)delta);
return true;
}
g_bsStats.missCount++;
float ratio = (float)(absDelta / period);
if (ratio > g_bsStats.maxRatio)
g_bsStats.maxRatio = ratio;
return false;
}
void ReadBlockData(vector<BYTE>& data, Track& track1, Track& track2, int maxNum)
{
data.clear();
BYTE b1, b2;
while ((maxNum -= 2) >= 0)
{
if (!track1.ReadByte(b1))
break;
data.push_back(b1); // Blocks with an odd number of bytes are possible, e.g. GST 68K/OS
if (!track2.ReadByte(b2))
break;
data.push_back(b2);
int missBefore = g_bsStats.missCount;
ByteSync(track1, track2);
if (g_bsStats.missCount > missBefore)
{
int idx = (int)data.size();
if (g_bsStats.firstMissByteIdx < 0)
g_bsStats.firstMissByteIdx = idx;
g_bsStats.lastMissByteIdx = idx;
}
}
}
float GetBytePeriod(Track& track1, Track& track2)
{
return (track1.GetPeriod() + track2.GetPeriod()) * 4;
}
bool SyncTracks(Track& track1, Track& track2)
{
float trackOffset = GetBytePeriod(track1, track2) * 0.5f;
if (track1.GetTime() + trackOffset > track2.GetTime())
{
track2.AdvanceTo(track1.GetTime() + trackOffset);
track2.ResetExpectedTime();
}
else
{
track1.AdvanceTo(track2.GetTime() - trackOffset);
track1.ResetExpectedTime();
}
return true;
}
enum FailReason
{
FR_NONE = 0,
FR_PHASE_LOCK_BOTH,
FR_PHASE_LOCK_TRACK1_RESCUED, // track1 failed, track2 rescued (soft success)
FR_PHASE_LOCK_TRACK2_RESCUED, // track2 failed, track1 rescued (soft success)
FR_PREAMBLE_BOTH,
FR_PREAMBLE_TRACK2_ONLY, // recovered with track1's preamble (soft success)
FR_NO_DATA,
FR_CHUNK_TOO_SHORT,
FR__COUNT
};
static const char* FailReasonName(FailReason r)
{
switch (r)
{
case FR_NONE: return "ok";
case FR_PHASE_LOCK_BOTH: return "phase-lock fail (both tracks)";
case FR_PHASE_LOCK_TRACK1_RESCUED: return "phase-lock track1 rescued via track2";
case FR_PHASE_LOCK_TRACK2_RESCUED: return "phase-lock track2 rescued via track1";
case FR_PREAMBLE_BOTH: return "preamble not found (both tracks)";
case FR_PREAMBLE_TRACK2_ONLY: return "preamble track2 only (recovered)";
case FR_NO_DATA: return "phase lock ok but no data extracted";
case FR_CHUNK_TOO_SHORT: return "chunk shorter than minBlockLen";
default: return "unknown";
}
}
bool DecodeBlock(Block& block, Track& track1, Track& track2, float maxDeviation, int minNumBytes, int maxBlockSize, const Params& params, FailReason& reason, const char* debugTag = nullptr)
{
block.data.clear();
block.preamble.clear();
reason = FR_NONE;
g_bsStats = ByteSyncStats();
#ifdef _DEBUG
__int64 timestamp = Timestamp(block.startTime, params.traceFreq);
#endif
// Try both tracks. If either locks, use its period estimate for the
// other and let SyncTracks position it. This rescues chunks where one
// track has a magnetisation dropout or PLL-warm-up glitch but the other
// stays clean (the two tracks share the same physical tape speed, so
// the bit period is authoritative from whichever one locked).
bool track1Locked = track1.StartPhaseLock(maxDeviation, minNumBytes);
bool track2Locked = track2.StartPhaseLock(maxDeviation, minNumBytes);
if (!track1Locked && !track2Locked)
{
reason = FR_PHASE_LOCK_BOTH;
return false;
}
if (!track1Locked)
{
track1.Restart(track2.GetPeriod()); // adopt track2's period; SyncTracks below repositions us
reason = FR_PHASE_LOCK_TRACK1_RESCUED;
}
else if (!track2Locked)
{
track2.Restart(track1.GetPeriod());
reason = FR_PHASE_LOCK_TRACK2_RESCUED;
}
// Valid data should have signal on both tracks, so ignore anything before
SyncTracks(track1, track2);
block.endTime = -track1.GetTime();
// QL specific logic:
// The ZX8302 returns whole bytes, so we need to make some assumptions about how it identifies the first valid bit of the stream.
// Zeroes would be needed to synchronize the PLL (since for 1s it's not clear which of the transitions is the one in the middle).
// We know that when formatting a cartridge, QDOS writes at least three zeroes followed by one FF to each track.
// Alternate bytes are stored on the two microdrive tracks and track 2 has a 4 bit offset compared to track 1.
// We will assume that the ZX8302 looks for zeros followed by one FF. Perhaps only on track1 (it comes first), or we could also look on both.
// The two tracks need to roughly keep their alignment, since the ZX8302 alternates reading from them and exposes only a single
// 'byte-available' hardware flag to the OS. Each track has its own byte-long output buffer.
// Looking at the output of QDOS sector reads, sometimes bad sectors end with all-zeros as QDOS gives up if it cannot receive a byte on one of
// the tracks for some time. To increase the amount of data recovered, we will continue to read bytes and temporarily fill with zeroes the
// bad track instead, with the hope that we will be able to sync up again later.
vector<BYTE> preamble1, preamble2;
int preambleStartTime1, preambleStartTime2;
bool preamble1Ok = track1.FindPreamble(2, 1, 30, preamble1, &preambleStartTime1);
bool preamble2Ok = track2.FindPreamble(2, 1, 30, preamble2, &preambleStartTime2);
if (preamble1Ok && !preamble2Ok)
{
if (params.verbose)
printf("Warning: track2 preamble not found, using track1\n");
track2.Restart(track1.GetPeriod());
SyncTracks(track1, track2);
preamble2 = preamble1;
reason = FR_PREAMBLE_TRACK2_ONLY;
}
else if (!preamble1Ok && preamble2Ok)
{
// Symmetric fallback: track1 had a magnetisation glitch or PLL
// warm-up issue; use track2's preamble timing to bootstrap track1.
if (params.verbose)
printf("Warning: track1 preamble not found, using track2\n");
track1.Restart(track2.GetPeriod());
SyncTracks(track1, track2);
preamble1 = preamble2;
preamble1Ok = true;
reason = FR_PREAMBLE_TRACK2_ONLY;
}
else if (!preamble1Ok && !preamble2Ok)
{
reason = FR_PREAMBLE_BOTH;
}
float bytePeriod = GetBytePeriod(track1, track2);
size_t preambleSize = min(preamble1.size(), preamble2.size());
if (preambleSize < preamble1.size())
preambleStartTime1 += (int)(bytePeriod * (preamble1.size() - preambleSize) + 0.5);
block.gapLen += preambleStartTime1;
block.startTime += preambleStartTime1;
block.endTime += block.startTime;
if (preamble1Ok)
{
// Align to end
for (size_t i = 0; i < preambleSize; i++)
{
block.preamble.push_back(preamble1[preamble1.size() - preambleSize + i]);
block.preamble.push_back(preamble2[preamble2.size() - preambleSize + i]);
}
}
else
{
// Align to start
for (size_t i = 0; i < preambleSize; i++)
{
block.preamble.push_back(preamble1[i]);
block.preamble.push_back(preamble2[i]);
}
}
// Check that track alignment is still consistent
if (params.verbose && preamble1Ok && preamble2Ok)
{
float distance = abs(track1.GetTime() + bytePeriod * 0.5 - track2.GetTime());
if (distance > bytePeriod * 0.25)
{
printf("Warning: track1 and track2 preambles are not aligned at timestamp %zu uSec\n", Timestamp(block.startTime, params.traceFreq));
}
}
if (preamble1Ok)
{
// Only the "data" pass (unbounded read) is worth a mid-block flux
// picture — the header pass is only a handful of bytes.
bool wantByteDump = debugTag && g_fluxByteOffset >= 0 && maxBlockSize == 0;
if (wantByteDump)
{
track1.EnableAlignmentCollection(true);
track2.EnableAlignmentCollection(true);
}
ReadBlockData(block.data, track1, track2, maxBlockSize ? maxBlockSize : INT_MAX);
if (wantByteDump)
{
SaveByteFluxPictures(track1, track2, debugTag, g_fluxByteOffset, g_fluxByteWindow);
track1.EnableAlignmentCollection(false);
track2.EnableAlignmentCollection(false);
}
if (params.verbose)
{
printf("Read block with %zu bytes\n", block.data.size());
if (g_bsStats.missCount > 0)
printf(" ByteSync misses (|delta| >= %.0f%% period): count=%d maxRatio=%.2f byte=[%d..%d] (ts=%lldus)\n",
BYTE_SYNC_TOLERANCE * 100.0f,
g_bsStats.missCount, g_bsStats.maxRatio,
g_bsStats.firstMissByteIdx, g_bsStats.lastMissByteIdx,
(long long)Timestamp(block.startTime, params.traceFreq));
}
}
else
{
block.data = block.preamble;
block.preamble.clear();
}
block.endTime += track1.GetTime();
/* if (block.data.size() == 16 && block.data[1] == 0)
{
return true;
} */
return preamble1Ok;
}
bool ShouldSwapTracks(const vector<Chunk>& chunkList, float avgPeriod, float maxDeviation, int minNumBytes)
{
// Same logic as the one used later when starting to decode blocks
int voteSwap = 0;
int voteDontSwap = 0;
for (const Chunk& chunk : chunkList)
{
Track track1(chunk.track1, avgPeriod);
Track track2(chunk.track2, avgPeriod);
if (track1.StartPhaseLock(maxDeviation, minNumBytes) &&
track2.StartPhaseLock(maxDeviation, minNumBytes))
{
SyncTracks(track1, track2);
vector<BYTE> preamble1, preamble2;
int preambleStartTime1_, preambleStartTime2_;
bool preamble1Ok = track1.FindPreamble(2, 1, 30, preamble1, &preambleStartTime1_);
bool preamble2Ok = track2.FindPreamble(2, 1, 30, preamble2, &preambleStartTime2_);
if (preamble1Ok && preamble2Ok)
{
float bytePeriod = GetBytePeriod(track1, track2);
float distance = abs(track1.GetTime() + bytePeriod * 0.5 - track2.GetTime());
if (distance < bytePeriod * 0.5)
voteDontSwap++;
else
voteSwap++;
if (voteSwap == 100 || voteDontSwap == 100)
break;
}
}
}
return voteSwap > voteDontSwap;
}
void SwapTracks(vector<Chunk>& chunkList)
{
for (Chunk& chunk : chunkList)
swap(chunk.track1, chunk.track2);
}
bool DecodeBlocks(const Chunk& chunk, vector<Block>& blockList, int time, float* pAvgPeriod, float maxDeviation, int minNumBytes, const Params& params, int *pExtraGap, int minBlockLen, FailReason& reason, const char* debugTag = nullptr)
{
reason = FR_NONE;
int maxBlockHeaderSize = params.blockHeaderLen;
Block block;
block.gapLen = chunk.gapLen + *pExtraGap;
block.startTime = time;
*pExtraGap = 0;
Track track1(chunk.track1, *pAvgPeriod);
Track track2(chunk.track2, *pAvgPeriod);
if (debugTag)
{
char t1[64], t2[64];
snprintf(t1, sizeof(t1), "%s_track1", debugTag);
snprintf(t2, sizeof(t2), "%s_track2", debugTag);
track1.SetDebug(true, t1);
track2.SetDebug(true, t2);
}
if (chunk.track1.size() >= (minBlockLen + 1) * 8)
{
// Update tape speed
vector<int> betterFlux;
HistResult hr1 = DoHistogramAndImproveFlux(chunk.track1, betterFlux);
if (!betterFlux.empty())
track1.ReplaceFlux(betterFlux); // TODO: check that speed is consistent with other track, or replacing the flux will cause issues
HistResult hr2 = DoHistogramAndImproveFlux(chunk.track2, betterFlux);
if (!betterFlux.empty())
track2.ReplaceFlux(betterFlux);
HistResult hr = MergeHistResult(hr1, hr2);
if (hr.quality != INT_MAX)
{
float damp = (hr.quality == 0) ? 0.95 : 0.99;
*pAvgPeriod = *pAvgPeriod * damp + hr.period * (1 - damp);
}
}
#ifdef _DEBUG
block.speed = *pAvgPeriod;
#endif
if (params.verbose)
printf("Speed %.1f\n", *pAvgPeriod);
track1.SetPeriod(*pAvgPeriod);
track2.SetPeriod(*pAvgPeriod);
// Per-track "has a long gap" diagnostic: at the current rough bit-period,
// scan the chunk's raw intervals for anything > 2× period. That's a
// dropout candidate (a stretch of tape with no captured transitions
// that PLL can't have kept lock through). Stamped on every block pushed
// from this chunk; DrawPhase2Layout renders it as a red "1" or "2".
{
float gapThreshold = *pAvgPeriod * 2.0f;
block.track1HasGap = false;
for (int iv : chunk.track1)
if ((float)iv > gapThreshold) { block.track1HasGap = true; break; }
block.track2HasGap = false;
for (int iv : chunk.track2)
if ((float)iv > gapThreshold) { block.track2HasGap = true; break; }
}
if (maxBlockHeaderSize && chunk.dataLen <= (int)(MIN_SECTOR_SIZE * 8 * *pAvgPeriod)) // Only large chunks can contain block headers + sector data
maxBlockHeaderSize = 0;
bool bOk = DecodeBlock(block, track1, track2, maxDeviation, minNumBytes, maxBlockHeaderSize, params, reason, debugTag);
if (block.data.size())
blockList.push_back(block);
else
{
*pExtraGap = block.gapLen + chunk.dataLen;
bOk = false;
if (reason == FR_NONE)
reason = FR_NO_DATA;
}
if (bOk && maxBlockHeaderSize)
{
track1.SetPeriod(*pAvgPeriod);
track2.SetPeriod(*pAvgPeriod);
block.startTime += track1.GetTime();
block.gapLen = 0;
FailReason innerReason = FR_NONE;
if (!DecodeBlock(block, track1, track2, maxDeviation, minNumBytes, 0, params, innerReason, debugTag))
return false;
if (block.data.size())
blockList.push_back(block);
else
{
int x = 0;
}
}
return bOk;
}
int ChooseFirstSector(vector<Block>& blockList, int loopLen)
{
int largestDistance = blockList[0].startTime + loopLen - blockList.back().startTime;
int firstSector = 0;
for (int i = 1; i < blockList.size(); i++)
{
int delta = blockList[i].startTime - blockList[i - 1].startTime;
if (delta > largestDistance)
{
largestDistance = delta;
firstSector = i;
}
}
/*
sort(gapList.begin(), gapList.end());
int medianGap = gapList[gapList.size() / 2];
int firstSector = 0;
int largestDistance = INT_MIN;
for (int i = 0; i < blockList.size(); i++)
{
int distance = abs(blockList[i].gapLen - medianGap);
if (distance > largestDistance)
{
largestDistance = distance;
firstSector = i;
}
}
*/
return firstSector;
}
static void ReportGapLen(const vector<Block>& blockList, size_t len, const char *name, float conversionScale)
{
vector<int> gapLen;
for (const Block& b : blockList)
if (b.data.size() == len)
gapLen.push_back(b.gapLen);
if (!gapLen.empty())
{
std::sort(gapLen.begin(), gapLen.end());
int medianLen = gapLen[gapLen.size() / 2];
printf("Median %s gap length: %.1f bytes\n", name, medianLen * conversionScale);
}
}
int DetectAndReport(const vector<Block>& blockList, const Params& params)
{
int detectedOS = OS_UNKNOWN;
vector<int> spuriousLen;
vector<int> bigLen;
vector<int> smallLen;
float k = params.mdvFreq / (4.0f * params.traceFreq);
int numReliable = 0;
int numUnreliable = 0;
for (const Block& b : blockList)
{
int size = (int)b.data.size();
if (b.preamble.size() < 8)
spuriousLen.push_back(size);
else if (size >= MIN_SECTOR_SIZE)
bigLen.push_back(size);
else
smallLen.push_back(size);
if (b.numCopies >= 2)
numReliable++;
else
numUnreliable++;
}
printf("Found %zu sectors and %zu chunks of spurious data\n", bigLen.size(), spuriousLen.size());
printf("%d good blocks out of %zu\n", numReliable, blockList.size());
if (bigLen.size() && smallLen.size())
{
std::sort(bigLen.begin(), bigLen.end());
std::sort(smallLen.begin(), smallLen.end());
unsigned int numSectors = bigLen.size();
int sectorLen = bigLen[numSectors / 2];
if (smallLen.size() > numSectors * 3 / 2)
{
// 2 or more headers per sector
int headerLen1 = smallLen[smallLen.size() / 4];
int headerLen2 = smallLen[smallLen.size() * 3 / 4];
printf("Header and sector sizes (bytes): %d, %d, %d\n", headerLen1, headerLen2, sectorLen);
ReportGapLen(blockList, headerLen1, "header1", k);
ReportGapLen(blockList, headerLen2, "header2", k);
if (sectorLen == 514)
detectedOS = OS_OPD;
}
else
{
// 1 header per sector
int headerLen = smallLen[smallLen.size() / 2];
printf("Header and sector sizes (bytes): %d, %d\n", headerLen, sectorLen);
ReportGapLen(blockList, headerLen, "header", k);
if (sectorLen == 1029 && headerLen == 17)
{
detectedOS = OS_GST;
if (numSectors < 100)
printf("%d sectors missing\n", 100 - numSectors);
}
else if (sectorLen == 526 && headerLen == 16 && numSectors >= 150)
detectedOS = OS_QDOS; // Note: block header and sector data are back to back
else if ((sectorLen == 528 || sectorLen == 529) && (headerLen == 15 || headerLen == 16))
detectedOS = OS_SPECTRUM;
}
ReportGapLen(blockList, sectorLen, "sector", k);
}
static const char* osName[] = { "Unknown", "QDOS", "Spectrum", "OPD", "GST" };
printf("Detected operating system: %s\n", osName[detectedOS]);
return detectedOS;
}
bool FindBestChannels(vector<BYTE>& data, Params& params)
{
int numTransitions[8] = { 0 };
for (size_t i = 1; i < data.size(); i++)
{
int diff = data[i - 1] ^ data[i];
for (int ch = 0; ch < 8; ch++)
{
if (diff & 1)
numTransitions[ch]++;
diff >>= 1;
}
}
int maxIndex = max_element(numTransitions, numTransitions + 8) - numTransitions;
if (numTransitions[maxIndex] < 200000)
return false;
params.track2Mask = 1 << maxIndex;
numTransitions[maxIndex] = 0;
maxIndex = max_element(numTransitions, numTransitions + 8) - numTransitions;
if (numTransitions[maxIndex] < 200000)
return false;
params.track1Mask = 1 << maxIndex;
return true;
}
void ExportTrack(const vector<BYTE>& data, BYTE mask, int start, int count)
{
count /= 8;
FILE* f = fopen("export.bin", "wb");
int n = 0;
BYTE acc = 0;
for (; start < data.size(); start++)
{
acc <<= 1;
if (data[start] & mask)
acc |= 1;
n = (n + 1) & 7;
if (n == 0)
{
fwrite(&acc, 1, 1, f);
if (--count == 0)
break;
}
}
fclose(f);
}
#ifdef _DEBUG
void ExportSpeed(const vector<Block>& blockList, const Params& params)
{
float nominalSpeed = 76; // 76 cm/sec
float k = nominalSpeed * params.traceFreq / params.mdvFreq;
FILE* f = fopen("speed.csv", "wb");
for (const Block& b : blockList)
fprintf(f, "%f,%f\r\n", (float)b.endTime / params.traceFreq, k / b.speed);
fclose(f);
}
#endif
int main(int argc, char* argv[])
{
printf("MdvDecode 1.1 by Daniele Terdina\n");
Params params;
params.verbose = false;
params.blockHeaderLen = 0;
//params.blockHeaderLen = 4; // OPD
params.traceFreq = 24000000;
params.mdvFreq = 100000;
params.track1Mask = 1;
params.track2Mask = 2;
bool canChooseChannels = true;
bool saveJpg = false;
int fluxJpgChunk = -1; // -flux-jpg <index>: dump flux picture for that chunk
int fluxByteOffset = -1; // -flux-byte <k>: also dump a mid-block flux picture around byte k of the block
int fluxByteWindow = 16; // -flux-window <w>: half-window in bytes (per track) around the byte of interest
int dumpBlock = -1; // -dump-block <index>: dump raw copies of the given merged block
const float maxFreqError = 0.2;
const float minGapDuration = 0.002;
#ifndef MDVDECODE_DEBUG_HARDCODED_INPUT
//==========================================================================
// Parse command line parameters
//==========================================================================
int argIndex = 1;
bool argError = false;
while (argIndex < argc && !argError && (argv[argIndex][0] == '-' || argv[argIndex][0] == '/'))
{
char* p = argv[argIndex];
while (*p == '-' || *p == '/')
p++;
if (!_stricmp(p, "opd"))
params.blockHeaderLen = 4;
else if (!_stricmp(p, "zx"))
params.mdvFreq = 80000;
else if (!_stricmp(p, "v") || !_stricmp(p, "verbose"))
params.verbose = true;
else if (!_stricmp(p, "flux-jpg") && argIndex + 1 < argc)
{
fluxJpgChunk = atoi(argv[argIndex + 1]);
argIndex++;
}
else if (!_stricmp(p, "dump-block") && argIndex + 1 < argc)
{
dumpBlock = atoi(argv[argIndex + 1]);
argIndex++;
}
else if (!_stricmp(p, "flux-byte") && argIndex + 1 < argc)
{
fluxByteOffset = atoi(argv[argIndex + 1]);
argIndex++;
}
else if (!_stricmp(p, "flux-window") && argIndex + 1 < argc)
{
fluxByteWindow = atoi(argv[argIndex + 1]);
argIndex++;
}
else if (!_stricmp(p, "channels") && argIndex + 2 < argc)
{
canChooseChannels = false;
int ch1 = atoi(argv[argIndex + 1]);
int ch2 = atoi(argv[argIndex + 2]);
argIndex += 2;
argError = ch1 == ch2 || ch1 < 0 || ch1 > 7 || ch2 < 0 || ch2 > 7;
params.track1Mask = 1 << ch1;
params.track2Mask = 1 << ch2;
}
else if (!_stricmp(p, "freq") && argIndex + 1 < argc)
{
int freq = atoi(argv[argIndex + 1]);
argIndex++;
argError = freq <= 0;
params.traceFreq = freq;
}
else if (!_stricmp(p, "jpg"))
saveJpg = true;
else
argError = true;
argIndex++;
}
if (argError || argIndex >= argc || argc > argIndex + 2)
{
printf("Usage: MdvDecode [<options>] <input_directory> [<output_file>]\n");
printf(" Possible options (can be omitted to use default values):\n"
" -verbose print additional messages for troubleshooting\n"
" -opd needed to succesfully parse ICL OPD cartridges\n"
" -zx hint that this is a ZX Spectrum cartridge\n"
" -channels <track1ch> <track2ch> specify which logic trace channels contain each track\n"
" -freq <frequency> trace sampling frequency (default: 24 MHz)\n"
" -jpg save a diagnostic .jpg visualization of the decoded blocks\n"
" -flux-jpg <chunk_index> save flux+alignment picture for phase-lock/preamble of that chunk\n"
" -flux-byte <byte_offset> also save mid-block flux picture around that byte of the data block\n"
" -flux-window <bytes> half-window (default 16) of bytes around -flux-byte to show\n"
" -dump-block <merged_idx> dump raw copies of the given merged block to stdout\n");
return 1;
}
string inputDir(argv[argIndex]);
string outputFile;
if (argIndex + 1 < argc)
outputFile = argv[argIndex + 1];
else
outputFile = inputDir + ".MDVRAW";
#else
// Debugging
//#define FILENAME "E:\\dev\\MdvDecode\\tapezx"
//#define FILENAME "E:\\dev\\MdvDecode\\ASM1"
//#define FILENAME "E:\\dev\\MdvDecode\\GstUtil"
#define FILENAME "E:\\dev\\MdvDecode\\zkul"
//#define FILENAME "E:\\dev\\MdvDecode\\dragonhold_backup"
//#define FILENAME "E:\\dev\\MdvDecode\\icl_Basic"
//#define FILENAME "E:\\dev\\MdvDecode\\icl_demo"
string inputDir(FILENAME);
string outputFile(FILENAME ".MDVRAW");
params.verbose = true;
#endif
//==========================================================================
// Load logic trace
//==========================================================================
vector<BYTE> data;
if (!Load(inputDir.c_str(), data))
{
printf("ERROR: Cannot open input files at %s\n", inputDir.c_str());
return 1;
}
// TODO: read trace frequency from metadata
printf("Read %zu bytes (%.2f seconds)\n", data.size(), (float)data.size() / params.traceFreq);
//==========================================================================
// Find the logic analyzer channels that contain signals
//==========================================================================
if (canChooseChannels)
{
if (!FindBestChannels(data, params))
{
printf("The logic trace doesn't contain enough channels with valid data\n");
return 1;
}
}
//ExportTrack(data, params.track1Mask, params.traceFreq, 100000);
//=======================================================================================================
// Remove short spikes, measure time between transitions, identify continuous chunks containing no gaps
//=======================================================================================================
const int maxSpuriousSize = params.traceFreq / params.mdvFreq / 20;
RemoveSpurious(data, maxSpuriousSize, params.track1Mask);
RemoveSpurious(data, maxSpuriousSize, params.track2Mask);
vector<Chunk> fluxList = FluxChunks(data, (int)(minGapDuration * params.traceFreq), params.track1Mask, params.track2Mask);
printf("Found %d data chunks\n", max((int)fluxList.size() - 2, 0));
const int minBlockLen = 6; // 4 preamble + 1 data + 1 checksum for each of the two tracks
float avgPeriod = (float)params.traceFreq / params.mdvFreq;
int numFailures = 0;
int time = fluxList[0].gapLen + fluxList[0].dataLen;
size_t start = 1;
if (fluxList[0].gapLen > avgPeriod * 20)
{
// Don't discard first chunk if the trace starts with a gap
start = 0;
time = 0;
}
//==========================================================================
// Find which of the two tracks is track1 (leads track2 by 4 bits)
//==========================================================================
if (canChooseChannels)
{
if (ShouldSwapTracks(fluxList, avgPeriod, maxFreqError, minBlockLen))
SwapTracks(fluxList);
}
//============================================================================================================
// Decode each chunk to a block of bytes, trying to keep the correct alignment with the magnetic transitions
//============================================================================================================
vector<Block> allBlocks;
int extraGap = 0;
int failByReason[FR__COUNT] = { 0 };
int nRecoveredTrack2 = 0;
// Propagate -flux-byte / -flux-window into DecodeBlock via file-scope statics.
g_fluxByteOffset = fluxByteOffset;
g_fluxByteWindow = fluxByteWindow > 0 ? fluxByteWindow : 16;
for (size_t i = start; i < fluxList.size() - 1; i++) // Ignore first and last chunk as they will usually be truncated
{
time += fluxList[i].gapLen;
//if (fluxList[i].track1.size() > (minBlockLen + 1) * 8 &&
// fluxList[i].track2.size() > (minBlockLen + 1) * 8)
{
FailReason reason = FR_NONE;
char dbgTag[32];
const char* debugTagArg = MakeFluxJpgTag((int)i, fluxJpgChunk, time, params.traceFreq, dbgTag, sizeof(dbgTag));
bool decoded = DecodeBlocks(fluxList[i], allBlocks, time, &avgPeriod, maxFreqError, minBlockLen, params, &extraGap, minBlockLen, reason, debugTagArg);
if (!decoded)
{
bool longEnough = fluxList[i].track1.size() > (minBlockLen + 1) * 8 &&
fluxList[i].track2.size() > (minBlockLen + 1) * 8;
if (longEnough)
{
if (params.verbose)
printf("Error decoding data chunk #%zu, timestamp %zu uSec (%s)\n",
i, Timestamp(time, params.traceFreq), FailReasonName(reason));
numFailures++;
if (reason >= 0 && reason < FR__COUNT)
failByReason[reason]++;
}
else
{
failByReason[FR_CHUNK_TOO_SHORT]++;
}
}
else if (reason == FR_PREAMBLE_TRACK2_ONLY)
{
nRecoveredTrack2++;
}
}
time += fluxList[i].dataLen;
}
if (params.verbose)
{
printf("Failure breakdown:\n");
for (int r = 1; r < FR__COUNT; r++)
if (failByReason[r])
printf(" %-40s %d\n", FailReasonName((FailReason)r), failByReason[r]);
if (nRecoveredTrack2)
printf(" (recovered %d chunks via track1 preamble fallback)\n", nRecoveredTrack2);
}
// Optionally drop blocks without a proper preamble before merging.
// DetectAndReport uses the same criterion (preamble.size() < 8) to label
// chunks as "spurious". Dropping them cleans up the merge on tapes with
// lots of overwritten sectors (e.g. ZX Spectrum captures), but the
// resulting .MDVRAW is no longer a faithful reproduction of the tape.
// Kept off by default; enable when debugging the ZX Spectrum path.
#if 0
{
size_t before = allBlocks.size();
int carriedTime = 0;
size_t out = 0;
for (size_t in = 0; in < allBlocks.size(); in++)
{
Block& b = allBlocks[in];
if (b.preamble.size() < 8)
{
carriedTime += b.gapLen + (b.endTime - b.startTime);
}
else
{
b.gapLen += carriedTime;
carriedTime = 0;
if (out != in)
allBlocks[out] = std::move(b);
out++;
}
}
allBlocks.resize(out);
if (params.verbose)
printf("Dropped %zu blocks with short preamble (%zu remain)\n",
before - allBlocks.size(), allBlocks.size());
}
#endif
for (size_t i = 0; i < allBlocks.size(); i++)
allBlocks[i].dbgId = (int)i;
#ifdef _DEBUG
//ExportSpeed(allBlocks, params);
#endif