2021-07-07 00:04:02 +00:00
|
|
|
import { NibbleDisk, DiskOptions, ENCODING_NIBBLE } from './types';
|
|
|
|
import { memory } from '../types';
|
|
|
|
|
2021-01-03 23:00:40 +00:00
|
|
|
/**
|
|
|
|
* Returns a `Disk` object from raw nibble image data.
|
2021-07-07 00:04:02 +00:00
|
|
|
* @param options the disk image and options
|
|
|
|
* @returns A nibblized disk
|
2021-01-03 23:00:40 +00:00
|
|
|
*/
|
2021-07-07 00:04:02 +00:00
|
|
|
export default function createDiskFromNibble(options: DiskOptions): NibbleDisk {
|
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,
|
|
|
|
readOnly: readOnly || false,
|
2021-07-07 00:04:02 +00:00
|
|
|
tracks: []
|
2019-10-02 02:56:10 +00:00
|
|
|
};
|
|
|
|
|
2021-07-07 00:04:02 +00:00
|
|
|
for (let t = 0; t < 35; t++) {
|
|
|
|
let track: memory;
|
2019-10-02 02:56:10 +00:00
|
|
|
if (rawData) {
|
2021-07-07 00:04:02 +00:00
|
|
|
const off = t * 0x1a00;
|
|
|
|
track = new Uint8Array(rawData.slice(off, off + 0x1a00));
|
|
|
|
} else if (data) {
|
|
|
|
track = data[t][0];
|
2019-10-02 02:56:10 +00:00
|
|
|
} else {
|
2021-07-07 00:04:02 +00:00
|
|
|
throw new Error('Requires data or rawData');
|
2019-10-02 02:56:10 +00:00
|
|
|
}
|
|
|
|
disk.tracks[t] = track;
|
|
|
|
}
|
|
|
|
|
|
|
|
return disk;
|
|
|
|
}
|