From cb8800689f9a8505e6ea6e5ba9563d8541cf8ada Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 6 Oct 2019 23:23:38 +0200 Subject: [PATCH] Make changes suggested by Clippy and few more --- src/address.rs | 12 +++++------- src/memory.rs | 48 +++++++++++++++++++++++++++--------------------- src/registers.rs | 11 ++++------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/address.rs b/src/address.rs index b3a00f4..98a1c19 100644 --- a/src/address.rs +++ b/src/address.rs @@ -73,21 +73,19 @@ impl Add for Address { } impl Address { - pub fn to_u16(&self) -> u16 { - match *self { - Address(address_) => address_, - } + pub fn to_u16(self) -> u16 { + self.0 } - pub fn to_usize(&self) -> usize { + pub fn to_usize(self) -> usize { self.to_u16() as usize } - pub fn get_page_number(&self) -> u8 { + pub fn get_page_number(self) -> u8 { (self.to_u16() & 0xff00 >> 8) as u8 } - pub fn get_offset(&self) -> u8 { + pub fn get_offset(self) -> u8 { (self.to_u16() & 0x00ff) as u8 } } diff --git a/src/memory.rs b/src/memory.rs index 21d3402..7c9596f 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -49,17 +49,11 @@ pub const IRQ_INTERRUPT_VECTOR_HI: Address = Address(0xFFFF); const MEMORY_SIZE: usize = (ADDR_HI_BARE - ADDR_LO_BARE) as usize + 1usize; // FIXME: Should this use indirection for `bytes`? -#[derive(Copy)] +#[derive(Copy, Clone)] pub struct Memory { bytes: [u8; MEMORY_SIZE], } -impl Clone for Memory { - fn clone(&self) -> Self { - *self - } -} - impl Memory { pub fn new() -> Memory { Memory { bytes: [0; MEMORY_SIZE] } @@ -73,11 +67,8 @@ impl Memory { &mut self.bytes[address.to_usize()] } - pub fn get_slice(&self, Address(start): Address, AddressDiff(diff): AddressDiff) -> &[u8] { - let start = start as usize; - let diff = diff as usize; - let end = start + diff; - &self.bytes[start..end] + pub fn get_slice(&self, start: Address, diff: AddressDiff) -> &[u8] { + &self.bytes[start.to_usize()..(start + diff).to_usize()] } // Sets the byte at the given address to the given value and returns the @@ -88,20 +79,35 @@ impl Memory { old_value } - pub fn set_bytes(&mut self, Address(start): Address, values: &[u8]) { - let start = start as usize; + pub fn set_bytes(&mut self, start: Address, values: &[u8]) { + let start = start.to_usize(); // This panics if the range is invalid let end = start + values.len(); - let slice = &mut self.bytes[start..end]; - // JAM: Is this the best way to do this copy? - for (dest, src) in slice.iter_mut().zip(values.iter()) { - *dest = *src; - } + self.bytes[start..end].copy_from_slice(values); } - pub fn is_stack_address(address: &Address) -> bool { - STACK_ADDRESS_LO <= *address && *address <= STACK_ADDRESS_HI + pub fn is_stack_address(address: Address) -> bool { + (STACK_ADDRESS_LO..=STACK_ADDRESS_HI).contains(&address) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_memory_set_bytes() { + let mut memory = Memory::new(); + memory.set_bytes(Address(0x0100), &[1, 2, 3, 4, 5]); + assert_eq!(memory.get_slice(Address(0x00FF), AddressDiff(7)), &[0, 1, 2, 3, 4, 5, 0]); + } + + #[test] + #[should_panic] + fn test_memory_overflow_panic() { + let mut memory = Memory::new(); + memory.set_bytes(Address(0xFFFE), &[1, 2, 3]); } } diff --git a/src/registers.rs b/src/registers.rs index 5bbd76b..e28fe32 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -144,21 +144,18 @@ impl Status { pub struct StackPointer(pub u8); impl StackPointer { - pub fn to_address(&self) -> Address { - let StackPointer(sp) = *self; - STACK_ADDRESS_LO + AddressDiff(i32::from(sp)) + pub fn to_address(self) -> Address { + STACK_ADDRESS_LO + AddressDiff(i32::from(self.0)) } // JAM: FIXME: Should we prevent overflow here? What would a 6502 do? pub fn decrement(&mut self) { - let StackPointer(val) = *self; - *self = StackPointer(val - 1); + self.0 -= 1; } pub fn increment(&mut self) { - let StackPointer(val) = *self; - *self = StackPointer(val + 1); + self.0 += 1; } }