diff options
| author | Uri Shaked | 2020-12-09 15:46:53 +0200 |
|---|---|---|
| committer | Uri Shaked | 2020-12-09 15:49:41 +0200 |
| commit | 9c1288f18889ae3bd10869a9f6ebc53defa3024b (patch) | |
| tree | 1857fe48d3e2d32a39cfe810a0dfdd7d96526b3a /src/peripherals/usart.ts | |
| parent | refactor: central interrupt handling #38 (diff) | |
| download | avr8js-9c1288f18889ae3bd10869a9f6ebc53defa3024b.tar.gz avr8js-9c1288f18889ae3bd10869a9f6ebc53defa3024b.tar.bz2 avr8js-9c1288f18889ae3bd10869a9f6ebc53defa3024b.zip | |
perf!: centeral timekeeping
This should improve performance, especially when running simulations with
multiple peripherals. For instance, the demo project now runs at ~322%,
up from ~185% in AVR8js 0.13.1.
BREAKING CHANGE: `tick()` methods were removed from individual peripherals.
You now need to call `cpu.tick()` instead.
Diffstat (limited to '')
| -rw-r--r-- | src/peripherals/usart.ts | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/src/peripherals/usart.ts b/src/peripherals/usart.ts index c1441ad..c7dcbd4 100644 --- a/src/peripherals/usart.ts +++ b/src/peripherals/usart.ts @@ -87,8 +87,6 @@ export class AVRUSART { enableMask: UCSRB_TXCIE, }; - private txCompleteCycles = 0; - constructor(private cpu: CPU, private config: USARTConfig, private freqMHz: number) { this.reset(); this.cpu.writeHooks[config.UCSRA] = (value) => { @@ -119,7 +117,11 @@ export class AVRUSART { } } const symbolsPerChar = 1 + this.bitsPerChar + this.stopBits + (this.parityEnabled ? 1 : 0); - this.txCompleteCycles = this.cpu.cycles + (this.UBRR * this.multiplier + 1) * symbolsPerChar; + const cyclesToComplete = (this.UBRR * this.multiplier + 1) * symbolsPerChar; + this.cpu.addClockEvent(() => { + cpu.setInterruptFlag(this.UDRE); + cpu.setInterruptFlag(this.TXC); + }, cyclesToComplete); this.cpu.clearInterrupt(this.TXC); this.cpu.clearInterrupt(this.UDRE); }; @@ -131,15 +133,6 @@ export class AVRUSART { this.cpu.data[this.config.UCSRC] = UCSRC_UCSZ1 | UCSRC_UCSZ0; // default: 8 bits per byte } - tick() { - const { txCompleteCycles, cpu } = this; - if (txCompleteCycles && cpu.cycles >= txCompleteCycles) { - cpu.setInterruptFlag(this.UDRE); - cpu.setInterruptFlag(this.TXC); - this.txCompleteCycles = 0; - } - } - private get UBRR() { return (this.cpu.data[this.config.UBRRH] << 8) | this.cpu.data[this.config.UBRRL]; } |
