Rejig serial periph

This commit is contained in:
2026-02-24 15:53:43 +00:00
parent 0608e2c476
commit d11805cd6c
2 changed files with 23 additions and 34 deletions

30
emu.js
View File

@@ -1,15 +1,13 @@
const TXBUF = 0;
const RXBUF = 32;
const TXDATA = 64;
const TXHEAD = 65;
const TXTAIL = 66;
const RXDATA = 67;
const RXHEAD = 68;
const RXTAIL = 69;
const TXHEAD = 64;
const TXTAIL = 65;
const RXHEAD = 66;
const RXTAIL = 67;
const TXBUF_SIZE = 32;
const RXBUF_SIZE = 32;
const PERIPHS_SIZE = 70;
const PERIPHS_SIZE = 68;
const POLL_INTERVAL_MS = 20;
@@ -36,29 +34,21 @@ class Emulator {
}
poll() {
const txdata = Atomics.load(this.mem_u8, TXDATA);
if (txdata !== 0)
this.handle_txdata();
const txhead = Atomics.load(this.mem_u8, TXHEAD);
const txtail = Atomics.load(this.mem_u8, TXTAIL);
if (txhead !== txtail)
this.handle_txdata(txhead, txtail);
}
handle_txdata() {
const head = Atomics.load(this.mem_u8, TXHEAD);
const tail = Atomics.load(this.mem_u8, TXTAIL);
handle_txdata(head, tail) {
const data = [];
let i = head;
do {
data.push(this.mem_u8[TXBUF + i]);
i = (i + 1) % TXBUF_SIZE;
} while (i !== tail);
Atomics.store(this.mem_u8, TXHEAD, tail);
// More data could have been added -- only clear TXDATA if
// tail is unchanged.
if (tail === Atomics.load(this.mem_u8, TXTAIL))
Atomics.store(this.mem_u8, TXDATA, 0);
const str = this.decoder.decode(new Uint8Array(data));
this.output.innerText += str;
}