diff options
| author | Uri Shaked | 2020-12-12 00:51:42 +0200 |
|---|---|---|
| committer | Uri Shaked | 2020-12-12 01:53:13 +0200 |
| commit | 60f41552ff1d3593b75eb73df4bb62fe1f9bee23 (patch) | |
| tree | 22fe5845f48a29f483194b3119988a51ae4639ab /src/peripherals/timer.spec.ts | |
| parent | 0.14.0 (diff) | |
| download | avr8js-60f41552ff1d3593b75eb73df4bb62fe1f9bee23.tar.gz avr8js-60f41552ff1d3593b75eb73df4bb62fe1f9bee23.tar.bz2 avr8js-60f41552ff1d3593b75eb73df4bb62fe1f9bee23.zip | |
fix(timer): Incorrect count when stopping a timer
fix #72
Diffstat (limited to 'src/peripherals/timer.spec.ts')
| -rw-r--r-- | src/peripherals/timer.spec.ts | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/peripherals/timer.spec.ts b/src/peripherals/timer.spec.ts index 40db1f6..43768d2 100644 --- a/src/peripherals/timer.spec.ts +++ b/src/peripherals/timer.spec.ts @@ -83,7 +83,7 @@ describe('timer', () => { cpu.writeData(TCCR0B, CS01 | CS00); // Set prescaler to 64 cpu.cycles = 1; cpu.tick(); - cpu.cycles = 64; + cpu.cycles = 1 + 64; cpu.tick(); const tcnt = cpu.readData(TCNT0); expect(tcnt).toEqual(1); @@ -353,11 +353,11 @@ describe('timer', () => { cpu.cycles = 1; cpu.tick(); - cpu.cycles = 511; + cpu.cycles = 1 + 511; cpu.tick(); expect(cpu.readData(TCNT2)).toEqual(1); - cpu.cycles = 512; + cpu.cycles = 1 + 512; cpu.tick(); expect(cpu.readData(TCNT2)).toEqual(2); }); @@ -366,7 +366,7 @@ describe('timer', () => { const { program, instructionCount } = asmProgram(` LDI r16, 0x1 ; TCCR0B = 1 << CS00 OUT 0x25, r16 - LDI r16, 0x0 ; TCNT0 <- 0x30 + LDI r16, 0x0 ; TCNT0 <- 0 OUT 0x26, r16 NOP LDS r1, 0x46 ; r1 <- TCNT0 (2 cycles) @@ -396,6 +396,22 @@ describe('timer', () => { expect(cpu.readData(R17)).toEqual(2); }); + it('should not keep counting for one more instruction when the timer is disabled (issue #72)', () => { + const { program, instructionCount } = asmProgram(` + EOR r1, r1 ; r1 = 0; + LDI r16, 0x1 ; TCCR2B = 1 << CS20; + STS 0xb1, r16 ; Should start counting after this instruction, + STS 0xb1, r1 ; and stop counting *after* this one. + NOP + LDS r17, 0xb2 ; TCNT2 should equal 2 at this point (not counting the NOP) + `); + const cpu = new CPU(program); + new AVRTimer(cpu, timer2Config); + const runner = new TestProgramRunner(cpu); + runner.runInstructions(instructionCount); + expect(cpu.readData(R17)).toEqual(2); + }); + describe('Phase-correct PWM mode', () => { it('should count up to TOP, down to 0, and then set TOV flag', () => { const { program, instructionCount } = asmProgram(` |
