Skip to content

Commit 92898c6

Browse files
committed
feat(usart): add onLineTransmit callback
1 parent a51446c commit 92898c6

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/usart.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,47 @@ describe('USART', () => {
113113
expect(cpu.data[0xc0]).toEqual(0x40 | 0x20); // UCSR0A: TXC | UDRE
114114
});
115115
});
116+
117+
describe('onLineTransmit', () => {
118+
it('should call onLineTransmit with the current line buffer after every newline', () => {
119+
const cpu = new CPU(new Uint16Array(1024));
120+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
121+
usart.onLineTransmit = jest.fn();
122+
cpu.writeData(0xc1, 0x8); // UCSR0B <- TXEN
123+
cpu.writeData(0xc6, 0x48); // 'H'
124+
cpu.writeData(0xc6, 0x65); // 'e'
125+
cpu.writeData(0xc6, 0x6c); // 'l'
126+
cpu.writeData(0xc6, 0x6c); // 'l'
127+
cpu.writeData(0xc6, 0x6f); // 'o'
128+
cpu.writeData(0xc6, 0xa); // '\n'
129+
expect(usart.onLineTransmit).toHaveBeenCalledWith('Hello');
130+
});
131+
132+
it('should not call onLineTransmit if no newline was received', () => {
133+
const cpu = new CPU(new Uint16Array(1024));
134+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
135+
usart.onLineTransmit = jest.fn();
136+
cpu.writeData(0xc1, 0x8); // UCSR0B <- TXEN
137+
cpu.writeData(0xc6, 0x48); // 'H'
138+
cpu.writeData(0xc6, 0x69); // 'i'
139+
expect(usart.onLineTransmit).not.toHaveBeenCalled();
140+
});
141+
142+
it('should clear the line buffer after each call to onLineTransmit', () => {
143+
const cpu = new CPU(new Uint16Array(1024));
144+
const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ);
145+
usart.onLineTransmit = jest.fn();
146+
cpu.writeData(0xc1, 0x8); // UCSR0B <- TXEN
147+
cpu.writeData(0xc6, 0x48); // 'H'
148+
cpu.writeData(0xc6, 0x69); // 'i'
149+
cpu.writeData(0xc6, 0xa); // '\n'
150+
cpu.writeData(0xc6, 0x74); // 't'
151+
cpu.writeData(0xc6, 0x68); // 'h'
152+
cpu.writeData(0xc6, 0x65); // 'e'
153+
cpu.writeData(0xc6, 0x72); // 'r'
154+
cpu.writeData(0xc6, 0x65); // 'e'
155+
cpu.writeData(0xc6, 0xa); // '\n'
156+
expect(usart.onLineTransmit).toHaveBeenCalledWith('there');
157+
});
158+
});
116159
});

src/usart.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const usart0Config: USARTConfig = {
2828
};
2929

3030
export type USARTTransmitCallback = (value: u8) => void;
31+
export type USARTLineTransmitCallback = (value: string) => void;
3132

3233
// Register bits
3334
const UCSRA_RXC = 0x80; // USART Receive Complete
@@ -57,6 +58,9 @@ const UCSRC_UCPOL = 0x1; // Clock Polarity
5758

5859
export class AVRUSART {
5960
public onByteTransmit: USARTTransmitCallback | null = null;
61+
public onLineTransmit: USARTLineTransmitCallback | null = null;
62+
63+
private lineBuffer: string = '';
6064

6165
constructor(private cpu: CPU, private config: USARTConfig, private freqMHz: number) {
6266
this.cpu.writeHooks[config.UCSRA] = (value) => {
@@ -73,6 +77,15 @@ export class AVRUSART {
7377
if (this.onByteTransmit) {
7478
this.onByteTransmit(value);
7579
}
80+
if (this.onLineTransmit) {
81+
const ch = String.fromCharCode(value);
82+
if (ch === '\n') {
83+
this.onLineTransmit(this.lineBuffer);
84+
this.lineBuffer = '';
85+
} else {
86+
this.lineBuffer += ch;
87+
}
88+
}
7689
this.cpu.data[config.UCSRA] |= UCSRA_UDRE | UCSRA_TXC;
7790
};
7891
}

0 commit comments

Comments
 (0)