mirror of
https://github.com/whscullin/apple2js.git
synced 2024-01-12 14:14:38 +00:00
41e0609f55
* 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.
172 lines
5.4 KiB
TypeScript
172 lines
5.4 KiB
TypeScript
import { h } from 'preact';
|
|
import cs from 'classnames';
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
import { Apple2 as Apple2Impl } from '../apple2';
|
|
import { ControlStrip } from './ControlStrip';
|
|
import { Debugger } from './debugger/Debugger';
|
|
import { ErrorModal } from './ErrorModal';
|
|
import { Inset } from './Inset';
|
|
import { Keyboard } from './Keyboard';
|
|
import { LanguageCard } from './LanguageCard';
|
|
import { Mouse } from './Mouse';
|
|
import { Screen } from './Screen';
|
|
import { Drives } from './Drives';
|
|
import { Slinky } from './Slinky';
|
|
import { ThunderClock } from './ThunderClock';
|
|
import { Videoterm } from './Videoterm';
|
|
import { spawn, Ready } from './util/promises';
|
|
|
|
import styles from './css/Apple2.module.css';
|
|
import { SupportedSectors } from 'js/formats/types';
|
|
|
|
declare global {
|
|
interface Window {
|
|
apple2: Apple2Impl;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Interface for the Apple2 component.
|
|
*/
|
|
export interface Apple2Props {
|
|
characterRom: string;
|
|
enhanced: boolean;
|
|
e: boolean;
|
|
gl: boolean;
|
|
rom: string;
|
|
sectors: SupportedSectors;
|
|
}
|
|
|
|
/**
|
|
* Component to bind various UI components together to form
|
|
* the application layout. Includes the screen, drives,
|
|
* emulator controls and keyboard. Bootstraps the core
|
|
* Apple2 emulator.
|
|
*
|
|
* @param props Apple2 initialization props
|
|
* @returns
|
|
*/
|
|
export const Apple2 = (props: Apple2Props) => {
|
|
const { e, enhanced, sectors } = props;
|
|
const screenRef = useRef<HTMLCanvasElement>(null);
|
|
const [apple2, setApple2] = useState<Apple2Impl>();
|
|
const [error, setError] = useState<unknown>();
|
|
const [ready, setReady] = useState(false);
|
|
const [showDebug, setShowDebug] = useState(false);
|
|
const drivesReady = useMemo(() => new Ready(setError), []);
|
|
|
|
const io = apple2?.getIO();
|
|
const cpu = apple2?.getCPU();
|
|
const vm = apple2?.getVideoModes();
|
|
const rom = apple2?.getROM();
|
|
|
|
const doPaste = useCallback((event: Event) => {
|
|
if (
|
|
(document.activeElement !== screenRef.current) &&
|
|
(document.activeElement !== document.body)
|
|
) {
|
|
return;
|
|
}
|
|
if (io) {
|
|
const paste = (event.clipboardData || window.clipboardData)?.getData('text');
|
|
if (paste) {
|
|
io.setKeyBuffer(paste);
|
|
}
|
|
}
|
|
event.preventDefault();
|
|
}, [io]);
|
|
|
|
const doCopy = useCallback((event: Event) => {
|
|
if (
|
|
(document.activeElement !== screenRef.current) &&
|
|
(document.activeElement !== document.body)
|
|
) {
|
|
return;
|
|
}
|
|
if (vm) {
|
|
event.clipboardData?.setData('text/plain', vm.getText());
|
|
}
|
|
event.preventDefault();
|
|
}, [vm]);
|
|
|
|
useEffect(() => {
|
|
if (screenRef.current) {
|
|
const options = {
|
|
canvas: screenRef.current,
|
|
tick: () => { /* do nothing */ },
|
|
...props,
|
|
};
|
|
const apple2 = new Apple2Impl(options);
|
|
const controller = spawn(async (signal) => {
|
|
try {
|
|
await apple2.ready;
|
|
if (signal.aborted) {
|
|
return;
|
|
}
|
|
setApple2(apple2);
|
|
await drivesReady.ready;
|
|
if (signal.aborted) {
|
|
setApple2(undefined);
|
|
return;
|
|
}
|
|
apple2.reset();
|
|
apple2.run();
|
|
} catch (e) {
|
|
setError(e);
|
|
}
|
|
setReady(true);
|
|
});
|
|
|
|
window.apple2 = apple2;
|
|
|
|
return () => controller.abort();
|
|
}
|
|
}, [props, drivesReady]);
|
|
|
|
useEffect(() => {
|
|
const { current } = screenRef;
|
|
|
|
window.addEventListener('paste', doPaste);
|
|
window.addEventListener('copy', doCopy);
|
|
|
|
current?.addEventListener('paste', doPaste);
|
|
current?.addEventListener('copy', doCopy);
|
|
|
|
return () => {
|
|
window.removeEventListener('paste', doPaste);
|
|
window.removeEventListener('copy', doCopy);
|
|
|
|
current?.removeEventListener('paste', doPaste);
|
|
current?.removeEventListener('copy', doCopy);
|
|
};
|
|
}, [doCopy, doPaste]);
|
|
|
|
const toggleDebugger = useCallback(() => {
|
|
setShowDebug((on) => !on);
|
|
}, []);
|
|
|
|
return (
|
|
<div className={styles.container}>
|
|
<div
|
|
className={cs(styles.outer, { apple2e: e, [styles.ready]: ready })}
|
|
>
|
|
<Screen screenRef={screenRef} />
|
|
{!e ? <LanguageCard cpu={cpu} io={io} rom={rom} slot={0} /> : null}
|
|
<Slinky io={io} slot={2} />
|
|
{!e ? <Videoterm io={io} slot={3} /> : null}
|
|
<Mouse cpu={cpu} screenRef={screenRef} io={io} slot={4} />
|
|
<ThunderClock io={io} slot={5} />
|
|
<Inset>
|
|
<Drives cpu={cpu} io={io} sectors={sectors} enhanced={enhanced} ready={drivesReady} />
|
|
</Inset>
|
|
<ControlStrip apple2={apple2} e={e} toggleDebugger={toggleDebugger} />
|
|
<Inset>
|
|
<Keyboard apple2={apple2} e={e} />
|
|
</Inset>
|
|
<ErrorModal error={error} setError={setError} />
|
|
</div>
|
|
{showDebug ? <Debugger apple2={apple2} /> : null}
|
|
</div>
|
|
);
|
|
};
|