2021-01-03 22:59:42 +00:00
|
|
|
import { explodeSector16, PO } from './format_utils';
|
2019-10-02 02:56:10 +00:00
|
|
|
import { bytify } from '../util';
|
2021-07-07 00:04:02 +00:00
|
|
|
import type { byte } from '../types';
|
|
|
|
import { NibbleDisk, DiskOptions, ENCODING_NIBBLE } from './types';
|
2019-10-02 02:56:10 +00:00
|
|
|
|
2021-01-03 22:59:42 +00:00
|
|
|
/**
|
|
|
|
* Returns a `Disk` object from ProDOS-ordered image data.
|
2021-07-07 00:04:02 +00:00
|
|
|
* @param options the disk image and options
|
|
|
|
* @returns A nibblized disk
|
2021-01-03 22:59:42 +00:00
|
|
|
*/
|
2021-07-07 00:04:02 +00:00
|
|
|
export default function createDiskFromProDOS(options: DiskOptions) {
|
2021-10-02 18:45:09 +00:00
|
|
|
const { data, name, side, rawData, volume, readOnly } = options;
|
2021-07-07 00:04:02 +00:00
|
|
|
const disk: NibbleDisk = {
|
2019-10-02 02:56:10 +00:00
|
|
|
format: 'nib',
|
2021-07-07 00:04:02 +00:00
|
|
|
encoding: ENCODING_NIBBLE,
|
2019-10-02 02:56:10 +00:00
|
|
|
name,
|
2021-10-02 18:45:09 +00:00
|
|
|
side,
|
2019-10-02 02:56:10 +00:00
|
|
|
volume: volume || 254,
|
|
|
|
tracks: [],
|
|
|
|
readOnly: readOnly || false,
|
|
|
|
};
|
|
|
|
|
2021-07-07 00:04:02 +00:00
|
|
|
for (let physical_track = 0; physical_track < 35; physical_track++) {
|
|
|
|
let track: byte[] = [];
|
|
|
|
for (let physical_sector = 0; physical_sector < 16; physical_sector++) {
|
2021-01-03 22:59:42 +00:00
|
|
|
const prodos_sector = PO[physical_sector];
|
2021-07-07 00:04:02 +00:00
|
|
|
let sector;
|
2019-10-02 02:56:10 +00:00
|
|
|
if (rawData) {
|
2021-07-07 00:04:02 +00:00
|
|
|
const off = (16 * physical_track + prodos_sector) * 256;
|
2019-10-02 02:56:10 +00:00
|
|
|
sector = new Uint8Array(rawData.slice(off, off + 256));
|
2021-07-07 00:04:02 +00:00
|
|
|
} else if (data) {
|
2021-01-03 22:59:42 +00:00
|
|
|
sector = data[physical_track][prodos_sector];
|
2021-07-07 00:04:02 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Requires data or rawData');
|
2019-10-02 02:56:10 +00:00
|
|
|
}
|
|
|
|
track = track.concat(
|
2021-01-03 22:59:42 +00:00
|
|
|
explodeSector16(volume, physical_track, physical_sector, sector)
|
2019-10-02 02:56:10 +00:00
|
|
|
);
|
|
|
|
}
|
2021-01-03 22:59:42 +00:00
|
|
|
disk.tracks[physical_track] = bytify(track);
|
2019-10-02 02:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return disk;
|
|
|
|
}
|