diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/assembler.ts | 2 | ||||
| -rw-r--r-- | src/utils/test-utils.ts | 31 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/utils/assembler.ts b/src/utils/assembler.ts index 17b20b4..4823937 100644 --- a/src/utils/assembler.ts +++ b/src/utils/assembler.ts @@ -46,7 +46,7 @@ interface LineTablePass1 { byteOffset: number; } -interface LineTable extends LineTablePass1 { +export interface LineTable extends LineTablePass1 { bytes: string; } 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(); + } + } +} |
