Skip to content

Commit 52c4caa

Browse files
committed
test(usart): more USART tests
1 parent 02373fe commit 52c4caa

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/usart.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,60 @@ describe('USART', () => {
5757
cpu.writeData(0xc1, 0x4);
5858
expect(usart.bitsPerChar).toEqual(9);
5959
});
60+
61+
it('should invoke onByteTransmit when UDR0 is written to', () => {
62+
const cpu = new CPU(new Uint16Array(1024));
63+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
64+
usart.onByteTransmit = jest.fn();
65+
cpu.writeData(0xc1, 0x8); // UCSR0B <- TXEN
66+
cpu.writeData(0xc6, 0x61); // UDR0
67+
expect(usart.onByteTransmit).toHaveBeenCalledWith(0x61);
68+
});
69+
70+
it('should set UDRE and TXC flags after UDR0', () => {
71+
const cpu = new CPU(new Uint16Array(1024));
72+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
73+
cpu.writeData(0xc1, 0x8); // UCSR0B <- TXEN
74+
cpu.writeData(0xc0, 0); // UCSR0A <- 0
75+
cpu.writeData(0xc6, 0x61); // UDR0
76+
expect(cpu.data[0xc0]).toEqual(0x40 | 0x20); // UCSR0A: TXC | UDRE
77+
});
78+
79+
describe('tick()', () => {
80+
it('should trigger data register empty interrupt if UDRE is set', () => {
81+
const cpu = new CPU(new Uint16Array(1024));
82+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
83+
cpu.writeData(0xc1, 0x28); // UCSR0B <- UDRIE | TXEN
84+
cpu.writeData(0xc6, 0x61); // UDR0
85+
cpu.data[95] = 0x80; // SREG: I-------
86+
usart.tick();
87+
expect(cpu.pc).toEqual(0x26);
88+
expect(cpu.cycles).toEqual(2);
89+
expect(cpu.data[0xc0] & 0x20).toEqual(0); // UCSR0A should clear UDRE
90+
});
91+
92+
it('should trigger data tx complete interrupt if TXCIE is set', () => {
93+
const cpu = new CPU(new Uint16Array(1024));
94+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
95+
cpu.writeData(0xc1, 0x48); // UCSR0B <- TXCIE | TXEN
96+
cpu.writeData(0xc6, 0x61); // UDR0
97+
cpu.data[95] = 0x80; // SREG: I-------
98+
usart.tick();
99+
expect(cpu.pc).toEqual(0x28);
100+
expect(cpu.cycles).toEqual(2);
101+
expect(cpu.data[0xc0] & 0x40).toEqual(0); // UCSR0A should clear TXC
102+
});
103+
104+
it('should not trigger any interrupt if interrupts are disabled', () => {
105+
const cpu = new CPU(new Uint16Array(1024));
106+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
107+
cpu.writeData(0xc1, 0x28); // UCSR0B <- UDRIE | TXEN
108+
cpu.writeData(0xc6, 0x61); // UDR0
109+
cpu.data[95] = 0; // SREG: 0 (disable interrupts)
110+
usart.tick();
111+
expect(cpu.pc).toEqual(0);
112+
expect(cpu.cycles).toEqual(0);
113+
expect(cpu.data[0xc0]).toEqual(0x40 | 0x20); // UCSR0A: TXC | UDRE
114+
});
115+
});
60116
});

src/usart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class AVRUSART {
5959
public onByteTransmit: USARTTransmitCallback | null = null;
6060

6161
constructor(private cpu: CPU, private config: USARTConfig, private freqMHz: number) {
62-
this.cpu.writeHooks[config.UCSRA] = (value, oldValue) => {
62+
this.cpu.writeHooks[config.UCSRA] = (value) => {
6363
this.cpu.data[config.UCSRA] = value | UCSRA_UDRE | UCSRA_TXC;
6464
return true;
6565
};

0 commit comments

Comments
 (0)