-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTape.cpp
More file actions
131 lines (116 loc) · 3.34 KB
/
Copy pathTape.cpp
File metadata and controls
131 lines (116 loc) · 3.34 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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#include "MdvDecode.h"
#include "ImageHeader.h"
void Tape::OutputGap(int numBits, BYTE value)
{
if (numBits)
{
if (numGapBits)
{
int numShift = min(numBits, 8 - numGapBits);
numGapBits += numShift;
numBits -= numShift;
gapByte <<= numShift;
gapByte |= (value >> (8 - numShift));
if (numGapBits == 8)
{
gapFlag.push_back(gapByte);
gapByte = 0;
numGapBits = 0;
}
}
}
int numBytes = numBits >> 3;
if (numBytes)
{
gapFlag.insert(gapFlag.end(), numBytes, value);
numBits &= 7;
}
if (numBits)
{
gapByte = value >> (8 - numBits);
numGapBits = numBits;
}
}
void Tape::Align(size_t size)
{
static BYTE zero = 0;
if (size & 1)
{
// Add a zero to properly align to track 1
data.insert(data.end(), &zero, &zero + 1);
OutputGap(1, 0xFF);
}
}
void Tape::CreateFromBlocks(const vector<Block>& blockList, int firstBlock, float bitDuration)
{
gapByte = 0;
numGapBits = 0;
float scale = 1.0f / (4.0f * bitDuration);
for (size_t i = 0; i < blockList.size(); i++)
{
const Block& block = blockList[(i + firstBlock) % blockList.size()];
// GAP
int gapBytes = (int)(block.gapLen * scale + 0.5);
gapBytes = max(gapBytes, 0);
gapBytes = (gapBytes + 1) & -2; // bytes come in pairs (one per track)
if (gapBytes)
{
data.resize(data.size() + gapBytes);
OutputGap(gapBytes, 0);
}
// PREAMBLE
Align(block.preamble.size());
data.insert(data.end(), block.preamble.begin(), block.preamble.end());
OutputGap(block.preamble.size(), 0xFF);
// DATA
data.insert(data.end(), block.data.begin(), block.data.end());
OutputGap(block.data.size(), 0xFF);
Align(block.data.size());
}
if (numGapBits)
gapFlag.push_back(gapByte << (8 - numGapBits));
}
#define ULA_QL 1
#define ULA_INTERFACE_1 2
#define CREATOR_MDVDECODE 1
void Tape::SaveToFile(const char* fileName, int mdvFreq, int detectedOS)
{
printf("Tape is %zu bytes long including gaps (%.1f seconds)\n", data.size(), (float)data.size() * 4 / mdvFreq);
int dataPad = (-(int)data.size() & 3);
int gapPad = (-(int)gapFlag.size() & 3);
Header header = { 0 };
strncpy(header.id, "QLMDVRAW", sizeof(header.id));
header.fileFormatMajorVersion = 1;
header.fileFormatMinorVersion = 0;
header.headerSize = sizeof(Header);
header.creatorId = CREATOR_MDVDECODE;
header.creatorMajorVersion = 1;
header.creatorMinorVersion = 2;
header.creatorRevision = 0;
header.ulaFamily = (detectedOS == OS_SPECTRUM) ? ULA_INTERFACE_1 : ULA_QL;
header.recognizedFileSystem = detectedOS;
//header.reserved[0] = header.reserved[1] = header.reserved[2] = 0;
header.frequency = mdvFreq;
header.dataOffset = sizeof(Header);
header.dataLength = (uint32_t)data.size();
header.gapBitmapOffset = header.dataOffset + header.dataLength + dataPad;
header.gapBitmapLength = (uint32_t)gapFlag.size();
//header.junctionStartOffset = header.junctionLength = 0;
// Sector 0 header block is emitted first by CreateFromBlocks, so its
// position in the data section is 0.
header.recommendedInitialTapePosition = 0;
if (dataPad)
data.insert(data.end(), dataPad, 0);
if (gapPad)
gapFlag.insert(gapFlag.end(), gapPad, 0);
FILE* f = fopen(fileName, "wb");
if (f)
{
fwrite(&header, sizeof(header), 1, f);
fwrite(&data[0], 1, data.size(), f);
fwrite(&gapFlag[0], 1, gapFlag.size(), f);
fclose(f);
}
}