我正在用 Typescript/React 编写一个 CPU 模拟器。我有一个CodeExecutionView,CPU和Terminal。
现在,当CPU触发适当的指令时,我想将一些数据写入Terminal. 我要写入的数据驻留在CPUstate. 我用来向终端写入数据的功能在组件中TerminalView。我如何传递该函数供班级CPU使用?
以下是我的代码结构:
___________
| |
| MAIN VIEW |
|(component)|
|___________|
___________ ___________
| | | |
| CPU | | TERMINAL |
| (class) | |(component)|
|___________| |___________|
主要成分:
type ExecutionState = {
assemblerOutput: Array<{ line: ParsedLine; bytes: Array<HexNum> }>;
currentInstruction: Instruction;
currentPC: number;
currentInstructionLength: number;
cpuState: CPUState;
}
type ExecutionProps = {
assemblerOutput: Array<{ line: ParsedLine; bytes: Array<HexNum> }>;
}
export default class ExecutionScreen extends React.Component<ExecutionProps, ExecutionState> {
state: ExecutionState = {
assemblerOutput: [],
currentInstruction: undefined,
currentPC: 0,
currentInstructionLength: 0,
cpuState: undefined
}
private CPU: CPU;
constructor(props: ExecutionProps) {
super(props);
this.CPU = new CPU(props.assemblerOutput);
this.state = {
assemblerOutput: props.assemblerOutput,
currentInstruction: this.CPU.currentInstruction,
currentPC: this.CPU.prevPC,
currentInstructionLength: undefined,
cpuState: this.CPU.getFullState()
};
}
runNextInstruction(): void {
this.CPU.executeInstruction();
this.setState({ ...this.state, currentInstruction: this.CPU.currentInstruction, currentInstructionLength: this.CPU.currentInstruction.size, currentPC: this.CPU.prevPC, cpuState: this.CPU.getFullState() });
}
我不想更改代码的结构,因为我需要 CPU 类存在于主视图中才能使其他东西正常工作
慕田峪7331174
相关分类