2024-02-16 08:44:20 +00:00
|
|
|
import { u8 } from "./etc";
|
|
|
|
import { EventHandler } from "./eventHandler";
|
|
|
|
import { Instruction, ParameterType } from "./instructionSet";
|
|
|
|
|
2024-02-16 05:17:23 +00:00
|
|
|
export enum CpuEvent {
|
|
|
|
MemoryChanged,
|
|
|
|
RegisterChanged,
|
|
|
|
ProgramCounterChanged,
|
2024-02-16 08:44:20 +00:00
|
|
|
MemoryByteParsed,
|
|
|
|
InstructionExecuted,
|
2024-02-16 05:17:23 +00:00
|
|
|
Print,
|
|
|
|
Reset,
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum MemoryCellType {
|
|
|
|
Instruction,
|
|
|
|
InvalidInstruction,
|
|
|
|
Register,
|
|
|
|
Memory,
|
|
|
|
Constant,
|
|
|
|
}
|
2024-02-16 08:44:20 +00:00
|
|
|
|
|
|
|
// Handily explained in https://www.cgjennings.ca/articles/typescript-events/
|
|
|
|
interface CpuEventMap {
|
|
|
|
[CpuEvent.MemoryChanged]: { address: u8; value: u8 };
|
|
|
|
[CpuEvent.RegisterChanged]: { register_no: u8; value: u8 };
|
|
|
|
[CpuEvent.ProgramCounterChanged]: { counter: u8 };
|
|
|
|
[CpuEvent.Reset]: null;
|
|
|
|
[CpuEvent.MemoryByteParsed]: { type: MemoryCellType; pos: u8; code: u8; param?: ParameterType; instr?: Instruction };
|
|
|
|
[CpuEvent.InstructionExecuted]: { instr: Instruction };
|
|
|
|
[CpuEvent.Print]: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CpuEventHandler extends EventHandler<CpuEvent> {
|
|
|
|
listen<E extends keyof CpuEventMap>(type: E, listener: (ev: CpuEventMap[E]) => void): void;
|
|
|
|
dispatch<E extends keyof CpuEventMap>(type: E, data: CpuEventMap[E]): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum UiEvent {
|
|
|
|
RequestCpuCycle,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UiEventMap {
|
|
|
|
[UiEvent.RequestCpuCycle]: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UiEventHandler extends EventHandler<UiEvent> {
|
|
|
|
listen<E extends keyof UiEventMap>(type: E, listener: (ev: UiEventMap[E]) => void): void;
|
|
|
|
dispatch<E extends keyof UiEventMap>(type: E, data: UiEventMap[E]): void;
|
|
|
|
}
|