Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 60x 60x 60x 60x 60x 60x 60x 60x 60x 2x 90x 13x 22x 75x 76x 6x 6x 12x 12x 12x 12x 6x 48x 12x 12x 1x 12x 1x 12x 12x 86x 73x 109x 109x 104x 102x 99x 98x 90x 75x 75x 75x 90x 90x 90x 90x 90x 90x 90x 2x 2x 2x 90x 90x | // Floppy disc assorted utils.
import {
Disc,
DiscConfig,
DiscLayout,
loadAdf,
loadSsd,
sniffDfsLayout,
sniffSurfaceLayout,
toSsdOrDsd,
} from "./disc.js";
import { loadHfe, sniffHfeLayout, toHfe } from "./disc-hfe.js";
import { stringToUint8Array } from "./binary.js";
import { loadData } from "./loader.js";
export function load(name) {
console.log("Loading disc from " + name);
return loadData(name);
}
/**
* Class representing a disc format type with associated loader and saver functions
*/
export class DiscType {
/**
* Create a new disc type.
* @param {Object} [options] - Configuration options for this disc type.
* @param {string} options.extension - File extension for this disc type (e.g. ".ssd", ".hfe").
* @param {function(Disc, Uint8Array, function?): Disc} options.loader - Function to load this disc type.
* @param {function(Disc): Uint8Array} options.saver - Function to save this disc type.
* @param {function(Uint8Array, string): void|null} [options.nameSetter] - Function to set the name/label in the disc image, or null if not supported.
* @param {function(Uint8Array): {is40Track: boolean, reason: string}} [options.layoutSniffer] - Function to tell a 40 track image from an 80 track one, for formats that carry the evidence.
* @param {boolean} [options.isFluxImage] - Whether the image is a picture of the surface rather than a list of the sectors on it.
* @param {boolean} [options.isDoubleSided] - Whether the disc format is double-sided.
* @param {boolean} [options.isDoubleDensity] - Whether the disc format is double density.
* @param {number|undefined} [options.byteSize] - The size in bytes of this disc format, or undefined if variable.
*/
constructor({
extension,
loader,
saver,
nameSetter = null,
layoutSniffer,
isFluxImage,
isDoubleSided,
isDoubleDensity,
byteSize,
} = {}) {
this._extension = extension;
this._loader = loader;
this._saver = saver;
this._nameSetter = nameSetter;
this._layoutSniffer = layoutSniffer;
this._isFluxImage = isFluxImage;
this._isDoubleSided = isDoubleSided;
this._isDoubleDensity = isDoubleDensity;
this._byteSize = byteSize;
}
/**
* Get the file extension for this disc type
* @returns {string} File extension including dot
*/
get name() {
return this._extension;
}
/**
* Get the file extension for this disc type
* @returns {string} File extension including dot
*/
get extension() {
return this._extension;
}
/**
* Get the loader function for this disc type
* @returns {function} Function that loads the disc
*/
get loader() {
return this._loader;
}
/**
* Get the saver function for this disc type
* @returns {function} Function that saves the disc
*/
get saver() {
return this._saver;
}
/**
* Get whether this disc format is double-sided
* @returns {boolean} True if double-sided, false otherwise
*/
get isDoubleSided() {
return this._isDoubleSided;
}
/**
* Get whether this disc format is double density
* @returns {boolean} True if double density, false otherwise
*/
get isDoubleDensity() {
return this._isDoubleDensity;
}
/**
* Get the size in bytes of this disc format
* @returns {number|undefined} The size in bytes, or undefined if variable
*/
get byteSize() {
return this._byteSize;
}
/**
* Whether this disc format supports setting a catalogue name
* @returns {boolean} True if a name setter function exists
*/
get supportsCatalogue() {
return this._nameSetter !== null;
}
/**
* Whether the image holds a picture of the surface, so that where its tracks sit is a fact
* about the disc rather than a decision the loader made.
* @returns {boolean}
*/
get isFluxImage() {
return !!this._isFluxImage;
}
/**
* What an image of this type says about its track layout.
* @param {Uint8Array} data - The disc image data
* @returns {?{is40Track: boolean, reason: string}} null for formats that cannot say
*/
sniffLayout(data) {
return this._layoutSniffer ? this._layoutSniffer(data) : null;
}
/**
* Sets the disc name in the disc data using the format-specific setter
* @param {Uint8Array} data - The disc data to modify
* @param {string} name - The name to set
* @throws {Error} If the disc format doesn't support setting a name
*/
setDiscName(data, name) {
Iif (!this.supportsCatalogue) {
throw new Error(`Cannot set disc name for ${this._extension} format`);
}
this._nameSetter(data, name);
}
}
// Standard sizes
const SsdByteSize = 80 * 10 * 256; // 80 tracks, 10 sectors, 256 bytes/sector
const DsdByteSize = SsdByteSize * 2; // Double-sided
// ADFS comes in three sizes: S is 40 tracks on one side, M is 80 on one, L is 80 on both.
const AdfsMediumByteSize = 80 * 16 * 256;
const AdfsLargeByteSize = AdfsMediumByteSize * 2;
/**
* Set the name in a DFS disc image (SSD/DSD format)
* @param {Uint8Array} data - The disc data to modify
* @param {string} name - The name to set (up to 8 characters)
*/
function setDfsDiscName(data, name) {
for (let i = 0; i < 8; ++i) {
data[i] = i < name.length ? name.charCodeAt(i) & 0xff : 0x20; // padded with spaces
}
}
// HFE disc type - variable size
const hfeDiscType = new DiscType({
extension: ".hfe",
loader: loadHfe,
saver: toHfe,
layoutSniffer: sniffHfeLayout,
isFluxImage: true,
isDoubleSided: true,
isDoubleDensity: true,
});
// ADFS (Large) discs are double density, double sided
const adlDiscType = new DiscType({
extension: ".adl",
loader: (disc, data, _onChange) => {
// TODO(#822) handle onChange
return loadAdf(disc, data, true);
},
saver: (_data) => {
throw new Error("ADL unsupported");
},
isDoubleSided: true,
isDoubleDensity: true,
byteSize: AdfsLargeByteSize,
});
// ADFS M and S: single sided, and sized as the larger of the two.
const adfDiscType = new DiscType({
extension: ".adf",
loader: (disc, data, _onChange) => {
// TODO(#822) handle onChange
return loadAdf(disc, data, false);
},
saver: (_data) => {
throw new Error("ADF unsupported");
},
isDoubleSided: false,
isDoubleDensity: true,
byteSize: AdfsMediumByteSize,
});
// DSD (Double-sided disc)
const dsdDiscType = new DiscType({
extension: ".dsd",
loader: (disc, data, onChange) => loadSsd(disc, data, true, onChange),
saver: toSsdOrDsd,
nameSetter: setDfsDiscName,
layoutSniffer: (data) => sniffDfsLayout(data, true),
isDoubleSided: true,
isDoubleDensity: false,
byteSize: DsdByteSize,
});
// SSD (Single-sided disc)
const ssdDiscType = new DiscType({
extension: ".ssd",
loader: (disc, data, onChange) => loadSsd(disc, data, false, onChange),
saver: toSsdOrDsd,
nameSetter: setDfsDiscName,
layoutSniffer: (data) => sniffDfsLayout(data, false),
isDoubleSided: false,
isDoubleDensity: false,
byteSize: SsdByteSize,
});
/**
* Determine the disc type based on the file name extension
* @param {string} name - The file name with extension
* @returns {DiscType} The appropriate disc type handler
*/
export function guessDiscTypeFromName(name) {
const lowerName = name.toLowerCase();
if (lowerName.endsWith(".hfe")) return hfeDiscType;
if (lowerName.endsWith(".adl")) return adlDiscType;
if (lowerName.endsWith(".adf") || lowerName.endsWith(".adm")) return adfDiscType;
if (lowerName.endsWith(".dsd")) return dsdDiscType;
return ssdDiscType;
}
/**
* @param {DiscType} discType
* @param {Uint8Array} data
* @param {string} name
* @param {string} layout - one of DiscLayout
* @returns {boolean} whether to lay the image out for a 40 track drive
*/
function is40TrackLayout(discType, data, name, layout) {
if (layout !== DiscLayout.auto) return layout === DiscLayout.expanded40;
const sniffed = discType.sniffLayout(data);
Iif (!sniffed) return false;
console.log(`${name} loaded as ${sniffed.is40Track ? "40" : "80"} track: ${sniffed.reason}`);
return sniffed.is40Track;
}
/**
* Create a disc object of the appropriate type based on the file name
* @param {string} name - The file name with extension
* @param {string|Uint8Array} stringData - The disc image data as string or Uint8Array
* @param {function(Uint8Array): void} [onChange] - called when disc content changes
* @param {string} [layout] - one of DiscLayout; by default the image is asked what it is
* @returns {Disc} The loaded disc object
*/
export function discFor(name, stringData, onChange, layout = DiscLayout.auto) {
const data = typeof stringData !== "string" ? stringData : stringToUint8Array(stringData);
const discType = guessDiscTypeFromName(name);
const config = new DiscConfig();
config.expandTo80 = is40TrackLayout(discType, data, name, layout);
const disc = discType.loader(new Disc(true, config, name), data, onChange);
// A flux image of a 40 track disc read in an 80 track drive already holds it spread across the
// surface, so nothing needs moving and only the drive needs telling to step twice.
if (layout === DiscLayout.auto && discType.isFluxImage && !disc.is40Track) {
const sniffed = sniffSurfaceLayout(disc);
disc.is40Track = sniffed.is40Track;
console.log(`${name} surface reads as ${sniffed.is40Track ? "40" : "80"} track: ${sniffed.reason}`);
}
disc.setOriginalImageCrc32(data instanceof Uint8Array ? data : new Uint8Array(data));
return disc;
}
|