Skip to content

Commit

Permalink
fix(timer): delay() is inaccurate #81
Browse files Browse the repository at this point in the history
fix #81
  • Loading branch information
urish committed Dec 29, 2020
1 parent cdc8bd9 commit 19e53fa
Showing 2 changed files with 26 additions and 21 deletions.
38 changes: 25 additions & 13 deletions src/peripherals/timer.spec.ts
Original file line number Diff line number Diff line change
@@ -101,14 +101,20 @@ describe('timer', () => {
expect(tcnt).toEqual(0); // TCNT should stay 0
});

it('should set the TOV flag when timer reaches the TOP value', () => {
it('should set the TOV flag when timer wraps above TOP value', () => {
const cpu = new CPU(new Uint16Array(0x1000));
new AVRTimer(cpu, timer0Config);
cpu.writeData(TCNT0, 0xff);
cpu.writeData(TCCR0B, CS00); // Set prescaler to 1

cpu.cycles = 1;
cpu.tick();
expect(cpu.readData(TCNT0)).toEqual(0xff);
expect(cpu.data[TIFR0] & TOV0).toEqual(0);

cpu.cycles++;
cpu.tick();
expect(cpu.readData(TCNT0)).toEqual(0);
expect(cpu.data[TIFR0] & TOV0).toEqual(TOV0);
});

@@ -294,11 +300,13 @@ describe('timer', () => {
cpu.writeData(TCCR0B, CS00); // Set prescaler to 1
cpu.cycles = 1;
cpu.tick();
cpu.cycles = 2;
cpu.cycles++;
cpu.tick();
cpu.cycles++;
cpu.tick();
const tcnt = cpu.readData(TCNT0);
expect(tcnt).toEqual(0x1f);
expect(cpu.data[TIFR0]).toEqual(OCF0A); // TOV0 clear
expect(tcnt).toEqual(0);
expect(cpu.data[TIFR0] & TOV0).toEqual(0); // TOV0 clear
});

it('should set the TOV bit when TOP == MAX in CTC mode (issue #75)', () => {
@@ -310,11 +318,16 @@ describe('timer', () => {
cpu.writeData(TCCR0B, CS00); // Set prescaler to 1
cpu.cycles = 1;
cpu.tick();
cpu.cycles = 2;

cpu.cycles++;
cpu.tick();
const tcnt = cpu.readData(TCNT0);
expect(tcnt).toEqual(0xff);
expect(cpu.data[TIFR0]).toEqual(OCF0A | TOV0);
expect(cpu.readData(TCNT0)).toEqual(0xff);
expect(cpu.data[TIFR0] & TOV0).toEqual(0); // TOV clear

cpu.cycles++;
cpu.tick();
expect(cpu.readData(TCNT0)).toEqual(0);
expect(cpu.data[TIFR0] & TOV0).toEqual(TOV0); // TOV set
});

it('should not set the TOV bit twice on overflow (issue #80)', () => {
@@ -328,16 +341,15 @@ describe('timer', () => {
cpu.cycles = 1;
cpu.tick();

cpu.cycles = 2;
cpu.cycles++;
cpu.tick();
expect(cpu.readData(TCNT0)).toEqual(0xff);
expect(cpu.data[TIFR0] & TOV0).toEqual(TOV0);
cpu.data[TIFR0] &= ~TOV0; // Clear the TOV0 bit
expect(cpu.data[TIFR0] & TOV0).toEqual(0); // TOV clear

cpu.cycles = 3;
cpu.cycles++;
cpu.tick();
expect(cpu.readData(TCNT0)).toEqual(0);
expect(cpu.data[TIFR0] & TOV0).toEqual(0);
expect(cpu.data[TIFR0] & TOV0).toEqual(TOV0); // TOV set
});

it('should set OCF0B flag when timer equals OCRB', () => {
9 changes: 1 addition & 8 deletions src/peripherals/timer.ts
Original file line number Diff line number Diff line change
@@ -481,20 +481,13 @@ export class AVRTimer {

// OCRUpdateMode.Bottom only occurs in Phase Correct modes, handled by phasePwmCount().
// Thus we only handle TOVUpdateMode.Top or TOVUpdateMode.Max here.
if (
(newVal === TOP || (overflow && val < TOP)) &&
(this.tovUpdateMode == TOVUpdateMode.Top || TOP === this.MAX)
) {
if (overflow && (this.tovUpdateMode == TOVUpdateMode.Top || TOP === this.MAX)) {
cpu.setInterruptFlag(this.OVF);
}
}
}
if (this.tcntUpdated) {
const { TOP } = this;
this.tcnt = this.tcntNext;
if (this.tcnt === TOP && (this.tovUpdateMode == TOVUpdateMode.Top || TOP === this.MAX)) {
cpu.setInterruptFlag(this.OVF);
}
this.tcntUpdated = false;
}
if (this.updateDivider) {

0 comments on commit 19e53fa

Please sign in to comment.