aboutsummaryrefslogtreecommitdiff
path: root/src/peripherals/watchdog.spec.ts
blob: 6fea3b36b0da8a1c067ffdbdee4a5ae0c5b4b059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * AVR8 Watchdog Timer Test Suite
 * Part of AVR8js
 *
 * Copyright (C) 2021 Uri Shaked
 */

import { AVRClock, clockConfig } from '..';
import { CPU } from '../cpu/cpu';
import { asmProgram, TestProgramRunner } from '../utils/test-utils';
import { AVRWatchdog, watchdogConfig } from './watchdog';

const R20 = 20;

const MCUSR = 0x54;
const WDRF = 1 << 3;

const WDTCSR = 0x60;
const WDP0 = 1 << 0;
const WDP1 = 1 << 1;
const WDP2 = 1 << 2;
const WDE = 1 << 3;
const WDCE = 1 << 4;
const WDP3 = 1 << 5;
const WDIE = 1 << 6;

const INT_WDT = 0xc;

describe('Watchdog', () => {
  it('should correctly calculate the prescaler from WDTCSR', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const clock = new AVRClock(cpu, 16e6, clockConfig);
    const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock);
    cpu.writeData(WDTCSR, WDCE | WDE);
    cpu.writeData(WDTCSR, 0);
    expect(watchdog.prescaler).toEqual(2048);
    cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0);
    expect(watchdog.prescaler).toEqual(256 * 1024);
    cpu.writeData(WDTCSR, WDP3 | WDP0);
    expect(watchdog.prescaler).toEqual(1024 * 1024);
  });

  it('should not change the prescaler unless WDCE is set', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const clock = new AVRClock(cpu, 16e6, clockConfig);
    const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock);
    cpu.writeData(WDTCSR, 0);
    expect(watchdog.prescaler).toEqual(2048);
    cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0);
    expect(watchdog.prescaler).toEqual(2048);

    cpu.writeData(WDTCSR, WDCE | WDE);
    cpu.cycles += 5; // WDCE should expire after 4 cycles
    cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0);
    expect(watchdog.prescaler).toEqual(2048);
  });

  it('should reset the CPU when the timer expires', () => {
    const { program } = asmProgram(`
    ; register addresses
    _REPLACE WDTCSR, ${WDTCSR}

    ; Setup watchdog
    ldi r16, ${WDE | WDCE}
    sts WDTCSR, r16
    ldi r16, ${WDE}
    sts WDTCSR, r16
    
    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. Watchdog should fire and reset!
    cpu.cycles += 16000 * 8;
    cpu.tick();
    expect(cpu.pc).toEqual(0);
    expect(cpu.readData(MCUSR)).toEqual(WDRF);
  });

  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);
  });

  it('should fire an interrupt when the watchdog expires and WDIE is set', () => {
    const { program } = asmProgram(`
    ; register addresses
    _REPLACE WDTCSR, ${WDTCSR}

    ; Setup watchdog
    ldi r16, ${WDE | WDCE}
    sts WDTCSR, r16
    ldi r16, ${WDE | WDIE}
    sts WDTCSR, r16
    
    nop
    sei

    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. Watchdog should fire and jump to the interrupt handler
    cpu.cycles += 16000 * 8;
    runner.runInstructions(1);

    expect(cpu.pc).toEqual(INT_WDT);
    // The watchdog timer should also clean the WDIE bit, so next timeout will reset the MCU.
    expect(cpu.readData(WDTCSR) & WDIE).toEqual(0);
  });

  it('should not reset the CPU if the watchdog has been disabled', () => {
    const { program } = asmProgram(`
    ; register addresses
    _REPLACE WDTCSR, ${WDTCSR}

    ; Setup watchdog
    ldi r16, ${WDE | WDCE}
    sts WDTCSR, r16
    ldi r16, ${WDE}
    sts WDTCSR, r16
    
    ; disable watchdog
    ldi r16, ${WDE | WDCE}
    sts WDTCSR, r16
    ldi r16, 0
    sts WDTCSR, r16

    ldi r20, 55

    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. We disable it.
    cpu.cycles += 16000 * 8;
    runner.runInstructions(4);

    // Now we skip an extra 20ms. Watchdog shouldn't reset!
    cpu.cycles += 16000 * 20;
    runner.runInstructions(1);
    expect(cpu.pc).not.toEqual(0);
    expect(cpu.data[R20]).toEqual(55); // assert that `ldi r20, 55` ran
  });
});