diff --git a/emu.js b/emu.js index 0558a0d..03e6477 100644 --- a/emu.js +++ b/emu.js @@ -27,6 +27,7 @@ class Emulator { this.mem_u8 = new Uint8Array(this.mem.buffer); for (let i = 0; i < PERIPHS_SIZE; ++i) this.mem_u8[i] = 0; + this.mem_i32 = new Int32Array(this.mem.buffer); this.decoder = new TextDecoder('utf-8'); this.encoder = new TextEncoder('utf-8'); @@ -103,6 +104,7 @@ class Emulator { tail = this.fifo_next(tail); } while (this.fifo_next(tail) != head && this.rx_queue.length != 0); Atomics.store(this.mem_u8, RXTAIL, tail); + Atomics.notify(this.mem_i32, RXTAIL / 4); } print(str) { diff --git a/wipforth.wat b/wipforth.wat index fb9e8f9..ede59f4 100644 --- a/wipforth.wat +++ b/wipforth.wat @@ -628,15 +628,24 @@ ;; Serial I/O (func $key (local $head i32) + global.get $RXHEAD + i32.atomic.load8_u + local.tee $head + ;; Wait for RXBUF to be non-empty - loop $wait - global.get $RXHEAD - i32.atomic.load8_u - local.tee $head + loop $wait (param i32) global.get $RXTAIL i32.atomic.load8_u i32.eq - br_if $wait + if + global.get $RXTAIL + local.get $head + i64.const -1 + memory.atomic.wait32 + + local.get $head + br $wait + end end ;; Read byte at head position