From 3685e1a9b43f8c79d1e0a5554a1a15d4d0c77142 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Thu, 16 Jul 2020 19:34:28 +0300 Subject: feat(eeprom): implement EEPROM peripheral close #15 --- src/utils/test-utils.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/utils/test-utils.ts (limited to 'src/utils/test-utils.ts') diff --git a/src/utils/test-utils.ts b/src/utils/test-utils.ts new file mode 100644 index 0000000..dfb6b32 --- /dev/null +++ b/src/utils/test-utils.ts @@ -0,0 +1,31 @@ +import { CPU } from '../cpu/cpu'; +import { assemble } from './assembler'; +import { avrInstruction } from '../cpu/instruction'; + +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 }; +} + +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] === 0x9598) { + onBreak?.(cpu); + throw new Error('BREAK instruction encountered'); + } + avrInstruction(cpu); + peripheral.tick(); + } + } +} -- cgit v1.2.3