|
| 1 | +// license:BSD-3-Clause |
| 2 | +// copyright-holders:Bartman/Abyss |
| 3 | +/*************************************************************************** |
| 4 | +
|
| 5 | + Brother LW-30 Disk image format |
| 6 | + see https://github.com/BartmanAbyss/brother-diskconv for disk conversion tool |
| 7 | +
|
| 8 | +***************************************************************************/ |
| 9 | +#include "lw30_dsk.h" |
| 10 | + |
| 11 | +#include "ioprocs.h" |
| 12 | + |
| 13 | +#include <array> |
| 14 | +#include <cassert> |
| 15 | +#include <cstring> |
| 16 | + |
| 17 | + |
| 18 | +namespace { |
| 19 | + constexpr uint16_t sync_table[]{ |
| 20 | + 0xdaef, 0xb7ad, 0xfbbe, 0xeadf, 0xbffa, 0xaeb6, 0xf5d7, 0xdbee, 0xbaab, 0xfdbd, |
| 21 | + 0xebde, 0xd5f7, 0xafb5, 0xf6d6, 0xdded, 0xbbaa, 0xedbb, 0xd6dd, 0xb5f6, 0xf7af, |
| 22 | + 0xded5, 0xbdeb, 0xabfd, 0xeeba, 0xd7db, 0xb6f5, 0xfaae, 0xdfbf, 0xbeea, 0xadfb, |
| 23 | + 0xefb7, 0xdada, 0xb7ef, 0xfbad, 0xeabe, 0xbfdf, 0xaefa, 0xf5b6, 0xdbd7, 0xbaee, |
| 24 | + 0xfdab, 0xebbd, 0xd5de, 0xaff7, 0xf6b5, 0xddd6, 0xbbed, 0xaadd, 0xedf6, 0xd6af, |
| 25 | + 0xb5d5, 0xf7eb, 0xdefd, 0xbdba, 0xabdb, 0xeef5, 0xd7ae, 0xb6bf, 0xfaea, 0xdffb, |
| 26 | + 0xbeb7, 0xadda, 0xefef, 0xdaad, 0xb7be, 0xfbdf, 0xeafa, 0xbfb6, 0xaed7, 0xf5ee, |
| 27 | + 0xdbab, 0xbabd, 0xfdde, 0xebf7, 0xd5b5, 0xafd6, 0xf6ed, 0xddaa, 0xd6bb, 0xb5dd |
| 28 | + }; |
| 29 | + |
| 30 | + constexpr uint8_t gcr_table[]{ |
| 31 | + 0xaa, 0xab, 0xad, 0xae, 0xaf, 0xb5, 0xb6, 0xb7, |
| 32 | + 0xba, 0xbb, 0xbd, 0xbe, 0xbf, 0xd5, 0xd6, 0xd7, |
| 33 | + 0xda, 0xdb, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xed, |
| 34 | + 0xee, 0xef, 0xf5, 0xf6, 0xf7, 0xfa, 0xfb, 0xfd, |
| 35 | + 0xfe, 0xff // FE, FF are reserved |
| 36 | + }; |
| 37 | + |
| 38 | + // format |
| 39 | + constexpr uint8_t sector_prefix[]{ |
| 40 | + 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xab |
| 41 | + }; |
| 42 | + |
| 43 | + // write |
| 44 | + constexpr uint8_t sector_header[]{ |
| 45 | + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 46 | + 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xed |
| 47 | + }; |
| 48 | + |
| 49 | + // write |
| 50 | + constexpr uint8_t sector_footer[]{ |
| 51 | + 0xf5, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 52 | + }; |
| 53 | + |
| 54 | + constexpr uint8_t sector_interleave1[]{ // 1-based |
| 55 | + 1, 6, 11, 4, 9, 2, 7, 12, 5, 10, 3, 8 |
| 56 | + }; |
| 57 | + |
| 58 | + constexpr uint8_t sector_interleave2[]{ // 1-based |
| 59 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 |
| 60 | + }; |
| 61 | + |
| 62 | + constexpr uint8_t sector_interleave3[]{ // 1-based |
| 63 | + 1, 4, 7, 10, 6, 9, 12, 3, 11, 2, 5, 8 |
| 64 | + }; |
| 65 | + |
| 66 | + void gcr_encode_5_to_8(const uint8_t *input, uint8_t *output) |
| 67 | + { |
| 68 | + // input: |
| 69 | + // 76543210 |
| 70 | + // -------- |
| 71 | + // 00000111 0 |
| 72 | + // 11222223 1 |
| 73 | + // 33334444 2 |
| 74 | + // 45555566 3 |
| 75 | + // 66677777 4 |
| 76 | + |
| 77 | + output[0] = gcr_table[(input[0] >> 3) & 0x1f]; |
| 78 | + output[1] = gcr_table[((input[0] << 2) & 0x1f) | ((input[1] >> 6) & 0b00000011)]; |
| 79 | + output[2] = gcr_table[(input[1] >> 1) & 0x1f]; |
| 80 | + output[3] = gcr_table[((input[1] << 4) & 0x1f) | ((input[2] >> 4) & 0b00001111)]; |
| 81 | + output[4] = gcr_table[((input[2] << 1) & 0x1f) | ((input[3] >> 7) & 0b00000001)]; |
| 82 | + output[5] = gcr_table[(input[3] >> 2) & 0x1f]; |
| 83 | + output[6] = gcr_table[((input[3] << 3) & 0x1f) | ((input[4] >> 5) & 0b00000111)]; |
| 84 | + output[7] = gcr_table[input[4] & 0x1f]; |
| 85 | + } |
| 86 | + |
| 87 | + std::array<uint8_t, 3> checksum_256_bytes(const uint8_t *input) |
| 88 | + { |
| 89 | + size_t i = 0; |
| 90 | + uint8_t a = 0; |
| 91 | + uint8_t c = input[i++]; |
| 92 | + uint8_t d = input[i++]; |
| 93 | + uint8_t e = input[i++]; |
| 94 | + for(size_t b = 0; b < 253; b++) { |
| 95 | + a = d; |
| 96 | + if(c & 0b10000000) |
| 97 | + a ^= 1; |
| 98 | + d = c; |
| 99 | + c = a; |
| 100 | + a = (d << 1) ^ e; |
| 101 | + e = d; |
| 102 | + d = a; |
| 103 | + e ^= input[i++]; |
| 104 | + } |
| 105 | + |
| 106 | + return { c, d, e }; |
| 107 | + } |
| 108 | + |
| 109 | + std::array<uint8_t, 416> gcr_encode_and_checksum(const uint8_t *input /* 256 bytes */) { |
| 110 | + std::array<uint8_t, 416> output; |
| 111 | + for(int i = 0; i < 51; i++) |
| 112 | + gcr_encode_5_to_8(&input[i * 5], &output[i * 8]); |
| 113 | + |
| 114 | + auto checksum = checksum_256_bytes(input); |
| 115 | + std::array<uint8_t, 5> end_and_checksum{ input[255], checksum[0], checksum[1], checksum[2], 0x58 }; |
| 116 | + gcr_encode_5_to_8(&end_and_checksum[0], &output[408]); |
| 117 | + |
| 118 | + return output; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +static constexpr int TRACKS_PER_DISK = 78; |
| 123 | +static constexpr int SECTORS_PER_TRACK = 12; |
| 124 | +static constexpr int SECTOR_SIZE = 256; |
| 125 | + |
| 126 | +static constexpr int RPM = 300; |
| 127 | +static constexpr int CELLS_PER_REV = 250'000 / (RPM / 60); |
| 128 | + |
| 129 | +// format track: 0xaa (2), 0xaa (48), 12*sector |
| 130 | +// 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 |
| 131 | +// write sector: (after sector_sync, 0xdd) sector_header (2+14), payload (416), sector_footer (11) |
| 132 | + |
| 133 | +// from write_format, write_sector_data_header_data_footer |
| 134 | +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*/; |
| 135 | +static constexpr int raw_track_size = 2/*0xaa*/ + 48/*0xaa*/ + SECTORS_PER_TRACK * raw_sector_size; |
| 136 | + |
| 137 | +int lw30_format::identify(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants) const |
| 138 | +{ |
| 139 | + uint64_t size = 0; |
| 140 | + io.length(size); |
| 141 | + if(size == TRACKS_PER_DISK * SECTORS_PER_TRACK * SECTOR_SIZE) |
| 142 | + return 50; // identified by size |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +bool lw30_format::load(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants, floppy_image *image) const |
| 148 | +{ |
| 149 | + uint8_t trackdata[SECTORS_PER_TRACK * SECTOR_SIZE], rawdata[CELLS_PER_REV / 8]; |
| 150 | + memset(rawdata, 0xaa, sizeof(rawdata)); |
| 151 | + for(int track = 0; track < TRACKS_PER_DISK; track++) { |
| 152 | + size_t actual{}; |
| 153 | + io.read_at(track * SECTORS_PER_TRACK * SECTOR_SIZE, trackdata, SECTORS_PER_TRACK * SECTOR_SIZE, actual); |
| 154 | + if(actual != SECTORS_PER_TRACK * SECTOR_SIZE) |
| 155 | + return false; |
| 156 | + size_t i = 0; |
| 157 | + for(int x = 0; x < 2 + 48; x++) |
| 158 | + rawdata[i++] = 0xaa; |
| 159 | + auto interleave_offset = (track % 4) * 4; |
| 160 | + for(size_t s = interleave_offset; s < interleave_offset + SECTORS_PER_TRACK; s++) { |
| 161 | + auto sector = sector_interleave1[s % SECTORS_PER_TRACK] - 1; |
| 162 | + // according to check_track_and_sector |
| 163 | + for(const auto& d : sector_prefix) // 8 bytes |
| 164 | + rawdata[i++] = d; |
| 165 | + rawdata[i++] = sync_table[track] & 0xff; |
| 166 | + rawdata[i++] = sync_table[track] >> 8; |
| 167 | + rawdata[i++] = sync_table[sector] & 0xff; |
| 168 | + rawdata[i++] = sync_table[sector] >> 8; |
| 169 | + rawdata[i++] = 0xdd; |
| 170 | + for(const auto& d : sector_header) // 16 bytes |
| 171 | + rawdata[i++] = d; |
| 172 | + auto payload = gcr_encode_and_checksum(trackdata + sector * SECTOR_SIZE); // 256 -> 416 bytes |
| 173 | + for(const auto &d : payload) |
| 174 | + rawdata[i++] = d; |
| 175 | + for(const auto &d : sector_footer) // 11 bytes |
| 176 | + rawdata[i++] = d; |
| 177 | + for(int x = 0; x < 42; x++) |
| 178 | + rawdata[i++] = 0xaa; |
| 179 | + } |
| 180 | + assert(i == raw_track_size); |
| 181 | + assert(i <= CELLS_PER_REV / 8); |
| 182 | + generate_track_from_bitstream(track, 0, rawdata, CELLS_PER_REV, image); |
| 183 | + } |
| 184 | + |
| 185 | + image->set_variant(floppy_image::SSDD); |
| 186 | + |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +const char *lw30_format::name() const |
| 191 | +{ |
| 192 | + return "lw30"; |
| 193 | +} |
| 194 | + |
| 195 | +const char *lw30_format::description() const |
| 196 | +{ |
| 197 | + return "Brother LW-30 floppy disk image"; |
| 198 | +} |
| 199 | + |
| 200 | +const char *lw30_format::extensions() const |
| 201 | +{ |
| 202 | + return "img"; |
| 203 | +} |
| 204 | + |
| 205 | +bool lw30_format::supports_save() const |
| 206 | +{ |
| 207 | + // TODO |
| 208 | + return false; |
| 209 | +} |
| 210 | + |
| 211 | +const lw30_format FLOPPY_LW30_FORMAT; |
0 commit comments