2024-02-22 05:29:29 +00:00
|
|
|
/**
|
|
|
|
* @file Specific definitions of the event handlers (CPU & UI) used within this program
|
|
|
|
* @copyright Alexander Bass 2024
|
|
|
|
* @license GPL-3.0
|
|
|
|
*/
|
2024-02-16 08:44:20 +00:00
|
|
|
import { EventHandler } from "./eventHandler";
|
|
|
|
import { Instruction, ParameterType } from "./instructionSet";
|
2024-03-06 06:46:29 +00:00
|
|
|
import { u2, u3, u8 } from "./num";
|
2024-02-16 08:44:20 +00:00
|
|
|
|
2024-02-22 05:29:29 +00:00
|
|
|
//
|
|
|
|
// CPU Event Handler Definition
|
|
|
|
//
|
2024-02-16 05:17:23 +00:00
|
|
|
export enum CpuEvent {
|
|
|
|
MemoryChanged,
|
|
|
|
RegisterChanged,
|
|
|
|
ProgramCounterChanged,
|
2024-02-21 05:45:14 +00:00
|
|
|
InstructionParsed,
|
|
|
|
ParameterParsed,
|
|
|
|
InvalidParsed,
|
2024-02-16 08:44:20 +00:00
|
|
|
InstructionExecuted,
|
2024-02-22 05:29:29 +00:00
|
|
|
Cycle,
|
2024-02-16 05:17:23 +00:00
|
|
|
Print,
|
|
|
|
Reset,
|
2024-03-26 03:03:20 +00:00
|
|
|
SoftReset,
|
2024-02-21 05:45:14 +00:00
|
|
|
Halt,
|
2024-02-23 06:37:12 +00:00
|
|
|
MemoryAccessed,
|
|
|
|
SwitchBank,
|
2024-02-25 01:33:54 +00:00
|
|
|
SetFlagCarry,
|
2024-03-13 06:01:33 +00:00
|
|
|
SetVramBank,
|
2024-02-16 05:17:23 +00:00
|
|
|
}
|
2024-02-16 08:44:20 +00:00
|
|
|
|
2024-03-26 03:03:20 +00:00
|
|
|
type VoidDataCpuEventList = CpuEvent.Halt | CpuEvent.Reset | CpuEvent.SoftReset | CpuEvent.Cycle;
|
2024-02-22 05:29:29 +00:00
|
|
|
|
2024-02-16 08:44:20 +00:00
|
|
|
interface CpuEventMap {
|
2024-02-25 01:33:54 +00:00
|
|
|
[CpuEvent.MemoryChanged]: { address: u8; bank: u2; value: u8 };
|
|
|
|
[CpuEvent.MemoryAccessed]: { address: u8; bank: u2; value: u8 };
|
2024-02-22 05:29:29 +00:00
|
|
|
[CpuEvent.RegisterChanged]: { register_no: u3; value: u8 };
|
2024-02-16 08:44:20 +00:00
|
|
|
[CpuEvent.ProgramCounterChanged]: { counter: u8 };
|
2024-02-21 05:45:14 +00:00
|
|
|
[CpuEvent.InstructionParsed]: { pos: u8; code: u8; instr: Instruction };
|
|
|
|
[CpuEvent.ParameterParsed]: { pos: u8; code: u8; param: ParameterType };
|
|
|
|
[CpuEvent.InvalidParsed]: { pos: u8; code: u8 };
|
2024-02-16 08:44:20 +00:00
|
|
|
[CpuEvent.InstructionExecuted]: { instr: Instruction };
|
2024-02-25 01:33:54 +00:00
|
|
|
[CpuEvent.SwitchBank]: { bank: u2 };
|
2024-03-13 06:01:33 +00:00
|
|
|
[CpuEvent.SetVramBank]: { bank: u2 };
|
2024-02-16 08:44:20 +00:00
|
|
|
[CpuEvent.Print]: string;
|
2024-02-25 01:33:54 +00:00
|
|
|
[CpuEvent.SetFlagCarry]: boolean;
|
2024-02-16 08:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CpuEventHandler extends EventHandler<CpuEvent> {
|
2024-02-22 05:29:29 +00:00
|
|
|
listen<E extends VoidDataCpuEventList>(type: E, listener: () => void): void;
|
|
|
|
dispatch<E extends VoidDataCpuEventList>(type: E): void;
|
2024-02-16 08:44:20 +00:00
|
|
|
listen<E extends keyof CpuEventMap>(type: E, listener: (ev: CpuEventMap[E]) => void): void;
|
|
|
|
dispatch<E extends keyof CpuEventMap>(type: E, data: CpuEventMap[E]): void;
|
|
|
|
}
|
|
|
|
|
2024-02-22 05:29:29 +00:00
|
|
|
interface CpuEventHandlerConstructor {
|
|
|
|
new (): CpuEventHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CpuEventHandler = EventHandler<CpuEvent> as CpuEventHandlerConstructor;
|
|
|
|
|
|
|
|
//
|
2024-03-09 18:40:49 +00:00
|
|
|
// Ui -> CPU Signaler definition
|
2024-02-22 05:29:29 +00:00
|
|
|
//
|
|
|
|
|
2024-03-09 18:40:49 +00:00
|
|
|
export enum UiCpuSignal {
|
2024-02-16 08:44:20 +00:00
|
|
|
RequestCpuCycle,
|
2024-02-21 05:45:14 +00:00
|
|
|
RequestMemoryChange,
|
2024-03-06 06:46:29 +00:00
|
|
|
RequestRegisterChange,
|
2024-03-09 18:40:49 +00:00
|
|
|
RequestCpuReset,
|
2024-03-26 03:03:20 +00:00
|
|
|
RequestCpuSoftReset,
|
2024-03-09 18:40:49 +00:00
|
|
|
RequestMemoryDump,
|
2024-03-13 06:01:33 +00:00
|
|
|
RequestProgramCounterChange,
|
2024-03-09 18:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 03:03:20 +00:00
|
|
|
type VoidDataUiCpuSignalList = UiCpuSignal.RequestCpuReset | UiCpuSignal.RequestCpuSoftReset;
|
2024-03-09 18:40:49 +00:00
|
|
|
|
|
|
|
interface UiCpuSignalMap {
|
|
|
|
[UiCpuSignal.RequestCpuCycle]: number;
|
|
|
|
[UiCpuSignal.RequestMemoryChange]: { address: u8; bank: u2; value: u8 };
|
|
|
|
[UiCpuSignal.RequestRegisterChange]: { register_no: u3; value: u8 };
|
2024-03-13 06:01:33 +00:00
|
|
|
[UiCpuSignal.RequestProgramCounterChange]: { address: u8 };
|
|
|
|
[UiCpuSignal.RequestMemoryDump]: (memory: [Uint8Array, Uint8Array, Uint8Array, Uint8Array]) => void;
|
2024-03-09 18:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface UiCpuSignalHandler extends EventHandler<UiCpuSignal> {
|
|
|
|
listen<E extends VoidDataUiCpuSignalList>(type: E, listener: () => void): void;
|
|
|
|
dispatch<E extends VoidDataUiCpuSignalList>(type: E): void;
|
|
|
|
listen<E extends keyof UiCpuSignalMap>(type: E, listener: (ev: UiCpuSignalMap[E]) => void): void;
|
|
|
|
dispatch<E extends keyof UiCpuSignalMap>(type: E, data: UiCpuSignalMap[E]): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UICpuSignalHandlerConstructor {
|
|
|
|
new (): UiCpuSignalHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const UiCpuSignalHandler = EventHandler<UiCpuSignal> as UICpuSignalHandlerConstructor;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Ui Event Handler Definition
|
|
|
|
//
|
|
|
|
|
|
|
|
export enum UiEvent {
|
2024-03-06 06:46:29 +00:00
|
|
|
EditOn,
|
|
|
|
EditOff,
|
2024-03-09 18:40:49 +00:00
|
|
|
ChangeViewBank,
|
2024-02-16 08:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface UiEventMap {
|
2024-03-09 18:40:49 +00:00
|
|
|
[UiEvent.ChangeViewBank]: { bank: u2 };
|
2024-02-16 08:44:20 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:46:29 +00:00
|
|
|
type VoidDataUiEventList = UiEvent.EditOn | UiEvent.EditOff;
|
|
|
|
|
2024-02-16 08:44:20 +00:00
|
|
|
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;
|
2024-03-06 06:46:29 +00:00
|
|
|
listen<E extends VoidDataUiEventList>(type: E, listener: () => void): void;
|
|
|
|
dispatch<E extends VoidDataUiEventList>(type: E): void;
|
2024-02-16 08:44:20 +00:00
|
|
|
}
|
2024-02-22 05:29:29 +00:00
|
|
|
|
|
|
|
interface UiEventHandlerConstructor {
|
|
|
|
new (): UiEventHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const UiEventHandler = EventHandler<UiEvent> as UiEventHandlerConstructor;
|