1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-10-31 23:09:49 +00:00
8bitworkshop/src/common/devices.ts

415 lines
9.6 KiB
TypeScript
Raw Permalink Normal View History

2019-08-22 02:55:32 +00:00
export interface SavesState<S> {
saveState(): S;
loadState(state: S): void;
2019-08-22 02:55:32 +00:00
}
export interface Bus {
read(a: number): number;
write(a: number, v: number): void;
readConst?(a: number): number;
2019-08-22 02:55:32 +00:00
}
2023-12-27 20:24:42 +00:00
export interface Bus32 {
read32(a: number): number;
write32(a: number, v: number): void;
readConst32?(a: number): number;
}
2019-08-22 02:55:32 +00:00
export interface ClockBased {
advanceClock(): void;
2019-08-22 02:55:32 +00:00
}
export interface InstructionBased {
advanceInsn(): number;
2019-08-22 02:55:32 +00:00
}
export type TrapCondition = () => boolean;
2019-08-22 02:55:32 +00:00
export interface FrameBased {
advanceFrame(trap: TrapCondition): number;
2019-08-22 02:55:32 +00:00
}
export interface VideoSource {
getVideoParams(): VideoParams;
connectVideo(pixels: Uint32Array): void;
2019-08-22 02:55:32 +00:00
}
export interface RasterFrameBased extends FrameBased, VideoSource {
getRasterY(): number;
getRasterX(): number;
getRasterCanvasPosition?(): { x: number, y: number };
2019-08-22 02:55:32 +00:00
}
export interface VideoParams {
width: number;
height: number;
overscan?: boolean;
rotate?: number;
videoFrequency?: number; // default = 60
aspect?: number;
2019-08-22 02:55:32 +00:00
}
// TODO: frame buffer optimization (apple2, etc)
export interface SampledAudioParams {
sampleRate: number;
stereo: boolean;
2019-08-22 02:55:32 +00:00
}
export interface SampledAudioSink {
feedSample(value: number, count: number): void;
//sendAudioFrame(samples:Uint16Array) : void;
}
export interface SampledAudioSource {
getAudioParams(): SampledAudioParams;
connectAudio(audio: SampledAudioSink): void;
2019-08-22 02:55:32 +00:00
}
export interface AcceptsROM {
loadROM(data: Uint8Array, title?: string): void;
2019-08-22 02:55:32 +00:00
}
export interface AcceptsBIOS {
loadBIOS(data: Uint8Array, title?: string): void;
}
2019-08-22 02:55:32 +00:00
export interface Resettable {
reset(): void;
2019-08-22 02:55:32 +00:00
}
export interface MemoryBusConnected {
connectMemoryBus(bus: Bus): void;
2019-08-22 02:55:32 +00:00
}
2019-08-23 02:01:05 +00:00
export interface IOBusConnected {
connectIOBus(bus: Bus): void;
2019-08-23 02:01:05 +00:00
}
export interface CPU extends MemoryBusConnected, Resettable, SavesState<any> {
getPC(): number;
getSP(): number;
isStable(): boolean;
2019-08-22 02:55:32 +00:00
}
export interface HasCPU extends Resettable {
cpu: CPU;
}
export interface Interruptable<IT> {
interrupt(type: IT): void;
2019-08-22 02:55:32 +00:00
}
export interface SavesInputState<CS> {
loadControlsState(cs: CS): void;
saveControlsState(): CS;
2019-08-22 02:55:32 +00:00
}
export interface AcceptsKeyInput {
setKeyInput(key: number, code: number, flags: number): void;
}
export interface AcceptsPaddleInput {
setPaddleInput(controller: number, value: number): void;
}
// TODO: interface not yet used (setKeyInput() handles joystick)
export interface AcceptsJoyInput {
setJoyInput(joy: number, bitmask: number): void;
}
// SERIAL I/O
export interface SerialEvent {
op: 'read' | 'write';
value: number;
nbits: number;
}
// TODO: all these needed?
export interface SerialIOInterface {
// from machine to platform
clearToSend(): boolean;
sendByte(b: number);
// from platform to machine
byteAvailable(): boolean;
recvByte(): number;
// implement these too
reset(): void;
advance(clocks: number): void;
// refresh() : void;
}
export interface HasSerialIO {
connectSerialIO(serial: SerialIOInterface);
serialOut?: SerialEvent[]; // outgoing event log
serialIn?: SerialEvent[]; // incoming queue
}
/// PROFILER
export interface Probeable {
connectProbe(probe: ProbeAll): void;
}
export interface ProbeTime {
logClocks(clocks: number);
logNewScanline();
logNewFrame();
}
export interface ProbeCPU {
logExecute(address: number, SP: number);
logInterrupt(type: number);
logIllegal(address: number);
logWait(address: number);
}
export interface ProbeBus {
logRead(address: number, value: number);
logWrite(address: number, value: number);
logDMARead(address: number, value: number);
logDMAWrite(address: number, value: number);
}
export interface ProbeIO {
logIORead(address: number, value: number);
logIOWrite(address: number, value: number);
}
2019-08-27 03:17:07 +00:00
export interface ProbeVRAM {
logVRAMRead(address: number, value: number);
logVRAMWrite(address: number, value: number);
2019-08-27 03:17:07 +00:00
}
export interface ProbeAll extends ProbeTime, ProbeCPU, ProbeBus, ProbeIO, ProbeVRAM {
logData(data: number); // entire 32 bits
addLogBuffer(src: Uint32Array);
}
export class NullProbe implements ProbeAll {
logClocks() { }
logNewScanline() { }
logNewFrame() { }
logExecute() { }
logInterrupt() { }
logRead() { }
logWrite() { }
logIORead() { }
logIOWrite() { }
logVRAMRead() { }
logVRAMWrite() { }
logIllegal() { }
logWait() { }
logDMARead() { }
logDMAWrite() { }
logData() { }
addLogBuffer(src: Uint32Array) { }
2019-08-22 02:55:32 +00:00
}
/// CONVENIENCE
export interface BasicMachineControlsState {
inputs: Uint8Array;
}
2019-08-22 02:55:32 +00:00
export interface BasicMachineState extends BasicMachineControlsState {
c: any; // TODO
ram: Uint8Array;
2019-08-22 02:55:32 +00:00
}
export abstract class BasicHeadlessMachine implements HasCPU, Bus, AcceptsROM, Probeable,
SavesState<BasicMachineState>, SavesInputState<BasicMachineControlsState> {
abstract cpuFrequency: number;
abstract defaultROMSize: number;
abstract cpu: CPU;
abstract ram: Uint8Array;
rom: Uint8Array;
inputs: Uint8Array = new Uint8Array(32);
handler: (key, code, flags) => void; // keyboard handler
nullProbe = new NullProbe();
probe: ProbeAll = this.nullProbe;
abstract read(a: number): number;
abstract write(a: number, v: number): void;
setKeyInput(key: number, code: number, flags: number): void {
this.handler && this.handler(key, code, flags);
2019-08-26 17:44:06 +00:00
}
connectProbe(probe: ProbeAll): void {
this.probe = probe || this.nullProbe;
}
reset() {
this.cpu.reset();
}
loadROM(data: Uint8Array, title?: string): void {
if (!this.rom) this.rom = new Uint8Array(this.defaultROMSize);
if (data.length > this.rom.length)
throw new Error(`ROM too big: ${data.length} > ${this.rom.length}}`);
this.rom.set(data);
}
loadState(state) {
this.cpu.loadState(state.c);
this.ram.set(state.ram);
this.inputs.set(state.inputs);
}
saveState() {
return {
c: this.cpu.saveState(),
ram: this.ram.slice(0),
inputs: this.inputs.slice(0),
};
}
loadControlsState(state) {
this.inputs.set(state.inputs);
}
saveControlsState() {
return {
inputs: this.inputs.slice(0)
};
}
advanceCPU() {
var c = this.cpu as any;
var n = 1;
2019-10-16 20:06:56 +00:00
if (this.cpu.isStable()) { this.probe.logExecute(this.cpu.getPC(), this.cpu.getSP()); }
2019-08-25 14:45:36 +00:00
if (c.advanceClock) { c.advanceClock(); }
else if (c.advanceInsn) { n = c.advanceInsn(1); }
this.probe.logClocks(n);
return n;
}
2023-12-27 20:24:42 +00:00
probeMemoryBus(membus: Bus & Partial<Bus32>): Bus & Partial<Bus32> {
2019-08-26 23:00:01 +00:00
return {
read: (a) => {
let val = membus.read(a);
this.probe.logRead(a, val);
return val;
},
write: (a, v) => {
this.probe.logWrite(a, v);
membus.write(a, v);
2023-12-27 20:24:42 +00:00
},
read32: (a) => {
let val = membus.read32(a);
this.probe.logRead(a, val);
return val;
},
write32: (a, v) => {
this.probe.logWrite(a, v);
membus.write32(a, v);
}
2019-08-26 23:00:01 +00:00
};
}
connectCPUMemoryBus(membus: Bus): void {
2023-12-27 20:24:42 +00:00
this.cpu.connectMemoryBus(this.probeMemoryBus(membus as Bus&Bus32));
2019-08-26 23:00:01 +00:00
}
probeIOBus(iobus: Bus): Bus {
2019-08-26 23:00:01 +00:00
return {
read: (a) => {
let val = iobus.read(a);
this.probe.logIORead(a, val);
return val;
},
write: (a, v) => {
this.probe.logIOWrite(a, v);
iobus.write(a, v);
2023-12-27 20:24:42 +00:00
},
2019-08-26 23:00:01 +00:00
};
}
probeDMABus(iobus: Bus): Bus {
2022-09-03 20:00:43 +00:00
return {
read: (a) => {
let val = iobus.read(a);
this.probe.logDMARead(a, val);
2022-09-03 20:00:43 +00:00
return val;
},
write: (a, v) => {
this.probe.logDMAWrite(a, v);
iobus.write(a, v);
2022-09-03 20:00:43 +00:00
}
};
}
connectCPUIOBus(iobus: Bus): void {
2019-08-26 23:00:01 +00:00
this.cpu['connectIOBus'](this.probeIOBus(iobus));
}
}
export abstract class BasicMachine extends BasicHeadlessMachine implements SampledAudioSource {
abstract canvasWidth: number;
abstract numVisibleScanlines: number;
abstract sampleRate: number;
overscan: boolean = false;
rotate: number = 0;
aspectRatio: number;
pixels: Uint32Array;
audio: SampledAudioSink;
scanline: number;
getAudioParams(): SampledAudioParams {
return { sampleRate: this.sampleRate, stereo: false };
}
connectAudio(audio: SampledAudioSink): void {
this.audio = audio;
}
getVideoParams(): VideoParams {
return {
width: this.canvasWidth,
height: this.numVisibleScanlines,
aspect: this.aspectRatio,
overscan: this.overscan,
rotate: this.rotate
};
}
connectVideo(pixels: Uint32Array): void {
this.pixels = pixels;
}
}
export abstract class BasicScanlineMachine extends BasicMachine implements RasterFrameBased {
abstract numTotalScanlines: number;
abstract cpuCyclesPerLine: number;
abstract startScanline(): void;
abstract drawScanline(): void;
frameCycles: number;
advanceFrame(trap: TrapCondition): number {
this.preFrame();
var endLineClock = 0;
var steps = 0;
this.probe.logNewFrame();
this.frameCycles = 0;
for (var sl = 0; sl < this.numTotalScanlines; sl++) {
2020-07-11 17:26:37 +00:00
endLineClock += this.cpuCyclesPerLine; // could be fractional
this.scanline = sl;
this.startScanline();
while (this.frameCycles < endLineClock) {
if (trap && trap()) {
sl = 999;
break;
}
this.frameCycles += this.advanceCPU();
steps++;
}
this.drawScanline();
this.probe.logNewScanline();
this.probe.logClocks(Math.floor(this.frameCycles - endLineClock)); // remainder of prev. line
}
this.postFrame();
return steps; // TODO: return steps, not clock? for recorder
}
preFrame() { }
postFrame() { }
getRasterY() { return this.scanline; }
getRasterX() { return this.frameCycles % this.cpuCyclesPerLine; }
}