aboutsummaryrefslogtreecommitdiff
path: root/src/utils/test-utils.ts
blob: e620748c5b592f0fddcb4f5840998e30afe06fcb (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
47
48
49
50
51
52
53
54
55
56
57
58
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, labels } = assemble(source);
  if (errors.length) {
    throw new Error('Assembly failed: ' + errors);
  }
  return { program: new Uint16Array(bytes.buffer), lines, instructionCount: lines.length, labels };
}

const defaultOnBreak = () => {
  throw new Error('BREAK instruction encountered');
};

export class TestProgramRunner {
  constructor(
    private readonly cpu: CPU,
    private readonly onBreak: (cpu: CPU) => void = defaultOnBreak
  ) {}

  runInstructions(count: number) {
    const { cpu, onBreak } = this;
    for (let i = 0; i < count; i++) {
      if (cpu.progMem[cpu.pc] === BREAK_OPCODE) {
        onBreak?.(cpu);
      }
      avrInstruction(cpu);
      cpu.tick();
    }
  }

  runUntil(predicate: (cpu: CPU) => boolean, maxIterations = 5000) {
    const { cpu, onBreak } = this;
    for (let i = 0; i < maxIterations; i++) {
      if (cpu.progMem[cpu.pc] === BREAK_OPCODE) {
        onBreak?.(cpu);
      }
      if (predicate(cpu)) {
        return;
      }
      avrInstruction(cpu);
      cpu.tick();
    }
    throw new Error('Test program ran for too long, check your predicate');
  }

  runToBreak() {
    this.runUntil((cpu) => cpu.progMem[cpu.pc] === BREAK_OPCODE);
  }

  runToAddress(byteAddr: number) {
    this.runUntil((cpu) => cpu.pc * 2 === byteAddr);
  }
}