diff --git a/src/vm/instruction.rs b/src/vm/instruction.rs index 980424f..2221aaa 100644 --- a/src/vm/instruction.rs +++ b/src/vm/instruction.rs @@ -312,6 +312,6 @@ fn trap_puts(vm: &mut VM) { } fn trap_getc(vm: &mut VM) { - while vm.mem.get_mem(0xFE00) & 1 == 0 {} + while vm.mem.get_mem(0xFE00) & (1 << 15) == 0 {} vm.registers.r0 = vm.mem.get_mem(0xFE02) & 0xFF; } diff --git a/src/vm/memory.rs b/src/vm/memory.rs index b5bfc57..901ec11 100644 --- a/src/vm/memory.rs +++ b/src/vm/memory.rs @@ -30,7 +30,13 @@ impl Memory<'_> { pub fn get_mem(&mut self, addr: u16) -> u16 { if addr >= 0xFE00 { return match addr { - 0xFE00 => self.io.check_key() as u16, + 0xFE00 => { + if self.io.check_key() { + 1 << 15 + } else { + 0 + } + } 0xFE02 => { let key = match self.io.get_key() { Some(key) => key as u16, diff --git a/src/vm/terminal_io.rs b/src/vm/terminal_io.rs index 288025e..9d90b81 100644 --- a/src/vm/terminal_io.rs +++ b/src/vm/terminal_io.rs @@ -29,6 +29,7 @@ pub trait KeyboardIO { pub struct TerminalIO { stdin_channel: Receiver, + char: Option, } impl TerminalIO { @@ -36,6 +37,7 @@ impl TerminalIO { setup_termios(); TerminalIO { stdin_channel: Self::spawn_stdin_channel(), + char: None, } } @@ -59,17 +61,14 @@ impl Drop for TerminalIO { impl KeyboardIO for TerminalIO { fn get_key(&mut self) -> Option { - match self.stdin_channel.try_recv() { - Ok(key) => Some(key), - Err(mpsc::TryRecvError::Empty) => None, - Err(mpsc::TryRecvError::Disconnected) => panic!("terminal keyboard stream broke"), - } + self.char } fn check_key(&mut self) -> bool { - match self.stdin_channel.try_iter().peekable().peek() { - Some(data) => true, - None => false, + match self.stdin_channel.try_recv() { + Ok(key) => { self.char = Some(key); true }, + Err(mpsc::TryRecvError::Empty) => false, + Err(mpsc::TryRecvError::Disconnected) => panic!("terminal keyboard stream broke"), } } }