diff options
| author | Uri Shaked | 2020-07-16 19:34:28 +0300 |
|---|---|---|
| committer | Uri Shaked | 2020-07-16 19:51:59 +0300 |
| commit | 3685e1a9b43f8c79d1e0a5554a1a15d4d0c77142 (patch) | |
| tree | 365bfa8634f75c1ef4fc84ecbc4ab290f4d2734a /src/utils/test-utils.ts | |
| parent | test(timer): remove stray console.log (diff) | |
| download | avr8js-3685e1a9b43f8c79d1e0a5554a1a15d4d0c77142.tar.gz avr8js-3685e1a9b43f8c79d1e0a5554a1a15d4d0c77142.tar.bz2 avr8js-3685e1a9b43f8c79d1e0a5554a1a15d4d0c77142.zip | |
feat(eeprom): implement EEPROM peripheral
close #15
Diffstat (limited to 'src/utils/test-utils.ts')
| -rw-r--r-- | src/utils/test-utils.ts | 31 |
1 files changed, 31 insertions, 0 deletions
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(); + } + } +} |
