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
|
// license:BSD-3-Clause
// copyright-holders:Bartman/Abyss
/***************************************************************************
Brother LW-30 Disk image format
see https://github.com/BartmanAbyss/brother-diskconv for disk conversion tool
***************************************************************************/
#include "lw30_dsk.h"
#include "ioprocs.h"
#include "multibyte.h"
#include <array>
#include <cassert>
#include <cstring>
namespace {
constexpr uint16_t sync_table[]{
0xdaef, 0xb7ad, 0xfbbe, 0xeadf, 0xbffa, 0xaeb6, 0xf5d7, 0xdbee, 0xbaab, 0xfdbd,
0xebde, 0xd5f7, 0xafb5, 0xf6d6, 0xdded, 0xbbaa, 0xedbb, 0xd6dd, 0xb5f6, 0xf7af,
0xded5, 0xbdeb, 0xabfd, 0xeeba, 0xd7db, 0xb6f5, 0xfaae, 0xdfbf, 0xbeea, 0xadfb,
0xefb7, 0xdada, 0xb7ef, 0xfbad, 0xeabe, 0xbfdf, 0xaefa, 0xf5b6, 0xdbd7, 0xbaee,
0xfdab, 0xebbd, 0xd5de, 0xaff7, 0xf6b5, 0xddd6, 0xbbed, 0xaadd, 0xedf6, 0xd6af,
0xb5d5, 0xf7eb, 0xdefd, 0xbdba, 0xabdb, 0xeef5, 0xd7ae, 0xb6bf, 0xfaea, 0xdffb,
0xbeb7, 0xadda, 0xefef, 0xdaad, 0xb7be, 0xfbdf, 0xeafa, 0xbfb6, 0xaed7, 0xf5ee,
0xdbab, 0xbabd, 0xfdde, 0xebf7, 0xd5b5, 0xafd6, 0xf6ed, 0xddaa, 0xd6bb, 0xb5dd
};
constexpr uint8_t gcr_table[]{
0xaa, 0xab, 0xad, 0xae, 0xaf, 0xb5, 0xb6, 0xb7,
0xba, 0xbb, 0xbd, 0xbe, 0xbf, 0xd5, 0xd6, 0xd7,
0xda, 0xdb, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xed,
0xee, 0xef, 0xf5, 0xf6, 0xf7, 0xfa, 0xfb, 0xfd,
0xfe, 0xff // FE, FF are reserved
};
// format
constexpr uint8_t sector_prefix[]{
0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xab
};
// write
constexpr uint8_t sector_header[]{
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xed
};
// write
constexpr uint8_t sector_footer[]{
0xf5, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
};
constexpr uint8_t sector_interleave1[]{ // 1-based
1, 6, 11, 4, 9, 2, 7, 12, 5, 10, 3, 8
};
constexpr uint8_t sector_interleave2[]{ // 1-based
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
};
constexpr uint8_t sector_interleave3[]{ // 1-based
1, 4, 7, 10, 6, 9, 12, 3, 11, 2, 5, 8
};
void gcr_encode_5_to_8(const uint8_t *input, uint8_t *output)
{
// input:
// 76543210
// --------
// 00000111 0
// 11222223 1
// 33334444 2
// 45555566 3
// 66677777 4
output[0] = gcr_table[(input[0] >> 3) & 0x1f];
output[1] = gcr_table[((input[0] << 2) & 0x1f) | ((input[1] >> 6) & 0b00000011)];
output[2] = gcr_table[(input[1] >> 1) & 0x1f];
output[3] = gcr_table[((input[1] << 4) & 0x1f) | ((input[2] >> 4) & 0b00001111)];
output[4] = gcr_table[((input[2] << 1) & 0x1f) | ((input[3] >> 7) & 0b00000001)];
output[5] = gcr_table[(input[3] >> 2) & 0x1f];
output[6] = gcr_table[((input[3] << 3) & 0x1f) | ((input[4] >> 5) & 0b00000111)];
output[7] = gcr_table[input[4] & 0x1f];
}
std::array<uint8_t, 3> checksum_256_bytes(const uint8_t *input)
{
size_t i = 0;
uint8_t a = 0;
uint8_t c = input[i++];
uint8_t d = input[i++];
uint8_t e = input[i++];
for(size_t b = 0; b < 253; b++) {
a = d;
if(c & 0b10000000)
a ^= 1;
d = c;
c = a;
a = (d << 1) ^ e;
e = d;
d = a;
e ^= input[i++];
}
return { c, d, e };
}
std::array<uint8_t, 416> gcr_encode_and_checksum(const uint8_t *input /* 256 bytes */) {
std::array<uint8_t, 416> output;
for(int i = 0; i < 51; i++)
gcr_encode_5_to_8(&input[i * 5], &output[i * 8]);
auto checksum = checksum_256_bytes(input);
std::array<uint8_t, 5> end_and_checksum{ input[255], checksum[0], checksum[1], checksum[2], 0x58 };
gcr_encode_5_to_8(&end_and_checksum[0], &output[408]);
return output;
}
}
static constexpr int TRACKS_PER_DISK = 78;
static constexpr int SECTORS_PER_TRACK = 12;
static constexpr int SECTOR_SIZE = 256;
static constexpr int RPM = 300;
static constexpr int CELLS_PER_REV = 250'000 / (RPM / 60);
// format track: 0xaa (2), 0xaa (48), 12*sector
// format sector: sector_prefix (8), track_sync (2), sector_sync (2), predata (19), payload=0xaa (414), postdata (13), 0xaa (42), should come out to ~4,000 bits
// write sector: (after sector_sync, 0xdd) sector_header (2+14), payload (416), sector_footer (11)
// from write_format, write_sector_data_header_data_footer
static constexpr int raw_sector_size = 8/*sector_prefix*/ + 2/*track_sync*/ + 2/*sector_sync*/ + 1/*0xdd*/ + 16/*sector_header*/ + 416/*payload*/ + 11/*sector_footer*/ + 42/*0xaa*/;
static constexpr int raw_track_size = 2/*0xaa*/ + 48/*0xaa*/ + SECTORS_PER_TRACK * raw_sector_size;
int lw30_format::identify(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants) const
{
uint64_t size = 0;
io.length(size);
if(size == TRACKS_PER_DISK * SECTORS_PER_TRACK * SECTOR_SIZE)
return 50; // identified by size
return 0;
}
bool lw30_format::load(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants, floppy_image &image) const
{
uint8_t trackdata[SECTORS_PER_TRACK * SECTOR_SIZE], rawdata[CELLS_PER_REV / 8];
memset(rawdata, 0xaa, sizeof(rawdata));
for(int track = 0; track < TRACKS_PER_DISK; track++) {
size_t actual{};
io.read_at(track * SECTORS_PER_TRACK * SECTOR_SIZE, trackdata, SECTORS_PER_TRACK * SECTOR_SIZE, actual);
if(actual != SECTORS_PER_TRACK * SECTOR_SIZE)
return false;
size_t i = 0;
for(int x = 0; x < 2 + 48; x++)
rawdata[i++] = 0xaa;
auto interleave_offset = (track % 4) * 4;
for(size_t s = interleave_offset; s < interleave_offset + SECTORS_PER_TRACK; s++) {
auto sector = sector_interleave1[s % SECTORS_PER_TRACK] - 1;
// according to check_track_and_sector
for(const auto& d : sector_prefix) // 8 bytes
rawdata[i++] = d;
put_u16le(&rawdata[i], sync_table[track]);
i += 2;
put_u16le(&rawdata[i], sync_table[sector]);
i += 2;
rawdata[i++] = 0xdd;
for(const auto& d : sector_header) // 16 bytes
rawdata[i++] = d;
auto payload = gcr_encode_and_checksum(trackdata + sector * SECTOR_SIZE); // 256 -> 416 bytes
for(const auto &d : payload)
rawdata[i++] = d;
for(const auto &d : sector_footer) // 11 bytes
rawdata[i++] = d;
for(int x = 0; x < 42; x++)
rawdata[i++] = 0xaa;
}
assert(i == raw_track_size);
assert(i <= CELLS_PER_REV / 8);
generate_track_from_bitstream(track, 0, rawdata, CELLS_PER_REV, image);
}
image.set_variant(floppy_image::SSDD);
return true;
}
const char *lw30_format::name() const noexcept
{
return "lw30";
}
const char *lw30_format::description() const noexcept
{
return "Brother LW-30 floppy disk image";
}
const char *lw30_format::extensions() const noexcept
{
return "img";
}
bool lw30_format::supports_save() const noexcept
{
// TODO
return false;
}
const lw30_format FLOPPY_LW30_FORMAT;
|