Skip to content

Commit

Permalink
add memory load command
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmix51 committed Apr 10, 2024
1 parent 236ecdb commit 161ecd3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
37 changes: 35 additions & 2 deletions soft65c02_tester/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fs::File, io::Read, path::PathBuf};

use soft65c02_lib::{execute_step, AddressableIO, LogLine, Memory, Registers};

use crate::{until_condition::BooleanExpression, AppResult};
Expand Down Expand Up @@ -103,7 +105,7 @@ impl Command for RegisterCommand {
#[derive(Debug)]
pub enum MemoryCommand {
Flush,
Load(String),
Load { address: usize, filepath: PathBuf },
Write { address: usize, bytes: Vec<u8> },
}

Expand All @@ -125,7 +127,23 @@ impl Command for MemoryCommand {
vec![format!("{n} bytes written")]
}
},
_ => todo!(),
Self::Load { address, filepath } => {
let vec = {
let mut f = File::open(filepath)?;
let mut buffer: Vec<u8> = vec![];
f.read_to_end(&mut buffer)?;

buffer
};
let buffer = vec;
memory.write(*address, &buffer).unwrap();

vec![format!(
"{} bytes loaded from '{}' at #0x{address:04X}.",
buffer.len(),
filepath.display()
)]
}
};

Ok(OutputToken::Setup(output))
Expand Down Expand Up @@ -317,6 +335,21 @@ mod memory_command_tests {
memory.read(0x1000, 3).unwrap().as_slice()
);
}

#[test]
fn test_load() {
let filepath = PathBuf::new().join("../Cargo.toml");
let command = MemoryCommand::Load {
address: 0x1000,
filepath,
};
let mut registers = Registers::new_initialized(0x0000);
let mut memory = Memory::new_with_ram();
let token = command.execute(&mut registers, &mut memory).unwrap();

let expected = "bytes loaded from '../Cargo.toml' at #0x1000.".to_owned();
assert!(matches!(token, OutputToken::Setup(s) if s[0].contains(&expected)));
}
}

#[cfg(test)]
Expand Down
46 changes: 46 additions & 0 deletions soft65c02_tester/src/pest_parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use anyhow::anyhow;
use pest::{
iterators::{Pair, Pairs},
Expand Down Expand Up @@ -40,6 +42,22 @@ impl MemoryCommandParser {
)?;
MemoryCommand::Write { address, bytes }
}
Rule::memory_load => {
let mut pairs = pair.into_inner();
let address = parse_memory(
&pairs
.next()
.expect("there shall be a memory address argument to memory load")
.as_str()[3..],
)?;
let filename = pairs
.next()
.expect("there shall be a filename argumentto memory load")
.as_str();
let filepath = PathBuf::from(&filename[1..filename.len() - 1]);

MemoryCommand::Load { address, filepath }
}
_ => {
panic!("Unexpected pair '{pair:?}'. memory_{{load,flush,write}} expected.");
}
Expand Down Expand Up @@ -80,6 +98,21 @@ mod memory_command_parser_tests {
matches!(command, MemoryCommand::Write { address, bytes } if address == 0x1234 && bytes == vec![0x01, 0x02, 0x03])
);
}

#[test]
fn test_memory_load() {
let input = "memory load #0x1000 \"script.txt\"";
let pairs = PestParser::parse(Rule::memory_instruction, input)
.unwrap()
.next()
.unwrap()
.into_inner();
let command = MemoryCommandParser::from_pairs(pairs).unwrap();

assert!(
matches!(command, MemoryCommand::Load { address, filepath } if address == 0x1000 && filepath == PathBuf::from("script.txt"))
);
}
}
pub struct RegisterCommandParser;

Expand Down Expand Up @@ -345,6 +378,19 @@ mod cli_command_parser_test {
));
}

#[test]
fn test_memory_load_parser() {
let cli_command = CliCommandParser::from("memory load #0x1234 \"file.test\"").unwrap();

assert!(matches!(
cli_command,
CliCommand::Memory(MemoryCommand::Load {
address,
filepath
}) if address == 0x1234 && filepath == PathBuf::from("file.test")
));
}

#[test]
fn test_code_comments() {
let cli_command = CliCommandParser::from("// This is a comment").unwrap();
Expand Down

0 comments on commit 161ecd3

Please sign in to comment.