diff --git a/soft65c02_tester/src/commands.rs b/soft65c02_tester/src/commands.rs index 06e08fb..c7ba16e 100644 --- a/soft65c02_tester/src/commands.rs +++ b/soft65c02_tester/src/commands.rs @@ -1,5 +1,5 @@ use anyhow::anyhow; -use soft65c02_lib::{execute_step, Memory, Registers}; +use soft65c02_lib::{execute_step, AddressableIO, Memory, Registers}; use crate::{until_condition::BooleanExpression, AppResult}; @@ -95,17 +95,31 @@ impl Command for RegisterCommand { pub enum MemoryCommand { Flush, Load(String), - Write(Vec), + Write { address: u16, bytes: Vec }, } impl Command for MemoryCommand { fn execute(&self, _registers: &mut Registers, memory: &mut Memory) -> AppResult> { - match self { - Self::Flush => *memory = Memory::new_with_ram(), + let output = match self { + Self::Flush => { + *memory = Memory::new_with_ram(); + Vec::new() + } + Self::Write { address, bytes } => match bytes.len() { + 0 => vec!["nothing was written".to_string()], + 1 => { + memory.write(*address as usize, bytes)?; + vec!["1 byte written".to_string()] + } + _ => { + memory.write(*address as usize, bytes)?; + vec![format!("{} bytes written", bytes.len())] + } + }, _ => todo!(), }; - Ok(vec![]) + Ok(output) } } @@ -241,6 +255,57 @@ mod memory_command_tests { assert_eq!(vec![0x00, 0x00, 0x00], memory.read(0x000, 3).unwrap()); assert_eq!(0, output.len()); } + + #[test] + fn test_write_command() { + let command = MemoryCommand::Write { + address: 0x1000, + bytes: vec![0x01, 0x02, 0x03], + }; + let mut registers = Registers::new_initialized(0x0000); + let mut memory = Memory::new_with_ram(); + let outputs = command.execute(&mut registers, &mut memory).unwrap(); + + assert_eq!("3 bytes written", (outputs[0])); + assert_eq!( + &[0x01, 0x02, 0x03], + memory.read(0x1000, 3).unwrap().as_slice() + ); + } + + #[test] + fn test_write_no_byte() { + let command = MemoryCommand::Write { + address: 0x1000, + bytes: Vec::new(), + }; + let mut registers = Registers::new_initialized(0x0000); + let mut memory = Memory::new_with_ram(); + let outputs = command.execute(&mut registers, &mut memory).unwrap(); + + assert_eq!("nothing was written", (outputs[0])); + assert_eq!( + &[0x00, 0x00, 0x00], + memory.read(0x1000, 3).unwrap().as_slice() + ); + } + + #[test] + fn test_write_one_byte() { + let command = MemoryCommand::Write { + address: 0x1000, + bytes: vec![0x01], + }; + let mut registers = Registers::new_initialized(0x0000); + let mut memory = Memory::new_with_ram(); + let outputs = command.execute(&mut registers, &mut memory).unwrap(); + + assert_eq!("1 byte written", (outputs[0])); + assert_eq!( + &[0x01, 0x00, 0x00], + memory.read(0x1000, 3).unwrap().as_slice() + ); + } } #[cfg(test)] diff --git a/soft65c02_tester/src/pest_parser.rs b/soft65c02_tester/src/pest_parser.rs index b44879e..00f8bd1 100644 --- a/soft65c02_tester/src/pest_parser.rs +++ b/soft65c02_tester/src/pest_parser.rs @@ -251,11 +251,11 @@ mod cli_command_parser_test { } #[test] - fn test_comment_cli_parser() { - let cli_command = CliCommandParser::from("marker $$This is a comment.$$").unwrap(); + fn test_marker_cli_parser() { + let cli_command = CliCommandParser::from("marker $$This is a marker.$$").unwrap(); assert!( - matches!(cli_command, CliCommand::Marker(comment) if comment == *"This is a comment.") + matches!(cli_command, CliCommand::Marker(comment) if comment == *"This is a marker.") ); }