apple2js/test/js/formats/do.spec.ts
Ian Flanigan 72ecce113a
DOS 3.3 image format tests and fixes (#49)
* Adds an initial test for DOS format (.do) files

* Fix physical sector order when nibblizing DOS 3.3 ordered images

Before, when `.dsk` or `.do` images were nibblized, the resulting
track had the sectors in the wrong physical layout.

Now the nibblized track has the correct physical layout (all sectors
in order) which results in the correct DOS 3.3 layout as well.

There is also a test that verifies the order.

* Add another test for a non-zero sector

The new test checks that the values in physical sector 1 are those for
DOS sector 7.

* Add test for all physical sectors on all tracks

This change also removes a few stray console.log calls in the test.
2020-12-29 06:40:40 -08:00

338 lines
12 KiB
TypeScript

import DOS from '../../../js/formats/do';
import { memory } from '../../../js/types';
import { BYTES_BY_SECTOR, BYTES_BY_TRACK } from './testdata/16sector';
function skipGap(track: memory, start: number = 0): number {
const end = start + 0x100; // no gap is this big
let i = start;
while (i < end && track[i] == 0xFF) {
i++;
}
if (i == end) {
fail(`found more than 0x100 0xFF bytes after ${start}`);
}
return i;
}
function compareSequences(track: memory, bytes: number[], pos: number): boolean {
for (let i = 0; i < bytes.length; i++) {
if (track[i + pos] != bytes[i]) {
return false;
}
}
return true;
}
function expectSequence(track: memory, pos: number, bytes: number[]): number {
if (!compareSequences(track, bytes, pos)) {
const track_slice = track.slice(pos, Math.min(track.length, pos + bytes.length));
fail(`expected ${bytes} got ${track_slice}`);
}
return pos + bytes.length;
}
function findBytes(track: memory, bytes: number[], start: number = 0): number {
for (let i = start; i < track.length; i++) {
if (compareSequences(track, bytes, i)) {
return i + bytes.length;
}
}
return -1;
}
describe('compareSequences', () => {
it('matches at pos 0', () => {
expect(
compareSequences([0x01, 0x02, 0x03], [0x01, 0x02, 0x03], 0)
).toBeTruthy();
});
it('matches at pos 1', () => {
expect(
compareSequences([0x00, 0x01, 0x02, 0x03], [0x01, 0x02, 0x03], 1)
).toBeTruthy();
});
});
describe('DOS format', () => {
it('is callable', () => {
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
expect(disk).not.toBeNull();
});
it('has correct number of tracks', () => {
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
expect(disk.tracks.length).toEqual(35);
});
it('has correct number of bytes in track 0', () => {
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
expect(disk.tracks[0].length).toEqual(6632);
});
it('has correct number of bytes in all tracks', () => {
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
// Track 0 is slightly longer for some reason.
expect(disk.tracks[0].length).toEqual(6632);
for (let i = 1; i < disk.tracks.length; i++) {
expect(disk.tracks[i].length).toEqual(6602);
}
});
it('has correct GAP 1', () => {
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
// From Beneith Apple DOS, GAP 1 should have 12-85 0xFF bytes
const track = disk.tracks[0];
let numFF = 0;
while (track[numFF] == 0xFF && numFF < 0x100) {
numFF++;
}
expect(numFF).toBeGreaterThanOrEqual(40);
expect(numFF).toBeLessThanOrEqual(128);
});
it('has correct Address Field for track 0, sector 0', () => {
// _Beneath Apple DOS_, TRACK FORMATTING, p. 3-12
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
const track = disk.tracks[0];
let i = skipGap(track);
// prologue
i = expectSequence(track, i, [0xD5, 0xAA, 0x96]);
// volume 10 = 0b00001010
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101010);
// track 0 = 0b00000000
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101010);
// sector 0 = 0b00000000
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101010);
// checksum = 0b00000101
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101010);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Data Field for track 0, sector 0 (BYTES_BY_TRACK)', () => {
// _Beneath Apple DOS_, DATA FIELD ENCODING, pp. 3-13 to 3-21
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
const track: memory = disk.tracks[0];
// skip to the first address epilogue
let i = findBytes(track, [0xDE, 0xAA, 0xEB]);
expect(i).toBeGreaterThan(50);
i = skipGap(track, i);
// prologue
i = expectSequence(track, i, [0xD5, 0xAA, 0xAD]);
// data (all zeros, which is 0x96 with 6 and 2 encoding)
for (let j = 0; j < 342; j++) {
expect(track[i++]).toBe(0x96);
}
// checksum (also zero)
expect(track[i++]).toBe(0x96);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Address Field for track 0, sector 1', () => {
// _Beneath Apple DOS_, TRACK FORMATTING, p. 3-12
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
const track = disk.tracks[0];
// first sector prologue
let i = findBytes(track, [0xD5, 0xAA, 0x96]);
// second sector prologue
i = findBytes(track, [0xD5, 0xAA, 0x96], i);
// volume 10 = 0b00001010
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101010);
// track 0 = 0b00000000
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101010);
// sector 1 = 0b00000001
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101011);
// checksum = 0b00000101
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101011);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Data Field for track 0, sector 1 (BYTES_BY_SECTOR)', () => {
// _Beneath Apple DOS_, DATA FIELD ENCODING, pp. 3-13 to 3-21
const disk = DOS({
name: 'test disk',
data: BYTES_BY_SECTOR,
volume: 10,
readOnly: true,
});
const track: memory = disk.tracks[0];
// First data field prologue
let i = findBytes(track, [0xD5, 0xAA, 0xAD]);
// Second data field prologue
i = findBytes(track, [0xD5, 0xAA, 0xAD], i);
// Sector 1 is DOS sector 7.
// In 6 x 2 encoding, the lowest 2 bits of all the bytes come first.
// 0x07 is 0b00000111, so the lowest two bits are 0b11, reversed and
// repeated would be 0b111111 (3F -> 0xFF), but since each byte is
// XOR'd with the previous, this means there are 85 0b00000000 (00 ->
// 0x96) bytes.
expect(track[i++]).toBe(0xFF);
for (let j = 0; j < 85; j++) {
expect(track[i++]).toBe(0x96);
}
// Next we get 256 instances of the top bits, 0b000001. Again, with
// the XOR, this means one 0b000001 XOR 0b111111 = 0b111110
// (3E -> 0xFE) followed by 255 0b0000000 (00 -> 0x96).
expect(track[i++]).toBe(0xFE);
for (let j = 0; j < 255; j++) {
expect(track[i++]).toBe(0x96);
}
// checksum
expect(track[i++]).toBe(0x97);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Address Field for track 1, sector 0', () => {
// _Beneath Apple DOS_, TRACK FORMATTING, p. 3-12
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
const track = disk.tracks[1];
let i = skipGap(track);
// prologue
i = expectSequence(track, i, [0xD5, 0xAA, 0x96]);
// volume 10 = 0b00001010
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101010);
// track 1 = 0b00000001
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101011);
// sector 0 = 0b00000000
expect(track[i++]).toBe(0b10101010);
expect(track[i++]).toBe(0b10101010);
// checksum = 0b00000100
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101011);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Data Field for track 1, sector 0 (BYTES_BY_TRACK)', () => {
// _Beneath Apple DOS_, DATA FIELD ENCODING, pp. 3-13 to 3-21
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
const track: memory = disk.tracks[1];
let i = findBytes(track, [0xDE, 0xAA, 0xEB]);
expect(i).toBeGreaterThan(50);
i = skipGap(track, i);
// prologue
i = expectSequence(track, i, [0xD5, 0xAA, 0xAD]);
// In 6 x 2 encoding, the lowest 2 bits of all the bytes come first.
// This would normally mean 86 instances of 0b101010 (2A -> 0xE6),
// but each byte is XOR'd with the previous. Since all of the bits
// are the same, this means there are 85 0b000000 (00 -> 0x96).
expect(track[i++]).toBe(0xE6);
for (let j = 0; j < 85; j++) {
expect(track[i++]).toBe(0x96);
}
// Next we get 256 instances of the top bits, 0b000000. Again, with
// the XOR, this means one 0x101010 (2A -> 0xE6) followed by 255
// 0b0000000 (00 -> 0x96).
expect(track[i++]).toBe(0xE6);
for (let j = 0; j < 255; j++) {
expect(track[i++]).toBe(0x96);
}
// checksum (also zero)
expect(track[i++]).toBe(0x96);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
});
it('has correct Address Fields for all tracks', () => {
// _Beneath Apple DOS_, TRACK FORMATTING, p. 3-12
const disk = DOS({
name: 'test disk',
data: BYTES_BY_TRACK,
volume: 10,
readOnly: true,
});
for (let t = 0; t < disk.tracks.length; t++) {
// We essentially seek through the track for the Address Fields
const track = disk.tracks[t];
let i = findBytes(track, [0xD5, 0xAA, 0x96]);
for (let s = 0; s <= 15; s++) {
// volume 10 = 0b00001010
expect(track[i++]).toBe(0b10101111);
expect(track[i++]).toBe(0b10101010);
// convert track to 4x4 encoding
const track4x4XX = ((t & 0b10101010) >> 1) | 0b10101010;
const track4x4YY = (t & 0b01010101) | 0b10101010;
expect(track[i++]).toBe(track4x4XX);
expect(track[i++]).toBe(track4x4YY);
// convert sector to 4x4 encoding
const sector4x4XX = ((s & 0b10101010) >> 1) | 0b10101010;
const sector4x4YY = (s & 0b01010101) | 0b10101010;
expect(track[i++]).toBe(sector4x4XX);
expect(track[i++]).toBe(sector4x4YY);
// checksum
expect(track[i++]).toBe(0b10101111 ^ track4x4XX ^ sector4x4XX);
expect(track[i++]).toBe(0b10101010 ^ track4x4YY ^ sector4x4YY);
// epilogue
i = expectSequence(track, i, [0xDE, 0xAA, 0xEB]);
// next sector
i = findBytes(track, [0xD5, 0xAA, 0x96], i);
}
}
});
});