blob: 2bec178398a1d25774baa246db2a69ab90ae291e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import { CPU } from '../cpu/cpu';
import { assemble } from './assembler';
import { avrInstruction } from '../cpu/instruction';
const BREAK_OPCODE = 0x9598;
export function asmProgram(source: string) {
const { bytes, errors, lines } = assemble(source);
if (errors.length) {
throw new Error('Assembly failed: ' + errors);
}
return { program: new Uint16Array(bytes.buffer), lines, instructionCount: lines.length };
}
export class TestProgramRunner {
constructor(
private readonly cpu: CPU,
private readonly peripheral: { tick: () => void },
private readonly onBreak?: (cpu: CPU) => void
) {}
runInstructions(count: number) {
const { cpu, peripheral, onBreak } = this;
for (let i = 0; i < count; i++) {
if (cpu.progMem[cpu.pc] === BREAK_OPCODE) {
onBreak?.(cpu);
throw new Error('BREAK instruction encountered');
}
avrInstruction(cpu);
peripheral.tick();
}
}
runToBreak(maxIterations = 5000) {
const { cpu, peripheral, onBreak } = this;
for (let i = 0; i < maxIterations; i++) {
if (cpu.progMem[cpu.pc] === BREAK_OPCODE) {
onBreak?.(cpu);
return;
}
avrInstruction(cpu);
peripheral.tick();
}
throw new Error('Program ran for too long without a BREAK instruction');
}
}
|