2021-03-13 21:18:32 +00:00
|
|
|
import { debug, toHex } from '../util';
|
|
|
|
import { rom as smartPortRom } from '../roms/cards/smartport';
|
|
|
|
import { Card, Restorable, byte, word, rom } from '../types';
|
2023-11-24 14:45:55 +00:00
|
|
|
import {
|
|
|
|
MassStorage,
|
|
|
|
BlockDisk,
|
|
|
|
ENCODING_BLOCK,
|
|
|
|
BlockFormat,
|
|
|
|
MassStorageData,
|
|
|
|
DiskFormat,
|
|
|
|
} from '../formats/types';
|
2023-11-23 00:28:40 +00:00
|
|
|
import { CPU6502, CpuState, flags } from '@whscullin/cpu6502';
|
2023-11-24 14:45:55 +00:00
|
|
|
import {
|
|
|
|
create2MGFromBlockDisk,
|
|
|
|
HeaderData,
|
|
|
|
read2MGHeader,
|
|
|
|
} from '../formats/2mg';
|
2021-07-07 00:04:02 +00:00
|
|
|
import createBlockDisk from '../formats/block';
|
2022-06-05 17:57:04 +00:00
|
|
|
import { DriveNumber } from '../formats/types';
|
2021-03-13 21:18:32 +00:00
|
|
|
|
2021-12-23 04:47:36 +00:00
|
|
|
const ID = 'SMARTPORT.J.S';
|
|
|
|
|
2021-03-13 21:18:32 +00:00
|
|
|
export interface SmartPortState {
|
2022-05-10 15:04:20 +00:00
|
|
|
disks: BlockDisk[];
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SmartPortOptions {
|
|
|
|
block: boolean;
|
|
|
|
}
|
|
|
|
|
2022-06-05 17:57:04 +00:00
|
|
|
export interface Callbacks {
|
2022-09-19 07:07:27 +00:00
|
|
|
driveLight: (driveNo: DriveNumber, on: boolean) => void;
|
|
|
|
dirty: (driveNo: DriveNumber, dirty: boolean) => void;
|
|
|
|
label: (driveNo: DriveNumber, name?: string, side?: string) => void;
|
2022-06-05 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 21:18:32 +00:00
|
|
|
class Address {
|
|
|
|
lo: byte;
|
|
|
|
hi: byte;
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
constructor(
|
|
|
|
private cpu: CPU6502,
|
|
|
|
a: byte,
|
|
|
|
b?: byte
|
|
|
|
) {
|
2021-03-13 21:18:32 +00:00
|
|
|
if (b === undefined) {
|
|
|
|
this.lo = a & 0xff;
|
|
|
|
this.hi = a >> 8;
|
|
|
|
} else {
|
|
|
|
this.lo = a;
|
|
|
|
this.hi = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loByte() {
|
|
|
|
return this.lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
hiByte() {
|
|
|
|
return this.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
inc(val: byte) {
|
2023-11-24 14:45:55 +00:00
|
|
|
return new Address(
|
|
|
|
this.cpu,
|
|
|
|
(((this.hi << 8) | this.lo) + val) & 0xffff
|
|
|
|
);
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readByte() {
|
|
|
|
return this.cpu.read(this.hi, this.lo);
|
|
|
|
}
|
|
|
|
|
|
|
|
readWord() {
|
|
|
|
const readLo = this.readByte();
|
|
|
|
const readHi = this.inc(1).readByte();
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
return (readHi << 8) | readLo;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readAddress() {
|
|
|
|
const readLo = this.readByte();
|
|
|
|
const readHi = this.inc(1).readByte();
|
|
|
|
|
|
|
|
return new Address(this.cpu, readLo, readHi);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeByte(val: byte) {
|
|
|
|
this.cpu.write(this.hi, this.lo, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeWord(val: word) {
|
|
|
|
this.writeByte(val & 0xff);
|
|
|
|
this.inc(1).writeByte(val >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeAddress(val: Address) {
|
|
|
|
this.writeByte(val.loByte());
|
|
|
|
this.inc(1).writeByte(val.hiByte());
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return '$' + toHex(this.hi) + toHex(this.lo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProDOS zero page locations
|
|
|
|
|
|
|
|
const COMMAND = 0x42;
|
|
|
|
const UNIT = 0x43;
|
|
|
|
const ADDRESS_LO = 0x44;
|
|
|
|
// const ADDRESS_HI = 0x45;
|
|
|
|
const BLOCK_LO = 0x46;
|
|
|
|
// const BLOCK_HI = 0x47;
|
|
|
|
|
2021-07-10 18:44:10 +00:00
|
|
|
// const IO_ERROR = 0x27;
|
|
|
|
const NO_DEVICE_CONNECTED = 0x28;
|
2023-11-24 14:45:55 +00:00
|
|
|
const WRITE_PROTECTED = 0x2b;
|
|
|
|
const DEVICE_OFFLINE = 0x2f;
|
2021-07-10 18:44:10 +00:00
|
|
|
// const VOLUME_DIRECTORY_NOT_FOUND = 0x45;
|
|
|
|
// const NOT_A_PRODOS_DISK = 0x52;
|
|
|
|
// const VOLUME_CONTROL_BLOCK_FULL = 0x55;
|
|
|
|
// const BAD_BUFFER_ADDRESS = 0x56;
|
|
|
|
// const DUPLICATE_VOLUME_ONLINE = 0x57;
|
|
|
|
|
2021-12-23 04:47:36 +00:00
|
|
|
// Type: Device
|
|
|
|
// $00: Memory Expansion Card (RAM disk)
|
|
|
|
// $01: 3.5" disk
|
|
|
|
// $02: ProFile-type hard disk
|
|
|
|
// $03: Generic SCSI
|
|
|
|
// $04: ROM disk
|
|
|
|
// $05: SCSI CD-ROM
|
|
|
|
// $06: SCSI tape or other SCSI sequential device
|
|
|
|
// $07: SCSI hard disk
|
|
|
|
const DEVICE_TYPE_SCSI_HD = 0x07;
|
|
|
|
// $08: Reserved
|
|
|
|
// $09: SCSI printer
|
|
|
|
// $0A: 5-1/4" disk
|
|
|
|
// $0B: Reserved
|
|
|
|
// $0C: Reserved
|
|
|
|
// $0D: Printer
|
|
|
|
// $0E: Clock
|
|
|
|
// $0F: Modem
|
2023-11-24 14:45:55 +00:00
|
|
|
export default class SmartPort
|
|
|
|
implements Card, MassStorage<BlockFormat>, Restorable<SmartPortState>
|
|
|
|
{
|
2021-03-13 21:18:32 +00:00
|
|
|
private rom: rom;
|
2021-07-07 00:04:02 +00:00
|
|
|
private disks: BlockDisk[] = [];
|
2022-06-05 17:57:04 +00:00
|
|
|
private busy: boolean[] = [];
|
|
|
|
private busyTimeout: ReturnType<typeof setTimeout>[] = [];
|
Split disk data out into its own record (#158)
* Harmonize drive and disk type hierarchies
Before, the `XXXDrive` and `XXXDisk` type hierarchies were similar,
but not exactly the same. For example, `encoding` and `format` were
missing on some `XXXDisk` types where they existed on the `XXXDrive`
type. This change attempts to bring the hierarchies closer together.
However, the biggest visible consequence is the introduction of the
`FLOPPY_FORMATS` array and its associated `FloppyFormat` type. This
replaces `NIBBLE_FORMATS` in most places. A couple of new type guards
for disk formats and disks have been added as well.
All tests pass, everything compiles with no errors, and both WOZ and
nibble format disks load in the emulator.
* Move disk data to a `disk` field in the drive
Before, disk data was mixed in with state about the drive itself (like
track, motor phase, etc.). This made it hard to know exactly what data
was necessary for different image formats.
Now, the disk data is in a `disk` field whose type depends on the
drive type. This makes responisbility a bit easier.
One oddity, though, is that the `Drive` has metadata _and_ the `Disk`
has metadata. When a disk is in the drive, these should be `===`, but
when there is no disk in the drive, obviously only the drive metadata
is set.
All tests pass, everything compiles, and both WOZ and nibble disks
work in the emulator (both preact and classic).
* Squash the `Drive` type hierarchy
Before, the type of the drive depended on the type of the disk in the
drive. Thus, `NibbleDrive` contained a `NibbleDisk` and a `WozDrive`
contained a `WozDisk`. With the extraction of the disk data to a
single field, this type hierarchy makes no sense. Instead, it
suffices to check the type of the disk.
This change removes the `NibbleDrive` and `WozDrive` types and type
guards, checking the disk type where necessary. This change also
introduces the `NoFloppyDisk` type to represent the lack of a
disk. This allows the drive to have metadata, for one.
All tests pass, everything compiles, and both WOZ and nibble disks
work locally.
* Use more destructuring assignment
Now, more places use constructs like:
```TypeScript
const { metadata, readOnly, track, head, phase, dirty } = drive;
return {
disk: getDiskState(drive.disk),
metadata: {...metadata},
readOnly,
track,
head,
phase,
dirty,
};
```
* Remove the `Disk` object from the `Drive` object
This change splits out the disk objects into a record parallel to the
drive objects. The idea is that the `Drive` structure becomes a
representation of the state of the drive that is separate from the
disk image actually in the drive. This helps in an upcoming
refactoring.
This also changes the default empty disks to be writable. While odd,
the write protect switch should be in the "off" position since there
is no disk pressing on it.
Finally, `insertDisk` now resets the head position to 0 since there is
no way of preserving the head position across disks. (Even in the real
world, the motor-off delay plus spindle spin-down would make it
impossible to know the disk head position with any accuracy.)
2022-09-17 13:41:35 +00:00
|
|
|
private ext: DiskFormat[] = [];
|
2022-07-23 19:00:38 +00:00
|
|
|
private metadata: Array<HeaderData | null> = [];
|
2022-06-05 17:57:04 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private cpu: CPU6502,
|
|
|
|
private callbacks: Callbacks | null,
|
|
|
|
options: SmartPortOptions
|
|
|
|
) {
|
2021-03-13 21:18:32 +00:00
|
|
|
if (options?.block) {
|
2022-05-18 15:19:45 +00:00
|
|
|
const dumbPortRom = new Uint8Array(smartPortRom);
|
2023-11-24 14:45:55 +00:00
|
|
|
dumbPortRom[0x07] = 0x3c;
|
2021-03-13 21:18:32 +00:00
|
|
|
this.rom = dumbPortRom;
|
|
|
|
debug('DumbPort card');
|
|
|
|
} else {
|
|
|
|
debug('SmartPort card');
|
2021-03-14 00:08:24 +00:00
|
|
|
this.rom = smartPortRom;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 02:08:28 +00:00
|
|
|
private debug(..._args: unknown[]) {
|
2021-03-13 21:18:32 +00:00
|
|
|
// debug.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
private driveLight(driveNo: DriveNumber) {
|
|
|
|
if (!this.busy[driveNo]) {
|
|
|
|
this.busy[driveNo] = true;
|
|
|
|
this.callbacks?.driveLight(driveNo, true);
|
2022-06-05 17:57:04 +00:00
|
|
|
}
|
2022-09-19 07:07:27 +00:00
|
|
|
clearTimeout(this.busyTimeout[driveNo]);
|
|
|
|
this.busyTimeout[driveNo] = setTimeout(() => {
|
|
|
|
this.busy[driveNo] = false;
|
|
|
|
this.callbacks?.driveLight(driveNo, false);
|
2022-06-05 17:57:04 +00:00
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:18:32 +00:00
|
|
|
/*
|
|
|
|
* dumpBlock
|
|
|
|
*/
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
dumpBlock(driveNo: DriveNumber, block: number) {
|
2021-07-07 00:04:02 +00:00
|
|
|
let result = '';
|
|
|
|
let b;
|
|
|
|
let jdx;
|
|
|
|
|
|
|
|
for (let idx = 0; idx < 32; idx++) {
|
2021-03-13 21:18:32 +00:00
|
|
|
result += toHex(idx << 4, 4) + ': ';
|
|
|
|
for (jdx = 0; jdx < 16; jdx++) {
|
2022-09-19 07:07:27 +00:00
|
|
|
b = this.disks[driveNo].blocks[block][idx * 16 + jdx];
|
2022-05-18 15:19:45 +00:00
|
|
|
if (jdx === 8) {
|
2021-03-13 21:18:32 +00:00
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
result += toHex(b) + ' ';
|
|
|
|
}
|
|
|
|
result += ' ';
|
|
|
|
for (jdx = 0; jdx < 16; jdx++) {
|
2022-09-19 07:07:27 +00:00
|
|
|
b = this.disks[driveNo].blocks[block][idx * 16 + jdx] & 0x7f;
|
2022-05-18 15:19:45 +00:00
|
|
|
if (jdx === 8) {
|
2021-03-13 21:18:32 +00:00
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
if (b >= 0x20 && b < 0x7f) {
|
|
|
|
result += String.fromCharCode(b);
|
|
|
|
} else {
|
|
|
|
result += '.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += '\n';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-07 00:04:02 +00:00
|
|
|
|
2021-03-13 21:18:32 +00:00
|
|
|
/*
|
|
|
|
* getDeviceInfo
|
|
|
|
*/
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
getDeviceInfo(state: CpuState, driveNo: DriveNumber) {
|
|
|
|
if (this.disks[driveNo]) {
|
|
|
|
const blocks = this.disks[driveNo].blocks.length;
|
2021-03-13 21:18:32 +00:00
|
|
|
state.x = blocks & 0xff;
|
|
|
|
state.y = blocks >> 8;
|
|
|
|
|
|
|
|
state.a = 0;
|
|
|
|
state.s &= ~flags.C;
|
|
|
|
} else {
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = NO_DEVICE_CONNECTED;
|
2021-03-13 21:18:32 +00:00
|
|
|
state.s |= flags.C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* readBlock
|
|
|
|
*/
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
readBlock(
|
|
|
|
state: CpuState,
|
|
|
|
driveNo: DriveNumber,
|
|
|
|
block: number,
|
|
|
|
buffer: Address
|
|
|
|
) {
|
2022-09-19 07:07:27 +00:00
|
|
|
this.debug(`read drive=${driveNo}`);
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`read buffer=${buffer.toString()}`);
|
|
|
|
this.debug(`read block=$${toHex(block)}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
if (!this.disks[driveNo]?.blocks.length) {
|
|
|
|
debug('Drive', driveNo, 'is empty');
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = DEVICE_OFFLINE;
|
|
|
|
state.s |= flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// debug('read', '\n' + dumpBlock(drive, block));
|
2022-09-19 07:07:27 +00:00
|
|
|
this.driveLight(driveNo);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
for (let idx = 0; idx < 512; idx++) {
|
2022-09-19 07:07:27 +00:00
|
|
|
buffer.writeByte(this.disks[driveNo].blocks[block][idx]);
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer = buffer.inc(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.a = 0;
|
2021-07-10 18:44:10 +00:00
|
|
|
state.s &= ~flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* writeBlock
|
|
|
|
*/
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
writeBlock(
|
|
|
|
state: CpuState,
|
|
|
|
driveNo: DriveNumber,
|
|
|
|
block: number,
|
|
|
|
buffer: Address
|
|
|
|
) {
|
2022-09-19 07:07:27 +00:00
|
|
|
this.debug(`write drive=${driveNo}`);
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`write buffer=${buffer.toString()}`);
|
|
|
|
this.debug(`write block=$${toHex(block)}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
if (!this.disks[driveNo]?.blocks.length) {
|
|
|
|
debug('Drive', driveNo, 'is empty');
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = DEVICE_OFFLINE;
|
|
|
|
state.s |= flags.C;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
if (this.disks[driveNo].readOnly) {
|
|
|
|
debug('Drive', driveNo, 'is write protected');
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = WRITE_PROTECTED;
|
|
|
|
state.s |= flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// debug('write', '\n' + dumpBlock(drive, block));
|
2022-09-19 07:07:27 +00:00
|
|
|
this.driveLight(driveNo);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
for (let idx = 0; idx < 512; idx++) {
|
2022-09-19 07:07:27 +00:00
|
|
|
this.disks[driveNo].blocks[block][idx] = buffer.readByte();
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer = buffer.inc(1);
|
|
|
|
}
|
|
|
|
state.a = 0;
|
2022-06-13 02:39:03 +00:00
|
|
|
state.s &= ~flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* formatDevice
|
|
|
|
*/
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
formatDevice(state: CpuState, driveNo: DriveNumber) {
|
|
|
|
if (!this.disks[driveNo]?.blocks.length) {
|
|
|
|
debug('Drive', driveNo, 'is empty');
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = DEVICE_OFFLINE;
|
|
|
|
state.s |= flags.C;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
if (this.disks[driveNo].readOnly) {
|
|
|
|
debug('Drive', driveNo, 'is write protected');
|
2021-07-10 18:44:10 +00:00
|
|
|
state.a = WRITE_PROTECTED;
|
|
|
|
state.s |= flags.C;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
for (let idx = 0; idx < this.disks[driveNo].blocks.length; idx++) {
|
|
|
|
this.disks[driveNo].blocks[idx] = new Uint8Array();
|
2021-03-13 21:18:32 +00:00
|
|
|
for (let jdx = 0; jdx < 512; jdx++) {
|
2022-09-19 07:07:27 +00:00
|
|
|
this.disks[driveNo].blocks[idx][jdx] = 0;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.a = 0;
|
2021-07-10 18:44:10 +00:00
|
|
|
state.s &= flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private access(off: byte, val: byte) {
|
|
|
|
let result;
|
|
|
|
const readMode = val === undefined;
|
|
|
|
|
|
|
|
switch (off & 0x8f) {
|
|
|
|
case 0x80:
|
|
|
|
if (readMode) {
|
|
|
|
result = 0;
|
|
|
|
for (let idx = 0; idx < this.disks.length; idx++) {
|
|
|
|
result <<= 1;
|
|
|
|
if (this.disks[idx]) {
|
|
|
|
result |= 0x01;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
ioSwitch(off: byte, val: byte) {
|
|
|
|
return this.access(off, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
read(_page: byte, off: byte) {
|
|
|
|
const state = this.cpu.getState();
|
|
|
|
let cmd;
|
|
|
|
let unit;
|
|
|
|
let buffer;
|
|
|
|
let block;
|
|
|
|
const blockOff = this.rom[0xff];
|
|
|
|
const smartOff = blockOff + 3;
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
if (off === blockOff && this.cpu.getSync()) {
|
|
|
|
// Regular block device entry POINT
|
2021-03-13 21:18:32 +00:00
|
|
|
this.debug('block device entry');
|
|
|
|
cmd = this.cpu.read(0x00, COMMAND);
|
|
|
|
unit = this.cpu.read(0x00, UNIT);
|
|
|
|
const bufferAddr = new Address(this.cpu, ADDRESS_LO);
|
|
|
|
const blockAddr = new Address(this.cpu, BLOCK_LO);
|
2023-11-24 14:45:55 +00:00
|
|
|
const drive = unit & 0x80 ? 2 : 1;
|
2021-03-13 21:18:32 +00:00
|
|
|
const driveSlot = (unit & 0x70) >> 4;
|
|
|
|
|
|
|
|
buffer = bufferAddr.readAddress();
|
|
|
|
block = blockAddr.readWord();
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`cmd=${cmd}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
this.debug('unit=$' + toHex(unit));
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`slot=${driveSlot} drive=${drive}`);
|
|
|
|
this.debug(`buffer=${buffer.toString()} block=$${toHex(block)}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case 0: // INFO
|
|
|
|
this.getDeviceInfo(state, drive);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // READ
|
|
|
|
this.readBlock(state, drive, block, buffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // WRITE
|
|
|
|
this.writeBlock(state, drive, block, buffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // FORMAT
|
2022-06-05 17:57:04 +00:00
|
|
|
this.formatDevice(state, drive);
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-05-18 15:19:45 +00:00
|
|
|
} else if (off === smartOff && this.cpu.getSync()) {
|
2021-03-13 21:18:32 +00:00
|
|
|
this.debug('smartport entry');
|
|
|
|
const stackAddr = new Address(this.cpu, state.sp + 1, 0x01);
|
|
|
|
let blocks;
|
|
|
|
|
|
|
|
const retVal = stackAddr.readAddress();
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`return=${retVal.toString()}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
const cmdBlockAddr = retVal.inc(1);
|
|
|
|
cmd = cmdBlockAddr.readByte();
|
|
|
|
const cmdListAddr = cmdBlockAddr.inc(1).readAddress();
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`cmd=${cmd}`);
|
|
|
|
this.debug(`cmdListAddr=${cmdListAddr.toString()}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
stackAddr.writeAddress(retVal.inc(3));
|
|
|
|
|
|
|
|
const parameterCount = cmdListAddr.readByte();
|
|
|
|
unit = cmdListAddr.inc(1).readByte();
|
2022-06-05 17:57:04 +00:00
|
|
|
const drive = unit ? 2 : 1;
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer = cmdListAddr.inc(2).readAddress();
|
|
|
|
let status;
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`parameterCount=${parameterCount}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case 0x00: // INFO
|
|
|
|
status = cmdListAddr.inc(4).readByte();
|
2022-05-31 15:38:40 +00:00
|
|
|
this.debug(`info unit=${unit}`);
|
|
|
|
this.debug(`info buffer=${buffer.toString()}`);
|
|
|
|
this.debug(`info status=${status}`);
|
2021-03-13 21:18:32 +00:00
|
|
|
switch (unit) {
|
|
|
|
case 0:
|
|
|
|
switch (status) {
|
|
|
|
case 0:
|
2021-07-10 18:44:10 +00:00
|
|
|
buffer.writeByte(2); // two devices
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer.inc(1).writeByte(1 << 6); // no interrupts
|
2021-12-23 04:47:36 +00:00
|
|
|
buffer.inc(2).writeByte(0x2); // Other vendor
|
|
|
|
buffer.inc(3).writeByte(0x0); // Other vendor
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer.inc(4).writeByte(0); // reserved
|
|
|
|
buffer.inc(5).writeByte(0); // reserved
|
|
|
|
buffer.inc(6).writeByte(0); // reserved
|
|
|
|
buffer.inc(7).writeByte(0); // reserved
|
|
|
|
state.x = 8;
|
|
|
|
state.y = 0;
|
|
|
|
state.a = 0;
|
2021-07-10 18:44:10 +00:00
|
|
|
state.s &= ~flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // Unit 1
|
|
|
|
switch (status) {
|
|
|
|
case 0:
|
2023-11-24 14:45:55 +00:00
|
|
|
blocks =
|
|
|
|
this.disks[unit]?.blocks.length ?? 0;
|
2021-03-13 21:18:32 +00:00
|
|
|
buffer.writeByte(0xf0); // W/R Block device in drive
|
|
|
|
buffer.inc(1).writeByte(blocks & 0xff); // 1600 blocks
|
2023-11-24 14:45:55 +00:00
|
|
|
buffer
|
|
|
|
.inc(2)
|
|
|
|
.writeByte((blocks & 0xff00) >> 8);
|
|
|
|
buffer
|
|
|
|
.inc(3)
|
|
|
|
.writeByte((blocks & 0xff0000) >> 16);
|
2021-03-13 21:18:32 +00:00
|
|
|
state.x = 4;
|
|
|
|
state.y = 0;
|
|
|
|
state.a = 0;
|
2021-07-10 18:44:10 +00:00
|
|
|
state.s &= ~flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
2021-12-23 04:47:36 +00:00
|
|
|
case 3:
|
2023-11-24 14:45:55 +00:00
|
|
|
blocks =
|
|
|
|
this.disks[unit]?.blocks.length ?? 0;
|
2021-12-23 04:47:36 +00:00
|
|
|
buffer.writeByte(0xf0); // W/R Block device in drive
|
|
|
|
buffer.inc(1).writeByte(blocks & 0xff); // Blocks low byte
|
2023-11-24 14:45:55 +00:00
|
|
|
buffer
|
|
|
|
.inc(2)
|
|
|
|
.writeByte((blocks & 0xff00) >> 8); // Blocks middle byte
|
|
|
|
buffer
|
|
|
|
.inc(3)
|
|
|
|
.writeByte((blocks & 0xff0000) >> 16); // Blocks high byte
|
2021-12-23 04:47:36 +00:00
|
|
|
buffer.inc(4).writeByte(ID.length); // Vendor ID length
|
2023-11-24 14:45:55 +00:00
|
|
|
for (let idx = 0; idx < ID.length; idx++) {
|
|
|
|
// Vendor ID
|
|
|
|
buffer
|
|
|
|
.inc(5 + idx)
|
|
|
|
.writeByte(ID.charCodeAt(idx));
|
2021-12-23 04:47:36 +00:00
|
|
|
}
|
2023-11-24 14:45:55 +00:00
|
|
|
buffer
|
|
|
|
.inc(21)
|
|
|
|
.writeByte(DEVICE_TYPE_SCSI_HD); // Device Type
|
2021-12-23 04:47:36 +00:00
|
|
|
buffer.inc(22).writeByte(0x0); // Device Subtype
|
|
|
|
buffer.inc(23).writeWord(0x0101); // Version
|
|
|
|
state.x = 24;
|
|
|
|
state.y = 0;
|
|
|
|
state.a = 0;
|
|
|
|
state.s &= ~flags.C;
|
|
|
|
break;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state.a = 0;
|
2021-07-10 18:44:10 +00:00
|
|
|
state.s &= ~flags.C;
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x01: // READ BLOCK
|
|
|
|
block = cmdListAddr.inc(4).readWord();
|
2022-06-05 17:57:04 +00:00
|
|
|
this.readBlock(state, drive, block, buffer);
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x02: // WRITE BLOCK
|
|
|
|
block = cmdListAddr.inc(4).readWord();
|
2022-06-05 17:57:04 +00:00
|
|
|
this.writeBlock(state, drive, block, buffer);
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x03: // FORMAT
|
2022-06-05 17:57:04 +00:00
|
|
|
this.formatDevice(state, drive);
|
2021-03-13 21:18:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x04: // CONTROL
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x05: // INIT
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x06: // OPEN
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x07: // CLOSE
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x08: // READ
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x09: // WRITE
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cpu.setState(state);
|
|
|
|
|
|
|
|
return this.rom[off];
|
|
|
|
}
|
|
|
|
|
|
|
|
write() {
|
2022-05-31 15:38:40 +00:00
|
|
|
// not writable
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getState() {
|
|
|
|
return {
|
2023-11-24 14:45:55 +00:00
|
|
|
disks: this.disks.map((disk) => {
|
2021-07-07 00:04:02 +00:00
|
|
|
const result: BlockDisk = {
|
2023-11-24 14:45:55 +00:00
|
|
|
blocks: disk.blocks.map((block) => new Uint8Array(block)),
|
2021-07-07 00:04:02 +00:00
|
|
|
encoding: ENCODING_BLOCK,
|
Split disk data out into its own record (#158)
* Harmonize drive and disk type hierarchies
Before, the `XXXDrive` and `XXXDisk` type hierarchies were similar,
but not exactly the same. For example, `encoding` and `format` were
missing on some `XXXDisk` types where they existed on the `XXXDrive`
type. This change attempts to bring the hierarchies closer together.
However, the biggest visible consequence is the introduction of the
`FLOPPY_FORMATS` array and its associated `FloppyFormat` type. This
replaces `NIBBLE_FORMATS` in most places. A couple of new type guards
for disk formats and disks have been added as well.
All tests pass, everything compiles with no errors, and both WOZ and
nibble format disks load in the emulator.
* Move disk data to a `disk` field in the drive
Before, disk data was mixed in with state about the drive itself (like
track, motor phase, etc.). This made it hard to know exactly what data
was necessary for different image formats.
Now, the disk data is in a `disk` field whose type depends on the
drive type. This makes responisbility a bit easier.
One oddity, though, is that the `Drive` has metadata _and_ the `Disk`
has metadata. When a disk is in the drive, these should be `===`, but
when there is no disk in the drive, obviously only the drive metadata
is set.
All tests pass, everything compiles, and both WOZ and nibble disks
work in the emulator (both preact and classic).
* Squash the `Drive` type hierarchy
Before, the type of the drive depended on the type of the disk in the
drive. Thus, `NibbleDrive` contained a `NibbleDisk` and a `WozDrive`
contained a `WozDisk`. With the extraction of the disk data to a
single field, this type hierarchy makes no sense. Instead, it
suffices to check the type of the disk.
This change removes the `NibbleDrive` and `WozDrive` types and type
guards, checking the disk type where necessary. This change also
introduces the `NoFloppyDisk` type to represent the lack of a
disk. This allows the drive to have metadata, for one.
All tests pass, everything compiles, and both WOZ and nibble disks
work locally.
* Use more destructuring assignment
Now, more places use constructs like:
```TypeScript
const { metadata, readOnly, track, head, phase, dirty } = drive;
return {
disk: getDiskState(drive.disk),
metadata: {...metadata},
readOnly,
track,
head,
phase,
dirty,
};
```
* Remove the `Disk` object from the `Drive` object
This change splits out the disk objects into a record parallel to the
drive objects. The idea is that the `Drive` structure becomes a
representation of the state of the drive that is separate from the
disk image actually in the drive. This helps in an upcoming
refactoring.
This also changes the default empty disks to be writable. While odd,
the write protect switch should be in the "off" position since there
is no disk pressing on it.
Finally, `insertDisk` now resets the head position to 0 since there is
no way of preserving the head position across disks. (Even in the real
world, the motor-off delay plus spindle spin-down would make it
impossible to know the disk head position with any accuracy.)
2022-09-17 13:41:35 +00:00
|
|
|
format: disk.format,
|
2021-07-07 00:04:02 +00:00
|
|
|
readOnly: disk.readOnly,
|
Floppy controller refactorings 1 (#155)
* Add `DiskMetada` to the `Disk` interface
Before, metadata about the image, such as name, side, etc. was mixed
in with actual disk image information. This change breaks that
information into a separate structure called `DiskMetadata`.
Currently, the only two fields are `name` and `side`, but the idea is
that more fields could be added as necessary, like a description, a
scan of the disk or label, etc. In a follow-on change, the default
write-protection status will come from the metadata as well.
The current implementation copies the metadata when saving/restoring
state, loading disk images, etc. In the future, the metadata should
passed around until the format is required to change (like saving one
disk image format as another). Likewise, in the future, in may be
desirable to be able to override the disk image metadata with
user-supplied metadata. This could be use, for example, to
temporarily add or remove write-protection from a disk image.
All existing tests pass and the emulator builds with no errors.
* Rename `writeMode` to `q7`
Before, nibble disk emulation used the `writeMode` field to keep track
of whether the drive should be read from or written to, but the WOZ
emulation used `q7` to keep track of the same state.
This change renames `writeMode` to `q7` because it more accurately
reflects the state of the Disk II controller as specified in the
manuals, DOS source, and, especially, _Understanding the Apple //e_ by
Jim Sather.
* Remove the coil state
Before, `q` captured the state of the coils. But it was never read.
This change just deletes it.
* Use the bootstrap and sequencer ROMs with indirection
Before, the contents of the bootstrap ROM and sequencer ROM were set
directly on fields of the controller. These were not saved or
restored with the state in `getState` and `setState`. (It would have
been very space inefficient if they had).
Now, these ROMs are used from constants indexed by the number of
sectors the card supports. This, in turn, means that if the number of
sectors is saved with the state, it can be easily restored.
* Split out the Disk II controller state
This change factors the emulated hardware state into a separate
structure in the Disk II controller. The idea is that this hardware
state will be able to be shared with the WOZ and nibble disk code
instead of sharing _all_ of the controller state (like callbacks and
so forth).
* Factor out disk insertion
Before, several places in the code essentially inserted a new disk
image into the drive, which similar—but not always exactly the
same—code. Now there is an `insertDisk` method that is responsible
for inserting a new `FloppyDisk`.
All tests pass, everything compiles, manually tested nibble disks and
WOZ disks.
2022-09-01 01:55:01 +00:00
|
|
|
metadata: { ...disk.metadata },
|
2021-07-07 00:04:02 +00:00
|
|
|
};
|
|
|
|
return result;
|
2023-11-24 14:45:55 +00:00
|
|
|
}),
|
|
|
|
};
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:45:55 +00:00
|
|
|
setState(state: SmartPortState) {
|
|
|
|
this.disks = state.disks.map((disk) => {
|
|
|
|
const result: BlockDisk = {
|
|
|
|
blocks: disk.blocks.map((block) => new Uint8Array(block)),
|
|
|
|
encoding: ENCODING_BLOCK,
|
|
|
|
format: disk.format,
|
|
|
|
readOnly: disk.readOnly,
|
|
|
|
metadata: { ...disk.metadata },
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setBinary(
|
|
|
|
driveNo: DriveNumber,
|
|
|
|
name: string,
|
|
|
|
fmt: BlockFormat,
|
|
|
|
rawData: ArrayBuffer
|
|
|
|
) {
|
2022-07-23 19:00:38 +00:00
|
|
|
let volume = 254;
|
|
|
|
let readOnly = false;
|
2022-05-18 15:19:45 +00:00
|
|
|
if (fmt === '2mg') {
|
2022-06-22 03:34:19 +00:00
|
|
|
const header = read2MGHeader(rawData);
|
2022-09-19 07:07:27 +00:00
|
|
|
this.metadata[driveNo] = header;
|
2022-06-22 03:34:19 +00:00
|
|
|
const { bytes, offset } = header;
|
2022-07-23 19:00:38 +00:00
|
|
|
volume = header.volume;
|
|
|
|
readOnly = header.readOnly;
|
2021-07-07 00:04:02 +00:00
|
|
|
rawData = rawData.slice(offset, offset + bytes);
|
2022-06-22 03:34:19 +00:00
|
|
|
} else {
|
2022-09-19 07:07:27 +00:00
|
|
|
this.metadata[driveNo] = null;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
2021-07-07 00:04:02 +00:00
|
|
|
const options = {
|
|
|
|
rawData,
|
|
|
|
name,
|
|
|
|
readOnly,
|
|
|
|
volume,
|
|
|
|
};
|
2021-03-13 21:18:32 +00:00
|
|
|
|
2022-09-19 07:07:27 +00:00
|
|
|
this.ext[driveNo] = fmt;
|
|
|
|
this.disks[driveNo] = createBlockDisk(fmt, options);
|
|
|
|
this.callbacks?.label(driveNo, name);
|
2021-07-07 00:04:02 +00:00
|
|
|
|
|
|
|
return true;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
2022-06-19 16:01:44 +00:00
|
|
|
|
|
|
|
getBinary(drive: number): MassStorageData | null {
|
|
|
|
if (!this.disks[drive]) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-22 03:34:19 +00:00
|
|
|
const disk = this.disks[drive];
|
|
|
|
const ext = this.ext[drive];
|
Floppy controller refactorings 1 (#155)
* Add `DiskMetada` to the `Disk` interface
Before, metadata about the image, such as name, side, etc. was mixed
in with actual disk image information. This change breaks that
information into a separate structure called `DiskMetadata`.
Currently, the only two fields are `name` and `side`, but the idea is
that more fields could be added as necessary, like a description, a
scan of the disk or label, etc. In a follow-on change, the default
write-protection status will come from the metadata as well.
The current implementation copies the metadata when saving/restoring
state, loading disk images, etc. In the future, the metadata should
passed around until the format is required to change (like saving one
disk image format as another). Likewise, in the future, in may be
desirable to be able to override the disk image metadata with
user-supplied metadata. This could be use, for example, to
temporarily add or remove write-protection from a disk image.
All existing tests pass and the emulator builds with no errors.
* Rename `writeMode` to `q7`
Before, nibble disk emulation used the `writeMode` field to keep track
of whether the drive should be read from or written to, but the WOZ
emulation used `q7` to keep track of the same state.
This change renames `writeMode` to `q7` because it more accurately
reflects the state of the Disk II controller as specified in the
manuals, DOS source, and, especially, _Understanding the Apple //e_ by
Jim Sather.
* Remove the coil state
Before, `q` captured the state of the coils. But it was never read.
This change just deletes it.
* Use the bootstrap and sequencer ROMs with indirection
Before, the contents of the bootstrap ROM and sequencer ROM were set
directly on fields of the controller. These were not saved or
restored with the state in `getState` and `setState`. (It would have
been very space inefficient if they had).
Now, these ROMs are used from constants indexed by the number of
sectors the card supports. This, in turn, means that if the number of
sectors is saved with the state, it can be easily restored.
* Split out the Disk II controller state
This change factors the emulated hardware state into a separate
structure in the Disk II controller. The idea is that this hardware
state will be able to be shared with the WOZ and nibble disk code
instead of sharing _all_ of the controller state (like callbacks and
so forth).
* Factor out disk insertion
Before, several places in the code essentially inserted a new disk
image into the drive, which similar—but not always exactly the
same—code. Now there is an `insertDisk` method that is responsible
for inserting a new `FloppyDisk`.
All tests pass, everything compiles, manually tested nibble disks and
WOZ disks.
2022-09-01 01:55:01 +00:00
|
|
|
const { readOnly } = disk;
|
|
|
|
const { name } = disk.metadata;
|
2022-06-22 03:34:19 +00:00
|
|
|
let data: ArrayBuffer;
|
|
|
|
if (ext === '2mg') {
|
|
|
|
data = create2MGFromBlockDisk(this.metadata[drive], disk);
|
|
|
|
} else {
|
|
|
|
const { blocks } = disk;
|
|
|
|
const byteArray = new Uint8Array(blocks.length * 512);
|
|
|
|
for (let idx = 0; idx < blocks.length; idx++) {
|
|
|
|
byteArray.set(blocks[idx], idx * 512);
|
|
|
|
}
|
|
|
|
data = byteArray.buffer;
|
2022-06-19 16:01:44 +00:00
|
|
|
}
|
|
|
|
return {
|
Floppy controller refactorings 1 (#155)
* Add `DiskMetada` to the `Disk` interface
Before, metadata about the image, such as name, side, etc. was mixed
in with actual disk image information. This change breaks that
information into a separate structure called `DiskMetadata`.
Currently, the only two fields are `name` and `side`, but the idea is
that more fields could be added as necessary, like a description, a
scan of the disk or label, etc. In a follow-on change, the default
write-protection status will come from the metadata as well.
The current implementation copies the metadata when saving/restoring
state, loading disk images, etc. In the future, the metadata should
passed around until the format is required to change (like saving one
disk image format as another). Likewise, in the future, in may be
desirable to be able to override the disk image metadata with
user-supplied metadata. This could be use, for example, to
temporarily add or remove write-protection from a disk image.
All existing tests pass and the emulator builds with no errors.
* Rename `writeMode` to `q7`
Before, nibble disk emulation used the `writeMode` field to keep track
of whether the drive should be read from or written to, but the WOZ
emulation used `q7` to keep track of the same state.
This change renames `writeMode` to `q7` because it more accurately
reflects the state of the Disk II controller as specified in the
manuals, DOS source, and, especially, _Understanding the Apple //e_ by
Jim Sather.
* Remove the coil state
Before, `q` captured the state of the coils. But it was never read.
This change just deletes it.
* Use the bootstrap and sequencer ROMs with indirection
Before, the contents of the bootstrap ROM and sequencer ROM were set
directly on fields of the controller. These were not saved or
restored with the state in `getState` and `setState`. (It would have
been very space inefficient if they had).
Now, these ROMs are used from constants indexed by the number of
sectors the card supports. This, in turn, means that if the number of
sectors is saved with the state, it can be easily restored.
* Split out the Disk II controller state
This change factors the emulated hardware state into a separate
structure in the Disk II controller. The idea is that this hardware
state will be able to be shared with the WOZ and nibble disk code
instead of sharing _all_ of the controller state (like callbacks and
so forth).
* Factor out disk insertion
Before, several places in the code essentially inserted a new disk
image into the drive, which similar—but not always exactly the
same—code. Now there is an `insertDisk` method that is responsible
for inserting a new `FloppyDisk`.
All tests pass, everything compiles, manually tested nibble disks and
WOZ disks.
2022-09-01 01:55:01 +00:00
|
|
|
metadata: { name },
|
2022-06-22 03:34:19 +00:00
|
|
|
ext,
|
|
|
|
data,
|
2022-07-23 19:00:38 +00:00
|
|
|
readOnly,
|
2022-06-19 16:01:44 +00:00
|
|
|
};
|
|
|
|
}
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|