Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-terminal stdin reading for GETC and IN #41

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::panic;
use std::{
cmp::Ordering,
i16,
io::{stdout, Write},
io::{stdin, stdout, IsTerminal, Read, Write},
u16, u32, u8, usize,
};

Expand Down Expand Up @@ -270,9 +270,15 @@ impl RunState {
match trap_vect {
// getc
0x20 => {
let cons = Term::stdout();
let c = cons.read_char().unwrap();
*self.reg(0) = c as u16;
if stdin().is_terminal() {
let cons = Term::stdout();
let c = cons.read_char().unwrap();
*self.reg(0) = c as u16;
} else {
let mut buf = [0; 1];
stdin().read_exact(&mut buf).unwrap();
*self.reg(0) = buf[0] as u16;
}
}
// out
0x21 => {
Expand All @@ -299,11 +305,19 @@ impl RunState {
}
// in
0x23 => {
let mut cons = Term::stdout();
let c = cons.read_char().unwrap();
*self.reg(0) = c as u16;
write!(cons, "{c}").unwrap();
cons.flush().unwrap();
if stdin().is_terminal() {
let mut cons = Term::stdout();
let c = cons.read_char().unwrap();
*self.reg(0) = c as u16;
write!(cons, "{c}").unwrap();
cons.flush().unwrap();
} else {
let mut buf = [0; 1];
stdin().read_exact(&mut buf).unwrap();
*self.reg(0) = buf[0] as u16;
print!("{}", buf[0] as char);
stdout().flush().unwrap();
}
}
// putsp
0x24 => {
Expand Down
Loading