Skip to content

Commit

Permalink
add memory::write command
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmix51 committed Apr 7, 2024
1 parent c0ae29a commit e734670
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
75 changes: 70 additions & 5 deletions soft65c02_tester/src/commands.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -95,17 +95,31 @@ impl Command for RegisterCommand {
pub enum MemoryCommand {
Flush,
Load(String),
Write(Vec<u8>),
Write { address: u16, bytes: Vec<u8> },
}

impl Command for MemoryCommand {
fn execute(&self, _registers: &mut Registers, memory: &mut Memory) -> AppResult<Vec<String>> {
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)
}
}

Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions soft65c02_tester/src/pest_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
);
}

Expand Down

0 comments on commit e734670

Please sign in to comment.