aboutsummaryrefslogtreecommitdiff
path: root/src/peripherals/usart.spec.ts
blob: fb569671c025e590b305830545b3d3c9905c55e4 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { CPU } from '../cpu/cpu';
import { AVRUSART, usart0Config } from './usart';

const FREQ_16MHZ = 16e6;
const FREQ_11_0529MHZ = 11059200;

// CPU registers
const SREG = 95;

// USART0 Registers
const UCSR0A = 0xc0;
const UCSR0B = 0xc1;
const UCSR0C = 0xc2;
const UBRR0L = 0xc4;
const UBRR0H = 0xc5;
const UDR0 = 0xc6;

// Register bit names
const U2X0 = 2;
const TXEN = 8;
const UDRIE = 0x20;
const TXCIE = 0x40;
const TXC = 0x40;
const UDRE = 0x20;
const USBS = 0x08;
const UPM0 = 0x10;
const UPM1 = 0x20;

// Interrupt address
const PC_INT_UDRE = 0x26;
const PC_INT_TXC = 0x28;
const UCSZ0 = 2;
const UCSZ1 = 4;
const UCSZ2 = 4;

describe('USART', () => {
  it('should correctly calculate the baudRate from UBRR', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const usart = new AVRUSART(cpu, usart0Config, FREQ_11_0529MHZ);
    cpu.writeData(UBRR0H, 0);
    cpu.writeData(UBRR0L, 5);
    expect(usart.baudRate).toEqual(115200);
  });

  it('should correctly calculate the baudRate from UBRR in double-speed mode', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
    cpu.writeData(UBRR0H, 3);
    cpu.writeData(UBRR0L, 64);
    cpu.writeData(UCSR0A, U2X0);
    expect(usart.baudRate).toEqual(2400);
  });

  describe('bitsPerChar', () => {
    it('should return 5-bits per byte when UCSZ = 0', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, 0);
      expect(usart.bitsPerChar).toEqual(5);
    });

    it('should return 6-bits per byte when UCSZ = 1', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UCSZ0);
      expect(usart.bitsPerChar).toEqual(6);
    });

    it('should return 7-bits per byte when UCSZ = 2', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UCSZ1);
      expect(usart.bitsPerChar).toEqual(7);
    });

    it('should return 8-bits per byte when UCSZ = 3', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UCSZ0 | UCSZ1);
      expect(usart.bitsPerChar).toEqual(8);
    });

    it('should return 9-bits per byte when UCSZ = 7', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UCSZ0 | UCSZ1);
      cpu.writeData(UCSR0B, UCSZ2);
      expect(usart.bitsPerChar).toEqual(9);
    });
  });

  describe('stopBits', () => {
    it('should return 1 when USBS = 0', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      expect(usart.stopBits).toEqual(1);
    });

    it('should return 2 when USBS = 1', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, USBS);
      expect(usart.stopBits).toEqual(2);
    });
  });

  describe('parityEnabled', () => {
    it('should return false when UPM1 = 0', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      expect(usart.parityEnabled).toEqual(false);
    });

    it('should return true when UPM1 = 1', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UPM1);
      expect(usart.parityEnabled).toEqual(true);
    });
  });

  describe('parityOdd', () => {
    it('should return false when UPM0 = 0', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      expect(usart.parityOdd).toEqual(false);
    });

    it('should return true when UPM0 = 1', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0C, UPM0);
      expect(usart.parityOdd).toEqual(true);
    });
  });

  it('should invoke onByteTransmit when UDR0 is written to', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
    usart.onByteTransmit = jest.fn();
    cpu.writeData(UCSR0B, TXEN);
    cpu.writeData(UDR0, 0x61);
    expect(usart.onByteTransmit).toHaveBeenCalledWith(0x61);
  });

  describe('tick()', () => {
    it('should trigger data register empty interrupt if UDRE is set', () => {
      const cpu = new CPU(new Uint16Array(1024));
      new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0B, UDRIE | TXEN);
      cpu.data[SREG] = 0x80; // SREG: I-------
      cpu.tick();
      expect(cpu.pc).toEqual(PC_INT_UDRE);
      expect(cpu.cycles).toEqual(2);
      expect(cpu.data[UCSR0A] & UDRE).toEqual(0);
    });

    it('should trigger data TX Complete interrupt if TXCIE is set', () => {
      const cpu = new CPU(new Uint16Array(1024));
      new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0B, TXCIE | TXEN);
      cpu.writeData(UDR0, 0x61);
      cpu.data[SREG] = 0x80; // SREG: I-------
      cpu.cycles = 1e6;
      cpu.tick();
      expect(cpu.pc).toEqual(PC_INT_TXC);
      expect(cpu.cycles).toEqual(1e6 + 2);
      expect(cpu.data[UCSR0A] & TXC).toEqual(0);
    });

    it('should not trigger data TX Complete interrupt if UDR was not written to', () => {
      const cpu = new CPU(new Uint16Array(1024));
      new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0B, TXCIE | TXEN);
      cpu.data[SREG] = 0x80; // SREG: I-------
      cpu.tick();
      expect(cpu.pc).toEqual(0);
      expect(cpu.cycles).toEqual(0);
    });

    it('should not trigger any interrupt if interrupts are disabled', () => {
      const cpu = new CPU(new Uint16Array(1024));
      new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0B, UDRIE | TXEN);
      cpu.writeData(UDR0, 0x61);
      cpu.data[SREG] = 0; // SREG: 0 (disable interrupts)
      cpu.cycles = 1e6;
      cpu.tick();
      expect(cpu.pc).toEqual(0);
      expect(cpu.cycles).toEqual(1e6);
      expect(cpu.data[UCSR0A]).toEqual(TXC | UDRE);
    });
  });

  describe('onLineTransmit', () => {
    it('should call onLineTransmit with the current line buffer after every newline', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      usart.onLineTransmit = jest.fn();
      cpu.writeData(UCSR0B, TXEN);
      cpu.writeData(UDR0, 0x48); // 'H'
      cpu.writeData(UDR0, 0x65); // 'e'
      cpu.writeData(UDR0, 0x6c); // 'l'
      cpu.writeData(UDR0, 0x6c); // 'l'
      cpu.writeData(UDR0, 0x6f); // 'o'
      cpu.writeData(UDR0, 0xa); // '\n'
      expect(usart.onLineTransmit).toHaveBeenCalledWith('Hello');
    });

    it('should not call onLineTransmit if no newline was received', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      usart.onLineTransmit = jest.fn();
      cpu.writeData(UCSR0B, TXEN);
      cpu.writeData(UDR0, 0x48); // 'H'
      cpu.writeData(UDR0, 0x69); // 'i'
      expect(usart.onLineTransmit).not.toHaveBeenCalled();
    });

    it('should clear the line buffer after each call to onLineTransmit', () => {
      const cpu = new CPU(new Uint16Array(1024));
      const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      usart.onLineTransmit = jest.fn();
      cpu.writeData(UCSR0B, TXEN);
      cpu.writeData(UDR0, 0x48); // 'H'
      cpu.writeData(UDR0, 0x69); // 'i'
      cpu.writeData(UDR0, 0xa); // '\n'
      cpu.writeData(UDR0, 0x74); // 't'
      cpu.writeData(UDR0, 0x68); // 'h'
      cpu.writeData(UDR0, 0x65); // 'e'
      cpu.writeData(UDR0, 0x72); // 'r'
      cpu.writeData(UDR0, 0x65); // 'e'
      cpu.writeData(UDR0, 0xa); // '\n'
      expect(usart.onLineTransmit).toHaveBeenCalledWith('there');
    });
  });

  describe('integration', () => {
    it('should set the TXC bit after ~1.04mS when baud rate set to 9600', () => {
      const cpu = new CPU(new Uint16Array(1024));
      new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
      cpu.writeData(UCSR0B, TXEN);
      cpu.writeData(UBRR0L, 103); // baud: 9600
      cpu.writeData(UDR0, 0x48); // 'H'
      cpu.cycles += 16000; // 1ms
      cpu.tick();
      expect(cpu.data[UCSR0A] & TXC).toEqual(0);
      cpu.cycles += 800; // 0.05ms
      cpu.tick();
      expect(cpu.data[UCSR0A] & TXC).toEqual(TXC);
    });
  });
});