Skip to content

Watchdog unit test seems to work not correctly #115

@Dudeplayz

Description

@Dudeplayz

While I was working at the peripherals, I discovered that the WDR command does not need to be run in order to finish all the watchdog unit tests. But one unit test is written to exactly test this.

it('should extend the watchdog timeout when executing a WDR instruction', () => {
const { program } = asmProgram(`
; register addresses
_REPLACE WDTCSR, ${WDTCSR}
; Setup watchdog
ldi r16, ${WDE | WDCE}
sts WDTCSR, r16
ldi r16, ${WDE}
sts WDTCSR, r16
wdr
nop
break
`);
const cpu = new CPU(program);
const clock = new AVRClock(cpu, 16e6, clockConfig);
const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock);
const runner = new TestProgramRunner(cpu);
// Setup: enable watchdog timer
runner.runInstructions(4);
expect(watchdog.enabled).toBe(true);
// Now we skip 8ms. Watchdog shouldn't fire, yet
cpu.cycles += 16000 * 8;
runner.runInstructions(1);
// Now we skip an extra 8ms. We extended the timeout with WDR, so watchdog won't fire yet
cpu.cycles += 16000 * 8;
runner.runInstructions(1);
// Finally, another 8ms bring us to 16ms since last WDR, and watchdog should fire
cpu.cycles += 16000 * 8;
cpu.tick();
expect(cpu.pc).toEqual(0);
});

If you comment out

this.resetWatchdog();

it is still successful, which should not be the case. So the question is, is the unit test written wrong or is there another reason?
Locally I have also enhanced the unit test with a spy on the callback:

    const callbackSpy = jest.spyOn(cpu, 'onWatchdogReset');

    // Setup: enable watchdog timer
    runner.runInstructions(4);
    expect(watchdog.enabled).toBe(true);
    expect(callbackSpy).not.toHaveBeenCalled();

    // Now we skip 8ms. Watchdog shouldn't fire, yet
    cpu.cycles += 16000 * 8;
    runner.runInstructions(1);
    expect(callbackSpy).toHaveBeenCalled();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions