Console now truncates input if it's too long (instead of failing to read)

master
Joey Pollack 1 year ago
parent d27cd2c3e4
commit 5a45347815

@ -1,8 +1,9 @@
@echo off
REM Usage: Drop the win2c64 directory into the same directory with this script REM Usage: Drop the win2c64 directory into the same directory with this script
REM Directory structure should be: ./assemble.bat ./win2c64/win2c64.exe ./bin REM Directory structure should be: ./assemble.bat ./win2c64/win2c64.exe ./bin
@echo off
set src_file=%1 set src_file=%1
echo assembling %src_file% echo assembling %src_file%

@ -1,6 +1,6 @@
; echo example - takes input and then prints it back out ; echo example - takes input and then prints it back out
; This is for testing input of the Simple Test Machine ; This is for testing I/O of the Simple Test Machine
; by Joey Pollack ; by Joey Pollack
; assemble with win2c64 (or lin2c64, or mac2c64) using the -R option ; assemble with win2c64 (or lin2c64, or mac2c64) using the -R option

@ -132,6 +132,9 @@ impl Console
let mut value = bus.read(CONSOLE_FLAGS_ADDR); let mut value = bus.read(CONSOLE_FLAGS_ADDR);
if (value & READ_LINE_FLAG) != 0 if (value & READ_LINE_FLAG) != 0
{ {
// reset the flag
value &= !(READ_LINE_FLAG);
let mut buffer = String::new(); let mut buffer = String::new();
let stdin = io::stdin(); let stdin = io::stdin();
stdin.read_line(&mut buffer).expect("Failed to read input from the console"); stdin.read_line(&mut buffer).expect("Failed to read input from the console");
@ -139,26 +142,27 @@ impl Console
// Make sure the string will fit in the input buffer // Make sure the string will fit in the input buffer
if (buffer.len() + 1) as u16 >= INPUT_BUF_SIZE if (buffer.len() + 1) as u16 >= INPUT_BUF_SIZE
{ {
// reset the read flag and set the overflow flag // The input is too large for the buffer so
value &= !(READ_LINE_FLAG); // set the overflow flag
value |= READ_OVERFLOW_FLAG; value |= READ_OVERFLOW_FLAG;
bus.write(CONSOLE_FLAGS_ADDR, value);
println!("ERROR: Console cannot store input string into memory, string is too large");
return;
} }
// Store input in the input buffer // Store input in the input buffer
for (i, byte) in buffer.chars().enumerate() for (i, byte) in buffer.chars().enumerate()
{ {
// Truncate the input if it's too large for the buffer
if i as u16 >= INPUT_BUF_SIZE
{
break;
}
bus.write(INPUT_BUF_ADDR + (i as u16), byte as u8); bus.write(INPUT_BUF_ADDR + (i as u16), byte as u8);
} }
// Add the null byte to the end // Add the null byte to the end
bus.write(INPUT_BUF_ADDR + buffer.len() as u16, 0); bus.write(INPUT_BUF_ADDR + buffer.len() as u16, 0);
// reset the flag // store the flags back into memory
value &= !(READ_LINE_FLAG);
bus.write(CONSOLE_FLAGS_ADDR, value); bus.write(CONSOLE_FLAGS_ADDR, value);
} }

Loading…
Cancel
Save