mirror of
https://github.com/mre/mos6502.git
synced 2024-11-25 02:33:26 +00:00
Make changes suggested by Clippy and few more
This commit is contained in:
parent
cb2658f3e3
commit
cb8800689f
@ -73,21 +73,19 @@ impl Add<CheckedAddressDiff> 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
|
||||
}
|
||||
}
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user