2022-05-10 13:52:06 +00:00
|
|
|
import { h } from 'preact';
|
|
|
|
import cs from 'classnames';
|
2022-06-05 17:57:04 +00:00
|
|
|
import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
2022-05-10 13:52:06 +00:00
|
|
|
import { Apple2 as Apple2Impl } from '../apple2';
|
|
|
|
import Apple2IO from '../apple2io';
|
2022-05-12 14:59:12 +00:00
|
|
|
import CPU6502 from '../cpu6502';
|
2022-05-10 13:52:06 +00:00
|
|
|
import { ControlStrip } from './ControlStrip';
|
2022-06-05 17:57:04 +00:00
|
|
|
import { ErrorModal } from './ErrorModal';
|
2022-05-10 13:52:06 +00:00
|
|
|
import { Inset } from './Inset';
|
|
|
|
import { Keyboard } from './Keyboard';
|
2022-05-12 00:21:21 +00:00
|
|
|
import { Mouse } from './Mouse';
|
2022-05-10 13:52:06 +00:00
|
|
|
import { Screen } from './Screen';
|
|
|
|
import { Drives } from './Drives';
|
2022-05-12 14:59:12 +00:00
|
|
|
import { Slinky } from './Slinky';
|
|
|
|
import { ThunderClock } from './ThunderClock';
|
2022-06-05 17:57:04 +00:00
|
|
|
import { noAwait, Ready } from './util/promises';
|
2022-05-10 13:52:06 +00:00
|
|
|
|
2022-06-03 22:30:39 +00:00
|
|
|
import styles from './css/Apple2.module.css';
|
|
|
|
|
2022-05-10 13:52:06 +00:00
|
|
|
/**
|
|
|
|
* Interface for the Apple2 component.
|
|
|
|
*/
|
|
|
|
export interface Apple2Props {
|
|
|
|
characterRom: string;
|
|
|
|
enhanced: boolean;
|
|
|
|
e: boolean;
|
|
|
|
gl: boolean;
|
|
|
|
rom: string;
|
|
|
|
sectors: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2022-06-05 17:57:04 +00:00
|
|
|
const { e, enhanced, sectors } = props;
|
2022-05-10 13:52:06 +00:00
|
|
|
const screen = useRef<HTMLCanvasElement>(null);
|
|
|
|
const [apple2, setApple2] = useState<Apple2Impl>();
|
|
|
|
const [io, setIO] = useState<Apple2IO>();
|
2022-05-12 00:21:21 +00:00
|
|
|
const [cpu, setCPU] = useState<CPU6502>();
|
2022-06-01 13:28:05 +00:00
|
|
|
const [error, setError] = useState<unknown>();
|
2022-06-05 17:57:04 +00:00
|
|
|
const drivesReady = useMemo(() => new Ready(setError), []);
|
2022-05-10 13:52:06 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (screen.current) {
|
|
|
|
const options = {
|
|
|
|
canvas: screen.current,
|
2022-05-31 15:38:40 +00:00
|
|
|
tick: () => { /* do nothing */ },
|
2022-05-10 13:52:06 +00:00
|
|
|
...props,
|
|
|
|
};
|
|
|
|
const apple2 = new Apple2Impl(options);
|
2022-06-05 17:57:04 +00:00
|
|
|
noAwait((async () => {
|
|
|
|
try {
|
|
|
|
await apple2.ready;
|
|
|
|
setApple2(apple2);
|
|
|
|
setIO(apple2.getIO());
|
|
|
|
setCPU(apple2.getCPU());
|
|
|
|
await drivesReady.ready;
|
|
|
|
apple2.reset();
|
|
|
|
apple2.run();
|
|
|
|
} catch (e) {
|
|
|
|
setError(e);
|
|
|
|
}
|
|
|
|
}))();
|
2022-05-10 13:52:06 +00:00
|
|
|
}
|
2022-06-05 17:57:04 +00:00
|
|
|
}, [props, drivesReady]);
|
2022-05-10 13:52:06 +00:00
|
|
|
|
|
|
|
return (
|
2022-06-03 22:30:39 +00:00
|
|
|
<div className={cs(styles.outer, { apple2e: e })}>
|
2022-05-10 13:52:06 +00:00
|
|
|
<Screen screen={screen} />
|
2022-06-05 17:57:04 +00:00
|
|
|
<Slinky io={io} slot={2} />
|
2022-05-12 14:59:12 +00:00
|
|
|
<Mouse cpu={cpu} screen={screen} io={io} slot={4} />
|
|
|
|
<ThunderClock io={io} slot={5} />
|
2022-05-10 13:52:06 +00:00
|
|
|
<Inset>
|
2022-06-05 17:57:04 +00:00
|
|
|
<Drives cpu={cpu} io={io} sectors={sectors} enhanced={enhanced} ready={drivesReady} />
|
2022-05-10 13:52:06 +00:00
|
|
|
</Inset>
|
|
|
|
<ControlStrip apple2={apple2} e={e} />
|
|
|
|
<Inset>
|
|
|
|
<Keyboard apple2={apple2} e={e} />
|
|
|
|
</Inset>
|
2022-06-01 00:41:24 +00:00
|
|
|
<ErrorModal error={error} setError={setError} />
|
2022-05-10 13:52:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|